main.c revision 1.1
1/*	$NetBSD: main.c,v 1.1 2015/11/30 19:59:34 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2002 Marcel Moolenaar
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * CRC32 code derived from work by Gary S. Brown.
29 */
30
31#if HAVE_NBTOOL_CONFIG_H
32#include "nbtool_config.h"
33#endif
34
35#include <sys/cdefs.h>
36#ifdef __RCSID
37__RCSID("$NetBSD: main.c,v 1.1 2015/11/30 19:59:34 christos Exp $");
38#endif
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <unistd.h>
43#include <err.h>
44
45#include "map.h"
46#include "gpt.h"
47
48static struct {
49	int (*fptr)(int, char *[]);
50	const char *name;
51} cmdsw[] = {
52	{ cmd_add, "add" },
53#ifndef HAVE_NBTOOL_CONFIG_H
54	{ cmd_backup, "backup" },
55#endif
56	{ cmd_biosboot, "biosboot" },
57	{ cmd_create, "create" },
58	{ cmd_destroy, "destroy" },
59	{ cmd_header, "header" },
60	{ NULL, "help" },
61	{ cmd_label, "label" },
62	{ cmd_migrate, "migrate" },
63	{ cmd_recover, "recover" },
64	{ cmd_remove, "remove" },
65	{ NULL, "rename" },
66	{ cmd_resize, "resize" },
67	{ cmd_resizedisk, "resizedisk" },
68#ifndef HAVE_NBTOOL_CONFIG_H
69	{ cmd_restore, "restore" },
70#endif
71	{ cmd_set, "set" },
72	{ cmd_show, "show" },
73	{ cmd_type, "type" },
74	{ cmd_unset, "unset" },
75	{ NULL, "verify" },
76	{ NULL, NULL }
77};
78
79__dead static void
80usage(void)
81{
82	extern const char addmsg1[], addmsg2[], biosbootmsg[];
83	extern const char createmsg[], destroymsg[], headermsg[], labelmsg1[];
84	extern const char labelmsg2[], labelmsg3[], migratemsg[], recovermsg[];
85	extern const char removemsg1[], removemsg2[], resizemsg[];
86	extern const char resizediskmsg[], setmsg[], showmsg[], typemsg1[];
87	extern const char typemsg2[], typemsg3[], unsetmsg[];
88#ifndef HAVE_NBTOOL_CONFIG_H
89	extern const char backupmsg[], restoremsg[];
90#endif
91	const char *p = getprogname();
92	const char *f =
93	    "[-nrqv] [-m <mediasize>] [-p <partitionnum>] [-s <sectorsize>]";
94
95	fprintf(stderr,
96	    "Usage: %s %s <command> [<args>]\n", p, f);
97	fprintf(stderr,
98	    "Commands:\n"
99#ifndef HAVE_NBTOOL_CONFIG_H
100	    "       %s\n"
101	    "       %s\n"
102#endif
103	    "       %s\n"
104	    "       %s\n"
105	    "       %s\n"
106	    "       %s\n"
107	    "       %s\n"
108	    "       %s\n"
109	    "       %s\n"
110	    "       %s\n"
111	    "       %s\n"
112	    "       %s\n"
113	    "       %s\n"
114	    "       %s\n"
115	    "       %s\n"
116	    "       %s\n"
117	    "       %s\n"
118	    "       %s\n"
119	    "       %s\n"
120	    "       %s\n"
121	    "       %s\n"
122	    "       %s\n"
123	    "       %s\n",
124	    addmsg1, addmsg2,
125#ifndef HAVE_NBTOOL_CONFIG_H
126	    backupmsg,
127#endif
128	    biosbootmsg, createmsg, destroymsg,
129	    headermsg, labelmsg1, labelmsg2, labelmsg3,
130	    migratemsg, recovermsg,
131	    removemsg1, removemsg2,
132	    resizemsg, resizediskmsg,
133#ifndef HAVE_NBTOOL_CONFIG_H
134	    restoremsg,
135#endif
136	    setmsg, showmsg,
137	    typemsg1, typemsg2, typemsg3,
138	    unsetmsg);
139	exit(1);
140}
141
142static void
143prefix(const char *cmd)
144{
145	char *pfx;
146
147	if (asprintf(&pfx, "%s %s", getprogname(), cmd) < 0)
148		pfx = NULL;
149	else
150		setprogname(pfx);
151}
152
153int
154main(int argc, char *argv[])
155{
156	char *cmd, *p;
157	int ch, i;
158
159	/* Get the generic options */
160	while ((ch = getopt(argc, argv, "m:np:qrs:v")) != -1) {
161		switch(ch) {
162		case 'm':
163			if (mediasz > 0)
164				usage();
165			mediasz = strtoul(optarg, &p, 10);
166			if (*p != 0 || mediasz < 1)
167				usage();
168			break;
169		case 'n':
170			nosync = 1;
171			break;
172		case 'p':
173			if (parts > 0)
174				usage();
175			parts = strtoul(optarg, &p, 10);
176			if (*p != 0 || parts < 1)
177				usage();
178			break;
179		case 'r':
180			readonly = 1;
181			break;
182		case 'q':
183			quiet = 1;
184			break;
185		case 's':
186			if (secsz > 0)
187				usage();
188			secsz = strtoul(optarg, &p, 10);
189			if (*p != 0 || secsz < 1)
190				usage();
191			break;
192		case 'v':
193			verbose++;
194			break;
195		default:
196			usage();
197		}
198	}
199	if (!parts)
200		parts = 128;
201
202	if (argc == optind)
203		usage();
204
205	cmd = argv[optind++];
206	for (i = 0; cmdsw[i].name != NULL && strcmp(cmd, cmdsw[i].name); i++);
207
208	if (cmdsw[i].fptr == NULL)
209		errx(1, "unknown command: %s", cmd);
210
211	prefix(cmd);
212	return ((*cmdsw[i].fptr)(argc, argv));
213}
214