[SPEC2DEF]
[reactos.git] / reactos / tools / spec2def / spec2def.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <string.h>
5
6 #ifdef _MSC_VER
7 #define strcasecmp _stricmp
8 #endif
9
10 typedef struct _STRING
11 {
12 const char *buf;
13 int len;
14 } STRING, *PSTRING;
15
16 typedef struct
17 {
18 STRING strName;
19 STRING strTarget;
20 int nCallingConvention;
21 int nOrdinal;
22 int nStackBytes;
23 int nArgCount;
24 int anArgs[30];
25 unsigned int uFlags;
26 int nNumber;
27 } EXPORT;
28
29 enum _ARCH
30 {
31 ARCH_X86,
32 ARCH_AMD64,
33 ARCH_IA64,
34 ARCH_ARM,
35 ARCH_PPC
36 };
37
38 typedef int (*PFNOUTLINE)(FILE *, EXPORT *);
39 int gbMSComp = 0;
40 int gbImportLib = 0;
41 int giArch = ARCH_X86;
42 char *pszArchString = "i386";
43 char *pszArchString2;
44 char *pszDllName = 0;
45 char *gpszUnderscore = "";
46 int gbDebug;
47 #define DbgPrint(...) (!gbDebug || fprintf(stderr, __VA_ARGS__))
48
49 enum
50 {
51 FL_PRIVATE = 1,
52 FL_STUB = 2,
53 FL_NONAME = 4,
54 FL_ORDINAL = 8,
55 FL_DATA_ALIAS = 16
56 };
57
58 enum
59 {
60 CC_STDCALL,
61 CC_CDECL,
62 CC_FASTCALL,
63 CC_THISCALL,
64 CC_EXTERN,
65 CC_STUB,
66 };
67
68 enum
69 {
70 ARG_LONG,
71 ARG_PTR,
72 ARG_STR,
73 ARG_WSTR,
74 ARG_DBL,
75 ARG_INT64,
76 ARG_INT128,
77 ARG_FLOAT
78 };
79
80 char* astrCallingConventions[] =
81 {
82 "STDCALL",
83 "CDECL",
84 "FASTCALL",
85 "THISCALL",
86 "EXTERN"
87 };
88
89 static
90 int
91 IsSeparator(char chr)
92 {
93 return ((chr <= ',' && chr != '$') ||
94 (chr >= ':' && chr < '?') );
95 }
96
97 int
98 CompareToken(const char *token, const char *comparand)
99 {
100 while (*comparand)
101 {
102 if (*token != *comparand) return 0;
103 token++;
104 comparand++;
105 }
106 if (!IsSeparator(*token)) return 0;
107 return 1;
108 }
109
110 const char *
111 ScanToken(const char *token, char chr)
112 {
113 while (!IsSeparator(*token))
114 {
115 if (*token == chr) return token;
116 token++;
117 }
118 return 0;
119 }
120
121 char *
122 NextLine(char *pc)
123 {
124 while (*pc != 0)
125 {
126 if (pc[0] == '\n' && pc[1] == '\r') return pc + 2;
127 else if (pc[0] == '\n') return pc + 1;
128 pc++;
129 }
130 return pc;
131 }
132
133 int
134 TokenLength(char *pc)
135 {
136 int length = 0;
137
138 while (!IsSeparator(*pc++)) length++;
139
140 return length;
141 }
142
143 char *
144 NextToken(char *pc)
145 {
146 /* Skip token */
147 while (!IsSeparator(*pc)) pc++;
148
149 /* Skip white spaces */
150 while (*pc == ' ' || *pc == '\t') pc++;
151
152 /* Check for end of line */
153 if (*pc == '\n' || *pc == '\r' || *pc == 0) return 0;
154
155 /* Check for comment */
156 if (*pc == '#' || *pc == ';') return 0;
157
158 return pc;
159 }
160
161 void
162 OutputHeader_stub(FILE *file)
163 {
164 fprintf(file, "/* This file is autogenerated, do not edit. */\n\n"
165 "#include <stubs.h>\n\n");
166 }
167
168 int
169 OutputLine_stub(FILE *file, EXPORT *pexp)
170 {
171 int i;
172
173 if (pexp->nCallingConvention != CC_STUB &&
174 (pexp->uFlags & FL_STUB) == 0) return 0;
175
176 fprintf(file, "int ");
177 if ((giArch == ARCH_X86) &&
178 pexp->nCallingConvention == CC_STDCALL)
179 {
180 fprintf(file, "__stdcall ");
181 }
182
183 /* Check for C++ */
184 if (pexp->strName.buf[0] == '?')
185 {
186 fprintf(file, "stub_function%d(", pexp->nNumber);
187 }
188 else
189 {
190 fprintf(file, "%.*s(", pexp->strName.len, pexp->strName.buf);
191 }
192
193 for (i = 0; i < pexp->nArgCount; i++)
194 {
195 if (i != 0) fprintf(file, ", ");
196 switch (pexp->anArgs[i])
197 {
198 case ARG_LONG: fprintf(file, "long"); break;
199 case ARG_PTR: fprintf(file, "void*"); break;
200 case ARG_STR: fprintf(file, "char*"); break;
201 case ARG_WSTR: fprintf(file, "wchar_t*"); break;
202 case ARG_DBL:
203 case ARG_INT64 : fprintf(file, "__int64"); break;
204 case ARG_INT128 : fprintf(file, "__int128"); break;
205 case ARG_FLOAT: fprintf(file, "float"); break;
206 }
207 fprintf(file, " a%d", i);
208 }
209 fprintf(file, ")\n{\n\tDbgPrint(\"WARNING: calling stub %.*s(",
210 pexp->strName.len, pexp->strName.buf);
211
212 for (i = 0; i < pexp->nArgCount; i++)
213 {
214 if (i != 0) fprintf(file, ",");
215 switch (pexp->anArgs[i])
216 {
217 case ARG_LONG: fprintf(file, "0x%%lx"); break;
218 case ARG_PTR: fprintf(file, "0x%%p"); break;
219 case ARG_STR: fprintf(file, "'%%s'"); break;
220 case ARG_WSTR: fprintf(file, "'%%ws'"); break;
221 case ARG_DBL: fprintf(file, "%%f"); break;
222 case ARG_INT64: fprintf(file, "%%\"PRix64\""); break;
223 case ARG_INT128: fprintf(file, "%%\"PRix128\""); break;
224 case ARG_FLOAT: fprintf(file, "%%f"); break;
225 }
226 }
227 fprintf(file, ")\\n\"");
228
229 for (i = 0; i < pexp->nArgCount; i++)
230 {
231 fprintf(file, ", ");
232 switch (pexp->anArgs[i])
233 {
234 case ARG_LONG: fprintf(file, "(long)a%d", i); break;
235 case ARG_PTR: fprintf(file, "(void*)a%d", i); break;
236 case ARG_STR: fprintf(file, "(char*)a%d", i); break;
237 case ARG_WSTR: fprintf(file, "(wchar_t*)a%d", i); break;
238 case ARG_DBL: fprintf(file, "(double)a%d", i); break;
239 case ARG_INT64: fprintf(file, "(__int64)a%d", i); break;
240 case ARG_INT128: fprintf(file, "(__int128)a%d", i); break;
241 case ARG_FLOAT: fprintf(file, "(float)a%d", i); break;
242 }
243 }
244 fprintf(file, ");\n");
245
246 if (pexp->nCallingConvention == CC_STUB)
247 {
248 fprintf(file, "\t__wine_spec_unimplemented_stub(\"%s\", __FUNCTION__);\n", pszDllName);
249 }
250
251 fprintf(file, "\treturn 0;\n}\n\n");
252
253 return 1;
254 }
255
256 void
257 OutputHeader_asmstub(FILE *file, char *libname)
258 {
259 fprintf(file, "; File generated automatically, do not edit! \n\n");
260
261 if (giArch == ARCH_X86)
262 fprintf(file, ".586\n.model flat\n");
263
264 fprintf(file, ".code\n");
265 }
266
267 int
268 OutputLine_asmstub(FILE *fileDest, EXPORT *pexp)
269 {
270 /* Handle autoname */
271 if (pexp->strName.len == 1 && pexp->strName.buf[0] == '@')
272 {
273 fprintf(fileDest, "PUBLIC %sordinal%d\n%sordinal%d: nop\n",
274 gpszUnderscore, pexp->nOrdinal, gpszUnderscore, pexp->nOrdinal);
275 }
276 else if (giArch != ARCH_X86)
277 {
278 fprintf(fileDest, "PUBLIC _stub_%.*s\n_stub_%.*s: nop\n",
279 pexp->strName.len, pexp->strName.buf,
280 pexp->strName.len, pexp->strName.buf);
281 }
282 else if (pexp->nCallingConvention == CC_STDCALL)
283 {
284 fprintf(fileDest, "PUBLIC __stub_%.*s@%d\n__stub_%.*s@%d: nop\n",
285 pexp->strName.len, pexp->strName.buf, pexp->nStackBytes,
286 pexp->strName.len, pexp->strName.buf, pexp->nStackBytes);
287 }
288 else if (pexp->nCallingConvention == CC_FASTCALL)
289 {
290 fprintf(fileDest, "PUBLIC @_stub_%.*s@%d\n@_stub_%.*s@%d: nop\n",
291 pexp->strName.len, pexp->strName.buf, pexp->nStackBytes,
292 pexp->strName.len, pexp->strName.buf, pexp->nStackBytes);
293 }
294 else if (pexp->nCallingConvention == CC_CDECL ||
295 pexp->nCallingConvention == CC_STUB)
296 {
297 fprintf(fileDest, "PUBLIC __stub_%.*s\n__stub_%.*s: nop\n",
298 pexp->strName.len, pexp->strName.buf,
299 pexp->strName.len, pexp->strName.buf);
300 }
301 else if (pexp->nCallingConvention == CC_EXTERN)
302 {
303 fprintf(fileDest, "PUBLIC __stub_%.*s\n__stub_%.*s:\n",
304 pexp->strName.len, pexp->strName.buf,
305 pexp->strName.len, pexp->strName.buf);
306 }
307
308 return 1;
309 }
310
311 void
312 OutputHeader_def(FILE *file, char *libname)
313 {
314 fprintf(file,
315 "; File generated automatically, do not edit!\n\n"
316 "NAME %s\n\n"
317 "EXPORTS\n",
318 libname);
319 }
320
321 void
322 PrintName(FILE *fileDest, EXPORT *pexp, PSTRING pstr, int fDeco)
323 {
324 const char *pcName = pstr->buf;
325 int nNameLength = pstr->len;
326 const char* pcDot, *pcAt;
327
328 if ((giArch == ARCH_X86) && fDeco &&
329 ((pexp->nCallingConvention == CC_STDCALL) ||
330 (pexp->nCallingConvention == CC_FASTCALL)))
331 {
332 /* Scan for a dll forwarding dot */
333 pcDot = ScanToken(pcName, '.');
334 if (pcDot)
335 {
336 /* First print the dll name, followed by a dot */
337 nNameLength = pcDot - pcName;
338 fprintf(fileDest, "%.*s.", nNameLength, pcName);
339
340 /* Now the actual function name */
341 pcName = pcDot + 1;
342 nNameLength = pexp->strTarget.len - nNameLength - 1;
343 }
344
345 /* Does the string already have decoration? */
346 pcAt = ScanToken(pcName, '@');
347 if (pcAt && (pcAt < (pcName + nNameLength)))
348 {
349 /* On GCC, we need to remove the leading stdcall underscore */
350 if (!gbMSComp && (pexp->nCallingConvention == CC_STDCALL))
351 {
352 pcName++;
353 nNameLength--;
354 }
355
356 /* Print the already decorated function name */
357 fprintf(fileDest, "%.*s", nNameLength, pcName);
358 }
359 else
360 {
361 /* Print the prefix, but skip it for (GCC && stdcall) */
362 if (gbMSComp || (pexp->nCallingConvention != CC_STDCALL))
363 {
364 fprintf(fileDest, "%c", pexp->nCallingConvention == CC_FASTCALL ? '@' : '_');
365 }
366
367 /* Print the name with trailing decoration */
368 fprintf(fileDest, "%.*s@%d", nNameLength, pcName, pexp->nStackBytes);
369 }
370 }
371 else
372 {
373 /* Does the string already have stdcall decoration? */
374 pcAt = ScanToken(pcName, '@');
375 if (pcAt && (pcAt < (pcName + nNameLength)) && pcName[0] == '_')
376 {
377 /* Skip leading underscore and remove trailing decoration */
378 pcName++;
379 nNameLength = pcAt - pcName;
380 }
381
382 /* Print the undecorated function name */
383 fprintf(fileDest, "%.*s", nNameLength, pcName);
384 }
385 }
386
387 void
388 OutputLine_def_MS(FILE *fileDest, EXPORT *pexp)
389 {
390 PrintName(fileDest, pexp, &pexp->strName, 0);
391
392 if (gbImportLib)
393 {
394 /* Redirect to a stub function, to get the right decoration in the lib */
395 fprintf(fileDest, "=_stub_%.*s", pexp->strName.len, pexp->strName.buf);
396 }
397 else if (pexp->strTarget.buf)
398 {
399 if (pexp->strName.buf[0] == '?')
400 {
401 fprintf(stderr, "warning: ignoring C++ redirection %.*s -> %.*s\n",
402 pexp->strName.len, pexp->strName.buf, pexp->strTarget.len, pexp->strTarget.buf);
403 }
404 else
405 {
406 fprintf(fileDest, "=");
407
408 /* If the original name was decorated, use decoration in the forwarder as well */
409 if ((giArch == ARCH_X86) && ScanToken(pexp->strName.buf, '@') &&
410 !ScanToken(pexp->strTarget.buf, '@') &&
411 ((pexp->nCallingConvention == CC_STDCALL) ||
412 (pexp->nCallingConvention == CC_FASTCALL)) )
413 {
414 PrintName(fileDest, pexp, &pexp->strTarget, 1);
415 }
416 else
417 {
418 /* Write the undecorated redirection name */
419 fprintf(fileDest, "%.*s", pexp->strTarget.len, pexp->strTarget.buf);
420 }
421 }
422 }
423 else if (((pexp->uFlags & FL_STUB) || (pexp->nCallingConvention == CC_STUB)) &&
424 (pexp->strName.buf[0] == '?'))
425 {
426 /* C++ stubs are forwarded to C stubs */
427 fprintf(fileDest, "=stub_function%d", pexp->nNumber);
428 }
429 }
430
431 void
432 OutputLine_def_GCC(FILE *fileDest, EXPORT *pexp)
433 {
434 /* Print the function name, with decoration for export libs */
435 PrintName(fileDest, pexp, &pexp->strName, gbImportLib);
436 DbgPrint("Generating def line for '%.*s'\n", pexp->strName.len, pexp->strName.buf);
437
438 /* Check if this is a forwarded export */
439 if (pexp->strTarget.buf)
440 {
441 int fIsExternal = !!ScanToken(pexp->strTarget.buf, '.');
442 DbgPrint("Got redirect '%.*s'\n", pexp->strTarget.len, pexp->strTarget.buf);
443
444 /* print the target name, don't decorate if it is external */
445 fprintf(fileDest, "=");
446 PrintName(fileDest, pexp, &pexp->strTarget, !fIsExternal);
447 }
448 else if (((pexp->uFlags & FL_STUB) || (pexp->nCallingConvention == CC_STUB)) &&
449 (pexp->strName.buf[0] == '?'))
450 {
451 /* C++ stubs are forwarded to C stubs */
452 fprintf(fileDest, "=stub_function%d", pexp->nNumber);
453 }
454
455 /* Special handling for stdcall and fastcall */
456 if ((giArch == ARCH_X86) &&
457 ((pexp->nCallingConvention == CC_STDCALL) ||
458 (pexp->nCallingConvention == CC_FASTCALL)))
459 {
460 /* Is this the import lib? */
461 if (gbImportLib)
462 {
463 /* Is the name in the spec file decorated? */
464 const char* pcDeco = ScanToken(pexp->strName.buf, '@');
465 if (pcDeco && (pcDeco < pexp->strName.buf + pexp->strName.len))
466 {
467 /* Write the name including the leading @ */
468 fprintf(fileDest, "==%.*s", pexp->strName.len, pexp->strName.buf);
469 }
470 }
471 else if (!pexp->strTarget.buf)
472 {
473 /* Write a forwarder to the actual decorated symbol */
474 fprintf(fileDest, "=");
475 PrintName(fileDest, pexp, &pexp->strName, 1);
476 }
477 }
478 }
479
480 int
481 OutputLine_def(FILE *fileDest, EXPORT *pexp)
482 {
483 fprintf(fileDest, " ");
484
485 if (gbMSComp)
486 OutputLine_def_MS(fileDest, pexp);
487 else
488 OutputLine_def_GCC(fileDest, pexp);
489
490 if (pexp->uFlags & FL_ORDINAL)
491 {
492 fprintf(fileDest, " @%d", pexp->nOrdinal);
493 }
494
495 if (pexp->uFlags & FL_NONAME)
496 {
497 fprintf(fileDest, " NONAME");
498 }
499
500 if (pexp->uFlags & FL_PRIVATE)
501 {
502 fprintf(fileDest, " PRIVATE");
503 }
504
505 /* Make this a data export, unless this is MSVC and -withalias was given */
506 if ((pexp->nCallingConvention == CC_EXTERN) &&
507 !(gbMSComp && (pexp->uFlags & FL_DATA_ALIAS)))
508 {
509 fprintf(fileDest, " DATA");
510 }
511
512 fprintf(fileDest, "\n");
513
514 return 1;
515 }
516
517 int
518 ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
519 {
520 char *pc, *pcLine;
521 int nLine;
522 EXPORT exp;
523 int included;
524 char namebuffer[16];
525
526 //fprintf(stderr, "info: line %d, pcStart:'%.30s'\n", nLine, pcStart);
527
528 /* Loop all lines */
529 nLine = 1;
530 exp.nNumber = 0;
531 for (pcLine = pcStart; *pcLine; pcLine = NextLine(pcLine), nLine++)
532 {
533 pc = pcLine;
534
535 exp.nArgCount = 0;
536 exp.uFlags = 0;
537 exp.nNumber++;
538
539
540 //if (!strncmp(pcLine, "22 stdcall @(long) MPR_Alloc",28))
541 // gbDebug = 1;
542
543 //fprintf(stderr, "info: line %d, token:'%d, %.20s'\n",
544 // nLine, TokenLength(pcLine), pcLine);
545
546 /* Skip white spaces */
547 while (*pc == ' ' || *pc == '\t') pc++;
548
549 /* Skip empty lines, stop at EOF */
550 if (*pc == ';' || *pc <= '#') continue;
551 if (*pc == 0) return 0;
552
553 //fprintf(stderr, "info: line %d, token:'%.*s'\n",
554 // nLine, TokenLength(pc), pc);
555
556 /* Now we should get either an ordinal or @ */
557 if (*pc == '@')
558 exp.nOrdinal = -1;
559 else
560 {
561 exp.nOrdinal = atol(pc);
562 exp.uFlags |= FL_ORDINAL;
563 }
564
565 /* Go to next token (type) */
566 if (!(pc = NextToken(pc)))
567 {
568 fprintf(stderr, "error: line %d, unexpected end of line\n", nLine);
569 return -10;
570 }
571
572 //fprintf(stderr, "info: Token:'%.*s'\n", TokenLength(pc), pc);
573
574 /* Now we should get the type */
575 if (CompareToken(pc, "stdcall"))
576 {
577 exp.nCallingConvention = CC_STDCALL;
578 }
579 else if (CompareToken(pc, "cdecl") ||
580 CompareToken(pc, "varargs"))
581 {
582 exp.nCallingConvention = CC_CDECL;
583 }
584 else if (CompareToken(pc, "fastcall"))
585 {
586 exp.nCallingConvention = CC_FASTCALL;
587 }
588 else if (CompareToken(pc, "thiscall"))
589 {
590 exp.nCallingConvention = CC_THISCALL;
591 }
592 else if (CompareToken(pc, "extern"))
593 {
594 exp.nCallingConvention = CC_EXTERN;
595 }
596 else if (CompareToken(pc, "stub"))
597 {
598 exp.nCallingConvention = CC_STUB;
599 }
600 else
601 {
602 fprintf(stderr, "error: line %d, expected callconv, got '%.*s' %d\n",
603 nLine, TokenLength(pc), pc, *pc);
604 return -11;
605 }
606
607 //fprintf(stderr, "info: nCallingConvention: %d\n", exp.nCallingConvention);
608
609 /* Go to next token (options or name) */
610 if (!(pc = NextToken(pc)))
611 {
612 fprintf(stderr, "fail2\n");
613 return -12;
614 }
615
616 /* Handle options */
617 included = 1;
618 while (*pc == '-')
619 {
620 if (CompareToken(pc, "-arch"))
621 {
622 /* Default to not included */
623 included = 0;
624 pc += 5;
625
626 /* Look if we are included */
627 while (*pc == '=' || *pc == ',')
628 {
629 pc++;
630 if (CompareToken(pc, pszArchString) ||
631 CompareToken(pc, pszArchString2))
632 {
633 included = 1;
634 }
635
636 /* Skip to next arch or end */
637 while (*pc > ',') pc++;
638 }
639 }
640 else if (CompareToken(pc, "-i386"))
641 {
642 if (giArch != ARCH_X86) included = 0;
643 }
644 else if (CompareToken(pc, "-private"))
645 {
646 exp.uFlags |= FL_PRIVATE;
647 }
648 else if (CompareToken(pc, "-noname"))
649 {
650 exp.uFlags |= FL_ORDINAL | FL_NONAME;
651 }
652 else if (CompareToken(pc, "-ordinal"))
653 {
654 exp.uFlags |= FL_ORDINAL;
655 }
656 else if (CompareToken(pc, "-stub"))
657 {
658 exp.uFlags |= FL_STUB;
659 }
660 else if (CompareToken(pc, "-withalias"))
661 {
662 /* This flag is to create a nin _imp_ prefixed alias for a
663 data export, so that the hacked DDK declarations work */
664 if (exp.nCallingConvention != CC_EXTERN)
665 fprintf(stderr, "error: line %d -withalias on non-data export\n", nLine);
666 else
667 exp.uFlags |= FL_DATA_ALIAS;
668 }
669 else if (CompareToken(pc, "-norelay") ||
670 CompareToken(pc, "-register") ||
671 CompareToken(pc, "-ret64"))
672 {
673 /* silently ignore these */
674 }
675 else
676 {
677 fprintf(stderr, "info: ignored option: '%.*s'\n",
678 TokenLength(pc), pc);
679 }
680
681 /* Go to next token */
682 pc = NextToken(pc);
683 }
684
685 //fprintf(stderr, "info: Name:'%.10s'\n", pc);
686
687 /* If arch didn't match ours, skip this entry */
688 if (!included) continue;
689
690 /* Get name */
691 exp.strName.buf = pc;
692 exp.strName.len = TokenLength(pc);
693
694 /* Check for autoname */
695 if ((exp.strName.len == 1) && (exp.strName.buf[0] == '@'))
696 {
697 sprintf(namebuffer, "ordinal%d", exp.nOrdinal);
698 exp.strName.len = strlen(namebuffer);
699 exp.strName.buf = namebuffer;
700 exp.uFlags |= FL_ORDINAL | FL_NONAME;
701 }
702
703 /* Handle parameters */
704 exp.nStackBytes = 0;
705 if (exp.nCallingConvention != CC_EXTERN &&
706 exp.nCallingConvention != CC_STUB)
707 {
708 //fprintf(stderr, "info: options:'%.10s'\n", pc);
709 /* Go to next token */
710 if (!(pc = NextToken(pc)))
711 {
712 fprintf(stderr, "fail4\n");
713 return -13;
714 }
715
716 /* Verify syntax */
717 if (*pc++ != '(')
718 {
719 fprintf(stderr, "error: line %d, expected '('\n", nLine);
720 return -14;
721 }
722
723 /* Skip whitespaces */
724 while (*pc == ' ' || *pc == '\t') pc++;
725
726 exp.nStackBytes = 0;
727 while (*pc >= '0')
728 {
729 if (CompareToken(pc, "long"))
730 {
731 exp.nStackBytes += 4;
732 exp.anArgs[exp.nArgCount] = ARG_LONG;
733 }
734 else if (CompareToken(pc, "double"))
735 {
736 exp.nStackBytes += 8;
737 exp.anArgs[exp.nArgCount] = ARG_DBL;
738 }
739 else if (CompareToken(pc, "ptr") ||
740 CompareToken(pc, "str") ||
741 CompareToken(pc, "wstr"))
742 {
743 exp.nStackBytes += 4; // sizeof(void*) on x86
744 exp.anArgs[exp.nArgCount] = ARG_PTR; // FIXME: handle strings
745 }
746 else if (CompareToken(pc, "int64"))
747 {
748 exp.nStackBytes += 8;
749 exp.anArgs[exp.nArgCount] = ARG_INT64;
750 }
751 else if (CompareToken(pc, "int128"))
752 {
753 exp.nStackBytes += 16;
754 exp.anArgs[exp.nArgCount] = ARG_INT128;
755 }
756 else if (CompareToken(pc, "float"))
757 {
758 exp.nStackBytes += 4;
759 exp.anArgs[exp.nArgCount] = ARG_FLOAT;
760 }
761 else
762 fprintf(stderr, "error: line %d, expected type, got: %.10s\n", nLine, pc);
763
764 exp.nArgCount++;
765
766 /* Go to next parameter */
767 if (!(pc = NextToken(pc)))
768 {
769 fprintf(stderr, "fail5\n");
770 return -15;
771 }
772 }
773
774 /* Check syntax */
775 if (*pc++ != ')')
776 {
777 fprintf(stderr, "error: line %d, expected ')'\n", nLine);
778 return -16;
779 }
780 }
781
782 /* Handle special stub cases */
783 if (exp.nCallingConvention == CC_STUB)
784 {
785 /* Check for c++ mangled name */
786 if (pc[0] == '?')
787 {
788 //printf("Found c++ mangled name...\n");
789 //
790 }
791 else
792 {
793 /* Check for stdcall name */
794 const char *p = ScanToken(pc, '@');
795 if (p && (p - pc < exp.strName.len))
796 {
797 int i;
798
799 /* Truncate the name to before the @ */
800 exp.strName.len = (int)(p - pc);
801 if (exp.strName.len < 1)
802 {
803 fprintf(stderr, "error, @ in line %d\n", nLine);
804 return -1;
805 }
806 exp.nStackBytes = atoi(p + 1);
807 exp.nArgCount = exp.nStackBytes / 4;
808 exp.nCallingConvention = CC_STDCALL;
809 exp.uFlags |= FL_STUB;
810 for (i = 0; i < exp.nArgCount; i++)
811 exp.anArgs[i] = ARG_LONG;
812 }
813 }
814 }
815
816 /* Get optional redirection */
817 pc = NextToken(pc);
818 if (pc)
819 {
820 exp.strTarget.buf = pc;
821 exp.strTarget.len = TokenLength(pc);
822
823 /* Check syntax (end of line) */
824 if (NextToken(pc))
825 {
826 fprintf(stderr, "error: line %d, additional tokens after ')'\n", nLine);
827 return -17;
828 }
829 }
830 else
831 {
832 exp.strTarget.buf = 0;
833 exp.strTarget.len = 0;
834 }
835
836 /* Check for no-name without ordinal */
837 if ((exp.uFlags & FL_ORDINAL) && (exp.nOrdinal == -1))
838 {
839 fprintf(stderr, "error: line %d, ordinal export without ordinal!\n", nLine);
840 return -1;
841 }
842
843 OutputLine(fileDest, &exp);
844 gbDebug = 0;
845 }
846
847 return 0;
848 }
849
850
851 void usage(void)
852 {
853 printf("syntax: spec2pdef [<options> ...] <spec file>\n"
854 "Possible options:\n"
855 " -h --help prints this screen\n"
856 " -l=<file> generates an asm lib stub\n"
857 " -d=<file> generates a def file\n"
858 " -s=<file> generates a stub file\n"
859 " --ms msvc compatibility\n"
860 " -n=<name> name of the dll\n"
861 " --implib generate a def file for an import library\n"
862 " -a=<arch> Set architecture to <arch>. (i386, x86_64, arm)\n");
863 }
864
865 int main(int argc, char *argv[])
866 {
867 size_t nFileSize;
868 char *pszSource, *pszDefFileName = 0, *pszStubFileName = 0, *pszLibStubName = 0;
869 char achDllName[40];
870 FILE *file;
871 int result = 0, i;
872
873 if (argc < 2)
874 {
875 usage();
876 return -1;
877 }
878
879 /* Read options */
880 for (i = 1; i < argc && *argv[i] == '-'; i++)
881 {
882 if ((strcasecmp(argv[i], "--help") == 0) ||
883 (strcasecmp(argv[i], "-h") == 0))
884 {
885 usage();
886 return 0;
887 }
888 else if (argv[i][1] == 'd' && argv[i][2] == '=')
889 {
890 pszDefFileName = argv[i] + 3;
891 }
892 else if (argv[i][1] == 'l' && argv[i][2] == '=')
893 {
894 pszLibStubName = argv[i] + 3;
895 }
896 else if (argv[i][1] == 's' && argv[i][2] == '=')
897 {
898 pszStubFileName = argv[i] + 3;
899 }
900 else if (argv[i][1] == 'n' && argv[i][2] == '=')
901 {
902 pszDllName = argv[i] + 3;
903 }
904 else if ((strcasecmp(argv[i], "--implib") == 0))
905 {
906 gbImportLib = 1;
907 }
908 else if ((strcasecmp(argv[i], "--ms") == 0))
909 {
910 gbMSComp = 1;
911 }
912 else if (argv[i][1] == 'a' && argv[i][2] == '=')
913 {
914 pszArchString = argv[i] + 3;
915 }
916 else
917 {
918 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
919 return -1;
920 }
921 }
922
923 if (strcasecmp(pszArchString, "i386") == 0)
924 {
925 giArch = ARCH_X86;
926 gpszUnderscore = "_";
927 }
928 else if (strcasecmp(pszArchString, "x86_64") == 0) giArch = ARCH_AMD64;
929 else if (strcasecmp(pszArchString, "ia64") == 0) giArch = ARCH_IA64;
930 else if (strcasecmp(pszArchString, "arm") == 0) giArch = ARCH_ARM;
931 else if (strcasecmp(pszArchString, "ppc") == 0) giArch = ARCH_PPC;
932
933 if ((giArch == ARCH_AMD64) || (giArch == ARCH_IA64))
934 {
935 pszArchString2 = "win64";
936 }
937 else
938 pszArchString2 = "win32";
939
940 /* Set a default dll name */
941 if (!pszDllName)
942 {
943 char *p1, *p2;
944 size_t len;
945
946 p1 = strrchr(argv[i], '\\');
947 if (!p1) p1 = strrchr(argv[i], '/');
948 p2 = p1 = p1 ? p1 + 1 : argv[i];
949
950 /* walk up to '.' */
951 while (*p2 != '.' && *p2 != 0) p2++;
952 len = p2 - p1;
953 if (len >= sizeof(achDllName) - 5)
954 {
955 fprintf(stderr, "name too long: %s\n", p1);
956 return -2;
957 }
958
959 strncpy(achDllName, p1, len);
960 strncpy(achDllName + len, ".dll", sizeof(achDllName) - len);
961 pszDllName = achDllName;
962 }
963
964 /* Open input file argv[1] */
965 file = fopen(argv[i], "r");
966 if (!file)
967 {
968 fprintf(stderr, "error: could not open file %s ", argv[i]);
969 return -3;
970 }
971
972 /* Get file size */
973 fseek(file, 0, SEEK_END);
974 nFileSize = ftell(file);
975 rewind(file);
976
977 /* Allocate memory buffer */
978 pszSource = malloc(nFileSize + 1);
979 if (!pszSource)
980 {
981 fclose(file);
982 return -4;
983 }
984
985 /* Load input file into memory */
986 nFileSize = fread(pszSource, 1, nFileSize, file);
987 fclose(file);
988
989 /* Zero terminate the source */
990 pszSource[nFileSize] = '\0';
991
992 if (pszDefFileName)
993 {
994 /* Open output file */
995 file = fopen(pszDefFileName, "w");
996 if (!file)
997 {
998 fprintf(stderr, "error: could not open output file %s ", argv[i + 1]);
999 return -5;
1000 }
1001
1002 OutputHeader_def(file, pszDllName);
1003 result = ParseFile(pszSource, file, OutputLine_def);
1004 fclose(file);
1005 }
1006
1007 if (pszStubFileName)
1008 {
1009 /* Open output file */
1010 file = fopen(pszStubFileName, "w");
1011 if (!file)
1012 {
1013 fprintf(stderr, "error: could not open output file %s ", argv[i + 1]);
1014 return -5;
1015 }
1016
1017 OutputHeader_stub(file);
1018 result = ParseFile(pszSource, file, OutputLine_stub);
1019 fclose(file);
1020 }
1021
1022 if (pszLibStubName)
1023 {
1024 /* Open output file */
1025 file = fopen(pszLibStubName, "w");
1026 if (!file)
1027 {
1028 fprintf(stderr, "error: could not open output file %s ", argv[i + 1]);
1029 return -5;
1030 }
1031
1032 OutputHeader_asmstub(file, pszDllName);
1033 result = ParseFile(pszSource, file, OutputLine_asmstub);
1034 fprintf(file, "\nEND\n");
1035 fclose(file);
1036 }
1037
1038
1039 return result;
1040 }