[SPEC2DEF]
[reactos.git] / reactos / tools / spec2def / spec2def.c
index 1c22275..de427f0 100644 (file)
@@ -51,6 +51,8 @@ enum
     FL_PRIVATE = 1,
     FL_STUB = 2,
     FL_NONAME = 4,
+    FL_ORDINAL = 8,
+    FL_DATA_ALIAS = 16
 };
 
 enum
@@ -204,7 +206,7 @@ OutputLine_stub(FILE *file, EXPORT *pexp)
         }
         fprintf(file, " a%d", i);
     }
-    fprintf(file, ")\n{\n\tDPRINT1(\"WARNING: calling stub %.*s(",
+    fprintf(file, ")\n{\n\tDbgPrint(\"WARNING: calling stub %.*s(",
             pexp->strName.len, pexp->strName.buf);
 
     for (i = 0; i < pexp->nArgCount; i++)
@@ -368,6 +370,15 @@ PrintName(FILE *fileDest, EXPORT *pexp, PSTRING pstr, int fDeco)
     }
     else
     {
+        /* Does the string already have stdcall decoration? */
+        pcAt = ScanToken(pcName, '@');
+        if (pcAt && (pcAt < (pcName + nNameLength)) && pcName[0] == '_')
+        {
+            /* Skip leading underscore and remove trailing decoration */
+            pcName++;
+            nNameLength = pcAt - pcName;
+        }
+
         /* Print the undecorated function name */
         fprintf(fileDest, "%.*s", nNameLength, pcName);
     }
@@ -476,14 +487,14 @@ OutputLine_def(FILE *fileDest, EXPORT *pexp)
     else
         OutputLine_def_GCC(fileDest, pexp);
 
-    if (pexp->nOrdinal != -1)
+    if (pexp->uFlags & FL_ORDINAL)
     {
         fprintf(fileDest, " @%d", pexp->nOrdinal);
     }
 
-    if (pexp->nCallingConvention == CC_EXTERN)
+    if (pexp->uFlags & FL_NONAME)
     {
-        fprintf(fileDest, " DATA");
+        fprintf(fileDest, " NONAME");
     }
 
     if (pexp->uFlags & FL_PRIVATE)
@@ -491,9 +502,11 @@ OutputLine_def(FILE *fileDest, EXPORT *pexp)
         fprintf(fileDest, " PRIVATE");
     }
 
-    if (pexp->uFlags & FL_NONAME)
+    /* Make this a data export, unless this is MSVC and -withalias was given */
+    if ((pexp->nCallingConvention == CC_EXTERN) &&
+        !(gbMSComp && (pexp->uFlags & FL_DATA_ALIAS)))
     {
-        fprintf(fileDest, " NONAME");
+        fprintf(fileDest, " DATA");
     }
 
     fprintf(fileDest, "\n");
@@ -541,8 +554,13 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
         //        nLine, TokenLength(pc), pc);
 
         /* Now we should get either an ordinal or @ */
-        if (*pc == '@') exp.nOrdinal = -1;
-        else exp.nOrdinal = atol(pc);
+        if (*pc == '@')
+            exp.nOrdinal = -1;
+        else
+        {
+            exp.nOrdinal = atol(pc);
+            exp.uFlags |= FL_ORDINAL;
+        }
 
         /* Go to next token (type) */
         if (!(pc = NextToken(pc)))
@@ -627,15 +645,27 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
             {
                 exp.uFlags |= FL_PRIVATE;
             }
-            else if (CompareToken(pc, "-noname") ||
-                     CompareToken(pc, "-ordinal"))
+            else if (CompareToken(pc, "-noname"))
+            {
+                exp.uFlags |= FL_ORDINAL | FL_NONAME;
+            }
+            else if (CompareToken(pc, "-ordinal"))
             {
-                exp.uFlags |= FL_NONAME;
+                exp.uFlags |= FL_ORDINAL;
             }
             else if (CompareToken(pc, "-stub"))
             {
                 exp.uFlags |= FL_STUB;
             }
+            else if (CompareToken(pc, "-withalias"))
+            {
+                /* This flag is to create a nin _imp_ prefixed alias for a
+                   data export, so that the hacked DDK declarations work */
+                if (exp.nCallingConvention != CC_EXTERN)
+                    fprintf(stderr, "error: line %d -withalias on non-data export\n", nLine);
+                else
+                    exp.uFlags |= FL_DATA_ALIAS;
+            }
             else if (CompareToken(pc, "-norelay") ||
                      CompareToken(pc, "-register") ||
                      CompareToken(pc, "-ret64"))
@@ -667,7 +697,7 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
             sprintf(namebuffer, "ordinal%d", exp.nOrdinal);
             exp.strName.len = strlen(namebuffer);
             exp.strName.buf = namebuffer;
-            exp.uFlags |= FL_NONAME;
+            exp.uFlags |= FL_ORDINAL | FL_NONAME;
         }
 
         /* Handle parameters */
@@ -804,9 +834,10 @@ ParseFile(char* pcStart, FILE *fileDest, PFNOUTLINE OutputLine)
         }
 
         /* Check for no-name without ordinal */
-        if ((exp.uFlags & FL_NONAME) && (exp.nOrdinal == -1))
+        if ((exp.uFlags & FL_ORDINAL) && (exp.nOrdinal == -1))
         {
-            fprintf(stderr, "error: line %d, noname export without ordinal!\n", nLine);
+            fprintf(stderr, "error: line %d, ordinal export without ordinal!\n", nLine);
+            return -1;
         }
 
         OutputLine(fileDest, &exp);