main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /****************************************************************
  10. Copyright (C) Lucent Technologies 1997
  11. All Rights Reserved
  12. Permission to use, copy, modify, and distribute this software and
  13. its documentation for any purpose and without fee is hereby
  14. granted, provided that the above copyright notice appear in all
  15. copies and that both that the copyright notice and this
  16. permission notice and warranty disclaimer appear in supporting
  17. documentation, and that the name Lucent Technologies or any of
  18. its entities not be used in advertising or publicity pertaining
  19. to distribution of the software without specific, written prior
  20. permission.
  21. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  22. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  23. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
  24. SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  25. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  26. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  27. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  28. THIS SOFTWARE.
  29. ****************************************************************/
  30. char *version = "version 19990602";
  31. #define DEBUG
  32. #include <stdio.h>
  33. #include <ctype.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <signal.h>
  37. #include "awk.h"
  38. #include "y.tab.h"
  39. extern char **environ;
  40. extern int nfields;
  41. int dbg = 0;
  42. char *cmdname; /* gets argv[0] for error messages */
  43. extern FILE *yyin; /* lex input file */
  44. char *lexprog; /* points to program argument if it exists */
  45. extern int errorflag; /* non-zero if any syntax errors; set by yyerror */
  46. int compile_time = 2; /* for error printing: */
  47. /* 2 = cmdline, 1 = compile, 0 = running */
  48. char *pfile[20]; /* program filenames from -f's */
  49. int npfile = 0; /* number of filenames */
  50. int curpfile = 0; /* current filename */
  51. int safe = 0; /* 1 => "safe" mode */
  52. int main(int argc, char *argv[])
  53. {
  54. char *fs = NULL, *marg;
  55. int temp;
  56. cmdname = argv[0];
  57. if (argc == 1) {
  58. fprintf(stderr, "Usage: %s [-F fieldsep] [-mf n] [-mr n] [-v var=value] [-f programfile | 'program'] [file ...]\n", cmdname);
  59. exit(1);
  60. }
  61. signal(SIGFPE, fpecatch);
  62. yyin = NULL;
  63. symtab = makesymtab(NSYMTAB);
  64. while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
  65. if (strcmp(argv[1], "--") == 0) { /* explicit end of args */
  66. argc--;
  67. argv++;
  68. break;
  69. }
  70. switch (argv[1][1]) {
  71. case 's':
  72. if (strcmp(argv[1], "-safe") == 0)
  73. safe = 1;
  74. break;
  75. case 'f': /* next argument is program filename */
  76. argc--;
  77. argv++;
  78. if (argc <= 1)
  79. FATAL("no program filename");
  80. pfile[npfile++] = argv[1];
  81. break;
  82. case 'F': /* set field separator */
  83. if (argv[1][2] != 0) { /* arg is -Fsomething */
  84. if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */
  85. fs = "\t";
  86. else if (argv[1][2] != 0)
  87. fs = &argv[1][2];
  88. } else { /* arg is -F something */
  89. argc--; argv++;
  90. if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */
  91. fs = "\t";
  92. else if (argc > 1 && argv[1][0] != 0)
  93. fs = &argv[1][0];
  94. }
  95. if (fs == NULL || *fs == '\0')
  96. WARNING("field separator FS is empty");
  97. break;
  98. case 'v': /* -v a=1 to be done NOW. one -v for each */
  99. if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1]))
  100. setclvar(argv[1]);
  101. break;
  102. case 'm': /* more memory: -mr=record, -mf=fields */
  103. /* no longer needed */
  104. marg = argv[1];
  105. if (argv[1][3])
  106. temp = atoi(&argv[1][3]);
  107. else {
  108. argv++; argc--;
  109. temp = atoi(&argv[1][0]);
  110. }
  111. switch (marg[2]) {
  112. case 'r': recsize = temp; break;
  113. case 'f': nfields = temp; break;
  114. default: FATAL("unknown option %s\n", marg);
  115. }
  116. break;
  117. case 'd':
  118. dbg = atoi(&argv[1][2]);
  119. if (dbg == 0)
  120. dbg = 1;
  121. printf("awk %s\n", version);
  122. break;
  123. case 'V': /* added for exptools "standard" */
  124. printf("awk %s\n", version);
  125. exit(0);
  126. break;
  127. default:
  128. WARNING("unknown option %s ignored", argv[1]);
  129. break;
  130. }
  131. argc--;
  132. argv++;
  133. }
  134. /* argv[1] is now the first argument */
  135. if (npfile == 0) { /* no -f; first argument is program */
  136. if (argc <= 1) {
  137. if (dbg)
  138. exit(0);
  139. FATAL("no program given");
  140. }
  141. dprintf( ("program = |%s|\n", argv[1]) );
  142. lexprog = argv[1];
  143. argc--;
  144. argv++;
  145. }
  146. recinit(recsize);
  147. syminit();
  148. compile_time = 1;
  149. argv[0] = cmdname; /* put prog name at front of arglist */
  150. dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
  151. arginit(argc, argv);
  152. if (!safe)
  153. envinit(environ);
  154. yyparse();
  155. if (fs)
  156. *FS = qstring(fs, '\0');
  157. dprintf( ("errorflag=%d\n", errorflag) );
  158. if (errorflag == 0) {
  159. compile_time = 0;
  160. run(winner);
  161. } else
  162. bracecheck();
  163. return(errorflag);
  164. }
  165. int pgetc(void) /* get 1 character from awk program */
  166. {
  167. int c;
  168. for (;;) {
  169. if (yyin == NULL) {
  170. if (curpfile >= npfile)
  171. return EOF;
  172. if (strcmp(pfile[curpfile], "-") == 0)
  173. yyin = stdin;
  174. else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
  175. FATAL("can't open file %s", pfile[curpfile]);
  176. lineno = 1;
  177. }
  178. if ((c = getc(yyin)) != EOF)
  179. return c;
  180. if (yyin != stdin)
  181. fclose(yyin);
  182. yyin = NULL;
  183. curpfile++;
  184. }
  185. }
  186. char *cursource(void) /* current source file name */
  187. {
  188. if (npfile > 0)
  189. return pfile[curpfile];
  190. else
  191. return NULL;
  192. }