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