dbghelp: Fix compile on Hurd.
[wine.git] / tools / winebuild / parser.c
blob755084ee80044cd8e9341e46fcc04822a2cf05b6
1 /*
2 * Spec file parser
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1995, 1996, 1997, 2004 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include "build.h"
37 int current_line = 0;
39 static char ParseBuffer[512];
40 static char TokenBuffer[512];
41 static char *ParseNext = ParseBuffer;
42 static FILE *input_file;
44 static const char *separator_chars;
45 static const char *comment_chars;
47 /* valid characters in ordinal names */
48 static const char valid_ordname_chars[] = "/$:-_@?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
50 static const char * const TypeNames[TYPE_NBTYPES] =
52 "variable", /* TYPE_VARIABLE */
53 "pascal", /* TYPE_PASCAL */
54 "equate", /* TYPE_ABS */
55 "stub", /* TYPE_STUB */
56 "stdcall", /* TYPE_STDCALL */
57 "cdecl", /* TYPE_CDECL */
58 "varargs", /* TYPE_VARARGS */
59 "thiscall", /* TYPE_THISCALL */
60 "extern" /* TYPE_EXTERN */
63 static const char * const FlagNames[] =
65 "norelay", /* FLAG_NORELAY */
66 "noname", /* FLAG_NONAME */
67 "ret16", /* FLAG_RET16 */
68 "ret64", /* FLAG_RET64 */
69 "register", /* FLAG_REGISTER */
70 "private", /* FLAG_PRIVATE */
71 "ordinal", /* FLAG_ORDINAL */
72 NULL
75 static const char * const ArgNames[ARG_MAXARG + 1] =
77 "word", /* ARG_WORD */
78 "s_word", /* ARG_SWORD */
79 "segptr", /* ARG_SEGPTR */
80 "segstr", /* ARG_SEGSTR */
81 "long", /* ARG_LONG */
82 "ptr", /* ARG_PTR */
83 "str", /* ARG_STR */
84 "wstr", /* ARG_WSTR */
85 "int64", /* ARG_INT64 */
86 "int128", /* ARG_INT128 */
87 "float", /* ARG_FLOAT */
88 "double" /* ARG_DOUBLE */
91 static int IsNumberString(const char *s)
93 while (*s) if (!isdigit(*s++)) return 0;
94 return 1;
97 static inline int is_token_separator( char ch )
99 return strchr( separator_chars, ch ) != NULL;
102 static inline int is_token_comment( char ch )
104 return strchr( comment_chars, ch ) != NULL;
107 /* get the next line from the input file, or return 0 if at eof */
108 static int get_next_line(void)
110 ParseNext = ParseBuffer;
111 current_line++;
112 return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
115 static const char * GetToken( int allow_eol )
117 char *p = ParseNext;
118 char *token = TokenBuffer;
120 for (;;)
122 /* remove initial white space */
123 p = ParseNext;
124 while (isspace(*p)) p++;
126 if (*p == '\\' && p[1] == '\n') /* line continuation */
128 if (!get_next_line())
130 if (!allow_eol) error( "Unexpected end of file\n" );
131 return NULL;
134 else break;
137 if ((*p == '\0') || is_token_comment(*p))
139 if (!allow_eol) error( "Declaration not terminated properly\n" );
140 return NULL;
144 * Find end of token.
146 if (is_token_separator(*p))
148 /* a separator is always a complete token */
149 *token++ = *p++;
151 else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
153 if (*p == '\\') p++;
154 if (*p) *token++ = *p++;
156 *token = '\0';
157 ParseNext = p;
158 return TokenBuffer;
162 static ORDDEF *add_entry_point( DLLSPEC *spec )
164 ORDDEF *ret;
166 if (spec->nb_entry_points == spec->alloc_entry_points)
168 spec->alloc_entry_points += 128;
169 spec->entry_points = xrealloc( spec->entry_points,
170 spec->alloc_entry_points * sizeof(*spec->entry_points) );
172 ret = &spec->entry_points[spec->nb_entry_points++];
173 memset( ret, 0, sizeof(*ret) );
174 return ret;
177 /*******************************************************************
178 * parse_spec_variable
180 * Parse a variable definition in a .spec file.
182 static int parse_spec_variable( ORDDEF *odp, DLLSPEC *spec )
184 char *endptr;
185 int *value_array;
186 int n_values;
187 int value_array_size;
188 const char *token;
190 if (spec->type == SPEC_WIN32)
192 error( "'variable' not supported in Win32, use 'extern' instead\n" );
193 return 0;
196 if (!(token = GetToken(0))) return 0;
197 if (*token != '(')
199 error( "Expected '(' got '%s'\n", token );
200 return 0;
203 n_values = 0;
204 value_array_size = 25;
205 value_array = xmalloc(sizeof(*value_array) * value_array_size);
207 for (;;)
209 if (!(token = GetToken(0)))
211 free( value_array );
212 return 0;
214 if (*token == ')')
215 break;
217 value_array[n_values++] = strtol(token, &endptr, 0);
218 if (n_values == value_array_size)
220 value_array_size += 25;
221 value_array = xrealloc(value_array,
222 sizeof(*value_array) * value_array_size);
225 if (endptr == NULL || *endptr != '\0')
227 error( "Expected number value, got '%s'\n", token );
228 free( value_array );
229 return 0;
233 odp->u.var.n_values = n_values;
234 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
235 return 1;
239 /*******************************************************************
240 * parse_spec_export
242 * Parse an exported function definition in a .spec file.
244 static int parse_spec_export( ORDDEF *odp, DLLSPEC *spec )
246 const char *token;
247 unsigned int i, arg;
248 int is_win32 = (spec->type == SPEC_WIN32) || (odp->flags & FLAG_EXPORT32);
250 if (!is_win32 && odp->type == TYPE_STDCALL)
252 error( "'stdcall' not supported for Win16\n" );
253 return 0;
255 if (!is_win32 && odp->type == TYPE_THISCALL)
257 error( "'thiscall' not supported for Win16\n" );
258 return 0;
260 if (is_win32 && odp->type == TYPE_PASCAL)
262 error( "'pascal' not supported for Win32\n" );
263 return 0;
266 if (!(token = GetToken(0))) return 0;
267 if (*token != '(')
269 error( "Expected '(' got '%s'\n", token );
270 return 0;
273 odp->u.func.nb_args = 0;
274 for (i = 0; i < MAX_ARGUMENTS; i++)
276 if (!(token = GetToken(0))) return 0;
277 if (*token == ')')
278 break;
280 for (arg = 0; arg <= ARG_MAXARG; arg++)
281 if (!strcmp( ArgNames[arg], token )) break;
283 if (arg > ARG_MAXARG)
285 error( "Unknown argument type '%s'\n", token );
286 return 0;
288 if (is_win32) switch (arg)
290 case ARG_WORD:
291 case ARG_SWORD:
292 case ARG_SEGPTR:
293 case ARG_SEGSTR:
294 error( "Argument type '%s' only allowed for Win16\n", token );
295 return 0;
297 odp->u.func.args[i] = arg;
299 if (*token != ')')
301 error( "Too many arguments\n" );
302 return 0;
305 odp->u.func.nb_args = i;
306 if (odp->type == TYPE_VARARGS)
307 odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
308 if (odp->type == TYPE_THISCALL && (!i || odp->u.func.args[0] != ARG_PTR))
310 error( "First argument of a thiscall function must be a pointer\n" );
311 return 0;
314 if (!(token = GetToken(1)))
316 if (!strcmp( odp->name, "@" ))
318 error( "Missing handler name for anonymous function\n" );
319 return 0;
321 odp->link_name = xstrdup( odp->name );
323 else
325 odp->link_name = xstrdup( token );
326 if (strchr( odp->link_name, '.' ))
328 if (!is_win32)
330 error( "Forwarded functions not supported for Win16\n" );
331 return 0;
333 odp->flags |= FLAG_FORWARD;
336 if (target_cpu == CPU_x86 && odp->type == TYPE_THISCALL && !(odp->flags & FLAG_FORWARD))
338 char *link_name = strmake( "__thiscall_%s", odp->link_name );
339 free( odp->link_name );
340 odp->link_name = link_name;
342 return 1;
346 /*******************************************************************
347 * parse_spec_equate
349 * Parse an 'equate' definition in a .spec file.
351 static int parse_spec_equate( ORDDEF *odp, DLLSPEC *spec )
353 char *endptr;
354 int value;
355 const char *token;
357 if (spec->type == SPEC_WIN32)
359 error( "'equate' not supported for Win32\n" );
360 return 0;
362 if (!(token = GetToken(0))) return 0;
363 value = strtol(token, &endptr, 0);
364 if (endptr == NULL || *endptr != '\0')
366 error( "Expected number value, got '%s'\n", token );
367 return 0;
369 if (value < -0x8000 || value > 0xffff)
371 error( "Value %d for absolute symbol doesn't fit in 16 bits\n", value );
372 value = 0;
374 odp->u.abs.value = value;
375 return 1;
379 /*******************************************************************
380 * parse_spec_stub
382 * Parse a 'stub' definition in a .spec file
384 static int parse_spec_stub( ORDDEF *odp, DLLSPEC *spec )
386 odp->u.func.nb_args = 0;
387 odp->link_name = xstrdup("");
388 /* don't bother generating stubs for Winelib */
389 if (odp->flags & FLAG_CPU_MASK)
390 odp->flags &= FLAG_CPU(CPU_x86) | FLAG_CPU(CPU_x86_64);
391 else
392 odp->flags |= FLAG_CPU(CPU_x86) | FLAG_CPU(CPU_x86_64);
393 return 1;
397 /*******************************************************************
398 * parse_spec_extern
400 * Parse an 'extern' definition in a .spec file.
402 static int parse_spec_extern( ORDDEF *odp, DLLSPEC *spec )
404 const char *token;
406 if (spec->type == SPEC_WIN16)
408 error( "'extern' not supported for Win16, use 'variable' instead\n" );
409 return 0;
411 if (!(token = GetToken(1)))
413 if (!strcmp( odp->name, "@" ))
415 error( "Missing handler name for anonymous extern\n" );
416 return 0;
418 odp->link_name = xstrdup( odp->name );
420 else
422 odp->link_name = xstrdup( token );
423 if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
425 return 1;
429 /*******************************************************************
430 * parse_spec_flags
432 * Parse the optional flags for an entry point in a .spec file.
434 static const char *parse_spec_flags( DLLSPEC *spec, ORDDEF *odp )
436 unsigned int i;
437 const char *token;
441 if (!(token = GetToken(0))) break;
442 if (!strncmp( token, "arch=", 5))
444 char *args = xstrdup( token + 5 );
445 char *cpu_name = strtok( args, "," );
446 while (cpu_name)
448 if (!strcmp( cpu_name, "win32" ))
450 if (spec->type == SPEC_WIN32)
451 odp->flags |= FLAG_CPU_WIN32;
452 else
453 odp->flags |= FLAG_EXPORT32;
455 else if (!strcmp( cpu_name, "win64" ))
456 odp->flags |= FLAG_CPU_WIN64;
457 else
459 enum target_cpu cpu = get_cpu_from_name( cpu_name );
460 if (cpu == -1)
462 error( "Unknown architecture '%s'\n", cpu_name );
463 return NULL;
465 odp->flags |= FLAG_CPU( cpu );
467 cpu_name = strtok( NULL, "," );
469 free( args );
471 else if (!strcmp( token, "i386" )) /* backwards compatibility */
473 odp->flags |= FLAG_CPU(CPU_x86);
475 else
477 for (i = 0; FlagNames[i]; i++)
478 if (!strcmp( FlagNames[i], token )) break;
479 if (!FlagNames[i])
481 error( "Unknown flag '%s'\n", token );
482 return NULL;
484 odp->flags |= 1 << i;
486 token = GetToken(0);
487 } while (token && *token == '-');
489 return token;
493 /*******************************************************************
494 * parse_spec_ordinal
496 * Parse an ordinal definition in a .spec file.
498 static int parse_spec_ordinal( int ordinal, DLLSPEC *spec )
500 const char *token;
501 size_t len;
502 ORDDEF *odp = add_entry_point( spec );
504 if (!(token = GetToken(0))) goto error;
506 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
507 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
508 break;
510 if (odp->type >= TYPE_NBTYPES)
512 error( "Expected type after ordinal, found '%s' instead\n", token );
513 goto error;
516 if (!(token = GetToken(0))) goto error;
517 if (*token == '-' && !(token = parse_spec_flags( spec, odp ))) goto error;
519 if (ordinal == -1 && spec->type != SPEC_WIN32 && !(odp->flags & FLAG_EXPORT32))
521 error( "'@' ordinals not supported for Win16\n" );
522 goto error;
525 odp->name = xstrdup( token );
526 odp->lineno = current_line;
527 odp->ordinal = ordinal;
529 len = strspn( odp->name, valid_ordname_chars );
530 if (len < strlen( odp->name ))
532 error( "Character '%c' is not allowed in exported name '%s'\n", odp->name[len], odp->name );
533 goto error;
536 switch(odp->type)
538 case TYPE_VARIABLE:
539 if (!parse_spec_variable( odp, spec )) goto error;
540 break;
541 case TYPE_PASCAL:
542 case TYPE_STDCALL:
543 case TYPE_VARARGS:
544 case TYPE_CDECL:
545 case TYPE_THISCALL:
546 if (!parse_spec_export( odp, spec )) goto error;
547 break;
548 case TYPE_ABS:
549 if (!parse_spec_equate( odp, spec )) goto error;
550 break;
551 case TYPE_STUB:
552 if (!parse_spec_stub( odp, spec )) goto error;
553 break;
554 case TYPE_EXTERN:
555 if (!parse_spec_extern( odp, spec )) goto error;
556 break;
557 default:
558 assert( 0 );
561 if ((odp->flags & FLAG_CPU_MASK) && !(odp->flags & FLAG_CPU(target_cpu)))
563 /* ignore this entry point */
564 spec->nb_entry_points--;
565 return 1;
568 if (ordinal != -1)
570 if (!ordinal)
572 error( "Ordinal 0 is not valid\n" );
573 goto error;
575 if (ordinal >= MAX_ORDINALS)
577 error( "Ordinal number %d too large\n", ordinal );
578 goto error;
580 if (ordinal > spec->limit) spec->limit = ordinal;
581 if (ordinal < spec->base) spec->base = ordinal;
582 odp->ordinal = ordinal;
585 if (odp->type == TYPE_STDCALL && !(odp->flags & FLAG_PRIVATE))
587 if (!strcmp( odp->name, "DllRegisterServer" ) ||
588 !strcmp( odp->name, "DllUnregisterServer" ) ||
589 !strcmp( odp->name, "DllGetClassObject" ) ||
590 !strcmp( odp->name, "DllGetVersion" ) ||
591 !strcmp( odp->name, "DllInstall" ) ||
592 !strcmp( odp->name, "DllCanUnloadNow" ))
594 warning( "Function %s should be marked private\n", odp->name );
595 if (strcmp( odp->name, odp->link_name ))
596 warning( "Function %s should not use a different internal name (%s)\n",
597 odp->name, odp->link_name );
601 if (!strcmp( odp->name, "@" ) || odp->flags & (FLAG_NONAME | FLAG_ORDINAL))
603 if (ordinal == -1)
605 if (!strcmp( odp->name, "@" ))
606 error( "Nameless function needs an explicit ordinal number\n" );
607 else
608 error( "Function imported by ordinal needs an explicit ordinal number\n" );
609 goto error;
611 if (spec->type != SPEC_WIN32)
613 error( "Nameless functions not supported for Win16\n" );
614 goto error;
616 if (!strcmp( odp->name, "@" ))
618 free( odp->name );
619 odp->name = NULL;
621 else if (!(odp->flags & FLAG_ORDINAL)) /* -ordinal only affects the import library */
623 odp->export_name = odp->name;
624 odp->name = NULL;
627 return 1;
629 error:
630 spec->nb_entry_points--;
631 free( odp->name );
632 return 0;
636 static int name_compare( const void *ptr1, const void *ptr2 )
638 const ORDDEF *odp1 = *(const ORDDEF * const *)ptr1;
639 const ORDDEF *odp2 = *(const ORDDEF * const *)ptr2;
640 const char *name1 = odp1->name ? odp1->name : odp1->export_name;
641 const char *name2 = odp2->name ? odp2->name : odp2->export_name;
642 return strcmp( name1, name2 );
645 /*******************************************************************
646 * assign_names
648 * Build the name array and catch duplicates.
650 static void assign_names( DLLSPEC *spec )
652 int i, j, nb_exp_names = 0;
653 ORDDEF **all_names;
655 spec->nb_names = 0;
656 for (i = 0; i < spec->nb_entry_points; i++)
657 if (spec->entry_points[i].name) spec->nb_names++;
658 else if (spec->entry_points[i].export_name) nb_exp_names++;
660 if (!spec->nb_names && !nb_exp_names) return;
662 /* check for duplicates */
664 all_names = xmalloc( (spec->nb_names + nb_exp_names) * sizeof(all_names[0]) );
665 for (i = j = 0; i < spec->nb_entry_points; i++)
666 if (spec->entry_points[i].name || spec->entry_points[i].export_name)
667 all_names[j++] = &spec->entry_points[i];
669 qsort( all_names, j, sizeof(all_names[0]), name_compare );
671 for (i = 0; i < j - 1; i++)
673 const char *name1 = all_names[i]->name ? all_names[i]->name : all_names[i]->export_name;
674 const char *name2 = all_names[i+1]->name ? all_names[i+1]->name : all_names[i+1]->export_name;
675 if (!strcmp( name1, name2 ) &&
676 !((all_names[i]->flags ^ all_names[i+1]->flags) & FLAG_EXPORT32))
678 current_line = max( all_names[i]->lineno, all_names[i+1]->lineno );
679 error( "'%s' redefined\n%s:%d: First defined here\n",
680 name1, input_file_name,
681 min( all_names[i]->lineno, all_names[i+1]->lineno ) );
684 free( all_names );
686 if (spec->nb_names)
688 spec->names = xmalloc( spec->nb_names * sizeof(spec->names[0]) );
689 for (i = j = 0; i < spec->nb_entry_points; i++)
690 if (spec->entry_points[i].name) spec->names[j++] = &spec->entry_points[i];
692 /* sort the list of names */
693 qsort( spec->names, spec->nb_names, sizeof(spec->names[0]), name_compare );
697 /*******************************************************************
698 * assign_ordinals
700 * Build the ordinal array.
702 static void assign_ordinals( DLLSPEC *spec )
704 int i, count, ordinal;
706 /* start assigning from base, or from 1 if no ordinal defined yet */
708 spec->base = MAX_ORDINALS;
709 spec->limit = 0;
710 for (i = 0; i < spec->nb_entry_points; i++)
712 ordinal = spec->entry_points[i].ordinal;
713 if (ordinal == -1) continue;
714 if (ordinal > spec->limit) spec->limit = ordinal;
715 if (ordinal < spec->base) spec->base = ordinal;
717 if (spec->base == MAX_ORDINALS) spec->base = 1;
718 if (spec->limit < spec->base) spec->limit = spec->base;
720 count = max( spec->limit + 1, spec->base + spec->nb_entry_points );
721 spec->ordinals = xmalloc( count * sizeof(spec->ordinals[0]) );
722 memset( spec->ordinals, 0, count * sizeof(spec->ordinals[0]) );
724 /* fill in all explicitly specified ordinals */
725 for (i = 0; i < spec->nb_entry_points; i++)
727 ordinal = spec->entry_points[i].ordinal;
728 if (ordinal == -1) continue;
729 if (spec->ordinals[ordinal])
731 current_line = max( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno );
732 error( "ordinal %d redefined\n%s:%d: First defined here\n",
733 ordinal, input_file_name,
734 min( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno ) );
736 else spec->ordinals[ordinal] = &spec->entry_points[i];
739 /* now assign ordinals to the rest */
740 for (i = 0, ordinal = spec->base; i < spec->nb_entry_points; i++)
742 if (spec->entry_points[i].ordinal != -1) continue;
743 while (spec->ordinals[ordinal]) ordinal++;
744 if (ordinal >= MAX_ORDINALS)
746 current_line = spec->entry_points[i].lineno;
747 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
749 spec->entry_points[i].ordinal = ordinal;
750 spec->ordinals[ordinal] = &spec->entry_points[i];
752 if (ordinal > spec->limit) spec->limit = ordinal;
756 /*******************************************************************
757 * add_16bit_exports
759 * Add the necessary exports to the 32-bit counterpart of a 16-bit module.
761 void add_16bit_exports( DLLSPEC *spec32, DLLSPEC *spec16 )
763 int i;
764 ORDDEF *odp;
766 spec32->file_name = xstrdup( spec16->file_name );
767 if (spec16->characteristics & IMAGE_FILE_DLL)
769 spec32->characteristics = IMAGE_FILE_DLL;
770 spec32->init_func = xstrdup( "__wine_spec_dll_entry" );
773 /* add an export for the NE module */
775 odp = add_entry_point( spec32 );
776 odp->type = TYPE_EXTERN;
777 odp->flags = FLAG_PRIVATE;
778 odp->name = xstrdup( "__wine_spec_dos_header" );
779 odp->lineno = 0;
780 odp->ordinal = 1;
781 odp->link_name = xstrdup( ".L__wine_spec_dos_header" );
783 if (spec16->main_module)
785 odp = add_entry_point( spec32 );
786 odp->type = TYPE_EXTERN;
787 odp->flags = FLAG_PRIVATE;
788 odp->name = xstrdup( "__wine_spec_main_module" );
789 odp->lineno = 0;
790 odp->ordinal = 2;
791 odp->link_name = xstrdup( ".L__wine_spec_main_module" );
794 /* add the explicit win32 exports */
796 for (i = 1; i <= spec16->limit; i++)
798 ORDDEF *odp16 = spec16->ordinals[i];
800 if (!odp16 || !odp16->name) continue;
801 if (!(odp16->flags & FLAG_EXPORT32)) continue;
803 odp = add_entry_point( spec32 );
804 odp->flags = odp16->flags & ~FLAG_EXPORT32;
805 odp->type = odp16->type;
806 odp->name = xstrdup( odp16->name );
807 odp->lineno = odp16->lineno;
808 odp->ordinal = -1;
809 odp->link_name = xstrdup( odp16->link_name );
810 odp->u.func.nb_args = odp16->u.func.nb_args;
811 memcpy( odp->u.func.args, odp16->u.func.args, odp->u.func.nb_args * sizeof(odp->u.func.args[0]) );
814 assign_names( spec32 );
815 assign_ordinals( spec32 );
819 /*******************************************************************
820 * parse_spec_file
822 * Parse a .spec file.
824 int parse_spec_file( FILE *file, DLLSPEC *spec )
826 const char *token;
828 input_file = file;
829 current_line = 0;
831 comment_chars = "#;";
832 separator_chars = "()-";
834 while (get_next_line())
836 if (!(token = GetToken(1))) continue;
837 if (strcmp(token, "@") == 0)
839 if (!parse_spec_ordinal( -1, spec )) continue;
841 else if (IsNumberString(token))
843 if (!parse_spec_ordinal( atoi(token), spec )) continue;
845 else
847 error( "Expected ordinal declaration, got '%s'\n", token );
848 continue;
850 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
853 current_line = 0; /* no longer parsing the input file */
854 assign_names( spec );
855 assign_ordinals( spec );
856 return !nb_errors;
860 /*******************************************************************
861 * parse_def_library
863 * Parse a LIBRARY declaration in a .def file.
865 static int parse_def_library( DLLSPEC *spec )
867 const char *token = GetToken(1);
869 if (!token) return 1;
870 if (strcmp( token, "BASE" ))
872 free( spec->file_name );
873 spec->file_name = xstrdup( token );
874 if (!(token = GetToken(1))) return 1;
876 if (strcmp( token, "BASE" ))
878 error( "Expected library name or BASE= declaration, got '%s'\n", token );
879 return 0;
881 if (!(token = GetToken(0))) return 0;
882 if (strcmp( token, "=" ))
884 error( "Expected '=' after BASE, got '%s'\n", token );
885 return 0;
887 if (!(token = GetToken(0))) return 0;
888 /* FIXME: do something with base address */
890 return 1;
894 /*******************************************************************
895 * parse_def_stack_heap_size
897 * Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
899 static int parse_def_stack_heap_size( int is_stack, DLLSPEC *spec )
901 const char *token = GetToken(0);
902 char *end;
903 unsigned long size;
905 if (!token) return 0;
906 size = strtoul( token, &end, 0 );
907 if (*end)
909 error( "Invalid number '%s'\n", token );
910 return 0;
912 if (is_stack) spec->stack_size = size / 1024;
913 else spec->heap_size = size / 1024;
914 if (!(token = GetToken(1))) return 1;
915 if (strcmp( token, "," ))
917 error( "Expected ',' after size, got '%s'\n", token );
918 return 0;
920 if (!(token = GetToken(0))) return 0;
921 /* FIXME: do something with reserve size */
922 return 1;
926 /*******************************************************************
927 * parse_def_export
929 * Parse an export declaration in a .def file.
931 static int parse_def_export( char *name, DLLSPEC *spec )
933 int i, args;
934 const char *token = GetToken(1);
935 ORDDEF *odp = add_entry_point( spec );
937 odp->lineno = current_line;
938 odp->ordinal = -1;
939 odp->name = name;
940 args = remove_stdcall_decoration( odp->name );
941 if (args == -1)
943 odp->type = TYPE_CDECL;
944 args = 0;
946 else
948 odp->type = TYPE_STDCALL;
949 args /= get_ptr_size();
950 if (args >= MAX_ARGUMENTS)
952 error( "Too many arguments in stdcall function '%s'\n", odp->name );
953 return 0;
955 for (i = 0; i < args; i++) odp->u.func.args[i] = ARG_LONG;
957 odp->u.func.nb_args = args;
959 /* check for optional internal name */
961 if (token && !strcmp( token, "=" ))
963 if (!(token = GetToken(0))) goto error;
964 odp->link_name = xstrdup( token );
965 remove_stdcall_decoration( odp->link_name );
966 token = GetToken(1);
968 else
970 odp->link_name = xstrdup( name );
973 /* check for optional ordinal */
975 if (token && token[0] == '@')
977 int ordinal;
979 if (!IsNumberString( token+1 ))
981 error( "Expected number after '@', got '%s'\n", token+1 );
982 goto error;
984 ordinal = atoi( token+1 );
985 if (!ordinal)
987 error( "Ordinal 0 is not valid\n" );
988 goto error;
990 if (ordinal >= MAX_ORDINALS)
992 error( "Ordinal number %d too large\n", ordinal );
993 goto error;
995 odp->ordinal = ordinal;
996 token = GetToken(1);
999 /* check for other optional keywords */
1001 while (token)
1003 if (!strcmp( token, "NONAME" ))
1005 if (odp->ordinal == -1)
1007 error( "NONAME requires an ordinal\n" );
1008 goto error;
1010 odp->export_name = odp->name;
1011 odp->name = NULL;
1012 odp->flags |= FLAG_NONAME;
1014 else if (!strcmp( token, "PRIVATE" ))
1016 odp->flags |= FLAG_PRIVATE;
1018 else if (!strcmp( token, "DATA" ))
1020 odp->type = TYPE_EXTERN;
1022 else
1024 error( "Garbage text '%s' found at end of export declaration\n", token );
1025 goto error;
1027 token = GetToken(1);
1029 return 1;
1031 error:
1032 spec->nb_entry_points--;
1033 free( odp->name );
1034 return 0;
1038 /*******************************************************************
1039 * parse_def_file
1041 * Parse a .def file.
1043 int parse_def_file( FILE *file, DLLSPEC *spec )
1045 const char *token;
1046 int in_exports = 0;
1048 input_file = file;
1049 current_line = 0;
1051 comment_chars = ";";
1052 separator_chars = ",=";
1054 while (get_next_line())
1056 if (!(token = GetToken(1))) continue;
1058 if (!strcmp( token, "LIBRARY" ) || !strcmp( token, "NAME" ))
1060 if (!parse_def_library( spec )) continue;
1061 goto end_of_line;
1063 else if (!strcmp( token, "STACKSIZE" ))
1065 if (!parse_def_stack_heap_size( 1, spec )) continue;
1066 goto end_of_line;
1068 else if (!strcmp( token, "HEAPSIZE" ))
1070 if (!parse_def_stack_heap_size( 0, spec )) continue;
1071 goto end_of_line;
1073 else if (!strcmp( token, "EXPORTS" ))
1075 in_exports = 1;
1076 if (!(token = GetToken(1))) continue;
1078 else if (!strcmp( token, "IMPORTS" ))
1080 in_exports = 0;
1081 if (!(token = GetToken(1))) continue;
1083 else if (!strcmp( token, "SECTIONS" ))
1085 in_exports = 0;
1086 if (!(token = GetToken(1))) continue;
1089 if (!in_exports) continue; /* ignore this line */
1090 if (!parse_def_export( xstrdup(token), spec )) continue;
1092 end_of_line:
1093 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
1096 current_line = 0; /* no longer parsing the input file */
1097 assign_names( spec );
1098 assign_ordinals( spec );
1099 return !nb_errors;