Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/error - use getopt



details:   https://anonhg.NetBSD.org/src/rev/9c859cd6f954
branches:  trunk
changeset: 765122:9c859cd6f954
user:      christos <christos%NetBSD.org@localhost>
date:      Thu May 19 22:55:53 2011 +0000

description:
- use getopt
- use err
- add -p <filelevel> to chop levels like patch
- document all the options

diffstat:

 usr.bin/error/error.1 |   21 ++++++-
 usr.bin/error/error.h |    4 +-
 usr.bin/error/main.c  |  132 +++++++++++++++++++++++++-------------------------
 usr.bin/error/touch.c |   50 ++++++++++++++----
 4 files changed, 123 insertions(+), 84 deletions(-)

diffs (truncated from 425 to 300 lines):

diff -r e9ccf7f5bc02 -r 9c859cd6f954 usr.bin/error/error.1
--- a/usr.bin/error/error.1     Thu May 19 22:23:12 2011 +0000
+++ b/usr.bin/error/error.1     Thu May 19 22:55:53 2011 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: error.1,v 1.14 2010/04/05 21:18:20 joerg Exp $
+.\"    $NetBSD: error.1,v 1.15 2011/05/19 22:55:53 christos Exp $
 .\"
 .\" Copyright (c) 1980, 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"    @(#)error.1     8.1 (Berkeley) 6/6/93
 .\"
-.Dd June 6, 1993
+.Dd May 19, 2011
 .Dt ERROR 1
 .Os
 .Sh NAME
@@ -37,12 +37,15 @@
 .Nd analyze and disperse compiler error messages
 .Sh SYNOPSIS
 .Nm
+.Op Fl I Ar ignorefile
 .Op Fl n
-.Op Fl s
+.Pp Fl p Ar filelevel
 .Op Fl q
-.Op Fl v
+.Op Fl S
+.Op Fl s
+.Op Fl T
 .Op Fl t Ar suffixlist
-.Op Fl I Ar ignorefile
+.Op Fl v
 .Op name
 .Sh DESCRIPTION
 .Nm
@@ -83,6 +86,8 @@
 or
 .Xr \&ed 1
 from standard places.
+.It Fl T
+Terse output.
 .It Fl t
 Take the following argument as a suffix list.
 Files whose suffixes do not appear in the suffix list are not touched.
@@ -94,6 +99,12 @@
 allows
 .Nm
 to touch files ending with ``.c'', ``.y'', ``.foo*'' and ``.h''.
+.It Fl p Ar filelevel
+Interpret filenumber as a level of path component names to skip,
+similar to 
+.Xr patch 1 .
+.It Fl S
+Show the errors in unsorted order (as they come from the error file).
 .It Fl s
 Print out
 .Em statistics
diff -r e9ccf7f5bc02 -r 9c859cd6f954 usr.bin/error/error.h
--- a/usr.bin/error/error.h     Thu May 19 22:23:12 2011 +0000
+++ b/usr.bin/error/error.h     Thu May 19 22:55:53 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: error.h,v 1.15 2009/08/13 06:59:37 dholland Exp $      */
+/*     $NetBSD: error.h,v 1.16 2011/05/19 22:55:53 christos Exp $      */
 
 /*
  * Copyright (c) 1980, 1993
@@ -88,6 +88,8 @@
 extern const char *class_table[];
 extern int class_count[];
 
+extern size_t filelevel;
+
 #define nunknown       class_count[C_UNKNOWN]
 #define nignore                class_count[C_IGNORE]
 #define nsyncerrors    class_count[C_SYNC]
diff -r e9ccf7f5bc02 -r 9c859cd6f954 usr.bin/error/main.c
--- a/usr.bin/error/main.c      Thu May 19 22:23:12 2011 +0000
+++ b/usr.bin/error/main.c      Thu May 19 22:55:53 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.17 2009/08/13 06:59:37 dholland Exp $       */
+/*     $NetBSD: main.c,v 1.18 2011/05/19 22:55:53 christos Exp $       */
 
 /*
  * Copyright (c) 1980, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)main.c     8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: main.c,v 1.17 2009/08/13 06:59:37 dholland Exp $");
+__RCSID("$NetBSD: main.c,v 1.18 2011/05/19 22:55:53 christos Exp $");
 #endif /* not lint */
 
 #include <signal.h>
@@ -48,6 +48,7 @@
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
+#include <err.h>
 #include "error.h"
 #include "pathnames.h"
 
@@ -57,6 +58,7 @@
 int nignored;
 char **names_ignored;
 
+size_t filelevel = 0;
 int nerrors = 0;
 Eptr er_head;
 static Eptr *errors;
@@ -81,11 +83,10 @@
 static int errorsort(const void *, const void *);
 static void forkvi(int, char **);
 static void try(const char *, int, char **);
+static void usage(void) __attribute__((__noreturn__));
 
 /*
- * error [-I ignorename] [-n] [-q] [-t suffixlist] [-s] [-v] [infile]
- *
- *     -T:     terse output
+ * error [-nqSsTv] [-I <ignorename>] [-t <suffixlist>] [-p <level>] <infile>
  *
  *     -I:     the following name, `ignorename' contains a list of
  *             function names that are not to be treated as hard errors.
@@ -93,42 +94,38 @@
  *
  *     -n:     don't touch ANY files!
  *
+ *     -p:     take the next argument as the number of levels to skip
+ *             from the filename, like perl.
+ *
  *     -q:     The user is to be queried before touching each
  *             file; if not specified, all files with hard, non
  *             ignorable errors are touched (assuming they can be).
  *
+ *     -S:     show the errors in unsorted order
+ *             (as they come from the error file)
+ *
+ *     -s:     print a summary of the error's categories.
+ *
+ *     -T:     terse output
+ *
  *     -t:     touch only files ending with the list of suffixes, each
  *             suffix preceded by a dot.
  *             eg, -t .c.y.l
  *             will touch only files ending with .c, .y or .l
  *
- *     -s:     print a summary of the error's categories.
- *
  *     -v:     after touching all files, overlay vi(1), ex(1) or ed(1)
  *             on top of error, entered in the first file with
  *             an error in it, with the appropriate editor
  *             set up to use the "next" command to get the other
  *             files containing errors.
  *
- *     -p:     (obsolete: for older versions of pi without bug
- *             fix regarding printing out the name of the main file
- *             with an error in it)
- *             Take the following argument and use it as the name of
- *             the pascal source file, suffix .p
- *
- *     -E:     show the errors in sorted order; intended for
- *             debugging.
- *
- *     -S:     show the errors in unsorted order
- *             (as they come from the error file)
- *
  *     infile: The error messages come from this file.
  *             Default: stdin
  */
 int
 main(int argc, char **argv)
 {
-       char *cp;
+       int c;
        char *ignorename = 0;
        int ed_argc;
        char **ed_argv;         /* return from touchfiles */
@@ -137,60 +134,55 @@
        boolean pr_summary = false;
        boolean edit_files = false;
 
-       processname = argv[0];
+       setprogname(argv[0]);
 
        errorfile = stdin;
-       if (argc > 1)
-               for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) {
-                       for (cp = argv[1] + 1; *cp; cp++)
-                               switch (*cp) {
-                               default:
-                                       fprintf(stderr, "%s: -%c: Unknown flag\n",
-                                               processname, *cp);
-                                       break;
+       while ((c = getopt(argc, argv, "I:np:qSsTt:v")) != -1)
+               switch (c) {
+               case 'I':       /*ignore file name*/
+                       ignorename = optarg;
+                       break;
+               case 'n':
+                       notouch = true;
+                       break;
+               case 'p':
+                       filelevel = (size_t)strtol(optarg, NULL, 0);
+                       break;
+               case 'q':
+                       query = true;
+                       break;
+               case 'S':
+                       Show_Errors = true;
+                       break;
+               case 's':
+                       pr_summary = true;
+                       break;
+               case 'T':
+                       terse = true;
+                       break;
+               case 't':
+                       suffixlist = optarg;
+                       break;
+               case 'v':
+                       edit_files = true;
+                       break;
+               default:
+                       usage();
+               }
 
-                               case 'n': notouch = true; break;
-                               case 'q': query = true; break;
-                               case 'S': Show_Errors = true; break;
-                               case 's': pr_summary = true; break;
-                               case 'v': edit_files = true; break;
-                               case 'T': terse = true; break;
-                               case 't':
-                                       *cp-- = 0; argv++; argc--;
-                                       if (argc > 1) {
-                                               suffixlist = argv[1];
-                                       }
-                                       break;
-                               case 'I':       /*ignore file name*/
-                                       *cp-- = 0;
-                                       argv++;
-                                       argc--;
-                                       if (argc > 1)
-                                               ignorename = argv[1];
-                                       break;
-                               }
-               }
+       argv += optind;
+       argc -= optind;
        if (notouch)
                suffixlist = 0;
        if (argc > 1) {
-               if (argc > 3) {
-                       fprintf(stderr, "%s: Only takes 0 or 1 arguments\n",
-                               processname);
-                       exit(3);
-               }
-               if ((errorfile = fopen(argv[1], "r")) == NULL) {
-                       fprintf(stderr, "%s: %s: No such file or directory for reading errors.\n",
-                               processname, argv[1]);
-                       exit(4);
-               }
+               if (argc > 3)
+                       usage();
+               if ((errorfile = fopen(argv[1], "r")) == NULL)
+                       err(1, "Cannot open `%s' to read errors", argv[1]);
        }
        if ((queryfile = fopen(im_on, "r")) == NULL) {
-               if (query) {
-                       fprintf(stderr,
-                               "%s: Can't open \"%s\" to query the user.\n",
-                               processname, im_on);
-                       exit(9);
-               }
+               if (query)
+                       err(1, "Cannot open `%s' to query the user", im_on);
        }
        if (signal(SIGINT, onintr) == SIG_IGN)
                signal(SIGINT, SIG_IGN);
@@ -301,3 +293,11 @@
        }
        return (order);
 }
+
+static void
+usage(void)
+{
+       fprintf(stderr, "Usage: %s [-nqSsTv] [-I <ignorename>] "
+           "[-t <suffixlist>] [-p <level>] <infile>\n", getprogname());
+       exit(1);
+}
diff -r e9ccf7f5bc02 -r 9c859cd6f954 usr.bin/error/touch.c
--- a/usr.bin/error/touch.c     Thu May 19 22:23:12 2011 +0000
+++ b/usr.bin/error/touch.c     Thu May 19 22:55:53 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: touch.c,v 1.22 2009/08/13 06:59:37 dholland Exp $      */
+/*     $NetBSD: touch.c,v 1.23 2011/05/19 22:55:53 christos Exp $      */
 



Home | Main Index | Thread Index | Old Index