Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/grfconfig PR/50867: David Binderman: Fix parsing loop.



details:   https://anonhg.NetBSD.org/src/rev/50e18d6277c2
branches:  trunk
changeset: 343884:50e18d6277c2
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Feb 29 18:59:52 2016 +0000

description:
PR/50867: David Binderman: Fix parsing loop.
While here, modernize error handling, merge copy and pasted code.

diffstat:

 usr.sbin/grfconfig/grfconfig.c |  229 ++++++++++++++++------------------------
 1 files changed, 90 insertions(+), 139 deletions(-)

diffs (truncated from 382 to 300 lines):

diff -r a8d848b7ec76 -r 50e18d6277c2 usr.sbin/grfconfig/grfconfig.c
--- a/usr.sbin/grfconfig/grfconfig.c    Mon Feb 29 18:25:29 2016 +0000
+++ b/usr.sbin/grfconfig/grfconfig.c    Mon Feb 29 18:59:52 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: grfconfig.c,v 1.15 2011/01/04 09:32:31 wiz Exp $       */
+/*     $NetBSD: grfconfig.c,v 1.16 2016/02/29 18:59:52 christos Exp $  */
 
 /*-
  * Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -36,11 +36,12 @@
 #endif /* not lint */
 
 #ifndef lint
-__RCSID("$NetBSD: grfconfig.c,v 1.15 2011/01/04 09:32:31 wiz Exp $");
+__RCSID("$NetBSD: grfconfig.c,v 1.16 2016/02/29 18:59:52 christos Exp $");
 #endif /* not lint */
 
 #include <sys/file.h>
 #include <sys/ioctl.h>
+#include <err.h>
 #include <ctype.h>
 #include <limits.h>
 #include <stdio.h>
@@ -50,8 +51,8 @@
 
 #include <amiga/dev/grfioctl.h>
 
