Restored MakeCriticalSectionGlobal that got lost somewhere.
[wine/multimedia.git] / msdos / dosconf.c
blob961271108463b080580ddabd0780916f75a418da
1 /*
2 * DOS CONFIG.SYS parser
4 * Copyright 1998 Andreas Mohr
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.
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.
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
21 #include "config.h"
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <ctype.h>
29 #include "winbase.h"
31 #include "file.h"
32 #include "miscemu.h"
33 #include "msdos.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(profile);
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);
57 DOSCONF DOSCONF_config =
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 */
72 typedef struct {
73 const char *tag_name;
74 int (*tag_handler)(char **p);
75 } TAG_ENTRY;
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
85 static const TAG_ENTRY tag_entries[] =
87 { ";", NULL },
88 { "REM ", NULL },
89 { "DEVICE", DOSCONF_Device },
90 { "[", DOSCONF_Menu },
91 { "SUBMENU", NULL },
92 { "MENUDEFAULT", DOSCONF_Menu },
93 { "INCLUDE", DOSCONF_Include },
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 }
109 static FILE *cfg_fd;
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 */
116 static void DOSCONF_skip(char **pconfline)
118 char *p;
120 p = *pconfline;
121 while ( (*p == ' ') || (*p == '\t') ) p++;
122 *pconfline = p;
125 static int DOSCONF_JumpToEntry(char **pconfline, char separator)
127 char *p;
129 p = *pconfline;
130 while ( (*p != separator) && (*p != '\0') ) p++;
132 if (*p != separator)
133 return 0;
134 else p++;
136 while ( (*p == ' ') || (*p == '\t') ) p++;
137 *pconfline = p;
138 return 1;
141 static int DOSCONF_Device(char **confline)
143 int loadhigh = 0;
145 *confline += 6; /* strlen("DEVICE") */
146 if (!(strncasecmp(*confline, "HIGH", 4)))
148 loadhigh = 1;
149 *confline += 4;
150 /* FIXME: get DEVICEHIGH parameters if avail ? */
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;
160 static int DOSCONF_Dos(char **confline)
162 *confline += 3; /* strlen("DOS") */
163 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
164 while (**confline != '\0')
166 if (!(strncasecmp(*confline, "HIGH", 4)))
168 DOSCONF_config.flags |= DOSCONF_MEM_HIGH;
169 *confline += 4;
171 else
172 if (!(strncasecmp(*confline, "UMB", 3)))
174 DOSCONF_config.flags |= DOSCONF_MEM_UMB;
175 *confline += 3;
177 else (*confline)++;
178 DOSCONF_JumpToEntry(confline, ',');
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;
185 static int DOSCONF_Fcbs(char **confline)
187 *confline += 4; /* strlen("FCBS") */
188 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
189 DOSCONF_config.fcbs = atoi(*confline);
190 if (DOSCONF_config.fcbs > 255)
192 MESSAGE("The FCBS value in the config.sys file is too high ! Setting to 255.\n");
193 DOSCONF_config.fcbs = 255;
195 TRACE("DOSCONF_Fcbs returning %d\n", DOSCONF_config.fcbs);
196 return 1;
199 static int DOSCONF_Break(char **confline)
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;
209 static int DOSCONF_Files(char **confline)
211 *confline += 5; /* strlen("FILES") */
212 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
213 DOSCONF_config.files = atoi(*confline);
214 if (DOSCONF_config.files > 255)
216 MESSAGE("The FILES value in the config.sys file is too high ! Setting to 255.\n");
217 DOSCONF_config.files = 255;
219 if (DOSCONF_config.files < 8)
221 MESSAGE("The FILES value in the config.sys file is too low ! Setting to 8.\n");
222 DOSCONF_config.files = 8;
224 TRACE("DOSCONF_Files returning %d\n", DOSCONF_config.files);
225 return 1;
228 static int DOSCONF_Install(char **confline)
230 #if 0
231 int loadhigh = 0;
232 #endif
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;
243 static int DOSCONF_Lastdrive(char **confline)
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;
252 static int DOSCONF_Country(char **confline)
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;
263 static int DOSCONF_Numlock(char **confline)
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;
273 static int DOSCONF_Switches(char **confline)
275 char *p;
277 *confline += 8; /* strlen("SWITCHES") */
278 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
279 p = strtok(*confline, "/");
282 if ( toupper(*p) == 'K')
283 DOSCONF_config.flags |= DOSCONF_KEYB_CONV;
285 while ((p = strtok(NULL, "/")));
286 TRACE("'Force conventional keyboard' is %d\n",
287 (DOSCONF_config.flags & DOSCONF_KEYB_CONV) != 0);
288 return 1;
291 static int DOSCONF_Shell(char **confline)
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;
302 static int DOSCONF_Stacks(char **confline)
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;
314 static int DOSCONF_Buffers(char **confline)
316 char *p;
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;
329 static int DOSCONF_Menu(char **confline)
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 == '[')
340 (*confline)++;
341 if ((menu_default)
342 && (!(strncasecmp(*confline, menu_default, strlen(menu_default)))))
344 free(menu_default);
345 menu_default = NULL;
346 menu_skip = 0;
348 else
349 menu_skip = 1;
350 menu_in_listing = 0;
352 else
353 if (!(strncasecmp(*confline, "menudefault", 11)) && (menu_in_listing))
355 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
356 *confline = strtok(*confline, ",");
357 menu_default = malloc(strlen(*confline) + 1);
358 strcpy(menu_default, *confline);
360 return 1;
363 static int DOSCONF_Include(char **confline)
365 fpos_t oldpos;
366 char *temp;
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;
381 static void DOSCONF_Parse(char *menuname)
383 char confline[256];
384 char *p, *trail;
385 int i;
387 if (menuname != NULL) /* we need to jump to a certain sub menu */
389 while (fgets(confline, 255, cfg_fd))
391 p = confline;
392 DOSCONF_skip(&p);
393 if (*p == '[')
395 p++;
396 if (!(trail = strrchr(p, ']')))
397 return;
398 if (!(strncasecmp(p, menuname, (int)trail - (int)p)))
399 break;
404 while (fgets(confline, 255, cfg_fd))
406 p = confline;
407 DOSCONF_skip(&p);
409 if ((menuname) && (*p == '['))
410 /* we were handling a specific sub menu, but now next menu begins */
411 break;
413 if ((trail = strrchr(confline, '\n')))
414 *trail = '\0';
415 if ((trail = strrchr(confline, '\r')))
416 *trail = '\0';
417 if (!(menu_skip))
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))))
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;
429 else /* the current menu gets skipped */
430 DOSCONF_Menu(&p);
434 int DOSCONF_ReadConfig(void)
436 char buffer[256];
437 DOS_FULL_NAME fullname;
438 char *filename, *menuname;
439 int ret = 1;
441 PROFILE_GetWineIniString( "wine", "config.sys", "", buffer, sizeof(buffer) );
442 if (!(filename = strtok(buffer, ","))) return ret;
443 menuname = strtok(NULL, ",");
445 DOSFS_GetFullName(filename, FALSE, &fullname);
446 if (menuname) menu_default = strdup(menuname);
447 if ((cfg_fd = fopen(fullname.long_name, "r")))
449 DOSCONF_Parse(NULL);
450 fclose(cfg_fd);
452 else
454 MESSAGE("Couldn't open config.sys file given as \"%s\" in" \
455 " wine.conf or .winerc, section [wine] !\n", filename);
456 ret = 0;
458 if (menu_default) free(menu_default);
459 return ret;