Esempio n. 1
0
int main(int argc, const char *argv[])
{
	const char *fs = NULL, *marg;
	int temp;

	__progname = argv[0];
	_wildcard(&argc, &argv);

	setlocale(LC_ALL, "");
	cmdname = __progname;
	if (argc == 1) {
		fprintf(stderr, "Usage: %s [-f programfile | 'program'] [-Ffieldsep] [-v var=value] [-safe] [-mrn] [-mfn] [files]\n", cmdname);
		exit(1);
	}
	signal(SIGFPE, fpecatch);
	yyin = NULL;
	symtab = makesymtab(NSYMTAB);
	while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
		if (strcmp(argv[1], "--") == 0) {	/* explicit end of args */
			argc--;
			argv++;
			break;
		}
		switch (argv[1][1]) {
		case 's':
			if (strcmp(argv[1], "-safe") == 0)
				safe = 1;
			break;
		case 'f':	/* next argument is program filename */
			argc--;
			argv++;
			if (argc <= 1)
				ERROR "no program filename" FATAL;
			pfile[npfile++] = argv[1];
			break;
		case 'F':	/* set field separator */
			if (argv[1][2] != 0) {	/* arg is -Fsomething */
				if (argv[1][2] == 't' && argv[1][3] == 0)	/* wart: t=>\t */
					fs = "\t";
				else if (argv[1][2] != 0)
					fs = &argv[1][2];
			} else {		/* arg is -F something */
				argc--; argv++;
				if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0)	/* wart: t=>\t */
					fs = "\t";
				else if (argc > 1 && argv[1][0] != 0)
					fs = &argv[1][0];
			}
			if (fs == NULL || *fs == '\0')
				ERROR "field separator FS is empty" WARNING;
			break;
		case 'v':	/* -v a=1 to be done NOW.  one -v for each */
			if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1]))
				setclvar((char*)argv[1]);
			break;
		case 'm':	/* more memory: -mr=record, -mf=fields */
				/* no longer needed */
			marg = argv[1];
			if (argv[1][3])
				temp = atoi(&argv[1][3]);
			else {
				argv++; argc--;
				temp = atoi(&argv[1][0]);
			}
			switch (marg[2]) {
			case 'r':	recsize = temp; break;
			case 'f':	nfields = temp; break;
			default: ERROR "unknown option %s\n", marg FATAL;
			}
			break;
		case 'd':
			dbg = atoi(&argv[1][2]);
			if (dbg == 0)
				dbg = 1;
			printf("awk %s\n", version);
			break;
		case 'V':	/* added for exptools "standard" */
			printf("awk %s\n", version);
			exit(0);
			break;
		default:
			ERROR "unknown option %s ignored", argv[1] WARNING;
			break;
		}
		argc--;
		argv++;
	}
	/* argv[1] is now the first argument */
	if (npfile == 0) {	/* no -f; first argument is program */
		if (argc <= 1) {
			if (dbg)
				exit(0);
			ERROR "no program given" FATAL;
		}
		   dprintf( ("program = |%s|\n", argv[1]) );
		lexprog = argv[1];
		argc--;
		argv++;
	}
	recinit(recsize);
	syminit();
	compile_time = 1;
	argv[0] = cmdname;	/* put prog name at front of arglist */
	   dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
	arginit(argc, argv);
	if (!safe)
		envinit(environ);
	yyparse();
	if (fs)
		*FS = qstring(fs, '\0');
	   dprintf( ("errorflag=%d\n", errorflag) );
	if (errorflag == 0) {
		compile_time = 0;
		run(winner);
	} else
		bracecheck();
	return(errorflag);
}
Esempio n. 2
0
int main( int argc, char *argv[] )
{
    const char  *fs = NULL;
    char        **eargv = NULL;
    int         eargc;

    setlocale( LC_CTYPE, "" );
    setlocale( LC_NUMERIC, "C" ); /* for parsing cmdline & prog */
    cmdname = argv[0];
    if( argc == 1 ) {
        fprintf( stderr,
          "usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n",
          cmdname );
        exit( 1 );
    }
    signal( SIGFPE, fpecatch );

    srand_seed = 1;
    srand( (unsigned)srand_seed );

    yyin = NULL;
    symtab = makesymtab( NSYMTAB/NSYMTAB );
    if( argc == 2 && argv[1][0] == '@' && argv[1][1] != '\0' ) {
        const char  *env;

        env = getenv( &argv[1][1] );
        if( env != NULL ) {
            eargc = ParseEnvVar( env, NULL, NULL );  // count parameters.
            eargv = malloc( eargc * sizeof( char * ) + strlen( env ) + 1 + eargc );
            ParseEnvVar( env, eargv, (char *)( eargv + eargc ) );
            argc = eargc;
            argv = eargv;
        }
    }
    while( argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0' ) {
        if( strcmp( argv[1], "-version" ) == 0 || strcmp( argv[1], "--version" ) == 0 ) {
            printf( "awk %s\n", version );
            exit( 0 );
            break;
        }
        if( strncmp( argv[1], "--", 2 ) == 0 ) {   /* explicit end of args */
            argc--;
            argv++;
            break;
        }
        switch( argv[1][1] ) {
        case 's':
            if( strcmp( argv[1], "-safe" ) == 0 )
                safe = true;
            break;
        case 'f':       /* next argument is program filename */
            if( argv[1][2] != '\0' ) {  /* arg is -fsomething */
                if( npfile >= MAX_PFILE - 1 ) {
                    FATAL( "too many -f options" );
                }
                pfile[npfile++] = unquote( &argv[1][2] );
            } else {        /* arg is -f something */
                argc--; argv++;
                if( argc <= 1 ) {
                    FATAL( "no program filename" );
                }
                if( npfile >= MAX_PFILE - 1 ) {
                    FATAL( "too many -f options" );
                }
                pfile[npfile++] = unquote( argv[1] );
            }
            break;
        case 'F':       /* set field separator */
            if( argv[1][2] != '\0' ) {  /* arg is -Fsomething */
                if( argv[1][2] == 't' && argv[1][3] == '\0' ) {     /* wart: t=>\t */
                    fs = "\t";
                } else if( argv[1][2] != '\0' ) {
                    fs = &argv[1][2];
                }
            } else {        /* arg is -F something */
                argc--; argv++;
                if( argc > 1 && argv[1][0] == 't' && argv[1][1] == '\0' ) { /* wart: t=>\t */
                    fs = "\t";
                } else if( argc > 1 && argv[1][0] != '\0' ) {
                    fs = &argv[1][0];
                }
            }
            if( fs == NULL || *fs == '\0' )
                WARNING( "field separator FS is empty" );
            break;
        case 'v':       /* -v a=1 to be done NOW.  one -v for each */
            if( argv[1][2] != '\0' ) {  /* arg is -vsomething */
                char *p;

                p = unquote( &argv[1][2] );
                if( isclvar( p ) ) {
                    setclvar( p );
                } else {
                    FATAL( "invalid -v option argument: %s", p );
                }
            } else {        /* arg is -v something */
                char *p;

                argc--; argv++;
                if( argc <= 1 ) {
                    FATAL( "no variable name" );
                }
                p = unquote( argv[1] );
                if( isclvar( p ) ) {
                    setclvar( p );
                } else {
                    FATAL( "invalid -v option argument: %s", p );
                }
            }
            break;
        case 'd':
            dbg = atoi( &argv[1][2] );
            if( dbg == 0 )
                dbg = 1;
            printf( "awk %s\n", version );
            break;
        default:
            WARNING( "unknown option %s ignored", argv[1] );
            break;
        }
        argc--;
        argv++;
    }
    /* argv[1] is now the first argument */
    if( npfile == 0 ) {      /* no -f; first argument is program */
        char *p;

        if( argc <= 1 ) {
            if( dbg )
                exit( 0 );
            FATAL( "no program given" );
        }
        p = unquote( argv[1] );
        dprintf(( "program = |%s|\n", p ));
        lexprog = p;
        argc--;
        argv++;
    }
    for( eargc = 1; eargc < argc; ++eargc ) {
        argv[eargc] = unquote( argv[eargc] );
    }
    recinit( recsize );
    syminit();
    compile_time = 1;
    argv[0] = cmdname;      /* put prog name at front of arglist */
    dprintf(( "argc=%d, argv[0]=%s\n", argc, argv[0] ));
    arginit( argc, argv );
    if( !safe )
        envinit( environ );
    yyparse();
    setlocale( LC_NUMERIC, "" ); /* back to whatever it is locally */
    if( fs )
        *FS = qstring( fs, '\0' );
    dprintf(( "errorflag=%d\n", errorflag ));
    if( errorflag == 0 ) {
        compile_time = 0;
        run( winner );
    } else {
        bracecheck();
    }
    return( errorflag );
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
	const char *fs = NULL;

	setlocale(LC_CTYPE, "");
	setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */
	cmdname = argv[0];
	if (argc == 1) {
		fprintf(stderr, 
		  "usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n", 
		  cmdname);
		exit(1);
	}
	signal(SIGFPE, fpecatch);
	yyin = NULL;
	symtab = makesymtab(NSYMTAB/NSYMTAB);
	while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
		if (strcmp(argv[1],"-version") == 0 || strcmp(argv[1],"--version") == 0) {
			printf("awk %s\n", version);
			exit(0);
			break;
		}
		if (strncmp(argv[1], "--", 2) == 0) {	/* explicit end of args */
			argc--;
			argv++;
			break;
		}
		switch (argv[1][1]) {
		case 's':
			if (strcmp(argv[1], "-safe") == 0)
				safe = 1;
			break;
		case 'f':	/* next argument is program filename */
			argc--;
			argv++;
			if (argc <= 1)
				FATAL("no program filename");
			if (npfile >= MAX_PFILE - 1)
				FATAL("too many -f options"); 
			pfile[npfile++] = argv[1];
			break;
		case 'F':	/* set field separator */
			if (argv[1][2] != 0) {	/* arg is -Fsomething */
				if (argv[1][2] == 't' && argv[1][3] == 0)	/* wart: t=>\t */
					fs = "\t";
				else if (argv[1][2] != 0)
					fs = &argv[1][2];
			} else {		/* arg is -F something */
				argc--; argv++;
				if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0)	/* wart: t=>\t */
					fs = "\t";
				else if (argc > 1 && argv[1][0] != 0)
					fs = &argv[1][0];
			}
			if (fs == NULL || *fs == '\0')
				WARNING("field separator FS is empty");
			break;
		case 'v':	/* -v a=1 to be done NOW.  one -v for each */
			if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1]))
				setclvar(argv[1]);
			break;
		case 'd':
			dbg = atoi(&argv[1][2]);
			if (dbg == 0)
				dbg = 1;
			printf("awk %s\n", version);
			break;
		default:
			WARNING("unknown option %s ignored", argv[1]);
			break;
		}
		argc--;
		argv++;
	}
	/* argv[1] is now the first argument */
	if (npfile == 0) {	/* no -f; first argument is program */
		if (argc <= 1) {
			if (dbg)
				exit(0);
			FATAL("no program given");
		}
		   dprintf( ("program = |%s|\n", argv[1]) );
		lexprog = argv[1];
		argc--;
		argv++;
	}
	recinit(recsize);
	syminit();
	compile_time = 1;
	argv[0] = cmdname;	/* put prog name at front of arglist */
	   dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
	arginit(argc, argv);
	if (!safe)
		envinit(environ);
	yyparse();
	setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */
	if (fs)
		*FS = qstring(fs, '\0');
	   dprintf( ("errorflag=%d\n", errorflag) );
	if (errorflag == 0) {
		compile_time = 0;
		run(winner);
	} else
		bracecheck();
	return(errorflag);
}