home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 2
/
FFMCD02.bin
/
new
/
dev
/
misc
/
p2c
/
src
/
makeproto.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-12-21
|
16KB
|
509 lines
/* "makeproto" Copyright 1989, 1990, 1991 Free Software Foundation */
/* Program to scan old-style source files and make prototypes */
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#ifdef FILE /* a #define in BSD, a typedef in SYSV (hp-ux, at least) */
# ifndef BSD
# define BSD 1
# endif
#endif
#ifdef BSD
# include <strings.h>
#else
# include <string.h>
#endif
#define isidchar(x) (isalnum(x) || (x) == '_')
#define dprintf if (!debug) ; else printf
#define MAXARGS 16
int verbose, debug, incomment;
struct warnstruct {
char *bad, *good;
} warntypes[] = {
{ "char", "int" },
{ "signed char", "int" },
{ "unsigned char", "int" },
{ "short", "int" },
{ "signed short", "int" },
{ "unsigned short", "int" },
{ "boolean", "int" },
{ "Boolean", "int" },
{ "float", "double" },
{ NULL, NULL }
} ;
int readline(buf, inf)
char *buf;
FILE *inf;
{
char *cp, *cp2;
int spflag;
for (;;) {
if (fgets(buf, 1000, inf)) {
cp = buf;
cp2 = buf;
spflag = 0;
while (*cp) {
if (incomment) {
if (cp[0] == '*' && cp[1] == '/') {
incomment = 0;
cp += 2;
} else
cp++;
spflag = 1;
} else {
if (cp[0] == '/' && cp[1] == '*') {
incomment = 1;
cp += 2;
} else if (isspace(*cp)) {
spflag = 1;
cp++;
} else {
if (spflag)
*cp2++ = ' ';
*cp2++ = *cp++;
spflag = 0;
}
}
}
*cp2 = 0;
if (!*buf)
continue;
if (verbose)
printf("\217%s\210\n", buf);
return 1;
} else
strcpy(buf, "\001");
return 0;
}
}
int strbeginsword(s1, s2)
register char *s1, *s2;
{
while (*s2 && *s1 == *s2)
s1++, s2++;
return (!*s2 && !isidchar(*s1));
}
void usage()
{
fprintf(stderr, "usage: makeproto [options] [infile ...] [-o outfile]]\n");
fprintf(stderr, " -tnnn Tab to nnn after type name [default 15]\n");
fprintf(stderr, " -annn Tab to nnn before arguments [default 30]\n");
fprintf(stderr, " -s0 Omit functions declared static\n");
fprintf(stderr, " -s1 Omit functions not declared static\n");
fprintf(stderr, " -x Add \"extern\" keyword (-X => \"Extern\")\n");
fprintf(stderr, " -n Include argument names in prototypes\n");
fprintf(stderr, " -m Use PP/PV macro notation\n");
#ifdef MCH_AMIGA
exit(20);
#else
exit(1);
#endif
}
#define bounce(msg) do { if (verbose) printf("Bounced: %s\n", msg); if (stupid) goto Lbounce; } while (0)
main(argc, argv)
int argc;
char **argv;
{
FILE *inf, *outf;
char outfname[256];
char buf[1000], ifdefname[256];
char ftype[256], fname[80], dtype[256], decl[256], dname[80], temp[256];
char argdecls[MAXARGS][256], argnames[MAXARGS][80];
char *cp, *cp2, *cp3;
int i, j, pos, len, thistab, numstars, whichf, nargs, incomment, errors = 0;
long li;
int typetab = 15, argtab = 30, width = 80, usenames = 0, usemacros = 0;
int useextern = 0, staticness = -1, hasheader = 0, useifdefs = 0;
int stupid = 1, firstdecl;
errors = 0;
verbose = 0;
debug = 0;
*outfname = 0;
while (argc > 1 && argv[1][0] == '-') {
if (argv[1][1] == 't') {
typetab = atoi(argv[1] + 2);
} else if (argv[1][1] == 'a') {
argtab = atoi(argv[1] + 2);
} else if (argv[1][1] == 'w') {
width = atoi(argv[1] + 2);
} else if (argv[1][1] == 's') {
staticness = atoi(argv[1] + 2);
} else if (argv[1][1] == 'v') {
verbose = 1;
} else if (argv[1][1] == 'D') {
debug = 1;
} else if (argv[1][1] == 'x') {
useextern = 1;
} else if (argv[1][1] == 'X') {
useextern = 2;
} else if (argv[1][1] == 'n') {
usenames = 1;
} else if (argv[1][1] == 'm') {
usemacros = 1;
} else if (argv[1][1] == 'h') {
hasheader = 1;
} else if (argv[1][1] == 'i') {
useifdefs = 1;
} else if (argv[1][1] == 'o' && argc > 2) {
strcpy(outfname, argv[2]);
argc--, argv++;
} else {
usage();
}
argc--, argv++;
}
if (argc > 2 && !strcmp(argv[argc-2], "-o")) {
strcpy(outfname, argv[argc-1]);
argc -= 2;
}
if (*outfname) {
outf = fopen(outfname, "w");
if (!outf) {
perror(outfname);
#ifdef MCH_AMIGA
exit(20);
#else
exit(1);
#endif
}
} else
outf = stdout;
if (hasheader) {
time(&li);
cp = ctime(&li);
cp[24] = 0;
fprintf(outf, "\n/* Declarations created by \"makeproto\" on %s */\n", cp);
fprintf(outf, "\n\n");
}
incomment = 0;
for (whichf = 1; whichf < argc + (argc < 2); whichf++) {
if (whichf >= argc || !strcmp(argv[whichf], "-")) {
inf = stdin;
} else {
inf = fopen(argv[whichf], "r");
if (!inf) {
perror(argv[whichf]);
fprintf(outf, "\n/* Unable to open file %s */\n", argv[whichf]);
errors++;
continue;
}
}
firstdecl = 1;
while (readline(buf, inf)) {
if (!isidchar(*buf))
continue;
cp = buf;
cp2 = ftype;
numstars = 0;
while (isspace(*cp) || isidchar(*cp))
*cp2++ = *cp++;
if (*cp == '*') {
while (*cp == '*' || isspace(*cp)) {
if (*cp == '*')
numstars++;
cp++;
}
} else {
while (cp > buf && isspace(cp[-1])) cp--, cp2--;
while (cp > buf && isidchar(cp[-1])) cp--, cp2--;
}
while (cp2 > ftype && isspace(cp2[-1])) cp2--;
*cp2 = 0;
if (!*ftype)
strcpy(ftype, "int");
dprintf("numstars is %d\n", numstars); /***/
dprintf("ftype is %s\n", ftype); /***/
dprintf("cp after ftype is %s\n", cp); /***/
if (strbeginsword(ftype, "static") || strbeginsword(ftype, "Static")) {
if (staticness == 0)
bounce("Function is static");
} else {
if (staticness == 1)
bounce("Function is not static");
if (useextern &&
!strbeginsword(ftype, "extern") && !strbeginsword(ftype, "Extern")) {
sprintf(temp, useextern == 2 ? "Extern %s" : "extern %s", ftype);
strcpy(ftype, temp);
}
}
while (isspace(*cp)) cp++;
if (!*cp) {
readline(buf, inf);
cp = buf;
}
dprintf("cp before fname is %s\n", cp); /***/
if (!isidchar(*cp))
bounce("No function name");
cp2 = fname;
while (isidchar(*cp))
*cp2++= *cp++;
*cp2 = 0;
dprintf("fname is %s\n", fname); /***/
dprintf("cp after fname is %s\n", cp); /***/
while (isspace(*cp)) cp++;
if (*cp++ != '(')
bounce("No function '('");
nargs = 0;
if (!*cp) {
readline(buf, inf);
cp = buf;
}
while (isspace(*cp)) cp++;
wh