Merge branch 'vendor/OPENSSL'
[dragonfly.git] / usr.bin / doscmd / config.c
1 /*
2  * Copyright (c) 1992, 1993, 1996
3  *      Berkeley Software Design, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Berkeley Software
16  *      Design, Inc.
17  *
18  * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      BSDI config.c,v 2.2 1996/04/08 19:32:22 bostic Exp
31  *
32  * $FreeBSD: src/usr.bin/doscmd/config.c,v 1.4.2.1 2002/04/25 11:04:50 tg Exp $
33  */
34
35 #include <sys/types.h>
36 #include <sys/uio.h>
37 #include <ctype.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 #include "doscmd.h"
43 #include "com.h"
44 #include "cwd.h"
45 #include "tty.h"
46 #include "video.h"
47
48 /*
49 ** doscmdrc parser
50 */
51 int
52 read_config(FILE *fp)
53 {
54     char *buffer;
55     char _buffer[1024];
56     char *_av[16];
57     char **av;
58     int ac;
59     int bootdrive = -1;
60
61     while ((buffer = fgets(_buffer, sizeof(_buffer), fp)) != NULL) {
62         char *comment = strchr(buffer, '#');
63         char *equal;
64
65         if (comment)
66             *comment = 0;
67
68         while (isspace(*buffer))
69             ++buffer;
70         if (!*buffer)
71             continue;
72
73         /*
74          * Strip <CR><LF>
75          */
76         comment = buffer;
77         while (*comment && *comment != '\n' && *comment != '\r')
78                 ++comment;
79         *comment = 0;
80
81         /*
82          * Check to see if this is to go in the environment
83          */
84         equal = buffer;
85         while (*equal && *equal != '=' && !isspace(*equal))
86             ++equal;
87
88         if (*equal == '=') {
89             if (strncmp(buffer, "MS_VERSION=", 11) == 0)
90                 setver(0, strtol(equal + 1, 0, 0));
91             else if (strncmp(buffer, "X11_FONT=", 9) == 0)
92                 xfont = strdup(equal + 1);
93             else
94                 put_dosenv(buffer);
95             continue;
96         }
97
98         ac = ParseBuffer(buffer, av = _av, 16);
99
100         if (ac == 0)
101             continue;
102         if (!strcasecmp(av[0], "assign")) {
103             int drive = -1;
104             int printer;
105             int ro = 0;
106
107             if (ac < 2) {
108                 fprintf(stderr, "Usage: assign device ...\n");
109                 quit(1);
110             }
111             if (av[2] && !strcasecmp(av[2], "-ro")) {
112                 av[2] = av[1];
113                 av[1] = av[0];
114                 ++av;
115                 --ac;
116                 ro = 1;
117             }
118             if (!strncasecmp(av[1], "lpt", 3)) {
119                 if (av[1][3] < '1' || av[1][3] > '4'
120                                    || av[1][4] != ':' || ac < 3) {
121                     fprintf(stderr, "Usage: assign lptn: [direct] lpr-name [ time-out]\n");
122                     quit(1);
123                 }
124                 printer = av[1][3] - '1';
125                 if (strchr(av[2], '/')) {
126                     printer_direct(printer);
127                     printer_spool(printer, av[2]);
128                 } else if (!strcasecmp(av[2], "direct")) {
129                     printer_direct(printer);
130                     printer_spool(printer, 0);
131                 } else {
132                     printer_spool(printer, av[2]);
133                     if (ac == 4)
134                         printer_timeout(printer, av[3]);
135                 }
136             } else if (!strncasecmp(av[1], "flop", 4)) {
137                 if (ac != 4) {
138                     fprintf(stderr, "Usage: assign flop [-ro] file type\n");
139                     quit(1);
140                 }
141
142                 if (isdigit(av[1][4])) {
143                     drive = atoi(&av[1][4]) - 1;
144                 } else if (isalpha(av[1][4]) && av[1][5] == ':' && !av[1][6]) {
145                     drive = drlton(av[1][4]);
146                 }
147 init_soft:
148                 drive = init_floppy(drive, atoi(av[3]), av[2]);
149                 if (ro)
150                     make_readonly(drive);
151             } else if (!strncasecmp(av[1], "hard", 4)) {
152                 int cyl, head, sec;
153
154                 if (isdigit(av[1][4])) {
155                     drive = atoi(&av[1][4]) + 1;
156                 } else if (isalpha(av[1][4]) && av[1][5] == ':' && !av[1][6]) {
157                     drive = drlton(av[1][4]);
158                 }
159
160 init_hard:
161                 switch (ac) {
162                 default:
163                     fprintf(stderr, "Usage: assign [A-Z]: [-ro] directory\n"
164                                     "       assign hard [-ro] file type [boot_sector]\n"
165                                     "       assign hard [-ro] file cylinders heads sectors/track [boot_sector]\n");
166                     quit(1);
167                 case 5:
168                 case 4:
169                     if (!map_type(atoi(av[3]), &cyl, &head, &sec)) {
170                         fprintf(stderr, "%s: invalid type\n", av[3]);
171                         quit(1);
172                     }
173                     drive = init_hdisk(drive, cyl, head, sec, av[2], av[4]);
174                     if (ro)
175                         make_readonly(drive);
176                     break;
177                 case 7:
178                 case 6:
179                     drive = init_hdisk(drive, atoi(av[3]), atoi(av[4]), atoi(av[5]),
180                                av[2], av[6]);
181                     if (ro)
182                         make_readonly(drive);
183                     break;
184                 }
185             } else if (av[1][1] == ':') {
186                 if (av[1][2] || !isalpha(av[1][0])) {
187                     fprintf(stderr, "Usage: assign [A-Z]: ...\n");
188                     quit(1);
189                 }
190                 drive = drlton(av[1][0]);
191
192                 if (ac == 3) {
193                     init_path(drive, (u_char *)av[2], 0);
194                     if (ro)
195                         dos_makereadonly(drive);
196                 } else if (drive < 2)
197                     goto init_soft;
198                 else
199                     goto init_hard;
200             } else if (!strncasecmp(av[1], "com", 3)) {
201                 int port;
202                 int addr;
203                 unsigned char irq;
204  
205                 if ((ac != 5) || (!isdigit(av[1][3]))) {
206                     fprintf(stderr, "Usage: assign com[1-4] path addr irq\n");
207                     quit(1);
208                 }
209                 port = atoi(&av[1][3]) - 1;
210                 if ((port < 0) || (port > (N_COMS_MAX - 1))) {
211                     fprintf(stderr, "Usage: assign com[1-4] path addr irq\n");
212                     quit(1);
213                 }
214                 errno = 0;
215                 addr = (int)strtol(av[3], '\0', 0);
216                 /* XXX DEBUG ISA-specific */
217                 if ((errno != 0) || (addr > MAXPORT)) {
218                     fprintf(stderr, "Usage: assign com[1-4] path addr irq\n");
219                     quit(1);
220                 }
221                 errno = 0;
222                 irq = (unsigned char)strtol(av[4], '\0', 0);
223                 /* XXX DEBUG ISA-specific */
224                 if ((errno != 0) || (irq < 2) || (irq > 7)) {
225                     fprintf(stderr, "Usage: assign com[1-4] path addr irq[2-7]\n");
226                     quit(1);
227                 }
228                 init_com(port, av[2], addr, irq);
229             } else {
230                 fprintf(stderr, "Usage: assign flop ...\n");
231                 fprintf(stderr, "       assign hard ...\n");
232                 fprintf(stderr, "       assign [A-Z]: ...\n");
233                 fprintf(stderr, "       assign comX ...\n");
234                 quit(1);
235             }
236         } else if (!strcasecmp(av[0], "boot")) {
237             if (ac != 2 || av[1][2] || !isalpha(av[1][0])) {
238                 fprintf(stderr, "Usage: boot [A: | C:]\n");
239                 quit(1);
240             }
241             bootdrive = drlton(av[1][0]);
242             if (bootdrive != 0 && bootdrive != 2) {
243                 fprintf(stderr, "Boot drive must be either A: or C:\n");
244                 quit(1);
245             }
246         } else if (!strcasecmp(av[0], "portmap")) {
247             int p, c;
248             if (ac < 2 || ac > 3 || !isdigit(av[1][0]) ||
249                     (ac == 3 && !isdigit(av[2][0]))) {
250                 fprintf(stderr, "Usage: portmap port [count]\n");
251                 quit(1);
252             }
253             p = strtol(av[1], 0, 0);
254             c = (ac == 3) ? strtol(av[2], 0, 0) : 1;
255             iomap_port(p, c);
256
257             while (c-- > 0) {
258                 define_input_port_handler(p++, inb_port);
259                 define_output_port_handler(p++, outb_port);
260             }
261         } else if (!strcasecmp(av[0], "setver")) {
262             int v = 0;
263             if (ac != 3 || !(v = strtol(av[2], 0, 0))) {
264                 fprintf(stderr, "Usage: setver command version\n");
265                 quit(1);
266             }
267             setver(av[1], v);
268         } else {
269             fprintf(stderr, "%s: invalid command\n", av[0]);
270             quit(1);
271         }
272     }
273     fclose(fp);
274     return(bootdrive);
275 }