source: trunk/minix/commands/awk/m.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 2.4 KB
Line 
1/*
2 * a small awk clone
3 *
4 * (C) 1989 Saeko Hirabauashi & Kouichi Hirabayashi
5 *
6 * Absolutely no warranty. Use this software with your own risk.
7 *
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.
11 *
12 * This program was written to fit into 64K+64K memory of the Minix 1.2.
13 */
14
15
16#include <stdio.h>
17#include <sys/types.h>
18#include <signal.h>
19#include "awk.h"
20
21extern char **FS, **FILENAME;
22extern char record[];
23extern FILE *ifp;
24
25NODE *parse();
26CELL *execute();
27FILE *efopen(), *fopen();
28char *strsave();
29
30int xargc;
31char **xargv;
32char *srcprg;
33FILE *pfp;
34char *cmd;
35#if 0
36int iflg; /* interactive mode */
37#endif
38
39main(argc, argv) char **argv;
40{
41 char *s, *strpbrk(), *strchr();
42 void onint();
43
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;
72 }
73 pfp = efopen(s, "r");
74 s += strlen(s) - 1;
75 break;
76 }
77 xargc = argc; xargv = argv;
78 if (pfp == NULL && xargc > 0) {
79 srcprg = *xargv++; xargc--;
80 }
81/*
82 if (pfp == NULL && xargc > 0) {
83 if (strpbrk(xargv[0], " !$^()={}[];<>,/~") != NULL) {
84 sprintf(record, "%s\n", xargv[0]);
85 srcprg = strsave(record);
86 }
87 else {
88 sprintf(record, "%s.awk", xargv[0]);
89 if ((pfp = fopen(record, "r")) == NULL)
90 error("can't open %s", record);
91 }
92 xargc--; xargv++;
93 }
94*/
95
96 while (*xargv != NULL && strchr(*xargv, '=') != NULL) {
97 setvar(*xargv++);
98 xargc--;
99 }
100
101 initarg(cmd, xargc, xargv);
102 if (xargc == 0) {
103 ifp = stdin; *FILENAME = "-";
104 }
105 parse();
106 closeall();
107 exit(0);
108}
109
110FILE *
111efopen(file, mode) char *file, *mode;
112{
113 FILE *fp, *fopen();
114
115 if ((fp = fopen(file, mode)) == NULL)
116 error("cannot open %s", file);
117 return fp;
118}
119
120error(s, t) char *s, *t;
121{
122 extern double *NR;
123
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);
129 }
130#ifdef DOS
131 closeall();
132#endif
133 exit(1);
134}
135
136void
137onint(i)
138{
139 closeall();
140 exit(0x80 | i);
141}
Note: See TracBrowser for help on using the repository browser.