1 /* cli/help.c - Show dynamic help text
2  * Copyright (C) 2001,2005,2006  Bruce Guenter <bruce@untroubled.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  */
18 
19 #include "iobuf.h"
20 #include "msg.h"
21 #include <time.h>
22 #include "systime.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include "cli.h"
26 #include "internal.h"
27 
calc_max_width()28 static unsigned calc_max_width()
29 {
30   /* maxwidth is the maximum width of the long argument */
31   unsigned maxwidth = 0;
32   unsigned i;
33   for(i = 0; i < cli_option_count; i++) {
34     unsigned width = 0;
35     cli_option* o = cli_full_options[i];
36     if(o->name) {
37       width += strlen(o->name);
38       switch(o->type) {
39       case CLI_STRING:     width += 6; break;
40       case CLI_INTEGER:    width += 4; break;
41       case CLI_UINTEGER:   width += 4; break;
42       case CLI_STRINGLIST: width += 5; break;
43       case CLI_FUNCTION:   width += 6; break;
44       case CLI_SEPARATOR:  width = 0; break;
45       case CLI_FLAG:       break;
46       case CLI_COUNTER:    break;
47       }
48     }
49     if(width > maxwidth)
50       maxwidth = width;
51   }
52   return maxwidth;
53 }
54 
show_option(cli_option * o,unsigned maxwidth)55 static void show_option(cli_option* o, unsigned maxwidth)
56 {
57   if(o->type == CLI_SEPARATOR) {
58     obuf_put3s(&outbuf, "\n", o->name, ":\n");
59     return;
60   }
61   if(o == &cli_help_option) obuf_putc(&outbuf, '\n');
62   if(o->ch) {
63     obuf_puts(&outbuf, "  -");
64     obuf_putc(&outbuf, o->ch);
65   }
66   else
67     obuf_puts(&outbuf, "    ");
68   obuf_puts(&outbuf, (o->ch && o->name) ? ", " : "  ");
69   if(o->name) {
70     const char* extra = "";
71     switch(o->type) {
72     case CLI_STRING:     extra = "=VALUE"; break;
73     case CLI_INTEGER:    extra = "=INT"; break;
74     case CLI_UINTEGER:   extra = "=UNS"; break;
75     case CLI_STRINGLIST: extra = "=ITEM"; break;
76     case CLI_FUNCTION:   extra = "=VALUE"; break;
77     case CLI_FLAG:       break;
78     case CLI_COUNTER:    break;
79     case CLI_SEPARATOR:  break;
80     }
81     obuf_put3s(&outbuf, "--", o->name, extra);
82     obuf_pad(&outbuf, maxwidth - strlen(o->name) - strlen(extra) + 2, ' ');
83   }
84   else
85     obuf_pad(&outbuf, maxwidth+4, ' ');
86   obuf_put2s(&outbuf, o->helpstr, "\n");
87   if(o->defaultstr) {
88     obuf_pad(&outbuf, maxwidth+10, ' ');
89     obuf_put3s(&outbuf, "(Defaults to ", o->defaultstr, ")\n");
90   }
91 }
92 
cli_show_help(void)93 void cli_show_help(void)
94 {
95   unsigned maxwidth;
96   unsigned i;
97   obuf_puts(&outbuf, cli_help_prefix);
98   maxwidth = calc_max_width();
99   for(i = 0; i < cli_option_count; i++)
100     show_option(cli_full_options[i], maxwidth);
101   obuf_puts(&outbuf, cli_help_suffix);
102 }
103