Module Name: src
Committed By: christos
Date: Mon Feb 29 18:59:53 UTC 2016
Modified Files:
src/usr.sbin/grfconfig: grfconfig.c
Log Message:
PR/50867: David Binderman: Fix parsing loop.
While here, modernize error handling, merge copy and pasted code.
To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/usr.sbin/grfconfig/grfconfig.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.sbin/grfconfig/grfconfig.c
diff -u src/usr.sbin/grfconfig/grfconfig.c:1.15 src/usr.sbin/grfconfig/grfconfig.c:1.16
--- src/usr.sbin/grfconfig/grfconfig.c:1.15 Tue Jan 4 04:32:31 2011
+++ src/usr.sbin/grfconfig/grfconfig.c Mon Feb 29 13:59:52 2016
@@ -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 @@ __COPYRIGHT("@(#) Copyright (c) 1997\
#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 @@ __RCSID("$NetBSD: grfconfig.c,v 1.15 201
#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 @@ static struct grf_flag {
* 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 @@ main(ac, av)
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 @@ main(ac, av)
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;
-
- if (i < 14) {
- printf("grfconfig: too few values in mode "
- "definition file:\n %s\n", obuf);
- (void)fclose(fp);
- (void)close(grffd);
- return (1);
- }
+#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)
+ 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 @@ main(ac, av)
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 @@ main(ac, av)
(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 @@ main(ac, av)
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 @@ main(ac, av)
(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 @@ main(ac, av)
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 @@ main(ac, av)
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 @@ main(ac, av)
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 @@ main(ac, av)
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 @@ main(ac, av)
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 @@ main(ac, av)
printf(" flags:");
if (gv->disp_flags == GRF_FLAGS_DEFAULT) {
- printf(" default");
- } else {
- for (grf_flagp = grf_flags;
- grf_flagp->grf_flag_number; grf_flagp++) {
- if (gv->disp_flags & grf_flagp->grf_flag_number) {
- printf(" %s", grf_flagp->grf_flag_name);
- }
- }
+ printf(" default\n");
+ continue;
}
+
+ for (grf_flagp = grf_flags;
+ grf_flagp->grf_flag_number; grf_flagp++)
+ if (gv->disp_flags & grf_flagp->grf_flag_number)
+ printf(" %s", grf_flagp->grf_flag_name);
printf("\n");
}
}
close(grffd);
- return (0);
+ return EXIT_SUCCESS;
+}
+
+static void
+suggest(struct grfvideo_mode *gv, const char *d, const char *s)
+{
+ warnx("Old and no longer supported %s: %s", d, s);
+ warnx("Wrong mode line, this could be a possible good model line:");
+ fprintf(stderr, "%s: ", getprogname());
+ print_modeline(stderr, gv, 0);
}
static void
-print_rawdata(gv, rawflags)
- struct grfvideo_mode *gv;
- int rawflags;
+print_modeline(FILE *fp, struct grfvideo_mode *gv, int rawflags)
{
struct grf_flag *grf_flagp;
- printf("%ld %d %d %d %d %d %d %d %d %d %d %d",
+ if (gv->mode_num == 255)
+ fprintf(fp, "c ");
+ else
+ fprintf(fp, "%d ", gv->mode_num);
+
+ fprintf(fp, "%ld %d %d %d %d %d %d %d %d %d %d %d",
gv->pixel_clock,
gv->disp_width,
gv->disp_height,
@@ -429,19 +381,18 @@ print_rawdata(gv, rawflags)
gv->vsync_start,
gv->vsync_stop,
gv->vtotal);
- if (rawflags) {
- printf(" 0x%.2x", gv->disp_flags);
- } else {
- if (gv->disp_flags == GRF_FLAGS_DEFAULT) {
- printf(" default");
- } else {
- for (grf_flagp = grf_flags;
- grf_flagp->grf_flag_number; grf_flagp++) {
- if (gv->disp_flags & grf_flagp->grf_flag_number) {
- printf(" %s", grf_flagp->grf_flag_name);
- }
- }
- }
- }
- printf("\n");
+
+ if (rawflags) {
+ fprintf(fp, " 0x%.2x\n", gv->disp_flags);
+ return;
+ }
+ if (gv->disp_flags == GRF_FLAGS_DEFAULT) {
+ fprintf(fp, " default\n");
+ return;
+ }
+
+ for (grf_flagp = grf_flags; grf_flagp->grf_flag_number; grf_flagp++)
+ if (gv->disp_flags & grf_flagp->grf_flag_number)
+ fprintf(fp, " %s", grf_flagp->grf_flag_name);
+ fprintf(fp, "\n");
}