Added regedit unit test, a couple minor changes to regedit.
[wine] / msdos / dosconf.c
1 /*
2  * DOS CONFIG.SYS parser
3  *
4  * Copyright 1998 Andreas Mohr
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28
29 #include "winbase.h"
30
31 #include "file.h"
32 #include "miscemu.h"
33 #include "msdos.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(profile);
38
39
40 static int DOSCONF_Device(char **confline);
41 static int DOSCONF_Dos(char **confline);
42 static int DOSCONF_Fcbs(char **confline);
43 static int DOSCONF_Break(char **confline);
44 static int DOSCONF_Files(char **confline);
45 static int DOSCONF_Install(char **confline);
46 static int DOSCONF_Lastdrive(char **confline);
47 static int DOSCONF_Menu(char **confline);
48 static int DOSCONF_Include(char **confline);
49 static int DOSCONF_Country(char **confline);
50 static int DOSCONF_Numlock(char **confline);
51 static int DOSCONF_Switches(char **confline);
52 static int DOSCONF_Shell(char **confline);
53 static int DOSCONF_Stacks(char **confline);
54 static int DOSCONF_Buffers(char **confline);
55 static void DOSCONF_Parse(char *menuname);
56
57 DOSCONF DOSCONF_config =
58 {
59     'E',  /* lastdrive */
60     0,    /* brk_flag */
61     8,    /* files */
62     9,    /* stacks_nr */
63     256,  /* stacks_sz */
64     15,   /* buf */
65     1,    /* buf2 */
66     4,    /* fcbs */
67     0,    /* flags */
68     NULL, /* shell */
69     NULL  /* country */
70 };
71
72 typedef struct {
73     const char *tag_name;
74     int (*tag_handler)(char **p);
75 } TAG_ENTRY;
76
77
78 /*
79  * see
80  * http://egeria.cm.cf.ac.uk/User/P.L.Poulain/project/internal/allinter.htm
81  * or
82  * http://www.csulb.edu/~murdock/dosindex.html
83  */
84
85 static const TAG_ENTRY tag_entries[] =
86 {
87     { ";", NULL },
88     { "REM ", NULL },
89     { "DEVICE", DOSCONF_Device },
90     { "[", DOSCONF_Menu },
91     { "SUBMENU", NULL },
92     { "MENUDEFAULT", DOSCONF_Menu },
93     { "INCLUDE", DOSCONF_Include },
94
95     { "INSTALL", DOSCONF_Install },
96     { "DOS", DOSCONF_Dos },
97     { "FCBS", DOSCONF_Fcbs },
98     { "BREAK", DOSCONF_Break },
99     { "FILES", DOSCONF_Files },
100     { "SHELL", DOSCONF_Shell },
101     { "STACKS", DOSCONF_Stacks },
102     { "BUFFERS", DOSCONF_Buffers },
103     { "COUNTRY", DOSCONF_Country },
104     { "NUMLOCK", DOSCONF_Numlock },
105     { "SWITCHES", DOSCONF_Switches },
106     { "LASTDRIVE", DOSCONF_Lastdrive }
107 };
108
109 static FILE *cfg_fd;
110
111 static char *menu_default = NULL;
112 static int menu_in_listing = 0;         /* we are in the [menu] section */
113 static int menu_skip = 0;                               /* the current menu gets skipped */
114
115
116 static void DOSCONF_skip(char **pconfline)
117 {
118     char *p;
119
120     p = *pconfline;
121     while ( (*p == ' ') || (*p == '\t') ) p++;
122     *pconfline = p;
123 }
124
125 static int DOSCONF_JumpToEntry(char **pconfline, char separator)
126 {
127     char *p;
128
129     p = *pconfline;
130     while ( (*p != separator) && (*p != '\0') ) p++;
131
132     if (*p != separator)
133         return 0;
134     else p++;
135
136     while ( (*p == ' ') || (*p == '\t') ) p++;
137     *pconfline = p;
138     return 1;
139 }
140
141 static int DOSCONF_Device(char **confline)
142 {
143     int loadhigh = 0;
144
145     *confline += 6; /* strlen("DEVICE") */
146     if (!(strncasecmp(*confline, "HIGH", 4)))
147     {
148         loadhigh = 1;
149         *confline += 4;
150         /* FIXME: get DEVICEHIGH parameters if avail ? */
151     }
152     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
153     TRACE("Loading device '%s'\n", *confline);
154 #if 0
155     DOSMOD_LoadDevice(*confline, loadhigh);
156 #endif
157     return 1;
158 }
159
160 static int DOSCONF_Dos(char **confline)
161 {
162     *confline += 3; /* strlen("DOS") */
163     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
164     while (**confline != '\0')
165     {
166         if (!(strncasecmp(*confline, "HIGH", 4)))
167         {
168             DOSCONF_config.flags |= DOSCONF_MEM_HIGH;
169             *confline += 4;
170         }
171         else
172         if (!(strncasecmp(*confline, "UMB", 3)))
173         {
174             DOSCONF_config.flags |= DOSCONF_MEM_UMB;
175             *confline += 3;
176         }
177         else (*confline)++;
178         DOSCONF_JumpToEntry(confline, ',');
179     }
180     TRACE("DOSCONF_Dos: HIGH is %d, UMB is %d\n",
181         (DOSCONF_config.flags & DOSCONF_MEM_HIGH) != 0, (DOSCONF_config.flags & DOSCONF_MEM_UMB) != 0);
182     return 1;
183 }
184
185 static int DOSCONF_Fcbs(char **confline)
186 {
187     *confline += 4; /* strlen("FCBS") */
188     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
189     DOSCONF_config.fcbs = atoi(*confline);
190     if (DOSCONF_config.fcbs > 255)
191     {
192                 MESSAGE("The FCBS value in the config.sys file is too high ! Setting to 255.\n");
193                 DOSCONF_config.fcbs = 255;
194     }
195     TRACE("DOSCONF_Fcbs returning %d\n", DOSCONF_config.fcbs);
196     return 1;
197 }
198
199 static int DOSCONF_Break(char **confline)
200 {
201     *confline += 5; /* strlen("BREAK") */
202     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
203     if (!(strcasecmp(*confline, "ON")))
204         DOSCONF_config.brk_flag = 1;
205     TRACE("BREAK is %d\n", DOSCONF_config.brk_flag);
206     return 1;
207 }
208
209 static int DOSCONF_Files(char **confline)
210 {
211     *confline += 5; /* strlen("FILES") */
212     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
213     DOSCONF_config.files = atoi(*confline);
214     if (DOSCONF_config.files > 255)
215     {
216         MESSAGE("The FILES value in the config.sys file is too high ! Setting to 255.\n");
217         DOSCONF_config.files = 255;
218     }
219     if (DOSCONF_config.files < 8)
220     {
221         MESSAGE("The FILES value in the config.sys file is too low ! Setting to 8.\n");
222         DOSCONF_config.files = 8;
223     }
224     TRACE("DOSCONF_Files returning %d\n", DOSCONF_config.files);
225     return 1;
226 }
227
228 static int DOSCONF_Install(char **confline)
229 {
230 #if 0
231     int loadhigh = 0;
232 #endif
233
234     *confline += 7; /* strlen("INSTALL") */
235     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
236     TRACE("Installing '%s'\n", *confline);
237 #if 0
238     DOSMOD_Install(*confline, loadhigh);
239 #endif
240     return 1;
241 }
242
243 static int DOSCONF_Lastdrive(char **confline)
244 {
245     *confline += 9; /* strlen("LASTDRIVE") */
246     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
247     DOSCONF_config.lastdrive = toupper(**confline);
248     TRACE("Lastdrive %c\n", DOSCONF_config.lastdrive);
249     return 1;
250 }
251
252 static int DOSCONF_Country(char **confline)
253 {
254     *confline += 7; /* strlen("COUNTRY") */
255     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
256     TRACE("Country '%s'\n", *confline);
257     if (DOSCONF_config.country == NULL)
258                 DOSCONF_config.country = malloc(strlen(*confline) + 1);
259     strcpy(DOSCONF_config.country, *confline);
260     return 1;
261 }
262
263 static int DOSCONF_Numlock(char **confline)
264 {
265     *confline += 7; /* strlen("NUMLOCK") */
266     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
267     if (!(strcasecmp(*confline, "ON")))
268         DOSCONF_config.flags |= DOSCONF_NUMLOCK;
269     TRACE("NUMLOCK is %d\n", (DOSCONF_config.flags & DOSCONF_NUMLOCK) != 0);
270     return 1;
271 }
272
273 static int DOSCONF_Switches(char **confline)
274 {
275     char *p;
276
277     *confline += 8; /* strlen("SWITCHES") */
278     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
279     p = strtok(*confline, "/");
280     do
281     {
282         if ( toupper(*p) == 'K')
283             DOSCONF_config.flags |= DOSCONF_KEYB_CONV;
284     }
285     while ((p = strtok(NULL, "/")));
286     TRACE("'Force conventional keyboard' is %d\n",
287                 (DOSCONF_config.flags & DOSCONF_KEYB_CONV) != 0);
288     return 1;
289 }
290
291 static int DOSCONF_Shell(char **confline)
292 {
293     *confline += 5; /* strlen("SHELL") */
294     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
295     TRACE("Shell '%s'\n", *confline);
296     if (DOSCONF_config.shell == NULL)
297                 DOSCONF_config.shell = malloc(strlen(*confline) + 1);
298     strcpy(DOSCONF_config.shell, *confline);
299     return 1;
300 }
301
302 static int DOSCONF_Stacks(char **confline)
303 {
304
305     *confline += 6; /* strlen("STACKS") */
306     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
307     DOSCONF_config.stacks_nr = atoi(strtok(*confline, ","));
308     DOSCONF_config.stacks_sz = atoi((strtok(NULL, ",")));
309     TRACE("%d stacks of size %d\n",
310           DOSCONF_config.stacks_nr, DOSCONF_config.stacks_sz);
311     return 1;
312 }
313
314 static int DOSCONF_Buffers(char **confline)
315 {
316     char *p;
317
318     *confline += 7; /* strlen("BUFFERS") */
319     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
320     p = strtok(*confline, ",");
321     DOSCONF_config.buf = atoi(p);
322     if ((p = strtok(NULL, ",")))
323         DOSCONF_config.buf2 = atoi(p);
324     TRACE("%d primary buffers, %d secondary buffers\n",
325           DOSCONF_config.buf, DOSCONF_config.buf2);
326     return 1;
327 }
328
329 static int DOSCONF_Menu(char **confline)
330 {
331     if (!(strncasecmp(*confline, "[MENU]", 6)))
332         menu_in_listing = 1;
333     else
334     if ((!(strncasecmp(*confline, "[COMMON]", 8)))
335     || (!(strncasecmp(*confline, "[WINE]", 6))))
336         menu_skip = 0;
337     else
338     if (**confline == '[')
339     {
340         (*confline)++;
341         if ((menu_default)
342         && (!(strncasecmp(*confline, menu_default, strlen(menu_default)))))
343     {
344                 free(menu_default);
345                 menu_default = NULL;
346             menu_skip = 0;
347     }
348         else
349             menu_skip = 1;
350         menu_in_listing = 0;
351     }
352     else
353     if (!(strncasecmp(*confline, "menudefault", 11)) && (menu_in_listing))
354     {
355         if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
356     *confline = strtok(*confline, ",");
357     menu_default = malloc(strlen(*confline) + 1);
358         strcpy(menu_default, *confline);
359     }
360     return 1;
361 }
362
363 static int DOSCONF_Include(char **confline)
364 {
365     fpos_t oldpos;
366     char *temp;
367
368     *confline += 7; /* strlen("INCLUDE") */
369     if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
370     fgetpos(cfg_fd, &oldpos);
371     fseek(cfg_fd, 0, SEEK_SET);
372     TRACE("Including menu '%s'\n", *confline);
373     temp = malloc(strlen(*confline) + 1);
374     strcpy(temp, *confline);
375     DOSCONF_Parse(temp);
376         free(temp);
377     fsetpos(cfg_fd, &oldpos);
378     return 1;
379 }
380
381 static void DOSCONF_Parse(char *menuname)
382 {
383    char confline[256];
384    char *p, *trail;
385    int i;
386
387     if (menuname != NULL) /* we need to jump to a certain sub menu */
388     {
389         while (fgets(confline, 255, cfg_fd))
390         {
391              p = confline;
392              DOSCONF_skip(&p);
393              if (*p == '[')
394              {
395                 p++;
396                 if (!(trail = strrchr(p, ']')))
397                     return;
398                 if (!(strncasecmp(p, menuname, (int)trail - (int)p)))
399                     break;
400              }
401         }
402     }
403
404     while (fgets(confline, 255, cfg_fd))
405     {
406         p = confline;
407         DOSCONF_skip(&p);
408
409         if ((menuname) && (*p == '['))
410             /* we were handling a specific sub menu, but now next menu begins */
411             break;
412
413         if ((trail = strrchr(confline, '\n')))
414                 *trail = '\0';
415         if ((trail = strrchr(confline, '\r')))
416                 *trail = '\0';
417         if (!(menu_skip))
418         {
419             for (i = 0; i < sizeof(tag_entries) / sizeof(TAG_ENTRY); i++)
420                 if (!(strncasecmp(p, tag_entries[i].tag_name,
421                    strlen(tag_entries[i].tag_name))))
422                 {
423                     TRACE("tag '%s'\n", tag_entries[i].tag_name);
424                     if (tag_entries[i].tag_handler != NULL)
425                             tag_entries[i].tag_handler(&p);
426                         break;
427                 }
428         }
429         else /* the current menu gets skipped */
430         DOSCONF_Menu(&p);
431     }
432 }
433
434 int DOSCONF_ReadConfig(void)
435 {
436     char buffer[256];
437     DOS_FULL_NAME fullname;
438     char *filename, *menuname;
439     int ret = 1;
440
441     PROFILE_GetWineIniString( "wine", "config.sys", "", buffer, sizeof(buffer) );
442     if (!(filename = strtok(buffer, ","))) return ret;
443     menuname = strtok(NULL,   ",");
444
445     DOSFS_GetFullName(filename, FALSE, &fullname);
446     if (menuname) menu_default = strdup(menuname);
447     if ((cfg_fd = fopen(fullname.long_name, "r")))
448     {
449         DOSCONF_Parse(NULL);
450         fclose(cfg_fd);
451     }
452     else
453     {
454         MESSAGE("Couldn't open config.sys file given as \"%s\" in" \
455             " wine.conf or .winerc, section [wine] !\n", filename);
456         ret = 0;
457     }
458     if (menu_default) free(menu_default);
459     return ret;
460 }