get rid of some duplication
[ia32rtools.git] / tools / mkdef_ord.c
index 4f6b50b..3873c3a 100644 (file)
@@ -1,13 +1,18 @@
+/*
+ * ia32rtools
+ * (C) notaz, 2013,2014
+ *
+ * This work is licensed under the terms of 3-clause BSD license.
+ * See COPYING file in the top-level directory.
+ */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "my_assert.h"
 #include "my_str.h"
-
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
-#define IS(w, y) !strcmp(w, y)
-#define IS_START(w, y) !strncmp(w, y, strlen(y))
+#include "common.h"
 
 #include "protoparse.h"
 
@@ -19,13 +24,22 @@ int main(int argc, char *argv[])
   char line[256];
   char fmt[256];
   char word[256];
+  int noname = 0;
+  const char *p2;
   char *p;
-  int arg = 1;
+  int arg;
   int ret, ord;
   int l;
 
-  if (argc != 3) {
-    printf("usage:\n%s <.h> <.def>\n", argv[0]);
+  for (arg = 1; arg < argc; arg++) {
+    if (IS(argv[arg], "-n"))
+      noname = 1;
+    else
+      break;
+  }
+
+  if (argc != arg + 2) {
+    printf("usage:\n%s [-n] <.h> <.def>\n", argv[0]);
     return 1;
   }
 
@@ -38,9 +52,12 @@ int main(int argc, char *argv[])
 
   p = strrchr(hdrfn, '.');
   my_assert_not(p, NULL);
-  l = p - hdrfn;
-  my_assert(l < 256, 1);
-  memcpy(basename, hdrfn, l);
+  p2 = strrchr(hdrfn, '/');
+  if (p2++ == NULL)
+    p2 = hdrfn;
+  l = p - p2;
+  my_assert((unsigned int)l < 256, 1);
+  memcpy(basename, p2, l);
   basename[l] = 0;
 
   snprintf(fmt, sizeof(fmt), "%s_%%d", basename);
@@ -54,7 +71,7 @@ int main(int argc, char *argv[])
     if (*p == 0)
       continue;
 
-    if (IS(p, "//"))
+    if (IS_START(p, "//"))
       continue;
 
     ret = 0;
@@ -79,7 +96,10 @@ int main(int argc, char *argv[])
       fprintf(fout, "@%-2d", pp->argc_stack * 4);
     else
       fprintf(fout, "   ");
-    fprintf(fout, " @%d\n", ord);
+    fprintf(fout, " @%d", ord);
+    if (noname)
+      fprintf(fout, " NONAME");
+    fprintf(fout, "\n");
   }
 
   fclose(fhdr);