5965fd777b
* Works fine under Debian GNU/Linux 7.4, NetBSD/amd64 6.99.36 * Merge pkgsrc specific changes Changelog: Dec 20, 2012: fiddled makefile to get correct yacc and bison flags. pick yacc (linux) or bison (mac) as necessary. added __attribute__((__noreturn__)) to a couple of lines in proto.h, to silence someone's enthusiastic checker. fixed obscure call by value bug in split(a[1],a) reported on 9fans. the management of temporary values is just a mess; i took a shortcut by making an extra string copy. thanks to paul patience and arnold robbins for passing it on and for proposed patches. tiny fiddle in setfval to eliminate -0 results in T.expr, which has irritated me for 20+ years. Aug 10, 2011: another fix to avoid core dump with delete(ARGV); again, many thanks to ruslan ermilov. Aug 7, 2011: split(s, a, //) now behaves the same as split(s, a, "") Jun 12, 2011: /pat/, \n /pat/ {...} is now legal, though bad style to use. added checks to new -v code that permits -vnospace; thanks to ruslan ermilov for spotting this and providing the patch. removed fixed limit on number of open files; thanks to aleksey cheusov and christos zoulos. fixed day 1 bug that resurrected deleted elements of ARGV when used as filenames (in lib.c). minor type fiddles to make gcc -Wall -pedantic happier (but not totally so); turned on -fno-strict-aliasing in makefile. May 6, 2011: added #ifdef for isblank. now allows -ffoo as well as -f foo arguments. (thanks, ruslan) May 1, 2011: after advice from todd miller, kevin lo, ruslan ermilov, and arnold robbins, changed srand() to return the previous seed (which is 1 on the first call of srand). the seed is an Awkfloat internally though converted to unsigned int to pass to the library srand(). thanks, everyone. fixed a subtle (and i hope low-probability) overflow error in fldbld, by adding space for one extra \0. thanks to robert bassett for spotting this one and providing a fix. removed the files related to compilation on windows. i no longer have anything like a current windows environment, so i can't test any of it. May 23, 2010: fixed long-standing overflow bug in run.c; many thanks to nelson beebe for spotting it and providing the fix. fixed bug that didn't parse -vd=1 properly; thanks to santiago vila for spotting it. Feb 8, 2010: i give up. replaced isblank with isspace in b.c; there are no consistent header files. Nov 26, 2009: fixed a long-standing issue with when FS takes effect. a change to FS is now noticed immediately for subsequent splits. changed the name getline() to awkgetline() to avoid yet another name conflict somewhere. Feb 11, 2009: temporarily for now defined HAS_ISBLANK, since that seems to be the best way through the thicket. isblank arrived in C99, but seems to be arriving at different systems at different times. Oct 8, 2008: fixed typo in b.c that set tmpvec wrongly. no one had ever run into the problem, apparently. thanks to alistair crooks. Oct 23, 2007: minor fix in lib.c: increase inputFS to 100, change malloc for fields to n+1. fixed memory fault caused by out of order test in setsval. thanks to david o'brien, freebsd, for both fixes. May 1, 2007: fiddle in makefile to fix for BSD make; thanks to igor sobrado. Mar 31, 2007: fixed some null pointer refs calling adjbuf. Feb 21, 2007: fixed a bug in matching the null RE in sub and gsub. thanks to al aho who actually did the fix (in b.c), and to wolfgang seeberg for finding it and providing a very compact test case. fixed quotation in b.c; thanks to Hal Pratt and the Princeton Dante Project. removed some no-effect asserts in run.c. fiddled maketab.c to not complain about bison-generated values. removed the obsolete -V argument; fixed --version to print the version and exit. fixed wording and an outright error in the usage message; thanks to igor sobrado and jason mcintyre. fixed a bug in -d that caused core dump if no program followed. Jan 1, 2007: dropped mac.code from makefile; there are few non-MacOSX mac's these days. Jan 17, 2006: system() not flagged as unsafe in the unadvertised -safe option. found it while enhancing tests before shipping the ;login: article. practice what you preach. removed the 9-years-obsolete -mr and -mf flags. added -version and --version options. core dump on linux with BEGIN {nextfile}, now fixed. removed some #ifdef's in run.c and lex.c that appear to no longer be necessary.
216 lines
5.7 KiB
C
216 lines
5.7 KiB
C
/* $NetBSD: main.c,v 1.2 2014/03/12 14:20:43 ryoon Exp $ */
|
|
|
|
/****************************************************************
|
|
Copyright (C) Lucent Technologies 1997
|
|
All Rights Reserved
|
|
|
|
Permission to use, copy, modify, and distribute this software and
|
|
its documentation for any purpose and without fee is hereby
|
|
granted, provided that the above copyright notice appear in all
|
|
copies and that both that the copyright notice and this
|
|
permission notice and warranty disclaimer appear in supporting
|
|
documentation, and that the name Lucent Technologies or any of
|
|
its entities not be used in advertising or publicity pertaining
|
|
to distribution of the software without specific, written prior
|
|
permission.
|
|
|
|
LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
|
|
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
|
|
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
|
|
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
THIS SOFTWARE.
|
|
****************************************************************/
|
|
|
|
const char *version = "version 20121220";
|
|
|
|
#define DEBUG
|
|
#include <stdio.h>
|
|
#include <ctype.h>
|
|
#include <locale.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <signal.h>
|
|
#include "awk.h"
|
|
#include "ytab.h"
|
|
|
|
extern char **environ;
|
|
extern int nfields;
|
|
|
|
int dbg = 0;
|
|
Awkfloat srand_seed = 1;
|
|
char *cmdname; /* gets argv[0] for error messages */
|
|
extern FILE *yyin; /* lex input file */
|
|
char *lexprog; /* points to program argument if it exists */
|
|
extern int errorflag; /* non-zero if any syntax errors; set by yyerror */
|
|
int compile_time = 2; /* for error printing: */
|
|
/* 2 = cmdline, 1 = compile, 0 = running */
|
|
|
|
#define MAX_PFILE 20 /* max number of -f's */
|
|
|
|
char *pfile[MAX_PFILE]; /* program filenames from -f's */
|
|
int npfile = 0; /* number of filenames */
|
|
int curpfile = 0; /* current filename */
|
|
|
|
int safe = 0; /* 1 => "safe" mode */
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
const char *fs = NULL;
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */
|
|
cmdname = argv[0];
|
|
if (argc == 1) {
|
|
fprintf(stderr,
|
|
"usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n",
|
|
cmdname);
|
|
exit(1);
|
|
}
|
|
signal(SIGFPE, fpecatch);
|
|
|
|
srand_seed = 1;
|
|
srand(srand_seed);
|
|
|
|
yyin = NULL;
|
|
symtab = makesymtab(NSYMTAB/NSYMTAB);
|
|
while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
|
|
if (strcmp(argv[1],"-version") == 0 || strcmp(argv[1],"--version") == 0) {
|
|
printf("awk %s\n", version);
|
|
exit(0);
|
|
break;
|
|
}
|
|
if (strncmp(argv[1], "--", 2) == 0) { /* explicit end of args */
|
|
argc--;
|
|
argv++;
|
|
break;
|
|
}
|
|
switch (argv[1][1]) {
|
|
case 's':
|
|
if (strcmp(argv[1], "-safe") == 0)
|
|
safe = 1;
|
|
break;
|
|
case 'f': /* next argument is program filename */
|
|
if (argv[1][2] != 0) { /* arg is -fsomething */
|
|
if (npfile >= MAX_PFILE - 1)
|
|
FATAL("too many -f options");
|
|
pfile[npfile++] = &argv[1][2];
|
|
} else { /* arg is -f something */
|
|
argc--; argv++;
|
|
if (argc <= 1)
|
|
FATAL("no program filename");
|
|
if (npfile >= MAX_PFILE - 1)
|
|
FATAL("too many -f options");
|
|
pfile[npfile++] = argv[1];
|
|
}
|
|
break;
|
|
case 'F': /* set field separator */
|
|
if (argv[1][2] != 0) { /* arg is -Fsomething */
|
|
if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */
|
|
fs = "\t";
|
|
else if (argv[1][2] != 0)
|
|
fs = &argv[1][2];
|
|
} else { /* arg is -F something */
|
|
argc--; argv++;
|
|
if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */
|
|
fs = "\t";
|
|
else if (argc > 1 && argv[1][0] != 0)
|
|
fs = &argv[1][0];
|
|
}
|
|
if (fs == NULL || *fs == '\0')
|
|
WARNING("field separator FS is empty");
|
|
break;
|
|
case 'v': /* -v a=1 to be done NOW. one -v for each */
|
|
if (argv[1][2] != 0) { /* arg is -vsomething */
|
|
if (isclvar(&argv[1][2]))
|
|
setclvar(&argv[1][2]);
|
|
else
|
|
FATAL("invalid -v option argument: %s", &argv[1][2]);
|
|
} else { /* arg is -v something */
|
|
argc--; argv++;
|
|
if (argc <= 1)
|
|
FATAL("no variable name");
|
|
if (isclvar(argv[1]))
|
|
setclvar(argv[1]);
|
|
else
|
|
FATAL("invalid -v option argument: %s", argv[1]);
|
|
}
|
|
break;
|
|
case 'd':
|
|
dbg = atoi(&argv[1][2]);
|
|
if (dbg == 0)
|
|
dbg = 1;
|
|
printf("awk %s\n", version);
|
|
break;
|
|
default:
|
|
WARNING("unknown option %s ignored", argv[1]);
|
|
break;
|
|
}
|
|
argc--;
|
|
argv++;
|
|
}
|
|
/* argv[1] is now the first argument */
|
|
if (npfile == 0) { /* no -f; first argument is program */
|
|
if (argc <= 1) {
|
|
if (dbg)
|
|
exit(0);
|
|
FATAL("no program given");
|
|
}
|
|
dprintf( ("program = |%s|\n", argv[1]) );
|
|
lexprog = argv[1];
|
|
argc--;
|
|
argv++;
|
|
}
|
|
recinit(recsize);
|
|
syminit();
|
|
compile_time = 1;
|
|
argv[0] = cmdname; /* put prog name at front of arglist */
|
|
dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
|
|
arginit(argc, argv);
|
|
if (!safe)
|
|
envinit(environ);
|
|
yyparse();
|
|
setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */
|
|
if (fs)
|
|
*FS = qstring(fs, '\0');
|
|
dprintf( ("errorflag=%d\n", errorflag) );
|
|
if (errorflag == 0) {
|
|
compile_time = 0;
|
|
run(winner);
|
|
} else
|
|
bracecheck();
|
|
return(errorflag);
|
|
}
|
|
|
|
int pgetc(void) /* get 1 character from awk program */
|
|
{
|
|
int c;
|
|
|
|
for (;;) {
|
|
if (yyin == NULL) {
|
|
if (curpfile >= npfile)
|
|
return EOF;
|
|
if (strcmp(pfile[curpfile], "-") == 0)
|
|
yyin = stdin;
|
|
else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
|
|
FATAL("can't open file %s", pfile[curpfile]);
|
|
lineno = 1;
|
|
}
|
|
if ((c = getc(yyin)) != EOF)
|
|
return c;
|
|
if (yyin != stdin)
|
|
fclose(yyin);
|
|
yyin = NULL;
|
|
curpfile++;
|
|
}
|
|
}
|
|
|
|
char *cursource(void) /* current source file name */
|
|
{
|
|
if (npfile > 0)
|
|
return pfile[curpfile];
|
|
else
|
|
return NULL;
|
|
}
|