minix/commands/awk.old/m.c

154 lines
2.6 KiB
C
Raw Normal View History

2005-04-21 16:53:53 +02:00
/*
* a small awk clone
*
* (C) 1989 Saeko Hirabauashi & Kouichi Hirabayashi
*
* Absolutely no warranty. Use this software with your own risk.
*
* Permission to use, copy, modify and distribute this software for any
* purpose and without fee is hereby granted, provided that the above
* copyright and disclaimer notice.
*
* This program was written to fit into 64K+64K memory of the Minix 1.2.
*/
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include "awk.h"
extern char **FS, **FILENAME;
extern char record[];
extern FILE *ifp;
NODE *parse();
CELL *execute();
FILE *efopen(), *fopen();
char *strsave();
int xargc;
char **xargv;
char *srcprg;
FILE *pfp;
char *cmd;
#if 0
int iflg; /* interactive mode */
#endif
2009-08-15 13:34:41 +02:00
main(argc, argv, envp) char **argv, *envp;
2005-04-21 16:53:53 +02:00
{
char *s, *strpbrk(), *strchr();
void onint();
#ifdef DOS
_sharg(&argc, &argv);
#endif
signal(SIGINT, onint);
signal(SIGFPE, onint);
cmd = argv[0];
init();
while (--argc > 0 && (*++argv)[0] == '-')
for (s = argv[0]+1; *s; s++)
if (strcmp(argv[0], "-") == 0)
break;
else
switch (*s) {
#if 0
case 'i':
iflg++;
pfp = stdin;
interactive();
/* no return */
#endif
case 'F':
*FS = ++s;
break;
case 'f':
if (*(s+1))
s++;
else {
argc--; s = *++argv;
}
2009-12-14 21:24:33 +01:00
if (s == NULL) usage();
2005-04-21 16:53:53 +02:00
pfp = efopen(s, "r");
s += strlen(s) - 1;
break;
}
xargc = argc; xargv = argv;
if (pfp == NULL && xargc > 0) {
srcprg = *xargv++; xargc--;
}
/*
if (pfp == NULL && xargc > 0) {
if (strpbrk(xargv[0], " !$^()={}[];<>,/~") != NULL) {
sprintf(record, "%s\n", xargv[0]);
srcprg = strsave(record);
}
else {
sprintf(record, "%s.awk", xargv[0]);
if ((pfp = fopen(record, "r")) == NULL)
error("can't open %s", record);
}
xargc--; xargv++;
}
*/
2009-12-14 21:24:33 +01:00
if (pfp == NULL && srcprg == NULL) usage();
2005-04-21 16:53:53 +02:00
while (*xargv != NULL && strchr(*xargv, '=') != NULL) {
setvar(*xargv++);
xargc--;
}
2009-08-15 13:34:41 +02:00
initarg(cmd, xargc, xargv, envp);
2005-04-21 16:53:53 +02:00
if (xargc == 0) {
ifp = stdin; *FILENAME = "-";
}
parse();
closeall();
exit(0);
}
FILE *
efopen(file, mode) char *file, *mode;
{
FILE *fp, *fopen();
if ((fp = fopen(file, mode)) == NULL)
error("cannot open %s", file);
return fp;
}
error(s, t) char *s, *t;
{
extern double *NR;
fprintf(stderr, "awk: ");
fprintf(stderr, s, t);
fprintf(stderr, "\n");
if (NR != NULL) {
fprintf(stderr, "record number %g\n", *NR);
}
#ifdef DOS
closeall();
#endif
exit(1);
}
void
onint(i)
{
closeall();
exit(0x80 | i);
}
2009-12-14 21:24:33 +01:00
void
usage()
{
fprintf(stderr,
"usage: %s [options] [-f <rulefile> | <rules>] [inputfiles]\n", cmd);
closeall();
exit(1);
}