scan.c revision 132718
1105398Stmm/* Utility functions for scan-decls and fix-header programs.
2105398Stmm   Copyright (C) 1993, 1994, 1998, 2002, 2003 Free Software Foundation, Inc.
3139749Simp
4105398StmmThis program is free software; you can redistribute it and/or modify it
5105398Stmmunder the terms of the GNU General Public License as published by the
6105398StmmFree Software Foundation; either version 2, or (at your option) any
7105398Stmmlater version.
8105398Stmm
9105398StmmThis program is distributed in the hope that it will be useful,
10105398Stmmbut WITHOUT ANY WARRANTY; without even the implied warranty of
11105398StmmMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12105398StmmGNU General Public License for more details.
13105398Stmm
14105398StmmYou should have received a copy of the GNU General Public License
15105398Stmmalong with this program; if not, write to the Free Software
16105398StmmFoundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17105398Stmm
18105398Stmm#include "bconfig.h"
19105398Stmm#include "system.h"
20105398Stmm#include "coretypes.h"
21105398Stmm#include "tm.h"
22105398Stmm#include "scan.h"
23105398Stmm
24105398Stmmint lineno = 1;
25105398Stmmint source_lineno = 1;
26105398Stmmsstring source_filename;
27105398Stmm
28105398Stmmvoid
29105398Stmmmake_sstring_space (sstring *str, int count)
30105398Stmm{
31105398Stmm  int cur_pos = str->ptr - str->base;
32105398Stmm  int cur_size = str->limit - str->base;
33105398Stmm  int new_size = cur_pos + count + 100;
34105398Stmm
35105398Stmm  if (new_size <= cur_size)
36105398Stmm    return;
37105398Stmm
38105398Stmm  str->base = xrealloc (str->base, new_size);
39105398Stmm  str->ptr = str->base + cur_size;
40105398Stmm  str->limit = str->base + new_size;
41105398Stmm}
42105398Stmm
43105398Stmmvoid
44105398Stmmsstring_append (sstring *dst, sstring *src)
45105398Stmm{
46105398Stmm  char *d, *s;
47146400Smarius  int count = SSTRING_LENGTH (src);
48105398Stmm
49146400Smarius  MAKE_SSTRING_SPACE (dst, count + 1);
50105398Stmm  d = dst->ptr;
51105398Stmm  s = src->base;
52105398Stmm  while (--count >= 0) *d++ = *s++;
53105398Stmm  dst->ptr = d;
54105398Stmm  *d = 0;
55105398Stmm}
56105398Stmm
57105398Stmmint
58129587Smariusscan_ident (FILE *fp, sstring *s, int c)
59105398Stmm{
60105398Stmm  s->ptr = s->base;
61105398Stmm  if (ISIDST (c))
62105398Stmm    {
63105398Stmm      for (;;)
64105398Stmm	{
65105398Stmm	  SSTRING_PUT (s, c);
66105398Stmm	  c = getc (fp);
67105398Stmm	  if (c == EOF || ! ISIDNUM (c))
68105398Stmm	    break;
69116210Stmm	}
70116210Stmm    }
71105398Stmm  MAKE_SSTRING_SPACE (s, 1);
72129587Smarius  *s->ptr = 0;
73129587Smarius  return c;
74129587Smarius}
75129587Smarius
76129587Smariusint
77105398Stmmscan_string (FILE *fp, sstring *s, int init)
78{
79  int c;
80
81  for (;;)
82    {
83      c = getc (fp);
84      if (c == EOF || c == '\n')
85	break;
86      if (c == init)
87	{
88	  c = getc (fp);
89	  break;
90	}
91      if (c == '\\')
92	{
93	  c = getc (fp);
94	  if (c == EOF)
95	    break;
96	  if (c == '\n')
97	    continue;
98	}
99      SSTRING_PUT (s, c);
100    }
101  MAKE_SSTRING_SPACE (s, 1);
102  *s->ptr = 0;
103  return c;
104}
105
106/* Skip horizontal white spaces (spaces, tabs, and C-style comments).  */
107
108int
109skip_spaces (FILE *fp, int c)
110{
111  for (;;)
112    {
113      if (c == ' ' || c == '\t')
114	c = getc (fp);
115      else if (c == '/')
116	{
117	  c = getc (fp);
118	  if (c != '*')
119	    {
120	      ungetc (c, fp);
121	      return '/';
122	    }
123	  c = getc (fp);
124	  for (;;)
125	    {
126	      if (c == EOF)
127		return EOF;
128	      else if (c != '*')
129		{
130		  if (c == '\n')
131		    source_lineno++, lineno++;
132		  c = getc (fp);
133		}
134	      else if ((c = getc (fp)) == '/')
135		return getc (fp);
136	    }
137	}
138      else
139	break;
140    }
141  return c;
142}
143
144int
145read_upto (FILE *fp, sstring *str, int delim)
146{
147  int ch;
148
149  for (;;)
150    {
151      ch = getc (fp);
152      if (ch == EOF || ch == delim)
153	break;
154      SSTRING_PUT (str, ch);
155    }
156  MAKE_SSTRING_SPACE (str, 1);
157  *str->ptr = 0;
158  return ch;
159}
160
161int
162get_token (FILE *fp, sstring *s)
163{
164  int c;
165
166  s->ptr = s->base;
167 retry:
168  c = ' ';
169  c = skip_spaces (fp, c);
170  if (c == '\n')
171    {
172      source_lineno++;
173      lineno++;
174      goto retry;
175    }
176  if (c == '#')
177    {
178      c = get_token (fp, s);
179      if (c == INT_TOKEN)
180	{
181	  source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */
182	  get_token (fp, &source_filename);
183	}
184      for (;;)
185	{
186	  c = getc (fp);
187	  if (c == EOF)
188	    return EOF;
189	  if (c == '\n')
190	    {
191	    source_lineno++;
192	    lineno++;
193	    goto retry;
194	    }
195	}
196    }
197  if (c == EOF)
198    return EOF;
199  if (ISDIGIT (c))
200    {
201      do
202	{
203	  SSTRING_PUT (s, c);
204	  c = getc (fp);
205	} while (c != EOF && ISDIGIT (c));
206      ungetc (c, fp);
207      c = INT_TOKEN;
208      goto done;
209    }
210  if (ISIDST (c))
211    {
212      c = scan_ident (fp, s, c);
213      ungetc (c, fp);
214      return IDENTIFIER_TOKEN;
215    }
216  if (c == '\'' || c == '"')
217    {
218      c = scan_string (fp, s, c);
219      ungetc (c, fp);
220      return c == '\'' ? CHAR_TOKEN : STRING_TOKEN;
221    }
222  SSTRING_PUT (s, c);
223 done:
224  MAKE_SSTRING_SPACE (s, 1);
225  *s->ptr = 0;
226  return c;
227}
228
229unsigned int
230hashstr (const char *str, unsigned int len)
231{
232  unsigned int n = len;
233  unsigned int r = 0;
234  const unsigned char *s = (const unsigned char *) str;
235
236  do
237    r = r * 67 + (*s++ - 113);
238  while (--n);
239  return r + len;
240}
241