Merge branch 'vendor/LIBARCHIVE'
[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  * $DragonFly: src/usr.bin/doscmd/config.c,v 1.2 2003/06/17 04:29:25 dillon Exp $
34  */
35
36 #include <sys/types.h>
37 #include <sys/uio.h>
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include "doscmd.h"
44 #include "com.h"
45 #include "cwd.h"
46 #include "tty.h"
47 #include "video.h"
48
49 /*
50 ** doscmdrc parser
51 */
52 int
53 read_config(FILE *fp)
54 {
55     char *buffer;
56     char _buffer[1024];
57     char *_av[16];
58     char **av;
59     int ac;
60     int bootdrive = -1;
61
62     while ((buffer = fgets(_buffer, sizeof(_buffer), fp)) != 0) {
63         char *comment = strchr(buffer, '#');
64         char *equal;
65
66         if (comment)
67             *comment = 0;
68
69         while (isspace(*buffer))
70             ++buffer;
71         if (!*buffer)
72             continue;
73
74         /*
75          * Strip <CR><LF>
76          */
77         comment = buffer;
78         while (*comment && *comment != '\n' && *comment != '\r')
79                 ++comment;
80         *comment = 0;
81
82         /*
83          * Check to see if this is to go in the environment
84          */
85         equal = buffer;
86         while (*equal && *equal != '=' && !isspace(*equal))
87             ++equal;
88
89         if (*equal == '=') {
90             if (strncmp(buffer, "MS_VERSION=", 11) == 0)
91                 setver(0, strtol(equal + 1, 0, 0));
92             else if (strncmp(buffer, "X11_FONT=", 9) == 0)
93                 xfont = strdup(equal + 1);
94             else
95                 put_dosenv(buffer);
96             continue;
97         }
98
99         ac = ParseBuffer(buffer, av = _av, 16);
100
101         if (ac == 0)
102             continue;
103         if (!strcasecmp(av[0], "assign")) {
104             int drive = -1;
105             int printer;
106             int ro = 0;
107
108             if (ac < 2) {
109                 fprintf(stderr, "Usage: assign device ...\n");
110                 quit(1);
111             }
112             if (av[2] && !strcasecmp(av[2], "-ro")) {
113                 av[2] = av[1];
114                 av[1] = av[0];
115                 ++av;
116                 --ac;
117                 ro = 1;
118             }
119             if (!strncasecmp(av[1], "lpt", 3)) {
120                 if (av[1][3] < '1' || av[1][3] > '4'
121                                    || av[1][4] != ':' || ac < 3) {
122                     fprintf(stderr, "Usage: assign lptn: [direct] lpr-name [ time-out]\n");
123                     quit(1);
124                 }
125                 printer = av[1][3] - '1';
126                 if (strchr(av[2], '/')) {
127                     printer_direct(printer);
128                     printer_spool(printer, av[2]);
129                 } else if (!strcasecmp(av[2], "direct")) {
130                     printer_direct(printer);
131                     printer_spool(printer, 0);
132                 } else {
133                     printer_spool(printer, av[2]);
134                     if (ac == 4)
135                         printer_timeout(printer, av[3]);
136                 }
137             } else if (!strncasecmp(av[1], "flop", 4)) {
138                 if (ac != 4) {
139                     fprintf(stderr, "Usage: assign flop [-ro] file type\n");
140                     quit(1);
141                 }
142
143                 if (isdigit(av[1][4])) {
144                     drive = atoi(&av[1][4]) - 1;
145                 } else if (isalpha(av[1][4]) && av[1][5] == ':' && !av[1][6]) {
146                     drive = drlton(av[1][4]);
147                 }
148 init_soft:
149                 drive = init_floppy(drive, atoi(av[3]), av[2]);
150                 if (ro)
151                     make_readonly(drive);
152             } else if (!strncasecmp(av[1], "hard", 4)) {
153                 int cyl, head, sec;
154
155                 if (isdigit(av[1][4])) {
156                     drive = atoi(&av[1][4]) + 1;
157                 } else if (isalpha(av[1][4]) && av[1][5] == ':' && !av[1][6]) {
158                     drive = drlton(av[1][4]);
159                 }
160
161 init_hard:
162                 switch (ac) {
163                 default:
164                     fprintf(stderr, "Usage: assign [A-Z]: [-ro] directory\n"
165                                     "       assign hard [-ro] file type [boot_sector]\n"
166                                     "       assign hard [-ro] file cylinders heads sectors/track [boot_sector]\n");
167                     quit(1);
168                 case 5:
169                 case 4:
170                     if (!map_type(atoi(av[3]), &cyl, &head, &sec)) {
171                         fprintf(stderr, "%s: invalid type\n", av[3]);
172                         quit(1);
173                     }
174                     drive = init_hdisk(drive, cyl, head, sec, av[2], av[4]);
175                     if (ro)
176                         make_readonly(drive);
177                     break;
178                 case 7:
179                 case 6:
180                     drive = init_hdisk(drive, atoi(av[3]), atoi(av[4]), atoi(av[5]),
181                                av[2], av[6]);
182                     if (ro)
183                         make_readonly(drive);
184                     break;
185                 }
186             } else if (av[1][1] == ':') {
187                 if (av[1][2] || !isalpha(av[1][0])) {
188                     fprintf(stderr, "Usage: assign [A-Z]: ...\n");
189                     quit(1);
190                 }
191                 drive = drlton(av[1][0]);
192
193                 if (ac == 3) {
194                     init_path(drive, (u_char *)av[2], 0);
195                     if (ro)
196                         dos_makereadonly(drive);
197                 } else if (drive < 2)
198                     goto init_soft;
199                 else
200                     goto init_hard;
201             } else if (!strncasecmp(av[1], "com", 3)) {
202                 int port;
203                 int addr;
204                 unsigned char irq;
205  
206                 if ((ac != 5) || (!isdigit(av[1][3]))) {
207                     fprintf(stderr, "Usage: assign com[1-4] path addr irq\n");
208                     quit(1);
209                 }
210                 port = atoi(&av[1][3]) - 1;
211                 if ((port < 0) || (port > (N_COMS_MAX - 1))) {
212                     fprintf(stderr, "Usage: assign com[1-4] path addr irq\n");
213                     quit(1);
214                 }
215                 errno = 0;
216                 addr = (int)strtol(av[3], '\0', 0);
217                 /* XXX DEBUG ISA-specific */
218                 if ((errno != 0) || (addr > MAXPORT)) {
219                     fprintf(stderr, "Usage: assign com[1-4] path addr irq\n");
220                     quit(1);
221                 }
222                 errno = 0;
223                 irq = (unsigned char)strtol(av[4], '\0', 0);
224                 /* XXX DEBUG ISA-specific */
225                 if ((errno != 0) || (irq < 2) || (irq > 7)) {
226                     fprintf(stderr, "Usage: assign com[1-4] path addr irq[2-7]\n");
227                     quit(1);
228                 }
229                 init_com(port, av[2], addr, irq);
230             } else {
231                 fprintf(stderr, "Usage: assign flop ...\n");
232                 fprintf(stderr, "       assign hard ...\n");
233                 fprintf(stderr, "       assign [A-Z]: ...\n");
234                 fprintf(stderr, "       assign comX ...\n");
235                 quit(1);
236             }
237         } else if (!strcasecmp(av[0], "boot")) {
238             if (ac != 2 || av[1][2] || !isalpha(av[1][0])) {
239                 fprintf(stderr, "Usage: boot [A: | C:]\n");
240                 quit(1);
241             }
242             bootdrive = drlton(av[1][0]);
243             if (bootdrive != 0 && bootdrive != 2) {
244                 fprintf(stderr, "Boot drive must be either A: or C:\n");
245                 quit(1);
246             }
247         } else if (!strcasecmp(av[0], "portmap")) {
248             int p, c;
249             if (ac < 2 || ac > 3 || !isdigit(av[1][0]) ||
250                     (ac == 3 && !isdigit(av[2][0]))) {
251                 fprintf(stderr, "Usage: portmap port [count]\n");
252                 quit(1);
253             }
254             p = strtol(av[1], 0, 0);
255             c = (ac == 3) ? strtol(av[2], 0, 0) : 1;
256             iomap_port(p, c);
257
258             while (c-- > 0) {
259                 define_input_port_handler(p++, inb_port);
260                 define_output_port_handler(p++, outb_port);
261             }
262         } else if (!strcasecmp(av[0], "setver")) {
263             int v = 0;
264             if (ac != 3 || !(v = strtol(av[2], 0, 0))) {
265                 fprintf(stderr, "Usage: setver command version\n");
266                 quit(1);
267             }
268             setver(av[1], v);
269         } else {
270             fprintf(stderr, "%s: invalid command\n", av[0]);
271             quit(1);
272         }
273     }
274     fclose(fp);
275     return(bootdrive);
276 }