mirror of
https://git.openldap.org/openldap/openldap.git
synced 2025-12-24 16:49:39 -05:00
The world is STDC.
This commit is contained in:
parent
92b28cf397
commit
af40e0cea7
24 changed files with 0 additions and 3917 deletions
|
|
@ -1,123 +0,0 @@
|
|||
# @(#) Makefile 1.6 93/06/18 22:29:40
|
||||
|
||||
## BEGIN CONFIGURATION STUFF
|
||||
|
||||
# In the unlikely case that your compiler has no hooks for alternate
|
||||
# compiler passes, use a "cc cflags -E file.c | unproto >file.i"
|
||||
# pipeline, then "cc cflags -c file.i" to compile the resulting
|
||||
# intermediate file.
|
||||
#
|
||||
# Otherwise, the "/lib/cpp | unproto" pipeline can be packaged as an
|
||||
# executable shell script (see the provided "cpp.sh" script) that should
|
||||
# be installed as "/whatever/cpp". This script should then be specified
|
||||
# to the C compiler as a non-default preprocessor.
|
||||
#
|
||||
# PROG = unproto
|
||||
# PIPE =
|
||||
|
||||
# The overhead and problems of shell script interpretation can be
|
||||
# eliminated by having the unprototyper program itself open the pipe to
|
||||
# the preprocessor. In that case, define the PIPE_THROUGH_CPP macro as
|
||||
# the path name of the default C preprocessor (usually "/lib/cpp"),
|
||||
# install the unprototyper as "/whatever/cpp" and specify that to the C
|
||||
# compiler as a non-default preprocessor.
|
||||
#
|
||||
PROG = cpp
|
||||
PIPE = -DPIPE_THROUGH_CPP=\"/lib/cpp\"
|
||||
|
||||
# Some compilers complain about some #directives. The following is only a
|
||||
# partial solution, because the directives are still seen by /lib/cpp.
|
||||
# Be careful with filtering out #pragma, because some pre-ANSI compilers
|
||||
# (SunOS) rely on its use.
|
||||
#
|
||||
# SKIP = -DIGNORE_DIRECTIVES=\"pragma\",\"foo\",\"bar\"
|
||||
#
|
||||
SKIP =
|
||||
|
||||
# The bell character code depends on the character set. With ASCII, it is
|
||||
# 7. Specify a string constant with exactly three octal digits. If you
|
||||
# change this definition, you will have to update the example.out file.
|
||||
#
|
||||
BELL = -DBELL=\"007\"
|
||||
|
||||
# Some C compilers have problems with "void". The nature of the problems
|
||||
# depends on the age of the compiler.
|
||||
#
|
||||
# If your compiler does not understand "void" at all, compile with
|
||||
# -DMAP_VOID. The unprototyper will replace "void *" by "char *", a
|
||||
# (void) argument list by an empty one, and will replace all other
|
||||
# instances of "void" by "int".
|
||||
#
|
||||
# If your compiler has problems with "void *" only, compile with
|
||||
# -DMAP_VOID_STAR. The unprototyper will replace "void *" by "char *",
|
||||
# and will replace a (void) argument list by an empty one. All other
|
||||
# instances of "void" will be left alone.
|
||||
#
|
||||
# If neither of these are defined, (void) argument lists will be replaced
|
||||
# by empty ones.
|
||||
#
|
||||
# MAP = -DMAP_VOID_STAR
|
||||
|
||||
# Now that we have brought up the subject of antique C compilers, here's
|
||||
# a couple of aliases that may be useful, too.
|
||||
#
|
||||
# ALIAS = -Dstrchr=index
|
||||
|
||||
# If you need support for functions that implement ANSI-style variable
|
||||
# length argument lists, edit the stdarg.h file provided with this
|
||||
# package so that it contains the proper definitions for your machine.
|
||||
|
||||
## END CONFIGURATION STUFF
|
||||
|
||||
SHELL = /bin/sh
|
||||
|
||||
CFILES = unproto.c tok_io.c tok_class.c tok_pool.c vstring.c symbol.c error.c \
|
||||
hash.c strsave.c
|
||||
HFILES = error.h token.h vstring.h symbol.h
|
||||
SCRIPTS = cpp.sh acc.sh
|
||||
SAMPLES = stdarg.h stddef.h stdlib.h varargs.c example.c example.out
|
||||
SOURCES = README $(CFILES) $(HFILES) Makefile $(SCRIPTS) $(SAMPLES)
|
||||
FILES = $(SOURCES) unproto.1
|
||||
OBJECTS = tok_io.o tok_class.o tok_pool.o unproto.o vstring.o symbol.o error.o \
|
||||
hash.o strsave.o
|
||||
|
||||
CFLAGS = -O $(PIPE) $(SKIP) $(BELL) $(MAP) $(ALIAS)
|
||||
#CFLAGS = -O $(PIPE) $(SKIP) $(BELL) $(MAP) $(ALIAS) -p -Dstatic=
|
||||
#CFLAGS = -g $(PIPE) $(SKIP) $(BELL) $(MAP) $(ALIAS) -DDEBUG
|
||||
|
||||
$(PROG): $(OBJECTS)
|
||||
$(CC) $(CFLAGS) -o $@ $(OBJECTS) $(MALLOC)
|
||||
|
||||
# For linting, enable all bells and whistles.
|
||||
|
||||
lint:
|
||||
lint -DPIPE_THROUGH_CPP=\"foo\" -DIGNORE_DIRECTIVES=\"foo\",\"bar\" \
|
||||
$(BELL) -DMAP_VOID $(ALIAS) $(CFILES)
|
||||
|
||||
# Testing requires that the program is compiled with -DDEBUG.
|
||||
|
||||
test: $(PROG) cpp example.c example.out
|
||||
./cpp example.c >example.tmp
|
||||
@echo the following diff command should produce no output
|
||||
diff -b example.out example.tmp
|
||||
rm -f example.tmp
|
||||
|
||||
shar: $(FILES)
|
||||
@shar $(FILES)
|
||||
|
||||
archive:
|
||||
$(ARCHIVE) $(SOURCES)
|
||||
|
||||
clean:
|
||||
rm -f *.o core cpp unproto mon.out varargs.o varargs example.tmp
|
||||
|
||||
error.o : error.c token.h error.h Makefile
|
||||
hash.o : hash.c Makefile
|
||||
strsave.o : strsave.c error.h Makefile
|
||||
symbol.o : symbol.c error.h token.h symbol.h Makefile
|
||||
tok_class.o : tok_class.c error.h vstring.h token.h symbol.h Makefile
|
||||
tok_io.o : tok_io.c token.h vstring.h error.h Makefile
|
||||
tok_pool.o : tok_pool.c token.h vstring.h error.h Makefile
|
||||
unproto.o : unproto.c vstring.h stdarg.h token.h error.h symbol.h Makefile
|
||||
varargs.o : varargs.c stdarg.h Makefile
|
||||
vstring.o : vstring.c vstring.h Makefile
|
||||
|
|
@ -1,160 +0,0 @@
|
|||
@(#) README 1.6 93/06/18 22:29:34
|
||||
|
||||
unproto - Compile ANSI C with traditional UNIX C compiler
|
||||
|
||||
Description:
|
||||
------------
|
||||
|
||||
This is a filter that sits in between the UNIX C preprocessor and the
|
||||
next UNIX C compiler stage, on the fly transforming ANSI C syntax to
|
||||
old C syntax. Line number information is preserved so that compiler
|
||||
diagnostics still make sense. It runs at roughly the same speed as
|
||||
/lib/cpp, so it has negligible impact on compilation time.
|
||||
|
||||
Typically, the program is invoked by the native UNIX C compiler as an
|
||||
alternate preprocessor. The unprototyper in turn invokes the native C
|
||||
preprocessor and massages its output. Similar tricks can be used with
|
||||
the lint(1) command. Details are given below.
|
||||
|
||||
The filter rewrites ANSI-style function headings, function pointer
|
||||
types and type casts, function prototypes, and combinations thereof.
|
||||
Unlike some other unprototypers, this one is fully recursive and does
|
||||
not depend on source file layout (see the example.c file).
|
||||
|
||||
Besides the rewriting of argument lists, the program does the following
|
||||
transformations: string concatenation, conversion of \a and \x escape
|
||||
sequences to their octal equivalents, translation of the __TIME__ and
|
||||
__DATE__ macros, optional mapping of `void *' to `char *', and optional
|
||||
mapping of plain `void' to `int'.
|
||||
|
||||
The unprototyper provides hooks for compilers that require special
|
||||
tricks for variadic functions (fortunately, many don't). <stdarg.h>
|
||||
support is provided for sparc, mips, mc68k, 80x86, vax, and others.
|
||||
|
||||
The program has been tested with SunOS 4.1.1 (sparc), Ultrix 4.0 and
|
||||
4.2 (mips), and Microport System V Release 2 (80286). It should work
|
||||
with almost every PCC-based UNIX C compiler.
|
||||
|
||||
Restrictions:
|
||||
-------------
|
||||
|
||||
A description of restrictions and workarounds can be found in the
|
||||
unproto.1 manual page.
|
||||
|
||||
Problems fixed with this release:
|
||||
---------------------------------
|
||||
|
||||
Prototypes and definitions of functions returning pointer to function
|
||||
were not rewritten to old style.
|
||||
|
||||
Operation:
|
||||
----------
|
||||
|
||||
This package implements a non-default C preprocessor (the output from
|
||||
the default C preprocessor being piped through the unprototyper). How
|
||||
one tells the C compiler to use a non-default preprocessor program is
|
||||
somewhat compiler-dependent:
|
||||
|
||||
SunOS 4.x: cc -Qpath directory_with_alternate_cpp ...
|
||||
|
||||
Ultrix 4.x: cc -tp -hdirectory_with_alternate_cpp -B ...
|
||||
|
||||
System V.2: cc -Bdirectory_with_alternate_cpp/ -tp ...
|
||||
|
||||
Examples of these, and others, can be found in the acc.sh shell script
|
||||
that emulates an ANSI C compiler. Your C compiler manual page should
|
||||
provide the necessary information.
|
||||
|
||||
A more portable, but less efficient, approach relies on the observation
|
||||
that almost every UNIX C compiler supports the -E (write preprocessor
|
||||
output to stdout) and -P options (preprocess file.c into file.i). Just
|
||||
add the following lines to your Makefiles:
|
||||
|
||||
.c.o:
|
||||
$(CC) $(CFLAGS) -E $*.c | unproto >$*.i # simulate -P option
|
||||
$(CC) $(CFLAGS) -c $*.i
|
||||
rm -f $*.i
|
||||
|
||||
On some systems the lint(1) command is just a shell script, and writing
|
||||
a version that uses the unprototyper should not be too hard. With SunOS
|
||||
4.x, /usr/bin/lint is not a shell script, but it does accept the same
|
||||
syntax as the cc(1) command for the specification of a non-default
|
||||
compiler pass.
|
||||
|
||||
You may have to do some research on the lint command provided with your
|
||||
own machine.
|
||||
|
||||
Configuration:
|
||||
--------------
|
||||
|
||||
Check the contents of the `stdarg.h' file provided with this package.
|
||||
This file serves a dual purpose: (1) on systems that do not provide a
|
||||
stdarg.h file, it should be included by C source files that implements
|
||||
ANSI-style variadic functions; (2) it is also used to configure the
|
||||
unprototyper so that it emits the proper magic when it sees `...'.
|
||||
|
||||
The `stdarg.h' file has support for sparc, mips, and for compilers that
|
||||
pass arguments via the stack (typical for 80*86, mc68k and vax). It
|
||||
gives general hints for other compilers.
|
||||
|
||||
The other sample header files (stddef.h and stdlib.h) are not required
|
||||
to build the unprototyper.
|
||||
|
||||
The `varargs.c' file provided with this package can be used to verify
|
||||
that the `stdarg.h' file has been set up correctly.
|
||||
|
||||
If your C compiler has no hooks for an alternate preprocessor (the
|
||||
unprototyper will be used as: `cc cflags -E file.c | unproto >file.i'),
|
||||
build the `unproto' executable without the `PIPE_THROUGH_CPP' feature.
|
||||
Details are given in the Makefile.
|
||||
|
||||
Otherwise, the `cpp.sh' shell script can be used to set up the pipe
|
||||
between the native C preprocessor and the unprototyper command. The
|
||||
script assumes that the unprototyper binary is called `unproto', and
|
||||
that it was compiled without the `PIPE_THROUGH_CPP' feature. See the
|
||||
Makefile and the `cpp.sh' script for details and for a description of
|
||||
possible problems with this approach.
|
||||
|
||||
The overhead and problems of shell-script interpretation can be avoided
|
||||
by letting the unprototyper itself pipe its standard input through the
|
||||
C preprocessor. For this mode of operation, the unprototyper binary
|
||||
should be called `cpp', and the `unproto.c' source file should be
|
||||
compiled with the `PIPE_THROUGH_CPP' macro defined as the absolute
|
||||
pathname of the native C preprocessor (usually `/lib/cpp'). See the
|
||||
Makefile for details.
|
||||
|
||||
Installation:
|
||||
-------------
|
||||
|
||||
Install the `unproto.1' manual page in a suitable place. If your system
|
||||
does not provide a `stdarg.h' file, find a suitable place for the one
|
||||
provided with the unprototyper and install it there. The same goes for
|
||||
the sample stddef.h and stdlib.h files; make sure that the definitions
|
||||
in there apply to your environment. Most or all of the latter files are
|
||||
already part of Ultrix 4.x and SunOS 4.1.1.
|
||||
|
||||
The ANSI float.h and limits.h files can be generated with the config
|
||||
program by Steve Pemberton (comp.sources.misc volume 10, issue 62,
|
||||
available from ftp.uu.net as comp.sources.misc/volume10/config42.Z).
|
||||
|
||||
If you run the unprototyper with "cc -E" just install the `unproto'
|
||||
binary; the `cpp' and `acc' shell scripts will not be needed.
|
||||
|
||||
If you use the `cpp' shell script to pipe the preprocessor output
|
||||
through the unprototyper program, install the `unproto' binary in a
|
||||
place where the `cpp' shell script can find it, and install the `cpp'
|
||||
shell script in a suitable place. Edit the `acc' shell script and
|
||||
install it in a suitable place. From now on, type `acc' instead of
|
||||
`cc'.
|
||||
|
||||
If the unprototyper itself opens the pipe to the C preprocessor (i.e.
|
||||
the unprototyper was built with the `PIPE_THROUGH_CPP' macro defined),
|
||||
install the `cpp' unprototyper binary in a suitable place. Edit the
|
||||
`acc' shell script and install it in a suitable place. From now on,
|
||||
type `acc' instead of `cc'.
|
||||
|
||||
Wietse Venema
|
||||
wietse@wzv.win.tue.nl
|
||||
Mathematics and Computing Science
|
||||
Eindhoven University of Technology
|
||||
The Netherlands
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
# @(#) acc.sh 1.1 93/06/18 22:29:42
|
||||
#
|
||||
# Script to emulate most of an ANSI C compiler with a traditional UNIX
|
||||
# C compiler.
|
||||
|
||||
# INCDIR should be the directory with auxiliary include files from the
|
||||
# unproto source distribution (stdarg.h, stdlib.h, stddef.h, and other
|
||||
# stuff that is missing from your compilation environment). With Ultrix
|
||||
# 4.[0-2] you need unproto's stdarg.h even though the system provides
|
||||
# one.
|
||||
#
|
||||
INCDIR=.
|
||||
|
||||
# CPPDIR should be the directory with the unprototypeing cpp filter
|
||||
# (preferably the version with the PIPE_THROUGH_CPP feature).
|
||||
#
|
||||
CPPDIR=.
|
||||
|
||||
# DEFINES: you will want to define volatile and const, and maybe even
|
||||
# __STDC__.
|
||||
#
|
||||
DEFINES="-Dvolatile= -Dconst= -D__STDC__"
|
||||
|
||||
# Possible problem: INCDIR should be listed after the user-specified -I
|
||||
# command-line options, not before them as we do here. This is a problem
|
||||
# only if you attempt to redefine system libraries.
|
||||
#
|
||||
# Choose one of the commands below that is appropriate for your system.
|
||||
#
|
||||
exec cc -Qpath ${CPPDIR} -I${INCDIR} ${DEFINES} "$@" # SunOS 4.x
|
||||
exec cc -tp -h${CPPDIR} -B -I${INCDIR} ${DEFINES} "$@" # Ultrix 4.2
|
||||
exec cc -Yp,${CPPDIR} -I${INCDIR} ${DEFINES} "$@" # M88 SysV.3
|
||||
exec cc -B${CPPDIR}/ -tp -I${INCDIR} ${DEFINES} "$@" # System V.2
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
# @(#) cpp.sh 1.3 92/01/15 21:53:22
|
||||
|
||||
# Unprototypeing preprocessor for pre-ANSI C compilers. On some systems,
|
||||
# this script can be as simple as:
|
||||
#
|
||||
# /lib/cpp "$@" | unproto
|
||||
#
|
||||
# However, some cc(1) drivers specify output file names on the
|
||||
# preprocessor command line, so this shell script must be prepared to
|
||||
# intercept them. Depending on the driver program, the cpp options may
|
||||
# even go before or after the file name argument(s). The script below
|
||||
# tries to tackle all these cases.
|
||||
#
|
||||
# You may want to add -Ipath_to_stdarg.h_file, -Dvoid=, -Dvolatile=,
|
||||
# and even -D__STDC__.
|
||||
|
||||
cpp_args=""
|
||||
|
||||
while :
|
||||
do
|
||||
case $1 in
|
||||
"") break;;
|
||||
-*) cpp_args="$cpp_args $1";;
|
||||
*) cpp_args="$cpp_args $1"
|
||||
case $2 in
|
||||
""|-*) ;;
|
||||
*) exec 1> $2 || exit 1; shift;;
|
||||
esac;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
/lib/cpp $cpp_args | unproto
|
||||
|
|
@ -1,97 +0,0 @@
|
|||
/*++
|
||||
/* NAME
|
||||
/* error 3
|
||||
/* SUMMARY
|
||||
/* diagnostics
|
||||
/* PACKAGE
|
||||
/* unproto
|
||||
/* SYNOPSIS
|
||||
/* #include "error.h"
|
||||
/*
|
||||
/* int errcount;
|
||||
/*
|
||||
/* void error(text)
|
||||
/* char *text;
|
||||
/*
|
||||
/* void error_where(path, line, text)
|
||||
/* char *path;
|
||||
/* int line;
|
||||
/* char *text;
|
||||
/*
|
||||
/* void fatal(text)
|
||||
/* char *text;
|
||||
/* DESCRIPTION
|
||||
/* The routines in this file print a diagnostic (text). Some also
|
||||
/* terminate the program. Upon each error*() call, the errcount variable
|
||||
/* is incremented.
|
||||
/*
|
||||
/* error() provides a default context, i.e. the source-file
|
||||
/* coordinate of the last read token.
|
||||
/*
|
||||
/* error_where() allows the caller to explicitly specify context: path
|
||||
/* is a source-file name, and line is a line number.
|
||||
/*
|
||||
/* fatal() is like error() but terminates the program with a non-zero
|
||||
/* exit status.
|
||||
/*
|
||||
/* context is ignored if the line number is zero or if the path
|
||||
/* is an empty string.
|
||||
/* AUTHOR(S)
|
||||
/* Wietse Venema
|
||||
/* Eindhoven University of Technology
|
||||
/* Department of Mathematics and Computer Science
|
||||
/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
|
||||
/* LAST MODIFICATION
|
||||
/* 92/01/15 21:53:10
|
||||
/* VERSION/RELEASE
|
||||
/* 1.2
|
||||
/*--*/
|
||||
|
||||
static char error_sccsid[] = "@(#) error.c 1.2 92/01/15 21:53:10";
|
||||
|
||||
/* C library */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
extern void exit();
|
||||
|
||||
/* Application-specific stuff */
|
||||
|
||||
#include "token.h"
|
||||
#include "error.h"
|
||||
|
||||
int errcount = 0; /* error counter */
|
||||
|
||||
/* error - report problem (implicit context) */
|
||||
|
||||
void error(text)
|
||||
char *text;
|
||||
{
|
||||
error_where(in_path, in_line, text);
|
||||
}
|
||||
|
||||
/* error_where - report problem (explicit context) */
|
||||
|
||||
void error_where(path, line, text)
|
||||
char *path;
|
||||
int line;
|
||||
char *text;
|
||||
{
|
||||
errcount++;
|
||||
|
||||
/* Suppress context info if there is none. */
|
||||
|
||||
if (line && path[0])
|
||||
fprintf(stderr, "%s, line %d: ", path, line);
|
||||
|
||||
fprintf(stderr, "%s\n", text);
|
||||
}
|
||||
|
||||
/* fatal - report problem and terminate unsuccessfully */
|
||||
|
||||
void fatal(text)
|
||||
char *text;
|
||||
{
|
||||
error(text);
|
||||
exit(1);
|
||||
}
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
/* @(#) error.h 1.2 92/01/15 21:53:14 */
|
||||
|
||||
extern int errcount; /* error counter */
|
||||
extern void error(); /* default context */
|
||||
extern void error_where(); /* user-specified context */
|
||||
extern void fatal(); /* fatal error */
|
||||
|
|
@ -1,222 +0,0 @@
|
|||
/*
|
||||
* @(#) example.c 1.5 93/06/18 22:29:46
|
||||
*
|
||||
* Examples of things that can be done with the unproto package
|
||||
*/
|
||||
|
||||
typedef char *charstar;
|
||||
|
||||
/*
|
||||
* New-style argument list with structured argument, one field being pointer
|
||||
* to function returning pointer to function with function-pointer argument
|
||||
*/
|
||||
|
||||
x(struct {
|
||||
struct {
|
||||
int (*(*foo) (int (*arg1) (double))) (float arg2);
|
||||
} foo;
|
||||
} baz) {
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* New-style function-pointer declaration. */
|
||||
|
||||
int (*(*bar0) (float)) (int);
|
||||
|
||||
/* Old-style argument list with new-style argument type. */
|
||||
|
||||
baz0(bar)
|
||||
int (*(*bar) (float)) (int);
|
||||
{}
|
||||
|
||||
/*
|
||||
* New-style argument list with new-style argument type, declaration
|
||||
* embedded within block. Plus a couple assignments with function calls that
|
||||
* look like casts.
|
||||
*/
|
||||
|
||||
foo(int (*(*bar) (float)) (int))
|
||||
{
|
||||
int (*baz) (int) = (int (*) (int)) 0,
|
||||
y = (y * (*baz) (y)),
|
||||
*(*z) (int) = (int *(*) (int)) 0;
|
||||
|
||||
struct { int (*foo)(int); } *(*s)(int) =
|
||||
(struct { int (*foo)(int); } *(*)(int)) 0;
|
||||
|
||||
{
|
||||
y = (y * (*baz) (y));
|
||||
}
|
||||
{
|
||||
z = (int *(*) (int)) 0;
|
||||
}
|
||||
{
|
||||
s = (struct { int (*foo)(int); } *(*)(int)) 0;
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Multiple declarations in one statement */
|
||||
|
||||
test1()
|
||||
{
|
||||
int foo2,*(*(*bar)(int))(float),*baz(double);
|
||||
}
|
||||
|
||||
/* Discriminate declarations from executable statements */
|
||||
|
||||
test2(charstar y)
|
||||
{
|
||||
int foo = 5,atoi(charstar);
|
||||
|
||||
foo = 5,atoi(y);
|
||||
}
|
||||
|
||||
/* Declarations without explicit type */
|
||||
|
||||
test3,test4(int);
|
||||
|
||||
test5(int y)
|
||||
{
|
||||
{
|
||||
test3;
|
||||
}
|
||||
{
|
||||
test4(y);
|
||||
}
|
||||
}
|
||||
|
||||
test6[1],test7(int);
|
||||
|
||||
test7(int x)
|
||||
{
|
||||
{
|
||||
test6[1];
|
||||
}
|
||||
{
|
||||
test7(x);
|
||||
}
|
||||
}
|
||||
|
||||
/* Checking a complicated cast */
|
||||
|
||||
struct {
|
||||
struct {
|
||||
int (*f)(int), o;
|
||||
} bar;
|
||||
} (*baz2)(int) = (struct { struct { int (*f)(int), o; } bar; } (*)(int)) 0;
|
||||
|
||||
/* Distinguish things with the same shape but with different meaning */
|
||||
|
||||
test8(x)
|
||||
{
|
||||
{
|
||||
struct {
|
||||
int foo;
|
||||
} bar(charstar);
|
||||
}
|
||||
{
|
||||
do {
|
||||
int foo;
|
||||
} while (x);
|
||||
}
|
||||
}
|
||||
|
||||
/* Do not think foo(*bar) is a function pointer declaration */
|
||||
|
||||
test9(char *bar)
|
||||
{
|
||||
foo(*bar);
|
||||
}
|
||||
|
||||
/* another couple of special-cased words. */
|
||||
|
||||
test10(int x)
|
||||
{
|
||||
{
|
||||
int test10(int);
|
||||
do test10(x);
|
||||
while (x);
|
||||
}
|
||||
{
|
||||
return test10(x);
|
||||
}
|
||||
}
|
||||
|
||||
test11(int *x)
|
||||
{
|
||||
while (*x)
|
||||
(putchar(*x++));
|
||||
}
|
||||
|
||||
test11a(int *x)
|
||||
{
|
||||
for (*x;;)
|
||||
(putchar(*x++));
|
||||
}
|
||||
|
||||
/* #include directive between stuff that requires lookahead */
|
||||
|
||||
test12()
|
||||
{
|
||||
char *x = "\xf\0002\002\02\2" /* foo */
|
||||
#include "/dev/null"
|
||||
"\abar";
|
||||
|
||||
printf("foo" /* 1 */ "bar" /* 2 */ "baz");
|
||||
|
||||
*x = '\a';
|
||||
*x = '\xff';
|
||||
}
|
||||
|
||||
int test13(void);
|
||||
|
||||
/* line continuations in the middle of tokens */
|
||||
|
||||
te\
|
||||
st14();
|
||||
charstar test15 = "foo\
|
||||
bar";
|
||||
char test16 = "foo\\
|
||||
abar";
|
||||
|
||||
/* Array dimensions with unexpanded macros */
|
||||
|
||||
test17(charstar foo[bar]){}
|
||||
|
||||
int (*(*test18[bar])(charstar))(charstar) = \
|
||||
(int (*(*[bar])(charstar))(charstar)) 0;
|
||||
|
||||
/* Function returning pointer to function */
|
||||
|
||||
int (*(*test19(long))(int))(double);
|
||||
|
||||
/* GCC accepts the following stuff, K&R C does not... */
|
||||
|
||||
void test20(int test21(double)) {}
|
||||
|
||||
void test22(struct { int foo; } test23(short)) {}
|
||||
|
||||
/* Do not blindly rewrite (*name(stuff))(otherstuff) */
|
||||
|
||||
void test23()
|
||||
{
|
||||
int (*test24(int)) (int),
|
||||
y = (*test24(2)) (3),
|
||||
z = ((*test24(2)) (3));
|
||||
}
|
||||
|
||||
/* Function returning pointer to function */
|
||||
|
||||
int (*(*test25(long foo))(int bar))(double baz){ /* body */ }
|
||||
|
||||
int (*(*test26(foo))())()
|
||||
long foo;
|
||||
{ /* body */ }
|
||||
|
||||
#define ARGSTR() struct {int l; char c[1];}
|
||||
|
||||
void functie(ARGSTR() *cmdlin, ARGSTR() *c1)
|
||||
{
|
||||
}
|
||||
|
|
@ -1,271 +0,0 @@
|
|||
# 1 "example.c"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
typedef char *charstar;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
x(
|
||||
|
||||
|
||||
|
||||
baz)
|
||||
# 14 "example.c"
|
||||
struct {
|
||||
struct {
|
||||
int (*(*foo)())();
|
||||
} foo;
|
||||
} baz;
|
||||
# 18 "example.c"
|
||||
{/*1*/
|
||||
/* end dcls */return (0);
|
||||
}/*1*/
|
||||
|
||||
|
||||
|
||||
int (*(*bar0)())();
|
||||
|
||||
|
||||
|
||||
baz0(bar)
|
||||
int (*(*bar)())();
|
||||
{/*1*/}/*1*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
foo(bar)
|
||||
# 38 "example.c"
|
||||
int (*(*bar)())();
|
||||
{/*1*/
|
||||
int (*baz)()= (int (*)()) 0,
|
||||
y = (y * (*baz)(y)),
|
||||
*(*z)()= (int *(*)()) 0;
|
||||
|
||||
struct {/*2*/ int (*foo)(); }/*2*/ *(*s)()=
|
||||
(struct { int (*foo)(); } *(*)()) 0;
|
||||
|
||||
/* end dcls */{/*2*/
|
||||
y /* end dcls */= (y * (*baz)(y));
|
||||
}/*2*/
|
||||
{/*2*/
|
||||
z /* end dcls */= (int *(*)()) 0;
|
||||
}/*2*/
|
||||
{/*2*/
|
||||
s /* end dcls */= (struct { int (*foo)(); } *(*)()) 0;
|
||||
}/*2*/
|
||||
|
||||
return (0);
|
||||
}/*1*/
|
||||
|
||||
|
||||
|
||||
test1()
|
||||
{/*1*/
|
||||
int foo2,*(*(*bar)())(),*baz();
|
||||
}/*1*/
|
||||
|
||||
|
||||
|
||||
test2(y)
|
||||
# 69 "example.c"
|
||||
charstar y;
|
||||
{/*1*/
|
||||
int foo = 5,atoi();
|
||||
|
||||
foo /* end dcls */= 5,atoi(y);
|
||||
}/*1*/
|
||||
|
||||
|
||||
|
||||
test3,test4();
|
||||
|
||||
test5(y)
|
||||
# 80 "example.c"
|
||||
int y;
|
||||
{/*1*/
|
||||
/* end dcls */{/*2*/
|
||||
test3/* end dcls */;
|
||||
}/*2*/
|
||||
{/*2*/
|
||||
test4/* end dcls */(y);
|
||||
}/*2*/
|
||||
}/*1*/
|
||||
|
||||
test6[1],test7();
|
||||
|
||||
test7(x)
|
||||
# 92 "example.c"
|
||||
int x;
|
||||
{/*1*/
|
||||
/* end dcls */{/*2*/
|
||||
test6/* end dcls */[1];
|
||||
}/*2*/
|
||||
{/*2*/
|
||||
test7/* end dcls */(x);
|
||||
}/*2*/
|
||||
}/*1*/
|
||||
|
||||
|
||||
|
||||
struct {/*1*/
|
||||
struct {/*2*/
|
||||
int (*f)(), o;
|
||||
}/*2*/ bar;
|
||||
}/*1*/ (*baz2)()= (struct { struct { int (*f)(), o; } bar; } (*)()) 0;
|
||||
|
||||
|
||||
|
||||
test8(x)
|
||||
{/*1*/
|
||||
/* end dcls */{/*2*/
|
||||
struct {/*3*/
|
||||
int foo;
|
||||
}/*3*/ bar();
|
||||
}/*2*/
|
||||
{/*2*/
|
||||
/* end dcls */do {/*3*/
|
||||
int foo;
|
||||
}/*3*/ while (x);
|
||||
}/*2*/
|
||||
}/*1*/
|
||||
|
||||
|
||||
|
||||
test9(bar)
|
||||
# 128 "example.c"
|
||||
char *bar;
|
||||
{/*1*/
|
||||
foo/* end dcls */(*bar);
|
||||
}/*1*/
|
||||
|
||||
|
||||
|
||||
test10(x)
|
||||
# 135 "example.c"
|
||||
int x;
|
||||
{/*1*/
|
||||
/* end dcls */{/*2*/
|
||||
int test10();
|
||||
/* end dcls */do test10(x);
|
||||
while (x);
|
||||
}/*2*/
|
||||
{/*2*/
|
||||
/* end dcls */return test10(x);
|
||||
}/*2*/
|
||||
}/*1*/
|
||||
|
||||
test11(x)
|
||||
# 147 "example.c"
|
||||
int *x;
|
||||
{/*1*/
|
||||
/* end dcls */while (*x)
|
||||
(putchar(*x++));
|
||||
}/*1*/
|
||||
|
||||
test11a(x)
|
||||
# 153 "example.c"
|
||||
int *x;
|
||||
{/*1*/
|
||||
/* end dcls */for (*x;;)
|
||||
(putchar(*x++));
|
||||
}/*1*/
|
||||
|
||||
|
||||
|
||||
test12()
|
||||
{/*1*/
|
||||
char *x =
|
||||
# 1 "/dev/null" 1
|
||||
# 165 "example.c" 2
|
||||
# 163 "example.c"
|
||||
"\017\0002\002\002\002\007bar"
|
||||
|
||||
;
|
||||
|
||||
printf/* end dcls */("foobarbaz" );
|
||||
|
||||
*x = '\007';
|
||||
*x = '\377';
|
||||
}/*1*/
|
||||
|
||||
int test13();
|
||||
|
||||
|
||||
|
||||
test14();
|
||||
|
||||
charstar test15 = "foobar";
|
||||
|
||||
char test16 = "foo\007bar";
|
||||
|
||||
|
||||
|
||||
|
||||
test17(foo)
|
||||
# 186 "example.c"
|
||||
charstar foo[bar];
|
||||
# 186 "example.c"
|
||||
{/*1*/}/*1*/
|
||||
|
||||
int (*(*test18[bar])())()= (int (*(*[bar])())()) 0;
|
||||
|
||||
|
||||
|
||||
|
||||
int (*(*test19())())();
|
||||
|
||||
|
||||
|
||||
void test20(test21)
|
||||
# 197 "example.c"
|
||||
int test21();
|
||||
# 197 "example.c"
|
||||
{/*1*/}/*1*/
|
||||
|
||||
void test22(test23)
|
||||
# 199 "example.c"
|
||||
struct { int foo; } test23();
|
||||
# 199 "example.c"
|
||||
{/*1*/}/*1*/
|
||||
|
||||
|
||||
|
||||
void test23()
|
||||
{/*1*/
|
||||
int (*test24())(),
|
||||
y = (*test24(2)) (3),
|
||||
z = ((*test24(2))(3));
|
||||
}/*1*/
|
||||
|
||||
|
||||
|
||||
int (*(*test25(foo))())()
|
||||
# 212 "example.c"
|
||||
long foo;
|
||||
# 212 "example.c"
|
||||
{/*1*/ }/*1*/
|
||||
|
||||
int (*(*test26(foo))())()
|
||||
long foo;
|
||||
{/*1*/ }/*1*/
|
||||
|
||||
|
||||
|
||||
void functie(cmdlin,c1)
|
||||
# 220 "example.c"
|
||||
struct {int l; char c[1];} *cmdlin;
|
||||
# 220 "example.c"
|
||||
struct {int l; char c[1];} *c1;
|
||||
{/*1*/
|
||||
}/*1*/
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
/*++
|
||||
/* NAME
|
||||
/* hash 3
|
||||
/* SUMMARY
|
||||
/* compute hash value for string
|
||||
/* SYNOPSIS
|
||||
/* int hash(string, size)
|
||||
/* char *string;
|
||||
/* int size;
|
||||
/* DESCRIPTION
|
||||
/* This function computes for the given null-terminated string an
|
||||
/* integer hash value in the range 0..size-1.
|
||||
/* SEE ALSO
|
||||
/* .fi
|
||||
/* Alfred V. Aho, Ravi Sethi and Jeffrey D. Ullman: Compilers:
|
||||
/* principles, techniques and tools; Addison-Wesley, Amsterdam, 1986.
|
||||
/* AUTHOR(S)
|
||||
/* Wietse Venema
|
||||
/* Eindhoven University of Technology
|
||||
/* Department of Mathematics and Computer Science
|
||||
/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
|
||||
/*
|
||||
/* Originally written by: P. J. Weinberger at Bell Labs.
|
||||
/* LAST MODIFICATION
|
||||
/* 92/01/15 21:53:12
|
||||
/* VERSION/RELEASE
|
||||
/* %I
|
||||
/*--*/
|
||||
|
||||
static char hash_sccsid[] = "@(#) hash.c 1.1 92/01/15 21:53:12";
|
||||
|
||||
/* hash - hash a string; original author: P. J. Weinberger at Bell Labs. */
|
||||
|
||||
int hash(s, size)
|
||||
register char *s;
|
||||
unsigned size;
|
||||
{
|
||||
register unsigned long h = 0;
|
||||
register unsigned long g;
|
||||
|
||||
/*
|
||||
* For a performance comparison with the hash function presented in K&R,
|
||||
* first edition, see the "Dragon" book by Aho, Sethi and Ullman.
|
||||
*/
|
||||
|
||||
while (*s) {
|
||||
h = (h << 4) + *s++;
|
||||
if (g = (h & 0xf0000000)) {
|
||||
h ^= (g >> 24);
|
||||
h ^= g;
|
||||
}
|
||||
}
|
||||
return (h % size);
|
||||
}
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
/*
|
||||
* @(#) stdarg.h 1.4 93/06/18 22:29:44
|
||||
*
|
||||
* Sample stdarg.h file for use with the unproto filter.
|
||||
*
|
||||
* This file serves two purposes.
|
||||
*
|
||||
* 1 - On systems that do not have a /usr/include/stdarg.h file, it should be
|
||||
* included by C source files that implement ANSI-style variadic functions.
|
||||
* Ultrix 4.[0-2] comes with stdarg.h but still needs the one that is
|
||||
* provided with the unproto filter.
|
||||
*
|
||||
* 2 - To configure the unprototyper itself. If the _VA_ALIST_ macro is
|
||||
* defined, its value will appear in the place of the "..." at the end of
|
||||
* argument lists of variadic function *definitions* (not declarations).
|
||||
* Some compilers (such as Greenhills m88k) have a non-empty va_dcl
|
||||
* definition in the system header file varargs.h. If that is the case,
|
||||
* define "_VA_DCL_" with the same value as va_dcl. If _VA_DCL_ is defined,
|
||||
* the unprototyper will emit its value just before the opening "{".
|
||||
*
|
||||
* Compilers that always pass arguments via the stack can use the default code
|
||||
* at the end of this file (this usually applies for the vax, mc68k and
|
||||
* 80*86 architectures).
|
||||
*
|
||||
* Special tricks are needed for compilers that pass some or all function
|
||||
* arguments via registers. Examples of the latter are given for the mips
|
||||
* and sparc architectures. Usually the compiler special-cases an argument
|
||||
* declaration such as "va_alist" or "__builtin_va_alist". For inspiration,
|
||||
* see the local /usr/include/varargs.h file.
|
||||
*
|
||||
* You can use the varargs.c program provided with the unproto package to
|
||||
* verify that the stdarg.h file has been set up correctly.
|
||||
*/
|
||||
|
||||
#ifdef sparc /* tested with SunOS 4.1.1 */
|
||||
|
||||
#define _VA_ALIST_ "__builtin_va_alist"
|
||||
typedef char *va_list;
|
||||
#define va_start(ap, p) (ap = (char *) &__builtin_va_alist)
|
||||
#define va_arg(ap, type) ((type *) __builtin_va_arg_incr((type *) ap))[0]
|
||||
#define va_end(ap)
|
||||
|
||||
#else
|
||||
#ifdef mips /* tested with Ultrix 4.0 and 4.2 */
|
||||
|
||||
#define _VA_ALIST_ "va_alist"
|
||||
#include "/usr/include/stdarg.h"
|
||||
|
||||
#else
|
||||
#ifdef m88k /* Motorola SYSTEM V/88 R32V3 */
|
||||
|
||||
#define _VA_ALIST_ "va_alist"
|
||||
#define _VA_DCL_ "va_type va_alist;"
|
||||
typedef struct _va_struct {
|
||||
int va_narg;
|
||||
int *va_stkaddr;
|
||||
int *va_iregs;
|
||||
} va_list;
|
||||
#define va_start(ap, p) \
|
||||
((ap).va_narg=(int *)&va_alist-va_stkarg, \
|
||||
(ap).va_stkaddr=va_stkarg, \
|
||||
(ap).va_iregs=(int *)va_intreg)
|
||||
#define va_end(p)
|
||||
#if defined(LittleEndian)
|
||||
#define va_arg(p,mode) \
|
||||
(*(mode *)_gh_va_arg(&p, va_align(mode), va_regtyp(mode), sizeof(mode)))
|
||||
#else /* defined(LittleEndian) */
|
||||
#define va_arg(p,mode) ( \
|
||||
(p).va_narg += ((p).va_narg & (va_align(mode) == 8)) + \
|
||||
(sizeof(mode)+3)/4, \
|
||||
((mode *)((va_regtyp(mode) && (p).va_narg <= 8 ? \
|
||||
(p).va_iregs: \
|
||||
(p).va_stkaddr) + (p).va_narg))[-1])
|
||||
#endif /* defined(LittleEndian) */
|
||||
|
||||
#else
|
||||
#ifdef hpux
|
||||
#include <stdarg.h>
|
||||
|
||||
#else /* vax, mc68k, 80*86 */
|
||||
|
||||
typedef char *va_list;
|
||||
#define va_start(ap, p) (ap = (char *) (&(p)+1))
|
||||
#define va_arg(ap, type) ((type *) (ap += sizeof(type)))[-1]
|
||||
#define va_end(ap)
|
||||
|
||||
#endif /* hpux */
|
||||
#endif /* m88k */
|
||||
#endif /* mips */
|
||||
#endif /* sparc */
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
/* @(#) stddef.h 1.1 92/02/15 17:25:46 */
|
||||
|
||||
#ifndef _stddef_h_
|
||||
#define _stddef_h_
|
||||
|
||||
/* NULL is also defined in <stdio.h> */
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
/* Structure member offset - some compilers barf on this. */
|
||||
|
||||
#define offsetof(type, member) ((size_t) &((type *)0)->member)
|
||||
|
||||
/* Some of the following types may already be defined in <sys/types.h>. */
|
||||
|
||||
/* #include <sys/types.h> */
|
||||
/* typedef long ptrdiff_t; /* type of pointer difference */
|
||||
/* typedef unsigned short wchar_t; /* wide character type */
|
||||
/* typedef unsigned size_t; /* type of sizeof */
|
||||
|
||||
#endif /* _stddef_h_ */
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
/* @(#) stdlib.h 1.1 92/02/15 17:25:45 */
|
||||
|
||||
#ifndef _stdlib_h_
|
||||
#define _stdlib_h_
|
||||
|
||||
/* NULL is also defined in <stdio.h> */
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Some functions in this file will be missing from the typical pre-ANSI
|
||||
* UNIX library. Some pre-ANSI UNIX library functions have return types
|
||||
* that differ from what ANSI requires.
|
||||
*/
|
||||
|
||||
extern double atof();
|
||||
extern int atoi();
|
||||
extern long atol();
|
||||
extern double strtod();
|
||||
extern long strtol();
|
||||
extern unsigned long strtoul();
|
||||
extern int rand();
|
||||
extern void srand();
|
||||
extern char *calloc();
|
||||
extern char *malloc();
|
||||
extern char *realloc();
|
||||
extern void free();
|
||||
extern void abort();
|
||||
extern void exit();
|
||||
extern int atextit();
|
||||
extern int system();
|
||||
extern char *getenv();
|
||||
extern char *bsearch();
|
||||
extern void qsort();
|
||||
extern int abs();
|
||||
extern long labs();
|
||||
|
||||
typedef struct {
|
||||
int quot;
|
||||
int rem;
|
||||
} div_t;
|
||||
|
||||
typedef struct {
|
||||
long quot;
|
||||
long rem;
|
||||
} ldiv_t;
|
||||
|
||||
extern div_t div();
|
||||
extern ldiv_t ldiv();
|
||||
|
||||
#endif /* _stdlib_h_ */
|
||||
|
|
@ -1,71 +0,0 @@
|
|||
/*++
|
||||
/* NAME
|
||||
/* strsave 3
|
||||
/* SUMMARY
|
||||
/* maintain unique copy of a string
|
||||
/* SYNOPSIS
|
||||
/* char *strsave(string)
|
||||
/* char *string;
|
||||
/* DESCRIPTION
|
||||
/* This function returns a pointer to an unique copy of its
|
||||
/* argument.
|
||||
/* DIAGNOSTISC
|
||||
/* strsave() calls fatal() when it runs out of memory.
|
||||
/* AUTHOR(S)
|
||||
/* Wietse Venema
|
||||
/* Eindhoven University of Technology
|
||||
/* Department of Mathematics and Computer Science
|
||||
/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
|
||||
/* LAST MODIFICATION
|
||||
/* 92/01/15 21:53:13
|
||||
/* VERSION/RELEASE
|
||||
/* 1.1
|
||||
/*--*/
|
||||
|
||||
static char strsave_sccsid[] = "@(#) strsave.c 1.1 92/01/15 21:53:13";
|
||||
|
||||
/* C library */
|
||||
|
||||
extern char *strcpy();
|
||||
extern char *malloc();
|
||||
|
||||
/* Application-specific stuff */
|
||||
|
||||
#include "error.h"
|
||||
|
||||
#define STR_TABSIZE 100
|
||||
|
||||
struct string {
|
||||
char *strval; /* unique string copy */
|
||||
struct string *next; /* next one in hash chain */
|
||||
};
|
||||
|
||||
static struct string *str_tab[STR_TABSIZE] = {0,};
|
||||
|
||||
/* More string stuff. Maybe it should go to an #include file. */
|
||||
|
||||
#define STREQ(x,y) (*(x) == *(y) && strcmp((x),(y)) == 0)
|
||||
|
||||
/* strsave - save unique copy of string */
|
||||
|
||||
char *strsave(str)
|
||||
register char *str;
|
||||
{
|
||||
register struct string *s;
|
||||
register int where = hash(str, STR_TABSIZE);
|
||||
|
||||
/* Look for existing entry. */
|
||||
|
||||
for (s = str_tab[where]; s; s = s->next)
|
||||
if (STREQ(str, s->strval))
|
||||
return (s->strval);
|
||||
|
||||
/* Add new entry. */
|
||||
|
||||
if ((s = (struct string *) malloc(sizeof(*s))) == 0
|
||||
|| (s->strval = malloc(strlen(str) + 1)) == 0)
|
||||
fatal("out of memory");
|
||||
s->next = str_tab[where];
|
||||
str_tab[where] = s;
|
||||
return (strcpy(s->strval, str));
|
||||
}
|
||||
|
|
@ -1,144 +0,0 @@
|
|||
/*++
|
||||
/* NAME
|
||||
/* symbol 3
|
||||
/* SUMMARY
|
||||
/* rudimentary symbol table package
|
||||
/* SYNOPSIS
|
||||
/* #include "symbol.h"
|
||||
/*
|
||||
/* void sym_init()
|
||||
/*
|
||||
/* void sym_enter(name, type)
|
||||
/* char *name;
|
||||
/* int type;
|
||||
/*
|
||||
/* struct symbol *sym_find(name)
|
||||
/* char *name;
|
||||
/* DESCRIPTION
|
||||
/* This is a rudimentary symbol-table package, just enough to
|
||||
/* keep track of a couple of C keywords.
|
||||
/*
|
||||
/* sym_init() primes the table with C keywords. At present, most of
|
||||
/* the keywords that have to do with types are left out.
|
||||
/* We need a different strategy to detect type definitions because
|
||||
/* we do not keep track of typedef names.
|
||||
/*
|
||||
/* sym_enter() adds an entry to the symbol table.
|
||||
/*
|
||||
/* sym_find() locates a symbol table entry (it returns 0 if
|
||||
/* it is not found).
|
||||
/* AUTHOR(S)
|
||||
/* Wietse Venema
|
||||
/* Eindhoven University of Technology
|
||||
/* Department of Mathematics and Computer Science
|
||||
/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
|
||||
/* LAST MODIFICATION
|
||||
/* 92/02/15 18:59:56
|
||||
/* VERSION/RELEASE
|
||||
/* 1.4
|
||||
/*--*/
|
||||
|
||||
static char symbol_sccsid[] = "@(#) symbol.c 1.4 92/02/15 18:59:56";
|
||||
|
||||
/* C library */
|
||||
|
||||
extern char *strcpy();
|
||||
extern char *malloc();
|
||||
|
||||
/* Application-specific stuff */
|
||||
|
||||
#include "error.h"
|
||||
#include "token.h"
|
||||
#include "symbol.h"
|
||||
|
||||
#define SYM_TABSIZE 20
|
||||
|
||||
static struct symbol *sym_tab[SYM_TABSIZE] = {0,};
|
||||
|
||||
/* More string stuff. Maybe it should go to an #include file. */
|
||||
|
||||
#define STREQ(x,y) (*(x) == *(y) && strcmp((x),(y)) == 0)
|
||||
|
||||
/* sym_enter - enter symbol into table */
|
||||
|
||||
void sym_enter(name, type)
|
||||
char *name;
|
||||
int type;
|
||||
{
|
||||
struct symbol *s;
|
||||
int where;
|
||||
|
||||
if ((s = (struct symbol *) malloc(sizeof(*s))) == 0
|
||||
|| (s->name = malloc(strlen(name) + 1)) == 0)
|
||||
fatal("out of memory");
|
||||
(void) strcpy(s->name, name);
|
||||
s->type = type;
|
||||
|
||||
where = hash(name, SYM_TABSIZE);
|
||||
s->next = sym_tab[where];
|
||||
sym_tab[where] = s;
|
||||
}
|
||||
|
||||
/* sym_find - locate symbol definition */
|
||||
|
||||
struct symbol *sym_find(name)
|
||||
register char *name;
|
||||
{
|
||||
register struct symbol *s;
|
||||
|
||||
/*
|
||||
* This function is called for almost every "word" token, so it better be
|
||||
* fast.
|
||||
*/
|
||||
|
||||
for (s = sym_tab[hash(name, SYM_TABSIZE)]; s; s = s->next)
|
||||
if (STREQ(name, s->name))
|
||||
return (s);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialization data for symbol table. We do not enter keywords for types.
|
||||
* We use a different strategy to detect type declarations because we do not
|
||||
* keep track of typedef names.
|
||||
*/
|
||||
|
||||
struct sym {
|
||||
char *name;
|
||||
int tokno;
|
||||
};
|
||||
|
||||
static struct sym syms[] = {
|
||||
"if", TOK_CONTROL,
|
||||
"else", TOK_CONTROL,
|
||||
"for", TOK_CONTROL,
|
||||
"while", TOK_CONTROL,
|
||||
"do", TOK_CONTROL,
|
||||
"switch", TOK_CONTROL,
|
||||
"case", TOK_CONTROL,
|
||||
"default", TOK_CONTROL,
|
||||
"return", TOK_CONTROL,
|
||||
"continue", TOK_CONTROL,
|
||||
"break", TOK_CONTROL,
|
||||
"goto", TOK_CONTROL,
|
||||
"struct", TOK_COMPOSITE,
|
||||
"union", TOK_COMPOSITE,
|
||||
"__DATE__", TOK_DATE,
|
||||
"__TIME__", TOK_TIME,
|
||||
#if defined(MAP_VOID_STAR) || defined(MAP_VOID)
|
||||
"void", TOK_VOID,
|
||||
#endif
|
||||
"asm", TOK_OTHER,
|
||||
0,
|
||||
};
|
||||
|
||||
/* sym_init - enter known keywords into symbol table */
|
||||
|
||||
void sym_init()
|
||||
{
|
||||
register struct sym *p;
|
||||
|
||||
for (p = syms; p->name; p++)
|
||||
sym_enter(p->name, p->tokno);
|
||||
}
|
||||
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
/* @(#) symbol.h 1.1 91/09/22 21:21:42 */
|
||||
|
||||
struct symbol {
|
||||
char *name; /* symbol name */
|
||||
int type; /* symbol type */
|
||||
struct symbol *next;
|
||||
};
|
||||
|
||||
extern void sym_enter(); /* add symbol to table */
|
||||
extern struct symbol *sym_find(); /* locate symbol */
|
||||
extern void sym_init(); /* prime the table */
|
||||
|
|
@ -1,432 +0,0 @@
|
|||
/*++
|
||||
/* NAME
|
||||
/* tok_class 3
|
||||
/* SUMMARY
|
||||
/* token classification
|
||||
/* PACKAGE
|
||||
/* unproto
|
||||
/* SYNOPSIS
|
||||
/* #include "token.h"
|
||||
/*
|
||||
/* void tok_unget(t)
|
||||
/* struct token *t;
|
||||
/*
|
||||
/* struct token *tok_class()
|
||||
/* DESCRIPTION
|
||||
/* tok_class() collects single and composite tokens, and
|
||||
/* recognizes keywords.
|
||||
/* At present, the only composite tokens are ()-delimited,
|
||||
/* comma-separated lists, and non-whitespace tokens with attached
|
||||
/* whitespace or comment tokens.
|
||||
/*
|
||||
/* Source transformations are: __DATE__ and __TIME__ are rewritten
|
||||
/* to string constants with the current date and time, respectively.
|
||||
/* Multiple string constants are concatenated. Optionally, "void *"
|
||||
/* is mapped to "char *", and plain "void" to "int".
|
||||
/*
|
||||
/* tok_unget() implements an arbitrary amount of token pushback.
|
||||
/* Only tokens obtained through tok_class() should be given to
|
||||
/* tok_unget(). This function accepts a list of tokens in
|
||||
/* last-read-first order.
|
||||
/* DIAGNOSTICS
|
||||
/* The code complains if input terminates in the middle of a list.
|
||||
/* BUGS
|
||||
/* Does not preserve white space at the beginning of a list element
|
||||
/* or after the end of a list.
|
||||
/* AUTHOR(S)
|
||||
/* Wietse Venema
|
||||
/* Eindhoven University of Technology
|
||||
/* Department of Mathematics and Computer Science
|
||||
/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
|
||||
/* LAST MODIFICATION
|
||||
/* 92/01/15 21:53:02
|
||||
/* VERSION/RELEASE
|
||||
/* 1.4
|
||||
/*--*/
|
||||
|
||||
static char class_sccsid[] = "@(#) tok_class.c 1.4 92/01/15 21:53:02";
|
||||
|
||||
/* C library */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
extern char *strcpy();
|
||||
extern long time();
|
||||
extern char *ctime();
|
||||
|
||||
/* Application-specific stuff */
|
||||
|
||||
#include "error.h"
|
||||
#include "vstring.h"
|
||||
#include "token.h"
|
||||
#include "symbol.h"
|
||||
|
||||
static struct token *tok_list();
|
||||
static void tok_list_struct();
|
||||
static void tok_list_append();
|
||||
static void tok_strcat();
|
||||
static void tok_time();
|
||||
static void tok_date();
|
||||
static void tok_space_append();
|
||||
|
||||
#if defined(MAP_VOID_STAR) || defined(MAP_VOID)
|
||||
static void tok_void(); /* rewrite void keyword */
|
||||
#endif
|
||||
|
||||
static struct token *tok_buf = 0; /* token push-back storage */
|
||||
|
||||
/* TOK_PREPEND - add token to LIFO queue, return head */
|
||||
|
||||
#define TOK_PREPEND(list,t) (t->next = list, list = t)
|
||||
|
||||
/* tok_space_append - append trailing space except at start of or after list */
|
||||
|
||||
static void tok_space_append(list, t)
|
||||
register struct token *list;
|
||||
register struct token *t;
|
||||
{
|
||||
|
||||
/*
|
||||
* The head/tail fields of a token do triple duty. They are used to keep
|
||||
* track of the members that make up a (list); to keep track of the
|
||||
* non-blank tokens that make up one list member; and, finally, to tack
|
||||
* whitespace and comment tokens onto the non-blank tokens that make up
|
||||
* one list member.
|
||||
*
|
||||
* Within a (list), white space and comment tokens are always tacked onto
|
||||
* the non-blank tokens to avoid parsing complications later on. For this
|
||||
* reason, blanks and comments at the beginning of a list member are
|
||||
* discarded because there is no token to tack them onto. (Well, we could
|
||||
* start each list member with a dummy token, but that would mess up the
|
||||
* whole unprototyper).
|
||||
*
|
||||
* Blanks or comments that follow a (list) are discarded, because the
|
||||
* head/tail fields of a (list) are already being used for other
|
||||
* purposes.
|
||||
*
|
||||
* Newlines within a (list) are discarded because they can mess up the
|
||||
* output when we rewrite function headers. The output routines will
|
||||
* regenerate discarded newlines, anyway.
|
||||
*/
|
||||
|
||||
if (list == 0 || list->tokno == TOK_LIST) {
|
||||
tok_free(t);
|
||||
} else {
|
||||
tok_list_append(list, t);
|
||||
}
|
||||
}
|
||||
|
||||
/* tok_class - discriminate single tokens, keywords, and composite tokens */
|
||||
|
||||
struct token *tok_class()
|
||||
{
|
||||
register struct token *t;
|
||||
register struct symbol *s;
|
||||
|
||||
/*
|
||||
* Use push-back token, if available. Push-back tokens are already
|
||||
* canonical and can be passed on to the caller without further
|
||||
* inspection.
|
||||
*/
|
||||
|
||||
if (t = tok_buf) {
|
||||
tok_buf = t->next;
|
||||
t->next = 0;
|
||||
return (t);
|
||||
}
|
||||
/* Read a new token and canonicalize it. */
|
||||
|
||||
if (t = tok_get()) {
|
||||
switch (t->tokno) {
|
||||
case '(': /* beginning of list */
|
||||
t = tok_list(t);
|
||||
break;
|
||||
case TOK_WORD: /* look up keyword */
|
||||
if ((s = sym_find(t->vstr->str))) {
|
||||
switch (s->type) {
|
||||
case TOK_TIME: /* map __TIME__ to string */
|
||||
tok_time(t);
|
||||
tok_strcat(t); /* look for more strings */
|
||||
break;
|
||||
case TOK_DATE: /* map __DATE__ to string */
|
||||
tok_date(t);
|
||||
tok_strcat(t); /* look for more strings */
|
||||
break;
|
||||
#if defined(MAP_VOID_STAR) || defined(MAP_VOID)
|
||||
case TOK_VOID: /* optionally map void types */
|
||||
tok_void(t);
|
||||
break;
|
||||
#endif
|
||||
default: /* other keyword */
|
||||
t->tokno = s->type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case '"': /* string, look for more */
|
||||
tok_strcat(t);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return (t);
|
||||
}
|
||||
|
||||
/* tok_list - collect ()-delimited, comma-separated list of tokens */
|
||||
|
||||
static struct token *tok_list(t)
|
||||
struct token *t;
|
||||
{
|
||||
register struct token *list = tok_alloc();
|
||||
char *filename;
|
||||
int lineno;
|
||||
|
||||
/* Save context of '(' for diagnostics. */
|
||||
|
||||
filename = t->path;
|
||||
lineno = t->line;
|
||||
|
||||
list->tokno = TOK_LIST;
|
||||
list->head = list->tail = t;
|
||||
list->path = t->path;
|
||||
list->line = t->line;
|
||||
#ifdef DEBUG
|
||||
strcpy(list->vstr->str, "LIST");
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Read until the matching ')' is found, accounting for structured stuff
|
||||
* (enclosed by '{' and '}' tokens). Break the list up at each ',' token,
|
||||
* and try to preserve as much whitespace as possible. Newlines are
|
||||
* discarded so that they will not mess up the layout when we rewrite
|
||||
* argument lists. The output routines will regenerate discarded
|
||||
* newlines.
|
||||
*/
|
||||
|
||||
while (t = tok_class()) { /* skip blanks */
|
||||
switch (t->tokno) {
|
||||
case ')': /* end of list */
|
||||
tok_list_append(list, t);
|
||||
return (list);
|
||||
case '{': /* struct/union type */
|
||||
tok_list_struct(list->tail, t);
|
||||
break;
|
||||
case TOK_WSPACE: /* preserve trailing blanks */
|
||||
tok_space_append(list->tail->tail, t); /* except after list */
|
||||
break;
|
||||
case '\n': /* fix newlines later */
|
||||
tok_free(t);
|
||||
break;
|
||||
case ',': /* list separator */
|
||||
tok_list_append(list, t);
|
||||
break;
|
||||
default: /* other */
|
||||
tok_list_append(list->tail, t);
|
||||
break;
|
||||
}
|
||||
}
|
||||
error_where(filename, lineno, "unmatched '('");
|
||||
return (list); /* do not waste any data */
|
||||
}
|
||||
|
||||
/* tok_list_struct - collect structured type info within list */
|
||||
|
||||
static void tok_list_struct(list, t)
|
||||
register struct token *list;
|
||||
register struct token *t;
|
||||
{
|
||||
char *filename;
|
||||
int lineno;
|
||||
|
||||
/*
|
||||
* Save context of '{' for diagnostics. This routine is called by the one
|
||||
* that collects list members. If the '}' is not found, the list
|
||||
* collector will not see the closing ')' either.
|
||||
*/
|
||||
|
||||
filename = t->path;
|
||||
lineno = t->line;
|
||||
|
||||
tok_list_append(list, t);
|
||||
|
||||
/*
|
||||
* Collect tokens until the matching '}' is found. Try to preserve as
|
||||
* much whitespace as possible. Newlines are discarded so that they do
|
||||
* not interfere when rewriting argument lists. The output routines will
|
||||
* regenerate discarded newlines.
|
||||
*/
|
||||
|
||||
while (t = tok_class()) {
|
||||
switch (t->tokno) {
|
||||
case TOK_WSPACE: /* preserve trailing blanks */
|
||||
tok_space_append(list->tail, t); /* except after list */
|
||||
break;
|
||||
case '\n': /* fix newlines later */
|
||||
tok_free(t);
|
||||
break;
|
||||
case '{': /* recurse */
|
||||
tok_list_struct(list, t);
|
||||
break;
|
||||
case '}': /* done */
|
||||
tok_list_append(list, t);
|
||||
return;
|
||||
default: /* other */
|
||||
tok_list_append(list, t);
|
||||
break;
|
||||
}
|
||||
}
|
||||
error_where(filename, lineno, "unmatched '{'");
|
||||
}
|
||||
|
||||
/* tok_strcat - concatenate multiple string constants */
|
||||
|
||||
static void tok_strcat(t1)
|
||||
register struct token *t1;
|
||||
{
|
||||
register struct token *t2;
|
||||
register struct token *lookahead = 0;
|
||||
|
||||
/*
|
||||
* Read ahead past whitespace, comments and newlines. If we find a string
|
||||
* token, concatenate it with the previous one and push back the
|
||||
* intervening tokens (thus preserving as much information as possible).
|
||||
* If we find something else, push back all lookahead tokens.
|
||||
*/
|
||||
|
||||
#define PUSHBACK_AND_RETURN { if (lookahead) tok_unget(lookahead); return; }
|
||||
|
||||
while (t2 = tok_class()) {
|
||||
switch (t2->tokno) {
|
||||
case TOK_WSPACE: /* read past comments/blanks */
|
||||
case '\n': /* read past newlines */
|
||||
TOK_PREPEND(lookahead, t2);
|
||||
break;
|
||||
case '"': /* concatenate string tokens */
|
||||
if (vs_strcpy(t1->vstr,
|
||||
t1->vstr->str + strlen(t1->vstr->str) - 1,
|
||||
t2->vstr->str + 1) == 0)
|
||||
fatal("out of memory");
|
||||
tok_free(t2);
|
||||
PUSHBACK_AND_RETURN;
|
||||
default: /* something else, push back */
|
||||
tok_unget(t2);
|
||||
PUSHBACK_AND_RETURN;
|
||||
}
|
||||
}
|
||||
PUSHBACK_AND_RETURN; /* hit EOF */
|
||||
}
|
||||
|
||||
#if defined(MAP_VOID_STAR) || defined(MAP_VOID)
|
||||
|
||||
/* tok_void - support for compilers that have problems with "void" */
|
||||
|
||||
static void tok_void(t)
|
||||
register struct token *t;
|
||||
{
|
||||
register struct token *t2;
|
||||
register struct token *lookahead = 0;
|
||||
|
||||
/*
|
||||
* Look ahead beyond whitespace, comments and newlines until we see a '*'
|
||||
* token. If one is found, replace "void" by "char". If we find something
|
||||
* else, and if "void" should always be mapped, replace "void" by "int".
|
||||
* Always push back the lookahead tokens.
|
||||
*
|
||||
* XXX The code also replaces the (void) argument list; this must be
|
||||
* accounted for later on. The alternative would be to add (in unproto.c)
|
||||
* TOK_VOID cases all over the place and that would be too error-prone.
|
||||
*/
|
||||
|
||||
#define PUSHBACK_AND_RETURN { if (lookahead) tok_unget(lookahead); return; }
|
||||
|
||||
while (t2 = tok_class()) {
|
||||
switch (TOK_PREPEND(lookahead, t2)->tokno) {
|
||||
case TOK_WSPACE: /* read past comments/blanks */
|
||||
case '\n': /* read past newline */
|
||||
break;
|
||||
case '*': /* "void *" -> "char *" */
|
||||
if (vs_strcpy(t->vstr, t->vstr->str, "char") == 0)
|
||||
fatal("out of memory");
|
||||
PUSHBACK_AND_RETURN;
|
||||
default:
|
||||
#ifdef MAP_VOID /* plain "void" -> "int" */
|
||||
if (vs_strcpy(t->vstr, t->vstr->str, "int") == 0)
|
||||
fatal("out of memory");
|
||||
#endif
|
||||
PUSHBACK_AND_RETURN;
|
||||
}
|
||||
}
|
||||
PUSHBACK_AND_RETURN; /* hit EOF */
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* tok_time - rewrite __TIME__ to "hh:mm:ss" string constant */
|
||||
|
||||
static void tok_time(t)
|
||||
struct token *t;
|
||||
{
|
||||
long now;
|
||||
char *cp;
|
||||
char buf[BUFSIZ];
|
||||
|
||||
/*
|
||||
* Using sprintf() to select parts of a string is gross, but this should
|
||||
* be fast enough.
|
||||
*/
|
||||
|
||||
(void) time(&now);
|
||||
cp = ctime(&now);
|
||||
sprintf(buf, "\"%.8s\"", cp + 11);
|
||||
if (vs_strcpy(t->vstr, t->vstr->str, buf) == 0)
|
||||
fatal("out of memory");
|
||||
t->tokno = buf[0];
|
||||
}
|
||||
|
||||
/* tok_date - rewrite __DATE__ to "Mmm dd yyyy" string constant */
|
||||
|
||||
static void tok_date(t)
|
||||
struct token *t;
|
||||
{
|
||||
long now;
|
||||
char *cp;
|
||||
char buf[BUFSIZ];
|
||||
|
||||
/*
|
||||
* Using sprintf() to select parts of a string is gross, but this should
|
||||
* be fast enough.
|
||||
*/
|
||||
|
||||
(void) time(&now);
|
||||
cp = ctime(&now);
|
||||
sprintf(buf, "\"%.3s %.2s %.4s\"", cp + 4, cp + 8, cp + 20);
|
||||
if (vs_strcpy(t->vstr, t->vstr->str, buf) == 0)
|
||||
fatal("out of memory");
|
||||
t->tokno = buf[0];
|
||||
}
|
||||
|
||||
/* tok_unget - push back one or more possibly composite tokens */
|
||||
|
||||
void tok_unget(t)
|
||||
register struct token *t;
|
||||
{
|
||||
register struct token *next;
|
||||
|
||||
do {
|
||||
next = t->next;
|
||||
TOK_PREPEND(tok_buf, t);
|
||||
} while (t = next);
|
||||
}
|
||||
|
||||
/* tok_list_append - append data to list */
|
||||
|
||||
static void tok_list_append(h, t)
|
||||
struct token *h;
|
||||
struct token *t;
|
||||
{
|
||||
if (h->head == 0) {
|
||||
h->head = h->tail = t;
|
||||
} else {
|
||||
h->tail->next = t;
|
||||
h->tail = t;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,612 +0,0 @@
|
|||
/*++
|
||||
/* NAME
|
||||
/* tok_io 3
|
||||
/* SUMMARY
|
||||
/* token I/O
|
||||
/* PACKAGE
|
||||
/* unproto
|
||||
/* SYNOPSIS
|
||||
/* #include "token.h"
|
||||
/*
|
||||
/* struct token *tok_get()
|
||||
/*
|
||||
/* void tok_flush(t)
|
||||
/* struct token *t;
|
||||
/*
|
||||
/* void tok_show(t)
|
||||
/* struct token *t;
|
||||
/*
|
||||
/* void tok_show_ch(t)
|
||||
/* struct token *t;
|
||||
/*
|
||||
/* void put_str(s)
|
||||
/* char *s;
|
||||
/*
|
||||
/* void put_ch(c)
|
||||
/* int c;
|
||||
/*
|
||||
/* void put_nl()
|
||||
/*
|
||||
/* char *in_path;
|
||||
/* int in_line;
|
||||
/* DESCRIPTION
|
||||
/* These functions read from stdin and write to stdout. The
|
||||
/* tokenizer keeps track of where the token appeared in the input
|
||||
/* stream; on output, this information is used to preserve correct
|
||||
/* line number information (even after lots of token lookahead or
|
||||
/* after function-header rewriting) so that diagnostics from the
|
||||
/* next compiler stage make sense.
|
||||
/*
|
||||
/* tok_get() reads the next token from standard input. It returns
|
||||
/* a null pointer when the end of input is reached.
|
||||
/*
|
||||
/* tok_show() displays the contents of a (possibly composite) token
|
||||
/* on the standard output.
|
||||
/*
|
||||
/* tok_show_ch() displays the contents of a single-character token
|
||||
/* on the standard output. The character should not be a newline.
|
||||
/*
|
||||
/* tok_flush() displays the contents of a (possibly composite) token
|
||||
/* on the standard output and makes it available for re-use.
|
||||
/*
|
||||
/* put_str() writes a null-terminated string to standard output.
|
||||
/* There should be no newline characters in the string argument.
|
||||
/*
|
||||
/* put_ch() writes one character to standard output. The character
|
||||
/* should not be a newline.
|
||||
/*
|
||||
/* put_nl() outputs a newline character and adjusts the program's idea of
|
||||
/* the current output line.
|
||||
/*
|
||||
/* The in_path and in_line variables contain the file name and
|
||||
/* line number of the most recently read token.
|
||||
/* BUGS
|
||||
/* The tokenizer is just good enough for the unproto filter.
|
||||
/* As a benefit, it is quite fast.
|
||||
/* AUTHOR(S)
|
||||
/* Wietse Venema
|
||||
/* Eindhoven University of Technology
|
||||
/* Department of Mathematics and Computer Science
|
||||
/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
|
||||
/* LAST MODIFICATION
|
||||
/* 92/01/15 21:52:59
|
||||
/* VERSION/RELEASE
|
||||
/* 1.3
|
||||
/*--*/
|
||||
|
||||
static char io_sccsid[] = "@(#) tok_io.c 1.3 92/01/15 21:52:59";
|
||||
|
||||
/* C library */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
extern char *strchr();
|
||||
extern char *malloc();
|
||||
extern char *realloc();
|
||||
extern char *strcpy();
|
||||
|
||||
/* Application-specific stuff */
|
||||
|
||||
#include "token.h"
|
||||
#include "vstring.h"
|
||||
#include "error.h"
|
||||
|
||||
extern char *strsave(); /* XXX need include file */
|
||||
|
||||
/* Stuff to keep track of original source file name and position */
|
||||
|
||||
static char def_path[] = ""; /* default path name */
|
||||
|
||||
char *in_path = def_path; /* current input file name */
|
||||
int in_line = 1; /* current input line number */
|
||||
|
||||
static char *out_path = def_path; /* last name in output line control */
|
||||
static int out_line = 1; /* current output line number */
|
||||
int last_ch; /* type of last output */
|
||||
|
||||
/* Forward declarations */
|
||||
|
||||
static int read_quoted();
|
||||
static void read_comment();
|
||||
static int backslash_newline();
|
||||
static char *read_hex();
|
||||
static char *read_octal();
|
||||
static void fix_line_control();
|
||||
|
||||
/*
|
||||
* Character input with one level of pushback. The INPUT() macro recursively
|
||||
* strips backslash-newline pairs from the input stream. The UNPUT() macro
|
||||
* should be used only for characters obtained through the INPUT() macro.
|
||||
*
|
||||
* After skipping a backslash-newline pair, the input line counter is not
|
||||
* updated, and we continue with the same logical source line. We just
|
||||
* update a counter with the number of backslash-newline sequences that must
|
||||
* be accounted for (backslash_newline() updates the counter). At the end of
|
||||
* the logical source line, an appropriate number of newline characters is
|
||||
* pushed back (in tok_get()). I do not know how GCC handles this, but it
|
||||
* seems to produce te same output.
|
||||
*
|
||||
* Because backslash_newline() recursively calls itself (through the INPUT()
|
||||
* macro), we will run out of stack space, given a sufficiently long
|
||||
* sequence of backslash-newline pairs.
|
||||
*/
|
||||
|
||||
static char in_char = 0; /* push-back storage */
|
||||
static int in_flag = 0; /* pushback available */
|
||||
static int nl_compensate = 0; /* line continuation kluge */
|
||||
|
||||
#define INPUT(c) (in_flag ? (in_flag = 0, c = in_char) : \
|
||||
(c = getchar()) != '\\' ? c : \
|
||||
(c = getchar()) != '\n' ? (ungetc(c, stdin), c = '\\') : \
|
||||
(c = backslash_newline()))
|
||||
#define UNPUT(c) (in_flag = 1, in_char = c)
|
||||
|
||||
/* Directives that should be ignored. */
|
||||
|
||||
#ifdef IGNORE_DIRECTIVES
|
||||
|
||||
static char *ignore_directives[] = {
|
||||
IGNORE_DIRECTIVES,
|
||||
0,
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
/* Modified string and ctype stuff. */
|
||||
|
||||
#define STREQUAL(x,y) (*(x) == *(y) && strcmp((x),(y)) == 0)
|
||||
|
||||
#define ISALNUM(c) (isalnum(c) || (c) == '_')
|
||||
#define ISALPHA(c) (isalpha(c) || (c) == '_')
|
||||
#define ISSPACE(c) (isspace(c) && c != '\n')
|
||||
#define ISDOT(c) (c == '.')
|
||||
#define ISHEX(c) (isdigit(c) || strchr("abcdefABCDEF", c) != 0)
|
||||
#define ISOCTAL(c) (isdigit(c) && (c) != '8' && (c) != '9')
|
||||
|
||||
/* Collect all characters that satisfy one condition */
|
||||
|
||||
#define COLLECT(v,c,cond) { \
|
||||
register struct vstring *vs = v; \
|
||||
register char *cp = vs->str; \
|
||||
*cp++ = c; \
|
||||
while (INPUT(c) != EOF) { \
|
||||
if (cond) { \
|
||||
if (VS_ADDCH(vs, cp, c) == 0) \
|
||||
fatal("out of memory"); \
|
||||
} else { \
|
||||
UNPUT(c); \
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
*cp = 0; \
|
||||
}
|
||||
|
||||
/* Ensure that output line information is correct */
|
||||
|
||||
#define CHECK_LINE_CONTROL(p,l) { if (out_path != (p) || out_line != (l)) \
|
||||
fix_line_control((p),(l)); }
|
||||
|
||||
/* do_control - parse control line */
|
||||
|
||||
static int do_control()
|
||||
{
|
||||
struct token *t;
|
||||
int line;
|
||||
char *path;
|
||||
|
||||
/* Make sure that the directive shows up in the right place. */
|
||||
|
||||
CHECK_LINE_CONTROL(in_path, in_line);
|
||||
|
||||
while (t = tok_get()) {
|
||||
switch (t->tokno) {
|
||||
|
||||
case TOK_WSPACE:
|
||||
/* Ignore blanks after "#" token. */
|
||||
tok_free(t);
|
||||
break;
|
||||
|
||||
case TOK_NUMBER:
|
||||
|
||||
/*
|
||||
* Line control is of the form: number pathname junk. Since we
|
||||
* have no idea what junk the preprocessor may generate, we copy
|
||||
* all line control tokens to stdout.
|
||||
*/
|
||||
|
||||
put_str("# ");
|
||||
line = atoi(t->vstr->str); /* extract line number */
|
||||
tok_flush(t);
|
||||
while ((t = tok_get()) && t->tokno == TOK_WSPACE)
|
||||
tok_flush(t); /* copy white space */
|
||||
if (t) { /* extract path name */
|
||||
path = (t->tokno == '"') ? strsave(t->vstr->str) : in_path;
|
||||
do {
|
||||
tok_flush(t); /* copy until newline */
|
||||
} while (t->tokno != '\n' && (t = tok_get()));
|
||||
}
|
||||
out_line = in_line = line; /* synchronize */
|
||||
out_path = in_path = path; /* synchronize */
|
||||
return;
|
||||
|
||||
#ifdef IGNORE_DIRECTIVES
|
||||
|
||||
case TOK_WORD:
|
||||
|
||||
/*
|
||||
* Optionally ignore other #directives. This is only a partial
|
||||
* solution, because the preprocessor will still see them.
|
||||
*/
|
||||
{
|
||||
char **cpp;
|
||||
char *cp = t->vstr->str;
|
||||
|
||||
for (cpp = ignore_directives; *cpp; cpp++) {
|
||||
if (STREQUAL(cp, *cpp)) {
|
||||
do {
|
||||
tok_free(t);
|
||||
} while (t->tokno != '\n' && (t = tok_get()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* FALLTHROUGH */
|
||||
#endif
|
||||
default:
|
||||
/* Pass through. */
|
||||
put_ch('#');
|
||||
do {
|
||||
tok_flush(t);
|
||||
} while (t->tokno != '\n' && (t = tok_get()));
|
||||
return;
|
||||
|
||||
case 0:
|
||||
/* Hit EOF, punt. */
|
||||
put_ch('#');
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* backslash_newline - fix up things after reading a backslash-newline pair */
|
||||
|
||||
static int backslash_newline()
|
||||
{
|
||||
register int c;
|
||||
|
||||
nl_compensate++;
|
||||
return (INPUT(c));
|
||||
}
|
||||
|
||||
/* tok_get - get next token */
|
||||
|
||||
static int last_tokno = '\n';
|
||||
|
||||
struct token *tok_get()
|
||||
{
|
||||
register struct token *t;
|
||||
register int c;
|
||||
int d;
|
||||
|
||||
/*
|
||||
* Get one from the pool and fill it in. The loop is here in case we hit
|
||||
* a preprocessor control line, which happens in a minority of all cases.
|
||||
* We update the token input path and line info *after* backslash-newline
|
||||
* processing or the newline compensation would go wrong.
|
||||
*/
|
||||
|
||||
t = tok_alloc();
|
||||
|
||||
for (;;) {
|
||||
if ((INPUT(c)) == EOF) {
|
||||
tok_free(t);
|
||||
return (0);
|
||||
} else if ((t->line = in_line, t->path = in_path), !isascii(c)) {
|
||||
t->vstr->str[0] = c;
|
||||
t->vstr->str[1] = 0;
|
||||
t->tokno = TOK_OTHER;
|
||||
break;
|
||||
} else if (ISSPACE(c)) {
|
||||
COLLECT(t->vstr, c, ISSPACE(c));
|
||||
t->tokno = TOK_WSPACE;
|
||||
break;
|
||||
} else if (ISALPHA(c)) {
|
||||
COLLECT(t->vstr, c, ISALNUM(c));
|
||||
t->tokno = TOK_WORD;
|
||||
break;
|
||||
} else if (isdigit(c)) {
|
||||
COLLECT(t->vstr, c, isdigit(c));
|
||||
t->tokno = TOK_NUMBER;
|
||||
break;
|
||||
} else if (c == '"' || c == '\'') {
|
||||
t->tokno = read_quoted(t->vstr, c); /* detect missing end quote */
|
||||
break;
|
||||
} else if (ISDOT(c)) {
|
||||
COLLECT(t->vstr, c, ISDOT(c));
|
||||
t->tokno = TOK_OTHER;
|
||||
break;
|
||||
} else if (c == '#' && last_tokno == '\n') {
|
||||
do_control();
|
||||
continue;
|
||||
} else {
|
||||
t->vstr->str[0] = c;
|
||||
if (c == '\n') {
|
||||
in_line++;
|
||||
if (nl_compensate > 0) { /* compensation for bs-nl */
|
||||
UNPUT('\n');
|
||||
nl_compensate--;
|
||||
}
|
||||
} else if (c == '/') {
|
||||
if ((INPUT(d)) == '*') {
|
||||
t->vstr->str[1] = d; /* comment */
|
||||
read_comment(t->vstr);
|
||||
t->tokno = TOK_WSPACE;
|
||||
break;
|
||||
} else {
|
||||
if (d != EOF)
|
||||
UNPUT(d);
|
||||
}
|
||||
} else if (c == '\\') {
|
||||
t->vstr->str[1] = (INPUT(c) == EOF ? 0 : c);
|
||||
t->vstr->str[2] = 0;
|
||||
t->tokno = TOK_OTHER;
|
||||
break;
|
||||
}
|
||||
t->vstr->str[1] = 0;
|
||||
t->tokno = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
last_tokno = t->tokno;
|
||||
t->end_line = in_line;
|
||||
return (t);
|
||||
}
|
||||
|
||||
/* read_quoted - read string or character literal, canonicalize escapes */
|
||||
|
||||
static int read_quoted(vs, ch)
|
||||
register struct vstring *vs;
|
||||
int ch;
|
||||
{
|
||||
register char *cp = vs->str;
|
||||
register int c;
|
||||
int ret = TOK_OTHER;
|
||||
|
||||
*cp++ = ch;
|
||||
|
||||
/*
|
||||
* Clobber the token type in case of a premature newline or EOF. This
|
||||
* prevents us from attempting to concatenate string constants with
|
||||
* broken ones that have no closing quote.
|
||||
*/
|
||||
|
||||
while (INPUT(c) != EOF) {
|
||||
if (c == '\n') { /* newline in string */
|
||||
UNPUT(c);
|
||||
break;
|
||||
}
|
||||
if (VS_ADDCH(vs, cp, c) == 0) /* store character */
|
||||
fatal("out of memory");
|
||||
if (c == ch) { /* closing quote */
|
||||
ret = c;
|
||||
break;
|
||||
}
|
||||
if (c == '\\') { /* parse escape sequence */
|
||||
if ((INPUT(c)) == EOF) { /* EOF, punt */
|
||||
break;
|
||||
} else if (c == 'a') { /* \a -> audible bell */
|
||||
if ((cp = vs_strcpy(vs, cp, BELL)) == 0)
|
||||
fatal("out of memory");
|
||||
} else if (c == 'x') { /* \xhh -> \nnn */
|
||||
cp = read_hex(vs, cp);
|
||||
} else if (ISOCTAL(c) && ch != '\'') {
|
||||
cp = read_octal(vs, cp, c); /* canonicalize \octal */
|
||||
} else {
|
||||
if (VS_ADDCH(vs, cp, c) == 0) /* \other: leave alone */
|
||||
fatal("out of memory");
|
||||
}
|
||||
}
|
||||
}
|
||||
*cp = 0;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/* read_comment - stuff a whole comment into one huge token */
|
||||
|
||||
static void read_comment(vs)
|
||||
register struct vstring *vs;
|
||||
{
|
||||
register char *cp = vs->str + 2; /* skip slash star */
|
||||
register int c;
|
||||
register int d;
|
||||
|
||||
while (INPUT(c) != EOF) {
|
||||
if (VS_ADDCH(vs, cp, c) == 0)
|
||||
fatal("out of memory");
|
||||
if (c == '*') {
|
||||
if ((INPUT(d)) == '/') {
|
||||
if (VS_ADDCH(vs, cp, d) == 0)
|
||||
fatal("out of memory");
|
||||
break;
|
||||
} else {
|
||||
if (d != EOF)
|
||||
UNPUT(d);
|
||||
}
|
||||
} else if (c == '\n') {
|
||||
in_line++;
|
||||
} else if (c == '\\') {
|
||||
if ((INPUT(d)) != EOF && VS_ADDCH(vs, cp, d) == 0)
|
||||
fatal("out of memory");
|
||||
}
|
||||
}
|
||||
*cp = 0;
|
||||
}
|
||||
|
||||
/* read_hex - rewrite hex escape to three-digit octal escape */
|
||||
|
||||
static char *read_hex(vs, cp)
|
||||
struct vstring *vs;
|
||||
register char *cp;
|
||||
{
|
||||
register int c;
|
||||
register int i;
|
||||
char buf[BUFSIZ];
|
||||
int len;
|
||||
unsigned val;
|
||||
|
||||
/*
|
||||
* Eat up all subsequent hex digits. Complain later when there are too
|
||||
* many.
|
||||
*/
|
||||
|
||||
for (i = 0; i < sizeof(buf) && (INPUT(c) != EOF) && ISHEX(c); i++)
|
||||
buf[i] = c;
|
||||
buf[i] = 0;
|
||||
|
||||
if (i < sizeof(buf) && c)
|
||||
UNPUT(c);
|
||||
|
||||
/*
|
||||
* Convert hex form to three-digit octal form. The three-digit form is
|
||||
* used so that strings can be concatenated without problems. Complain
|
||||
* about malformed input; truncate the result to at most three octal
|
||||
* digits.
|
||||
*/
|
||||
|
||||
if (i == 0) {
|
||||
error("\\x escape sequence without hexadecimal digits");
|
||||
if (VS_ADDCH(vs, cp, 'x') == 0)
|
||||
fatal("out of memory");
|
||||
} else {
|
||||
(void) sscanf(buf, "%x", &val);
|
||||
sprintf(buf, "%03o", val);
|
||||
if ((len = strlen(buf)) > 3)
|
||||
error("\\x escape sequence yields non-character value");
|
||||
if ((cp = vs_strcpy(vs, cp, buf + len - 3)) == 0)
|
||||
fatal("out of memory");
|
||||
}
|
||||
return (cp);
|
||||
}
|
||||
|
||||
/* read_octal - convert octal escape to three-digit format */
|
||||
|
||||
static char obuf[] = "00123";
|
||||
|
||||
static char *read_octal(vs, cp, c)
|
||||
register struct vstring *vs;
|
||||
register char *cp;
|
||||
register int c;
|
||||
{
|
||||
register int i;
|
||||
|
||||
#define buf_input (obuf + 2)
|
||||
|
||||
/* Eat up at most three octal digits. */
|
||||
|
||||
buf_input[0] = c;
|
||||
for (i = 1; i < 3 && (INPUT(c) != EOF) && ISOCTAL(c); i++)
|
||||
buf_input[i] = c;
|
||||
buf_input[i] = 0;
|
||||
|
||||
if (i < 3 && c)
|
||||
UNPUT(c);
|
||||
|
||||
/*
|
||||
* Leave three-digit octal escapes alone. Convert one-digit and two-digit
|
||||
* octal escapes to three-digit form by prefixing them with a suitable
|
||||
* number of '0' characters. This is done so that strings can be
|
||||
* concatenated without problems.
|
||||
*/
|
||||
|
||||
if ((cp = vs_strcpy(vs, cp, buf_input + i - 3)) == 0)
|
||||
fatal("out of memory");
|
||||
return (cp);
|
||||
}
|
||||
|
||||
/* put_nl - emit newline and adjust output line count */
|
||||
|
||||
void put_nl()
|
||||
{
|
||||
put_ch('\n');
|
||||
out_line++;
|
||||
}
|
||||
|
||||
/* fix_line_control - to adjust path and/or line count info in output */
|
||||
|
||||
static void fix_line_control(path, line)
|
||||
register char *path;
|
||||
register int line;
|
||||
{
|
||||
|
||||
/*
|
||||
* This function is called sporadically, so it should not be a problem
|
||||
* that we repeat some of the tests that preceded this function call.
|
||||
*
|
||||
* Emit a newline if we are not at the start of a line.
|
||||
*
|
||||
* If we switch files, or if we jump backwards, emit line control. If we
|
||||
* jump forward, emit the proper number of newlines to compensate.
|
||||
*/
|
||||
|
||||
if (last_ch != '\n') /* terminate open line */
|
||||
put_nl();
|
||||
if (path != out_path || line < out_line) { /* file switch or back jump */
|
||||
printf("# %d %s\n", out_line = line, out_path = path);
|
||||
last_ch = '\n';
|
||||
} else { /* forward jump */
|
||||
while (line > out_line)
|
||||
put_nl();
|
||||
}
|
||||
}
|
||||
|
||||
/* tok_show_ch - output single-character token (not newline) */
|
||||
|
||||
void tok_show_ch(t)
|
||||
register struct token *t;
|
||||
{
|
||||
CHECK_LINE_CONTROL(t->path, t->line);
|
||||
|
||||
put_ch(t->tokno); /* show token contents */
|
||||
}
|
||||
|
||||
/* tok_show - output (possibly composite) token */
|
||||
|
||||
void tok_show(t)
|
||||
register struct token *t;
|
||||
{
|
||||
register struct token *p;
|
||||
|
||||
if (t->tokno == TOK_LIST) {
|
||||
register struct token *s;
|
||||
|
||||
/*
|
||||
* This branch is completely in terms of tok_xxx() primitives, so
|
||||
* there is no need to check the line control information.
|
||||
*/
|
||||
|
||||
for (s = t->head; s; s = s->next) {
|
||||
tok_show_ch(s); /* '(' or ',' or ')' */
|
||||
for (p = s->head; p; p = p->next)
|
||||
tok_show(p); /* show list element */
|
||||
}
|
||||
} else {
|
||||
register char *cp = t->vstr->str;
|
||||
|
||||
/*
|
||||
* Measurements show that it pays off to give special treatment to
|
||||
* single-character tokens. Note that both types of token may cause a
|
||||
* change of output line number.
|
||||
*/
|
||||
|
||||
CHECK_LINE_CONTROL(t->path, t->line);
|
||||
if (cp[1] == 0) {
|
||||
put_ch(*cp); /* single-character token */
|
||||
} else {
|
||||
put_str(cp); /* multi_character token */
|
||||
}
|
||||
out_line = t->end_line; /* may span multiple lines */
|
||||
for (p = t->head; p; p = p->next)
|
||||
tok_show(p); /* trailing blanks */
|
||||
}
|
||||
}
|
||||
|
|
@ -1,103 +0,0 @@
|
|||
/*++
|
||||
/* NAME
|
||||
/* tok_pool 3
|
||||
/* SUMMARY
|
||||
/* maintain pool of unused token structures
|
||||
/* PACKAGE
|
||||
/* unproto
|
||||
/* SYNOPSIS
|
||||
/* #include "token.h"
|
||||
/*
|
||||
/* struct token *tok_alloc()
|
||||
/*
|
||||
/* void tok_free(t)
|
||||
/* struct token *t;
|
||||
/* DESCRIPTION
|
||||
/* tok_alloc() and tok_free() maintain a pool of unused token
|
||||
/* structures.
|
||||
/*
|
||||
/* tok_alloc() takes the first free token structure from the pool
|
||||
/* or allocates a new one if the pool is empty.
|
||||
/*
|
||||
/* tok_free() adds a (possibly composite) token structure to the pool.
|
||||
/* BUGS
|
||||
/* The pool never shrinks.
|
||||
/* AUTHOR(S)
|
||||
/* Wietse Venema
|
||||
/* Eindhoven University of Technology
|
||||
/* Department of Mathematics and Computer Science
|
||||
/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
|
||||
/* LAST MODIFICATION
|
||||
/* 92/01/15 21:53:04
|
||||
/* VERSION/RELEASE
|
||||
/* 1.2
|
||||
/*--*/
|
||||
|
||||
static char pool_sccsid[] = "@(#) tok_pool.c 1.2 92/01/15 21:53:04";
|
||||
|
||||
/* C library */
|
||||
|
||||
extern char *malloc();
|
||||
|
||||
/* Application-specific stuff */
|
||||
|
||||
#include "token.h"
|
||||
#include "vstring.h"
|
||||
#include "error.h"
|
||||
|
||||
#define TOKLEN 5 /* initial string buffer length */
|
||||
|
||||
struct token *tok_pool = 0; /* free token pool */
|
||||
|
||||
/* tok_alloc - allocate token structure from pool or heap */
|
||||
|
||||
struct token *tok_alloc()
|
||||
{
|
||||
register struct token *t;
|
||||
|
||||
if (tok_pool) { /* re-use an old one */
|
||||
t = tok_pool;
|
||||
tok_pool = t->next;
|
||||
} else { /* create a new one */
|
||||
if ((t = (struct token *) malloc(sizeof(struct token))) == 0
|
||||
|| (t->vstr = vs_alloc(TOKLEN)) == 0)
|
||||
fatal("out of memory");
|
||||
}
|
||||
t->next = t->head = t->tail = 0;
|
||||
#ifdef DEBUG
|
||||
strcpy(t->vstr->str, "BUSY");
|
||||
#endif
|
||||
return (t);
|
||||
}
|
||||
|
||||
/* tok_free - return (possibly composite) token to pool of free tokens */
|
||||
|
||||
void tok_free(t)
|
||||
register struct token *t;
|
||||
{
|
||||
#ifdef DEBUG
|
||||
/* Check if we are freeing free token */
|
||||
|
||||
register struct token *p;
|
||||
|
||||
for (p = tok_pool; p; p = p->next)
|
||||
if (p == t)
|
||||
fatal("freeing free token");
|
||||
#endif
|
||||
|
||||
/* Free neighbours and subordinates first */
|
||||
|
||||
if (t->next)
|
||||
tok_free(t->next);
|
||||
if (t->head)
|
||||
tok_free(t->head);
|
||||
|
||||
/* Free self */
|
||||
|
||||
t->next = tok_pool;
|
||||
t->head = t->tail = 0;
|
||||
tok_pool = t;
|
||||
#ifdef DEBUG
|
||||
strcpy(t->vstr->str, "FREE");
|
||||
#endif
|
||||
}
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
/* @(#) token.h 1.4 92/01/15 21:53:17 */
|
||||
|
||||
struct token {
|
||||
int tokno; /* token value, see below */
|
||||
char *path; /* file name */
|
||||
int line; /* line number at token start */
|
||||
int end_line; /* line number at token end */
|
||||
struct vstring *vstr; /* token contents */
|
||||
struct token *next;
|
||||
struct token *head;
|
||||
struct token *tail;
|
||||
};
|
||||
|
||||
/* Special token values */
|
||||
|
||||
#define TOK_LIST 256 /* () delimited list */
|
||||
#define TOK_WORD 257 /* keyword or identifier */
|
||||
#define TOK_NUMBER 258 /* one or more digits */
|
||||
#define TOK_WSPACE 259 /* comment, white space, not newline */
|
||||
#define TOK_OTHER 260 /* other token */
|
||||
#define TOK_CONTROL 261 /* flow control keyword */
|
||||
#define TOK_COMPOSITE 262 /* struct or union keyword */
|
||||
#define TOK_DATE 263 /* date: Mmm dd yyyy */
|
||||
#define TOK_TIME 264 /* time: hh:mm:ss */
|
||||
#define TOK_VOID 265 /* void keyword */
|
||||
|
||||
/* Input/output functions and macros */
|
||||
|
||||
extern struct token *tok_get(); /* read next single token */
|
||||
extern void tok_show(); /* display (composite) token */
|
||||
extern struct token *tok_class(); /* classify tokens */
|
||||
extern void tok_unget(); /* stuff token back into input */
|
||||
extern void put_nl(); /* print newline character */
|
||||
extern void tok_show_ch(); /* emit single-character token */
|
||||
|
||||
#define tok_flush(t) (tok_show(t), tok_free(t))
|
||||
|
||||
#ifdef DEBUG
|
||||
#define put_ch(c) (putchar(last_ch = c),fflush(stdout))
|
||||
#define put_str(s) (fputs(s,stdout),last_ch = 0,fflush(stdout))
|
||||
#else
|
||||
#define put_ch(c) putchar(last_ch = c)
|
||||
#define put_str(s) (fputs(s,stdout),last_ch = 0)
|
||||
#endif
|
||||
|
||||
/* Memory management */
|
||||
|
||||
struct token *tok_alloc(); /* allocate token storage */
|
||||
extern void tok_free(); /* re-cycle storage */
|
||||
|
||||
/* Context */
|
||||
|
||||
extern char *in_path; /* current input path name */
|
||||
extern int in_line; /* current input line number */
|
||||
extern int last_ch; /* type of last output */
|
||||
|
|
@ -1,152 +0,0 @@
|
|||
.TH UNPROTO 1
|
||||
.ad
|
||||
.fi
|
||||
.SH NAME
|
||||
unproto
|
||||
\-
|
||||
compile ANSI C with traditional UNIX C compiler
|
||||
.SH PACKAGE
|
||||
.na
|
||||
.nf
|
||||
unproto
|
||||
.SH SYNOPSIS
|
||||
.na
|
||||
.nf
|
||||
/somewhere/cpp ...
|
||||
|
||||
cc cflags -E file.c | unproto >file.i; cc cflags -c file.i
|
||||
.SH DESCRIPTION
|
||||
.ad
|
||||
.fi
|
||||
This document describes a filter that sits in between the UNIX
|
||||
C preprocessor and the next UNIX C compiler stage, on the fly rewriting
|
||||
ANSI-style syntax to old-style syntax. Typically, the program is
|
||||
invoked by the native UNIX C compiler as an alternate preprocessor.
|
||||
The unprototyper in turn invokes the native C preprocessor and
|
||||
massages its output. Similar tricks can be used with the lint(1)
|
||||
command.
|
||||
|
||||
Language constructs that are always rewritten:
|
||||
.TP
|
||||
function headings, prototypes, pointer types
|
||||
ANSI-C style function headings, function prototypes, function
|
||||
pointer types and type casts are rewritten to old style.
|
||||
<stdarg.h> support is provided for functions with variable-length
|
||||
argument lists.
|
||||
.TP
|
||||
character and string constants
|
||||
The \\a and \\x escape sequences are rewritten to their (three-digit)
|
||||
octal equivalents.
|
||||
|
||||
Multiple string tokens are concatenated; an arbitrary number of
|
||||
whitespace or comment tokens may appear between successive
|
||||
string tokens.
|
||||
|
||||
Within string constants, octal escape sequences are rewritten to the
|
||||
three-digit \\ddd form, so that string concatenation produces correct
|
||||
results.
|
||||
.TP
|
||||
date and time
|
||||
The __DATE__ and __TIME__ tokens are replaced by string constants
|
||||
of the form "Mmm dd yyyy" and "hh:mm:ss", respectively. The result
|
||||
is subjected to string concatenation, just like any other string
|
||||
constant.
|
||||
.PP
|
||||
Language constructs that are rewritten only if the program has been
|
||||
configured to do so:
|
||||
.TP
|
||||
void types
|
||||
The unprototyper can be configured to rewrite "void *" to "char *",
|
||||
and even to rewrite plain "void" to "int".
|
||||
These features are configurable because many traditional UNIX C
|
||||
compilers do not need them.
|
||||
|
||||
Note: (void) argument lists are always replaced by empty ones.
|
||||
.PP
|
||||
ANSI C constructs that are not rewritten because the traditional
|
||||
UNIX C preprocessor provides suitable workarounds:
|
||||
.TP
|
||||
const and volatile
|
||||
Use the "-Dconst=" and/or "-Dvolatile=" preprocessor directives to
|
||||
get rid of unimplemented keywords.
|
||||
.TP
|
||||
token pasting and stringizing
|
||||
The traditional UNIX C preprocessor provides excellent alternatives.
|
||||
For example:
|
||||
|
||||
.nf
|
||||
.ne 2
|
||||
#define string(bar) "bar" /* instead of: # x */
|
||||
#define paste(x,y) x/**\/y /* instead of: x##y */
|
||||
.fi
|
||||
|
||||
There is a good reason why the # and ## operators are not implemented
|
||||
in the unprototyper.
|
||||
After program text has gone through a non-ANSI C preprocessor, all
|
||||
information about the grouping of the operands of # and ## is lost.
|
||||
Thus, if the unprototyper were to perform these operations, it would
|
||||
produce correct results only in the most trivial cases. Operands
|
||||
with embedded blanks, operands that expand to null tokens, and nested
|
||||
use of # and/or ## would cause all kinds of obscure problems.
|
||||
.PP
|
||||
Unsupported ANSI features:
|
||||
.TP
|
||||
trigraphs and #pragmas
|
||||
Trigraphs are useful only for systems with broken character sets.
|
||||
If the local compiler chokes on #pragma, insert a blank before the
|
||||
"#" character, and enclose the offending directive between #ifdef
|
||||
and #endif.
|
||||
.SH SEE ALSO
|
||||
.na
|
||||
.nf
|
||||
.ad
|
||||
.fi
|
||||
cc(1), how to specify a non-default C preprocessor.
|
||||
Some versions of the lint(1) command are implemented as a shell
|
||||
script. It should require only minor modification for integration
|
||||
with the unprototyper. Other versions of the lint(1) command accept
|
||||
the same command syntax as the C compiler for the specification of a
|
||||
non-default preprocessor. Some research may be needed.
|
||||
.SH FILES
|
||||
.na
|
||||
.nf
|
||||
/wherever/stdarg.h, provided with the unproto filter.
|
||||
.SH DIAGNOSTICS
|
||||
.ad
|
||||
.fi
|
||||
Problems are reported on the standard error stream.
|
||||
A non-zero exit status means that there was a problem.
|
||||
.SH BUGS
|
||||
.ad
|
||||
.fi
|
||||
The unprototyper should be run on preprocessed source only:
|
||||
unexpanded macros may confuse the program.
|
||||
|
||||
Declarations of (object) are misunderstood and will result in
|
||||
syntax errors: the objects between parentheses disappear.
|
||||
|
||||
Sometimes does not preserve whitespace after parentheses and commas.
|
||||
This is a purely aesthetical matter, and the compiler should not care.
|
||||
Whitespace within string constants is, of course, left intact.
|
||||
|
||||
Does not generate explicit type casts for function-argument
|
||||
expressions. The lack of explicit conversions between integral
|
||||
and/or pointer argument types should not be a problem in environments
|
||||
where sizeof(int) == sizeof(long) == sizeof(pointer). A more serious
|
||||
problem is the lack of automatic type conversions between integral and
|
||||
floating-point argument types. Let lint(1) be your friend.
|
||||
.SH AUTHOR(S)
|
||||
.na
|
||||
.nf
|
||||
Wietse Venema (wietse@wzv.win.tue.nl)
|
||||
Eindhoven University of Technology
|
||||
Department of Mathematics and Computer Science
|
||||
Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
|
||||
.SH LAST MODIFICATION
|
||||
.na
|
||||
.nf
|
||||
93/06/18 22:29:37
|
||||
.SH VERSION/RELEASE
|
||||
.na
|
||||
.nf
|
||||
1.6
|
||||
|
|
@ -1,999 +0,0 @@
|
|||
/*++
|
||||
/* NAME
|
||||
/* unproto 1
|
||||
/* SUMMARY
|
||||
/* compile ANSI C with traditional UNIX C compiler
|
||||
/* PACKAGE
|
||||
/* unproto
|
||||
/* SYNOPSIS
|
||||
/* /somewhere/cpp ...
|
||||
/*
|
||||
/* cc cflags -E file.c | unproto >file.i; cc cflags -c file.i
|
||||
/* DESCRIPTION
|
||||
/* This document describes a filter that sits in between the UNIX
|
||||
/* C preprocessor and the next UNIX C compiler stage, on the fly rewriting
|
||||
/* ANSI-style syntax to old-style syntax. Typically, the program is
|
||||
/* invoked by the native UNIX C compiler as an alternate preprocessor.
|
||||
/* The unprototyper in turn invokes the native C preprocessor and
|
||||
/* massages its output. Similar tricks can be used with the lint(1)
|
||||
/* command.
|
||||
/*
|
||||
/* Language constructs that are always rewritten:
|
||||
/* .TP
|
||||
/* function headings, prototypes, pointer types
|
||||
/* ANSI-C style function headings, function prototypes, function
|
||||
/* pointer types and type casts are rewritten to old style.
|
||||
/* <stdarg.h> support is provided for functions with variable-length
|
||||
/* argument lists.
|
||||
/* .TP
|
||||
/* character and string constants
|
||||
/* The \\a and \\x escape sequences are rewritten to their (three-digit)
|
||||
/* octal equivalents.
|
||||
/*
|
||||
/* Multiple string tokens are concatenated; an arbitrary number of
|
||||
/* whitespace or comment tokens may appear between successive
|
||||
/* string tokens.
|
||||
/*
|
||||
/* Within string constants, octal escape sequences are rewritten to the
|
||||
/* three-digit \\ddd form, so that string concatenation produces correct
|
||||
/* results.
|
||||
/* .TP
|
||||
/* date and time
|
||||
/* The __DATE__ and __TIME__ tokens are replaced by string constants
|
||||
/* of the form "Mmm dd yyyy" and "hh:mm:ss", respectively. The result
|
||||
/* is subjected to string concatenation, just like any other string
|
||||
/* constant.
|
||||
/* .PP
|
||||
/* Language constructs that are rewritten only if the program has been
|
||||
/* configured to do so:
|
||||
/* .TP
|
||||
/* void types
|
||||
/* The unprototyper can be configured to rewrite "void *" to "char *",
|
||||
/* and even to rewrite plain "void" to "int".
|
||||
/* These features are configurable because many traditional UNIX C
|
||||
/* compilers do not need them.
|
||||
/*
|
||||
/* Note: (void) argument lists are always replaced by empty ones.
|
||||
/* .PP
|
||||
/* ANSI C constructs that are not rewritten because the traditional
|
||||
/* UNIX C preprocessor provides suitable workarounds:
|
||||
/* .TP
|
||||
/* const and volatile
|
||||
/* Use the "-Dconst=" and/or "-Dvolatile=" preprocessor directives to
|
||||
/* get rid of unimplemented keywords.
|
||||
/* .TP
|
||||
/* token pasting and stringizing
|
||||
/* The traditional UNIX C preprocessor provides excellent alternatives.
|
||||
/* For example:
|
||||
/*
|
||||
/* .nf
|
||||
/* .ne 2
|
||||
/* #define string(bar) "bar" /* instead of: # x */
|
||||
/* #define paste(x,y) x/**\/y /* instead of: x##y */
|
||||
/* .fi
|
||||
/*
|
||||
/* There is a good reason why the # and ## operators are not implemented
|
||||
/* in the unprototyper.
|
||||
/* After program text has gone through a non-ANSI C preprocessor, all
|
||||
/* information about the grouping of the operands of # and ## is lost.
|
||||
/* Thus, if the unprototyper were to perform these operations, it would
|
||||
/* produce correct results only in the most trivial cases. Operands
|
||||
/* with embedded blanks, operands that expand to null tokens, and nested
|
||||
/* use of # and/or ## would cause all kinds of obscure problems.
|
||||
/* .PP
|
||||
/* Unsupported ANSI features:
|
||||
/* .TP
|
||||
/* trigraphs and #pragmas
|
||||
/* Trigraphs are useful only for systems with broken character sets.
|
||||
/* If the local compiler chokes on #pragma, insert a blank before the
|
||||
/* "#" character, and enclose the offending directive between #ifdef
|
||||
/* and #endif.
|
||||
/* SEE ALSO
|
||||
/* .ad
|
||||
/* .fi
|
||||
/* cc(1), how to specify a non-default C preprocessor.
|
||||
/* Some versions of the lint(1) command are implemented as a shell
|
||||
/* script. It should require only minor modification for integration
|
||||
/* with the unprototyper. Other versions of the lint(1) command accept
|
||||
/* the same command syntax as the C compiler for the specification of a
|
||||
/* non-default preprocessor. Some research may be needed.
|
||||
/* FILES
|
||||
/* /wherever/stdarg.h, provided with the unproto filter.
|
||||
/* DIAGNOSTICS
|
||||
/* Problems are reported on the standard error stream.
|
||||
/* A non-zero exit status means that there was a problem.
|
||||
/* BUGS
|
||||
/* The unprototyper should be run on preprocessed source only:
|
||||
/* unexpanded macros may confuse the program.
|
||||
/*
|
||||
/* Declarations of (object) are misunderstood and will result in
|
||||
/* syntax errors: the objects between parentheses disappear.
|
||||
/*
|
||||
/* Sometimes does not preserve whitespace after parentheses and commas.
|
||||
/* This is a purely aesthetical matter, and the compiler should not care.
|
||||
/* Whitespace within string constants is, of course, left intact.
|
||||
/*
|
||||
/* Does not generate explicit type casts for function-argument
|
||||
/* expressions. The lack of explicit conversions between integral
|
||||
/* and/or pointer argument types should not be a problem in environments
|
||||
/* where sizeof(int) == sizeof(long) == sizeof(pointer). A more serious
|
||||
/* problem is the lack of automatic type conversions between integral and
|
||||
/* floating-point argument types. Let lint(1) be your friend.
|
||||
/* AUTHOR(S)
|
||||
/* Wietse Venema (wietse@wzv.win.tue.nl)
|
||||
/* Eindhoven University of Technology
|
||||
/* Department of Mathematics and Computer Science
|
||||
/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
|
||||
/* LAST MODIFICATION
|
||||
/* 93/06/18 22:29:37
|
||||
/* VERSION/RELEASE
|
||||
/* 1.6
|
||||
/*--*/
|
||||
|
||||
static char unproto_sccsid[] = "@(#) unproto.c 1.6 93/06/18 22:29:37";
|
||||
|
||||
/* C library */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
extern void exit();
|
||||
extern int optind;
|
||||
extern char *optarg;
|
||||
extern int getopt();
|
||||
|
||||
/* Application-specific stuff */
|
||||
|
||||
#include "vstring.h"
|
||||
#include "stdarg.h"
|
||||
#include "token.h"
|
||||
#include "error.h"
|
||||
#include "symbol.h"
|
||||
|
||||
/* Forward declarations. */
|
||||
|
||||
static struct token *dcl_flush();
|
||||
static void block_flush();
|
||||
static void block_dcls();
|
||||
static struct token *show_func_ptr_type();
|
||||
static struct token *show_struct_type();
|
||||
static void show_arg_name();
|
||||
static void show_type();
|
||||
static void pair_flush();
|
||||
static void check_cast();
|
||||
static void show_empty_list();
|
||||
|
||||
#define check_cast_flush(t) (check_cast(t), tok_free(t))
|
||||
|
||||
#ifdef PIPE_THROUGH_CPP
|
||||
static int pipe_stdin_through_cpp();
|
||||
#endif
|
||||
|
||||
/* Disable debugging printfs while preserving side effects. */
|
||||
|
||||
#ifdef DEBUG
|
||||
#define DPRINTF printf
|
||||
#else
|
||||
#define DPRINTF (void)
|
||||
#endif
|
||||
|
||||
/* An attempt to make some complicated expressions a bit more readable. */
|
||||
|
||||
#define STREQ(x,y) (*(x) == *(y) && !strcmp((x),(y)))
|
||||
|
||||
#define LAST_ARG_AND_EQUAL(s,c) ((s)->next && (s)->next->next == 0 \
|
||||
&& (s)->head && ((s)->head == (s)->tail) \
|
||||
&& (STREQ((s)->head->vstr->str, (c))))
|
||||
|
||||
#define LIST_BEGINS_WITH_STAR(s) (s->head->head && s->head->head->tokno == '*')
|
||||
|
||||
#define IS_FUNC_PTR_TYPE(s) (s->tokno == TOK_LIST && s->next \
|
||||
&& s->next->tokno == TOK_LIST \
|
||||
&& LIST_BEGINS_WITH_STAR(s))
|
||||
|
||||
/* What to look for to detect a (void) argument list. */
|
||||
|
||||
#ifdef MAP_VOID
|
||||
#define VOID_ARG "int" /* bare "void" is mapped to "int" */
|
||||
#else
|
||||
#define VOID_ARG "void" /* bare "void" is left alone */
|
||||
#endif
|
||||
|
||||
/* main - driver */
|
||||
|
||||
int main(argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
{
|
||||
register struct token *t;
|
||||
#ifdef PIPE_THROUGH_CPP /* pipe through /lib/cpp */
|
||||
int cpp_status;
|
||||
int wait_pid;
|
||||
int cpp_pid;
|
||||
|
||||
cpp_pid = pipe_stdin_through_cpp(argv);
|
||||
#endif
|
||||
|
||||
sym_init(); /* prime the symbol table */
|
||||
|
||||
while (t = tok_class()) {
|
||||
if (t = dcl_flush(t)) { /* try declaration */
|
||||
if (t->tokno == '{') { /* examine rejected token */
|
||||
block_flush(t); /* body */
|
||||
} else {
|
||||
tok_flush(t); /* other, recover */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef PIPE_THROUGH_CPP /* pipe through /lib/cpp */
|
||||
while ((wait_pid = wait(&cpp_status)) != -1 && wait_pid != cpp_pid)
|
||||
/* void */ ;
|
||||
return (errcount != 0 || wait_pid != cpp_pid || cpp_status != 0);
|
||||
#else
|
||||
return (errcount != 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef PIPE_THROUGH_CPP /* pipe through /lib/cpp */
|
||||
|
||||
/* pipe_stdin_through_cpp - avoid shell script overhead */
|
||||
|
||||
static int pipe_stdin_through_cpp(argv)
|
||||
char **argv;
|
||||
{
|
||||
int pipefds[2];
|
||||
int pid;
|
||||
char **cpptr = argv;
|
||||
int i;
|
||||
struct stat st;
|
||||
|
||||
/*
|
||||
* The code that sets up the pipe requires that file descriptors 0,1,2
|
||||
* are already open. All kinds of mysterious things will happen if that
|
||||
* is not the case. The following loops makes sure that descriptors 0,1,2
|
||||
* are set up properly.
|
||||
*/
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
if (fstat(i, &st) == -1 && open("/dev/null", 2) != i) {
|
||||
perror("open /dev/null");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* With most UNIX implementations, the second non-option argument to
|
||||
* /lib/cpp specifies the output file. If an output file other than
|
||||
* stdout is specified, we must force /lib/cpp to write to stdout, and we
|
||||
* must redirect our own standard output to the specified output file.
|
||||
*/
|
||||
|
||||
#define IS_OPTION(cp) ((cp)[0] == '-' && (cp)[1] != 0)
|
||||
|
||||
/* Skip to first non-option argument, if any. */
|
||||
|
||||
while (*++cpptr && IS_OPTION(*cpptr))
|
||||
/* void */ ;
|
||||
|
||||
/*
|
||||
* Assume that the first non-option argument is the input file name. The
|
||||
* next argument could be the output destination or an option (System V
|
||||
* Release 2 /lib/cpp gets the options *after* the file arguments).
|
||||
*/
|
||||
|
||||
if (*cpptr && *++cpptr && **cpptr != '-') {
|
||||
|
||||
/*
|
||||
* The first non-option argument is followed by another argument that
|
||||
* is not an option ("-stuff") or a hyphen ("-"). Redirect our own
|
||||
* standard output before we clobber the file name.
|
||||
*/
|
||||
|
||||
if (freopen(*cpptr, "w", stdout) == 0) {
|
||||
perror(*cpptr);
|
||||
exit(1);
|
||||
}
|
||||
/* Clobber the file name argument so that /lib/cpp writes to stdout */
|
||||
|
||||
*cpptr = "-";
|
||||
}
|
||||
/* Set up the pipe that connects /lib/cpp to our standard input. */
|
||||
|
||||
if (pipe(pipefds)) {
|
||||
perror("pipe");
|
||||
exit(1);
|
||||
}
|
||||
switch (pid = fork()) {
|
||||
case -1: /* error */
|
||||
perror("fork");
|
||||
exit(1);
|
||||
/* NOTREACHED */
|
||||
case 0: /* child */
|
||||
(void) close(pipefds[0]); /* close reading end */
|
||||
(void) close(1); /* connect stdout to pipe */
|
||||
if (dup(pipefds[1]) != 1)
|
||||
fatal("dup() problem");
|
||||
(void) close(pipefds[1]); /* close redundant fd */
|
||||
(void) execv(PIPE_THROUGH_CPP, argv);
|
||||
perror(PIPE_THROUGH_CPP);
|
||||
exit(1);
|
||||
/* NOTREACHED */
|
||||
default: /* parent */
|
||||
(void) close(pipefds[1]); /* close writing end */
|
||||
(void) close(0); /* connect stdin to pipe */
|
||||
if (dup(pipefds[0]) != 0)
|
||||
fatal("dup() problem");
|
||||
close(pipefds[0]); /* close redundant fd */
|
||||
return (pid);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* show_arg_names - display function argument names */
|
||||
|
||||
static void show_arg_names(t)
|
||||
register struct token *t;
|
||||
{
|
||||
register struct token *s;
|
||||
|
||||
/* Do argument names, but suppress void and rewrite trailing ... */
|
||||
|
||||
if (LAST_ARG_AND_EQUAL(t->head, VOID_ARG)) {
|
||||
show_empty_list(t); /* no arguments */
|
||||
} else {
|
||||
for (s = t->head; s; s = s->next) { /* foreach argument... */
|
||||
if (LAST_ARG_AND_EQUAL(s, "...")) {
|
||||
#ifdef _VA_ALIST_ /* see ./stdarg.h */
|
||||
tok_show_ch(s); /* ',' */
|
||||
put_str(_VA_ALIST_); /* varargs magic */
|
||||
#endif
|
||||
} else {
|
||||
tok_show_ch(s); /* '(' or ',' or ')' */
|
||||
show_arg_name(s); /* extract argument name */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* show_arg_types - display function argument types */
|
||||
|
||||
static void show_arg_types(t)
|
||||
register struct token *t;
|
||||
{
|
||||
register struct token *s;
|
||||
|
||||
/* Do argument types, but suppress void and trailing ... */
|
||||
|
||||
if (!LAST_ARG_AND_EQUAL(t->head, VOID_ARG)) {
|
||||
for (s = t->head; s; s = s->next) { /* foreach argument... */
|
||||
if (LAST_ARG_AND_EQUAL(s, "...")) {
|
||||
#ifdef _VA_DCL_ /* see ./stdarg.h */
|
||||
put_str(_VA_DCL_); /* varargs magic */
|
||||
put_nl(); /* make output look nicer */
|
||||
#endif
|
||||
} else {
|
||||
if (s->head != s->tail) { /* really new-style argument? */
|
||||
show_type(s); /* rewrite type info */
|
||||
put_ch(';');
|
||||
put_nl(); /* make output look nicer */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* header_flush - rewrite new-style function heading to old style */
|
||||
|
||||
static void header_flush(t)
|
||||
register struct token *t;
|
||||
{
|
||||
show_arg_names(t); /* show argument names */
|
||||
put_nl(); /* make output look nicer */
|
||||
show_arg_types(t); /* show argument types */
|
||||
tok_free(t); /* discard token */
|
||||
}
|
||||
|
||||
/* fpf_header_names - define func returning ptr to func, no argument types */
|
||||
|
||||
static void fpf_header_names(list)
|
||||
struct token *list;
|
||||
{
|
||||
register struct token *s;
|
||||
register struct token *p;
|
||||
|
||||
/*
|
||||
* Recurse until we find the argument list. Account for the rare case
|
||||
* that list is a comma-separated list (which should be a syntax error).
|
||||
* Display old-style fuction argument names.
|
||||
*/
|
||||
|
||||
for (s = list->head; s; s = s->next) {
|
||||
tok_show_ch(s); /* '(' or ',' or ')' */
|
||||
for (p = s->head; p; p = p->next) {
|
||||
if (p->tokno == TOK_LIST) {
|
||||
if (IS_FUNC_PTR_TYPE(p)) { /* recurse */
|
||||
fpf_header_names(p);
|
||||
show_empty_list(p = p->next);
|
||||
} else { /* display argument names */
|
||||
show_arg_names(p);
|
||||
}
|
||||
} else { /* pass through other stuff */
|
||||
tok_show(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* fpf_header_types - define func returning ptr to func, argument types only */
|
||||
|
||||
static void fpf_header_types(list)
|
||||
struct token *list;
|
||||
{
|
||||
register struct token *s;
|
||||
register struct token *p;
|
||||
|
||||
/*
|
||||
* Recurse until we find the argument list. Account for the rare case
|
||||
* that list is a comma-separated list (which should be a syntax error).
|
||||
* Display old-style function argument types.
|
||||
*/
|
||||
|
||||
for (s = list->head; s; s = s->next) {
|
||||
for (p = s->head; p; p = p->next) {
|
||||
if (p->tokno == TOK_LIST) {
|
||||
if (IS_FUNC_PTR_TYPE(p)) { /* recurse */
|
||||
fpf_header_types(p);
|
||||
p = p->next;
|
||||
} else { /* display argument types */
|
||||
show_arg_types(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* fpf_header - define function returning pointer to function */
|
||||
|
||||
static void fpf_header(l1, l2)
|
||||
struct token *l1;
|
||||
struct token *l2;
|
||||
{
|
||||
fpf_header_names(l1); /* strip argument types */
|
||||
show_empty_list(l2); /* strip prototype */
|
||||
put_nl(); /* nicer output */
|
||||
fpf_header_types(l1); /* show argument types */
|
||||
}
|
||||
|
||||
/* skip_enclosed - skip over enclosed tokens */
|
||||
|
||||
static struct token *skip_enclosed(p, stop)
|
||||
register struct token *p;
|
||||
register int stop;
|
||||
{
|
||||
register int start = p->tokno;
|
||||
|
||||
/* Always return a pointer to the last processed token, never NULL. */
|
||||
|
||||
while (p->next) {
|
||||
p = p->next;
|
||||
if (p->tokno == start) {
|
||||
p = skip_enclosed(p, stop); /* recurse */
|
||||
} else if (p->tokno == stop) {
|
||||
break; /* done */
|
||||
}
|
||||
}
|
||||
return (p);
|
||||
}
|
||||
|
||||
/* show_arg_name - extract argument name from argument type info */
|
||||
|
||||
static void show_arg_name(s)
|
||||
register struct token *s;
|
||||
{
|
||||
if (s->head) {
|
||||
register struct token *p;
|
||||
register struct token *t = 0;
|
||||
|
||||
/* Find the last interesting item. */
|
||||
|
||||
for (p = s->head; p; p = p->next) {
|
||||
if (p->tokno == TOK_WORD) {
|
||||
t = p; /* remember last word */
|
||||
} else if (p->tokno == '{') {
|
||||
p = skip_enclosed(p, '}'); /* skip structured stuff */
|
||||
} else if (p->tokno == '[') {
|
||||
break; /* dimension may be a macro */
|
||||
} else if (IS_FUNC_PTR_TYPE(p)) {
|
||||
t = p; /* or function pointer */
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
|
||||
/* Extract argument name from last interesting item. */
|
||||
|
||||
if (t) {
|
||||
if (t->tokno == TOK_LIST)
|
||||
show_arg_name(t->head); /* function pointer, recurse */
|
||||
else
|
||||
tok_show(t); /* print last word */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* show_type - rewrite type to old-style syntax */
|
||||
|
||||
static void show_type(s)
|
||||
register struct token *s;
|
||||
{
|
||||
register struct token *p;
|
||||
|
||||
/*
|
||||
* Rewrite (*stuff)(args) to (*stuff)(). Rewrite word(args) to word(),
|
||||
* but only if the word was preceded by a word, '*' or '}'. Leave
|
||||
* anything else alone.
|
||||
*/
|
||||
|
||||
for (p = s->head; p; p = p->next) {
|
||||
if (IS_FUNC_PTR_TYPE(p)) {
|
||||
p = show_func_ptr_type(p, p->next); /* function pointer type */
|
||||
} else {
|
||||
register struct token *q;
|
||||
register struct token *r;
|
||||
|
||||
tok_show(p); /* other */
|
||||
if ((p->tokno == TOK_WORD || p->tokno == '*' || p->tokno == '}')
|
||||
&& (q = p->next) && q->tokno == TOK_WORD
|
||||
&& (r = q->next) && r->tokno == TOK_LIST) {
|
||||
tok_show(q); /* show name */
|
||||
show_empty_list(p = r); /* strip args */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* show_func_ptr_type - display function_pointer type using old-style syntax */
|
||||
|
||||
static struct token *show_func_ptr_type(t1, t2)
|
||||
struct token *t1;
|
||||
struct token *t2;
|
||||
{
|
||||
register struct token *s;
|
||||
|
||||
/*
|
||||
* Rewrite (list1) (list2) to (list1) (). Account for the rare case that
|
||||
* (list1) is a comma-separated list. That should be an error, but we do
|
||||
* not want to waste any information.
|
||||
*/
|
||||
|
||||
for (s = t1->head; s; s = s->next) {
|
||||
tok_show_ch(s); /* '(' or ',' or ')' */
|
||||
show_type(s); /* recurse */
|
||||
}
|
||||
show_empty_list(t2);
|
||||
return (t2);
|
||||
}
|
||||
|
||||
/* show_empty_list - display opening and closing parentheses (if available) */
|
||||
|
||||
static void show_empty_list(t)
|
||||
register struct token *t;
|
||||
{
|
||||
tok_show_ch(t->head); /* opening paren */
|
||||
if (t->tail->tokno == ')')
|
||||
tok_show_ch(t->tail); /* closing paren */
|
||||
}
|
||||
|
||||
/* show_struct_type - display structured type, rewrite function-pointer types */
|
||||
|
||||
static struct token *show_struct_type(p)
|
||||
register struct token *p;
|
||||
{
|
||||
tok_show(p); /* opening brace */
|
||||
|
||||
while (p->next) { /* XXX cannot return 0 */
|
||||
p = p->next;
|
||||
if (IS_FUNC_PTR_TYPE(p)) {
|
||||
p = show_func_ptr_type(p, p->next); /* function-pointer member */
|
||||
} else if (p->tokno == '{') {
|
||||
p = show_struct_type(p); /* recurse */
|
||||
} else {
|
||||
tok_show(p); /* other */
|
||||
if (p->tokno == '}') {
|
||||
return (p); /* done */
|
||||
}
|
||||
}
|
||||
}
|
||||
DPRINTF("/* missing '}' */");
|
||||
return (p);
|
||||
}
|
||||
|
||||
/* is_func_ptr_cast - recognize function-pointer type cast */
|
||||
|
||||
static int is_func_ptr_cast(t)
|
||||
register struct token *t;
|
||||
{
|
||||
register struct token *p;
|
||||
|
||||
/*
|
||||
* Examine superficial structure. Require (list1) (list2). Require that
|
||||
* list1 begins with a star.
|
||||
*/
|
||||
|
||||
if (!IS_FUNC_PTR_TYPE(t))
|
||||
return (0);
|
||||
|
||||
/*
|
||||
* Make sure that there is no name in (list1). Do not worry about
|
||||
* unexpected tokens, because the compiler will complain anyway.
|
||||
*/
|
||||
|
||||
for (p = t->head->head; p; p = p->next) {
|
||||
switch (p->tokno) {
|
||||
case TOK_LIST: /* recurse */
|
||||
return (is_func_ptr_cast(p));
|
||||
case TOK_WORD: /* name in list */
|
||||
return (0);
|
||||
case '[':
|
||||
return (1); /* dimension may be a macro */
|
||||
}
|
||||
}
|
||||
return (1); /* no name found */
|
||||
}
|
||||
|
||||
/* check_cast - display ()-delimited, comma-separated list */
|
||||
|
||||
static void check_cast(t)
|
||||
struct token *t;
|
||||
{
|
||||
register struct token *s;
|
||||
register struct token *p;
|
||||
|
||||
/*
|
||||
* Rewrite function-pointer types and function-pointer casts. Do not
|
||||
* blindly rewrite (*list1)(list2) to (*list1)(). Function argument lists
|
||||
* are about the only thing we can discard without provoking diagnostics
|
||||
* from the compiler.
|
||||
*/
|
||||
|
||||
for (s = t->head; s; s = s->next) {
|
||||
tok_show_ch(s); /* '(' or ',' or ')' */
|
||||
for (p = s->head; p; p = p->next) {
|
||||
switch (p->tokno) {
|
||||
case TOK_LIST:
|
||||
if (is_func_ptr_cast(p)) { /* not: IS_FUNC_PTR_TYPE(p) */
|
||||
p = show_func_ptr_type(p, p->next);
|
||||
} else {
|
||||
check_cast(p); /* recurse */
|
||||
}
|
||||
break;
|
||||
case '{':
|
||||
p = show_struct_type(p); /* rewrite func. ptr. types */
|
||||
break;
|
||||
default:
|
||||
tok_show(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* block_dcls - on the fly rewrite decls/initializers at start of block */
|
||||
|
||||
static void block_dcls()
|
||||
{
|
||||
register struct token *t;
|
||||
|
||||
/*
|
||||
* Away from the top level, a declaration should be preceded by type or
|
||||
* storage-class information. That is why inside blocks, structs and
|
||||
* unions we insist on reading one word before passing the _next_ token
|
||||
* to the dcl_flush() function.
|
||||
*
|
||||
* Struct and union declarations look the same everywhere: we make an
|
||||
* exception for these more regular constructs and pass the "struct" and
|
||||
* "union" tokens to the type_dcl() function.
|
||||
*/
|
||||
|
||||
while (t = tok_class()) {
|
||||
switch (t->tokno) {
|
||||
case TOK_WSPACE: /* preserve white space */
|
||||
case '\n': /* preserve line count */
|
||||
tok_flush(t);
|
||||
break;
|
||||
case TOK_WORD: /* type declarations? */
|
||||
tok_flush(t); /* advance to next token */
|
||||
t = tok_class(); /* null return is ok */
|
||||
/* FALLTRHOUGH */
|
||||
case TOK_COMPOSITE: /* struct or union */
|
||||
if ((t = dcl_flush(t)) == 0)
|
||||
break;
|
||||
/* FALLTRHOUGH */
|
||||
default: /* end of declarations */
|
||||
DPRINTF("/* end dcls */");
|
||||
/* FALLTRHOUGH */
|
||||
case '}': /* end of block */
|
||||
tok_unget(t);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* block_flush - rewrite struct, union or statement block on the fly */
|
||||
|
||||
static void block_flush(t)
|
||||
register struct token *t;
|
||||
{
|
||||
static int count = 0;
|
||||
|
||||
tok_flush(t);
|
||||
DPRINTF("/*%d*/", ++count);
|
||||
|
||||
/*
|
||||
* Rewrite function pointer types in declarations and function pointer
|
||||
* casts in initializers at start of block.
|
||||
*/
|
||||
|
||||
block_dcls();
|
||||
|
||||
/* Remainder of block: only rewrite function pointer casts. */
|
||||
|
||||
while (t = tok_class()) {
|
||||
if (t->tokno == TOK_LIST) {
|
||||
check_cast_flush(t);
|
||||
} else if (t->tokno == '{') {
|
||||
block_flush(t);
|
||||
} else {
|
||||
tok_flush(t);
|
||||
if (t->tokno == '}') {
|
||||
DPRINTF("/*%d*/", count--);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
DPRINTF("/* missing '}' */");
|
||||
}
|
||||
|
||||
/* pair_flush - on the fly rewrite casts in grouped stuff */
|
||||
|
||||
static void pair_flush(t, start, stop)
|
||||
register struct token *t;
|
||||
register int start;
|
||||
register int stop;
|
||||
{
|
||||
tok_flush(t);
|
||||
|
||||
while (t = tok_class()) {
|
||||
if (t->tokno == start) { /* recurse */
|
||||
pair_flush(t, start, stop);
|
||||
} else if (t->tokno == TOK_LIST) { /* expression or cast */
|
||||
check_cast_flush(t);
|
||||
} else { /* other, copy */
|
||||
tok_flush(t);
|
||||
if (t->tokno == stop) { /* done */
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
DPRINTF("/* missing '%c' */", stop);
|
||||
}
|
||||
|
||||
/* initializer - on the fly rewrite casts in initializer */
|
||||
|
||||
static void initializer()
|
||||
{
|
||||
register struct token *t;
|
||||
|
||||
while (t = tok_class()) {
|
||||
switch (t->tokno) {
|
||||
case ',': /* list separator */
|
||||
case ';': /* list terminator */
|
||||
tok_unget(t);
|
||||
return;
|
||||
case TOK_LIST: /* expression or cast */
|
||||
check_cast_flush(t);
|
||||
break;
|
||||
case '[': /* array subscript, may nest */
|
||||
pair_flush(t, '[', ']');
|
||||
break;
|
||||
case '{': /* structured data, may nest */
|
||||
pair_flush(t, '{', '}');
|
||||
break;
|
||||
default: /* other, just copy */
|
||||
tok_flush(t);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* func_ptr_dcl_flush - rewrite function pointer stuff */
|
||||
|
||||
static struct token *func_ptr_dcl_flush(list)
|
||||
register struct token *list;
|
||||
{
|
||||
register struct token *t;
|
||||
register struct token *t2;
|
||||
|
||||
/*
|
||||
* Ignore blanks and newlines because we are too lazy to maintain more
|
||||
* than one token worth of lookahead. The output routines will regenerate
|
||||
* discarded newline tokens.
|
||||
*/
|
||||
|
||||
while (t = tok_class()) {
|
||||
switch (t->tokno) {
|
||||
case TOK_WSPACE:
|
||||
case '\n':
|
||||
tok_free(t);
|
||||
break;
|
||||
case TOK_LIST:
|
||||
/* Function pointer or function returning pointer to function. */
|
||||
while ((t2 = tok_class()) /* skip blanks etc. */
|
||||
&&(t2->tokno == TOK_WSPACE || t2->tokno == '\n'))
|
||||
tok_free(t2);
|
||||
switch (t2 ? t2->tokno : 0) {
|
||||
case '{': /* function heading (new) */
|
||||
fpf_header(list, t);
|
||||
break;
|
||||
case TOK_WORD: /* function heading (old) */
|
||||
tok_show(list);
|
||||
tok_show(t);
|
||||
break;
|
||||
default: /* func pointer type */
|
||||
(void) show_func_ptr_type(list, t);
|
||||
break;
|
||||
}
|
||||
tok_free(list);
|
||||
tok_free(t);
|
||||
if (t2)
|
||||
tok_unget(t2);
|
||||
return (0);
|
||||
default: /* not a declaration */
|
||||
tok_unget(t);
|
||||
return (list);
|
||||
}
|
||||
}
|
||||
|
||||
/* Hit EOF; must be mistake, but do not waste any information. */
|
||||
|
||||
return (list);
|
||||
}
|
||||
|
||||
/* function_dcl_flush - rewrite function { heading, type declaration } */
|
||||
|
||||
static struct token *function_dcl_flush(list)
|
||||
register struct token *list;
|
||||
{
|
||||
register struct token *t;
|
||||
|
||||
/*
|
||||
* Ignore blanks and newlines because we are too lazy to maintain more
|
||||
* than one token worth of lookahead. The output routines will regenerate
|
||||
* ignored newline tokens.
|
||||
*/
|
||||
|
||||
while (t = tok_class()) {
|
||||
switch (t->tokno) {
|
||||
case TOK_WSPACE:
|
||||
case '\n':
|
||||
tok_free(t);
|
||||
break;
|
||||
case '{':
|
||||
/* Function heading: word (list) { -> old style heading */
|
||||
header_flush(list);
|
||||
tok_unget(t);
|
||||
return (0);
|
||||
case TOK_WORD:
|
||||
/* Old-style function heading: word (list) word... */
|
||||
tok_flush(list);
|
||||
tok_unget(t);
|
||||
return (0);
|
||||
case TOK_LIST:
|
||||
/* Function pointer: word (list1) (list2) -> word (list1) () */
|
||||
tok_flush(list);
|
||||
show_empty_list(t);
|
||||
tok_free(t);
|
||||
return (0);
|
||||
case ',':
|
||||
case ';':
|
||||
/* Function type declaration: word (list) -> word () */
|
||||
show_empty_list(list);
|
||||
tok_free(list);
|
||||
tok_unget(t);
|
||||
return (0);
|
||||
default:
|
||||
/* Something else, reject the list. */
|
||||
tok_unget(t);
|
||||
return (list);
|
||||
}
|
||||
}
|
||||
|
||||
/* Hit EOF; must be mistake, but do not waste any information. */
|
||||
|
||||
return (list);
|
||||
}
|
||||
|
||||
/* dcl_flush - parse declaration on the fly, return rejected token */
|
||||
|
||||
static struct token *dcl_flush(t)
|
||||
register struct token *t;
|
||||
{
|
||||
register int got_word;
|
||||
|
||||
/*
|
||||
* Away from the top level, type or storage-class information is required
|
||||
* for an (extern or forward) function type declaration or a variable
|
||||
* declaration.
|
||||
*
|
||||
* With our naive word-counting approach, this means that the caller should
|
||||
* read one word before passing the next token to us. This is how we
|
||||
* distinguish, for example, function declarations from function calls.
|
||||
*
|
||||
* An exception are structs and unions, because they look the same at any
|
||||
* level. The caller should give is the "struct" or "union" token.
|
||||
*/
|
||||
|
||||
for (got_word = 0; t; t = tok_class()) {
|
||||
switch (t->tokno) {
|
||||
case TOK_WSPACE: /* advance past blanks */
|
||||
case '\n': /* advance past newline */
|
||||
case '*': /* indirection: keep trying */
|
||||
tok_flush(t);
|
||||
break;
|
||||
case TOK_WORD: /* word: keep trying */
|
||||
case TOK_COMPOSITE: /* struct or union */
|
||||
got_word = 1;
|
||||
tok_flush(t);
|
||||
break;
|
||||
default:
|
||||
|
||||
/*
|
||||
* Function pointer types can be preceded by zero or more words
|
||||
* (at least one when not at the top level). Other stuff can be
|
||||
* accepted only after we have seen at least one word (two words
|
||||
* when not at the top level). See also the above comment on
|
||||
* structs and unions.
|
||||
*/
|
||||
|
||||
if (t->tokno == TOK_LIST && LIST_BEGINS_WITH_STAR(t)) {
|
||||
if (t = func_ptr_dcl_flush(t)) {
|
||||
return (t); /* reject token */
|
||||
} else {
|
||||
got_word = 1; /* for = and [ and , and ; */
|
||||
}
|
||||
} else if (got_word == 0) {
|
||||
return (t); /* reject token */
|
||||
} else {
|
||||
switch (t->tokno) {
|
||||
case TOK_LIST: /* function type */
|
||||
if (t = function_dcl_flush(t))
|
||||
return (t); /* reject token */
|
||||
break;
|
||||
case '[': /* dimension, does not nest */
|
||||
pair_flush(t, '[', ']');
|
||||
break;
|
||||
case '=': /* initializer follows */
|
||||
tok_flush(t);
|
||||
initializer(); /* rewrite casts */
|
||||
break;
|
||||
case '{': /* struct, union, may nest */
|
||||
block_flush(t); /* use code for stmt blocks */
|
||||
break;
|
||||
case ',': /* separator: keep trying */
|
||||
got_word = 0;
|
||||
tok_flush(t);
|
||||
break;
|
||||
case ';': /* terminator: succeed */
|
||||
tok_flush(t);
|
||||
return (0);
|
||||
default: /* reject token */
|
||||
return (t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (0); /* hit EOF */
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
/*
|
||||
* @(#) varargs.c 1.1 91/09/01 23:08:45
|
||||
*
|
||||
* This program can be used to verify that the stdarg.h file is set up
|
||||
* correctly for your system. If it works, it should print one line with the
|
||||
* text "stdarg.h works".
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "stdarg.h"
|
||||
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
varargs_test("%s %s\n", "stdarg.h", "works");
|
||||
}
|
||||
|
||||
varargs_test(char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
while (*fmt) {
|
||||
if (strncmp("%s", fmt, 2) == 0) {
|
||||
fputs(va_arg(ap, char *), stdout);
|
||||
fmt += 2;
|
||||
} else {
|
||||
putchar(*fmt);
|
||||
fmt++;
|
||||
}
|
||||
}
|
||||
va_end(ap);
|
||||
}
|
||||
|
|
@ -1,122 +0,0 @@
|
|||
/*++
|
||||
/* NAME
|
||||
/* vs_alloc(), VS_ADDCH()
|
||||
/* SUMMARY
|
||||
/* auto-resizing string library
|
||||
/* PACKAGE
|
||||
/* vstring
|
||||
/* SYNOPSIS
|
||||
/* #include "vstring.h"
|
||||
/*
|
||||
/* struct vstring *vs_alloc(len)
|
||||
/* int len;
|
||||
/*
|
||||
/* int VS_ADDCH(vs, wp, ch)
|
||||
/* struct vstring *vs;
|
||||
/* char *wp;
|
||||
/* int ch;
|
||||
/*
|
||||
/* char *vs_strcpy(vp, dst, src)
|
||||
/* struct vstring *vp;
|
||||
/* char *dst;
|
||||
/* char *src;
|
||||
/* DESCRIPTION
|
||||
/* These functions and macros implement a small library for
|
||||
/* arbitrary-length strings that grow automatically when
|
||||
/* they fill up. The allocation strategy is such that there
|
||||
/* will always be place for the terminating null character.
|
||||
/*
|
||||
/* vs_alloc() allocates storage for a variable-length string
|
||||
/* of at least "len" bytes.
|
||||
/*
|
||||
/* VS_ADDCH() adds a character to a variable-length string
|
||||
/* and automagically extends the string if fills up.
|
||||
/* \fIvs\fP is a pointer to a vstring structure; \fIwp\fP
|
||||
/* the current write position in the corresponding character
|
||||
/* array; \fIch\fP the character value to be written.
|
||||
/* Note that VS_ADDCH() is a macro that evaluates some
|
||||
/* arguments more than once.
|
||||
/*
|
||||
/* vs_strcpy() appends a null-terminated string to a variable-length
|
||||
/* string. \fIsrc\fP provides the data to be copied; \fIvp\fP is the
|
||||
/* target, and \fIdst\fP the current write position within the target.
|
||||
/* The result is null-terminated. The return value is the new write
|
||||
/* position.
|
||||
/* DIAGNOSTICS
|
||||
/* VS_ADDCH() returns zero if it was unable to dynamically
|
||||
/* resize a string.
|
||||
/*
|
||||
/* vs_alloc() returns a null pointer in case of problems.
|
||||
/*
|
||||
/* vs_strcpy() returns a null pointer if the request failed.
|
||||
/* BUGS
|
||||
/* Auto-resizing may change the address of the string data in
|
||||
/* a vstring structure. Beware of dangling pointers.
|
||||
/* AUTHOR(S)
|
||||
/* Wietse Venema
|
||||
/* Eindhoven University of Technology
|
||||
/* Department of Mathematics and Computer Science
|
||||
/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
|
||||
/* LAST MODIFICATION
|
||||
/* 92/01/15 21:53:06
|
||||
/* VERSION/RELEASE
|
||||
/* 1.3
|
||||
/*--*/
|
||||
|
||||
static char vstring_sccsid[] = "@(#) vstring.c 1.3 92/01/15 21:53:06";
|
||||
|
||||
/* C library */
|
||||
|
||||
extern char *malloc();
|
||||
extern char *realloc();
|
||||
|
||||
/* Application-specific stuff */
|
||||
|
||||
#include "vstring.h"
|
||||
|
||||
/* vs_alloc - initial string allocation */
|
||||
|
||||
struct vstring *vs_alloc(len)
|
||||
int len;
|
||||
{
|
||||
register struct vstring *vp;
|
||||
|
||||
if (len < 1
|
||||
|| (vp = (struct vstring *) malloc(sizeof(struct vstring))) == 0
|
||||
|| (vp->str = malloc(len)) == 0)
|
||||
return (0);
|
||||
vp->last = vp->str + len - 1;
|
||||
return (vp);
|
||||
}
|
||||
|
||||
/* vs_realloc - extend string, update write pointer */
|
||||
|
||||
char *vs_realloc(vp, cp)
|
||||
register struct vstring *vp;
|
||||
char *cp;
|
||||
{
|
||||
int where = cp - vp->str;
|
||||
int len = vp->last - vp->str + 1;
|
||||
|
||||
if ((vp->str = realloc(vp->str, len *= 2)) == 0)
|
||||
return (0);
|
||||
vp->last = vp->str + len - 1;
|
||||
return (vp->str + where);
|
||||
}
|
||||
|
||||
/* vs_strcpy - copy string */
|
||||
|
||||
char *vs_strcpy(vp, dst, src)
|
||||
register struct vstring *vp;
|
||||
register char *dst;
|
||||
register char *src;
|
||||
{
|
||||
while (*src) {
|
||||
if (VS_ADDCH(vp, dst, *src) == 0)
|
||||
return (0);
|
||||
src++;
|
||||
}
|
||||
*dst = '\0';
|
||||
return (dst);
|
||||
}
|
||||
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
/* @(#) vstring.h 1.2 92/01/15 21:53:19 */
|
||||
|
||||
struct vstring {
|
||||
char *str; /* string value */
|
||||
char *last; /* last position */
|
||||
};
|
||||
|
||||
extern struct vstring *vs_alloc(); /* initial allocation */
|
||||
extern char *vs_realloc(); /* string extension */
|
||||
extern char *vs_strcpy(); /* copy string */
|
||||
|
||||
/* macro to add one character to auto-resized string */
|
||||
|
||||
#define VS_ADDCH(vs,wp,c) \
|
||||
((wp < (vs)->last || (wp = vs_realloc(vs,wp))) ? (*wp++ = c) : 0)
|
||||
Loading…
Reference in a new issue