diff options
author | Federico G. Schwindt <fgsch@cvs.openbsd.org> | 2010-08-20 00:20:56 +0000 |
---|---|---|
committer | Federico G. Schwindt <fgsch@cvs.openbsd.org> | 2010-08-20 00:20:56 +0000 |
commit | 4028699c62453407e9248e833fbfbcc4dab7be21 (patch) | |
tree | 62d9889ea085bcbabb39045e78049b8918093b82 /sbin/wsconsctl | |
parent | b0821890140f70e9e8358a5d7d1764f182999781 (diff) |
Don't silently ignore errors if we cannot open the device. While I'm here
simplify the code and apply some style(9).
Discussed with and ok miod@
Diffstat (limited to 'sbin/wsconsctl')
-rw-r--r-- | sbin/wsconsctl/display.c | 29 | ||||
-rw-r--r-- | sbin/wsconsctl/keyboard.c | 23 | ||||
-rw-r--r-- | sbin/wsconsctl/mouse.c | 26 | ||||
-rw-r--r-- | sbin/wsconsctl/wsconsctl.c | 288 | ||||
-rw-r--r-- | sbin/wsconsctl/wsconsctl.h | 44 |
5 files changed, 176 insertions, 234 deletions
diff --git a/sbin/wsconsctl/display.c b/sbin/wsconsctl/display.c index faaea19b75c..a1ca974b030 100644 --- a/sbin/wsconsctl/display.c +++ b/sbin/wsconsctl/display.c @@ -1,4 +1,4 @@ -/* $OpenBSD: display.c,v 1.15 2010/07/01 16:47:58 maja Exp $ */ +/* $OpenBSD: display.c,v 1.16 2010/08/20 00:20:55 fgsch Exp $ */ /* $NetBSD: display.c,v 1.1 1998/12/28 14:01:16 hannken Exp $ */ /*- @@ -72,7 +72,7 @@ struct field display_field_tab[] = { #define fillioctl(n) { cmd = n; cmd_str = #n; } void -display_get_values(const char *pre, int fd) +display_get_values(int fd) { struct wsdisplay_addscreendata gscr; struct wsdisplay_param param; @@ -178,7 +178,7 @@ display_get_values(const char *pre, int fd) } int -display_put_values(const char *pre, int fd) +display_put_values(int fd) { struct wsdisplay_param param; struct wsdisplay_burner burners; @@ -261,23 +261,14 @@ display_put_values(const char *pre, int fd) return 0; } -int -display_next_device(int *index) +char * +display_next_device(int index) { - char devname[20]; - int fd = -1; - - snprintf(devname, sizeof(devname), "/dev/tty%c0", *index + 'C'); + static char devname[20]; - if (*index > 7) { - *index = -1; - return(fd); - } + if (index > 7) + return (NULL); - if ((fd = open(devname, O_WRONLY)) < 0 && - (fd = open(devname, O_RDONLY)) < 0) { - if (errno != ENXIO) - *index = -1; - } - return(fd); + snprintf(devname, sizeof(devname), "/dev/tty%c0", index + 'C'); + return (devname); } diff --git a/sbin/wsconsctl/keyboard.c b/sbin/wsconsctl/keyboard.c index d1462e4caf3..bc617e43ed9 100644 --- a/sbin/wsconsctl/keyboard.c +++ b/sbin/wsconsctl/keyboard.c @@ -1,4 +1,4 @@ -/* $OpenBSD: keyboard.c,v 1.9 2010/07/01 16:47:58 maja Exp $ */ +/* $OpenBSD: keyboard.c,v 1.10 2010/08/20 00:20:55 fgsch Exp $ */ /* $NetBSD: keyboard.c 1.1 1998/12/28 14:01:17 hannken Exp $ */ /*- @@ -70,7 +70,7 @@ struct field keyboard_field_tab[] = { }; void -keyboard_get_values(const char *pre, int fd) +keyboard_get_values(int fd) { if (field_by_value(keyboard_field_tab, &kbtype)->flags & FLG_GET) if (ioctl(fd, WSKBDIO_GTYPE, &kbtype) < 0) @@ -134,7 +134,7 @@ keyboard_get_values(const char *pre, int fd) } int -keyboard_put_values(const char *pre, int fd) +keyboard_put_values(int fd) { bell.which = 0; if (field_by_value(keyboard_field_tab, &bell.pitch)->flags & FLG_SET) @@ -207,18 +207,11 @@ keyboard_put_values(const char *pre, int fd) return 0; } -int -keyboard_next_device(int *index) +char * +keyboard_next_device(int index) { - char devname[20]; - int fd; - - snprintf(devname, sizeof(devname), "/dev/wskbd%d", *index); + static char devname[20]; - if ((fd = open(devname, O_WRONLY)) < 0 && - (fd = open(devname, O_RDONLY)) < 0) { - if (errno != ENXIO) - *index = -1; - } - return(fd); + snprintf(devname, sizeof(devname), "/dev/wskbd%d", index); + return (devname); } diff --git a/sbin/wsconsctl/mouse.c b/sbin/wsconsctl/mouse.c index b43b4d9ff68..e9dd90fe03f 100644 --- a/sbin/wsconsctl/mouse.c +++ b/sbin/wsconsctl/mouse.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mouse.c,v 1.10 2010/07/01 16:47:58 maja Exp $ */ +/* $OpenBSD: mouse.c,v 1.11 2010/08/20 00:20:55 fgsch Exp $ */ /* $NetBSD: mouse.c,v 1.3 1999/11/15 13:47:30 ad Exp $ */ /*- @@ -56,7 +56,7 @@ struct field mouse_field_tab[] = { }; void -mouse_get_values(const char *pre, int fd) +mouse_get_values(int fd) { if (field_by_value(mouse_field_tab, &mstype)->flags & FLG_GET) if (ioctl(fd, WSMOUSEIO_GTYPE, &mstype) < 0) @@ -84,7 +84,7 @@ mouse_get_values(const char *pre, int fd) } int -mouse_put_values(const char *pre, int fd) +mouse_put_values(int fd) { if (field_by_value(mouse_field_tab, &resolution)->flags & FLG_SET) { if (ioctl(fd, WSMOUSEIO_SRES, &resolution) < 0) { @@ -111,12 +111,13 @@ mouse_put_values(const char *pre, int fd) } } if (field_by_value(mouse_field_tab, &wmcoords)->flags & FLG_SET) { - if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &wmcoords_save) < 0) + if (ioctl(fd, WSMOUSEIO_GCALIBCOORDS, &wmcoords_save) < 0) { if (errno == ENOTTY) field_by_value(mouse_field_tab, &wmcoords)->flags |= FLG_DEAD; else warn("WSMOUSEIO_GCALIBCOORDS"); + } wmcoords.samplelen = wmcoords_save.samplelen; if (ioctl(fd, WSMOUSEIO_SCALIBCOORDS, &wmcoords) < 0) { if (errno == ENOTTY) { @@ -132,18 +133,11 @@ mouse_put_values(const char *pre, int fd) return 0; } -int -mouse_next_device(int *index) +char * +mouse_next_device(int index) { - char devname[20]; - int fd; + static char devname[20]; - snprintf(devname, sizeof(devname), "/dev/wsmouse%d", *index); - - if ((fd = open(devname, O_WRONLY)) < 0 && - (fd = open(devname, O_RDONLY)) < 0) { - if (errno != ENXIO) - *index = -1; - } - return(fd); + snprintf(devname, sizeof(devname), "/dev/wsmouse%d", index); + return (devname); } diff --git a/sbin/wsconsctl/wsconsctl.c b/sbin/wsconsctl/wsconsctl.c index a82d4323bb2..3141d8d208d 100644 --- a/sbin/wsconsctl/wsconsctl.c +++ b/sbin/wsconsctl/wsconsctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: wsconsctl.c,v 1.25 2010/07/01 16:47:58 maja Exp $ */ +/* $OpenBSD: wsconsctl.c,v 1.26 2010/08/20 00:20:55 fgsch Exp $ */ /* $NetBSD: wsconsctl.c,v 1.2 1998/12/29 22:40:20 hannken Exp $ */ /*- @@ -32,6 +32,7 @@ #include <fcntl.h> #include <err.h> +#include <errno.h> #include <string.h> #include <stdio.h> #include <stdlib.h> @@ -44,14 +45,14 @@ extern struct field keyboard_field_tab[]; extern struct field mouse_field_tab[]; extern struct field display_field_tab[]; -void usage(char *); +void usage(void); struct vartypesw { - const char *name; + const char *name; struct field *field_tab; - void (*getval)(const char *pre, int); - int (*putval)(const char *pre, int); - int (*nextdev)(int *); + void (*getval)(int); + int (*putval)(int); + char * (*nextdev)(int); } typesw[] = { { "keyboard", keyboard_field_tab, keyboard_get_values, keyboard_put_values, keyboard_next_device }, @@ -65,18 +66,14 @@ struct vartypesw { struct vartypesw *tab_by_name(const char *, int *); void -usage(char *msg) +usage() { - if (msg != NULL) - fprintf(stderr, "%s: %s\n", __progname, msg); - fprintf(stderr, "usage: %s [-an]\n" " %s [-n] [-f file] name ...\n" " %s [-n] [-f file] name=value ...\n" " %s [-n] [-f file] name+=value ...\n", __progname, __progname, __progname, __progname); - exit(1); } @@ -87,6 +84,7 @@ main(int argc, char *argv[]) struct vartypesw *sw = NULL; char *getsep = "=", *setsep = " -> ", *p; char *wdev = NULL; + char *device; struct field *f; char devname[20]; @@ -105,7 +103,7 @@ main(int argc, char *argv[]) /* compat */ break; default: - usage(NULL); + usage(); } } @@ -113,209 +111,173 @@ main(int argc, char *argv[]) argv += optind; if (argc > 0 && aflag != 0) - usage("excess arguments after -a"); + errx(1, "excess arguments after -a"); if (argc == 0) aflag = 1; if (aflag != 0) { for (sw = typesw; sw->name; sw++) { - devidx = 0; - while (devidx >= 0) { - devfd = (*sw->nextdev)(&devidx); - if (devidx < 0) - continue; + for (devidx = 0;; devidx++) { + device = (*sw->nextdev)(devidx); + if (!device || + ((devfd = open(device, O_WRONLY)) < 0 && + (devfd = open(device, O_RDONLY)) < 0)) { + if (!device || errno != ENXIO) { + if (device && errno != ENOENT) { + warn("%s", device); + error = 1; + } + break; + } else + continue; + } if (devidx == 0) - snprintf(devname, - sizeof(devname), - "%s", - sw->name); + snprintf(devname, sizeof(devname), + "%s", sw->name); else - snprintf(devname, - sizeof(devname), - "%s%d", - sw->name, - devidx); - devidx++; - - if (devfd < 0) - continue; + snprintf(devname, sizeof(devname), + "%s%d", sw->name, devidx); for (f = sw->field_tab; f->name; f++) - if ((f->flags & (FLG_NOAUTO|FLG_WRONLY)) == 0) + if (!(f->flags & + (FLG_NOAUTO|FLG_WRONLY))) f->flags |= FLG_GET; - (*sw->getval)(devname, devfd); + (*sw->getval)(devfd); for (f = sw->field_tab; f->name; f++) if (f->flags & FLG_DEAD) continue; else if (f->flags & FLG_NOAUTO) - warnx("Use explicit arg to view %s.%s.", - devname, f->name); + warnx("Use explicit arg to " + "view %s.%s.", + devname, f->name); else if (f->flags & FLG_GET) pr_field(devname, f, getsep); } } } else if (argc > 0) { for (i = 0; i < argc; i++) { - p = strchr(argv[i], '='); - if (p == NULL) { - sw = tab_by_name(argv[i], &devidx); - if (!sw) - continue; + sw = tab_by_name(argv[i], &devidx); + if (!sw) + continue; + + if (!wdev) + device = (*sw->nextdev)(devidx); + else + device = wdev; - devfd = (*sw->nextdev)(&devidx); - if (devidx < 0) { + if (!device || + ((devfd = open(device, O_WRONLY)) < 0 && + (devfd = open(device, O_RDONLY)) < 0)) { + if (!device) { const char *c = strchr(argv[i], '.'); int k; if (!c) c = strchr(argv[i], '\0'); k = c - argv[i]; - warnx("%*.*s: no such variable", k, k, argv[i]); - continue; - } - - if (devidx == 0) - snprintf(devname, - sizeof(devname), - "%s", - sw->name); - else - snprintf(devname, - sizeof(devname), - "%s%d", - sw->name, - devidx); - - if (wdev != NULL && - (devfd = open(wdev, O_WRONLY)) < 0 && - (devfd = open(wdev, O_RDONLY)) < 0) { - warn("open: %s", wdev); - error = 1; - continue; - } - - if (devfd < 0) - continue; + warnx("%*.*s: no such variable", + k, k, argv[i]); + } else + warn("%s", device); + error = 1; + continue; + } - if (!strchr(argv[i],'.')) { + if (devidx == 0) + snprintf(devname, sizeof(devname), + "%s", sw->name); + else + snprintf(devname, sizeof(devname), + "%s%d", sw->name, devidx); + p = strchr(argv[i], '='); + if (p == NULL) { + if (!strchr(argv[i], '.')) { for (f = sw->field_tab; f->name; f++) - if ((f->flags & (FLG_NOAUTO|FLG_WRONLY)) == 0) + if (!(f->flags & + (FLG_NOAUTO|FLG_WRONLY))) f->flags |= FLG_GET; - (*sw->getval)(devname, devfd); + (*sw->getval)(devfd); for (f = sw->field_tab; f->name; f++) if (f->flags & FLG_DEAD) continue; else if (f->flags & FLG_NOAUTO) - warnx("Use explicit arg to view %s.%s.", - devname, f->name); + warnx("Use explicit " + "arg to view " + "%s.%s.", + devname, f->name); else if (f->flags & FLG_GET) - pr_field(devname, f, getsep); - + pr_field(devname, f, + getsep); continue; } f = field_by_name(sw->field_tab, argv[i]); if (f->flags & FLG_DEAD) continue; - if ((f->flags & FLG_WRONLY) != 0) { + if ((f->flags & FLG_WRONLY)) { warnx("%s: write only", argv[i]); continue; } f->flags |= FLG_GET; - (*sw->getval)(devname, devfd); + (*sw->getval)(devfd); if (f->flags & FLG_DEAD) continue; pr_field(devname, f, getsep); - - continue; - } - if (!strchr(argv[i],'.') || - (strchr(argv[i],'.') > p)) { - warnx("%s: illegal variable name", argv[i]); - continue; - } - if (p > argv[i] && - (*(p - 1) == '+' || *(p - 1) == '-')) { - do_merge = *(p - 1); - *(p - 1) = '\0'; - } else - do_merge = 0; - *p++ = '\0'; - - sw = tab_by_name(argv[i], &devidx); - if (!sw) - continue; - - devfd = (*sw->nextdev)(&devidx); - if (devidx < 0) { - const char *c = strchr(argv[i], '.'); - int k; - if (!c) - c = strchr(argv[i], '\0'); - k = c - argv[i]; - warnx("%*.*s: no such variable", k, k, argv[i]); - continue; - } - - if (devidx == 0) - snprintf(devname, - sizeof(devname), - "%s", - sw->name); - else - snprintf(devname, - sizeof(devname), - "%s%d", - sw->name, - devidx); - - if (wdev != NULL && - (devfd = open(wdev, O_WRONLY)) < 0 && - (devfd = open(wdev, O_RDONLY)) < 0) { - warn("open: %s", wdev); - error = 1; - continue; - } - - if (devfd < 0) - continue; - - f = field_by_name(sw->field_tab, argv[i]); - if (f->flags & FLG_DEAD) - continue; - if ((f->flags & FLG_RDONLY) != 0) { - warnx("%s: read only", argv[i]); - continue; - } - if (do_merge || f->flags & FLG_INIT) { - if ((f->flags & FLG_MODIFY) == 0) - errx(1, "%s: can only be set", - argv[i]); - f->flags |= FLG_GET; - (*sw->getval)(devname, devfd); - f->flags &= ~FLG_GET; - } - rd_field(f, p, do_merge); - f->flags |= FLG_SET; - putval = (*sw->putval)(devname, devfd); - f->flags &= ~FLG_SET; - if (putval != 0 || f->flags & (FLG_DEAD | FLG_NOAUTO)) - continue; - if (f->flags & FLG_WRONLY) { - pr_field(devname, f, setsep); } else { - f->flags |= FLG_GET; - (*sw->getval)(devname, devfd); + if (!strchr(argv[i], '.') || + (strchr(argv[i], '.') > p)) { + warnx("%s: illegal variable name", + argv[i]); + continue; + } + if (p > argv[i] && + (*(p - 1) == '+' || *(p - 1) == '-')) { + do_merge = *(p - 1); + *(p - 1) = '\0'; + } else + do_merge = 0; + *p++ = '\0'; + + f = field_by_name(sw->field_tab, argv[i]); if (f->flags & FLG_DEAD) continue; - pr_field(devname, f, setsep); + if (f->flags & FLG_RDONLY) { + warnx("%s: read only", argv[i]); + continue; + } + if (do_merge || f->flags & FLG_INIT) { + if (!(f->flags & FLG_MODIFY)) + errx(1, "%s: can only be set", + argv[i]); + f->flags |= FLG_GET; + (*sw->getval)(devfd); + f->flags &= ~FLG_GET; + } + rd_field(f, p, do_merge); + f->flags |= FLG_SET; + putval = (*sw->putval)(devfd); + f->flags &= ~FLG_SET; + if (putval != 0 || + f->flags & (FLG_DEAD|FLG_NOAUTO)) + continue; + if (f->flags & FLG_WRONLY) { + pr_field(devname, f, setsep); + } else { + f->flags |= FLG_GET; + (*sw->getval)(devfd); + if (f->flags & FLG_DEAD) + continue; + pr_field(devname, f, setsep); + } } + + close(devfd); } } else - usage(NULL); + usage(); - return (error); + exit(error); } struct vartypesw * @@ -331,7 +293,7 @@ tab_by_name(const char *var, int *idx) break; if (!p) - p=strchr(var, '\0'); + p = strchr(var, '\0'); if (!sw->name) { i = p - var; @@ -345,15 +307,15 @@ tab_by_name(const char *var, int *idx) i = 0; while (c < p) { if (*c >= '0' && *c <= '9') - i=i*10 + *c - '0'; + i = i * 10 + *c - '0'; else - i=-1; + i = -1; c++; } if (i < 0 || i > 32) { i = p - var; warnx("%*.*s: no such variable", i, i, var); - return(NULL); + return (NULL); } } else i = 0; diff --git a/sbin/wsconsctl/wsconsctl.h b/sbin/wsconsctl/wsconsctl.h index c2cf973b3f3..d4398247a0f 100644 --- a/sbin/wsconsctl/wsconsctl.h +++ b/sbin/wsconsctl/wsconsctl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: wsconsctl.h,v 1.12 2010/07/01 16:47:58 maja Exp $ */ +/* $OpenBSD: wsconsctl.h,v 1.13 2010/08/20 00:20:55 fgsch Exp $ */ /* $NetBSD: wsconsctl.h 1.1 1998/12/28 14:01:17 hannken Exp $ */ /*- @@ -63,24 +63,26 @@ struct field_pc { int max, min, cur; }; -struct field *field_by_name(struct field *, char *); -struct field *field_by_value(struct field *, void *); -void pr_field(const char *, struct field *, const char *); -void rd_field(struct field *, char *, int); -int name2ksym(char *); -char *ksym2name(int); -void ksymenc(int); +struct field * + field_by_name(struct field *, char *); +struct field * + field_by_value(struct field *, void *); +void pr_field(const char *, struct field *, const char *); +void rd_field(struct field *, char *, int); +int name2ksym(char *); +char * ksym2name(int); +void ksymenc(int); keysym_t ksym_upcase(keysym_t); -void keyboard_get_values(const char *,int); -int keyboard_put_values(const char *,int); -int keyboard_next_device(int *); -void mouse_get_values(const char *,int); -int mouse_put_values(const char *,int); -int mouse_next_device(int *); -void display_get_values(const char *,int); -int display_put_values(const char *,int); -int display_next_device(int *); -int yyparse(void); -void yyerror(char *); -int yylex(void); -void map_scan_setinput(char *); +void keyboard_get_values(int); +int keyboard_put_values(int); +char * keyboard_next_device(int); +void mouse_get_values(int); +int mouse_put_values(int); +char * mouse_next_device(int); +void display_get_values(int); +int display_put_values(int); +char * display_next_device(int); +int yyparse(void); +void yyerror(char *); +int yylex(void); +void map_scan_setinput(char *); |