diff options
author | wollman <wollman@FreeBSD.org> | 1995-04-13 21:11:01 +0000 |
---|---|---|
committer | wollman <wollman@FreeBSD.org> | 1995-04-13 21:11:01 +0000 |
commit | cb57ef82b7725eaf891f041e5bd8e7fe7ef3d25b (patch) | |
tree | f4997e98b20c71306d79847a66120ced7d4a96ec /usr.bin/devmenu | |
parent | 81860156cc5e3c0aa7f3cae5f4e89f26bc558bd7 (diff) | |
download | FreeBSD-src-cb57ef82b7725eaf891f041e5bd8e7fe7ef3d25b.zip FreeBSD-src-cb57ef82b7725eaf891f041e5bd8e7fe7ef3d25b.tar.gz |
Another sample devconf application. This one's a menu of devices; read the
man page.
Diffstat (limited to 'usr.bin/devmenu')
-rw-r--r-- | usr.bin/devmenu/Makefile | 9 | ||||
-rw-r--r-- | usr.bin/devmenu/devfilter.c | 195 | ||||
-rw-r--r-- | usr.bin/devmenu/devmenu.1 | 124 | ||||
-rw-r--r-- | usr.bin/devmenu/devmenu.c | 206 | ||||
-rw-r--r-- | usr.bin/devmenu/devmenu.h | 54 | ||||
-rw-r--r-- | usr.bin/devmenu/ifmenu.c | 51 |
6 files changed, 639 insertions, 0 deletions
diff --git a/usr.bin/devmenu/Makefile b/usr.bin/devmenu/Makefile new file mode 100644 index 0000000..38d37bb --- /dev/null +++ b/usr.bin/devmenu/Makefile @@ -0,0 +1,9 @@ +# $Id$ + +PROG= devmenu +SRCS= devmenu.c devfilter.c ifmenu.c +MAN1= devmenu.1 +LDADD+= -ldialog -lncurses +CFLAGS+= -I/sys + +.include <bsd.prog.mk> diff --git a/usr.bin/devmenu/devfilter.c b/usr.bin/devmenu/devfilter.c new file mode 100644 index 0000000..d48a6b8 --- /dev/null +++ b/usr.bin/devmenu/devfilter.c @@ -0,0 +1,195 @@ +/* + * Copyright 1995 Massachusetts Institute of Technology + * + * Permission to use, copy, modify, and distribute this software and + * its documentation for any purpose and without fee is hereby + * granted, provided that both the above copyright notice and this + * permission notice appear in all copies, that both the above + * copyright notice and this permission notice appear in all + * supporting documentation, and that the name of M.I.T. not be used + * in advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. M.I.T. makes + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied + * warranty. + * + * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS + * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT + * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <stdlib.h> +#include <string.h> + +#include <sys/types.h> +#include <sys/param.h> +#include <sys/devconf.h> +#include <sys/proc.h> +#include <vm/vm.h> +#include <sys/sysctl.h> +#include <err.h> +#include <sysexits.h> + +#include "devmenu.h" + +char * +devmenu_toname(const struct devconf *dev) +{ + static char buf[2*MAXDEVNAME]; + snprintf(buf, sizeof buf, "%s%d", dev->dc_name, dev->dc_unit); + return buf; +} + +int +devmenu_filter(const struct devconf *dev, char **userlist) +{ + int i; + char *name; + + if (!userlist) + return 1; + + name = devmenu_toname(dev); + + for (i = 0; userlist[i]; i++) { + if (strcmp(name, userlist[i]) == 0) { + return 1; + } + } + + return 0; +} + +struct devconf ** +devmenu_alldevs(void) +{ + int mib[3]; + size_t size; + int ndevs, i, ndx; + struct devconf **rv; + + size = sizeof ndevs; + mib[0] = CTL_HW; + mib[1] = HW_DEVCONF; + mib[2] = DEVCONF_NUMBER; + + if (sysctl(mib, 3, &ndevs, &size, 0, 0) < 0) { + err(EX_OSERR, "sysctl failed reading hw.devconf.number"); + } + + rv = malloc((ndevs + 1) * sizeof *rv); + if (!rv) { + err(EX_UNAVAILABLE, "malloc(%lu)", + (unsigned long)(ndevs * sizeof *rv)); + } + + for (ndx = 0, i = 1; i <= ndevs; i++) { + mib[2] = i; + if (sysctl(mib, 3, 0, &size, 0, 0) < 0) { + continue; + } + + rv[ndx] = malloc(size); + if (!rv[ndx]) { + err(EX_UNAVAILABLE, "malloc(%lu)", + (unsigned long)size); + } + + if (sysctl(mib, 3, rv[ndx], &size, 0, 0) < 0) { + err(EX_OSERR, "sysctl reading hw.devconf.%d", i); + } + + ndx++; + } + + rv[ndx] = 0; + + return rv; +} + +void +devmenu_freedevs(struct devconf ***devpp) +{ + struct devconf **devp = *devpp; + int i; + + for (i = 0; devp[i]; i++) { + free(devp[i]); + } + + free(devp); + *devpp = 0; +} + +const char * +devmenu_common(const char *title, const char *hfile, char **devnames, + const char *prompt, const char *none, enum dc_class class) +{ + struct devconf **devs; + int s; + unsigned char **items = 0; + int nitems = 0; + int itemindex = 0; + char *name; + int i; + static char resbuf[2*MAXDEVNAME]; + + if (hfile) { + use_helpfile((char *)hfile); + } + devs = devmenu_alldevs(); + + for (i = 0; devs[i]; i++) { + if (class && !(devs[i]->dc_class & class)) + continue; + if (devmenu_filter(devs[i], devnames)) { + ++nitems; + items = realloc(items, 2 * nitems * sizeof *items); + if (!items) { + err(EX_UNAVAILABLE, "malloc(%lu)", + (unsigned long)(2 * nitems * sizeof *items)); + } + + name = devmenu_toname(devs[i]); + + items[itemindex] = strdup(name); + if (!items[itemindex]) { + err(EX_UNAVAILABLE, "strdup-malloc(%lu)", + (unsigned long)(strlen(name) + 1)); + } + + items[itemindex + 1] = devs[i]->dc_descr; + itemindex += 2; + } + } + + if (!nitems) { + dialog_msgbox((char *)title, none, -1, -1, 1); + return "none"; + } + + name = resbuf; + + if(dialog_menu((char *)title, prompt, 24, 78, nitems, nitems, items, + resbuf, 0, 0) != 0) { + name = "none"; + } + + for (i = 0; i < 2 * nitems; i += 2) { + free(items[i]); + } + + free(items); + + devmenu_freedevs(&devs); + return name; +} diff --git a/usr.bin/devmenu/devmenu.1 b/usr.bin/devmenu/devmenu.1 new file mode 100644 index 0000000..c102626 --- /dev/null +++ b/usr.bin/devmenu/devmenu.1 @@ -0,0 +1,124 @@ +.\" +.\" Copyright 1995 Massachusetts Institute of Technology +.\" +.\" Permission to use, copy, modify, and distribute this software and +.\" its documentation for any purpose and without fee is hereby +.\" granted, provided that both the above copyright notice and this +.\" permission notice appear in all copies, that both the above +.\" copyright notice and this permission notice appear in all +.\" supporting documentation, and that the name of M.I.T. not be used +.\" in advertising or publicity pertaining to distribution of the +.\" software without specific, written prior permission. M.I.T. makes +.\" no representations about the suitability of this software for any +.\" purpose. It is provided "as is" without express or implied +.\" warranty. +.\" +.\" THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS +.\" ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, +.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT +.\" SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +.\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $Id$ +.Dd April 13, 1995 +.Dt DEVMENU 1 +.Os FreeBSD 2.1 +.Sh NAME +.Nm devmenu +.Nd present a menu of available devices +.Sh SYNOPSIS +.Nm lsdev +.Op Fl c Ar class +.Op Fl f Ar outfile +.Op Fl h Ar helpfile +.Op Fl t Ar title +.Op Fl nid +.Op Ar name ... +.Sh DESCRIPTION +The +.Nm +program uses the +.Xr dialog 3 +library to present a menu of available devices meeting some set of +criteria. The list of devices is obtained using the +.Xr sysctl 3 +device configuration database +.Pq Dq devconf , +and intersected with the list of +.Ar name s +if one was specified on the command line. The following command-line +flags are supported: +.Bl -tag -width mhmhelpfile +.It Fl c Ar class +specifies a class of devices to further restrict the available +choices. Multiple +.Fl c +options can be specified, in which case the union of the specified +classes is used. The following classes are currently supported: +.Bl -tag -width parallel +.It Li cpu +a processor +.It Li bus +a system bus, or a peripheral bus +.It Li disk +a read-write disk drive +.It Li rodisk +a read-only disk drive (e.g., CD-ROM) +.It Li display +a display device (usually there is only one) +.It Li serial +a serial port or multiport controller +.It Li parallel +a parallel printer port +.It Li netif +a network interface +.It Li misc +everything else +.El +.Pp +.It Fl f Ar outfile +specifies the name a file where the name of the selected device is +written. If no file is specified, the name is written on the standard +output. +.It Fl h Ar helpfile +can be used to specify the name of a file to be displayed when the +user presses the `?' key. +.It Fl t Ar title +specifies the title of the window passed to the +.Xr dialog 3 +library when creating the menu. +.El +.Pp +In addition, there are three flags to request special menus with +unique prompt text and possibly pseudo-devices (but this is not yet +implemented). These are: +.Bl -tag -width xxx +.It Fl n +requests a menu of network interfaces +.It Fl i +requests a menu of possible installation media (i.e., disks, CD-ROMs, +and tapes) +.It Fl d +requests a menu of disks +.El +.Sh SEE ALSO +.Xr sysctl 3 , +.Xr lsdev 8 +.Sh AUTHOR +The +.Nm +program was written by Garrett A. Wollman at the MIT Laboratory for +Computer Science. +.Sh HISTORY +A +.Nm +command first appeared in +.Tn FreeBSD +2.1. diff --git a/usr.bin/devmenu/devmenu.c b/usr.bin/devmenu/devmenu.c new file mode 100644 index 0000000..e1dc7b4 --- /dev/null +++ b/usr.bin/devmenu/devmenu.c @@ -0,0 +1,206 @@ +/* + * Copyright 1995 Massachusetts Institute of Technology + * + * Permission to use, copy, modify, and distribute this software and + * its documentation for any purpose and without fee is hereby + * granted, provided that both the above copyright notice and this + * permission notice appear in all copies, that both the above + * copyright notice and this permission notice appear in all + * supporting documentation, and that the name of M.I.T. not be used + * in advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. M.I.T. makes + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied + * warranty. + * + * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS + * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT + * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * devmenu - present a menu of installed devices of a specified + * class or classes + * + * Garrett A. Wollman, April 1995 + */ + +#ifndef lint +static const char rcsid[] = + "$Id$"; +#endif + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include <sys/types.h> +#include <unistd.h> + +#include <sys/devconf.h> +#include <sysexits.h> +#include <err.h> +#include <dialog.h> + +#include "devmenu.h" + +static enum dc_class interpret_class(char *str); +static void dm_err_exit(int); +static int err_writefn(void *cookie, const char *val, int nchars); +static void usage(const char *); + +static FILE *errfp; + +int +main(int argc, char **argv) +{ + enum dc_class class = 0; + enum mode { GENERIC, NETIF, INSTALL, DISK } mode = GENERIC; + const char *title = 0, *hfile = 0; + char **devnames = 0; + int c; + const char *fn = 0; + const char *sel = "none"; + + while ((c = getopt(argc, argv, "c:t:h:nidf:")) != EOF) { + switch(c) { + case 'c': + class |= interpret_class(optarg); + break; + case 't': + title = optarg; + break; + case 'h': + hfile = optarg; + break; + case 'n': + mode = NETIF; + break; + case 'i': + mode = INSTALL; + break; + case 'd': + mode = DISK; + break; + case 'f': + fn = optarg; + break; + case '?': + default: + usage(argv[0]); + return EX_USAGE; + } + } + + if (optind < argc) { + devnames = &argv[optind]; + } + + errfp = fwopen(0, err_writefn); + if (!errfp) { + err(EX_UNAVAILABLE, "fwopen"); + } + + setvbuf(errfp, (char *)0, _IOLBF, (size_t)0); + + init_dialog(); + err_set_exit(dm_err_exit); + err_set_file(errfp); + + switch(mode) { + case NETIF: + sel = devmenu_netif(title, hfile, devnames); + break; + case INSTALL: + sel = devmenu_common(title, hfile, devnames, + "Select an installation device", + "No installation devices found", + DC_CLS_DISK | DC_CLS_RDISK | DC_CLS_TAPE); + break; + case DISK: + sel = devmenu_common(title, hfile, devnames, + "Select a disk", + "No disks found", + DC_CLS_DISK); + break; + case GENERIC: + sel = devmenu_common(title, hfile, devnames, + "Select a device", + "No devices found", + class); + break; + } + err_set_file(0); + fclose(errfp); + end_dialog(); + err_set_exit(0); + if (fn) { + errfp = fopen(fn, "w"); + if (!errfp) { + err(EX_OSERR, "fopen(%s)", fn); + } + fprintf(errfp, "%s\n", sel); + fclose(errfp); + } else { + printf("%s\n", sel); + } + return 0; +} + +static const char *classes[] = DC_CLASSNAMES; +#define NCLASSES ((sizeof classes)/(sizeof classes[0])) + +static enum dc_class +interpret_class(char *str) +{ + int i; + enum dc_class rv; + + for(i = 1; i < NCLASSES; i++) { + if(! strcmp(classes[i], str)) { + rv = (1 << (i - 1)); + break; + } + } + if (i == NCLASSES) { + err(EX_USAGE, "unknown class `%s'", str); + } + + return rv; +} + +static void +dm_err_exit(int rval) +{ + fflush(errfp); + fclose(errfp); + end_dialog(); + exit(rval); +} + +static int +err_writefn(void *cookie, const char *val, int nchars) +{ + char buf[nchars + 1]; + + strncpy(buf, val, nchars); + buf[nchars] = '\0'; + + dialog_msgbox("Error", buf, -1, -1, 1); + return nchars; +} + +static void +usage(const char *argv0) +{ + fprintf(stderr, "%s: usage:\n%s foo\n", argv0, argv0); +} diff --git a/usr.bin/devmenu/devmenu.h b/usr.bin/devmenu/devmenu.h new file mode 100644 index 0000000..f5b14a1 --- /dev/null +++ b/usr.bin/devmenu/devmenu.h @@ -0,0 +1,54 @@ +/* + * Copyright 1995 Massachusetts Institute of Technology + * + * Permission to use, copy, modify, and distribute this software and + * its documentation for any purpose and without fee is hereby + * granted, provided that both the above copyright notice and this + * permission notice appear in all copies, that both the above + * copyright notice and this permission notice appear in all + * supporting documentation, and that the name of M.I.T. not be used + * in advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. M.I.T. makes + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied + * warranty. + * + * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS + * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT + * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id$ + */ + +/* NB: must include sys/devconf.h before this file */ + +/* Args are: title, help file, device name list */ +extern const char *devmenu_netif(const char *, const char *, char **); + +/* Args are: title, help file, class mask, name list */ +extern const char *devmenu(const char *, const char *, enum dc_class, char **); + +extern char *devmenu_toname(const struct devconf *); + +/* Args are: device, user's list of devices */ +extern int devmenu_filter(const struct devconf *, char **); + +/* Returns an array of pointers to all the devconf devices in the system */ +extern struct devconf **devmenu_alldevs(void); + +/* Frees the array allocated above */ +extern void devmenu_freedevs(struct devconf ***); + +/* Args are: title, help file, name list, prompt, no-devices message, class */ +extern const char *devmenu_common(const char *, const char *, char **, + const char *, const char *, enum dc_class); + diff --git a/usr.bin/devmenu/ifmenu.c b/usr.bin/devmenu/ifmenu.c new file mode 100644 index 0000000..aecbb7f --- /dev/null +++ b/usr.bin/devmenu/ifmenu.c @@ -0,0 +1,51 @@ +/* + * Copyright 1995 Massachusetts Institute of Technology + * + * Permission to use, copy, modify, and distribute this software and + * its documentation for any purpose and without fee is hereby + * granted, provided that both the above copyright notice and this + * permission notice appear in all copies, that both the above + * copyright notice and this permission notice appear in all + * supporting documentation, and that the name of M.I.T. not be used + * in advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. M.I.T. makes + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied + * warranty. + * + * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS + * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT + * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * ifmenu - present a menu of network interfaces + * + * Garrett A. Wollman, April 1995 + */ + +#include <sys/types.h> +#include <sys/devconf.h> +#include "devmenu.h" + +/* + * This is provided in a separate file so that, in the future, non-physical + * network interfaces might be added as well. + */ +const char * +devmenu_netif(const char *title, const char *hfile, char **devnames) +{ + return devmenu_common(title, hfile, devnames, + "Select a network interface", + "No network interfaces available", + DC_CLS_NETIF); +} |