]> granicus.if.org Git - yasm/commitdiff
Change preprocessor interface from block-oriented to line-oriented.
authorPeter Johnson <peter@tortall.net>
Sat, 3 Nov 2007 04:27:35 +0000 (04:27 -0000)
committerPeter Johnson <peter@tortall.net>
Sat, 3 Nov 2007 04:27:35 +0000 (04:27 -0000)
This will make certain types of parser-preprocessor synchronization
easier for upcoming feature enhancements.

Due to additional complexity in GAS (rept), internally GAS converts
lines back into blocks.

svn path=/trunk/yasm/; revision=2007

12 files changed:
frontends/yasm/yasm.c
libyasm/preproc.h
modules/parsers/gas/gas-parser.c
modules/parsers/gas/gas-parser.h
modules/parsers/gas/gas-token.re
modules/parsers/nasm/nasm-parse.c
modules/parsers/nasm/nasm-parser.c
modules/parsers/nasm/nasm-parser.h
modules/parsers/nasm/nasm-token.re
modules/preprocs/cpp/cpp-preproc.c
modules/preprocs/nasm/nasm-preproc.c
modules/preprocs/raw/raw-preproc.c

index 31281874a43b1f84802cc9c9cae200bdeabef729..b7bfd80c00d0d049fa16228360f350f199620d79 100644 (file)
@@ -223,7 +223,7 @@ static int
 do_preproc_only(void)
 {
     yasm_linemap *linemap;
-    char *preproc_buf = yasm_xmalloc(PREPROC_BUF_SIZE);
+    char *preproc_buf;
     size_t got;
     const char *base_filename;
     FILE *out = NULL;
@@ -257,10 +257,8 @@ do_preproc_only(void)
     } else {
         /* Open output (object) file */
         out = open_file(obj_filename, "wt");
-        if (!out) {
-            yasm_xfree(preproc_buf);
+        if (!out)
             return EXIT_FAILURE;
-        }
     }
 
     /* Pre-process until done */
@@ -273,6 +271,8 @@ do_preproc_only(void)
     if (generate_make_dependencies) {
         size_t totlen;
 
+        preproc_buf = yasm_xmalloc(PREPROC_BUF_SIZE);
+
         fprintf(stdout, "%s: %s", obj_filename, in_filename);
         totlen = strlen(obj_filename)+2+strlen(in_filename);
 
@@ -287,10 +287,13 @@ do_preproc_only(void)
             fwrite(preproc_buf, got, 1, stdout);
         }
         fputc('\n', stdout);
+        yasm_xfree(preproc_buf);
     } else {
-        while ((got = yasm_preproc_input(cur_preproc, preproc_buf,
-                                         PREPROC_BUF_SIZE)) != 0)
-            fwrite(preproc_buf, got, 1, out);
+        while ((preproc_buf = yasm_preproc_get_line(cur_preproc)) != NULL) {
+            fputs(preproc_buf, out);
+            fputc('\n', out);
+            yasm_xfree(preproc_buf);
+        }
     }
 
     if (out != stdout)
@@ -301,7 +304,6 @@ do_preproc_only(void)
                                  print_yasm_error, print_yasm_warning);
         if (out != stdout)
             remove(obj_filename);
-        yasm_xfree(preproc_buf);
         yasm_linemap_destroy(linemap);
         yasm_errwarns_destroy(errwarns);
         cleanup(NULL);
@@ -310,7 +312,6 @@ do_preproc_only(void)
 
     yasm_errwarns_output_all(errwarns, linemap, warning_error,
                              print_yasm_error, print_yasm_warning);
-    yasm_xfree(preproc_buf);
     yasm_linemap_destroy(linemap);
     yasm_errwarns_destroy(errwarns);
     cleanup(NULL);
