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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 static SPEC_TYPE SpecType = SPEC_INVALID;
40 static char ParseBuffer[512];
41 static char TokenBuffer[512];
42 static char *ParseNext = ParseBuffer;
43 static FILE *input_file;
45 static const char * const TypeNames[TYPE_NBTYPES] =
47 "variable", /* TYPE_VARIABLE */
48 "pascal16", /* TYPE_PASCAL_16 */
49 "pascal", /* TYPE_PASCAL */
50 "equate", /* TYPE_ABS */
51 "stub", /* TYPE_STUB */
52 "stdcall", /* TYPE_STDCALL */
53 "cdecl", /* TYPE_CDECL */
54 "varargs", /* TYPE_VARARGS */
55 "extern", /* TYPE_EXTERN */
56 "forward" /* TYPE_FORWARD */
59 static const char * const FlagNames[] =
61 "noimport", /* FLAG_NOIMPORT */
62 "norelay", /* FLAG_NORELAY */
63 "ret64", /* FLAG_RET64 */
64 "i386", /* FLAG_I386 */
65 "register", /* FLAG_REGISTER */
66 "interrupt", /* FLAG_INTERRUPT */
70 static int IsNumberString(const char *s)
72 while (*s) if (!isdigit(*s++)) return 0;
76 inline static int is_token_separator( char ch )
78 return (ch == '(' || ch == ')' || ch == '-');
81 static const char * GetTokenInLine(void)
84 char *token = TokenBuffer;
87 * Remove initial white space.
89 while (isspace(*p)) p++;
91 if ((*p == '\0') || (*p == '#')) return NULL;
96 if (is_token_separator(*p))
98 /* a separator is always a complete token */
101 else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
104 if (*p) *token++ = *p++;
111 static const char * GetToken( int allow_eof )
115 while ((token = GetTokenInLine()) == NULL)
117 ParseNext = ParseBuffer;
119 if (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) == NULL)
121 if (!allow_eof) fatal_error( "Unexpected end of file\n" );
129 /*******************************************************************
132 * Parse a debug channel definition.
134 static void ParseDebug(void)
136 const char *token = GetToken(0);
137 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
141 if (*token == ')') break;
142 debug_channels = xrealloc( debug_channels,
143 (nb_debug_channels + 1) * sizeof(*debug_channels));
144 debug_channels[nb_debug_channels++] = xstrdup(token);
149 /*******************************************************************
152 * Parse an 'ignore' definition.
154 static void ParseIgnore(void)
156 const char *token = GetToken(0);
157 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
161 if (*token == ')') break;
162 add_ignore_symbol( token );
167 /*******************************************************************
170 * Parse a variable definition.
172 static void ParseVariable( ORDDEF *odp )
177 int value_array_size;
179 const char *token = GetToken(0);
180 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
183 value_array_size = 25;
184 value_array = xmalloc(sizeof(*value_array) * value_array_size);
192 value_array[n_values++] = strtol(token, &endptr, 0);
193 if (n_values == value_array_size)
195 value_array_size += 25;
196 value_array = xrealloc(value_array,
197 sizeof(*value_array) * value_array_size);
200 if (endptr == NULL || *endptr != '\0')
201 fatal_error( "Expected number value, got '%s'\n", token );
204 odp->u.var.n_values = n_values;
205 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
209 /*******************************************************************
210 * ParseExportFunction
212 * Parse a function definition.
214 static void ParseExportFunction( ORDDEF *odp )
222 if (odp->type == TYPE_STDCALL)
223 fatal_error( "'stdcall' not supported for Win16\n" );
224 if (odp->type == TYPE_VARARGS)
225 fatal_error( "'varargs' not supported for Win16\n" );
228 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
229 fatal_error( "'pascal' not supported for Win32\n" );
230 if (odp->flags & FLAG_INTERRUPT)
231 fatal_error( "'interrupt' not supported for Win32\n" );
238 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
240 for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
246 if (!strcmp(token, "word"))
247 odp->u.func.arg_types[i] = 'w';
248 else if (!strcmp(token, "s_word"))
249 odp->u.func.arg_types[i] = 's';
250 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
251 odp->u.func.arg_types[i] = 'l';
252 else if (!strcmp(token, "ptr"))
253 odp->u.func.arg_types[i] = 'p';
254 else if (!strcmp(token, "str"))
255 odp->u.func.arg_types[i] = 't';
256 else if (!strcmp(token, "wstr"))
257 odp->u.func.arg_types[i] = 'W';
258 else if (!strcmp(token, "segstr"))
259 odp->u.func.arg_types[i] = 'T';
260 else if (!strcmp(token, "double"))
262 odp->u.func.arg_types[i++] = 'l';
263 if (i < sizeof(odp->u.func.arg_types)) odp->u.func.arg_types[i] = 'l';
265 else fatal_error( "Unknown variable type '%s'\n", token );
267 if (SpecType == SPEC_WIN32)
269 if (strcmp(token, "long") &&
270 strcmp(token, "ptr") &&
271 strcmp(token, "str") &&
272 strcmp(token, "wstr") &&
273 strcmp(token, "double"))
275 fatal_error( "Type '%s' not supported for Win32\n", token );
279 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
280 fatal_error( "Too many arguments\n" );
282 odp->u.func.arg_types[i] = '\0';
283 if (odp->type == TYPE_VARARGS)
284 odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
285 odp->link_name = xstrdup( GetToken(0) );
289 /*******************************************************************
292 * Parse an 'equate' definition.
294 static void ParseEquate( ORDDEF *odp )
298 const char *token = GetToken(0);
299 int value = strtol(token, &endptr, 0);
300 if (endptr == NULL || *endptr != '\0')
301 fatal_error( "Expected number value, got '%s'\n", token );
302 if (SpecType == SPEC_WIN32)
303 fatal_error( "'equate' not supported for Win32\n" );
304 odp->u.abs.value = value;
308 /*******************************************************************
311 * Parse a 'stub' definition.
313 static void ParseStub( ORDDEF *odp )
315 odp->u.func.arg_types[0] = '\0';
316 odp->link_name = xstrdup("");
320 /*******************************************************************
323 * Parse an 'extern' definition.
325 static void ParseExtern( ORDDEF *odp )
327 if (SpecType == SPEC_WIN16) fatal_error( "'extern' not supported for Win16\n" );
328 odp->link_name = xstrdup( GetToken(0) );
329 /* 'extern' definitions are not available for implicit import */
330 odp->flags |= FLAG_NOIMPORT;
334 /*******************************************************************
337 * Parse a 'forward' definition.
339 static void ParseForward( ORDDEF *odp )
341 if (SpecType == SPEC_WIN16) fatal_error( "'forward' not supported for Win16\n" );
342 odp->link_name = xstrdup( GetToken(0) );
346 /*******************************************************************
349 * Parse the optional flags for an entry point
351 static const char *ParseFlags( ORDDEF *odp )
359 for (i = 0; FlagNames[i]; i++)
360 if (!strcmp( FlagNames[i], token )) break;
361 if (!FlagNames[i]) fatal_error( "Unknown flag '%s'\n", token );
362 odp->flags |= 1 << i;
364 } while (*token == '-');
369 /*******************************************************************
372 * Fix an exported function name by removing a possible @xx suffix
374 static void fix_export_name( char *name )
376 char *p, *end = strrchr( name, '@' );
377 if (!end || !end[1] || end == name) return;
378 /* make sure all the rest is digits */
379 for (p = end + 1; *p; p++) if (!isdigit(*p)) return;
383 /*******************************************************************
386 * Parse an ordinal definition.
388 static void ParseOrdinal(int ordinal)
392 ORDDEF *odp = xmalloc( sizeof(*odp) );
393 memset( odp, 0, sizeof(*odp) );
394 EntryPoints[nb_entry_points++] = odp;
398 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
399 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
402 if (odp->type >= TYPE_NBTYPES)
403 fatal_error( "Expected type after ordinal, found '%s' instead\n", token );
406 if (*token == '-') token = ParseFlags( odp );
408 odp->name = xstrdup( token );
409 fix_export_name( odp->name );
410 odp->lineno = current_line;
411 odp->ordinal = ordinal;
416 ParseVariable( odp );
423 ParseExportFunction( odp );
442 if (odp->flags & FLAG_I386)
444 /* ignore this entry point on non-Intel archs */
445 EntryPoints[--nb_entry_points] = NULL;
453 if (ordinal >= MAX_ORDINALS) fatal_error( "Ordinal number %d too large\n", ordinal );
454 if (ordinal > Limit) Limit = ordinal;
455 if (ordinal < Base) Base = ordinal;
456 odp->ordinal = ordinal;
457 Ordinals[ordinal] = odp;
460 if (!strcmp( odp->name, "@" ))
463 fatal_error( "Nameless function needs an explicit ordinal number\n" );
464 if (SpecType != SPEC_WIN32)
465 fatal_error( "Nameless functions not supported for Win16\n" );
468 else Names[nb_names++] = odp;
472 static int name_compare( const void *name1, const void *name2 )
474 ORDDEF *odp1 = *(ORDDEF **)name1;
475 ORDDEF *odp2 = *(ORDDEF **)name2;
476 return strcmp( odp1->name, odp2->name );
479 /*******************************************************************
482 * Sort the name array and catch duplicates.
484 static void sort_names(void)
488 if (!nb_names) return;
490 /* sort the list of names */
491 qsort( Names, nb_names, sizeof(Names[0]), name_compare );
493 /* check for duplicate names */
494 for (i = 0; i < nb_names - 1; i++)
496 if (!strcmp( Names[i]->name, Names[i+1]->name ))
498 current_line = max( Names[i]->lineno, Names[i+1]->lineno );
499 fatal_error( "'%s' redefined (previous definition at line %d)\n",
500 Names[i]->name, min( Names[i]->lineno, Names[i+1]->lineno ) );
506 /*******************************************************************
511 SPEC_TYPE ParseTopLevel( FILE *file, int def_only )
517 while ((token = GetToken(1)) != NULL)
519 if (strcmp(token, "name") == 0)
521 strcpy(DLLName, GetToken(0));
523 else if (strcmp(token, "file") == 0)
525 strcpy(DLLFileName, GetToken(0));
527 else if (strcmp(token, "type") == 0)
530 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
531 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
532 else fatal_error( "Type must be 'win16' or 'win32'\n" );
534 else if (strcmp(token, "mode") == 0)
537 if (!strcmp(token, "dll" )) SpecMode = SPEC_MODE_DLL;
538 else if (!strcmp(token, "guiexe" )) SpecMode = SPEC_MODE_GUIEXE;
539 else if (!strcmp(token, "cuiexe" )) SpecMode = SPEC_MODE_CUIEXE;
540 else if (!strcmp(token, "guiexe_unicode" )) SpecMode = SPEC_MODE_GUIEXE_UNICODE;
541 else if (!strcmp(token, "cuiexe_unicode" )) SpecMode = SPEC_MODE_CUIEXE_UNICODE;
542 else fatal_error( "Mode must be 'dll', 'guiexe', 'cuiexe', 'guiexe_unicode' or 'cuiexe_unicode'\n" );
544 else if (strcmp(token, "heap") == 0)
547 if (!IsNumberString(token)) fatal_error( "Expected number after heap\n" );
548 DLLHeapSize = atoi(token);
550 else if (strcmp(token, "stack") == 0)
553 if (!IsNumberString(token)) fatal_error( "Expected number after stack\n" );
554 stack_size = atoi(token);
556 else if (strcmp(token, "init") == 0)
558 if (SpecType == SPEC_WIN16)
559 fatal_error( "init cannot be used for Win16 spec files\n" );
560 init_func = xstrdup( GetToken(0) );
562 else if (strcmp(token, "import") == 0)
567 if (SpecType != SPEC_WIN32)
568 fatal_error( "Imports not supported for Win16\n" );
573 if (!strcmp(name, "delay"))
580 warning( "The 'delay' option is not yet supported on the PPC. 'delay' will be ignored.\n");
583 else fatal_error( "Unknown option '%s' for import directive\n", name );
585 if (!def_only) add_import_dll( name, delay );
587 else if (strcmp(token, "rsrc") == 0)
591 if (SpecType != SPEC_WIN16) load_res32_file( GetToken(0) );
592 else load_res16_file( GetToken(0) );
594 else GetToken(0); /* skip it */
596 else if (strcmp(token, "owner") == 0)
598 if (SpecType != SPEC_WIN16)
599 fatal_error( "Owner only supported for Win16 spec files\n" );
600 strcpy( owner_name, GetToken(0) );
602 else if (strcmp(token, "debug_channels") == 0)
604 if (SpecType != SPEC_WIN32)
605 fatal_error( "debug channels only supported for Win32 spec files\n" );
608 else if (strcmp(token, "ignore") == 0)
610 if (SpecType != SPEC_WIN32)
611 fatal_error( "'ignore' only supported for Win32 spec files\n" );
614 else if (strcmp(token, "@") == 0)
616 if (SpecType != SPEC_WIN32)
617 fatal_error( "'@' ordinals not supported for Win16\n" );
620 else if (IsNumberString(token))
622 ParseOrdinal( atoi(token) );
625 fatal_error( "Expected name, id, length or ordinal\n" );
630 if (SpecMode == SPEC_MODE_DLL)
632 strcpy( DLLFileName, DLLName );
633 /* Append .dll to name if no extension present */
634 if (!strrchr( DLLFileName, '.'))
635 strcat( DLLFileName, ".dll" );
638 sprintf( DLLFileName, "%s.exe", DLLName );
641 if (SpecType == SPEC_INVALID) fatal_error( "Missing 'type' declaration\n" );
642 if (SpecType == SPEC_WIN16 && !owner_name[0])
643 fatal_error( "'owner' not specified for Win16 dll\n" );
645 current_line = 0; /* no longer parsing the input file */