-int main __P((int, char **));
-static void print_rawdata __P((struct grfvideo_mode *, int));    
+static void print_modeline(FILE *fp, struct grfvideo_mode *, int); 
+static void suggest(struct grfvideo_mode *, const char *, const char *);
 
 static struct grf_flag {
        u_short grf_flag_number;
@@ -71,18 +72,17 @@
  * Dynamic mode loader for NetBSD/Amiga grf devices.
  */
 int
-main(ac, av)
-       int     ac;
-       char  **av;
+main(int ac, char  **av)
 {
        struct  grfvideo_mode gv[1];
        struct  grf_flag *grf_flagp;
        FILE    *fp;
        int     c, y, grffd;
-       int     i, lineno = 0;
+       size_t  i;
+       int     lineno = 0;
        int     uplim, lowlim;
        char    rawdata = 0, testmode = 0;
-       char    *grfdevice = 0;
+       char    *grfdevice = 0, *ptr;
        char    *modefile = 0;
        char    buf[_POSIX2_LINE_MAX];
        char    *cps[31];
@@ -107,29 +107,23 @@
        av += optind;
 
 
-       if (ac >= 1)
-               grfdevice = av[0];
-       else {
-               printf("grfconfig: No grf device specified.\n");
-               return (1);
-       }
+       if (ac < 1)
+               errx(EXIT_FAILURE, "No grf device specified");
+       grfdevice = av[0];
 
        if (ac >= 2)
                modefile = av[1];
 
-       if ((grffd = open(grfdevice, O_RDWR)) < 0) {
-               printf("grfconfig: can't open grf device.\n");
-               return (1);
-       }
+       if ((grffd = open(grfdevice, O_RDWR)) == -1)
+               err(EXIT_FAILURE, "Can't open grf device `%s'", grfdevice);
+
        /* If a mode file is specificied, load it in, don't display any info. */
 
        if (modefile) {
-               if (!(fp = fopen(modefile, "r"))) {
-                       printf("grfconfig: Cannot open mode definition "
-                           "file.\n");
-                       (void)close(grffd);
-                       return (1);
-               }
+               if (!(fp = fopen(modefile, "r")))
+                       err(EXIT_FAILURE, 
+                           "Cannot open mode definition file `%s'", modefile);
+
                while (fgets(buf, sizeof(buf), fp)) {
                        char *obuf, tbuf[_POSIX2_LINE_MAX], *tbuf2;
                        /*
@@ -161,18 +155,17 @@
 
                        lineno = lineno + 1;
 
-                       for (i = 0, *cps = strtok(buf, " \b\t\r\n");
-                           cps[i] != NULL && i < 30; i++)
-                               cps[i + 1] = strtok(NULL, " \b\t\r\n");
-                       cps[i] = NULL;
+#define SP " \b\t\r\n"
+                       memset(cps, 0, sizeof(cps));
+                       for (i = 0, ptr = strtok(buf, SP);
+                           ptr != NULL && i < __arraycount(cps);
+                           i++, ptr = strtok(NULL, SP))
+                               cps[i] = ptr;
 
-                       if (i < 14) {
-                               printf("grfconfig: too few values in mode "
-                                   "definition file:\n %s\n", obuf);
-                               (void)fclose(fp);
-                               (void)close(grffd);
-                               return (1);
-                       }
+
+                       if (i < 14)
+                               errx(EXIT_FAILURE, "Too few values in mode "
+                                   "definition file: `%s'\n", obuf);
 
                        gv->pixel_clock = atoi(cps[1]);
                        gv->disp_width  = atoi(cps[2]);
@@ -194,11 +187,8 @@
                                        gv->mode_num = 255;
                                        gv->depth = 4;
                                } else {
-                                       printf("grfconfig: Illegal mode "
-                                           "number: %s\n", cps[0]);
-                                       (void)fclose(fp);
-                                       (void)close(grffd);
-                                       return (1);
+                                       errx(EXIT_FAILURE,
+                                           "Illegal mode number: %s", cps[0]);
                                }
 
                        if ((gv->pixel_clock == 0) ||
@@ -213,11 +203,8 @@
                            (gv->vsync_start == 0) ||
                            (gv->vsync_stop == 0) ||
                            (gv->vtotal == 0)) {
-                               printf("grfconfig: Illegal value in "
-                                   "mode #%d:\n %s\n", gv->mode_num, obuf);
-                               (void)fclose(fp);
-                               (void)close(grffd);
-                               return (1);  
+                               errx(EXIT_FAILURE, "Illegal value in "
+                                   "mode #%d: `%s'", gv->mode_num, obuf);
                        }
 
                        if (strstr(obuf, "default") != NULL) {
@@ -230,17 +217,10 @@
                                        gv->disp_flags |= grf_flagp->grf_flag_number;
                                    }
                                }
-                               if (gv->disp_flags == GRF_FLAGS_DEFAULT) {
-                                       printf("grfconfig: Your are using an "
+                               if (gv->disp_flags == GRF_FLAGS_DEFAULT)
+                                       errx(EXIT_FAILURE, "Your are using a "
                                            "mode file with an obsolete "
-                                           "format.\n See the manpage of "
-                                           "grfconfig for more information "
-                                           "about the new mode definition "
-                                           "file.\n");
-                                       (void)fclose(fp);
-                                       (void)close(grffd);
-                                       return (1);
-                               }
+                                           "format");
                        }
 
                        /*
@@ -260,14 +240,10 @@
                            (gv->disp_flags & GRF_FLAGS_NVSYNC))
                                errortext = "+vsync and -vsync";
 
-                       if (errortext != NULL) {
-                               printf("grfconfig: Illegal flags in "
-                                   "mode #%d: %s are both defined!\n",
+                       if (errortext != NULL)
+                               errx(EXIT_FAILURE, "Illegal flags in "
+                                   "mode #%d: `%s' are both defined",
                                    gv->mode_num, errortext);
-                               (void)fclose(fp);
-                               (void)close(grffd);
-                               return (1);
-                       }
 
                        /* Check for old horizontal cycle values */
                        if ((gv->htotal < (gv->disp_width / 4))) {
@@ -275,19 +251,9 @@
                                gv->hsync_start *= 8;
                                gv->hsync_stop *= 8;
                                gv->htotal *= 8;
-                               printf("grfconfig: Old and no longer "
-                                   "supported horizontal videoclock cycle "
-                                   "values.\n Wrong mode line:\n  %s\n "
-                                   "This could be a possible good mode "
-                                   "line:\n  ", obuf);
-                               printf("%d ", gv->mode_num);
-                               print_rawdata(gv, 0);
-                               printf(" See the manpage of grfconfig for "
-                                   "more information about the new mode "
-                                   "definition file.\n");
-                               (void)fclose(fp);
-                               (void)close(grffd);
-                               return (1);
+                               suggest(gv, "horizontal videoclock cycle "
+                                   "values", obuf);
+                               return EXIT_FAILURE;
                        }
 
                        /* Check for old interlace or doublescan modes */
@@ -301,19 +267,9 @@
                                gv->vtotal *= 2;
                                gv->disp_flags &= ~GRF_FLAGS_DBLSCAN;
                                gv->disp_flags |= GRF_FLAGS_LACE;
-                               printf("grfconfig: Old and no longer "
-                                   "supported vertical values for "
-                                   "interlace modes.\n Wrong mode "
-                                   "line:\n  %s\n This could be a "
-                                   "possible good mode line:\n  ", obuf);
-                               printf("%d ", gv->mode_num);
-                               print_rawdata(gv, 0);
-                               printf(" See the manpage of grfconfig for "
-                                   "more information about the new mode "
-                                   "definition file.\n");
-                               (void)fclose(fp);
-                               (void)close(grffd);
-                               return (1);
+                               suggest(gv, "vertical values for interlace "
+                                   "modes", obuf);
+                               return EXIT_FAILURE;
                        } else if (((gv->vtotal / 2) > lowlim) &&
                            ((gv->vtotal / 2) < uplim)) {
                                gv->vblank_start /= 2;
@@ -322,19 +278,9 @@
                                gv->vtotal /= 2;
                                gv->disp_flags &= ~GRF_FLAGS_LACE;
                                gv->disp_flags |= GRF_FLAGS_DBLSCAN;
-                               printf("grfconfig: Old and no longer "
-                                   "supported vertical values for "
-                                   "doublescan modes.\n Wrong mode "
-                                   "line:\n  %s\n This could be a "
-                                   "possible good mode line:\n  ", obuf);
-                               printf("%d ", gv->mode_num);
-                               print_rawdata(gv, 0);
-                               printf(" See the manpage of grfconfig for "
-                                   "more information about the new mode "
-                                   "definition file.\n");
-                               (void)fclose(fp);
-                               (void)close(grffd);
-                               return (1);
+                               suggest(gv, "vertical values for doublescan "
+                                   "modes", obuf);
+                               return EXIT_FAILURE;
                        }
 
                        if (testmode == 1) {
@@ -342,13 +288,12 @@
                                        printf("num clk wid hi dep hbs "
                                            "hss hse ht vbs vss vse vt "
                                            "flags\n");
-                               printf("%d ", gv->mode_num);
-                               print_rawdata(gv, 1);
+                               print_modeline(stdout, gv, 1);
                        } else {
                                gv->mode_descr[0] = 0;
                                if (ioctl(grffd, GRFIOCSETMON, (char *) gv) < 0)
-                                       printf("grfconfig: bad monitor "
-                                           "definition for mode #%d.\n",
+                                       err(EXIT_FAILURE, "bad monitor "
+                                           "definition for mode #%d",
                                            gv->mode_num);
                        }
                }
@@ -361,11 +306,7 @@
                        if (ioctl(grffd, GRFGETVMODE, gv) < 0)
                                continue;
                        if (rawdata) {
-                               if (c == 255)
-                                       printf("c ");
-                               else
-                                       printf("%d ", c);
-                               print_rawdata(gv, 0);
+                               print_modeline(stdout, gv, 0);
                                continue;
                        }
                        if (c == 255)
@@ -392,31 +333,42 @@
                        printf(" flags:");
                                
                        if (gv->disp_flags == GRF_FLAGS_DEFAULT) {
-                               printf(" default");
-                       } else {
-                               for (grf_flagp = grf_flags;
-                                 grf_flagp->grf_flag_number; grf_flagp++) {



Home | Main Index | Thread Index | Old Index