index 98f56e8fc3d509708693629c14a42870031bec26..0964c0e8a4200fbdb22b0f50cf6590972f3287e3 100644 (file)
@@ -73,11 +73,10 @@ typedef struct yasm_preproc_module {
      */
     void (*destroy) (/*@only@*/ yasm_preproc *preproc);
 
-    /** Module-level implementation of yasm_preproc_input().
-     * Call yasm_preproc_input() instead of calling this function.
+    /** Module-level implementation of yasm_preproc_get_line().
+     * Call yasm_preproc_get_line() instead of calling this function.
      */
-    size_t (*input) (yasm_preproc *preproc, /*@out@*/ char *buf,
-                     size_t max_size);
+    char * (*get_line) (yasm_preproc *preproc);
 
     /** Module-level implementation of yasm_preproc_get_included_file().
      * Call yasm_preproc_get_included_file() instead of calling this function.
@@ -125,15 +124,11 @@ typedef struct yasm_preproc_module {
  */
 void yasm_preproc_destroy(/*@only@*/ yasm_preproc *preproc);
 
-/** Gets more preprocessed source code (up to max_size bytes) into buf.
- * More than a single line may be returned in buf.
+/** Gets a single line of preprocessed source code.
  * \param preproc       preprocessor
- * \param buf           destination buffer for preprocessed source
- * \param max_size      maximum number of bytes that can be returned in buf
- * \return Actual number of bytes returned in buf.
+ * \return Allocated line of code, without the trailing \n.
  */
-size_t yasm_preproc_input(yasm_preproc *preproc, /*@out@*/ char *buf,
-                          size_t max_size);
+char *yasm_preproc_get_line(yasm_preproc *preproc);
 
 /** Get the next filename included by the source code.
  * \param preproc       preprocessor
@@ -180,8 +175,8 @@ void yasm_preproc_define_builtin(yasm_preproc *preproc,
 
 #define yasm_preproc_destroy(preproc) \
     ((yasm_preproc_base *)preproc)->module->destroy(preproc)
-#define yasm_preproc_input(preproc, buf, max_size) \
-    ((yasm_preproc_base *)preproc)->module->input(preproc, buf, max_size)
+#define yasm_preproc_get_line(preproc) \
+    ((yasm_preproc_base *)preproc)->module->get_line(preproc)
 #define yasm_preproc_get_included_file(preproc, buf, max_size) \
     ((yasm_preproc_base *)preproc)->module->get_included_file(preproc, buf, max_size)
 #define yasm_preproc_add_include_file(preproc, filename) \
index 521851cdd8ce93de0157f4ac2b0eadd8db344c96..80bd5039c0abb07f58a6a82b95d0a838cc33a6a8 100644 (file)
@@ -64,6 +64,8 @@ gas_parser_do_parse(yasm_object *object, yasm_preproc *pp,
 
     parser_gas.peek_token = NONE;
 
+    parser_gas.line = NULL;
+
     /* initialize scanner structure */
     yasm_scanner_initialize(&parser_gas.s);
 
index eece2a23f8f65d1fc96f24fe8fab26fba1eeed14..0a22e3e4fa67670693885b1d2f1077e16b08cea2 100644 (file)
@@ -122,6 +122,10 @@ typedef struct yasm_parser_gas {
     YYCTYPE save_line[2][MAX_SAVED_LINE_LEN];
     int save_last;
 
+    /* Line data storage used in preproc_input(). */
+    char *line, *linepos;
+    size_t lineleft;
+
     yasm_scanner s;
     enum gas_parser_state state;
 
index 1251225dc54296269654f3330c86c9f16b90b1cb..c72ce901e77a3cb35a1681a050b26d82b0ae7cee 100644 (file)
@@ -125,6 +125,43 @@ rept_input(yasm_parser_gas *parser_gas, /*@out@*/ YYCTYPE *buf,
 
     return (max_size-numleft);
 }
+
+/* Bridge function to convert byte-oriented parser with line-oriented
+ * preprocessor.
+ */
+static size_t
+preproc_input(yasm_parser_gas *parser_gas, /*@out@*/ YYCTYPE *buf,
+              size_t max_size)
+{
+    size_t tot=0;
+    while (max_size > 0) {
+        size_t n;
+
+        if (!parser_gas->line) {
+            parser_gas->line = yasm_preproc_get_line(parser_gas->preproc);
+            if (!parser_gas->line)
+                return tot; /* EOF */
+            parser_gas->linepos = parser_gas->line;
+            parser_gas->lineleft = strlen(parser_gas->line) + 1;
+            parser_gas->line[parser_gas->lineleft-1] = '\n';
+        }
+
+        n = parser_gas->lineleft<max_size ? parser_gas->lineleft : max_size;
+        strncpy((char *)buf+tot, parser_gas->linepos, n);
+
+        if (n == parser_gas->lineleft) {
+            yasm_xfree(parser_gas->line);
+            parser_gas->line = NULL;
+        } else {
+            parser_gas->lineleft -= n;
+            parser_gas->linepos += n;
+        }
+
+        tot += n;
+        max_size -= n;
+    }
+    return tot;
+}
 #if 0
 static size_t
 fill_input(void *d, unsigned char *buf, size_t max)
@@ -163,8 +200,7 @@ fill(yasm_parser_gas *parser_gas, YYCTYPE *cursor)
         if (parser_gas->rept && parser_gas->rept->ended) {
             /* Pull from rept lines instead of preproc */
             cnt = rept_input(parser_gas, s->lim, BSIZE);
-        } else if((cnt = yasm_preproc_input(parser_gas->preproc,
-                                            (char *)s->lim, BSIZE)) == 0) {
+        } else if((cnt = preproc_input(parser_gas, s->lim, BSIZE)) == 0) {
             s->eof = &s->lim[cnt]; *s->eof++ = '\n';
         }
         s->lim += cnt;
index c615ecea78b40b414e23474b98079e7802313e6d..2f7f59ba34b03e41d6a85836480e90809240c3f1 100644 (file)
@@ -64,7 +64,7 @@ static void nasm_parser_directive
 static void define_label(yasm_parser_nasm *parser_nasm, /*@only@*/ char *name,
                          int local);
 
-#define is_eol_tok(tok) ((tok) == '\n' || (tok) == 0)
+#define is_eol_tok(tok) ((tok) == 0)
 #define is_eol()        is_eol_tok(curtok)
 
 #define get_next_token()    (curtok = nasm_parser_lex(&curval, parser_nasm))
@@ -186,9 +186,19 @@ expect_(yasm_parser_nasm *parser_nasm, int token)
 void
 nasm_parser_parse(yasm_parser_nasm *parser_nasm)
 {
-    while (get_next_token() != 0) {
+    unsigned char *line;
+    while ((line = (unsigned char *)
+            yasm_preproc_get_line(parser_nasm->preproc)) != NULL) {
         yasm_bytecode *bc = NULL, *temp_bc;
-        
+
+        parser_nasm->s.bot = line;
+        parser_nasm->s.tok = line;
+        parser_nasm->s.ptr = line;
+        parser_nasm->s.cur = line;
+        parser_nasm->s.lim = line + strlen((char *)line)+1;
+        parser_nasm->s.top = parser_nasm->s.lim;
+
+        get_next_token();
         if (!is_eol()) {
             bc = parse_line(parser_nasm);
             demand_eol();
@@ -230,10 +240,10 @@ nasm_parser_parse(yasm_parser_nasm *parser_nasm)
         yasm_errwarn_propagate(parser_nasm->errwarns, cur_line);
 
         if (parser_nasm->save_input)
-            yasm_linemap_add_source(parser_nasm->linemap,
-                temp_bc,
-                (char *)parser_nasm->save_line[parser_nasm->save_last ^ 1]);
+            yasm_linemap_add_source(parser_nasm->linemap, temp_bc,
+                                    (char *)line);
         yasm_linemap_goto_next(parser_nasm->linemap);
+        yasm_xfree(line);
     }
 }
 
index 7138c9d2a4248779ba2cb02692767c3f43f522b1..dc46a7170133783d4639c04fb5d5c6faaddc6faf 100644 (file)
@@ -51,7 +51,6 @@ nasm_parser_do_parse(yasm_object *object, yasm_preproc *pp,
     parser_nasm.prev_bc = yasm_section_bcs_first(object->cur_section);
 
     parser_nasm.save_input = save_input;
-    parser_nasm.save_last = 0;
 
     parser_nasm.peek_token = NONE;
 
@@ -68,7 +67,7 @@ nasm_parser_do_parse(yasm_object *object, yasm_preproc *pp,
 
     nasm_parser_parse(&parser_nasm);
 
-    yasm_scanner_delete(&parser_nasm.s);
+    /*yasm_scanner_delete(&parser_nasm.s);*/
 
     /* Free locallabel base if necessary */
     if (parser_nasm.locallabel_base)
index 2f14522891305c40dc6b70c5511ea9803f9655d8..359761c31d5af2d9e1c3fa9e656dbb82dec454e3 100644 (file)
@@ -97,8 +97,6 @@ typedef struct yasm_parser_nasm {
     /*@null@*/ yasm_bytecode *prev_bc;
 
     int save_input;
-    YYCTYPE save_line[2][MAX_SAVED_LINE_LEN];
-    int save_last;
 
     yasm_scanner s;
     enum {
index cfaad6d57bf1fe80329fa157293c49917e5bc46d..cbf9fb0f7408ca5c1611fe074c4de97cdee7e69e 100644 (file)
@@ -34,12 +34,10 @@ RCSID("$Id$");
 #include "modules/parsers/nasm/nasm-parser.h"
 
 
-#define BSIZE   8192
-
 #define YYCURSOR        cursor
 #define YYLIMIT         (s->lim)
 #define YYMARKER        (s->ptr)
-#define YYFILL(n)       {fill(parser_nasm, &cursor);}
+#define YYFILL(n)       {}
 
 #define RETURN(i)       {s->cur = cursor; parser_nasm->tokch = s->tok[0]; \
                          return i;}
@@ -50,48 +48,6 @@ RCSID("$Id$");
 #define TOKLEN          (size_t)(cursor-s->tok)
 
 
-static size_t
-fill_input(void *d, unsigned char *buf, size_t max)
-{
-    return yasm_preproc_input((yasm_preproc *)d, (char *)buf, max);
-}
-
-static void
-fill(yasm_parser_nasm *parser_nasm, YYCTYPE **cursor)
-{
-    yasm_scanner *s = &parser_nasm->s;
-    if (yasm_fill_helper(s, cursor, fill_input, parser_nasm->preproc)
-        && parser_nasm->save_input) {
-        int i;
-        YYCTYPE *saveline;
-        parser_nasm->save_last ^= 1;
-        saveline = parser_nasm->save_line[parser_nasm->save_last];
-        /* save next line into cur_line */
-        for (i=0; i<79 && &s->tok[i] < s->lim && s->tok[i] != '\n'; i++)
-            saveline[i] = s->tok[i];
-        saveline[i] = '\0';
-    }
-}
-
-static YYCTYPE *
-save_line(yasm_parser_nasm *parser_nasm, YYCTYPE *cursor)
-{
-    yasm_scanner *s = &parser_nasm->s;
-    int i = 0;
-    YYCTYPE *saveline;
-
-    parser_nasm->save_last ^= 1;
-    saveline = parser_nasm->save_line[parser_nasm->save_last];
-
-    /* save next line into cur_line */
-    if ((YYLIMIT - YYCURSOR) < 80)
-        YYFILL(80);
-    for (i=0; i<79 && &cursor[i] < s->lim && cursor[i] != '\n'; i++)
-        saveline[i] = cursor[i];
-    saveline[i] = '\0';
-    return cursor;
-}
-
 /* starting size of string buffer */
 #define STRBUF_ALLOC_SIZE       128
 
@@ -104,7 +60,7 @@ static size_t strbuf_size = 0;
 static int linechg_numcount;
 
 /*!re2c
-  any = [\000-\377];
+  any = [\001-\377];
   digit = [0-9];
   iletter = [a-zA-Z];
   bindigit = [01];
@@ -133,7 +89,7 @@ nasm_parser_lex(YYSTYPE *lvalp, yasm_parser_nasm *parser_nasm)
         return tok;
     }
 
-    /* Catch EOF */
+    /* Catch EOL (EOF from the scanner perspective) */
     if (s->eof && cursor == s->eof)
         return 0;
 
@@ -406,9 +362,7 @@ scan:
 
         ws+                     { goto scan; }
 
-        "\n"                    {
-            if (parser_nasm->save_input)
-                cursor = save_line(parser_nasm, cursor);
+        [\000]                  {
             parser_nasm->state = INITIAL;
             RETURN(s->tok[0]);
         }
@@ -435,9 +389,7 @@ linechg:
             RETURN(INTNUM);
         }
 
-        "\n" {
-            if (parser_nasm->save_input)
-                cursor = save_line(parser_nasm, cursor);
+        [\000] {
             parser_nasm->state = INITIAL;
             RETURN(s->tok[0]);
         }
@@ -466,9 +418,7 @@ linechg2:
     SCANINIT();
 
     /*!re2c
-        "\n" {
-            if (parser_nasm->save_input)
-                cursor = save_line(parser_nasm, cursor);
+        [\000] {
             parser_nasm->state = INITIAL;
             RETURN(s->tok[0]);
         }
@@ -487,9 +437,7 @@ directive:
     SCANINIT();
 
     /*!re2c
-        [\]\n] {
-            if (parser_nasm->save_input)
-                cursor = save_line(parser_nasm, cursor);
+        [\]\000] {
             parser_nasm->state = INITIAL;
             RETURN(s->tok[0]);
         }
@@ -540,9 +488,7 @@ section_directive:
             RETURN(s->tok[0]);
         }
 
-        "\n"                    {
-            if (parser_nasm->save_input)
-                cursor = save_line(parser_nasm, cursor);
+        [\000]          {
             parser_nasm->state = INITIAL;
             RETURN(s->tok[0]);
         }
@@ -654,9 +600,7 @@ directive2:
 
         ws+                     { goto directive2; }
 
-        "\n"                    {
-            if (parser_nasm->save_input)
-                cursor = save_line(parser_nasm, cursor);
+        [\000]                  {
             parser_nasm->state = INITIAL;
             RETURN(s->tok[0]);
         }
@@ -679,17 +623,11 @@ stringconst_scan:
     SCANINIT();
 
     /*!re2c
-        "\n"    {
-            if (cursor == s->eof)
-                yasm_error_set(YASM_ERROR_SYNTAX,
-                               N_("unexpected end of file in string"));
-            else
-                yasm_error_set(YASM_ERROR_SYNTAX, N_("unterminated string"));
+        [\000]  {
+            yasm_error_set(YASM_ERROR_SYNTAX, N_("unterminated string"));
             strbuf[count] = '\0';
             lvalp->str.contents = (char *)strbuf;
             lvalp->str.len = count;
-            if (parser_nasm->save_input)
-                cursor = save_line(parser_nasm, cursor);
             RETURN(STRING);
         }
 
index 6b87c427da348eaee3703ef0ca2fe366178b904e..77f3107ff06372805c2be8e620e722d9f54e6633 100644 (file)
@@ -32,6 +32,8 @@
 /* TODO: Use autoconf to get the limit on the command line length. */
 #define CMDLINE_SIZE 32770
 
+#define BSIZE 512
+
 /* Pre-declare the preprocessor module object. */
 yasm_preproc_module yasm_cpp_LTX_preproc;
 
@@ -228,11 +230,12 @@ cpp_preproc_destroy(yasm_preproc *preproc)
     yasm_xfree(pp);
 }
 
-static size_t
-cpp_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size)
+static char *
+cpp_preproc_get_line(yasm_preproc *preproc)
 {
-    size_t n;
     yasm_preproc_cpp *pp = (yasm_preproc_cpp *)preproc;
+    int bufsize = BSIZE;
+    char *buf, *p;
 
     if (! (pp->flags & CPP_HAS_BEEN_INVOKED) ) {
         pp->flags |= CPP_HAS_BEEN_INVOKED;
@@ -244,14 +247,42 @@ cpp_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size)
         Once the preprocessor has been run, we're just dealing with a normal
         file.
     */
-    if (((n = fread(buf, 1, max_size, pp->f)) == 0) &&
-               ferror(pp->f)) {
-        yasm_error_set(YASM_ERROR_IO, N_("error reading from pipe"));
-        yasm_errwarn_propagate(pp->errwarns,
-                               yasm_linemap_get_current(pp->cur_lm));
+
+    /* Loop to ensure entire line is read (don't want to limit line length). */
+    buf = yasm_xmalloc((size_t)bufsize);
+    p = buf;
+    for (;;) {
+        if (!fgets(p, bufsize-(p-buf), pp->f)) {
+            if (ferror(pp->f)) {
+                yasm_error_set(YASM_ERROR_IO,
+                               N_("error when reading from file"));
+                yasm_errwarn_propagate(pp->errwarns,
+                    yasm_linemap_get_current(pp->cur_lm));
+            }
+            break;
+        }
+        p += strlen(p);
+        if (p > buf && p[-1] == '\n')
+            break;
+        if ((p-buf) >= bufsize) {
+            /* Increase size of buffer */
+            char *oldbuf = buf;
+            bufsize *= 2;
+            buf = yasm_xrealloc(buf, (size_t)bufsize);
+            p = buf + (p-oldbuf);
+        }
     }
 
-    return n;
+    if (p == buf) {
+        /* No data; must be at EOF */
+        yasm_xfree(buf);
+        return NULL;
+    }
+
+    /* Strip the line ending */
+    buf[strcspn(buf, "\r\n")] = '\0';
+
+    return buf;
 }
 
 static size_t
@@ -354,7 +385,7 @@ yasm_preproc_module yasm_cpp_LTX_preproc = {
     "cpp",
     cpp_preproc_create,
     cpp_preproc_destroy,
-    cpp_preproc_input,
+    cpp_preproc_get_line,
     cpp_preproc_get_included_file,
     cpp_preproc_add_include_file,
     cpp_preproc_predefine_macro,
index 78993b57af21960f6219103ab1955fd3c2269e4e..c1394a266b34617bcc30cf98f97989ca8ab25f25 100644 (file)
@@ -38,8 +38,7 @@ typedef struct yasm_preproc_nasm {
     yasm_preproc_base preproc;   /* Base structure */
 
     FILE *in;
-    char *line, *linepos;
-    size_t lineleft;
+    char *line;
     char *file_name;
     long prior_linnum;
     int lineinc;
@@ -129,7 +128,7 @@ nasm_efunc(int severity, const char *fmt, ...)
 
 static yasm_preproc *
 nasm_preproc_create(const char *in_filename, yasm_linemap *lm,
-                    yasm_errwarns *errwarns)
+                   yasm_errwarns *errwarns)
 {
     FILE *f;
     yasm_preproc_nasm *preproc_nasm = yasm_xmalloc(sizeof(yasm_preproc_nasm));
@@ -163,6 +162,8 @@ nasm_preproc_destroy(yasm_preproc *preproc)
 {
     yasm_preproc_nasm *preproc_nasm = (yasm_preproc_nasm *)preproc;
     nasmpp.cleanup(0);
+    if (preproc_nasm->line)
+        yasm_xfree(preproc_nasm->line);
     if (preproc_nasm->file_name)
         yasm_xfree(preproc_nasm->file_name);
     yasm_xfree(preproc);
@@ -170,54 +171,37 @@ nasm_preproc_destroy(yasm_preproc *preproc)
         yasm_xfree(preproc_deps);
 }
 
-static size_t
-nasm_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size)
+static char *
+nasm_preproc_get_line(yasm_preproc *preproc)
 {
     yasm_preproc_nasm *preproc_nasm = (yasm_preproc_nasm *)preproc;
-    size_t tot = 0, n;
-    long linnum = preproc_nasm->prior_linnum += preproc_nasm->lineinc;
+    long linnum;
     int altline;
+    char *line;
 
-    if (!preproc_nasm->line) {
-        preproc_nasm->line = nasmpp.getline();
-        if (!preproc_nasm->line)
-            return 0;
-        preproc_nasm->linepos = preproc_nasm->line;
-        preproc_nasm->lineleft = strlen(preproc_nasm->line) + 1;
-        preproc_nasm->line[preproc_nasm->lineleft-1] = '\n';
-
-        altline = nasm_src_get(&linnum, &preproc_nasm->file_name);
-        if (altline) {
-            if (altline == 1 && preproc_nasm->lineinc == 1) {
-                *buf++ = '\n';
-                max_size--;
-                tot++;
-            } else {
-                preproc_nasm->lineinc =
-                    (altline != -1 || preproc_nasm->lineinc != 1);
-                n = sprintf(buf, "%%line %ld+%d %s\n", linnum,
-                            preproc_nasm->lineinc, preproc_nasm->file_name);
-                buf += n;
-                max_size -= n;
-                tot += n;
-            }
-            preproc_nasm->prior_linnum = linnum;
-        }
+    if (preproc_nasm->line) {
+        char *retval = preproc_nasm->line;
+        preproc_nasm->line = NULL;
+        return retval;
     }
 
-    n = preproc_nasm->lineleft<max_size?preproc_nasm->lineleft:max_size;
-    strncpy(buf, preproc_nasm->linepos, n);
-    tot += n;
-
-    if (n == preproc_nasm->lineleft) {
-        yasm_xfree(preproc_nasm->line);
-        preproc_nasm->line = NULL;
-    } else {
-        preproc_nasm->lineleft -= n;
-        preproc_nasm->linepos += n;
+    line = nasmpp.getline();
+    if (!line)
+        return NULL;    /* EOF */
+
+    linnum = preproc_nasm->prior_linnum += preproc_nasm->lineinc;
+    altline = nasm_src_get(&linnum, &preproc_nasm->file_name);
+    if (altline != 0) {
+        preproc_nasm->lineinc =
+            (altline != -1 || preproc_nasm->lineinc != 1);
+        preproc_nasm->line = line;
+        line = yasm_xmalloc(40+strlen(preproc_nasm->file_name));
+        sprintf(line, "%%line %ld+%d %s", linnum,
+                preproc_nasm->lineinc, preproc_nasm->file_name);
+        preproc_nasm->prior_linnum = linnum;
     }
 
-    return tot;
+    return line;
 }
 
 void
@@ -310,7 +294,7 @@ yasm_preproc_module yasm_nasm_LTX_preproc = {
     "nasm",
     nasm_preproc_create,
     nasm_preproc_destroy,
-    nasm_preproc_input,
+    nasm_preproc_get_line,
     nasm_preproc_get_included_file,
     nasm_preproc_add_include_file,
     nasm_preproc_predefine_macro,
index 869cbec4579e1f5046700d3c44c313a17bfd7e71..87ec4e078988894f4271604fc81759977d566851 100644 (file)
 #include <libyasm.h>
 
 
+#define BSIZE 512
+
 typedef struct yasm_preproc_raw {
     yasm_preproc_base preproc;   /* base structure */
 
-    int is_interactive;
     FILE *in;
     yasm_linemap *cur_lm;
     yasm_errwarns *errwarns;
@@ -41,8 +42,6 @@ typedef struct yasm_preproc_raw {
 
 yasm_preproc_module yasm_raw_LTX_preproc;
 
-int isatty(int);
-
 static yasm_preproc *
 raw_preproc_create(const char *in_filename, yasm_linemap *lm,
                    yasm_errwarns *errwarns)
@@ -62,9 +61,6 @@ raw_preproc_create(const char *in_filename, yasm_linemap *lm,
     preproc_raw->in = f;
     preproc_raw->cur_lm = lm;
     preproc_raw->errwarns = errwarns;
-    /*@-unrecog@*/
-    preproc_raw->is_interactive = f ? (isatty(fileno(f)) > 0) : 0;
-    /*@=unrecog@*/
 
     return (yasm_preproc *)preproc_raw;
 }
@@ -75,32 +71,48 @@ raw_preproc_destroy(yasm_preproc *preproc)
     yasm_xfree(preproc);
 }
 
-static size_t
-raw_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size)
+static char *
+raw_preproc_get_line(yasm_preproc *preproc)
 {
     yasm_preproc_raw *preproc_raw = (yasm_preproc_raw *)preproc;
-    int c = '*';
-    size_t n;
-
-    if (preproc_raw->is_interactive) {
-        for (n = 0; n < max_size && (c = getc(preproc_raw->in)) != EOF &&
-             c != '\n'; n++)
-            buf[n] = (char)c;
-        if (c == '\n')
-            buf[n++] = (char)c;
-        if (c == EOF && ferror(preproc_raw->in)) {
-            yasm_error_set(YASM_ERROR_IO, N_("error when reading from file"));
-            yasm_errwarn_propagate(preproc_raw->errwarns,
-                                   yasm_linemap_get_current(preproc_raw->cur_lm));
+    int bufsize = BSIZE;
+    char *buf = yasm_xmalloc((size_t)bufsize);
+    char *p;
+
+    /* Loop to ensure entire line is read (don't want to limit line length). */
+    p = buf;
+    for (;;) {
+        if (!fgets(p, bufsize-(p-buf), preproc_raw->in)) {
+            if (ferror(preproc_raw->in)) {
+                yasm_error_set(YASM_ERROR_IO,
+                               N_("error when reading from file"));
+                yasm_errwarn_propagate(preproc_raw->errwarns,
+                    yasm_linemap_get_current(preproc_raw->cur_lm));
+            }
+            break;
+        }
+        p += strlen(p);
+        if (p > buf && p[-1] == '\n')
+            break;
+        if ((p-buf) >= bufsize) {
+            /* Increase size of buffer */
+            char *oldbuf = buf;
+            bufsize *= 2;
+            buf = yasm_xrealloc(buf, (size_t)bufsize);
+            p = buf + (p-oldbuf);
         }
-    } else if (((n = fread(buf, 1, max_size, preproc_raw->in)) == 0) &&
-               ferror(preproc_raw->in)) {
-        yasm_error_set(YASM_ERROR_IO, N_("error when reading from file"));
-        yasm_errwarn_propagate(preproc_raw->errwarns,
-                               yasm_linemap_get_current(preproc_raw->cur_lm));
     }
 
-    return n;
+    if (p == buf) {
+        /* No data; must be at EOF */
+        yasm_xfree(buf);
+        return NULL;
+    }
+
+    /* Strip the line ending */
+    buf[strcspn(buf, "\r\n")] = '\0';
+
+    return buf;
 }
 
 static size_t
@@ -142,7 +154,7 @@ yasm_preproc_module yasm_raw_LTX_preproc = {
     "raw",
     raw_preproc_create,
     raw_preproc_destroy,
-    raw_preproc_input,
+    raw_preproc_get_line,
     raw_preproc_get_included_file,
     raw_preproc_add_include_file,
     raw_preproc_predefine_macro,