vm: include no-caching bits in PTF_ALLFLAGS for flags sanity check.
[minix.git] / commands / awk.old / m.c
blobaf8b6e62a39a4777d4609eb7d667a70e0f1ae977
1 /*
2 * a small awk clone
4 * (C) 1989 Saeko Hirabauashi & Kouichi Hirabayashi
6 * Absolutely no warranty. Use this software with your own risk.
8 * Permission to use, copy, modify and distribute this software for any
9 * purpose and without fee is hereby granted, provided that the above
10 * copyright and disclaimer notice.
12 * This program was written to fit into 64K+64K memory of the Minix 1.2.
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <signal.h>
19 #include "awk.h"
21 extern char **FS, **FILENAME;
22 extern char record[];
23 extern FILE *ifp;
25 NODE *parse();
26 CELL *execute();
27 FILE *efopen(), *fopen();
28 char *strsave();
30 int xargc;
31 char **xargv;
32 char *srcprg;
33 FILE *pfp;
34 char *cmd;
35 #if 0
36 int iflg; /* interactive mode */
37 #endif
39 main(argc, argv, envp) char **argv, *envp;
41 char *s, *strpbrk(), *strchr();
42 void onint();
44 #ifdef DOS
45 _sharg(&argc, &argv);
46 #endif
47 signal(SIGINT, onint);
48 signal(SIGFPE, onint);
49 cmd = argv[0];
50 init();
51 while (--argc > 0 && (*++argv)[0] == '-')
52 for (s = argv[0]+1; *s; s++)
53 if (strcmp(argv[0], "-") == 0)
54 break;
55 else
56 switch (*s) {
57 #if 0
58 case 'i':
59 iflg++;
60 pfp = stdin;
61 interactive();
62 /* no return */
63 #endif
64 case 'F':
65 *FS = ++s;
66 break;
67 case 'f':
68 if (*(s+1))
69 s++;
70 else {
71 argc--; s = *++argv;
73 if (s == NULL) usage();
74 pfp = efopen(s, "r");
75 s += strlen(s) - 1;
76 break;
78 xargc = argc; xargv = argv;
79 if (pfp == NULL && xargc > 0) {
80 srcprg = *xargv++; xargc--;
83 if (pfp == NULL && xargc > 0) {
84 if (strpbrk(xargv[0], " !$^()={}[];<>,/~") != NULL) {
85 sprintf(record, "%s\n", xargv[0]);
86 srcprg = strsave(record);
88 else {
89 sprintf(record, "%s.awk", xargv[0]);
90 if ((pfp = fopen(record, "r")) == NULL)
91 error("can't open %s", record);
93 xargc--; xargv++;
97 if (pfp == NULL && srcprg == NULL) usage();
99 while (*xargv != NULL && strchr(*xargv, '=') != NULL) {
100 setvar(*xargv++);
101 xargc--;
104 initarg(cmd, xargc, xargv, envp);
105 if (xargc == 0) {
106 ifp = stdin; *FILENAME = "-";
108 parse();
109 closeall();
110 exit(0);
113 FILE *
114 efopen(file, mode) char *file, *mode;
116 FILE *fp, *fopen();
118 if ((fp = fopen(file, mode)) == NULL)
119 error("cannot open %s", file);
120 return fp;
123 error(s, t) char *s, *t;
125 extern double *NR;
127 fprintf(stderr, "awk: ");
128 fprintf(stderr, s, t);
129 fprintf(stderr, "\n");
130 if (NR != NULL) {
131 fprintf(stderr, "record number %g\n", *NR);
133 #ifdef DOS
134 closeall();
135 #endif
136 exit(1);
139 void
140 onint(i)
142 closeall();
143 exit(0x80 | i);
146 void
147 usage()
149 fprintf(stderr,
150 "usage: %s [options] [-f <rulefile> | <rules>] [inputfiles]\n", cmd);
151 closeall();
152 exit(1);