Add 3 long options for getting information about mkimg itself:

--version	print the version of mkimg and also whether it's
		64- or 32-bit.
--formats	list the supported output formats separated by space.
--schemes	list the supported partitioning schemes separated by
		space.

Inspired by a patch from: gjb@

MFC after:	1 week
Relnotes:	yes
This commit is contained in:
Marcel Moolenaar 2014-09-27 04:53:51 +00:00
parent d3e19acf3d
commit a53d83a210
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=272198
2 changed files with 90 additions and 13 deletions

View File

@ -6,6 +6,8 @@ PROG= mkimg
SRCS= format.c image.c mkimg.c scheme.c
MAN= mkimg.1
MKIMG_VERSION=20140926
CFLAGS+=-DMKIMG_VERSION=${MKIMG_VERSION}
CFLAGS+=-DSPARSE_WRITE
# List of formats to support

View File

@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$");
#include <errno.h>
#include <err.h>
#include <fcntl.h>
#include <getopt.h>
#include <libutil.h>
#include <limits.h>
#include <stdio.h>
@ -48,6 +49,17 @@ __FBSDID("$FreeBSD$");
#include "mkimg.h"
#include "scheme.h"
#define LONGOPT_FORMATS 0x01000001
#define LONGOPT_SCHEMES 0x01000002
#define LONGOPT_VERSION 0x01000003
static struct option longopts[] = {
{ "formats", no_argument, NULL, LONGOPT_FORMATS },
{ "schemes", no_argument, NULL, LONGOPT_SCHEMES },
{ "version", no_argument, NULL, LONGOPT_VERSION },
{ NULL, 0, NULL, 0 }
};
struct partlisthead partlist = STAILQ_HEAD_INITIALIZER(partlist);
u_int nparts = 0;
@ -61,10 +73,69 @@ u_int secsz = 512;
u_int blksz = 0;
static void
usage(const char *why)
print_formats(int usage)
{
struct mkimg_format *f, **f_iter;
const char *sep;
if (usage) {
fprintf(stderr, "\n formats:\n");
SET_FOREACH(f_iter, formats) {
f = *f_iter;
fprintf(stderr, "\t%s\t- %s\n", f->name,
f->description);
}
} else {
sep = "";
SET_FOREACH(f_iter, formats) {
f = *f_iter;
printf("%s%s", sep, f->name);
sep = " ";
}
putchar('\n');
}
}
static void
print_schemes(int usage)
{
struct mkimg_scheme *s, **s_iter;
const char *sep;
if (usage) {
fprintf(stderr, "\n schemes:\n");
SET_FOREACH(s_iter, schemes) {
s = *s_iter;
fprintf(stderr, "\t%s\t- %s\n", s->name,
s->description);
}
} else {
sep = "";
SET_FOREACH(s_iter, schemes) {
s = *s_iter;
printf("%s%s", sep, s->name);
sep = " ";
}
putchar('\n');
}
}
static void
print_version(void)
{
u_int width;
#ifdef __LP64__
width = 64;
#else
width = 32;
#endif
printf("mkimg %u (%u-bit)\n", MKIMG_VERSION, width);
}
static void
usage(const char *why)
{
warnx("error: %s", why);
fprintf(stderr, "\nusage: %s <options>\n", getprogname());
@ -82,17 +153,8 @@ usage(const char *why)
fprintf(stderr, "\t-S <num>\t- logical sector size\n");
fprintf(stderr, "\t-T <num>\t- number of tracks to simulate\n");
fprintf(stderr, "\n formats:\n");
SET_FOREACH(f_iter, formats) {
f = *f_iter;
fprintf(stderr, "\t%s\t- %s\n", f->name, f->description);
}
fprintf(stderr, "\n schemes:\n");
SET_FOREACH(s_iter, schemes) {
s = *s_iter;
fprintf(stderr, "\t%s\t- %s\n", s->name, s->description);
}
print_formats(1);
print_schemes(1);
fprintf(stderr, "\n partition specification:\n");
fprintf(stderr, "\t<t>[/<l>]::<size>\t- empty partition of given "
@ -366,7 +428,8 @@ main(int argc, char *argv[])
bcfd = -1;
outfd = 1; /* Write to stdout by default */
while ((c = getopt(argc, argv, "b:f:o:p:s:vyH:P:S:T:")) != -1) {
while ((c = getopt_long(argc, argv, "b:f:o:p:s:vyH:P:S:T:",
longopts, NULL)) != -1) {
switch (c) {
case 'b': /* BOOT CODE */
if (bcfd != -1)
@ -432,6 +495,18 @@ main(int argc, char *argv[])
if (error)
errc(EX_DATAERR, error, "track size");
break;
case LONGOPT_FORMATS:
print_formats(0);
exit(EX_OK);
/*NOTREACHED*/
case LONGOPT_SCHEMES:
print_schemes(0);
exit(EX_OK);
/*NOTREACHED*/
case LONGOPT_VERSION:
print_version();
exit(EX_OK);
/*NOTREACHED*/
default:
usage("unknown option");
}