* as/z80/z80mch.c: fixed bug #1704376: missing as-z80 errors
[fw/sdcc] / as / asxxsrc / aslex.c
index ecf37f908755ccc19cb4b64e047b6a04bfe53e3d..d63920948f17af3148bca66085b4a63a24a9d0dd 100644 (file)
@@ -1,13 +1,22 @@
-/* aslex.c */
+/* aslex.c
+
+   Copyright (C) 1989-1995 Alan R. Baldwin
+   721 Berkeley St., Kent, Ohio 44240
+
+This program is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 3, or (at your option) any
+later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 /*
- * (C) Copyright 1989-1995
- * All Rights Reserved
- *
- * Alan R. Baldwin
- * 721 Berkeley St.
- * Kent, Ohio  44240
- *
  * 28-Oct-97 JLH bug in getst(): sign extend on ~(SPACE|ILL)
  *           causes infinite loop
  */
@@ -19,6 +28,7 @@
 #include <stdio.h>
 #include <setjmp.h>
 #include <string.h>
+#include "dbuf_string.h"
 #include "asm.h"
 
 /*)Module       aslex.c
@@ -356,77 +366,6 @@ getmap(d)
         return (c);
 }
 
-/*)Function     char *  readlin()
- *
- *              char *  str             poinetr to beginning of the buffer
- *              size_t  n               size of the buffer
- *              FILE *  infp            input file pointer
- *
- *      The function readlin() reads a line from a file to the buffer s.
- *      The buffer length is n. If the line is truncterd to n -1 caharecters
- *      if it is longer the n - 1 characters.
- *      Trailing LF or CR/LF are removed from str, if present.
- *
- *      local variables:
- *              int c                   character read from the file
- *              char *s                 buffer pointer
- *              satic char eof_f        EOF flag
- *
- *      global variables:
- *              none
- *
- *      called functions:
- *              int     getc            c-library
- */
-
-char *
-readlin(char *str, size_t n, FILE *infp)
-{
-  int c;
-  char *s;
-  static char eof_f = 0;
-
-  if (eof_f)
-    {
-      eof_f = 0;
-      return NULL;
-    }
-
-  if (n > 0)
-    {
-    s = str;
-    if (n > 1)
-      {
-        while (--n && (c = getc(infp)) != '\n' && c != EOF)
-          *s++ = c;
-      }
-    else
-      c = ' ';  /* initialize it to something for the caharacter eating step */
-
-    /* chop CR */
-    if (s > str && *(s - 1) == '\r')
-      --s;
-
-    /* terminate the line */
-    *s = '\0';
-
-    /* eat characters until the end of line */
-    while (c != '\n' && c != EOF)
-      c = getc(infp);
-
-    /* if the EOF is not at the beginning of the line, return the line;
-       return NULL at the next call of redlin */
-    if (c == EOF)
-      {
-        if (s == str)
-          return NULL;
-        eof_f = 1;
-      }
-    }
-
-  return str;
-}
-
 /*)Function     int     as_getline()
  *
  *      The function as_getline() reads a line of assembler-source text
@@ -444,7 +383,7 @@ readlin(char *str, size_t n, FILE *infp)
  *              int     i               string length
  *
  *      global variables:
- *              char    ib[]            string buffer containing
+ *              const char ib[]         string buffer containing
  *                                      assembler-source text line
  *              char    ifp[]           array of file handles for
  *                                      include files
@@ -461,6 +400,10 @@ readlin(char *str, size_t n, FILE *infp)
  *              int     inpfil          maximum input file index
  *
  *      called functions:
+ *              int     dbuf_init()
+ *              int     dbuf_set_length()
+ *              int     dbuf_getline()
+ *              const char * dbuf_c_str()
  *              int     fclose()        c-library
  *              char *  fgets()         c-library
  *              int     strlen()        c-library
@@ -474,29 +417,64 @@ readlin(char *str, size_t n, FILE *infp)
  */
 
 int
-as_getline()
+as_getline(void)
 {
-loop:   if (incfil >= 0) {
-                if (readlin(ib, sizeof ib, ifp[incfil]) == NULL) {
-                        fclose(ifp[incfil]);
-                        ifp[incfil--] = NULL;
-                        lop = NLPP;
-                        goto loop;
-                } else {
-                        ++incline[incfil];
-                }
-        } else {
-                if (readlin(ib, sizeof ib, sfp[cfile]) == NULL) {
-                        if (++cfile <= inpfil) {
-                                srcline[cfile] = 0;
-                                goto loop;
-                        }
-                        return (0);
-                } else {
-                        ++srcline[cfile];
-                }
+  static struct dbuf_s dbuf;
+  static char dbufInitialized = 0;
+  size_t len;
+
+  if (!dbufInitialized)
+    {
+      dbuf_init (&dbuf, 1024);
+      dbufInitialized = 1;
+    }
+  else
+    dbuf_set_length (&dbuf, 0);
+
+loop:
+  if (incfil >= 0)
+    {
+      if ((len = dbuf_getline (&dbuf, ifp[incfil])) == 0)
+        {
+          fclose (ifp[incfil]);
+          ifp[incfil--] = NULL;
+          lop = NLPP;
+          goto loop;
+        }
+      else
+        {
+          ++incline[incfil];
+        }
+    }
+  else
+    {
+      if ((len = dbuf_getline (&dbuf, sfp[cfile])) == 0)
+        {
+          if (++cfile <= inpfil)
+            {
+              srcline[cfile] = 0;
+              goto loop;
+            }
+          return 0;
+        }
+      else
+        {
+          ++srcline[cfile];
         }
-        return (1);
+    }
+  ib = dbuf_c_str (&dbuf);
+
+  /* remove the trailing NL */
+  if (len > 0 && '\n' == ib[len - 1])
+    {
+      --len;
+      if (len > 0 && '\r' == ib[len - 1])
+        --len;
+      dbuf_set_length (&dbuf, len);
+      ib = dbuf_c_str (&dbuf);
+    }
+
+  return 1;
 }
 
 /*)Function     int     more()