. let kernel use read_tsc() from sysutil library
[minix3.git] / commands / awk / m.c
blob14a779ce95abfb0e1580f0dee854022f4568f16b
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) char **argv;
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 pfp = efopen(s, "r");
74 s += strlen(s) - 1;
75 break;
77 xargc = argc; xargv = argv;
78 if (pfp == NULL && xargc > 0) {
79 srcprg = *xargv++; xargc--;
82 if (pfp == NULL && xargc > 0) {
83 if (strpbrk(xargv[0], " !$^()={}[];<>,/~") != NULL) {
84 sprintf(record, "%s\n", xargv[0]);
85 srcprg = strsave(record);
87 else {
88 sprintf(record, "%s.awk", xargv[0]);
89 if ((pfp = fopen(record, "r")) == NULL)
90 error("can't open %s", record);
92 xargc--; xargv++;
96 while (*xargv != NULL && strchr(*xargv, '=') != NULL) {
97 setvar(*xargv++);
98 xargc--;
101 initarg(cmd, xargc, xargv);
102 if (xargc == 0) {
103 ifp = stdin; *FILENAME = "-";
105 parse();
106 closeall();
107 exit(0);
110 FILE *
111 efopen(file, mode) char *file, *mode;
113 FILE *fp, *fopen();
115 if ((fp = fopen(file, mode)) == NULL)
116 error("cannot open %s", file);
117 return fp;
120 error(s, t) char *s, *t;
122 extern double *NR;
124 fprintf(stderr, "awk: ");
125 fprintf(stderr, s, t);
126 fprintf(stderr, "\n");
127 if (NR != NULL) {
128 fprintf(stderr, "record number %g\n", *NR);
130 #ifdef DOS
131 closeall();
132 #endif
133 exit(1);
136 void
137 onint(i)
139 closeall();
140 exit(0x80 | i);