mmdevapi: Add an empty IMMDeviceCollection implementation.
[wine/multimedia.git] / tools / winebuild / parser.c
blobf48471c8c5006f2a66e0732291fd27a0543715ce
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 "extern" /* TYPE_EXTERN */
62 static const char * const FlagNames[] =
64 "norelay", /* FLAG_NORELAY */
65 "noname", /* FLAG_NONAME */
66 "ret16", /* FLAG_RET16 */
67 "ret64", /* FLAG_RET64 */
68 "register", /* FLAG_REGISTER */
69 "private", /* FLAG_PRIVATE */
70 "ordinal", /* FLAG_ORDINAL */
71 NULL
74 static int IsNumberString(const char *s)
76 while (*s) if (!isdigit(*s++)) return 0;
77 return 1;
80 static inline int is_token_separator( char ch )
82 return strchr( separator_chars, ch ) != NULL;
85 static inline int is_token_comment( char ch )
87 return strchr( comment_chars, ch ) != NULL;
90 /* get the next line from the input file, or return 0 if at eof */
91 static int get_next_line(void)
93 ParseNext = ParseBuffer;
94 current_line++;
95 return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
98 static const char * GetToken( int allow_eol )
100 char *p = ParseNext;
101 char *token = TokenBuffer;
103 for (;;)
105 /* remove initial white space */
106 p = ParseNext;
107 while (isspace(*p)) p++;
109 if (*p == '\\' && p[1] == '\n') /* line continuation */
111 if (!get_next_line())
113 if (!allow_eol) error( "Unexpected end of file\n" );
114 return NULL;
117 else break;
120 if ((*p == '\0') || is_token_comment(*p))
122 if (!allow_eol) error( "Declaration not terminated properly\n" );
123 return NULL;
127 * Find end of token.
129 if (is_token_separator(*p))
131 /* a separator is always a complete token */
132 *token++ = *p++;
134 else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
136 if (*p == '\\') p++;
137 if (*p) *token++ = *p++;
139 *token = '\0';
140 ParseNext = p;
141 return TokenBuffer;
145 static ORDDEF *add_entry_point( DLLSPEC *spec )
147 ORDDEF *ret;
149 if (spec->nb_entry_points == spec->alloc_entry_points)
151 spec->alloc_entry_points += 128;
152 spec->entry_points = xrealloc( spec->entry_points,
153 spec->alloc_entry_points * sizeof(*spec->entry_points) );
155 ret = &spec->entry_points[spec->nb_entry_points++];
156 memset( ret, 0, sizeof(*ret) );
157 return ret;
160 /*******************************************************************
161 * parse_spec_variable
163 * Parse a variable definition in a .spec file.
165 static int parse_spec_variable( ORDDEF *odp, DLLSPEC *spec )
167 char *endptr;
168 int *value_array;
169 int n_values;
170 int value_array_size;
171 const char *token;
173 if (spec->type == SPEC_WIN32)
175 error( "'variable' not supported in Win32, use 'extern' instead\n" );
176 return 0;
179 if (!(token = GetToken(0))) return 0;
180 if (*token != '(')
182 error( "Expected '(' got '%s'\n", token );
183 return 0;
186 n_values = 0;
187 value_array_size = 25;
188 value_array = xmalloc(sizeof(*value_array) * value_array_size);
190 for (;;)
192 if (!(token = GetToken(0)))
194 free( value_array );
195 return 0;
197 if (*token == ')')
198 break;
200 value_array[n_values++] = strtol(token, &endptr, 0);
201 if (n_values == value_array_size)
203 value_array_size += 25;
204 value_array = xrealloc(value_array,
205 sizeof(*value_array) * value_array_size);
208 if (endptr == NULL || *endptr != '\0')
210 error( "Expected number value, got '%s'\n", token );
211 free( value_array );
212 return 0;
216 odp->u.var.n_values = n_values;
217 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
218 return 1;
222 /*******************************************************************
223 * parse_spec_export
225 * Parse an exported function definition in a .spec file.
227 static int parse_spec_export( ORDDEF *odp, DLLSPEC *spec )
229 const char *token;
230 unsigned int i;
231 int is_win32 = (spec->type == SPEC_WIN32) || (odp->flags & FLAG_EXPORT32);
233 if (!is_win32 && odp->type == TYPE_STDCALL)
235 error( "'stdcall' not supported for Win16\n" );
236 return 0;
238 if (is_win32 && odp->type == TYPE_PASCAL)
240 error( "'pascal' not supported for Win32\n" );
241 return 0;
244 if (!(token = GetToken(0))) return 0;
245 if (*token != '(')
247 error( "Expected '(' got '%s'\n", token );
248 return 0;
251 for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
253 if (!(token = GetToken(0))) return 0;
254 if (*token == ')')
255 break;
257 if (!strcmp(token, "word"))
258 odp->u.func.arg_types[i] = 'w';
259 else if (!strcmp(token, "s_word"))
260 odp->u.func.arg_types[i] = 's';
261 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
262 odp->u.func.arg_types[i] = 'l';
263 else if (!strcmp(token, "ptr"))
264 odp->u.func.arg_types[i] = 'p';
265 else if (!strcmp(token, "str"))
266 odp->u.func.arg_types[i] = 't';
267 else if (!strcmp(token, "wstr"))
268 odp->u.func.arg_types[i] = 'W';
269 else if (!strcmp(token, "segstr"))
270 odp->u.func.arg_types[i] = 'T';
271 else if (!strcmp(token, "double"))
273 odp->u.func.arg_types[i++] = 'l';
274 if (get_ptr_size() == 4 && i < sizeof(odp->u.func.arg_types))
275 odp->u.func.arg_types[i] = 'l';
277 else
279 error( "Unknown argument type '%s'\n", token );
280 return 0;
283 if (is_win32)
285 if (strcmp(token, "long") &&
286 strcmp(token, "ptr") &&
287 strcmp(token, "str") &&
288 strcmp(token, "wstr") &&
289 strcmp(token, "double"))
291 error( "Type '%s' not supported for Win32 function\n", token );
292 return 0;
296 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
298 error( "Too many arguments\n" );
299 return 0;
302 odp->u.func.arg_types[i] = '\0';
303 if (odp->type == TYPE_VARARGS)
304 odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
306 if (!(token = GetToken(1)))
308 if (!strcmp( odp->name, "@" ))
310 error( "Missing handler name for anonymous function\n" );
311 return 0;
313 odp->link_name = xstrdup( odp->name );
315 else
317 odp->link_name = xstrdup( token );
318 if (strchr( odp->link_name, '.' ))
320 if (!is_win32)
322 error( "Forwarded functions not supported for Win16\n" );
323 return 0;
325 odp->flags |= FLAG_FORWARD;
328 return 1;
332 /*******************************************************************
333 * parse_spec_equate
335 * Parse an 'equate' definition in a .spec file.
337 static int parse_spec_equate( ORDDEF *odp, DLLSPEC *spec )
339 char *endptr;
340 int value;
341 const char *token;
343 if (spec->type == SPEC_WIN32)
345 error( "'equate' not supported for Win32\n" );
346 return 0;
348 if (!(token = GetToken(0))) return 0;
349 value = strtol(token, &endptr, 0);
350 if (endptr == NULL || *endptr != '\0')
352 error( "Expected number value, got '%s'\n", token );
353 return 0;
355 if (value < -0x8000 || value > 0xffff)
357 error( "Value %d for absolute symbol doesn't fit in 16 bits\n", value );
358 value = 0;
360 odp->u.abs.value = value;
361 return 1;
365 /*******************************************************************
366 * parse_spec_stub
368 * Parse a 'stub' definition in a .spec file
370 static int parse_spec_stub( ORDDEF *odp, DLLSPEC *spec )
372 odp->u.func.arg_types[0] = '\0';
373 odp->link_name = xstrdup("");
374 odp->flags |= FLAG_CPU(CPU_x86) | FLAG_CPU(CPU_x86_64); /* don't bother generating stubs for Winelib */
375 return 1;
379 /*******************************************************************
380 * parse_spec_extern
382 * Parse an 'extern' definition in a .spec file.
384 static int parse_spec_extern( ORDDEF *odp, DLLSPEC *spec )
386 const char *token;
388 if (spec->type == SPEC_WIN16)
390 error( "'extern' not supported for Win16, use 'variable' instead\n" );
391 return 0;
393 if (!(token = GetToken(1)))
395 if (!strcmp( odp->name, "@" ))
397 error( "Missing handler name for anonymous extern\n" );
398 return 0;
400 odp->link_name = xstrdup( odp->name );
402 else
404 odp->link_name = xstrdup( token );
405 if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
407 return 1;
411 /*******************************************************************
412 * parse_spec_flags
414 * Parse the optional flags for an entry point in a .spec file.
416 static const char *parse_spec_flags( DLLSPEC *spec, ORDDEF *odp )
418 unsigned int i;
419 const char *token;
423 if (!(token = GetToken(0))) break;
424 if (!strncmp( token, "arch=", 5))
426 char *args = xstrdup( token + 5 );
427 char *cpu_name = strtok( args, "," );
428 while (cpu_name)
430 if (!strcmp( cpu_name, "win32" ))
432 if (spec->type == SPEC_WIN32)
433 odp->flags |= FLAG_CPU_WIN32;
434 else
435 odp->flags |= FLAG_EXPORT32;
437 else if (!strcmp( cpu_name, "win64" ))
438 odp->flags |= FLAG_CPU_WIN64;
439 else
441 enum target_cpu cpu = get_cpu_from_name( cpu_name );
442 if (cpu == -1)
444 error( "Unknown architecture '%s'\n", cpu_name );
445 return NULL;
447 odp->flags |= FLAG_CPU( cpu );
449 cpu_name = strtok( NULL, "," );
451 free( args );
453 else if (!strcmp( token, "i386" )) /* backwards compatibility */
455 odp->flags |= FLAG_CPU(CPU_x86);
457 else
459 for (i = 0; FlagNames[i]; i++)
460 if (!strcmp( FlagNames[i], token )) break;
461 if (!FlagNames[i])
463 error( "Unknown flag '%s'\n", token );
464 return NULL;
466 odp->flags |= 1 << i;
468 token = GetToken(0);
469 } while (token && *token == '-');
471 return token;
475 /*******************************************************************
476 * parse_spec_ordinal
478 * Parse an ordinal definition in a .spec file.
480 static int parse_spec_ordinal( int ordinal, DLLSPEC *spec )
482 const char *token;
483 size_t len;
484 ORDDEF *odp = add_entry_point( spec );
486 if (!(token = GetToken(0))) goto error;
488 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
489 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
490 break;
492 if (odp->type >= TYPE_NBTYPES)
494 error( "Expected type after ordinal, found '%s' instead\n", token );
495 goto error;
498 if (!(token = GetToken(0))) goto error;
499 if (*token == '-' && !(token = parse_spec_flags( spec, odp ))) goto error;
501 if (ordinal == -1 && spec->type != SPEC_WIN32 && !(odp->flags & FLAG_EXPORT32))
503 error( "'@' ordinals not supported for Win16\n" );
504 goto error;
507 odp->name = xstrdup( token );
508 odp->lineno = current_line;
509 odp->ordinal = ordinal;
511 len = strspn( odp->name, valid_ordname_chars );
512 if (len < strlen( odp->name ))
514 error( "Character '%c' is not allowed in exported name '%s'\n", odp->name[len], odp->name );
515 goto error;
518 switch(odp->type)
520 case TYPE_VARIABLE:
521 if (!parse_spec_variable( odp, spec )) goto error;
522 break;
523 case TYPE_PASCAL:
524 case TYPE_STDCALL:
525 case TYPE_VARARGS:
526 case TYPE_CDECL:
527 if (!parse_spec_export( odp, spec )) goto error;
528 break;
529 case TYPE_ABS:
530 if (!parse_spec_equate( odp, spec )) goto error;
531 break;
532 case TYPE_STUB:
533 if (!parse_spec_stub( odp, spec )) goto error;
534 break;
535 case TYPE_EXTERN:
536 if (!parse_spec_extern( odp, spec )) goto error;
537 break;
538 default:
539 assert( 0 );
542 if ((odp->flags & FLAG_CPU_MASK) && !(odp->flags & FLAG_CPU(target_cpu)))
544 /* ignore this entry point */
545 spec->nb_entry_points--;
546 return 1;
549 if (ordinal != -1)
551 if (!ordinal)
553 error( "Ordinal 0 is not valid\n" );
554 goto error;
556 if (ordinal >= MAX_ORDINALS)
558 error( "Ordinal number %d too large\n", ordinal );
559 goto error;
561 if (ordinal > spec->limit) spec->limit = ordinal;
562 if (ordinal < spec->base) spec->base = ordinal;
563 odp->ordinal = ordinal;
566 if (odp->type == TYPE_STDCALL && !(odp->flags & FLAG_PRIVATE))
568 if (!strcmp( odp->name, "DllRegisterServer" ) ||
569 !strcmp( odp->name, "DllUnregisterServer" ) ||
570 !strcmp( odp->name, "DllGetClassObject" ) ||
571 !strcmp( odp->name, "DllGetVersion" ) ||
572 !strcmp( odp->name, "DllInstall" ) ||
573 !strcmp( odp->name, "DllCanUnloadNow" ))
575 warning( "Function %s should be marked private\n", odp->name );
576 if (strcmp( odp->name, odp->link_name ))
577 warning( "Function %s should not use a different internal name (%s)\n",
578 odp->name, odp->link_name );
582 if (!strcmp( odp->name, "@" ) || odp->flags & (FLAG_NONAME | FLAG_ORDINAL))
584 if (ordinal == -1)
586 if (!strcmp( odp->name, "@" ))
587 error( "Nameless function needs an explicit ordinal number\n" );
588 else
589 error( "Function imported by ordinal needs an explicit ordinal number\n" );
590 goto error;
592 if (spec->type != SPEC_WIN32)
594 error( "Nameless functions not supported for Win16\n" );
595 goto error;
597 if (!strcmp( odp->name, "@" ))
599 free( odp->name );
600 odp->name = NULL;
602 else if (!(odp->flags & FLAG_ORDINAL)) /* -ordinal only affects the import library */
604 odp->export_name = odp->name;
605 odp->name = NULL;
608 return 1;
610 error:
611 spec->nb_entry_points--;
612 free( odp->name );
613 return 0;
617 static int name_compare( const void *ptr1, const void *ptr2 )
619 const ORDDEF *odp1 = *(const ORDDEF * const *)ptr1;
620 const ORDDEF *odp2 = *(const ORDDEF * const *)ptr2;
621 const char *name1 = odp1->name ? odp1->name : odp1->export_name;
622 const char *name2 = odp2->name ? odp2->name : odp2->export_name;
623 return strcmp( name1, name2 );
626 /*******************************************************************
627 * assign_names
629 * Build the name array and catch duplicates.
631 static void assign_names( DLLSPEC *spec )
633 int i, j, nb_exp_names = 0;
634 ORDDEF **all_names;
636 spec->nb_names = 0;
637 for (i = 0; i < spec->nb_entry_points; i++)
638 if (spec->entry_points[i].name) spec->nb_names++;
639 else if (spec->entry_points[i].export_name) nb_exp_names++;
641 if (!spec->nb_names && !nb_exp_names) return;
643 /* check for duplicates */
645 all_names = xmalloc( (spec->nb_names + nb_exp_names) * sizeof(all_names[0]) );
646 for (i = j = 0; i < spec->nb_entry_points; i++)
647 if (spec->entry_points[i].name || spec->entry_points[i].export_name)
648 all_names[j++] = &spec->entry_points[i];
650 qsort( all_names, j, sizeof(all_names[0]), name_compare );
652 for (i = 0; i < j - 1; i++)
654 const char *name1 = all_names[i]->name ? all_names[i]->name : all_names[i]->export_name;
655 const char *name2 = all_names[i+1]->name ? all_names[i+1]->name : all_names[i+1]->export_name;
656 if (!strcmp( name1, name2 ) &&
657 !((all_names[i]->flags ^ all_names[i+1]->flags) & FLAG_EXPORT32))
659 current_line = max( all_names[i]->lineno, all_names[i+1]->lineno );
660 error( "'%s' redefined\n%s:%d: First defined here\n",
661 name1, input_file_name,
662 min( all_names[i]->lineno, all_names[i+1]->lineno ) );
665 free( all_names );
667 if (spec->nb_names)
669 spec->names = xmalloc( spec->nb_names * sizeof(spec->names[0]) );
670 for (i = j = 0; i < spec->nb_entry_points; i++)
671 if (spec->entry_points[i].name) spec->names[j++] = &spec->entry_points[i];
673 /* sort the list of names */
674 qsort( spec->names, spec->nb_names, sizeof(spec->names[0]), name_compare );
678 /*******************************************************************
679 * assign_ordinals
681 * Build the ordinal array.
683 static void assign_ordinals( DLLSPEC *spec )
685 int i, count, ordinal;
687 /* start assigning from base, or from 1 if no ordinal defined yet */
689 spec->base = MAX_ORDINALS;
690 spec->limit = 0;
691 for (i = 0; i < spec->nb_entry_points; i++)
693 ordinal = spec->entry_points[i].ordinal;
694 if (ordinal == -1) continue;
695 if (ordinal > spec->limit) spec->limit = ordinal;
696 if (ordinal < spec->base) spec->base = ordinal;
698 if (spec->base == MAX_ORDINALS) spec->base = 1;
699 if (spec->limit < spec->base) spec->limit = spec->base;
701 count = max( spec->limit + 1, spec->base + spec->nb_entry_points );
702 spec->ordinals = xmalloc( count * sizeof(spec->ordinals[0]) );
703 memset( spec->ordinals, 0, count * sizeof(spec->ordinals[0]) );
705 /* fill in all explicitly specified ordinals */
706 for (i = 0; i < spec->nb_entry_points; i++)
708 ordinal = spec->entry_points[i].ordinal;
709 if (ordinal == -1) continue;
710 if (spec->ordinals[ordinal])
712 current_line = max( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno );
713 error( "ordinal %d redefined\n%s:%d: First defined here\n",
714 ordinal, input_file_name,
715 min( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno ) );
717 else spec->ordinals[ordinal] = &spec->entry_points[i];
720 /* now assign ordinals to the rest */
721 for (i = 0, ordinal = spec->base; i < spec->nb_entry_points; i++)
723 if (spec->entry_points[i].ordinal != -1) continue;
724 while (spec->ordinals[ordinal]) ordinal++;
725 if (ordinal >= MAX_ORDINALS)
727 current_line = spec->entry_points[i].lineno;
728 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
730 spec->entry_points[i].ordinal = ordinal;
731 spec->ordinals[ordinal] = &spec->entry_points[i];
733 if (ordinal > spec->limit) spec->limit = ordinal;
737 /*******************************************************************
738 * add_16bit_exports
740 * Add the necessary exports to the 32-bit counterpart of a 16-bit module.
742 void add_16bit_exports( DLLSPEC *spec32, DLLSPEC *spec16 )
744 int i;
745 ORDDEF *odp;
747 spec32->file_name = xstrdup( spec16->file_name );
748 if (spec16->characteristics & IMAGE_FILE_DLL)
750 spec32->characteristics = IMAGE_FILE_DLL;
751 spec32->init_func = xstrdup( "__wine_spec_dll_entry" );
754 /* add an export for the NE module */
756 odp = add_entry_point( spec32 );
757 odp->type = TYPE_EXTERN;
758 odp->flags = FLAG_PRIVATE;
759 odp->name = xstrdup( "__wine_spec_dos_header" );
760 odp->lineno = 0;
761 odp->ordinal = 1;
762 odp->link_name = xstrdup( ".L__wine_spec_dos_header" );
764 if (spec16->main_module)
766 odp = add_entry_point( spec32 );
767 odp->type = TYPE_EXTERN;
768 odp->flags = FLAG_PRIVATE;
769 odp->name = xstrdup( "__wine_spec_main_module" );
770 odp->lineno = 0;
771 odp->ordinal = 2;
772 odp->link_name = xstrdup( ".L__wine_spec_main_module" );
775 /* add the explicit win32 exports */
777 for (i = 1; i <= spec16->limit; i++)
779 ORDDEF *odp16 = spec16->ordinals[i];
781 if (!odp16 || !odp16->name) continue;
782 if (!(odp16->flags & FLAG_EXPORT32)) continue;
784 odp = add_entry_point( spec32 );
785 odp->flags = odp16->flags & ~FLAG_EXPORT32;
786 odp->type = odp16->type;
787 odp->name = xstrdup( odp16->name );
788 odp->lineno = odp16->lineno;
789 odp->ordinal = -1;
790 odp->link_name = xstrdup( odp16->link_name );
791 strcpy( odp->u.func.arg_types, odp16->u.func.arg_types );
794 assign_names( spec32 );
795 assign_ordinals( spec32 );
799 /*******************************************************************
800 * parse_spec_file
802 * Parse a .spec file.
804 int parse_spec_file( FILE *file, DLLSPEC *spec )
806 const char *token;
808 input_file = file;
809 current_line = 0;
811 comment_chars = "#;";
812 separator_chars = "()-";
814 while (get_next_line())
816 if (!(token = GetToken(1))) continue;
817 if (strcmp(token, "@") == 0)
819 if (!parse_spec_ordinal( -1, spec )) continue;
821 else if (IsNumberString(token))
823 if (!parse_spec_ordinal( atoi(token), spec )) continue;
825 else
827 error( "Expected ordinal declaration, got '%s'\n", token );
828 continue;
830 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
833 current_line = 0; /* no longer parsing the input file */
834 assign_names( spec );
835 assign_ordinals( spec );
836 return !nb_errors;
840 /*******************************************************************
841 * parse_def_library
843 * Parse a LIBRARY declaration in a .def file.
845 static int parse_def_library( DLLSPEC *spec )
847 const char *token = GetToken(1);
849 if (!token) return 1;
850 if (strcmp( token, "BASE" ))
852 free( spec->file_name );
853 spec->file_name = xstrdup( token );
854 if (!(token = GetToken(1))) return 1;
856 if (strcmp( token, "BASE" ))
858 error( "Expected library name or BASE= declaration, got '%s'\n", token );
859 return 0;
861 if (!(token = GetToken(0))) return 0;
862 if (strcmp( token, "=" ))
864 error( "Expected '=' after BASE, got '%s'\n", token );
865 return 0;
867 if (!(token = GetToken(0))) return 0;
868 /* FIXME: do something with base address */
870 return 1;
874 /*******************************************************************
875 * parse_def_stack_heap_size
877 * Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
879 static int parse_def_stack_heap_size( int is_stack, DLLSPEC *spec )
881 const char *token = GetToken(0);
882 char *end;
883 unsigned long size;
885 if (!token) return 0;
886 size = strtoul( token, &end, 0 );
887 if (*end)
889 error( "Invalid number '%s'\n", token );
890 return 0;
892 if (is_stack) spec->stack_size = size / 1024;
893 else spec->heap_size = size / 1024;
894 if (!(token = GetToken(1))) return 1;
895 if (strcmp( token, "," ))
897 error( "Expected ',' after size, got '%s'\n", token );
898 return 0;
900 if (!(token = GetToken(0))) return 0;
901 /* FIXME: do something with reserve size */
902 return 1;
906 /*******************************************************************
907 * parse_def_export
909 * Parse an export declaration in a .def file.
911 static int parse_def_export( char *name, DLLSPEC *spec )
913 int i, args;
914 const char *token = GetToken(1);
915 ORDDEF *odp = add_entry_point( spec );
917 odp->lineno = current_line;
918 odp->ordinal = -1;
919 odp->name = name;
920 args = remove_stdcall_decoration( odp->name );
921 if (args == -1) odp->type = TYPE_CDECL;
922 else
924 odp->type = TYPE_STDCALL;
925 args /= get_ptr_size();
926 if (args >= sizeof(odp->u.func.arg_types))
928 error( "Too many arguments in stdcall function '%s'\n", odp->name );
929 return 0;
931 for (i = 0; i < args; i++) odp->u.func.arg_types[i] = 'l';
934 /* check for optional internal name */
936 if (token && !strcmp( token, "=" ))
938 if (!(token = GetToken(0))) goto error;
939 odp->link_name = xstrdup( token );
940 remove_stdcall_decoration( odp->link_name );
941 token = GetToken(1);
943 else
945 odp->link_name = xstrdup( name );
948 /* check for optional ordinal */
950 if (token && token[0] == '@')
952 int ordinal;
954 if (!IsNumberString( token+1 ))
956 error( "Expected number after '@', got '%s'\n", token+1 );
957 goto error;
959 ordinal = atoi( token+1 );
960 if (!ordinal)
962 error( "Ordinal 0 is not valid\n" );
963 goto error;
965 if (ordinal >= MAX_ORDINALS)
967 error( "Ordinal number %d too large\n", ordinal );
968 goto error;
970 odp->ordinal = ordinal;
971 token = GetToken(1);
974 /* check for other optional keywords */
976 while (token)
978 if (!strcmp( token, "NONAME" ))
980 if (odp->ordinal == -1)
982 error( "NONAME requires an ordinal\n" );
983 goto error;
985 odp->export_name = odp->name;
986 odp->name = NULL;
987 odp->flags |= FLAG_NONAME;
989 else if (!strcmp( token, "PRIVATE" ))
991 odp->flags |= FLAG_PRIVATE;
993 else if (!strcmp( token, "DATA" ))
995 odp->type = TYPE_EXTERN;
997 else
999 error( "Garbage text '%s' found at end of export declaration\n", token );
1000 goto error;
1002 token = GetToken(1);
1004 return 1;
1006 error:
1007 spec->nb_entry_points--;
1008 free( odp->name );
1009 return 0;
1013 /*******************************************************************
1014 * parse_def_file
1016 * Parse a .def file.
1018 int parse_def_file( FILE *file, DLLSPEC *spec )
1020 const char *token;
1021 int in_exports = 0;
1023 input_file = file;
1024 current_line = 0;
1026 comment_chars = ";";
1027 separator_chars = ",=";
1029 while (get_next_line())
1031 if (!(token = GetToken(1))) continue;
1033 if (!strcmp( token, "LIBRARY" ) || !strcmp( token, "NAME" ))
1035 if (!parse_def_library( spec )) continue;
1036 goto end_of_line;
1038 else if (!strcmp( token, "STACKSIZE" ))
1040 if (!parse_def_stack_heap_size( 1, spec )) continue;
1041 goto end_of_line;
1043 else if (!strcmp( token, "HEAPSIZE" ))
1045 if (!parse_def_stack_heap_size( 0, spec )) continue;
1046 goto end_of_line;
1048 else if (!strcmp( token, "EXPORTS" ))
1050 in_exports = 1;
1051 if (!(token = GetToken(1))) continue;
1053 else if (!strcmp( token, "IMPORTS" ))
1055 in_exports = 0;
1056 if (!(token = GetToken(1))) continue;
1058 else if (!strcmp( token, "SECTIONS" ))
1060 in_exports = 0;
1061 if (!(token = GetToken(1))) continue;
1064 if (!in_exports) continue; /* ignore this line */
1065 if (!parse_def_export( xstrdup(token), spec )) continue;
1067 end_of_line:
1068 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
1071 current_line = 0; /* no longer parsing the input file */
1072 assign_names( spec );
1073 assign_ordinals( spec );
1074 return !nb_errors;