From owner-p4-projects@FreeBSD.ORG Sun Feb 22 01:19:48 2009 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id C8C661065673; Sun, 22 Feb 2009 01:19:47 +0000 (UTC) Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 88CE41065670 for ; Sun, 22 Feb 2009 01:19:47 +0000 (UTC) (envelope-from gabor@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id 5CD2D8FC0A for ; Sun, 22 Feb 2009 01:19:47 +0000 (UTC) (envelope-from gabor@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.3/8.14.3) with ESMTP id n1M1Jl4b020107 for ; Sun, 22 Feb 2009 01:19:47 GMT (envelope-from gabor@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.3/8.14.3/Submit) id n1M1JljJ020105 for perforce@freebsd.org; Sun, 22 Feb 2009 01:19:47 GMT (envelope-from gabor@freebsd.org) Date: Sun, 22 Feb 2009 01:19:47 GMT Message-Id: <200902220119.n1M1JljJ020105@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to gabor@freebsd.org using -f From: Gabor Kovesdan To: Perforce Change Reviews Cc: Subject: PERFORCE change 158046 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 22 Feb 2009 01:19:48 -0000 http://perforce.freebsd.org/chv.cgi?CH=158046 Change 158046 by gabor@gabor_server on 2009/02/22 01:19:13 - Add support for GREP_OPTIONS Requested by: obrien Affected files ... .. //depot/projects/soc2008/gabor_textproc/grep/grep.c#81 edit Differences ... ==== //depot/projects/soc2008/gabor_textproc/grep/grep.c#81 (text+ko) ==== @@ -293,6 +293,8 @@ int c, lastc, prevoptind, newarg, i, needpattern; char *ep; unsigned long long l; + char **eargv, **aargv, *eopts; + int eargc, aargc; setlocale(LC_ALL, ""); @@ -333,8 +335,45 @@ newarg = 1; prevoptind = 1; needpattern = 1; - while ((c = getopt_long(argc, argv, optstr, - long_options, NULL)) != -1) { + + eopts = getenv("GREP_OPTIONS"); + + eargc = 1; + if (eopts != NULL) { + char *str; + + for(i = 0; i < strlen(eopts); i++) + if (eopts[i] == ' ') + eargc++; + + eargv = (char **)grep_malloc(sizeof(char *) * (eargc + 1)); + + str = strtok(eopts, " "); + eargc = 0; + + while(str != NULL) { + eargv[++eargc] = (char *)grep_malloc(sizeof(char) * (strlen(str) + 1)); + strlcpy(eargv[eargc], str, strlen(str) + 1); + str = strtok(NULL, " "); + } + eargv[++eargc] = NULL; + + aargv = (char **)grep_malloc(sizeof(char *) * (eargc + argc + 1)); + aargv[0] = argv[0]; + + for(i = 1; i < eargc; i++) + aargv[i] = eargv[i]; + for(int j = 1; j < argc; j++) + aargv[i++] = argv[j]; + + aargc = eargc + argc - 1; + + } else { + aargv = argv; + aargc = argc; + } + + while (((c = getopt_long(aargc, aargv, optstr, long_options, NULL)) != -1)) { switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': @@ -536,18 +575,18 @@ newarg = optind != prevoptind; prevoptind = optind; } - argc -= optind; - argv += optind; + aargc -= optind; + aargv += optind; /* Fail if we don't have any pattern */ - if (argc == 0 && needpattern) + if (aargc == 0 && needpattern) usage(); /* Process patterns from command line */ - if (argc != 0 && needpattern) { - add_pattern(*argv, strlen(*argv)); - --argc; - ++argv; + if (aargc != 0 && needpattern) { + add_pattern(*aargv, strlen(*aargv)); + --aargc; + ++aargv; } switch (grepbehave) { @@ -588,17 +627,19 @@ if (lbflag) setlinebuf(stdout); - if ((argc == 0 || argc == 1) && !Hflag) + if ((aargc == 0 || aargc == 1) && !Hflag) hflag = 1; - if (argc == 0) + if (aargc == 0) exit(!procfile("-")); if (dirbehave == DIR_RECURSE) - c = grep_tree(argv); + c = grep_tree(aargv); else - for (c = 0; argc--; ++argv) - c+= procfile(*argv); + for (c = 0; aargc--; ++aargv) { + printf("aargc: %d\n", aargc); + c+= procfile(*aargv); + } #ifndef WITHOUT_NLS catclose(catalog);