Welcome to little lamb

Code » ssp » commit 3f8513a

show/list: Add -e/--escape-comments

author Olivier Brunel
2023-08-20 14:45:53 UTC
committer Olivier Brunel
2023-08-20 14:55:39 UTC
parent 3161dd874c3a05c3f7e80ca1df64e11414451bfe

show/list: Add -e/--escape-comments

src/ssp/list.c +11 -5
src/ssp/show.c +18 -7

diff --git a/src/ssp/list.c b/src/ssp/list.c
index 047536c..458d205 100644
--- a/src/ssp/list.c
+++ b/src/ssp/list.c
@@ -10,10 +10,11 @@
 #include "ssp.h"
 
 enum {
-    OPT_DETAILS     = 1 << 0,
-    OPT_SECRET      = 1 << 1, /* must be the same as in show */
-    OPT_NO_COMMENTS = 1 << 2, /* must be the same as in show */
-    OPT_FMT_INI     = 1 << 3,
+    OPT_DETAILS         = 1 << 0,
+    OPT_SECRET          = 1 << 1, /* must be the same as in show */
+    OPT_NO_COMMENTS     = 1 << 2, /* must be the same as in show */
+    OPT_ESC_COMMENTS    = 1 << 3, /* must be the same as in show */
+    OPT_FMT_INI         = 1 << 4,
 };
 
 enum {
@@ -30,6 +31,7 @@ struct list {
 COMMAND(list, "List all entries", "[OPTION..] [<pattern>]",
 " -C, --no-comments                     Do not show comments with --details/--format\n"
 " -d, --details                         Show entries' details\n"
+" -e, --escape-comments                 Escape comments on a single line (implies --details)\n"
 " -f, --format                          Output in INI-like format\n"
 " -S, --secret                          Show entries' secrets (implies --details)\n"
 " -s, --sep SEP                         Use SEP as separator\n"
@@ -41,6 +43,7 @@ parse_cmdline(int argc, const char *argv[], const char usage[], struct list *ctx
     const struct option options[] = {
         OPTION_ARG_NONE('C', "no-comments",             0,          OPTID_SHORTOPT),
         OPTION_ARG_NONE('d', "details",                 0,          OPTID_SHORTOPT),
+        OPTION_ARG_NONE('e', "escape-comments",         0,          OPTID_SHORTOPT),
         OPTION_ARG_NONE('f', "format",                  0,          OPTID_SHORTOPT),
         OPTION_ARG_NONE('S', "secret",                  0,          OPTID_SHORTOPT),
         OPTION_ARG_REQ ('s', "sep",                     0,          OPTID_SHORTOPT),
@@ -58,6 +61,9 @@ parse_cmdline(int argc, const char *argv[], const char usage[], struct list *ctx
         case 'd':
             ctx->options |= OPT_DETAILS;
             break;
+        case 'e':
+            ctx->options |= OPT_DETAILS | OPT_ESC_COMMENTS;
+            break;
         case 'f':
             ctx->options |= OPT_FMT_INI;
             break;
@@ -108,7 +114,7 @@ list_main(int argc, const char *argv[], const char *env[], const char usage[], v
         if (ctx.options & OPT_FMT_INI) {
             show_entry_ini(ctx.options & OPT_NO_COMMENTS, ssp);
         } else if (ctx.options & OPT_DETAILS) {
-            show_entry(ctx.options & (OPT_NO_COMMENTS | OPT_SECRET), ssp);
+            show_entry(ctx.options & (OPT_NO_COMMENTS | OPT_ESC_COMMENTS | OPT_SECRET), ssp);
         } else {
             if (i) add(ctx.sep);
             add(ESC, db_entry(ssp), ESC);
diff --git a/src/ssp/show.c b/src/ssp/show.c
index 0ee9221..9c9fa90 100644
--- a/src/ssp/show.c
+++ b/src/ssp/show.c
@@ -12,9 +12,10 @@
 #include "ssp.h"
 
 enum {
-    OPT_SECRET      = 1 << 1, /* must be the same in list */
-    OPT_NO_COMMENTS = 1 << 2, /* must be the same in list */
-    OPT_FMT_INI     = 1 << 3,
+    OPT_SECRET          = 1 << 1, /* must be the same in list */
+    OPT_NO_COMMENTS     = 1 << 2, /* must be the same in list */
+    OPT_ESC_COMMENTS    = 1 << 3, /* must be the same in list */
+    OPT_FMT_INI         = 1 << 4,
 };
 
 struct show {
@@ -29,7 +30,8 @@ enum {
 
 COMMAND(show, "Show an entry", "[OPTION..] <entry>",
 " -C, --no-comments                     Do not show entry's comments\n"
-" -f, --format                          Output in INI-like format\n"
+" -e, --escape-comments                 Escape comments on a single line\n"
+" -f, --format                          Output in INI-like format. Implies --secret\n"
 " -S, --secret                          Show entry's secret\n"
 );
 
@@ -38,6 +40,7 @@ parse_cmdline(int argc, const char *argv[], const char usage[], struct show *ctx
 {
     const struct option options[] = {
         OPTION_ARG_NONE('C', "no-comments",             0,          OPTID_SHORTOPT),
+        OPTION_ARG_NONE('e', "escape-comments",         0,          OPTID_SHORTOPT),
         OPTION_ARG_NONE('f', "format",                  0,          OPTID_SHORTOPT),
         OPTION_ARG_NONE('S', "secret",                  0,          OPTID_SHORTOPT),
         LOADOPT_ARGUMENTS,
@@ -51,6 +54,9 @@ parse_cmdline(int argc, const char *argv[], const char usage[], struct show *ctx
         case 'C':
             ctx->options |= OPT_NO_COMMENTS;
             break;
+        case 'e':
+            ctx->options |= OPT_ESC_COMMENTS;
+            break;
         case 'f':
             ctx->options |= OPT_FMT_INI;
             break;
@@ -92,9 +98,14 @@ show_entry(unsigned options, struct ssp *ctx)
         out("   Secret: ", buf);
     }
     if (!(options & OPT_NO_COMMENTS) && otp->data[otp->slen]) {
-        const char *s = otp->data + otp->slen;
-        size_t l = strlen(s);
-        out("   Comments:", (byte_chr(s, l, '\n') < l) ? "\n" : " ", s);
+        add("   Comments:");
+        if (options & OPT_ESC_COMMENTS) {
+            out(" ", ESC, otp->data + otp->slen, ESC);
+        } else {
+            const char *s = otp->data + otp->slen;
+            size_t l = strlen(s);
+            out((byte_chr(s, l, '\n') < l) ? "\n" : " ", s);
+        }
     }
 }