4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1995, 1996, 1997 Alexandre Julliard
7 * Copyright 1997 Eric Youngdale
8 * Copyright 1999 Ulrich Weigand
24 static SPEC_TYPE SpecType = SPEC_INVALID;
26 static char ParseBuffer[512];
27 static char TokenBuffer[512];
28 static char *ParseNext = ParseBuffer;
29 static FILE *input_file;
31 static const char * const TypeNames[TYPE_NBTYPES] =
33 "variable", /* TYPE_VARIABLE */
34 "pascal16", /* TYPE_PASCAL_16 */
35 "pascal", /* TYPE_PASCAL */
36 "equate", /* TYPE_ABS */
37 "register", /* TYPE_REGISTER */
38 "interrupt", /* TYPE_INTERRUPT */
39 "stub", /* TYPE_STUB */
40 "stdcall", /* TYPE_STDCALL */
41 "cdecl", /* TYPE_CDECL */
42 "varargs", /* TYPE_VARARGS */
43 "extern", /* TYPE_EXTERN */
44 "forward" /* TYPE_FORWARD */
47 static const char * const FlagNames[] =
49 "noimport", /* FLAG_NOIMPORT */
50 "norelay", /* FLAG_NORELAY */
51 "ret64", /* FLAG_RET64 */
52 "i386", /* FLAG_I386 */
56 static int IsNumberString(const char *s)
58 while (*s) if (!isdigit(*s++)) return 0;
62 inline static int is_token_separator( char ch )
64 return (ch == '(' || ch == ')' || ch == '-');
67 static const char * GetTokenInLine(void)
70 char *token = TokenBuffer;
73 * Remove initial white space.
75 while (isspace(*p)) p++;
77 if ((*p == '\0') || (*p == '#')) return NULL;
82 if (is_token_separator(*p))
84 /* a separator is always a complete token */
87 else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
90 if (*p) *token++ = *p++;
97 static const char * GetToken( int allow_eof )
101 while ((token = GetTokenInLine()) == NULL)
103 ParseNext = ParseBuffer;
105 if (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) == NULL)
107 if (!allow_eof) fatal_error( "Unexpected end of file\n" );
115 /*******************************************************************
118 * Parse a debug channel definition.
120 static void ParseDebug(void)
122 const char *token = GetToken(0);
123 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
127 if (*token == ')') break;
128 debug_channels = xrealloc( debug_channels,
129 (nb_debug_channels + 1) * sizeof(*debug_channels));
130 debug_channels[nb_debug_channels++] = xstrdup(token);
135 /*******************************************************************
138 * Parse an 'ignore' definition.
140 static void ParseIgnore(void)
142 const char *token = GetToken(0);
143 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
147 if (*token == ')') break;
148 add_ignore_symbol( token );
153 /*******************************************************************
156 * Parse a variable definition.
158 static void ParseVariable( ORDDEF *odp )
163 int value_array_size;
165 const char *token = GetToken(0);
166 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
169 value_array_size = 25;
170 value_array = xmalloc(sizeof(*value_array) * value_array_size);
178 value_array[n_values++] = strtol(token, &endptr, 0);
179 if (n_values == value_array_size)
181 value_array_size += 25;
182 value_array = xrealloc(value_array,
183 sizeof(*value_array) * value_array_size);
186 if (endptr == NULL || *endptr != '\0')
187 fatal_error( "Expected number value, got '%s'\n", token );
190 odp->u.var.n_values = n_values;
191 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
195 /*******************************************************************
196 * ParseExportFunction
198 * Parse a function definition.
200 static void ParseExportFunction( ORDDEF *odp )
208 if (odp->type == TYPE_STDCALL)
209 fatal_error( "'stdcall' not supported for Win16\n" );
210 if (odp->type == TYPE_VARARGS)
211 fatal_error( "'varargs' not supported for Win16\n" );
214 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
215 fatal_error( "'pascal' not supported for Win32\n" );
222 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
224 for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
230 if (!strcmp(token, "word"))
231 odp->u.func.arg_types[i] = 'w';
232 else if (!strcmp(token, "s_word"))
233 odp->u.func.arg_types[i] = 's';
234 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
235 odp->u.func.arg_types[i] = 'l';
236 else if (!strcmp(token, "ptr"))
237 odp->u.func.arg_types[i] = 'p';
238 else if (!strcmp(token, "str"))
239 odp->u.func.arg_types[i] = 't';
240 else if (!strcmp(token, "wstr"))
241 odp->u.func.arg_types[i] = 'W';
242 else if (!strcmp(token, "segstr"))
243 odp->u.func.arg_types[i] = 'T';
244 else if (!strcmp(token, "double"))
246 odp->u.func.arg_types[i++] = 'l';
247 if (i < sizeof(odp->u.func.arg_types)) odp->u.func.arg_types[i] = 'l';
249 else fatal_error( "Unknown variable type '%s'\n", token );
251 if (SpecType == SPEC_WIN32)
253 if (strcmp(token, "long") &&
254 strcmp(token, "ptr") &&
255 strcmp(token, "str") &&
256 strcmp(token, "wstr") &&
257 strcmp(token, "double"))
259 fatal_error( "Type '%s' not supported for Win32\n", token );
263 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
264 fatal_error( "Too many arguments\n" );
266 odp->u.func.arg_types[i] = '\0';
267 if (odp->type == TYPE_VARARGS)
268 odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
269 odp->link_name = xstrdup( GetToken(0) );
273 /*******************************************************************
276 * Parse an 'equate' definition.
278 static void ParseEquate( ORDDEF *odp )
282 const char *token = GetToken(0);
283 int value = strtol(token, &endptr, 0);
284 if (endptr == NULL || *endptr != '\0')
285 fatal_error( "Expected number value, got '%s'\n", token );
286 if (SpecType == SPEC_WIN32)
287 fatal_error( "'equate' not supported for Win32\n" );
288 odp->u.abs.value = value;
292 /*******************************************************************
295 * Parse a 'stub' definition.
297 static void ParseStub( ORDDEF *odp )
299 odp->u.func.arg_types[0] = '\0';
300 odp->link_name = xstrdup("");
304 /*******************************************************************
307 * Parse an 'interrupt' definition.
309 static void ParseInterrupt( ORDDEF *odp )
313 if (SpecType == SPEC_WIN32)
314 fatal_error( "'interrupt' not supported for Win32\n" );
317 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
320 if (*token != ')') fatal_error( "Expected ')' got '%s'\n", token );
322 odp->u.func.arg_types[0] = '\0';
323 odp->link_name = xstrdup( GetToken(0) );
327 /*******************************************************************
330 * Parse an 'extern' definition.
332 static void ParseExtern( ORDDEF *odp )
334 if (SpecType == SPEC_WIN16) fatal_error( "'extern' not supported for Win16\n" );
335 odp->link_name = xstrdup( GetToken(0) );
336 /* 'extern' definitions are not available for implicit import */
337 odp->flags |= FLAG_NOIMPORT;
341 /*******************************************************************
344 * Parse a 'forward' definition.
346 static void ParseForward( ORDDEF *odp )
348 if (SpecType == SPEC_WIN16) fatal_error( "'forward' not supported for Win16\n" );
349 odp->link_name = xstrdup( GetToken(0) );
353 /*******************************************************************
356 * Parse the optional flags for an entry point
358 static const char *ParseFlags( ORDDEF *odp )
366 for (i = 0; FlagNames[i]; i++)
367 if (!strcmp( FlagNames[i], token )) break;
368 if (!FlagNames[i]) fatal_error( "Unknown flag '%s'\n", token );
369 odp->flags |= 1 << i;
371 } while (*token == '-');
376 /*******************************************************************
379 * Fix an exported function name by removing a possible @xx suffix
381 static void fix_export_name( char *name )
383 char *p, *end = strrchr( name, '@' );
384 if (!end || !end[1] || end == name) return;
385 /* make sure all the rest is digits */
386 for (p = end + 1; *p; p++) if (!isdigit(*p)) return;
390 /*******************************************************************
393 * Parse an ordinal definition.
395 static void ParseOrdinal(int ordinal)
399 ORDDEF *odp = xmalloc( sizeof(*odp) );
400 memset( odp, 0, sizeof(*odp) );
401 EntryPoints[nb_entry_points++] = odp;
405 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
406 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
409 if (odp->type >= TYPE_NBTYPES)
410 fatal_error( "Expected type after ordinal, found '%s' instead\n", token );
413 if (*token == '-') token = ParseFlags( odp );
415 odp->name = xstrdup( token );
416 fix_export_name( odp->name );
417 odp->lineno = current_line;
418 odp->ordinal = ordinal;
423 ParseVariable( odp );
431 ParseExportFunction( odp );
434 ParseInterrupt( odp );
453 if (odp->flags & FLAG_I386)
455 /* ignore this entry point on non-Intel archs */
456 EntryPoints[--nb_entry_points] = NULL;
464 if (ordinal >= MAX_ORDINALS) fatal_error( "Ordinal number %d too large\n", ordinal );
465 if (ordinal > Limit) Limit = ordinal;
466 if (ordinal < Base) Base = ordinal;
467 odp->ordinal = ordinal;
468 Ordinals[ordinal] = odp;
471 if (!strcmp( odp->name, "@" ))
474 fatal_error( "Nameless function needs an explicit ordinal number\n" );
475 if (SpecType != SPEC_WIN32)
476 fatal_error( "Nameless functions not supported for Win16\n" );
479 else Names[nb_names++] = odp;
483 static int name_compare( const void *name1, const void *name2 )
485 ORDDEF *odp1 = *(ORDDEF **)name1;
486 ORDDEF *odp2 = *(ORDDEF **)name2;
487 return strcmp( odp1->name, odp2->name );
490 /*******************************************************************
493 * Sort the name array and catch duplicates.
495 static void sort_names(void)
499 if (!nb_names) return;
501 /* sort the list of names */
502 qsort( Names, nb_names, sizeof(Names[0]), name_compare );
504 /* check for duplicate names */
505 for (i = 0; i < nb_names - 1; i++)
507 if (!strcmp( Names[i]->name, Names[i+1]->name ))
509 current_line = max( Names[i]->lineno, Names[i+1]->lineno );
510 fatal_error( "'%s' redefined (previous definition at line %d)\n",
511 Names[i]->name, min( Names[i]->lineno, Names[i+1]->lineno ) );
517 /*******************************************************************
522 SPEC_TYPE ParseTopLevel( FILE *file )
528 while ((token = GetToken(1)) != NULL)
530 if (strcmp(token, "name") == 0)
532 strcpy(DLLName, GetToken(0));
534 else if (strcmp(token, "file") == 0)
536 strcpy(DLLFileName, GetToken(0));
537 strupper(DLLFileName);
539 else if (strcmp(token, "type") == 0)
542 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
543 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
544 else fatal_error( "Type must be 'win16' or 'win32'\n" );
546 else if (strcmp(token, "mode") == 0)
549 if (!strcmp(token, "dll" )) SpecMode = SPEC_MODE_DLL;
550 else if (!strcmp(token, "guiexe" )) SpecMode = SPEC_MODE_GUIEXE;
551 else if (!strcmp(token, "cuiexe" )) SpecMode = SPEC_MODE_CUIEXE;
552 else if (!strcmp(token, "guiexe_unicode" )) SpecMode = SPEC_MODE_GUIEXE_UNICODE;
553 else if (!strcmp(token, "cuiexe_unicode" )) SpecMode = SPEC_MODE_CUIEXE_UNICODE;
554 else fatal_error( "Mode must be 'dll', 'guiexe', 'cuiexe', 'guiexe_unicode' or 'cuiexe_unicode'\n" );
556 else if (strcmp(token, "heap") == 0)
559 if (!IsNumberString(token)) fatal_error( "Expected number after heap\n" );
560 DLLHeapSize = atoi(token);
562 else if (strcmp(token, "init") == 0)
564 if (SpecType == SPEC_WIN16)
565 fatal_error( "init cannot be used for Win16 spec files\n" );
566 init_func = xstrdup( GetToken(0) );
568 else if (strcmp(token, "import") == 0)
573 if (SpecType != SPEC_WIN32)
574 fatal_error( "Imports not supported for Win16\n" );
579 if (!strcmp(name, "delay"))
586 warning( "The 'delay' option is not yet supported on the PPC. 'delay' will be ignored.\n");
589 else fatal_error( "Unknown option '%s' for import directive\n", name );
591 add_import_dll( name, delay );
593 else if (strcmp(token, "rsrc") == 0)
595 if (SpecType != SPEC_WIN16) load_res32_file( GetToken(0) );
596 else load_res16_file( GetToken(0) );
598 else if (strcmp(token, "owner") == 0)
600 if (SpecType != SPEC_WIN16)
601 fatal_error( "Owner only supported for Win16 spec files\n" );
602 strcpy( owner_name, GetToken(0) );
604 else if (strcmp(token, "debug_channels") == 0)
606 if (SpecType != SPEC_WIN32)
607 fatal_error( "debug channels only supported for Win32 spec files\n" );
610 else if (strcmp(token, "ignore") == 0)
612 if (SpecType != SPEC_WIN32)
613 fatal_error( "'ignore' only supported for Win32 spec files\n" );
616 else if (strcmp(token, "@") == 0)
618 if (SpecType != SPEC_WIN32)
619 fatal_error( "'@' ordinals not supported for Win16\n" );
622 else if (IsNumberString(token))
624 ParseOrdinal( atoi(token) );
627 fatal_error( "Expected name, id, length or ordinal\n" );
632 if (SpecMode == SPEC_MODE_DLL)
633 sprintf( DLLFileName, "%s.dll", DLLName );
635 sprintf( DLLFileName, "%s.exe", DLLName );
638 if (SpecType == SPEC_INVALID) fatal_error( "Missing 'type' declaration\n" );
639 if (SpecType == SPEC_WIN16 && !owner_name[0])
640 fatal_error( "'owner' not specified for Win16 dll\n" );
642 current_line = 0; /* no longer parsing the input file */