]> sourceware.org Git - lvm2.git/commitdiff
man: enhance handling of option description
authorZdenek Kabelac <zkabelac@redhat.com>
Thu, 21 Nov 2024 21:47:11 +0000 (22:47 +0100)
committerZdenek Kabelac <zkabelac@redhat.com>
Fri, 22 Nov 2024 13:25:54 +0000 (14:25 +0100)
Update the _print_man_option_desc() to also handle common parts
as the initial text without any specified section and also
add support for '#\n' to be able to revert to common part.

tools/man-generator.c

index 286dbf7191e588bc24ae80619b03eabfcdfc2fa4..b5b8be5724b95e7f13b25fb66f069f946ea0dc9e 100644 (file)
@@ -914,70 +914,55 @@ static void _print_man_usage_common_cmd(struct command *cmd)
  *
  * "text bar goes here"
  * "another line of text."
+ *
+ * Supports also 'prefix' for all commands before the first '#cmdname'.
+ * "#\n" is restorting printing for all commands.
  */
-
 static void _print_man_option_desc(const struct command_name *cname, int opt_enum)
 {
        const char *desc = opt_names[opt_enum].desc;
+       size_t clen = strlen(cname->name);
        char buf[DESC_LINE];
-       int started_cname = 0;
-       int line_count = 0;
-       int bi = 0;
-       unsigned di;
+       int check_for_new_section = 1;
+       int print_section = 1; /* initial description without cmdname is printed */
+       unsigned bi = 0;
 
-       if (desc[0] != '#') {
-               printf("%s", desc);
-               return;
-       }
-
-       for (di = 0; desc[di]; di++) {
-               buf[bi++] = desc[di];
+       while (*desc) {
+               buf[bi++] = *desc;
 
                if (bi == DESC_LINE) {
                        log_error("Parsing command defs: print_man_option_desc line too long.");
                        exit(EXIT_FAILURE);
                }
 
-               if (buf[bi-1] != '\n')
-                       continue;
-
-               if (buf[0] != '#') {
-                       if (started_cname) {
+               if (*desc++ != '\n' && *desc)
+                       continue;  /* read until '\n' or end of description */
+
+               /* Line could be either new cmdname or a regular text description
+                * either for all commands or for the matching cmdname.
+                * Line starting with #cmdname starts a new 'text section'.
+                * Multiple command names can use the same text */
+               if (buf[0] == '#') {
+                       if (check_for_new_section) {
+                               check_for_new_section = 0;
+                               print_section = 0;
+                       }
+                       bi -= 2;
+                       if (!bi ||  /* empty cmd resets section to all commands */
+                           ((bi == clen) && !strncmp(buf + 1, cname->name, clen))) {
+                               print_section = 1;
+                       }
+               } else if (print_section) {
+                       /* Printable text 'splits' individual 'cmdname' section */
+                       check_for_new_section = 1;
+                       if (bi) {
+                               buf[bi] = 0;
                                printf("%s", buf);
-                               line_count++;
                        }
-
-                       memset(buf, 0, sizeof(buf));
-                       bi = 0;
-                       continue;
                }
 
-               /* Line starting with #cmdname */
-
-               /*
-                * Must be starting a new command name.
-                * If no lines have been printed, multiple command names
-                * are using the same text. If lines have been printed,
-                * then the start of a new command name means the end
-                * of text for the current command name.
-                */
-               if (line_count && started_cname)
-                       return;
-
-               if (!strncmp(buf + 1, cname->name, strlen(cname->name))) {
-                       /* The start of our command name. */
-                       started_cname = 1;
-                       memset(buf, 0, sizeof(buf));
-                       bi = 0;
-               } else {
-                       /* The start of another command name. */
-                       memset(buf, 0, sizeof(buf));
-                       bi = 0;
-               }
+               bi = 0;
        }
-
-       if (bi && started_cname)
-               printf("%s", buf);
 }
 
 /*
This page took 0.044644 seconds and 5 git commands to generate.