Sync to Wine-0_9_4:
[reactos.git] / reactos / tools / winebuild / parser.c
1 /*
2 * Spec file parser
3 *
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
9 *
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.
14 *
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.
19 *
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
23 */
24
25 #include "config.h"
26
27 #include <assert.h>
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "winglue.h"
35 #include "build.h"
36
37 int current_line = 0;
38
39 static char ParseBuffer[512];
40 static char TokenBuffer[512];
41 static char *ParseNext = ParseBuffer;
42 static FILE *input_file;
43
44 static const char *separator_chars;
45 static const char *comment_chars;
46
47 static const char * const TypeNames[TYPE_NBTYPES] =
48 {
49 "variable", /* TYPE_VARIABLE */
50 "pascal", /* TYPE_PASCAL */
51 "equate", /* TYPE_ABS */
52 "stub", /* TYPE_STUB */
53 "stdcall", /* TYPE_STDCALL */
54 "cdecl", /* TYPE_CDECL */
55 "varargs", /* TYPE_VARARGS */
56 "extern" /* TYPE_EXTERN */
57 };
58
59 static const char * const FlagNames[] =
60 {
61 "norelay", /* FLAG_NORELAY */
62 "noname", /* FLAG_NONAME */
63 "ret16", /* FLAG_RET16 */
64 "ret64", /* FLAG_RET64 */
65 "i386", /* FLAG_I386 */
66 "register", /* FLAG_REGISTER */
67 "private", /* FLAG_PRIVATE */
68 NULL
69 };
70
71 static int IsNumberString(const char *s)
72 {
73 while (*s) if (!isdigit(*s++)) return 0;
74 return 1;
75 }
76
77 inline static int is_token_separator( char ch )
78 {
79 return strchr( separator_chars, ch ) != NULL;
80 }
81
82 inline static int is_token_comment( char ch )
83 {
84 return strchr( comment_chars, ch ) != NULL;
85 }
86
87 /* get the next line from the input file, or return 0 if at eof */
88 static int get_next_line(void)
89 {
90 ParseNext = ParseBuffer;
91 current_line++;
92 return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
93 }
94
95 static const char * GetToken( int allow_eol )
96 {
97 char *p = ParseNext;
98 char *token = TokenBuffer;
99
100 for (;;)
101 {
102 /* remove initial white space */
103 p = ParseNext;
104 while (isspace(*p)) p++;
105
106 if (*p == '\\' && p[1] == '\n') /* line continuation */
107 {
108 if (!get_next_line())
109 {
110 if (!allow_eol) error( "Unexpected end of file\n" );
111 return NULL;
112 }
113 }
114 else break;
115 }
116
117 if ((*p == '\0') || is_token_comment(*p))
118 {
119 if (!allow_eol) error( "Declaration not terminated properly\n" );
120 return NULL;
121 }
122
123 /*
124 * Find end of token.
125 */
126 if (is_token_separator(*p))
127 {
128 /* a separator is always a complete token */
129 *token++ = *p++;
130 }
131 else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
132 {
133 if (*p == '\\') p++;
134 if (*p) *token++ = *p++;
135 }
136 *token = '\0';
137 ParseNext = p;
138 return TokenBuffer;
139 }
140
141
142 static ORDDEF *add_entry_point( DLLSPEC *spec )
143 {
144 if (spec->nb_entry_points == spec->alloc_entry_points)
145 {
146 spec->alloc_entry_points += 128;
147 spec->entry_points = xrealloc( spec->entry_points,
148 spec->alloc_entry_points * sizeof(*spec->entry_points) );
149 }
150 return &spec->entry_points[spec->nb_entry_points++];
151 }
152
153 /*******************************************************************
154 * parse_spec_variable
155 *
156 * Parse a variable definition in a .spec file.
157 */
158 static int parse_spec_variable( ORDDEF *odp, DLLSPEC *spec )
159 {
160 char *endptr;
161 int *value_array;
162 int n_values;
163 int value_array_size;
164 const char *token;
165
166 if (spec->type == SPEC_WIN32)
167 {
168 error( "'variable' not supported in Win32, use 'extern' instead\n" );
169 return 0;
170 }
171
172 if (!(token = GetToken(0))) return 0;
173 if (*token != '(')
174 {
175 error( "Expected '(' got '%s'\n", token );
176 return 0;
177 }
178
179 n_values = 0;
180 value_array_size = 25;
181 value_array = xmalloc(sizeof(*value_array) * value_array_size);
182
183 for (;;)
184 {
185 if (!(token = GetToken(0)))
186 {
187 free( value_array );
188 return 0;
189 }
190 if (*token == ')')
191 break;
192
193 value_array[n_values++] = strtol(token, &endptr, 0);
194 if (n_values == value_array_size)
195 {
196 value_array_size += 25;
197 value_array = xrealloc(value_array,
198 sizeof(*value_array) * value_array_size);
199 }
200
201 if (endptr == NULL || *endptr != '\0')
202 {
203 error( "Expected number value, got '%s'\n", token );
204 free( value_array );
205 return 0;
206 }
207 }
208
209 odp->u.var.n_values = n_values;
210 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
211 return 1;
212 }
213
214
215 /*******************************************************************
216 * parse_spec_export
217 *
218 * Parse an exported function definition in a .spec file.
219 */
220 static int parse_spec_export( ORDDEF *odp, DLLSPEC *spec )
221 {
222 const char *token;
223 unsigned int i;
224
225 switch(spec->type)
226 {
227 case SPEC_WIN16:
228 if (odp->type == TYPE_STDCALL)
229 {
230 error( "'stdcall' not supported for Win16\n" );
231 return 0;
232 }
233 break;
234 case SPEC_WIN32:
235 if (odp->type == TYPE_PASCAL)
236 {
237 error( "'pascal' not supported for Win32\n" );
238 return 0;
239 }
240 break;
241 default:
242 break;
243 }
244
245 if (!(token = GetToken(0))) return 0;
246 if (*token != '(')
247 {
248 error( "Expected '(' got '%s'\n", token );
249 return 0;
250 }
251
252 for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
253 {
254 if (!(token = GetToken(0))) return 0;
255 if (*token == ')')
256 break;
257
258 if (!strcmp(token, "word"))
259 odp->u.func.arg_types[i] = 'w';
260 else if (!strcmp(token, "s_word"))
261 odp->u.func.arg_types[i] = 's';
262 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
263 odp->u.func.arg_types[i] = 'l';
264 else if (!strcmp(token, "ptr"))
265 odp->u.func.arg_types[i] = 'p';
266 else if (!strcmp(token, "str"))
267 odp->u.func.arg_types[i] = 't';
268 else if (!strcmp(token, "wstr"))
269 odp->u.func.arg_types[i] = 'W';
270 else if (!strcmp(token, "segstr"))
271 odp->u.func.arg_types[i] = 'T';
272 else if (!strcmp(token, "double"))
273 {
274 odp->u.func.arg_types[i++] = 'l';
275 if (get_ptr_size() == 4 && i < sizeof(odp->u.func.arg_types))
276 odp->u.func.arg_types[i] = 'l';
277 }
278 else
279 {
280 error( "Unknown argument type '%s'\n", token );
281 return 0;
282 }
283
284 if (spec->type == SPEC_WIN32)
285 {
286 if (strcmp(token, "long") &&
287 strcmp(token, "ptr") &&
288 strcmp(token, "str") &&
289 strcmp(token, "wstr") &&
290 strcmp(token, "double"))
291 {
292 error( "Type '%s' not supported for Win32\n", token );
293 return 0;
294 }
295 }
296 }
297 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
298 {
299 error( "Too many arguments\n" );
300 return 0;
301 }
302
303 odp->u.func.arg_types[i] = '\0';
304 if (odp->type == TYPE_VARARGS)
305 odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
306
307 if (!(token = GetToken(1)))
308 {
309 if (!strcmp( odp->name, "@" ))
310 {
311 error( "Missing handler name for anonymous function\n" );
312 return 0;
313 }
314 odp->link_name = xstrdup( odp->name );
315 }
316 else
317 {
318 odp->link_name = xstrdup( token );
319 if (strchr( odp->link_name, '.' ))
320 {
321 if (spec->type == SPEC_WIN16)
322 {
323 error( "Forwarded functions not supported for Win16\n" );
324 return 0;
325 }
326 odp->flags |= FLAG_FORWARD;
327 }
328 }
329 return 1;
330 }
331
332
333 /*******************************************************************
334 * parse_spec_equate
335 *
336 * Parse an 'equate' definition in a .spec file.
337 */
338 static int parse_spec_equate( ORDDEF *odp, DLLSPEC *spec )
339 {
340 char *endptr;
341 int value;
342 const char *token;
343
344 if (spec->type == SPEC_WIN32)
345 {
346 error( "'equate' not supported for Win32\n" );
347 return 0;
348 }
349 if (!(token = GetToken(0))) return 0;
350 value = strtol(token, &endptr, 0);
351 if (endptr == NULL || *endptr != '\0')
352 {
353 error( "Expected number value, got '%s'\n", token );
354 return 0;
355 }
356 if (value < -0x8000 || value > 0xffff)
357 {
358 error( "Value %d for absolute symbol doesn't fit in 16 bits\n", value );
359 value = 0;
360 }
361 odp->u.abs.value = value;
362 return 1;
363 }
364
365
366 /*******************************************************************
367 * parse_spec_stub
368 *
369 * Parse a 'stub' definition in a .spec file
370 */
371 static int parse_spec_stub( ORDDEF *odp, DLLSPEC *spec )
372 {
373 odp->u.func.arg_types[0] = '\0';
374 odp->link_name = xstrdup("");
375 odp->flags |= FLAG_I386; /* don't bother generating stubs for Winelib */
376 return 1;
377 }
378
379
380 /*******************************************************************
381 * parse_spec_extern
382 *
383 * Parse an 'extern' definition in a .spec file.
384 */
385 static int parse_spec_extern( ORDDEF *odp, DLLSPEC *spec )
386 {
387 const char *token;
388
389 if (spec->type == SPEC_WIN16)
390 {
391 error( "'extern' not supported for Win16, use 'variable' instead\n" );
392 return 0;
393 }
394 if (!(token = GetToken(1)))
395 {
396 if (!strcmp( odp->name, "@" ))
397 {
398 error( "Missing handler name for anonymous extern\n" );
399 return 0;
400 }
401 odp->link_name = xstrdup( odp->name );
402 }
403 else
404 {
405 odp->link_name = xstrdup( token );
406 if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
407 }
408 return 1;
409 }
410
411
412 /*******************************************************************
413 * parse_spec_flags
414 *
415 * Parse the optional flags for an entry point in a .spec file.
416 */
417 static const char *parse_spec_flags( ORDDEF *odp )
418 {
419 unsigned int i;
420 const char *token;
421
422 do
423 {
424 if (!(token = GetToken(0))) break;
425 for (i = 0; FlagNames[i]; i++)
426 if (!strcmp( FlagNames[i], token )) break;
427 if (!FlagNames[i])
428 {
429 error( "Unknown flag '%s'\n", token );
430 return NULL;
431 }
432 odp->flags |= 1 << i;
433 token = GetToken(0);
434 } while (token && *token == '-');
435
436 return token;
437 }
438
439
440 /*******************************************************************
441 * parse_spec_ordinal
442 *
443 * Parse an ordinal definition in a .spec file.
444 */
445 static int parse_spec_ordinal( int ordinal, DLLSPEC *spec )
446 {
447 const char *token;
448
449 ORDDEF *odp = add_entry_point( spec );
450 memset( odp, 0, sizeof(*odp) );
451
452 if (!(token = GetToken(0))) goto error;
453
454 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
455 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
456 break;
457
458 if (odp->type >= TYPE_NBTYPES)
459 {
460 error( "Expected type after ordinal, found '%s' instead\n", token );
461 goto error;
462 }
463
464 if (!(token = GetToken(0))) goto error;
465 if (*token == '-' && !(token = parse_spec_flags( odp ))) goto error;
466
467 odp->name = xstrdup( token );
468 odp->lineno = current_line;
469 odp->ordinal = ordinal;
470
471 switch(odp->type)
472 {
473 case TYPE_VARIABLE:
474 if (!parse_spec_variable( odp, spec )) goto error;
475 break;
476 case TYPE_PASCAL:
477 case TYPE_STDCALL:
478 case TYPE_VARARGS:
479 case TYPE_CDECL:
480 if (!parse_spec_export( odp, spec )) goto error;
481 break;
482 case TYPE_ABS:
483 if (!parse_spec_equate( odp, spec )) goto error;
484 break;
485 case TYPE_STUB:
486 if (!parse_spec_stub( odp, spec )) goto error;
487 break;
488 case TYPE_EXTERN:
489 if (!parse_spec_extern( odp, spec )) goto error;
490 break;
491 default:
492 assert( 0 );
493 }
494
495 if ((target_cpu != CPU_x86) && (odp->flags & FLAG_I386))
496 {
497 /* ignore this entry point on non-Intel archs */
498 spec->nb_entry_points--;
499 return 1;
500 }
501
502 if (ordinal != -1)
503 {
504 if (!ordinal)
505 {
506 error( "Ordinal 0 is not valid\n" );
507 goto error;
508 }
509 if (ordinal >= MAX_ORDINALS)
510 {
511 error( "Ordinal number %d too large\n", ordinal );
512 goto error;
513 }
514 if (ordinal > spec->limit) spec->limit = ordinal;
515 if (ordinal < spec->base) spec->base = ordinal;
516 odp->ordinal = ordinal;
517 }
518
519 if (odp->type == TYPE_STDCALL && !(odp->flags & FLAG_PRIVATE))
520 {
521 if (!strcmp( odp->name, "DllRegisterServer" ) ||
522 !strcmp( odp->name, "DllUnregisterServer" ) ||
523 !strcmp( odp->name, "DllGetClassObject" ) ||
524 !strcmp( odp->name, "DllGetVersion" ) ||
525 !strcmp( odp->name, "DllInstall" ) ||
526 !strcmp( odp->name, "DllCanUnloadNow" ))
527 {
528 warning( "Function %s should be marked private\n", odp->name );
529 if (strcmp( odp->name, odp->link_name ))
530 warning( "Function %s should not use a different internal name (%s)\n",
531 odp->name, odp->link_name );
532 }
533 }
534
535 if (!strcmp( odp->name, "@" ) || odp->flags & FLAG_NONAME)
536 {
537 if (ordinal == -1)
538 {
539 error( "Nameless function needs an explicit ordinal number\n" );
540 goto error;
541 }
542 if (spec->type != SPEC_WIN32)
543 {
544 error( "Nameless functions not supported for Win16\n" );
545 goto error;
546 }
547 if (!strcmp( odp->name, "@" )) free( odp->name );
548 else odp->export_name = odp->name;
549 odp->name = NULL;
550 }
551 return 1;
552
553 error:
554 spec->nb_entry_points--;
555 free( odp->name );
556 return 0;
557 }
558
559
560 static int name_compare( const void *ptr1, const void *ptr2 )
561 {
562 const ORDDEF *odp1 = *(const ORDDEF * const *)ptr1;
563 const ORDDEF *odp2 = *(const ORDDEF * const *)ptr2;
564 const char *name1 = odp1->name ? odp1->name : odp1->export_name;
565 const char *name2 = odp2->name ? odp2->name : odp2->export_name;
566 return strcmp( name1, name2 );
567 }
568
569 /*******************************************************************
570 * assign_names
571 *
572 * Build the name array and catch duplicates.
573 */
574 static void assign_names( DLLSPEC *spec )
575 {
576 int i, j, nb_exp_names = 0;
577 ORDDEF **all_names;
578
579 spec->nb_names = 0;
580 for (i = 0; i < spec->nb_entry_points; i++)
581 if (spec->entry_points[i].name) spec->nb_names++;
582 else if (spec->entry_points[i].export_name) nb_exp_names++;
583
584 if (!spec->nb_names && !nb_exp_names) return;
585
586 /* check for duplicates */
587
588 all_names = xmalloc( (spec->nb_names + nb_exp_names) * sizeof(all_names[0]) );
589 for (i = j = 0; i < spec->nb_entry_points; i++)
590 if (spec->entry_points[i].name || spec->entry_points[i].export_name)
591 all_names[j++] = &spec->entry_points[i];
592
593 qsort( all_names, j, sizeof(all_names[0]), name_compare );
594
595 for (i = 0; i < j - 1; i++)
596 {
597 const char *name1 = all_names[i]->name ? all_names[i]->name : all_names[i]->export_name;
598 const char *name2 = all_names[i+1]->name ? all_names[i+1]->name : all_names[i+1]->export_name;
599 if (!strcmp( name1, name2 ))
600 {
601 current_line = max( all_names[i]->lineno, all_names[i+1]->lineno );
602 error( "'%s' redefined\n%s:%d: First defined here\n",
603 name1, input_file_name,
604 min( all_names[i]->lineno, all_names[i+1]->lineno ) );
605 }
606 }
607 free( all_names );
608
609 if (spec->nb_names)
610 {
611 spec->names = xmalloc( spec->nb_names * sizeof(spec->names[0]) );
612 for (i = j = 0; i < spec->nb_entry_points; i++)
613 if (spec->entry_points[i].name) spec->names[j++] = &spec->entry_points[i];
614
615 /* sort the list of names */
616 qsort( spec->names, spec->nb_names, sizeof(spec->names[0]), name_compare );
617 }
618 }
619
620 /*******************************************************************
621 * assign_ordinals
622 *
623 * Build the ordinal array.
624 */
625 static void assign_ordinals( DLLSPEC *spec )
626 {
627 int i, count, ordinal;
628
629 /* start assigning from base, or from 1 if no ordinal defined yet */
630
631 spec->base = MAX_ORDINALS;
632 spec->limit = 0;
633 for (i = 0; i < spec->nb_entry_points; i++)
634 {
635 ordinal = spec->entry_points[i].ordinal;
636 if (ordinal == -1) continue;
637 if (ordinal > spec->limit) spec->limit = ordinal;
638 if (ordinal < spec->base) spec->base = ordinal;
639 }
640 if (spec->base == MAX_ORDINALS) spec->base = 1;
641 if (spec->limit < spec->base) spec->limit = spec->base;
642
643 count = max( spec->limit + 1, spec->base + spec->nb_entry_points );
644 spec->ordinals = xmalloc( count * sizeof(spec->ordinals[0]) );
645 memset( spec->ordinals, 0, count * sizeof(spec->ordinals[0]) );
646
647 /* fill in all explicitly specified ordinals */
648 for (i = 0; i < spec->nb_entry_points; i++)
649 {
650 ordinal = spec->entry_points[i].ordinal;
651 if (ordinal == -1) continue;
652 if (spec->ordinals[ordinal])
653 {
654 current_line = max( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno );
655 error( "ordinal %d redefined\n%s:%d: First defined here\n",
656 ordinal, input_file_name,
657 min( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno ) );
658 }
659 else spec->ordinals[ordinal] = &spec->entry_points[i];
660 }
661
662 /* now assign ordinals to the rest */
663 for (i = 0, ordinal = spec->base; i < spec->nb_entry_points; i++)
664 {
665 if (spec->entry_points[i].ordinal != -1) continue;
666 while (spec->ordinals[ordinal]) ordinal++;
667 if (ordinal >= MAX_ORDINALS)
668 {
669 current_line = spec->entry_points[i].lineno;
670 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
671 }
672 spec->entry_points[i].ordinal = ordinal;
673 spec->ordinals[ordinal] = &spec->entry_points[i];
674 }
675 if (ordinal > spec->limit) spec->limit = ordinal;
676 }
677
678
679 /*******************************************************************
680 * parse_spec_file
681 *
682 * Parse a .spec file.
683 */
684 int parse_spec_file( FILE *file, DLLSPEC *spec )
685 {
686 const char *token;
687
688 input_file = file;
689 current_line = 0;
690
691 comment_chars = "#;";
692 separator_chars = "()-";
693
694 while (get_next_line())
695 {
696 if (!(token = GetToken(1))) continue;
697 if (strcmp(token, "@") == 0)
698 {
699 if (spec->type != SPEC_WIN32)
700 {
701 error( "'@' ordinals not supported for Win16\n" );
702 continue;
703 }
704 if (!parse_spec_ordinal( -1, spec )) continue;
705 }
706 else if (IsNumberString(token))
707 {
708 if (!parse_spec_ordinal( atoi(token), spec )) continue;
709 }
710 else
711 {
712 error( "Expected ordinal declaration, got '%s'\n", token );
713 continue;
714 }
715 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
716 }
717
718 current_line = 0; /* no longer parsing the input file */
719 assign_names( spec );
720 assign_ordinals( spec );
721 return !nb_errors;
722 }
723
724
725 /*******************************************************************
726 * parse_def_library
727 *
728 * Parse a LIBRARY declaration in a .def file.
729 */
730 static int parse_def_library( DLLSPEC *spec )
731 {
732 const char *token = GetToken(1);
733
734 if (!token) return 1;
735 if (strcmp( token, "BASE" ))
736 {
737 free( spec->file_name );
738 spec->file_name = xstrdup( token );
739 if (!(token = GetToken(1))) return 1;
740 }
741 if (strcmp( token, "BASE" ))
742 {
743 error( "Expected library name or BASE= declaration, got '%s'\n", token );
744 return 0;
745 }
746 if (!(token = GetToken(0))) return 0;
747 if (strcmp( token, "=" ))
748 {
749 error( "Expected '=' after BASE, got '%s'\n", token );
750 return 0;
751 }
752 if (!(token = GetToken(0))) return 0;
753 /* FIXME: do something with base address */
754
755 return 1;
756 }
757
758
759 /*******************************************************************
760 * parse_def_stack_heap_size
761 *
762 * Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
763 */
764 static int parse_def_stack_heap_size( int is_stack, DLLSPEC *spec )
765 {
766 const char *token = GetToken(0);
767 char *end;
768 unsigned long size;
769
770 if (!token) return 0;
771 size = strtoul( token, &end, 0 );
772 if (*end)
773 {
774 error( "Invalid number '%s'\n", token );
775 return 0;
776 }
777 if (is_stack) spec->stack_size = size / 1024;
778 else spec->heap_size = size / 1024;
779 if (!(token = GetToken(1))) return 1;
780 if (strcmp( token, "," ))
781 {
782 error( "Expected ',' after size, got '%s'\n", token );
783 return 0;
784 }
785 if (!(token = GetToken(0))) return 0;
786 /* FIXME: do something with reserve size */
787 return 1;
788 }
789
790
791 /*******************************************************************
792 * parse_def_export
793 *
794 * Parse an export declaration in a .def file.
795 */
796 static int parse_def_export( char *name, DLLSPEC *spec )
797 {
798 int i, args;
799 const char *token = GetToken(1);
800
801 ORDDEF *odp = add_entry_point( spec );
802 memset( odp, 0, sizeof(*odp) );
803
804 odp->lineno = current_line;
805 odp->ordinal = -1;
806 odp->name = name;
807 args = remove_stdcall_decoration( odp->name );
808 if (args == -1) odp->type = TYPE_CDECL;
809 else
810 {
811 odp->type = TYPE_STDCALL;
812 args /= get_ptr_size();
813 if (args >= sizeof(odp->u.func.arg_types))
814 {
815 error( "Too many arguments in stdcall function '%s'\n", odp->name );
816 return 0;
817 }
818 for (i = 0; i < args; i++) odp->u.func.arg_types[i] = 'l';
819 }
820
821 /* check for optional internal name */
822
823 if (token && !strcmp( token, "=" ))
824 {
825 if (!(token = GetToken(0))) goto error;
826 odp->link_name = xstrdup( token );
827 remove_stdcall_decoration( odp->link_name );
828 token = GetToken(1);
829 }
830 else
831 {
832 odp->link_name = xstrdup( name );
833 }
834
835 /* check for optional ordinal */
836
837 if (token && token[0] == '@')
838 {
839 int ordinal;
840
841 if (!IsNumberString( token+1 ))
842 {
843 error( "Expected number after '@', got '%s'\n", token+1 );
844 goto error;
845 }
846 ordinal = atoi( token+1 );
847 if (!ordinal)
848 {
849 error( "Ordinal 0 is not valid\n" );
850 goto error;
851 }
852 if (ordinal >= MAX_ORDINALS)
853 {
854 error( "Ordinal number %d too large\n", ordinal );
855 goto error;
856 }
857 odp->ordinal = ordinal;
858 token = GetToken(1);
859 }
860
861 /* check for other optional keywords */
862
863 if (token && !strcmp( token, "NONAME" ))
864 {
865 if (odp->ordinal == -1)
866 {
867 error( "NONAME requires an ordinal\n" );
868 goto error;
869 }
870 odp->export_name = odp->name;
871 odp->name = NULL;
872 odp->flags |= FLAG_NONAME;
873 token = GetToken(1);
874 }
875 if (token && !strcmp( token, "PRIVATE" ))
876 {
877 odp->flags |= FLAG_PRIVATE;
878 token = GetToken(1);
879 }
880 if (token && !strcmp( token, "DATA" ))
881 {
882 odp->type = TYPE_EXTERN;
883 token = GetToken(1);
884 }
885 if (token)
886 {
887 error( "Garbage text '%s' found at end of export declaration\n", token );
888 goto error;
889 }
890 return 1;
891
892 error:
893 spec->nb_entry_points--;
894 free( odp->name );
895 return 0;
896 }
897
898
899 /*******************************************************************
900 * parse_def_file
901 *
902 * Parse a .def file.
903 */
904 int parse_def_file( FILE *file, DLLSPEC *spec )
905 {
906 const char *token;
907 int in_exports = 0;
908
909 input_file = file;
910 current_line = 0;
911
912 comment_chars = ";";
913 separator_chars = ",=";
914
915 while (get_next_line())
916 {
917 if (!(token = GetToken(1))) continue;
918
919 if (!strcmp( token, "LIBRARY" ) || !strcmp( token, "NAME" ))
920 {
921 if (!parse_def_library( spec )) continue;
922 goto end_of_line;
923 }
924 else if (!strcmp( token, "STACKSIZE" ))
925 {
926 if (!parse_def_stack_heap_size( 1, spec )) continue;
927 goto end_of_line;
928 }
929 else if (!strcmp( token, "HEAPSIZE" ))
930 {
931 if (!parse_def_stack_heap_size( 0, spec )) continue;
932 goto end_of_line;
933 }
934 else if (!strcmp( token, "EXPORTS" ))
935 {
936 in_exports = 1;
937 if (!(token = GetToken(1))) continue;
938 }
939 else if (!strcmp( token, "IMPORTS" ))
940 {
941 in_exports = 0;
942 if (!(token = GetToken(1))) continue;
943 }
944 else if (!strcmp( token, "SECTIONS" ))
945 {
946 in_exports = 0;
947 if (!(token = GetToken(1))) continue;
948 }
949
950 if (!in_exports) continue; /* ignore this line */
951 if (!parse_def_export( xstrdup(token), spec )) continue;
952
953 end_of_line:
954 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
955 }
956
957 current_line = 0; /* no longer parsing the input file */
958 assign_names( spec );
959 assign_ordinals( spec );
960 return !nb_errors;
961 }