1 /* $NetBSD: main.c,v 1.3 2001/04/17 13:32:39 ad 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.3 2001/04/17 13:32:39 ad 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 59 #include "extern.h" 60 61 const char *mlxname; 62 const char *memf; 63 const char *nlistf; 64 int mlxfd = -1; 65 int verbosity; 66 67 struct cmd { 68 const char *label; 69 int flags; 70 int (*func)(char **); 71 }; 72 #define CMD_DISKS 0x01 73 #define CMD_NOARGS 0x02 74 75 static const struct cmd cmdtab[] = { 76 { "check", CMD_DISKS, cmd_check }, 77 { "config", CMD_NOARGS, cmd_config }, 78 { "cstatus", CMD_NOARGS, cmd_cstatus }, 79 { "detach", CMD_DISKS, cmd_detach }, 80 { "rebuild", 0, cmd_rebuild }, 81 { "rescan", CMD_NOARGS, cmd_rescan }, 82 { "status", CMD_DISKS, cmd_status }, 83 }; 84 85 int 86 main(int argc, char **argv) 87 { 88 const struct cmd *cmd, *maxcmd; 89 const char *cmdname, *dv; 90 int ch, i, rv; 91 92 dv = "/dev/mlx0"; 93 mlx_disk_init(); 94 95 while ((ch = getopt(argc, argv, "af:v")) != -1) { 96 switch (ch) { 97 case 'a': 98 mlx_disk_add_all(); 99 break; 100 101 case 'f': 102 dv = optarg; 103 break; 104 105 case 'v': 106 verbosity++; 107 break; 108 109 default: 110 usage(); 111 /* NOTREACHED */ 112 } 113 } 114 115 if ((cmdname = argv[optind++]) == NULL) 116 usage(); 117 118 for (i = 0; dv[i] != '\0'; i++) 119 if (dv[i] == '/') 120 mlxname = &dv[i + 1]; 121 122 if ((mlxfd = open(dv, O_RDWR)) < 0) 123 err(EXIT_FAILURE, "%s", dv); 124 125 cmd = &cmdtab[0]; 126 maxcmd = &cmdtab[sizeof(cmdtab) / sizeof(cmdtab[0])]; 127 while (cmd < maxcmd) { 128 if (strcmp(cmdname, cmd->label) == 0) 129 break; 130 cmd++; 131 } 132 if (cmd == maxcmd) 133 usage(); 134 135 if ((cmd->flags & CMD_DISKS) != 0) { 136 while (argv[optind] != NULL) 137 mlx_disk_add(argv[optind++]); 138 if (mlx_disk_empty()) 139 usage(); 140 } else if ((cmd->flags & CMD_NOARGS) != 0) 141 if (argv[optind] != NULL) 142 usage(); 143 144 rv = (*cmd->func)(&argv[optind]); 145 close(mlxfd); 146 exit(rv); 147 /* NOTREACHED */ 148 } 149 150 void 151 usage(void) 152 { 153 154 (void)fprintf(stderr, "usage: %s [-f dev] [-av] command [...]\n", 155 getprogname()); 156 exit(EXIT_FAILURE); 157 /* NOTREACHED */ 158 } 159