translate: add ref hint, document others
[ia32rtools.git] / tools / cvt_data.c
index 8f999a3..09e7ebc 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "my_assert.h"
 #include "my_str.h"
+#include "common.h"
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
 #define IS(w, y) !strcmp(w, y)
@@ -87,7 +88,7 @@ static void next_section(FILE *fasm, char *name)
 
   name[0] = 0;
 
-  while (fgets(line, sizeof(line), fasm))
+  while (my_fgets(line, sizeof(line), fasm))
   {
     wordc = 0;
     asmln++;
@@ -96,14 +97,8 @@ static void next_section(FILE *fasm, char *name)
     if (*p == 0)
       continue;
 
-    if (*p == ';') {
-      while (strlen(line) == sizeof(line) - 1) {
-        // one of those long comment lines..
-        if (!fgets(line, sizeof(line), fasm))
-          break;
-      }
+    if (*p == ';')
       continue;
-    }
 
     for (wordc = 0; wordc < ARRAY_SIZE(words); wordc++) {
       p = sskip(next_word(words[wordc], sizeof(words[0]), p));
@@ -200,7 +195,7 @@ static char *escape_string(char *s)
 
   for (; *s != 0; s++) {
     if (*s == '"') {
-      strcpy(t, "\\22");
+      strcpy(t, "\\\"");
       t += strlen(t);
       continue;
     }
@@ -211,7 +206,9 @@ static char *escape_string(char *s)
     }
     *t++ = *s;
   }
-  *t = *s;
+  *t++ = *s;
+  if (t - buf > sizeof(buf))
+    aerr("string is too long\n");
   return strcpy(s, buf);
 }
 
@@ -462,7 +459,7 @@ int main(int argc, char *argv[])
     frlist = fopen(argv[arg], "r");
     my_assert_not(frlist, NULL);
 
-    while (fgets(line, sizeof(line), frlist)) {
+    while (my_fgets(line, sizeof(line), frlist)) {
       p = sskip(line);
       if (*p == 0 || *p == ';')
         continue;
@@ -514,7 +511,7 @@ int main(int argc, char *argv[])
     if (!header_mode)
       fprintf(fout, ".align %d\n", align_value(4));
 
-    while (fgets(line, sizeof(line), fasm))
+    while (my_fgets(line, sizeof(line), fasm))
     {
       sym = NULL;
       asmln++;
@@ -696,7 +693,9 @@ int main(int argc, char *argv[])
             val = parse_number(words[w]);
             if (val & ~0xff)
               aerr("bad string trailing byte?\n");
-            fprintf(fout, "\\x%02lx", val);
+            // unfortunately \xHH is unusable - gas interprets
+            // things like \x27b as 0x7b, so have to use octal here
+            fprintf(fout, "\\%03lo", val);
           }
         }
         fprintf(fout, "\"");