From: Nick Bowler Date: Mon, 13 Sep 2021 17:49:40 +0000 (-0400) Subject: Convert tools to use option generator script from dxcommon. X-Git-Url: http://git.draconx.ca/gitweb/liblbx.git/commitdiff_plain/efc068f39f6811cdc0e004d2acf4ef08b2b71c3e Convert tools to use option generator script from dxcommon. This generator makes it simple to maintain long and short option lists, as well as provide an actually useful --help output. --- diff --git a/Makefile.am b/Makefile.am index fb85562..30a8c5c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -14,7 +14,8 @@ EXTRA_LTLIBRARIES = EXTRA_DIST = bootstrap $(DX_BASEDIR)/scripts/fix-gnulib.pl m4/gnulib-cache.m4 CLEANFILES = $(EXTRA_LTLIBRARIES) -AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib -I$(top_srcdir)/src \ +AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib \ + -I$(top_builddir)/src -I$(top_srcdir)/src \ -I$(top_srcdir)/common/src AM_CFLAGS = $(LIBPNG_CFLAGS) $(GTK_CFLAGS) @@ -23,7 +24,7 @@ dist_doc_DATA = doc/txt/moo2-data.txt lbxdir = $(includedir)/lbx lbx_HEADERS = src/lbx.h src/image.h src/error.h -noinst_HEADERS = src/misc.h src/tools.h +noinst_HEADERS = src/misc.h src/tools.h src/imgoutput.h common/src/help.h lib_LTLIBRARIES = liblbx.la liblbx_la_SOURCES = src/lbx.c src/fops.c src/image.c src/error.c \ @@ -35,13 +36,15 @@ if BUILD_LBXGUI bin_PROGRAMS += lbxgui endif -lbxtool_SOURCES = src/lbxtool.c src/tools.c +lbxtool_SOURCES = src/lbxtool.c src/tools.c common/src/help.c lbxtool_LDADD = liblbx.la libgnu.la $(lbxtool_OBJECTS): $(gnulib_headers) +$(lbxtool_OBJECTS): src/toolopts.h -lbximg_SOURCES = src/lbximg.c src/tools.c src/pnm.c src/imgoutput.h +lbximg_SOURCES = src/lbximg.c src/tools.c src/pnm.c common/src/help.c lbximg_LDADD = liblbx.la libgnu.la $(LIBPNG_LIBS) $(lbximg_OBJECTS): $(gnulib_headers) +$(lbximg_OBJECTS): src/imgopts.h if HAVE_LIBPNG lbximg_SOURCES += src/png.c @@ -72,6 +75,14 @@ clean-testdirs: done .PHONY: clean-testdirs +OPTFILES = src/toolopts.opt src/imgopts.opt +.opt.h: + $(AM_V_GEN) $(AWK) -f $(DX_BASEDIR)/scripts/gen-options.awk $< >$@.tmp + $(AM_V_at) mv -f $@.tmp $@ +$(OPTFILES:.opt=.h): $(DX_BASEDIR)/scripts/gen-options.awk +CLEANFILES += $(OPTFILES:.opt=.h) +EXTRA_DIST += $(DX_BASEDIR)/scripts/gen-options.awk $(OPTFILES) + .xml.c: $(AM_V_GEN) printf '%s\n' $(*F) | sed -e 's/[^[:alnum:]]/_/g' \ -e 's/.*/const char &[] =/' > $@.tmp diff --git a/common b/common index aa6ab45..843799d 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit aa6ab453c6ff523a4d8538554c068117f2de3aca +Subproject commit 843799dbec995b276a7d017bb32c053747b55400 diff --git a/configure.ac b/configure.ac index 22e7cee..c054f51 100644 --- a/configure.ac +++ b/configure.ac @@ -5,7 +5,7 @@ dnl This is free software: you are free to do what the fuck you want to. dnl There is NO WARRANTY, to the extent permitted by law. AC_PREREQ([2.68]) -AC_INIT([liblbx], [0.1], [toom-devel@lists.sourceforge.net]) +AC_INIT([liblbx], [0.1], [nbowler@draconx.ca]) AC_CONFIG_SRCDIR([src/lbx.c]) AC_CONFIG_HEADER([config.h]) diff --git a/doc/man/lbximg.1 b/doc/man/lbximg.1 index 5f0c6ee..e3a91c1 100644 --- a/doc/man/lbximg.1 +++ b/doc/man/lbximg.1 @@ -1,4 +1,4 @@ -.Dd June 14, 2013 +.Dd September 12, 2021 .Os liblbx .Dt LBXIMG \&1 "2ooM Reference Manual" .Sh NAME @@ -125,10 +125,12 @@ Decodes an image of a red star fortress. .Sh AUTHORS Nick Bowler .Sh COPYRIGHT -Copyright \(co 2008\(en2010, 2013 Nick Bowler +Copyright \(co 2008, 2010, 2013, 2021 Nick Bowler .Pp Permission is granted to copy, distribute and/or modify this manual under the -terms of the Do What The Fuck You Want To Public License, version 2. +terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. .Sh SEE ALSO .Xr lbxtool 1 , .Xr lbxgui 1 diff --git a/doc/man/lbxtool.1 b/doc/man/lbxtool.1 index 4581878..63bd254 100644 --- a/doc/man/lbxtool.1 +++ b/doc/man/lbxtool.1 @@ -1,9 +1,4 @@ -.\" Copyright (C) 2008-2010 Nick Bowler -.\" Copying and distribution of this file, with or without modification, -.\" are permitted in any medium without royalty provided the copyright -.\" notice and this notice are preserved. This file is offered as-is, -.\" without any warranty. -.Dd February 9, 2010 +.Dd September 12, 2021 .Os liblbx .Dt LBXTOOL \&1 "2ooM Reference Manual" .Sh NAME @@ -34,9 +29,7 @@ Read from the specified instead of standard input. .It Fl V , -version Print a version message and exit. -.It Fl -usage -Print a short usage message and exit. -.It Fl -help +.It Fl H , -help Print a help message and exit .It Ar Limit the operation to these files in the archive. By default, all files are @@ -46,3 +39,12 @@ selected. Supports common shell globbing features. When extracting from an archive read from standard input, be sure to specify files in the order that they appear in the archive. This limitation will hopefully be lifted in the future. +.Sh AUTHORS +Nick Bowler +.Sh COPYRIGHT +Copyright \(co 2008, 2010, 2021 Nick Bowler +.Pp +Permission is granted to copy, distribute and/or modify this manual under the +terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..34968f0 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,2 @@ +/toolopts.h +/imgopts.h diff --git a/src/imgopts.opt b/src/imgopts.opt new file mode 100644 index 0000000..05fdb18 --- /dev/null +++ b/src/imgopts.opt @@ -0,0 +1,35 @@ +-i, --identify +Display details about the image file. + +-d, --decode +Convert frames to another image format. + +-v, --verbose +Display additional information on standard output. + +-f, --file=FILE +Read image data from FILE instead of standard input. + +-F, --format=FMT +When decoding, set the output format to FMT. One of png, +pam, ppm or pbm. + +--output-prefix=NAME +When decoding, output filenames will begin with NAME +instead of "out". + +-n, --no-palette +Instead of performing palette lookups, the palette index +values for each pixel are used directly as grayscale level. + +-p, --palette=FILE +Load FILE as the base image palette. + +-O, --override=FILE +Load FILE as the override palette. + +-V, --version +Print a version message and then exit. + +-H, --help +Print a help message and then exit. diff --git a/src/lbximg.c b/src/lbximg.c index 98d2676..5beaaae 100644 --- a/src/lbximg.c +++ b/src/lbximg.c @@ -1,7 +1,7 @@ /* * 2ooM: The Master of Orion II Reverse Engineering Project * Simple command-line tool to convert an LBX image to other formats. - * Copyright © 2006-2011, 2013-2014 Nick Bowler + * Copyright © 2006-2011, 2013-2014, 2021 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,6 +27,8 @@ #include #include +#include "help.h" + #include "tools.h" #include "image.h" #include "error.h" @@ -41,21 +43,42 @@ static int verbose = 0; static char *outname = "out"; static int usepalette = 1; -enum { - OPT_PREFIX = UCHAR_MAX+1, -}; +#include "imgopts.h" +static const char sopts[] = SOPT_STRING; +static const struct option lopts[] = { LOPTS_INITIALIZER, {0} }; -static void printusage(void) +static void print_usage(FILE *f) { - puts("usage: lbximg [-i|-d] [-v] [-p palette_file] [-O override_file]" - " [-f path]"); - puts(" [-F format] [frameno ...]"); + const char *progname = tool_invocation(); + + fprintf(f, "Usage: %s [options] [-i|-d] [frame ...]\n", progname); + if (f != stdout) + fprintf(f, "Try %s --help for more information.\n", progname); } -static void printhelp(void) +static void print_help(void) { - printusage(); - puts("For now, see the man page for detailed help."); + const struct option *opt; + + print_usage(stdout); + + putchar('\n'); + puts("Options:"); + for (opt = lopts; opt->name; opt++) { + struct lopt_help help; + int w; + + if (!lopt_get_help(opt, &help)) + continue; + + help_print_option(opt, help.arg, help.desc, 20); + } + putchar('\n'); + + puts("For more information, see the lbximg(1) man page."); + putchar('\n'); + + printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT); } enum { @@ -442,26 +465,6 @@ int main(int argc, char **argv) FILE *palf = NULL, *overf = NULL; struct lbx_image *img; - static const char sopts[] = "idnvF:f:p:O:VH"; - static const struct option lopts[] = { - { "identify", 0, NULL, 'i' }, - { "decode", 0, NULL, 'd' }, - { "verbose", 0, NULL, 'v' }, - { "file", 1, NULL, 'f' }, - { "format", 1, NULL, 'F' }, - { "palette", 1, NULL, 'p' }, - { "override", 1, NULL, 'O' }, - { "no-palette", 0, NULL, 'n' }, - - { "output-prefix", 1, NULL, OPT_PREFIX }, - - { "version", 0, NULL, 'V' }, - { "usage", 0, NULL, 'U' }, - { "help", 0, NULL, 'H' }, - - { 0 } - }; - tool_init("lbximg", argc, argv); while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) { switch(opt) { @@ -490,17 +493,14 @@ int main(int argc, char **argv) case 'O': ovr_palette = optarg; break; - case OPT_PREFIX: + case LOPT_OUTPUT_PREFIX: outname = optarg; break; case 'V': tool_version(); return EXIT_SUCCESS; - case 'U': - printusage(); - return EXIT_SUCCESS; case 'H': - printhelp(); + print_help(); return EXIT_SUCCESS; default: return EXIT_FAILURE; diff --git a/src/lbxtool.c b/src/lbxtool.c index 3c4e436..6629969 100644 --- a/src/lbxtool.c +++ b/src/lbxtool.c @@ -1,7 +1,7 @@ /* * 2ooM: The Master of Orion II Reverse Engineering Project * Simple command-line tool to extract LBX archive files. - * Copyright © 2006-2011, 2013 Nick Bowler + * Copyright © 2006-2011, 2013-2014, 2021 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,6 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + #include #include #include @@ -24,19 +25,51 @@ #include #include +#include "help.h" + #include "tools.h" #include "lbx.h" #include "error.h" -static void printusage(void) +#include "toolopts.h" +static const char sopts[] = SOPT_STRING; +static const struct option lopts[] = { LOPTS_INITIALIZER, {0} }; + +static void print_usage(FILE *f) { - puts("usage: lbxtool [-l|-x] [-v] [-f path] [file ...]"); + const char *progname = tool_invocation(); + + fprintf(f, "Usage: %s [options] [-l|-x] [file ...]\n", progname); + if (f != stdout) + fprintf(f, "Try %s --help for more information.\n", progname); } -static void printhelp(void) +static void print_help(void) { - printusage(); - puts("For now, see the man page for detailed help."); + const struct option *opt; + + print_usage(stdout); + + puts("This is \"lbxtool\": a command-line tool for manipulating the LBX archives\n" + "used in Moo2."); + + putchar('\n'); + puts("Options:"); + for (opt = lopts; opt->name; opt++) { + struct lopt_help help; + int w; + + if (!lopt_get_help(opt, &help)) + continue; + + help_print_option(opt, help.arg, help.desc, 20); + } + putchar('\n'); + + puts("For more information, see the lbxtool(1) man page."); + putchar('\n'); + + printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT); } enum { @@ -177,23 +210,6 @@ int main(int argc, char **argv) const char *file = NULL; struct lbx *lbx; - static const char *sopts = "lxf:i:vV"; - static const struct option lopts[] = { - { "list", 0, NULL, 'l' }, - { "extract", 0, NULL, 'x' }, - - { "file", 1, NULL, 'f' }, - { "index", 1, NULL, 'i' }, - - { "verbose", 0, NULL, 'v' }, - - { "version", 0, NULL, 'V' }, - { "usage", 0, NULL, 'U' }, - { "help", 0, NULL, 'H' }, - - { 0 } - }; - tool_init("lbxtool", argc, argv); while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) { switch(opt) { @@ -206,20 +222,14 @@ int main(int argc, char **argv) case 'f': file = optarg; break; - case 'i': - /* FIXME: Add index file support. */ - break; case 'v': verbose = 1; break; case 'V': tool_version(); return EXIT_SUCCESS; - case 'U': - printusage(); - return EXIT_SUCCESS; case 'H': - printhelp(); + print_help(); return EXIT_SUCCESS; default: return EXIT_FAILURE; diff --git a/src/toolopts.opt b/src/toolopts.opt new file mode 100644 index 0000000..813c9e1 --- /dev/null +++ b/src/toolopts.opt @@ -0,0 +1,17 @@ +-l, --list +Display files contained in the archive. + +-x, --extract +Extract files from the archive. + +-f, --file=FILE +Read archive data from FILE instead of standard input. + +-v, --verbose +Display additional information on standard output. + +-V, --version +Print a version message and then exit. + +-H, --help +Print this message and then exit.