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