diff options
-rw-r--r-- | project/headers.mk | 1 | ||||
-rw-r--r-- | src/driver/pe_driver_ctx.c | 81 | ||||
-rw-r--r-- | src/internal/perk_synopsis_impl.h | 46 |
3 files changed, 95 insertions, 33 deletions
diff --git a/project/headers.mk b/project/headers.mk index fc1f132..e87271b 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -16,6 +16,7 @@ INTERNAL_HEADERS = \ $(SOURCE_DIR)/src/internal/$(PACKAGE)_errinfo_impl.h \ $(SOURCE_DIR)/src/internal/$(PACKAGE)_hexdump_impl.h \ $(SOURCE_DIR)/src/internal/$(PACKAGE)_reader_impl.h \ + $(SOURCE_DIR)/src/internal/$(PACKAGE)_synopsis_impl.h \ $(SOURCE_DIR)/src/internal/$(PACKAGE)_visibility_impl.h \ ALL_HEADERS = $(API_HEADERS) $(INTERNAL_HEADERS) diff --git a/src/driver/pe_driver_ctx.c b/src/driver/pe_driver_ctx.c index a427252..8214f86 100644 --- a/src/driver/pe_driver_ctx.c +++ b/src/driver/pe_driver_ctx.c @@ -14,6 +14,7 @@ #include <perk/perk_output.h> #include "perk_version.h" #include "perk_driver_impl.h" +#include "perk_synopsis_impl.h" #include "argv/argv.h" /* package info */ @@ -76,30 +77,40 @@ static int pe_driver_usage( const char * arg, const struct argv_option ** optv, struct argv_meta * meta, + const char * cmdarg, enum pe_cmd cmd) { - char header[512]; - char * cmdarg[2]; - const char * cmdname; - - if (cmd == PERK_CMD_DEFAULT) { - cmdarg[0] = ""; - cmdarg[1] = ""; - cmdname = ""; - } else { - cmdarg[0] = " (--cmd="; - cmdarg[1] = ")"; - cmdname = perk_cmd_name[cmd]; + char header [2048]; + char cmdname[128]; + + snprintf(cmdname,sizeof(cmdname),"%s%s%s", + program, + cmdarg ? " --cmd=" : "", + cmdarg ? perk_cmd_name[cmd] : ""); + + switch (cmd) { + case PERK_CMD_AR: + snprintf(header,sizeof(header), + PERK_AR_CMD_SYNOPSIS, + cmdname,cmdname,cmdname,cmdname, + cmdname,cmdname,cmdname,cmdname, + cmdname,cmdname,cmdname,cmdname, + cmdname,cmdname,cmdname); + break; + + case PERK_CMD_PERK: + snprintf(header,sizeof(header), + PERK_PERK_CMD_SYNOPSIS, + cmdname,cmdname,cmdname); + break; + + default: + snprintf(header,sizeof(header), + PERK_DEFAULT_CMD_SYNOPSIS, + program,program,program,program); + break; } - snprintf(header,sizeof(header), - "Usage: %s [options] ...\n" - "Usage: %s [options] [--cmd=<command>] <arg> <arg> ...\n\n" - "Notes: --cmd must precede all non-option arguments, as well as\n" - " all arguments that are specific to the selected command.\n\n" - "Options%s%s%s:\n", - program,program,cmdarg[0],cmdname,cmdarg[1]); - argv_usage(fdout,header,optv,arg); argv_free(meta); @@ -176,7 +187,8 @@ static int pe_cctx_update( struct argv_meta * meta, const struct pe_fd_ctx * fdctx, struct pe_common_ctx * cctx, - size_t * nunits) + size_t * nunits, + const char * cmdarg) { struct argv_entry * entry; const char * pretty; @@ -191,7 +203,7 @@ static int pe_cctx_update( return pe_driver_usage( fdctx->fdout, program,entry->arg, - optv,0,cctx->cmd); + optv,0,cmdarg,cctx->cmd); break; case TAG_CMD: @@ -199,9 +211,10 @@ static int pe_cctx_update( return pe_driver_usage( fdctx->fderr, program,0, - optv,0,cctx->cmd); + optv,0,cmdarg,cctx->cmd); cctx->cmd = pe_cmd_from_program(entry->arg); + cmdarg = entry->arg; break; case TAG_VERSION: @@ -319,6 +332,7 @@ int pe_lib_get_driver_ctx( struct argv_meta * meta; size_t nunits; const char * program; + const char * cmdarg; char ** parg; char ** pargcap; char ** cmdargv; @@ -341,6 +355,7 @@ int pe_lib_get_driver_ctx( cctx.cmd = pe_cmd_from_program(program); cctx.drvflags = flags; nunits = 0; + cmdarg = 0; /* missing arguments? */ argv_optv_init(perk_cmd_options[cctx.cmd],optv); @@ -349,7 +364,7 @@ int pe_lib_get_driver_ctx( return pe_driver_usage( fdctx->fderr, program,0, - optv,0,cctx.cmd); + optv,0,0,cctx.cmd); /* initial argv scan: ... --cmd=xxx ... */ argv_scan(argv,optv,&actx,0); @@ -370,9 +385,11 @@ int pe_lib_get_driver_ctx( if (!strcmp(*parg,"--cmd") && parg[1]) { cmdargv = &parg[2]; cmdmark = parg[2]; + cmdarg = parg[1]; } else if (!strncmp(*parg,"--cmd=",6)) { cmdargv = &parg[1]; cmdmark = parg[1]; + cmdarg = &parg[0][6]; } } @@ -404,7 +421,8 @@ int pe_lib_get_driver_ctx( if (pe_cctx_update( program,optv,meta, - fdctx,&cctx,&nunits)) { + fdctx,&cctx,&nunits, + cmdarg)) { argv_free(meta); return -1; } @@ -436,22 +454,19 @@ int pe_lib_get_driver_ctx( if (pe_cctx_update( program,optv,meta, - fdctx,&cctx,&nunits)) { + fdctx,&cctx,&nunits, + cmdarg)) { argv_free(meta); return -1; } /* utility mode and no action to take? */ - if (cctx.cmd == PERK_CMD_AR) { - (void)0; - - } else if (cctx.drvflags & PERK_DRIVER_VERBOSITY_UTILITY) { + if (cctx.drvflags & PERK_DRIVER_VERBOSITY_UTILITY) if (!nunits && !(cctx.drvflags & PERK_DRIVER_VERSION)) return pe_driver_usage( fdctx->fdout, - program,0, - optv,meta,cctx.cmd); - } + program,0,optv,meta, + cmdarg,cctx.cmd); /* context allocation */ if (!(ctx = pe_driver_ctx_alloc(meta,fdctx,&cctx,nunits))) diff --git a/src/internal/perk_synopsis_impl.h b/src/internal/perk_synopsis_impl.h new file mode 100644 index 0000000..4f6959b --- /dev/null +++ b/src/internal/perk_synopsis_impl.h @@ -0,0 +1,46 @@ +/***************************************************************/ +/* perk: PE Resource Kit */ +/* Copyright (C) 2015--2025 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.PERK. */ +/***************************************************************/ + +#ifndef SLIBTOOL_SYNOPSIS_IMPL_H +#define SLIBTOOL_SYNOPSIS_IMPL_H + +#define PERK_DEFAULT_CMD_SYNOPSIS \ + "%s — PE/COFF Resource Kit\n\n" \ + "Synopsis:\n" \ + " %s [option] ...\n" \ + " %s --cmd=<command> [option] ...\n" \ + " %s --cmd=<command> [option] ... <file> ...\n\n" \ + "Options:\n" + +#define PERK_PERK_CMD_SYNOPSIS \ + "%s — PE/COFF Resource Kit\n\n" \ + "Synopsis:\n" \ + " %s [option] ...\n" \ + " %s [option] ... <file> ...\n\n" \ + "Options:\n" + +#define PERK_AR_CMD_SYNOPSIS \ + "%s — the PE/COFF Resource Kit Archiver\n\n" \ + "Synopsis:\n" \ + " %s -d [-v] <archive> <file> ...\n" \ + " %s -p [-v] [-s] <archive> <file> ...\n" \ + " %s -q [-v] [-c] <archive> <file> ...\n" \ + " %s -r [-v] [-c] [-u] <archive> <file> ...\n" \ + " %s -t [-v] [-s] <archive> [<file> ...]\n" \ + " %s -x [-v] [-s] [-C] [-T] <archive> [<file> ...]\n\n" \ + \ + " %s -m [-v] <archive> <file> ...\n" \ + " %s -m -a [-v] <posname> <archive> <file> ...\n" \ + " %s -m -b [-v] <posname> <archive> <file> ...\n" \ + " %s -m -i [-v] <posname> <archive> <file> ...\n\n" \ + \ + " %s -r [-v] [-c] [-u] <archive> <file> ...\n" \ + " %s -r -a [-v] [-c] [-u] <posname> <archive> <file> ...\n" \ + " %s -r -b [-v] [-c] [-u] <posname> <archive> <file> ...\n" \ + " %s -r -i [-v] [-c] [-u] <posname> <archive> <file> ...\n\n" \ + "Options:\n" + +#endif |