scan.c 4.32 KB
Newer Older
1
/* Utility functions for scan-decls and fix-header programs.
2
   Copyright (C) 1993, 1994, 1998 Free Software Foundation, Inc.
Per Bothner committed
3 4 5 6 7 8 9 10 11 12 13 14 15

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 2, 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, write to the Free Software
Richard Kenner committed
16
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
Per Bothner committed
17

Richard Kenner committed
18
#include "hconfig.h"
19 20
#include "system.h"
#include "scan.h"
Per Bothner committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

int lineno = 1;
int source_lineno = 1;
sstring source_filename;

void
make_sstring_space (str, count)
     sstring *str;
     int count;
{
  int cur_pos = str->ptr - str->base;
  int cur_size = str->limit - str->base;
  int new_size = cur_pos + count + 100;

  if (new_size <= cur_size)
    return;
  
  if (str->base == NULL)
    str->base = xmalloc (new_size);
  else
    str->base = xrealloc (str->base, new_size);
  str->ptr = str->base + cur_size;
  str->limit = str->base + new_size;
}

void
sstring_append (dst, src)
     sstring *dst;
     sstring *src;
{
  register char *d, *s;
52
  register int count = SSTRING_LENGTH(src);
Per Bothner committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  MAKE_SSTRING_SPACE(dst, count + 1);
  d = dst->ptr;
  s = src->base;
  while (--count >= 0) *d++ = *s++;
  dst->ptr = d;
  *d = 0;  
}

int
scan_ident (fp, s, c)
     register FILE *fp;
     register sstring *s;
     int c;
{
  s->ptr = s->base;
68
  if (ISALPHA(c) || c == '_')
Per Bothner committed
69 70 71 72 73
    {
      for (;;)
	{
	  SSTRING_PUT(s, c);
	  c = getc (fp);
74
	  if (c == EOF || !(ISALNUM(c) || c == '_'))
Per Bothner committed
75 76 77 78 79 80 81 82
	    break;
	}
    }
  MAKE_SSTRING_SPACE(s, 1);
  *s->ptr = 0;
  return c;
}

83 84
int
scan_string (fp, s, init)
Per Bothner committed
85 86
     register FILE *fp;
     register sstring *s;
87
     int init;
Per Bothner committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
{
  int c;
  for (;;)
    {
      c = getc (fp);
      if (c == EOF || c == '\n')
	break;
      if (c == init)
	{
	  c = getc (fp);
	  break;
	}
      if (c == '\\')
	{
	  c = getc (fp);
	  if (c == EOF)
	    break;
	  if (c == '\n')
	    continue;
	}
      SSTRING_PUT(s, c);
    }
  MAKE_SSTRING_SPACE(s, 1);
  *s->ptr = 0;
  return c;
}

Mike Stump committed
115
/* Skip horizontal white spaces (spaces, tabs, and C-style comments).  */
Per Bothner committed
116

117 118
int
skip_spaces (fp, c)
Per Bothner committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
     register FILE *fp;
     int c;
{
  for (;;)
    {
      if (c == ' ' || c == '\t')
	c = getc (fp);
      else if (c == '/')
	{
	  c = getc (fp);
	  if (c != '*')
	    {
	      ungetc (c, fp);
	      return '/';
	    }
	  c = getc (fp);
	  for (;;)
	    {
	      if (c == EOF)
		return EOF;
	      else if (c != '*')
		{
		  if (c == '\n')
		    source_lineno++, lineno++;
		  c = getc (fp);
		}
	      else if ((c = getc (fp)) == '/')
		return getc (fp);
	    }
	}
      else
	break;
    }
  return c;
}

int
read_upto (fp, str, delim)
     FILE *fp;
     sstring *str;
     int delim;
{
  int ch;
  for (;;)
    {
      ch = getc (fp);
      if (ch == EOF || ch == delim)
	break;
      SSTRING_PUT(str, ch);
    }
  MAKE_SSTRING_SPACE(str, 1);
  *str->ptr = 0;
  return ch;
}

int
get_token (fp, s)
     register FILE *fp;
     register sstring *s;
{
  int c;
  s->ptr = s->base;
 retry:
  c = ' ';
  c = skip_spaces (fp, c);
  if (c == '\n')
    {
      source_lineno++;
      lineno++;
      goto retry;
    }
  if (c == '#')
    {
      c = get_token (fp, s);
      if (c == INT_TOKEN)
	{
Per Bothner committed
195
	  source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */
Per Bothner committed
196 197 198 199 200 201 202 203
	  get_token (fp, &source_filename);
	}
      for (;;)
	{
	  c = getc (fp);
	  if (c == EOF)
	    return EOF;
	  if (c == '\n')
Per Bothner committed
204 205 206
	    {
	    source_lineno++;
	    lineno++;
Per Bothner committed
207
	    goto retry;
Per Bothner committed
208
	    }
Per Bothner committed
209 210 211 212
	}
    }
  if (c == EOF)
    return EOF;
213
  if (ISDIGIT (c))
Per Bothner committed
214 215 216 217 218
    {
      do
	{
	  SSTRING_PUT(s, c);
	  c = getc (fp);
219
	} while (c != EOF && ISDIGIT(c));
Per Bothner committed
220 221 222 223
      ungetc (c, fp);
      c = INT_TOKEN;
      goto done;
    }
224
  if (ISALPHA (c) || c == '_')
Per Bothner committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    {
      c = scan_ident (fp, s, c);
      ungetc (c, fp);
      return IDENTIFIER_TOKEN;
    }
  if (c == '\'' || c == '"')
    {
      c = scan_string (fp, s, c);
      ungetc (c, fp);
      return c == '\'' ? CHAR_TOKEN : STRING_TOKEN;
    }
  SSTRING_PUT(s, c);
 done:
  MAKE_SSTRING_SPACE(s, 1);
  *s->ptr = 0;
  return c;
}