main.c
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 169 行
C
169 行
#ifndef lintstatic char *sccsid = "@(#)main.c 4.1 ULTRIX 7/17/90";#endif lint/************************************************************************ * * * Copyright (c) 1989 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//************************************************************************ * Modification History * -------------------- * * 9-Jan-89 Tim N * Added usage error message for bad options. * Added check for null pointer on command line when getting * the -f switch. Added sccs keywords, header and this comment * block. This module contained the ident: * "@(#)main.c 4.2 8/11/83"; ************************************************************************/#include "stdio.h"#include "ctype.h"#include "awk.def"#include "awk.h"#define TOLOWER(c) (isupper(c) ? tolower(c) : c) /* ugh!!! */int dbg = 0;int svflg = 0;int rstflg = 0;int svargc;char **svargv, **xargv;extern FILE *yyin; /* lex input file */char *lexprog; /* points to program argument if it exists */extern errorflag; /* non-zero if any syntax errors; set by yyerror */int filefd, symnum, ansfd;char *filelist;extern int maxsym, errno;main(argc, argv) int argc; char *argv[]; { if (argc == 1) error(FATAL, "Usage: awk [-f source | 'cmds'] [files]"); if (strcmp(argv[0], "a.out")) logit(argc, argv); syminit(); while (argc > 1) { argc--; argv++; /* this nonsense is because gcos argument handling */ /* folds -F into -f. accordingly, one checks the next /* character after f to see if it's -f file or -Fx. */ if (argv[0][0] == '-' && TOLOWER(argv[0][1]) == 'f' && argv[0][2] == '\0') { /* check for file name argument - Tim N */ if (!argv[1]) error(FATAL, "Usage: awk [-f source | 'cmds'] [files]"); yyin = fopen(argv[1], "r"); if (yyin == NULL) error(FATAL, "can't open %s", argv[1]); argc--; argv++; break; } else if (argv[0][0] == '-' && TOLOWER(argv[0][1]) == 'f') { /* set field sep */ if (argv[0][2] == 't') /* special case for tab */ **FS = '\t'; else **FS = argv[0][2]; continue; } else if (argv[0][0] != '-') { dprintf("cmds=|%s|\n", argv[0], NULL, NULL); yyin = NULL; lexprog = argv[0]; argv[0] = argv[-1]; /* need this space */ break; } else if (strcmp("-d", argv[0])==0) { dbg = 1; } else if(strcmp("-S", argv[0]) == 0) { svflg = 1; } else if(strncmp("-R", argv[0], 2) == 0) { if(thaw(argv[0] + 2) == 0) rstflg = 1; else { fprintf(stderr, "not restored\n"); exit(1); } } else error(FATAL, "Usage: awk [-f source | 'cmds'] [files]"); } if (argc <= 1) { argv[0][0] = '-'; argv[0][1] = '\0'; argc++; argv--; } svargc = --argc; svargv = ++argv; dprintf("svargc=%d svargv[0]=%s\n", svargc, svargv[0], NULL); *FILENAME = *svargv; /* initial file name */ if(rstflg == 0) yyparse(); dprintf("errorflag=%d\n", errorflag, NULL, NULL); if (errorflag) exit(errorflag); if(svflg) { svflg = 0; if(freeze("awk.out") != 0) fprintf(stderr, "not saved\n"); exit(0); } run(); exit(errorflag);}logit(n, s) char *s[];{ int i, tvec[2]; FILE *f, *g; char buf[512]; if ((f=fopen("/crp/pjw/awkhist/awkhist", "a"))==NULL) return; time(tvec); fprintf(f, "%-8s %s", getlogin(), ctime(tvec)); for (i=0; i<n; i++) fprintf(f, "'%s'", s[i]); putc('\n', f); if (strcmp(s[1], "-f")) { fclose(f); return; } if ((g=fopen(s[2], "r"))==NULL) { fclose(f); return; } while ((i=fread(buf, 1, 512, g))>0) fwrite(buf, 1, i, f); fclose(f); fclose(g);}yywrap(){ return(1);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?