From 360b20d06f697c7864185e8c4ad808215b51080b Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Mon, 4 Jan 2016 14:52:05 -0500 Subject: [PATCH] Make output verbosity configurable from the CLI and standardize options. --- MasterPassword/C/build | 1 - MasterPassword/C/mpw-bench.c | 1 + MasterPassword/C/mpw-cli.c | 38 +++++++++++++++++------------ MasterPassword/C/mpw-util.h | 47 +++++++++++++++++++++++++++++------- 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/MasterPassword/C/build b/MasterPassword/C/build index e56a7dec..809d450e 100755 --- a/MasterPassword/C/build +++ b/MasterPassword/C/build @@ -1,7 +1,6 @@ #!/usr/bin/env bash # # TROUBLESHOOTING -# - To enable verbose algorithm/implementation debugging, use ./build -DDEBUG # - If you see 'undefined reference to `AES_encrypt'', # make sure you have openssl installed. # If libcrypto.a is in a non-standard directory, try ./build -L[your-lib-dir] diff --git a/MasterPassword/C/mpw-bench.c b/MasterPassword/C/mpw-bench.c index 67e6aaa5..987d6b74 100644 --- a/MasterPassword/C/mpw-bench.c +++ b/MasterPassword/C/mpw-bench.c @@ -16,6 +16,7 @@ #include #include "mpw-algorithm.h" +#include "mpw-util.h" #define MP_N 32768 #define MP_r 8 diff --git a/MasterPassword/C/mpw-cli.c b/MasterPassword/C/mpw-cli.c index 68f744a4..fceafcf1 100644 --- a/MasterPassword/C/mpw-cli.c +++ b/MasterPassword/C/mpw-cli.c @@ -13,8 +13,6 @@ #include #endif -#define ftl(...) do { fprintf( stderr, __VA_ARGS__ ); exit(2); } while (0) - #include "mpw-algorithm.h" #include "mpw-util.h" @@ -25,9 +23,9 @@ static void usage() { - fprintf( stderr, "Usage: mpw [-u name] [-t type] [-c counter] [-V version] [-v variant] [-C context] [-h] site\n\n" ); + fprintf( stderr, "Usage: mpw [-u name] [-t type] [-c counter] [-a algorithm] [-V variant] [-C context] [-v|-q] [-h] site\n\n" ); fprintf( stderr, " -u name Specify the full name of the user.\n" - " Defaults to %s in env.\n\n", MP_env_fullname ); + " Defaults to %s in env or prompts.\n\n", MP_env_fullname ); fprintf( stderr, " -t type Specify the password's template.\n" " Defaults to %s in env or 'long' for password, 'name' for login.\n" " x, max, maximum | 20 characters, contains symbols.\n" @@ -40,23 +38,27 @@ static void usage() { " p, phrase | 20 character sentence.\n\n", MP_env_sitetype ); fprintf( stderr, " -c counter The value of the counter.\n" " Defaults to %s in env or 1.\n\n", MP_env_sitecounter ); - fprintf( stderr, " -V version The algorithm version to use.\n" + fprintf( stderr, " -a version The algorithm version to use.\n" " Defaults to %s in env or %d.\n\n", MP_env_algorithm, MPAlgorithmVersionCurrent ); - fprintf( stderr, " -v variant The kind of content to generate.\n" + fprintf( stderr, " -V variant The kind of content to generate.\n" " Defaults to 'password'.\n" " p, password | The password to log in with.\n" " l, login | The username to log in as.\n" " a, answer | The answer to a security question.\n\n" ); fprintf( stderr, " -C context A variant-specific context.\n" " Defaults to empty.\n" - " -v p, password | Doesn't currently use a context.\n" - " -v l, login | Doesn't currently use a context.\n" - " -v a, answer | Empty for a universal site answer or\n" + " -V p, password | Doesn't currently use a context.\n" + " -V l, login | Doesn't currently use a context.\n" + " -V a, answer | Empty for a universal site answer or\n" " | the most significant word(s) of the question.\n\n" ); + fprintf( stderr, " -v Increase output verbosity (can be repeated).\n\n" ); + fprintf( stderr, " -q Decrease output verbosity (can be repeated).\n\n" ); fprintf( stderr, " ENVIRONMENT\n\n" - " MP_FULLNAME | The full name of the user.\n" - " MP_SITETYPE | The default password template.\n" - " MP_SITECOUNTER | The default counter value.\n\n" ); + " %-14s | The full name of the user (see -u).\n" + " %-14s | The default password template (see -t).\n" + " %-14s | The default counter value (see -c).\n" + " %-14s | The default algorithm version (see -a).\n\n", + MP_env_fullname, MP_env_sitetype, MP_env_sitecounter, MP_env_algorithm ); exit( 0 ); } @@ -111,7 +113,7 @@ int main(int argc, char *const argv[]) { ftl( "Invalid %s: %s\n", MP_env_algorithm, algorithmVersionString ); // Read the options. - for (int opt; (opt = getopt( argc, argv, "u:P:t:c:v:V:C:h" )) != -1;) + for (int opt; (opt = getopt( argc, argv, "u:P:t:c:V:a:C:vqh" )) != -1;) switch (opt) { case 'u': fullName = optarg; @@ -127,16 +129,22 @@ int main(int argc, char *const argv[]) { case 'c': siteCounterString = optarg; break; - case 'v': + case 'V': siteVariantString = optarg; break; - case 'V': + case 'a': if (sscanf( optarg, "%u", &algorithmVersion ) != 1) ftl( "Not a version: %s\n", optarg ); break; case 'C': siteContextString = optarg; break; + case 'v': + ++mpw_verbosity; + break; + case 'q': + --mpw_verbosity; + break; case 'h': usage(); break; diff --git a/MasterPassword/C/mpw-util.h b/MasterPassword/C/mpw-util.h index 41a515ae..154acbb9 100644 --- a/MasterPassword/C/mpw-util.h +++ b/MasterPassword/C/mpw-util.h @@ -6,21 +6,50 @@ // Copyright (c) 2014 Lyndir. All rights reserved. // +#include #include //// Logging. -#ifdef DEBUG - #ifndef trc - #define trc(...) fprintf( stderr, __VA_ARGS__ ) - #endif -#else - #ifndef trc - #define trc(...) do {} while (0) - #endif +int mpw_verbosity; +#ifndef trc + #define trc_level 3 + #define trc(...) \ + if (mpw_verbosity >= 3) \ + fprintf( stderr, __VA_ARGS__ ) +#endif +#ifndef dbg + #define dbg_level 2 + #define dbg(...) \ + if (mpw_verbosity >= 2) \ + fprintf( stderr, __VA_ARGS__ ) +#endif +#ifndef inf + #define inf_level 1 + #define inf(...) \ + if (mpw_verbosity >= 1) \ + fprintf( stderr, __VA_ARGS__ ) +#endif +#ifndef wrn + #define wrn_level 0 + #define wrn(...) \ + if (mpw_verbosity >= 0) \ + fprintf( stderr, __VA_ARGS__ ) +#endif +#ifndef err + #define err_level -1 + #define err(...) \ + if (mpw_verbosity >= -1) \ + fprintf( stderr, __VA_ARGS__ ) #endif #ifndef ftl - #define ftl(...) do { fprintf( stderr, __VA_ARGS__ ); abort(); } while (0) + #define ftl_level -2 + #define ftl(...) \ + do { \ + if (mpw_verbosity >= -2) \ + fprintf( stderr, __VA_ARGS__ ); \ + exit( 2 ); \ + } while (0) #endif //// Buffers and memory.