1 /* $NetBSD: main.c,v 1.2 2001/02/20 23:57:03 cgd Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #ifndef lint 40 #include <sys/cdefs.h> 41 __RCSID("$NetBSD: main.c,v 1.2 2001/02/20 23:57:03 cgd Exp $"); 42 #endif /* not lint */ 43 44 #include <sys/types.h> 45 #include <sys/ioctl.h> 46 #include <sys/queue.h> 47 48 #include <dev/ic/mlxreg.h> 49 #include <dev/ic/mlxio.h> 50 51 #include <err.h> 52 #include <fcntl.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <ctype.h> 57 #include <unistd.h> 58 #include <getopt.h> 59 60 #include "extern.h" 61 62 const char *mlxname; 63 const char *memf; 64 const char *nlistf; 65 int mlxfd = -1; 66 int verbosity; 67 68 struct cmd { 69 const char *label; 70 int flags; 71 int (*func)(char **); 72 }; 73 #define CMD_DISKS 0x01 74 #define CMD_NOARGS 0x02 75 76 static const struct cmd cmdtab[] = { 77 { "check", CMD_DISKS, cmd_check }, 78 { "config", CMD_NOARGS, cmd_config }, 79 { "cstatus", CMD_NOARGS, cmd_cstatus }, 80 { "detach", CMD_DISKS, cmd_detach }, 81 { "rebuild", 0, cmd_rebuild }, 82 { "rescan", CMD_NOARGS, cmd_rescan }, 83 { "status", CMD_DISKS, cmd_status }, 84 }; 85 86 int 87 main(int argc, char **argv) 88 { 89 const struct cmd *cmd, *maxcmd; 90 const char *cmdname, *dv; 91 int ch, i, rv; 92 93 dv = "/dev/mlx0"; 94 mlx_disk_init(); 95 96 while ((ch = getopt(argc, argv, "af:v")) != -1) { 97 switch (ch) { 98 case 'a': 99 mlx_disk_add_all(); 100 break; 101 102 case 'f': 103 dv = optarg; 104 break; 105 106 case 'v': 107 verbosity++; 108 break; 109 110 default: 111 usage(); 112 /* NOTREACHED */ 113 } 114 } 115 116 if ((cmdname = argv[optind++]) == NULL) 117 usage(); 118 119 for (i = 0; dv[i] != '\0'; i++) 120 if (dv[i] == '/') 121 mlxname = &dv[i + 1]; 122 123 if ((mlxfd = open(dv, O_RDWR)) < 0) 124 err(EXIT_FAILURE, "%s", dv); 125 126 cmd = &cmdtab[0]; 127 maxcmd = &cmdtab[sizeof(cmdtab) / sizeof(cmdtab[0])]; 128 while (cmd < maxcmd) { 129 if (strcmp(cmdname, cmd->label) == 0) 130 break; 131 cmd++; 132 } 133 if (cmd == maxcmd) 134 usage(); 135 136 if ((cmd->flags & CMD_DISKS) != 0) { 137 while (argv[optind] != NULL) 138 mlx_disk_add(argv[optind++]); 139 if (mlx_disk_empty()) 140 usage(); 141 } else if ((cmd->flags & CMD_NOARGS) != 0) 142 if (argv[optind] != NULL) 143 usage(); 144 145 rv = (*cmd->func)(&argv[optind]); 146 close(mlxfd); 147 exit(rv); 148 /* NOTREACHED */ 149 } 150 151 void 152 usage(void) 153 { 154 155 (void)fprintf(stderr, "usage: %s [-f dev] [-av] command [...]\n", 156 getprogname()); 157 exit(EXIT_FAILURE); 158 /* NOTREACHED */ 159 } 160