main.c 5.2 KB

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