From dade489409018d2b2b7dddb779b114018830498c Mon Sep 17 00:00:00 2001 From: marcel Date: Tue, 14 Oct 2014 16:11:23 +0000 Subject: [PATCH] MF10 r272774: Add 3 long options (--version, --formats & --schemes) for getting information about mkimg itself. mkimg version: 20140927 Requested by: gjb Approved by: re (marius) Relnotes: yes git-svn-id: svn://svn.freebsd.org/base/releng/10.1@273098 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- usr.bin/mkimg/Makefile | 2 + usr.bin/mkimg/mkimg.1 | 30 +++++++++-- usr.bin/mkimg/mkimg.c | 115 +++++++++++++++++++++++++++++++++++------ 3 files changed, 127 insertions(+), 20 deletions(-) diff --git a/usr.bin/mkimg/Makefile b/usr.bin/mkimg/Makefile index 4e64aaa24..bc7baed82 100644 --- a/usr.bin/mkimg/Makefile +++ b/usr.bin/mkimg/Makefile @@ -4,6 +4,8 @@ PROG= mkimg SRCS= format.c image.c mkimg.c scheme.c MAN= mkimg.1 +MKIMG_VERSION=20140927 +CFLAGS+=-DMKIMG_VERSION=${MKIMG_VERSION} CFLAGS+=-DSPARSE_WRITE # List of formats to support diff --git a/usr.bin/mkimg/mkimg.1 b/usr.bin/mkimg/mkimg.1 index db492ed91..bfe05c01c 100644 --- a/usr.bin/mkimg/mkimg.1 +++ b/usr.bin/mkimg/mkimg.1 @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd September 12, 2014 +.Dd September 27, 2014 .Dt MKIMG 1 .Os .Sh NAME @@ -44,6 +44,8 @@ .Fl s Ar scheme .Fl p Ar partition .Op Fl p Ar partition ... +.Nm +.Ar --formats | --schemes | --version .Sh DESCRIPTION The .Nm @@ -122,10 +124,32 @@ utility will generate predictable values for Universally Unique Identifiers .Nm utility will create images that are identical. .Pp -For a complete list of supported partitioning schemes or supported output -format, or for a detailed description of how to specify partitions, run the +A set of long options exist to query about the +.Nm +utilty itself. +Options in this set should be given by themselves because the +.Nm +utility exits immediately after providing the requested information. +The version of the +.Nm +utility is printed when the +.Ar --version +option is given. +The list of supported output formats is printed when the +.Ar --formats +option is given and the list of supported partitioning schemes is printed +when the +.Ar --schemes +option is given. +Both the format and scheme lists a space-separated lists for easy handling +in scripts. +.Pp +For a more descriptive list of supported partitioning schemes or supported +output format, or for a detailed description of how to specify partitions, +run the .Nm utility without any arguments. +This will print a usage message with all the necessary details. .Sh ENVIRONMENT .Bl -tag -width "TMPDIR" -compact .It Ev TMPDIR diff --git a/usr.bin/mkimg/mkimg.c b/usr.bin/mkimg/mkimg.c index ab95d94b8..d054423a4 100644 --- a/usr.bin/mkimg/mkimg.c +++ b/usr.bin/mkimg/mkimg.c @@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -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,15 +73,79 @@ 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, " 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, " 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 \n", getprogname()); + fputc('\n', stderr); + fprintf(stderr, "usage: %s \n", getprogname()); fprintf(stderr, " options:\n"); + fprintf(stderr, "\t--formats\t- list image formats\n"); + fprintf(stderr, "\t--schemes\t- list partition schemes\n"); + fprintf(stderr, "\t--version\t- show version information\n"); + fputc('\n', stderr); fprintf(stderr, "\t-b \t- file containing boot code\n"); fprintf(stderr, "\t-f \n"); fprintf(stderr, "\t-o \t- file to write image into\n"); @@ -81,20 +157,12 @@ usage(const char *why) fprintf(stderr, "\t-P \t- physical sector size\n"); fprintf(stderr, "\t-S \t- logical sector size\n"); fprintf(stderr, "\t-T \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); - } - - fprintf(stderr, "\n partition specification:\n"); + fputc('\n', stderr); + print_formats(1); + fputc('\n', stderr); + print_schemes(1); + fputc('\n', stderr); + fprintf(stderr, " partition specification:\n"); fprintf(stderr, "\t[/]::\t- empty partition of given " "size\n"); fprintf(stderr, "\t[/]:=\t- partition content and size " @@ -366,7 +434,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 +501,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"); } -- 2.45.0