diff --git a/src/p7/args.c b/src/p7/args.c index 1ff09af..f9f597b 100644 --- a/src/p7/args.c +++ b/src/p7/args.c @@ -36,45 +36,6 @@ QUOTE(BIN) " - from " QUOTE(NAME) " v" QUOTE(VERSION) "There is NO warranty; not even for MERCHANTABILITY or\n" "FITNESS FOR A PARTICULAR PURPOSE."; -/* Main help message */ -static const char help_main[] = -"Usage: " QUOTE(BIN) " [--version|-v] [--help|-h] [--no-init] [--no-exit]\n" -" [--storage ] [--com ]\n" -" [options...]\n" -"\n" -"Subcommands you can use are:\n" -" send Send a file to the calculator.\n" -" get Get a file from the calculator.\n" -" copy Copy a file into another on the calculator.\n" -" delete Delete a file on the calculator.\n" -" reset Reset the flash memory.\n" -" optimize Optimize the distant filesystem.\n" -" list List files on the distant filesystem.\n" -" info Dump info about the calculator.\n" -" idle Do nothing (only start or end the communication)\n" -"\n" -"General options:\n" -" -h, --help Display the help page of the (sub)command and quit.\n" -" -v, --version Display the version message and quit.\n" -" --com The serial device, if you want to communicate with a\n" -" calculator connected using a USB-to-serial cable.\n" -" If this option isn't used, the program will look for a\n" -" calculator connected using direct USB.\n" -" --storage The storage device with which to interact (fls0, crd0).\n" -" Default storage device is '" QUOTE(DEFAULT_STORAGE) "'.\n" -" --no-exit Does not terminate connection when action is completed.\n" -" --no-init Does not initialize connection (should only be used\n" -" when --no-exit was used last time p7 was called).\n" -" --use Use the following serial settings (when used with `--com`).\n" -" For example, \"9600N2\" represents 9600 bauds, no parity,\n" -" and two stop bits. (E for even parity, O for odd parity)\n" -" --set Set the following serial settings (when used with `--com`).\n" -" The string has the same format than for `--use`.\n" -" --reset Reset the default communication settings (9600N2).\n" -"\n" -"Type \"" QUOTE(BIN) " --help\" for some help about the subcommand.\n" -"Report bugs to " QUOTE(MAINTAINER) "."; - /* Parts */ #define FOOT \ "\nType \"" QUOTE(BIN) " --help\" for other subcommands and general options." @@ -167,6 +128,93 @@ static const char help_idle[] = "end a series of calls, without any side effects.\n" FOOT; /* ************************************************************************** */ +/* Help helpers */ +/* ************************************************************************** */ +/* Main help message parts */ +static const char help_main_part0[] = +"Usage: " QUOTE(BIN) " [--version|-v] [--help|-h] [--no-init] [--no-exit]\n" +" [--storage ] [--com ]\n" +" [options...]\n" +"\n" +"Subcommands you can use are:\n" +" send Send a file to the calculator.\n" +" get Get a file from the calculator.\n" +" copy Copy a file into another on the calculator.\n" +" delete Delete a file on the calculator.\n" +" reset Reset the flash memory.\n" +" optimize Optimize the distant filesystem.\n" +" list List files on the distant filesystem.\n" +" info Dump info about the calculator.\n" +" idle Do nothing (only start or end the communication)\n" +"\n" +"General options:\n" +" -h, --help Display the help page of the (sub)command and quit.\n" +" -v, --version Display the version message and quit.\n" +" --com The serial device, if you want to communicate with a\n" +" calculator connected using a USB-to-serial cable.\n" +" If this option isn't used, the program will look for a\n" +" calculator connected using direct USB.\n" +" --storage The storage device with which to interact (fls0, crd0).\n" +" Default storage device is '" QUOTE(DEFAULT_STORAGE) "'.\n" +" --no-exit Does not terminate connection when action is completed.\n" +" --no-init Does not initialize connection (should only be used\n" +" when --no-exit was used last time p7 was called).\n" +" --use Use the following serial settings (when used with `--com`).\n" +" For example, \"9600N2\" represents 9600 bauds, no parity,\n" +" and two stop bits. (E for even parity, O for odd parity)\n" +" --set Set the following serial settings (when used with `--com`).\n" +" The string has the same format than for `--use`.\n"; + +static const char help_main_loglevel_init[] = +" --log The library log levels (default: %s).\n" +" One of: %s"; + +static const char help_main_part1[] = +" --reset Reset the default communication settings (9600N2).\n" +"\n" +"Type \"" QUOTE(BIN) " --help\" for some help about the subcommand.\n" +"Report bugs to " QUOTE(MAINTAINER) ".\n"; + +/** + * put_loglevel: + * Put a loglevel (for listing). + * + * @arg initialized if the list was initialized. + * @arg level the level string. + */ + +static void put_loglevel(int *initialized, const char *level) +{ + /* initialize */ + if (!(*initialized)) { + printf(help_main_loglevel_init, p7_loglevel_tostring(p7_getlog()), + level); + *initialized = 1; + } else + printf(", %s", level); +} + +/** + * put_main_help: + * Put the main help on standard header. + */ + +static void put_main_help(void) +{ + int initialized; + + /* first big part */ + fputs(help_main_part0, stdout); + + /* loglevels */ + initialized = 0; + p7_loglevel_list((p7_liststr_t*)&put_loglevel, (void*)&initialized); + if (initialized) fputc('\n', stdout); + + /* second big part */ + fputs(help_main_part1, stdout); +} +/* ************************************************************************** */ /* Main function */ /* ************************************************************************** */ /* useful macros */ @@ -221,6 +269,7 @@ int parse_args(int ac, char **av, args_t *args) {"set", required_argument, NULL, 'S'}, {"reset", no_argument, NULL, 'R'}, {"use", required_argument, NULL, 'U'}, + {"log", required_argument, NULL, 'L'}, /* sentinel */ {NULL, 0, NULL, 0} @@ -230,7 +279,7 @@ int parse_args(int ac, char **av, args_t *args) int c; opterr = 0; int help = 0, rst = 0; const char *s_out = NULL, *s_dir = NULL, *s_todir = NULL; - const char *s_use = NULL, *s_set = NULL; + const char *s_use = NULL, *s_set = NULL, *s_log = NULL; while ((c = getopt_long(ac, av, short_options, long_options, NULL)) != -1) { switch (c) { /* help */ @@ -241,6 +290,8 @@ int parse_args(int ac, char **av, args_t *args) case 'f': args->force = 1; break; /* nice display */ case '#': args->nicedisp = 1; break; + /* logging */ + case 'L': s_log = optarg; break; /* output file (on calc or local) */ case 'o': s_out = optarg; break; @@ -291,7 +342,7 @@ int parse_args(int ac, char **av, args_t *args) args->localpath = NULL; /* - all subcommands - */ if (!aac || !strcmp(aav[0], "help")) { - puts(help_main); + put_main_help(); return (0); } else if (!strcmp(aav[0], "version")) { puts(version_message); @@ -403,6 +454,10 @@ int parse_args(int ac, char **av, args_t *args) } } + /* set the log level */ + if (s_log) + p7_setlog(p7_loglevel_fromstring(s_log)); + /* everything went well */ return (1); }