[BusyBox] -A patch to bb 0.49

Matt Kraai kraai at alumni.carnegiemellon.edu
Thu Feb 1 17:41:07 UTC 2001


On Thu, Feb 01, 2001 at 12:15:12PM -0500, Richard June wrote:
> I've recently begun adding support for -A in bb (I intend to do -A and 
> -C as well(assuming I can figure out an easy way to do A, :-) ) here's 
> the first of the patches, although I'm sure that it could be made 
> smaller and prettier. Suggestions?

Yes.  :)

> --- grep.c      Wed Jan 31 12:18:00 2001
> +++ grep.c.new  Wed Jan 31 12:17:26 2001
> @@ -33,6 +33,10 @@
> extern int optind; /* in unistd.h */
> extern int errno;  /* for use with strerror() */
> 
> +#ifdef LINES_BEFORE_AFTER
> +extern char *optarg;
> +#endif /* LINES_BEFORE_AFTER */
> +
> /* options */
> static int ignore_case       = 0;
> static int print_filename    = 0;
> @@ -42,6 +46,10 @@
> static int invert_search     = 0;
> static int suppress_err_msgs = 0;
> 
> +#ifdef LINES_BEFORE_AFTER
> +static int lines             = 0;
> +#endif /* LINES_BEFORE_AFTER */
> +
> /* globals */
> static regex_t regex; /* storage space for compiled regular expression */
> static int matched; /* keeps track of whether we ever matched */
> @@ -69,7 +77,8 @@
>        int nmatches = 0;
> 
>        while ((line = get_line_from_file(file)) != NULL) {
> -               chomp(line);
> +               if (line[strlen(line)-1] == '\n')
> +                       line[strlen(line)-1] = '\0';

Please revert this change.  The strlen style isn't correct, and it
is also bigger.

>                linenum++;
>                ret = regexec(&regex, line, 0, NULL, 0);
>                if (ret == 0 && !invert_search) { /* match */
> @@ -82,7 +91,21 @@
>                        }
> 
>                        nmatches++;
> +#ifdef LINES_BEFORE_AFTER
> +                       print_matched_line(line, linenum);
> +                       if(lines > 0)
> +                       {
> +                               if((line = get_line_from_file(file)) != 
> NULL)
> +                               {
> +                                       if (line[strlen(line)-1] == '\n')
> +                                               line[strlen(line)-1] = '\0';

Please use chomp here.  The above isn't correct (think about the
empty line).

> +                                       linenum++;
> +                                       print_matched_line(line, linenum);
> +                               }
> +                       }
> +#else
>                        print_matched_line(line, linenum);
> +#endif /* LINES_BEFORE_AFTER */
> 
>                }
>                else if (ret == REG_NOMATCH && invert_search) {
> @@ -92,7 +115,21 @@
>                        }
> 
>                        nmatches++;
> +#ifdef LINES_BEFORE_AFTER
> +                       print_matched_line(line, linenum);
> +                       if(lines > 0)
> +                       {
> +                               if((line = get_line_from_file(file)) != 
> NULL)
> +                               {
> +                                       if (line[strlen(line)-1] == '\n')
> +                                               line[strlen(line)-1] = '\0';

Same as above.

> +                                       linenum++;
> +                                       print_matched_line(line, linenum);
> +                               }
> +                       }
> +#else
>                        print_matched_line(line, linenum);
> +#endif /* LINES_BEFORE_AFTER */
>                }
> 
>                free(line);
> @@ -116,7 +153,7 @@
>        int reflags;
> 
>        /* do normal option parsing */
> -       while ((opt = getopt(argc, argv, "iHhnqvsc")) > 0) {
> +       while ((opt = getopt(argc, argv, "iHhnqvscA:")) > 0) {
>                switch (opt) {
>                        case 'i':
>                                ignore_case++;
> @@ -142,6 +179,11 @@
>                        case 'c':
>                                print_count_only++;
>                                break;
> +#ifdef LINES_BEFORE_AFTER
> +                       case 'A':
> +                               lines = atoi(optarg);
> +                       break;
> +#endif /* LINES_BEFORE_AFTER */

I don't know for sure, but I personally think that this should use
parse_number with the null suffix list.  That way, constructs like

grep -A 23sds pattern

will be complained about rather than silently ignored.  See tail.c
for an example of how to do this (though I should probably move
the null_suffixes structure into utility.c).

Matt





More information about the busybox mailing list