summaryrefslogtreecommitdiffstats
path: root/usr.bin/doscmd/config.c
blob: c6463d87566e9c7a570b88f051a1053e06417c16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Copyright (c) 1992, 1993, 1996
 *	Berkeley Software Design, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Berkeley Software
 *	Design, Inc.
 *
 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	BSDI config.c,v 2.2 1996/04/08 19:32:22 bostic Exp
 *
 * $Id: config.c,v 1.2 1996/09/18 16:12:24 miff Exp $
 */

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>

#include "doscmd.h"

/*
** doscmdrc parser
*/
int
read_config(FILE *fp)
{
    char *buffer;
    char _buffer[1024];
    char *_av[16];
    char **av;
    int ac;
    int bootdrive = -1;

    while (buffer = fgets(_buffer, sizeof(_buffer), fp)) {
	char *comment = strchr(buffer, '#');
	char *equal;

    	if (comment)
	    *comment = 0;

    	while (isspace(*buffer))
	    ++buffer;
    	if (!*buffer)
	    continue;

	/*
	 * Strip <CR><LF>
	 */
    	comment = buffer;
    	while (*comment && *comment != '\n' && *comment != '\r')
		++comment;
    	*comment = 0;

	/*
	 * Check to see if this is to go in the environment
	 */
    	equal = buffer;
    	while (*equal && *equal != '=' && !isspace(*equal))
	    ++equal;

    	if (*equal == '=') {
	    if (strncmp(buffer, "MS_VERSION=", 11) == 0)
	    	setver(0, strtol(equal + 1, 0, 0));
	    else if (strncmp(buffer, "X11_FONT=", 9) == 0)
		xfont = strdup(equal + 1);
	    else
		put_dosenv(buffer);
	    continue;
    	}

    	ac = ParseBuffer(buffer, av = _av, 16);

    	if (ac == 0)
	    continue;
    	if (!strcasecmp(av[0], "assign")) {
	    int drive = -1;
    	    int printer;
    	    int ro = 0;

    	    if (ac < 2) {
		fprintf(stderr, "Usage: assign device ...\n");
		quit(1);
	    }
	    if (av[2] && !strcasecmp(av[2], "-ro")) {
		av[2] = av[1];
		av[1] = av[0];
		++av;
		--ac;
		ro = 1;
	    }
	    if (!strncasecmp(av[1], "lpt", 3)) {
		if (av[1][3] < '1' || av[1][3] > '4'
				   || av[1][4] != ':' || ac < 3) {
		    fprintf(stderr, "Usage: assign lptn: [direct] lpr-name [ time-out]\n");
		    quit(1);
		}
		printer = av[1][3] - '1';
		if (strchr(av[2], '/')) {
		    printer_direct(printer);
		    printer_spool(printer, av[2]);
		} else if (!strcasecmp(av[2], "direct")) {
		    printer_direct(printer);
		    printer_spool(printer, 0);
		} else {
		    printer_spool(printer, av[2]);
		    if (ac == 4)
			printer_timeout(printer, av[3]);
		}
	    } else if (!strncasecmp(av[1], "flop", 4)) {
		if (ac != 4) {
		    fprintf(stderr, "Usage: assign flop [-ro] file type\n");
		    quit(1);
		}

    	    	if (isdigit(av[1][4])) {
		    drive = atoi(&av[1][4]) - 1;
		} else if (islower(av[1][4]) && av[1][5] == ':' && !av[1][6]) {
		    drive = av[1][4] - 'a';
		} else if (isupper(av[1][4]) && av[1][5] == ':' && !av[1][6]) {
		    drive = av[1][4] - 'A';
		}
init_soft:
		drive = init_floppy(drive, atoi(av[3]), av[2]);
    	    	if (ro)
    	    	    make_readonly(drive);
    	    } else if (!strncasecmp(av[1], "hard", 4)) {
		int cyl, head, sec;

    	    	if (isdigit(av[1][4])) {
		    drive = atoi(&av[1][4]) + 1;
		} else if (islower(av[1][4]) && av[1][5] == ':' && !av[1][6]) {
		    drive = av[1][4] - 'a';
		} else if (isupper(av[1][4]) && av[1][5] == ':' && !av[1][6]) {
		    drive = av[1][4] - 'A';
		}

init_hard:
		switch (ac) {
		default:
		    fprintf(stderr, "Usage: assign [A-Z]: [-ro] directory\n"
			            "       assign hard [-ro] file type [boot_sector]\n"
		    		    "       assign hard [-ro] file cylinders heads sectors/track [boot_sector]\n");
		    quit(1);
    	    	case 5:
    	    	case 4:
		    if (!map_type(atoi(av[3]), &cyl, &head, &sec)) {
			fprintf(stderr, "%s: invalid type\n", av[3]);
		    	quit(1);
		    }
    	    	    drive = init_hdisk(drive, cyl, head, sec, av[2], av[4]);
		    if (ro)
			make_readonly(drive);
		    break;
    	    	case 7:
    	    	case 6:
    	    	    drive = init_hdisk(drive, atoi(av[3]), atoi(av[4]), atoi(av[5]),
			       av[2], av[6]);
		    if (ro)
			make_readonly(drive);
		    break;
		}
	    } else if (av[1][1] == ':') {
		if (av[1][2] || !isalpha(av[1][0])) {
		    fprintf(stderr, "Usage: assign [A-Z]: ...\n");
		    quit(1);
		}
    	    	if (isupper(av[1][0]))
		    drive = av[1][0] - 'A';
		else
		    drive = av[1][0] - 'a';

    	    	if (ac == 3) {
		    init_path(drive, (u_char *)av[2], 0);
		    if (ro)
		    	dos_makereadonly(drive);
    	    	} else if (drive < 2)
		    goto init_soft;
    	    	else
		    goto init_hard;
            } else if (!strncasecmp(av[1], "com", 3)) {
                int port;
                int addr;
                unsigned char irq;
                int i;
 
                if ((ac != 5) || (!isdigit(av[1][3]))) {
                    fprintf(stderr, "Usage: assign com[1-4] path addr irq\n");
                    quit(1);
                }
                port = atoi(&av[1][3]) - 1;
                if ((port < 0) || (port > (N_COMS_MAX - 1))) {
                    fprintf(stderr, "Usage: assign com[1-4] path addr irq\n");
                    quit(1);
                }
                errno = 0;
                addr = (int)strtol(av[3], '\0', 0);
                /* XXX DEBUG ISA-specific */
                if ((errno != 0) || (addr > MAXPORT)) {
                    fprintf(stderr, "Usage: assign com[1-4] path addr irq\n");
                    quit(1);
                }
                errno = 0;
                irq = (unsigned char)strtol(av[4], '\0', 0);
                /* XXX DEBUG ISA-specific */
                if ((errno != 0) || (irq < 1) || (irq > 15)) {
                    fprintf(stderr, "Usage: assign com[1-4] path addr irq\n");
                    quit(1);
                }
                init_com(port, av[2], addr, irq);
	    } else {
		fprintf(stderr, "Usage: assign flop ...\n");
		fprintf(stderr, "       assign hard ...\n");
		fprintf(stderr, "       assign [A-Z]: ...\n");
		fprintf(stderr, "       assign comX ...\n");
		quit(1);
	    }
    	} else if (!strcasecmp(av[0], "boot")) {
	    if (ac != 2 || av[1][2] || !isalpha(av[1][0])) {
		fprintf(stderr, "Usage: boot [A: | C:]\n");
		quit(1);
	    }
	    if (isupper(av[1][0]))
		bootdrive = av[1][0] - 'A';
	    else
		bootdrive = av[1][0] - 'a';
	    if (bootdrive != 0 && bootdrive != 2) {
	    	fprintf(stderr, "Boot drive must be either A: or C:\n");
		quit(1);
	    }
    	} else if (!strcasecmp(av[0], "setver")) {
	    int v;
	    if (ac != 3 || !(v = strtol(av[2], 0, 0))) {
		fprintf(stderr, "Usage: setver command version\n");
		quit(1);
	    }
    	    setver(av[1], v);
	} else {
	    fprintf(stderr, "%s: invalid command\n", av[0]);
	    quit(1);
    	}
    }
    fclose(fp);
    return(bootdrive);
}
OpenPOWER on IntegriCloud