Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / mlxctl / main.c
blob37c79bc880fa5175ffe756c49ca5c1bb2a9f0f64
1 /* $NetBSD: main.c,v 1.6 2007/02/21 20:53:59 hubertf Exp $ */
3 /*-
4 * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #ifndef lint
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: main.c,v 1.6 2007/02/21 20:53:59 hubertf Exp $");
35 #endif /* not lint */
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/queue.h>
41 #include <dev/ic/mlxreg.h>
42 #include <dev/ic/mlxio.h>
44 #include <err.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
51 #include "extern.h"
53 const char *mlxname;
54 const char *memf;
55 const char *nlistf;
56 struct mlx_cinfo ci;
57 int mlxfd = -1;
58 int verbosity;
60 struct cmd {
61 const char *label;
62 int flags;
63 int (*func)(char **);
65 #define CMD_DISKS 0x01
66 #define CMD_NOARGS 0x02
68 static const struct cmd cmdtab[] = {
69 { "check", CMD_DISKS, cmd_check },
70 { "config", CMD_NOARGS, cmd_config },
71 { "cstatus", CMD_NOARGS, cmd_cstatus },
72 { "detach", CMD_DISKS, cmd_detach },
73 { "rebuild", 0, cmd_rebuild },
74 { "rescan", CMD_NOARGS, cmd_rescan },
75 { "status", CMD_DISKS, cmd_status },
78 int
79 main(int argc, char **argv)
81 const struct cmd *cmd, *maxcmd;
82 const char *cmdname, *dv;
83 int ch, i, rv, all;
85 all = 0;
86 dv = "/dev/mlx0";
87 mlx_disk_init();
89 while ((ch = getopt(argc, argv, "af:v")) != -1) {
90 switch (ch) {
91 case 'a':
92 all = 1;
93 break;
95 case 'f':
96 dv = optarg;
97 break;
99 case 'v':
100 verbosity++;
101 break;
103 default:
104 usage();
105 /* NOTREACHED */
109 if ((cmdname = argv[optind++]) == NULL)
110 usage();
112 for (i = 0; dv[i] != '\0'; i++)
113 if (dv[i] == '/')
114 mlxname = &dv[i + 1];
116 if ((mlxfd = open(dv, O_RDWR)) < 0)
117 err(EXIT_FAILURE, "%s", dv);
119 cmd = &cmdtab[0];
120 maxcmd = &cmdtab[sizeof(cmdtab) / sizeof(cmdtab[0])];
121 while (cmd < maxcmd) {
122 if (strcmp(cmdname, cmd->label) == 0)
123 break;
124 cmd++;
126 if (cmd == maxcmd)
127 usage();
129 if (ioctl(mlxfd, MLX_GET_CINFO, &ci))
130 err(EXIT_FAILURE, "ioctl(MLX_GET_CINFO)");
132 if ((cmd->flags & CMD_DISKS) != 0) {
133 if (all)
134 mlx_disk_add_all();
136 while (argv[optind] != NULL)
137 mlx_disk_add(argv[optind++]);
138 if (mlx_disk_empty())
139 errx(EXIT_FAILURE,
140 "no logical drives attached to this controller");
141 } else if ((cmd->flags & CMD_NOARGS) != 0)
142 if (argv[optind] != NULL)
143 usage();
145 rv = (*cmd->func)(&argv[optind]);
146 close(mlxfd);
147 exit(rv);
148 /* NOTREACHED */
151 void
152 usage(void)
155 (void)fprintf(stderr, "usage: %s [-f dev] [-av] command [...]\n",
156 getprogname());
157 exit(EXIT_FAILURE);
158 /* NOTREACHED */