m.c

Go to the documentation of this file.
00001 /*
00002  * a small awk clone
00003  *
00004  * (C) 1989 Saeko Hirabauashi & Kouichi Hirabayashi
00005  *
00006  * Absolutely no warranty. Use this software with your own risk.
00007  *
00008  * Permission to use, copy, modify and distribute this software for any
00009  * purpose and without fee is hereby granted, provided that the above
00010  * copyright and disclaimer notice.
00011  *
00012  * This program was written to fit into 64K+64K memory of the Minix 1.2.
00013  */
00014 
00015 
00016 #include <stdio.h>
00017 #include <sys/types.h>
00018 #include <signal.h>
00019 #include "awk.h"
00020 
00021 extern char **FS, **FILENAME;
00022 extern char record[];
00023 extern FILE *ifp;
00024 
00025 NODE *parse();
00026 CELL *execute();
00027 FILE *efopen(), *fopen();
00028 char *strsave();
00029 
00030 int xargc;
00031 char **xargv;
00032 char *srcprg;
00033 FILE *pfp;
00034 char *cmd;
00035 #if 0
00036 int iflg;       /* interactive mode */
00037 #endif
00038 
00039 main(argc, argv) char **argv;
00040 {
00041   char *s, *strpbrk(), *strchr();
00042   void onint();
00043 
00044 #ifdef DOS
00045   _sharg(&argc, &argv);
00046 #endif
00047   signal(SIGINT, onint);
00048   signal(SIGFPE, onint);
00049   cmd = argv[0];
00050   init();
00051   while (--argc > 0 && (*++argv)[0] == '-')
00052         for (s = argv[0]+1; *s; s++)
00053                 if (strcmp(argv[0], "-") == 0)
00054                         break;
00055                 else
00056                 switch (*s) {
00057 #if 0
00058                 case 'i':
00059                         iflg++;
00060                         pfp = stdin;
00061                         interactive();
00062                         /* no return */
00063 #endif
00064                 case 'F':
00065                         *FS = ++s;
00066                         break;
00067                 case 'f':
00068                         if (*(s+1))
00069                                 s++;
00070                         else {
00071                                 argc--; s = *++argv;
00072                         }
00073                         pfp = efopen(s, "r");
00074                         s += strlen(s) - 1;
00075                         break;
00076                 }
00077   xargc = argc; xargv = argv;
00078   if (pfp == NULL && xargc > 0) {
00079         srcprg = *xargv++; xargc--;
00080   }
00081 /*
00082   if (pfp == NULL && xargc > 0) {
00083         if (strpbrk(xargv[0], " !$^()={}[];<>,/~") != NULL) {
00084                 sprintf(record, "%s\n", xargv[0]);
00085                 srcprg = strsave(record);
00086         }
00087         else {
00088                 sprintf(record, "%s.awk", xargv[0]);
00089                 if ((pfp = fopen(record, "r")) == NULL)
00090                         error("can't open %s", record);
00091         }
00092         xargc--; xargv++;
00093   }
00094 */
00095 
00096   while (*xargv != NULL && strchr(*xargv, '=') != NULL) {
00097         setvar(*xargv++);
00098         xargc--;
00099   }
00100 
00101   initarg(cmd, xargc, xargv);
00102   if (xargc == 0) {
00103         ifp = stdin; *FILENAME = "-";
00104   }
00105   parse();
00106   closeall();
00107   exit(0);
00108 }
00109 
00110 FILE *
00111 efopen(file, mode) char *file, *mode;
00112 {
00113   FILE *fp, *fopen();
00114 
00115   if ((fp = fopen(file, mode)) == NULL)
00116         error("cannot open %s", file);
00117   return fp;
00118 }
00119 
00120 error(s, t) char *s, *t;
00121 {
00122   extern double *NR;
00123 
00124   fprintf(stderr, "awk: ");
00125   fprintf(stderr, s, t);
00126   fprintf(stderr, "\n");
00127   if (NR != NULL) {
00128         fprintf(stderr, "record number %g\n", *NR);
00129   }
00130 #ifdef DOS
00131   closeall();
00132 #endif
00133   exit(1);
00134 }
00135 
00136 void
00137 onint(i)
00138 {
00139   closeall();
00140   exit(0x80 | i);
00141 }

Generated on Fri Apr 14 22:56:41 2006 for minix by  doxygen 1.4.6