push c6aba86b6d7f7dbe994528d67faed47594362b60
[wine/hacks.git] / tools / winebuild / parser.c
blob59640d8f26a22a69672160c3defd78aa1dbf9b1b
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;
232 switch(spec->type)
234 case SPEC_WIN16:
235 if (odp->type == TYPE_STDCALL)
237 error( "'stdcall' not supported for Win16\n" );
238 return 0;
240 break;
241 case SPEC_WIN32:
242 if (odp->type == TYPE_PASCAL)
244 error( "'pascal' not supported for Win32\n" );
245 return 0;
247 break;
248 default:
249 break;
252 if (!(token = GetToken(0))) return 0;
253 if (*token != '(')
255 error( "Expected '(' got '%s'\n", token );
256 return 0;
259 for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
261 if (!(token = GetToken(0))) return 0;
262 if (*token == ')')
263 break;
265 if (!strcmp(token, "word"))
266 odp->u.func.arg_types[i] = 'w';
267 else if (!strcmp(token, "s_word"))
268 odp->u.func.arg_types[i] = 's';
269 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
270 odp->u.func.arg_types[i] = 'l';
271 else if (!strcmp(token, "ptr"))
272 odp->u.func.arg_types[i] = 'p';
273 else if (!strcmp(token, "str"))
274 odp->u.func.arg_types[i] = 't';
275 else if (!strcmp(token, "wstr"))
276 odp->u.func.arg_types[i] = 'W';
277 else if (!strcmp(token, "segstr"))
278 odp->u.func.arg_types[i] = 'T';
279 else if (!strcmp(token, "double"))
281 odp->u.func.arg_types[i++] = 'l';
282 if (get_ptr_size() == 4 && i < sizeof(odp->u.func.arg_types))
283 odp->u.func.arg_types[i] = 'l';
285 else
287 error( "Unknown argument type '%s'\n", token );
288 return 0;
291 if (spec->type == SPEC_WIN32)
293 if (strcmp(token, "long") &&
294 strcmp(token, "ptr") &&
295 strcmp(token, "str") &&
296 strcmp(token, "wstr") &&
297 strcmp(token, "double"))
299 error( "Type '%s' not supported for Win32\n", token );
300 return 0;
304 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
306 error( "Too many arguments\n" );
307 return 0;
310 odp->u.func.arg_types[i] = '\0';
311 if (odp->type == TYPE_VARARGS)
312 odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
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 (spec->type == SPEC_WIN16)
330 error( "Forwarded functions not supported for Win16\n" );
331 return 0;
333 odp->flags |= FLAG_FORWARD;
336 return 1;
340 /*******************************************************************
341 * parse_spec_equate
343 * Parse an 'equate' definition in a .spec file.
345 static int parse_spec_equate( ORDDEF *odp, DLLSPEC *spec )
347 char *endptr;
348 int value;
349 const char *token;
351 if (spec->type == SPEC_WIN32)
353 error( "'equate' not supported for Win32\n" );
354 return 0;
356 if (!(token = GetToken(0))) return 0;
357 value = strtol(token, &endptr, 0);
358 if (endptr == NULL || *endptr != '\0')
360 error( "Expected number value, got '%s'\n", token );
361 return 0;
363 if (value < -0x8000 || value > 0xffff)
365 error( "Value %d for absolute symbol doesn't fit in 16 bits\n", value );
366 value = 0;
368 odp->u.abs.value = value;
369 return 1;
373 /*******************************************************************
374 * parse_spec_stub
376 * Parse a 'stub' definition in a .spec file
378 static int parse_spec_stub( ORDDEF *odp, DLLSPEC *spec )
380 odp->u.func.arg_types[0] = '\0';
381 odp->link_name = xstrdup("");
382 odp->flags |= FLAG_CPU(CPU_x86) | FLAG_CPU(CPU_x86_64); /* don't bother generating stubs for Winelib */
383 return 1;
387 /*******************************************************************
388 * parse_spec_extern
390 * Parse an 'extern' definition in a .spec file.
392 static int parse_spec_extern( ORDDEF *odp, DLLSPEC *spec )
394 const char *token;
396 if (spec->type == SPEC_WIN16)
398 error( "'extern' not supported for Win16, use 'variable' instead\n" );
399 return 0;
401 if (!(token = GetToken(1)))
403 if (!strcmp( odp->name, "@" ))
405 error( "Missing handler name for anonymous extern\n" );
406 return 0;
408 odp->link_name = xstrdup( odp->name );
410 else
412 odp->link_name = xstrdup( token );
413 if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
415 return 1;
419 /*******************************************************************
420 * parse_spec_flags
422 * Parse the optional flags for an entry point in a .spec file.
424 static const char *parse_spec_flags( ORDDEF *odp )
426 unsigned int i;
427 const char *token;
431 if (!(token = GetToken(0))) break;
432 if (!strncmp( token, "arch=", 5))
434 char *args = xstrdup( token + 5 );
435 char *cpu_name = strtok( args, "," );
436 while (cpu_name)
438 enum target_cpu cpu = get_cpu_from_name( cpu_name );
439 if (cpu == -1)
441 error( "Unknown architecture '%s'\n", cpu_name );
442 return NULL;
444 odp->flags |= FLAG_CPU( cpu );
445 cpu_name = strtok( NULL, "," );
447 free( args );
449 else if (!strcmp( token, "i386" )) /* backwards compatibility */
451 odp->flags |= FLAG_CPU(CPU_x86);
453 else
455 for (i = 0; FlagNames[i]; i++)
456 if (!strcmp( FlagNames[i], token )) break;
457 if (!FlagNames[i])
459 error( "Unknown flag '%s'\n", token );
460 return NULL;
462 odp->flags |= 1 << i;
464 token = GetToken(0);
465 } while (token && *token == '-');
467 return token;
471 /*******************************************************************
472 * parse_spec_ordinal
474 * Parse an ordinal definition in a .spec file.
476 static int parse_spec_ordinal( int ordinal, DLLSPEC *spec )
478 const char *token;
479 size_t len;
480 ORDDEF *odp = add_entry_point( spec );
482 if (!(token = GetToken(0))) goto error;
484 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
485 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
486 break;
488 if (odp->type >= TYPE_NBTYPES)
490 error( "Expected type after ordinal, found '%s' instead\n", token );
491 goto error;
494 if (!(token = GetToken(0))) goto error;
495 if (*token == '-' && !(token = parse_spec_flags( odp ))) goto error;
497 odp->name = xstrdup( token );
498 odp->lineno = current_line;
499 odp->ordinal = ordinal;
501 len = strspn( odp->name, valid_ordname_chars );
502 if (len < strlen( odp->name ))
504 error( "Character '%c' is not allowed in exported name '%s'\n", odp->name[len], odp->name );
505 goto error;
508 switch(odp->type)
510 case TYPE_VARIABLE:
511 if (!parse_spec_variable( odp, spec )) goto error;
512 break;
513 case TYPE_PASCAL:
514 case TYPE_STDCALL:
515 case TYPE_VARARGS:
516 case TYPE_CDECL:
517 if (!parse_spec_export( odp, spec )) goto error;
518 break;
519 case TYPE_ABS:
520 if (!parse_spec_equate( odp, spec )) goto error;
521 break;
522 case TYPE_STUB:
523 if (!parse_spec_stub( odp, spec )) goto error;
524 break;
525 case TYPE_EXTERN:
526 if (!parse_spec_extern( odp, spec )) goto error;
527 break;
528 default:
529 assert( 0 );
532 if ((odp->flags & FLAG_CPU_MASK) && !(odp->flags & FLAG_CPU(target_cpu)))
534 /* ignore this entry point */
535 spec->nb_entry_points--;
536 return 1;
539 if (ordinal != -1)
541 if (!ordinal)
543 error( "Ordinal 0 is not valid\n" );
544 goto error;
546 if (ordinal >= MAX_ORDINALS)
548 error( "Ordinal number %d too large\n", ordinal );
549 goto error;
551 if (ordinal > spec->limit) spec->limit = ordinal;
552 if (ordinal < spec->base) spec->base = ordinal;
553 odp->ordinal = ordinal;
556 if (odp->type == TYPE_STDCALL && !(odp->flags & FLAG_PRIVATE))
558 if (!strcmp( odp->name, "DllRegisterServer" ) ||
559 !strcmp( odp->name, "DllUnregisterServer" ) ||
560 !strcmp( odp->name, "DllGetClassObject" ) ||
561 !strcmp( odp->name, "DllGetVersion" ) ||
562 !strcmp( odp->name, "DllInstall" ) ||
563 !strcmp( odp->name, "DllCanUnloadNow" ))
565 warning( "Function %s should be marked private\n", odp->name );
566 if (strcmp( odp->name, odp->link_name ))
567 warning( "Function %s should not use a different internal name (%s)\n",
568 odp->name, odp->link_name );
572 if (!strcmp( odp->name, "@" ) || odp->flags & (FLAG_NONAME | FLAG_ORDINAL))
574 if (ordinal == -1)
576 if (!strcmp( odp->name, "@" ))
577 error( "Nameless function needs an explicit ordinal number\n" );
578 else
579 error( "Function imported by ordinal needs an explicit ordinal number\n" );
580 goto error;
582 if (spec->type != SPEC_WIN32)
584 error( "Nameless functions not supported for Win16\n" );
585 goto error;
587 if (!strcmp( odp->name, "@" ))
589 free( odp->name );
590 odp->name = NULL;
592 else if (!(odp->flags & FLAG_ORDINAL)) /* -ordinal only affects the import library */
594 odp->export_name = odp->name;
595 odp->name = NULL;
598 return 1;
600 error:
601 spec->nb_entry_points--;
602 free( odp->name );
603 return 0;
607 static int name_compare( const void *ptr1, const void *ptr2 )
609 const ORDDEF *odp1 = *(const ORDDEF * const *)ptr1;
610 const ORDDEF *odp2 = *(const ORDDEF * const *)ptr2;
611 const char *name1 = odp1->name ? odp1->name : odp1->export_name;
612 const char *name2 = odp2->name ? odp2->name : odp2->export_name;
613 return strcmp( name1, name2 );
616 /*******************************************************************
617 * assign_names
619 * Build the name array and catch duplicates.
621 static void assign_names( DLLSPEC *spec )
623 int i, j, nb_exp_names = 0;
624 ORDDEF **all_names;
626 spec->nb_names = 0;
627 for (i = 0; i < spec->nb_entry_points; i++)
628 if (spec->entry_points[i].name) spec->nb_names++;
629 else if (spec->entry_points[i].export_name) nb_exp_names++;
631 if (!spec->nb_names && !nb_exp_names) return;
633 /* check for duplicates */
635 all_names = xmalloc( (spec->nb_names + nb_exp_names) * sizeof(all_names[0]) );
636 for (i = j = 0; i < spec->nb_entry_points; i++)
637 if (spec->entry_points[i].name || spec->entry_points[i].export_name)
638 all_names[j++] = &spec->entry_points[i];
640 qsort( all_names, j, sizeof(all_names[0]), name_compare );
642 for (i = 0; i < j - 1; i++)
644 const char *name1 = all_names[i]->name ? all_names[i]->name : all_names[i]->export_name;
645 const char *name2 = all_names[i+1]->name ? all_names[i+1]->name : all_names[i+1]->export_name;
646 if (!strcmp( name1, name2 ))
648 current_line = max( all_names[i]->lineno, all_names[i+1]->lineno );
649 error( "'%s' redefined\n%s:%d: First defined here\n",
650 name1, input_file_name,
651 min( all_names[i]->lineno, all_names[i+1]->lineno ) );
654 free( all_names );
656 if (spec->nb_names)
658 spec->names = xmalloc( spec->nb_names * sizeof(spec->names[0]) );
659 for (i = j = 0; i < spec->nb_entry_points; i++)
660 if (spec->entry_points[i].name) spec->names[j++] = &spec->entry_points[i];
662 /* sort the list of names */
663 qsort( spec->names, spec->nb_names, sizeof(spec->names[0]), name_compare );
667 /*******************************************************************
668 * assign_ordinals
670 * Build the ordinal array.
672 static void assign_ordinals( DLLSPEC *spec )
674 int i, count, ordinal;
676 /* start assigning from base, or from 1 if no ordinal defined yet */
678 spec->base = MAX_ORDINALS;
679 spec->limit = 0;
680 for (i = 0; i < spec->nb_entry_points; i++)
682 ordinal = spec->entry_points[i].ordinal;
683 if (ordinal == -1) continue;
684 if (ordinal > spec->limit) spec->limit = ordinal;
685 if (ordinal < spec->base) spec->base = ordinal;
687 if (spec->base == MAX_ORDINALS) spec->base = 1;
688 if (spec->limit < spec->base) spec->limit = spec->base;
690 count = max( spec->limit + 1, spec->base + spec->nb_entry_points );
691 spec->ordinals = xmalloc( count * sizeof(spec->ordinals[0]) );
692 memset( spec->ordinals, 0, count * sizeof(spec->ordinals[0]) );
694 /* fill in all explicitly specified ordinals */
695 for (i = 0; i < spec->nb_entry_points; i++)
697 ordinal = spec->entry_points[i].ordinal;
698 if (ordinal == -1) continue;
699 if (spec->ordinals[ordinal])
701 current_line = max( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno );
702 error( "ordinal %d redefined\n%s:%d: First defined here\n",
703 ordinal, input_file_name,
704 min( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno ) );
706 else spec->ordinals[ordinal] = &spec->entry_points[i];
709 /* now assign ordinals to the rest */
710 for (i = 0, ordinal = spec->base; i < spec->nb_entry_points; i++)
712 if (spec->entry_points[i].ordinal != -1) continue;
713 while (spec->ordinals[ordinal]) ordinal++;
714 if (ordinal >= MAX_ORDINALS)
716 current_line = spec->entry_points[i].lineno;
717 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
719 spec->entry_points[i].ordinal = ordinal;
720 spec->ordinals[ordinal] = &spec->entry_points[i];
722 if (ordinal > spec->limit) spec->limit = ordinal;
726 /*******************************************************************
727 * add_16bit_exports
729 * Add the necessary exports to the 32-bit counterpart of a 16-bit module.
731 void add_16bit_exports( DLLSPEC *spec32, DLLSPEC *spec16 )
733 ORDDEF *odp;
735 /* add an export for the NE module */
737 odp = add_entry_point( spec32 );
738 odp->type = TYPE_EXTERN;
739 odp->name = xstrdup( "__wine_spec_dos_header" );
740 odp->lineno = 0;
741 odp->ordinal = 1;
742 odp->link_name = xstrdup( ".L__wine_spec_dos_header" );
744 if (spec16->main_module)
746 odp = add_entry_point( spec32 );
747 odp->type = TYPE_EXTERN;
748 odp->name = xstrdup( "__wine_spec_main_module" );
749 odp->lineno = 0;
750 odp->ordinal = 2;
751 odp->link_name = xstrdup( ".L__wine_spec_main_module" );
754 assign_names( spec32 );
755 assign_ordinals( spec32 );
759 /*******************************************************************
760 * parse_spec_file
762 * Parse a .spec file.
764 int parse_spec_file( FILE *file, DLLSPEC *spec )
766 const char *token;
768 input_file = file;
769 current_line = 0;
771 comment_chars = "#;";
772 separator_chars = "()-";
774 while (get_next_line())
776 if (!(token = GetToken(1))) continue;
777 if (strcmp(token, "@") == 0)
779 if (spec->type != SPEC_WIN32)
781 error( "'@' ordinals not supported for Win16\n" );
782 continue;
784 if (!parse_spec_ordinal( -1, spec )) continue;
786 else if (IsNumberString(token))
788 if (!parse_spec_ordinal( atoi(token), spec )) continue;
790 else
792 error( "Expected ordinal declaration, got '%s'\n", token );
793 continue;
795 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
798 current_line = 0; /* no longer parsing the input file */
799 assign_names( spec );
800 assign_ordinals( spec );
801 return !nb_errors;
805 /*******************************************************************
806 * parse_def_library
808 * Parse a LIBRARY declaration in a .def file.
810 static int parse_def_library( DLLSPEC *spec )
812 const char *token = GetToken(1);
814 if (!token) return 1;
815 if (strcmp( token, "BASE" ))
817 free( spec->file_name );
818 spec->file_name = xstrdup( token );
819 if (!(token = GetToken(1))) return 1;
821 if (strcmp( token, "BASE" ))
823 error( "Expected library name or BASE= declaration, got '%s'\n", token );
824 return 0;
826 if (!(token = GetToken(0))) return 0;
827 if (strcmp( token, "=" ))
829 error( "Expected '=' after BASE, got '%s'\n", token );
830 return 0;
832 if (!(token = GetToken(0))) return 0;
833 /* FIXME: do something with base address */
835 return 1;
839 /*******************************************************************
840 * parse_def_stack_heap_size
842 * Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
844 static int parse_def_stack_heap_size( int is_stack, DLLSPEC *spec )
846 const char *token = GetToken(0);
847 char *end;
848 unsigned long size;
850 if (!token) return 0;
851 size = strtoul( token, &end, 0 );
852 if (*end)
854 error( "Invalid number '%s'\n", token );
855 return 0;
857 if (is_stack) spec->stack_size = size / 1024;
858 else spec->heap_size = size / 1024;
859 if (!(token = GetToken(1))) return 1;
860 if (strcmp( token, "," ))
862 error( "Expected ',' after size, got '%s'\n", token );
863 return 0;
865 if (!(token = GetToken(0))) return 0;
866 /* FIXME: do something with reserve size */
867 return 1;
871 /*******************************************************************
872 * parse_def_export
874 * Parse an export declaration in a .def file.
876 static int parse_def_export( char *name, DLLSPEC *spec )
878 int i, args;
879 const char *token = GetToken(1);
880 ORDDEF *odp = add_entry_point( spec );
882 odp->lineno = current_line;
883 odp->ordinal = -1;
884 odp->name = name;
885 args = remove_stdcall_decoration( odp->name );
886 if (args == -1) odp->type = TYPE_CDECL;
887 else
889 odp->type = TYPE_STDCALL;
890 args /= get_ptr_size();
891 if (args >= sizeof(odp->u.func.arg_types))
893 error( "Too many arguments in stdcall function '%s'\n", odp->name );
894 return 0;
896 for (i = 0; i < args; i++) odp->u.func.arg_types[i] = 'l';
899 /* check for optional internal name */
901 if (token && !strcmp( token, "=" ))
903 if (!(token = GetToken(0))) goto error;
904 odp->link_name = xstrdup( token );
905 remove_stdcall_decoration( odp->link_name );
906 token = GetToken(1);
908 else
910 odp->link_name = xstrdup( name );
913 /* check for optional ordinal */
915 if (token && token[0] == '@')
917 int ordinal;
919 if (!IsNumberString( token+1 ))
921 error( "Expected number after '@', got '%s'\n", token+1 );
922 goto error;
924 ordinal = atoi( token+1 );
925 if (!ordinal)
927 error( "Ordinal 0 is not valid\n" );
928 goto error;
930 if (ordinal >= MAX_ORDINALS)
932 error( "Ordinal number %d too large\n", ordinal );
933 goto error;
935 odp->ordinal = ordinal;
936 token = GetToken(1);
939 /* check for other optional keywords */
941 if (token && !strcmp( token, "NONAME" ))
943 if (odp->ordinal == -1)
945 error( "NONAME requires an ordinal\n" );
946 goto error;
948 odp->export_name = odp->name;
949 odp->name = NULL;
950 odp->flags |= FLAG_NONAME;
951 token = GetToken(1);
953 if (token && !strcmp( token, "PRIVATE" ))
955 odp->flags |= FLAG_PRIVATE;
956 token = GetToken(1);
958 if (token && !strcmp( token, "DATA" ))
960 odp->type = TYPE_EXTERN;
961 token = GetToken(1);
963 if (token)
965 error( "Garbage text '%s' found at end of export declaration\n", token );
966 goto error;
968 return 1;
970 error:
971 spec->nb_entry_points--;
972 free( odp->name );
973 return 0;
977 /*******************************************************************
978 * parse_def_file
980 * Parse a .def file.
982 int parse_def_file( FILE *file, DLLSPEC *spec )
984 const char *token;
985 int in_exports = 0;
987 input_file = file;
988 current_line = 0;
990 comment_chars = ";";
991 separator_chars = ",=";
993 while (get_next_line())
995 if (!(token = GetToken(1))) continue;
997 if (!strcmp( token, "LIBRARY" ) || !strcmp( token, "NAME" ))
999 if (!parse_def_library( spec )) continue;
1000 goto end_of_line;
1002 else if (!strcmp( token, "STACKSIZE" ))
1004 if (!parse_def_stack_heap_size( 1, spec )) continue;
1005 goto end_of_line;
1007 else if (!strcmp( token, "HEAPSIZE" ))
1009 if (!parse_def_stack_heap_size( 0, spec )) continue;
1010 goto end_of_line;
1012 else if (!strcmp( token, "EXPORTS" ))
1014 in_exports = 1;
1015 if (!(token = GetToken(1))) continue;
1017 else if (!strcmp( token, "IMPORTS" ))
1019 in_exports = 0;
1020 if (!(token = GetToken(1))) continue;
1022 else if (!strcmp( token, "SECTIONS" ))
1024 in_exports = 0;
1025 if (!(token = GetToken(1))) continue;
1028 if (!in_exports) continue; /* ignore this line */
1029 if (!parse_def_export( xstrdup(token), spec )) continue;
1031 end_of_line:
1032 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
1035 current_line = 0; /* no longer parsing the input file */
1036 assign_names( spec );
1037 assign_ordinals( spec );
1038 return !nb_errors;