1996-09-18 05:35:50 +00:00
|
|
|
/* Utility functions for scan-decls and fix-header programs.
|
2002-02-01 18:16:02 +00:00
|
|
|
Copyright (C) 1993, 1994, 1998, 2002 Free Software Foundation, Inc.
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
|
|
|
|
|
|
#include "hconfig.h"
|
1999-08-26 09:30:50 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "scan.h"
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
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;
|
2003-07-11 03:40:53 +00:00
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
str->base = xrealloc (str->base, new_size);
|
1996-09-18 05:35:50 +00:00
|
|
|
str->ptr = str->base + cur_size;
|
|
|
|
str->limit = str->base + new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sstring_append (dst, src)
|
|
|
|
sstring *dst;
|
|
|
|
sstring *src;
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
char *d, *s;
|
|
|
|
int count = SSTRING_LENGTH (src);
|
|
|
|
|
|
|
|
MAKE_SSTRING_SPACE (dst, count + 1);
|
1996-09-18 05:35:50 +00:00
|
|
|
d = dst->ptr;
|
|
|
|
s = src->base;
|
|
|
|
while (--count >= 0) *d++ = *s++;
|
|
|
|
dst->ptr = d;
|
2003-07-11 03:40:53 +00:00
|
|
|
*d = 0;
|
1996-09-18 05:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
scan_ident (fp, s, c)
|
2002-02-01 18:16:02 +00:00
|
|
|
FILE *fp;
|
|
|
|
sstring *s;
|
1996-09-18 05:35:50 +00:00
|
|
|
int c;
|
|
|
|
{
|
|
|
|
s->ptr = s->base;
|
2002-02-01 18:16:02 +00:00
|
|
|
if (ISIDST (c))
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
SSTRING_PUT (s, c);
|
1996-09-18 05:35:50 +00:00
|
|
|
c = getc (fp);
|
2002-02-01 18:16:02 +00:00
|
|
|
if (c == EOF || ! ISIDNUM (c))
|
1996-09-18 05:35:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
MAKE_SSTRING_SPACE (s, 1);
|
1996-09-18 05:35:50 +00:00
|
|
|
*s->ptr = 0;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
scan_string (fp, s, init)
|
2002-02-01 18:16:02 +00:00
|
|
|
FILE *fp;
|
|
|
|
sstring *s;
|
1999-08-26 09:30:50 +00:00
|
|
|
int init;
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
int c;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
1996-09-18 05:35:50 +00:00
|
|
|
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;
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
SSTRING_PUT (s, c);
|
1996-09-18 05:35:50 +00:00
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
MAKE_SSTRING_SPACE (s, 1);
|
1996-09-18 05:35:50 +00:00
|
|
|
*s->ptr = 0;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
1999-08-26 09:30:50 +00:00
|
|
|
/* Skip horizontal white spaces (spaces, tabs, and C-style comments). */
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
skip_spaces (fp, c)
|
2002-02-01 18:16:02 +00:00
|
|
|
FILE *fp;
|
1996-09-18 05:35:50 +00:00
|
|
|
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;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
1996-09-18 05:35:50 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
ch = getc (fp);
|
|
|
|
if (ch == EOF || ch == delim)
|
|
|
|
break;
|
2002-02-01 18:16:02 +00:00
|
|
|
SSTRING_PUT (str, ch);
|
1996-09-18 05:35:50 +00:00
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
MAKE_SSTRING_SPACE (str, 1);
|
1996-09-18 05:35:50 +00:00
|
|
|
*str->ptr = 0;
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_token (fp, s)
|
2002-02-01 18:16:02 +00:00
|
|
|
FILE *fp;
|
|
|
|
sstring *s;
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
int c;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
1996-09-18 05:35:50 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */
|
|
|
|
get_token (fp, &source_filename);
|
|
|
|
}
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
c = getc (fp);
|
|
|
|
if (c == EOF)
|
|
|
|
return EOF;
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
source_lineno++;
|
|
|
|
lineno++;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c == EOF)
|
|
|
|
return EOF;
|
1999-08-26 09:30:50 +00:00
|
|
|
if (ISDIGIT (c))
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
SSTRING_PUT (s, c);
|
1996-09-18 05:35:50 +00:00
|
|
|
c = getc (fp);
|
2002-02-01 18:16:02 +00:00
|
|
|
} while (c != EOF && ISDIGIT (c));
|
1996-09-18 05:35:50 +00:00
|
|
|
ungetc (c, fp);
|
|
|
|
c = INT_TOKEN;
|
|
|
|
goto done;
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
if (ISIDST (c))
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
SSTRING_PUT (s, c);
|
1996-09-18 05:35:50 +00:00
|
|
|
done:
|
2002-02-01 18:16:02 +00:00
|
|
|
MAKE_SSTRING_SPACE (s, 1);
|
1996-09-18 05:35:50 +00:00
|
|
|
*s->ptr = 0;
|
|
|
|
return c;
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
|
|
|
|
unsigned int
|
|
|
|
hashstr (str, len)
|
|
|
|
const char *str;
|
|
|
|
unsigned int len;
|
|
|
|
{
|
|
|
|
unsigned int n = len;
|
|
|
|
unsigned int r = 0;
|
|
|
|
const unsigned char *s = (const unsigned char *) str;
|
|
|
|
|
|
|
|
do
|
|
|
|
r = r * 67 + (*s++ - 113);
|
|
|
|
while (--n);
|
|
|
|
return r + len;
|
|
|
|
}
|