Implement a bunch of shlwapi functions, add some new stubs.
[wine/hacks.git] / msdos / dosconf.c
blob37e4d8a17cc7430302b413d1e2f86155b54b54ea
1 /*
2 * DOS CONFIG.SYS parser
4 * Copyright 1998 Andreas Mohr
6 */
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include "winbase.h"
14 #include "msdos.h"
15 #include "debugtools.h"
16 #include "options.h"
17 #include "file.h"
19 DEFAULT_DEBUG_CHANNEL(profile);
22 static int DOSCONF_Device(char **confline);
23 static int DOSCONF_Dos(char **confline);
24 static int DOSCONF_Fcbs(char **confline);
25 static int DOSCONF_Break(char **confline);
26 static int DOSCONF_Files(char **confline);
27 static int DOSCONF_Install(char **confline);
28 static int DOSCONF_Lastdrive(char **confline);
29 static int DOSCONF_Menu(char **confline);
30 static int DOSCONF_Include(char **confline);
31 static int DOSCONF_Country(char **confline);
32 static int DOSCONF_Numlock(char **confline);
33 static int DOSCONF_Switches(char **confline);
34 static int DOSCONF_Shell(char **confline);
35 static int DOSCONF_Stacks(char **confline);
36 static int DOSCONF_Buffers(char **confline);
37 static void DOSCONF_Parse(char *menuname);
39 DOSCONF DOSCONF_config =
41 'E', /* lastdrive */
42 0, /* brk_flag */
43 8, /* files */
44 9, /* stacks_nr */
45 256, /* stacks_sz */
46 15, /* buf */
47 1, /* buf2 */
48 4, /* fcbs */
49 0, /* flags */
50 NULL, /* shell */
51 NULL /* country */
54 typedef struct {
55 const char *tag_name;
56 int (*tag_handler)(char **p);
57 } TAG_ENTRY;
61 * see
62 * http://egeria.cm.cf.ac.uk/User/P.L.Poulain/project/internal/allinter.htm
63 * or
64 * http://www.csulb.edu/~murdock/dosindex.html
67 static const TAG_ENTRY tag_entries[] =
69 { ";", NULL },
70 { "REM ", NULL },
71 { "DEVICE", DOSCONF_Device },
72 { "[", DOSCONF_Menu },
73 { "SUBMENU", NULL },
74 { "MENUDEFAULT", DOSCONF_Menu },
75 { "INCLUDE", DOSCONF_Include },
77 { "INSTALL", DOSCONF_Install },
78 { "DOS", DOSCONF_Dos },
79 { "FCBS", DOSCONF_Fcbs },
80 { "BREAK", DOSCONF_Break },
81 { "FILES", DOSCONF_Files },
82 { "SHELL", DOSCONF_Shell },
83 { "STACKS", DOSCONF_Stacks },
84 { "BUFFERS", DOSCONF_Buffers },
85 { "COUNTRY", DOSCONF_Country },
86 { "NUMLOCK", DOSCONF_Numlock },
87 { "SWITCHES", DOSCONF_Switches },
88 { "LASTDRIVE", DOSCONF_Lastdrive }
91 static FILE *cfg_fd;
93 static char *menu_default = NULL;
94 static int menu_in_listing = 0; /* we are in the [menu] section */
95 static int menu_skip = 0; /* the current menu gets skipped */
98 static void DOSCONF_skip(char **pconfline)
100 char *p;
102 p = *pconfline;
103 while ( (*p == ' ') || (*p == '\t') ) p++;
104 *pconfline = p;
107 static int DOSCONF_JumpToEntry(char **pconfline, char separator)
109 char *p;
111 p = *pconfline;
112 while ( (*p != separator) && (*p != '\0') ) p++;
114 if (*p != separator)
115 return 0;
116 else p++;
118 while ( (*p == ' ') || (*p == '\t') ) p++;
119 *pconfline = p;
120 return 1;
123 static int DOSCONF_Device(char **confline)
125 int loadhigh = 0;
127 *confline += 6; /* strlen("DEVICE") */
128 if (!(strncasecmp(*confline, "HIGH", 4)))
130 loadhigh = 1;
131 *confline += 4;
132 /* FIXME: get DEVICEHIGH parameters if avail ? */
134 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
135 TRACE("Loading device '%s'\n", *confline);
136 #if 0
137 DOSMOD_LoadDevice(*confline, loadhigh);
138 #endif
139 return 1;
142 static int DOSCONF_Dos(char **confline)
144 *confline += 3; /* strlen("DOS") */
145 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
146 while (**confline != '\0')
148 if (!(strncasecmp(*confline, "HIGH", 4)))
150 DOSCONF_config.flags |= DOSCONF_MEM_HIGH;
151 *confline += 4;
153 else
154 if (!(strncasecmp(*confline, "UMB", 3)))
156 DOSCONF_config.flags |= DOSCONF_MEM_UMB;
157 *confline += 3;
159 else (*confline)++;
160 DOSCONF_JumpToEntry(confline, ',');
162 TRACE("DOSCONF_Dos: HIGH is %d, UMB is %d\n",
163 (DOSCONF_config.flags & DOSCONF_MEM_HIGH) != 0, (DOSCONF_config.flags & DOSCONF_MEM_UMB) != 0);
164 return 1;
167 static int DOSCONF_Fcbs(char **confline)
169 *confline += 4; /* strlen("FCBS") */
170 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
171 DOSCONF_config.fcbs = atoi(*confline);
172 if (DOSCONF_config.fcbs > 255)
174 MESSAGE("The FCBS value in the config.sys file is too high ! Setting to 255.\n");
175 DOSCONF_config.fcbs = 255;
177 TRACE("DOSCONF_Fcbs returning %d\n", DOSCONF_config.fcbs);
178 return 1;
181 static int DOSCONF_Break(char **confline)
183 *confline += 5; /* strlen("BREAK") */
184 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
185 if (!(strcasecmp(*confline, "ON")))
186 DOSCONF_config.brk_flag = 1;
187 TRACE("BREAK is %d\n", DOSCONF_config.brk_flag);
188 return 1;
191 static int DOSCONF_Files(char **confline)
193 *confline += 5; /* strlen("FILES") */
194 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
195 DOSCONF_config.files = atoi(*confline);
196 if (DOSCONF_config.files > 255)
198 MESSAGE("The FILES value in the config.sys file is too high ! Setting to 255.\n");
199 DOSCONF_config.files = 255;
201 if (DOSCONF_config.files < 8)
203 MESSAGE("The FILES value in the config.sys file is too low ! Setting to 8.\n");
204 DOSCONF_config.files = 8;
206 TRACE("DOSCONF_Files returning %d\n", DOSCONF_config.files);
207 return 1;
210 static int DOSCONF_Install(char **confline)
212 #if 0
213 int loadhigh = 0;
214 #endif
216 *confline += 7; /* strlen("INSTALL") */
217 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
218 TRACE("Installing '%s'\n", *confline);
219 #if 0
220 DOSMOD_Install(*confline, loadhigh);
221 #endif
222 return 1;
225 static int DOSCONF_Lastdrive(char **confline)
227 *confline += 9; /* strlen("LASTDRIVE") */
228 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
229 DOSCONF_config.lastdrive = toupper(**confline);
230 TRACE("Lastdrive %c\n", DOSCONF_config.lastdrive);
231 return 1;
234 static int DOSCONF_Country(char **confline)
236 *confline += 7; /* strlen("COUNTRY") */
237 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
238 TRACE("Country '%s'\n", *confline);
239 if (DOSCONF_config.country == NULL)
240 DOSCONF_config.country = malloc(strlen(*confline) + 1);
241 strcpy(DOSCONF_config.country, *confline);
242 return 1;
245 static int DOSCONF_Numlock(char **confline)
247 *confline += 7; /* strlen("NUMLOCK") */
248 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
249 if (!(strcasecmp(*confline, "ON")))
250 DOSCONF_config.flags |= DOSCONF_NUMLOCK;
251 TRACE("NUMLOCK is %d\n", (DOSCONF_config.flags & DOSCONF_NUMLOCK) != 0);
252 return 1;
255 static int DOSCONF_Switches(char **confline)
257 char *p;
259 *confline += 8; /* strlen("SWITCHES") */
260 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
261 p = strtok(*confline, "/");
264 if ( toupper(*p) == 'K')
265 DOSCONF_config.flags |= DOSCONF_KEYB_CONV;
267 while ((p = strtok(NULL, "/")));
268 TRACE("'Force conventional keyboard' is %d\n",
269 (DOSCONF_config.flags & DOSCONF_KEYB_CONV) != 0);
270 return 1;
273 static int DOSCONF_Shell(char **confline)
275 *confline += 5; /* strlen("SHELL") */
276 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
277 TRACE("Shell '%s'\n", *confline);
278 if (DOSCONF_config.shell == NULL)
279 DOSCONF_config.shell = malloc(strlen(*confline) + 1);
280 strcpy(DOSCONF_config.shell, *confline);
281 return 1;
284 static int DOSCONF_Stacks(char **confline)
287 *confline += 6; /* strlen("STACKS") */
288 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
289 DOSCONF_config.stacks_nr = atoi(strtok(*confline, ","));
290 DOSCONF_config.stacks_sz = atoi((strtok(NULL, ",")));
291 TRACE("%d stacks of size %d\n",
292 DOSCONF_config.stacks_nr, DOSCONF_config.stacks_sz);
293 return 1;
296 static int DOSCONF_Buffers(char **confline)
298 char *p;
300 *confline += 7; /* strlen("BUFFERS") */
301 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
302 p = strtok(*confline, ",");
303 DOSCONF_config.buf = atoi(p);
304 if ((p = strtok(NULL, ",")))
305 DOSCONF_config.buf2 = atoi(p);
306 TRACE("%d primary buffers, %d secondary buffers\n",
307 DOSCONF_config.buf, DOSCONF_config.buf2);
308 return 1;
311 static int DOSCONF_Menu(char **confline)
313 if (!(strncasecmp(*confline, "[MENU]", 6)))
314 menu_in_listing = 1;
315 else
316 if ((!(strncasecmp(*confline, "[COMMON]", 8)))
317 || (!(strncasecmp(*confline, "[WINE]", 6))))
318 menu_skip = 0;
319 else
320 if (**confline == '[')
322 (*confline)++;
323 if ((menu_default)
324 && (!(strncasecmp(*confline, menu_default, strlen(menu_default)))))
326 free(menu_default);
327 menu_default = NULL;
328 menu_skip = 0;
330 else
331 menu_skip = 1;
332 menu_in_listing = 0;
334 else
335 if (!(strncasecmp(*confline, "menudefault", 11)) && (menu_in_listing))
337 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
338 *confline = strtok(*confline, ",");
339 menu_default = malloc(strlen(*confline) + 1);
340 strcpy(menu_default, *confline);
342 return 1;
345 static int DOSCONF_Include(char **confline)
347 fpos_t oldpos;
348 char *temp;
350 *confline += 7; /* strlen("INCLUDE") */
351 if (!(DOSCONF_JumpToEntry(confline, '='))) return 0;
352 fgetpos(cfg_fd, &oldpos);
353 fseek(cfg_fd, 0, SEEK_SET);
354 TRACE("Including menu '%s'\n", *confline);
355 temp = malloc(strlen(*confline) + 1);
356 strcpy(temp, *confline);
357 DOSCONF_Parse(temp);
358 free(temp);
359 fsetpos(cfg_fd, &oldpos);
360 return 1;
363 static void DOSCONF_Parse(char *menuname)
365 char confline[256];
366 char *p, *trail;
367 int i;
369 if (menuname != NULL) /* we need to jump to a certain sub menu */
371 while (fgets(confline, 255, cfg_fd))
373 p = confline;
374 DOSCONF_skip(&p);
375 if (*p == '[')
377 p++;
378 if (!(trail = strrchr(p, ']')))
379 return;
380 if (!(strncasecmp(p, menuname, (int)trail - (int)p)))
381 break;
386 while (fgets(confline, 255, cfg_fd))
388 p = confline;
389 DOSCONF_skip(&p);
391 if ((menuname) && (*p == '['))
392 /* we were handling a specific sub menu, but now next menu begins */
393 break;
395 if ((trail = strrchr(confline, '\n')))
396 *trail = '\0';
397 if ((trail = strrchr(confline, '\r')))
398 *trail = '\0';
399 if (!(menu_skip))
401 for (i = 0; i < sizeof(tag_entries) / sizeof(TAG_ENTRY); i++)
402 if (!(strncasecmp(p, tag_entries[i].tag_name,
403 strlen(tag_entries[i].tag_name))))
405 TRACE("tag '%s'\n", tag_entries[i].tag_name);
406 if (tag_entries[i].tag_handler != NULL)
407 tag_entries[i].tag_handler(&p);
408 break;
411 else /* the current menu gets skipped */
412 DOSCONF_Menu(&p);
416 int DOSCONF_ReadConfig(void)
418 char buffer[256];
419 DOS_FULL_NAME fullname;
420 char *filename, *menuname;
421 int ret = 1;
423 PROFILE_GetWineIniString( "wine", "config.sys", "", buffer, sizeof(buffer) );
424 filename = strtok(buffer, ",");
425 menuname = strtok(NULL, ",");
426 if (!filename) return ret;
428 DOSFS_GetFullName(filename, FALSE, &fullname);
429 if (menuname) menu_default = strdup(menuname);
430 if ((cfg_fd = fopen(fullname.long_name, "r")))
432 DOSCONF_Parse(NULL);
433 fclose(cfg_fd);
435 else
437 MESSAGE("Couldn't open config.sys file given as \"%s\" in" \
438 " wine.conf or .winerc, section [wine] !\n", filename);
439 ret = 0;
441 if (menu_default) free(menu_default);
442 return ret;