1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov * Copyright (c) 1995 - 2006 Kungliga Tekniska Högskolan
3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray * All rights reserved.
5b528cefcSMark Murray *
6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray * modification, are permitted provided that the following conditions
8b528cefcSMark Murray * are met:
9b528cefcSMark Murray *
10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray *
13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray * documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray *
17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray * may be used to endorse or promote products derived from this software
19b528cefcSMark Murray * without specific prior written permission.
20b528cefcSMark Murray *
21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray * SUCH DAMAGE.
32b528cefcSMark Murray */
33b528cefcSMark Murray
34b528cefcSMark Murray #include <config.h>
35b528cefcSMark Murray
36b528cefcSMark Murray #include "sl_locl.h"
375e9cd1aeSAssar Westerlund #include <setjmp.h>
385e9cd1aeSAssar Westerlund
395e9cd1aeSAssar Westerlund static void
mandoc_template(SL_cmd * cmds,const char * extra_string)405e9cd1aeSAssar Westerlund mandoc_template(SL_cmd *cmds,
415e9cd1aeSAssar Westerlund const char *extra_string)
425e9cd1aeSAssar Westerlund {
435e9cd1aeSAssar Westerlund SL_cmd *c, *prev;
445e9cd1aeSAssar Westerlund char timestr[64], cmd[64];
455e9cd1aeSAssar Westerlund const char *p;
465e9cd1aeSAssar Westerlund time_t t;
475e9cd1aeSAssar Westerlund
485e9cd1aeSAssar Westerlund printf(".\\\" Things to fix:\n");
495e9cd1aeSAssar Westerlund printf(".\\\" * correct section, and operating system\n");
505e9cd1aeSAssar Westerlund printf(".\\\" * remove Op from mandatory flags\n");
515e9cd1aeSAssar Westerlund printf(".\\\" * use better macros for arguments (like .Pa for files)\n");
525e9cd1aeSAssar Westerlund printf(".\\\"\n");
535e9cd1aeSAssar Westerlund t = time(NULL);
545e9cd1aeSAssar Westerlund strftime(timestr, sizeof(timestr), "%b %d, %Y", localtime(&t));
555e9cd1aeSAssar Westerlund printf(".Dd %s\n", timestr);
56adb0ddaeSAssar Westerlund p = strrchr(getprogname(), '/');
57adb0ddaeSAssar Westerlund if(p) p++; else p = getprogname();
585e9cd1aeSAssar Westerlund strncpy(cmd, p, sizeof(cmd));
595e9cd1aeSAssar Westerlund cmd[sizeof(cmd)-1] = '\0';
605e9cd1aeSAssar Westerlund strupr(cmd);
615e9cd1aeSAssar Westerlund
625e9cd1aeSAssar Westerlund printf(".Dt %s SECTION\n", cmd);
635e9cd1aeSAssar Westerlund printf(".Os OPERATING_SYSTEM\n");
645e9cd1aeSAssar Westerlund printf(".Sh NAME\n");
655e9cd1aeSAssar Westerlund printf(".Nm %s\n", p);
665e9cd1aeSAssar Westerlund printf(".Nd\n");
675e9cd1aeSAssar Westerlund printf("in search of a description\n");
685e9cd1aeSAssar Westerlund printf(".Sh SYNOPSIS\n");
695e9cd1aeSAssar Westerlund printf(".Nm\n");
705e9cd1aeSAssar Westerlund for(c = cmds; c->name; ++c) {
715e9cd1aeSAssar Westerlund /* if (c->func == NULL)
725e9cd1aeSAssar Westerlund continue; */
735e9cd1aeSAssar Westerlund printf(".Op Fl %s", c->name);
745e9cd1aeSAssar Westerlund printf("\n");
755e9cd1aeSAssar Westerlund
765e9cd1aeSAssar Westerlund }
775e9cd1aeSAssar Westerlund if (extra_string && *extra_string)
785e9cd1aeSAssar Westerlund printf (".Ar %s\n", extra_string);
795e9cd1aeSAssar Westerlund printf(".Sh DESCRIPTION\n");
805e9cd1aeSAssar Westerlund printf("Supported options:\n");
815e9cd1aeSAssar Westerlund printf(".Bl -tag -width Ds\n");
825e9cd1aeSAssar Westerlund prev = NULL;
835e9cd1aeSAssar Westerlund for(c = cmds; c->name; ++c) {
845e9cd1aeSAssar Westerlund if (c->func) {
855e9cd1aeSAssar Westerlund if (prev)
865e9cd1aeSAssar Westerlund printf ("\n%s\n", prev->usage);
875e9cd1aeSAssar Westerlund
885e9cd1aeSAssar Westerlund printf (".It Fl %s", c->name);
895e9cd1aeSAssar Westerlund prev = c;
905e9cd1aeSAssar Westerlund } else
915e9cd1aeSAssar Westerlund printf (", %s\n", c->name);
925e9cd1aeSAssar Westerlund }
935e9cd1aeSAssar Westerlund if (prev)
945e9cd1aeSAssar Westerlund printf ("\n%s\n", prev->usage);
955e9cd1aeSAssar Westerlund
965e9cd1aeSAssar Westerlund printf(".El\n");
975e9cd1aeSAssar Westerlund printf(".\\\".Sh ENVIRONMENT\n");
985e9cd1aeSAssar Westerlund printf(".\\\".Sh FILES\n");
995e9cd1aeSAssar Westerlund printf(".\\\".Sh EXAMPLES\n");
1005e9cd1aeSAssar Westerlund printf(".\\\".Sh DIAGNOSTICS\n");
1015e9cd1aeSAssar Westerlund printf(".\\\".Sh SEE ALSO\n");
1025e9cd1aeSAssar Westerlund printf(".\\\".Sh STANDARDS\n");
1035e9cd1aeSAssar Westerlund printf(".\\\".Sh HISTORY\n");
1045e9cd1aeSAssar Westerlund printf(".\\\".Sh AUTHORS\n");
1055e9cd1aeSAssar Westerlund printf(".\\\".Sh BUGS\n");
1065e9cd1aeSAssar Westerlund }
107b528cefcSMark Murray
108c19800e8SDoug Rabson SL_cmd *
sl_match(SL_cmd * cmds,char * cmd,int exactp)109b528cefcSMark Murray sl_match (SL_cmd *cmds, char *cmd, int exactp)
110b528cefcSMark Murray {
111b528cefcSMark Murray SL_cmd *c, *current = NULL, *partial_cmd = NULL;
112b528cefcSMark Murray int partial_match = 0;
113b528cefcSMark Murray
114b528cefcSMark Murray for (c = cmds; c->name; ++c) {
115b528cefcSMark Murray if (c->func)
116b528cefcSMark Murray current = c;
117b528cefcSMark Murray if (strcmp (cmd, c->name) == 0)
118b528cefcSMark Murray return current;
119b528cefcSMark Murray else if (strncmp (cmd, c->name, strlen(cmd)) == 0 &&
120b528cefcSMark Murray partial_cmd != current) {
121b528cefcSMark Murray ++partial_match;
122b528cefcSMark Murray partial_cmd = current;
123b528cefcSMark Murray }
124b528cefcSMark Murray }
125b528cefcSMark Murray if (partial_match == 1 && !exactp)
126b528cefcSMark Murray return partial_cmd;
127b528cefcSMark Murray else
128b528cefcSMark Murray return NULL;
129b528cefcSMark Murray }
130b528cefcSMark Murray
131b528cefcSMark Murray void
sl_help(SL_cmd * cmds,int argc,char ** argv)132b528cefcSMark Murray sl_help (SL_cmd *cmds, int argc, char **argv)
133b528cefcSMark Murray {
134b528cefcSMark Murray SL_cmd *c, *prev_c;
135b528cefcSMark Murray
1365e9cd1aeSAssar Westerlund if (getenv("SLMANDOC")) {
1375e9cd1aeSAssar Westerlund mandoc_template(cmds, NULL);
1385e9cd1aeSAssar Westerlund return;
1395e9cd1aeSAssar Westerlund }
1405e9cd1aeSAssar Westerlund
141b528cefcSMark Murray if (argc == 1) {
142b528cefcSMark Murray prev_c = NULL;
143b528cefcSMark Murray for (c = cmds; c->name; ++c) {
144b528cefcSMark Murray if (c->func) {
145b528cefcSMark Murray if(prev_c)
146b528cefcSMark Murray printf ("\n\t%s%s", prev_c->usage ? prev_c->usage : "",
147b528cefcSMark Murray prev_c->usage ? "\n" : "");
148b528cefcSMark Murray prev_c = c;
149b528cefcSMark Murray printf ("%s", c->name);
150b528cefcSMark Murray } else
151b528cefcSMark Murray printf (", %s", c->name);
152b528cefcSMark Murray }
153b528cefcSMark Murray if(prev_c)
154b528cefcSMark Murray printf ("\n\t%s%s", prev_c->usage ? prev_c->usage : "",
155b528cefcSMark Murray prev_c->usage ? "\n" : "");
156b528cefcSMark Murray } else {
157b528cefcSMark Murray c = sl_match (cmds, argv[1], 0);
158b528cefcSMark Murray if (c == NULL)
159b528cefcSMark Murray printf ("No such command: %s. "
160b528cefcSMark Murray "Try \"help\" for a list of all commands\n",
161b528cefcSMark Murray argv[1]);
162b528cefcSMark Murray else {
163b528cefcSMark Murray printf ("%s\t%s\n", c->name, c->usage);
164b528cefcSMark Murray if(c->help && *c->help)
165b528cefcSMark Murray printf ("%s\n", c->help);
166b528cefcSMark Murray if((++c)->name && c->func == NULL) {
167b528cefcSMark Murray printf ("Synonyms:");
168b528cefcSMark Murray while (c->name && c->func == NULL)
169b528cefcSMark Murray printf ("\t%s", (c++)->name);
170b528cefcSMark Murray printf ("\n");
171b528cefcSMark Murray }
172b528cefcSMark Murray }
173b528cefcSMark Murray }
174b528cefcSMark Murray }
175b528cefcSMark Murray
176b528cefcSMark Murray #ifdef HAVE_READLINE
177b528cefcSMark Murray
178b528cefcSMark Murray char *readline(char *prompt);
179b528cefcSMark Murray void add_history(char *p);
180b528cefcSMark Murray
181b528cefcSMark Murray #else
182b528cefcSMark Murray
183b528cefcSMark Murray static char *
readline(char * prompt)184b528cefcSMark Murray readline(char *prompt)
185b528cefcSMark Murray {
186b528cefcSMark Murray char buf[BUFSIZ];
187b528cefcSMark Murray printf ("%s", prompt);
188b528cefcSMark Murray fflush (stdout);
189b528cefcSMark Murray if(fgets(buf, sizeof(buf), stdin) == NULL)
190b528cefcSMark Murray return NULL;
191c19800e8SDoug Rabson buf[strcspn(buf, "\r\n")] = '\0';
192b528cefcSMark Murray return strdup(buf);
193b528cefcSMark Murray }
194b528cefcSMark Murray
195b528cefcSMark Murray static void
add_history(char * p)196b528cefcSMark Murray add_history(char *p)
197b528cefcSMark Murray {
198b528cefcSMark Murray }
199b528cefcSMark Murray
200b528cefcSMark Murray #endif
201b528cefcSMark Murray
202b528cefcSMark Murray int
sl_command(SL_cmd * cmds,int argc,char ** argv)203b528cefcSMark Murray sl_command(SL_cmd *cmds, int argc, char **argv)
204b528cefcSMark Murray {
205b528cefcSMark Murray SL_cmd *c;
206b528cefcSMark Murray c = sl_match (cmds, argv[0], 0);
207b528cefcSMark Murray if (c == NULL)
208b528cefcSMark Murray return -1;
209b528cefcSMark Murray return (*c->func)(argc, argv);
210b528cefcSMark Murray }
211b528cefcSMark Murray
212b528cefcSMark Murray struct sl_data {
213b528cefcSMark Murray int max_count;
214b528cefcSMark Murray char **ptr;
215b528cefcSMark Murray };
216b528cefcSMark Murray
217b528cefcSMark Murray int
sl_make_argv(char * line,int * ret_argc,char *** ret_argv)218b528cefcSMark Murray sl_make_argv(char *line, int *ret_argc, char ***ret_argv)
219b528cefcSMark Murray {
220c19800e8SDoug Rabson char *p, *begining;
221b528cefcSMark Murray int argc, nargv;
222b528cefcSMark Murray char **argv;
223c19800e8SDoug Rabson int quote = 0;
224b528cefcSMark Murray
225b528cefcSMark Murray nargv = 10;
226b528cefcSMark Murray argv = malloc(nargv * sizeof(*argv));
227b528cefcSMark Murray if(argv == NULL)
228b528cefcSMark Murray return ENOMEM;
229b528cefcSMark Murray argc = 0;
230b528cefcSMark Murray
231c19800e8SDoug Rabson p = line;
232c19800e8SDoug Rabson
233c19800e8SDoug Rabson while(isspace((unsigned char)*p))
234c19800e8SDoug Rabson p++;
235c19800e8SDoug Rabson begining = p;
236c19800e8SDoug Rabson
237c19800e8SDoug Rabson while (1) {
238c19800e8SDoug Rabson if (*p == '\0') {
239c19800e8SDoug Rabson ;
240c19800e8SDoug Rabson } else if (*p == '"') {
241c19800e8SDoug Rabson quote = !quote;
242c19800e8SDoug Rabson memmove(&p[0], &p[1], strlen(&p[1]) + 1);
243c19800e8SDoug Rabson continue;
244c19800e8SDoug Rabson } else if (*p == '\\') {
245c19800e8SDoug Rabson if (p[1] == '\0')
246c19800e8SDoug Rabson goto failed;
247c19800e8SDoug Rabson memmove(&p[0], &p[1], strlen(&p[1]) + 1);
248c19800e8SDoug Rabson p += 2;
249c19800e8SDoug Rabson continue;
250c19800e8SDoug Rabson } else if (quote || !isspace((unsigned char)*p)) {
251c19800e8SDoug Rabson p++;
252c19800e8SDoug Rabson continue;
253c19800e8SDoug Rabson } else
254c19800e8SDoug Rabson *p++ = '\0';
255c19800e8SDoug Rabson if (quote)
256c19800e8SDoug Rabson goto failed;
257b528cefcSMark Murray if(argc == nargv - 1) {
258b528cefcSMark Murray char **tmp;
259b528cefcSMark Murray nargv *= 2;
260b528cefcSMark Murray tmp = realloc (argv, nargv * sizeof(*argv));
261b528cefcSMark Murray if (tmp == NULL) {
262b528cefcSMark Murray free(argv);
263b528cefcSMark Murray return ENOMEM;
264b528cefcSMark Murray }
265b528cefcSMark Murray argv = tmp;
266b528cefcSMark Murray }
267c19800e8SDoug Rabson argv[argc++] = begining;
268c19800e8SDoug Rabson while(isspace((unsigned char)*p))
269c19800e8SDoug Rabson p++;
270c19800e8SDoug Rabson if (*p == '\0')
271c19800e8SDoug Rabson break;
272c19800e8SDoug Rabson begining = p;
273b528cefcSMark Murray }
274b528cefcSMark Murray argv[argc] = NULL;
275b528cefcSMark Murray *ret_argc = argc;
276b528cefcSMark Murray *ret_argv = argv;
277b528cefcSMark Murray return 0;
278c19800e8SDoug Rabson failed:
279c19800e8SDoug Rabson free(argv);
280c19800e8SDoug Rabson return ERANGE;
281b528cefcSMark Murray }
282b528cefcSMark Murray
2835e9cd1aeSAssar Westerlund static jmp_buf sl_jmp;
2845e9cd1aeSAssar Westerlund
sl_sigint(int sig)2855e9cd1aeSAssar Westerlund static void sl_sigint(int sig)
2865e9cd1aeSAssar Westerlund {
2875e9cd1aeSAssar Westerlund longjmp(sl_jmp, 1);
2885e9cd1aeSAssar Westerlund }
2895e9cd1aeSAssar Westerlund
sl_readline(const char * prompt)2905e9cd1aeSAssar Westerlund static char *sl_readline(const char *prompt)
2915e9cd1aeSAssar Westerlund {
2925e9cd1aeSAssar Westerlund char *s;
2935e9cd1aeSAssar Westerlund void (*old)(int);
2945e9cd1aeSAssar Westerlund old = signal(SIGINT, sl_sigint);
2955e9cd1aeSAssar Westerlund if(setjmp(sl_jmp))
2965e9cd1aeSAssar Westerlund printf("\n");
297c19800e8SDoug Rabson s = readline(rk_UNCONST(prompt));
2985e9cd1aeSAssar Westerlund signal(SIGINT, old);
2995e9cd1aeSAssar Westerlund return s;
3005e9cd1aeSAssar Westerlund }
3015e9cd1aeSAssar Westerlund
302c19800e8SDoug Rabson /* return values:
303c19800e8SDoug Rabson * 0 on success,
304c19800e8SDoug Rabson * -1 on fatal error,
305c19800e8SDoug Rabson * -2 if EOF, or
306c19800e8SDoug Rabson * return value of command */
307b528cefcSMark Murray int
sl_command_loop(SL_cmd * cmds,const char * prompt,void ** data)3085e9cd1aeSAssar Westerlund sl_command_loop(SL_cmd *cmds, const char *prompt, void **data)
309b528cefcSMark Murray {
310b528cefcSMark Murray int ret = 0;
311b528cefcSMark Murray char *buf;
312b528cefcSMark Murray int argc;
313b528cefcSMark Murray char **argv;
314b528cefcSMark Murray
3155e9cd1aeSAssar Westerlund buf = sl_readline(prompt);
316b528cefcSMark Murray if(buf == NULL)
317c19800e8SDoug Rabson return -2;
318b528cefcSMark Murray
319b528cefcSMark Murray if(*buf)
320b528cefcSMark Murray add_history(buf);
321b528cefcSMark Murray ret = sl_make_argv(buf, &argc, &argv);
322b528cefcSMark Murray if(ret) {
323b528cefcSMark Murray fprintf(stderr, "sl_loop: out of memory\n");
324b528cefcSMark Murray free(buf);
325b528cefcSMark Murray return -1;
326b528cefcSMark Murray }
327b528cefcSMark Murray if (argc >= 1) {
328b528cefcSMark Murray ret = sl_command(cmds, argc, argv);
329b528cefcSMark Murray if(ret == -1) {
330b528cefcSMark Murray printf ("Unrecognized command: %s\n", argv[0]);
331b528cefcSMark Murray ret = 0;
332b528cefcSMark Murray }
333b528cefcSMark Murray }
334b528cefcSMark Murray free(buf);
335b528cefcSMark Murray free(argv);
336b528cefcSMark Murray return ret;
337b528cefcSMark Murray }
338b528cefcSMark Murray
339b528cefcSMark Murray int
sl_loop(SL_cmd * cmds,const char * prompt)3405e9cd1aeSAssar Westerlund sl_loop(SL_cmd *cmds, const char *prompt)
341b528cefcSMark Murray {
342b528cefcSMark Murray void *data = NULL;
343b528cefcSMark Murray int ret;
344c19800e8SDoug Rabson while((ret = sl_command_loop(cmds, prompt, &data)) >= 0)
345b528cefcSMark Murray ;
346b528cefcSMark Murray return ret;
347b528cefcSMark Murray }
3485e9cd1aeSAssar Westerlund
3495e9cd1aeSAssar Westerlund void
sl_apropos(SL_cmd * cmd,const char * topic)3505e9cd1aeSAssar Westerlund sl_apropos (SL_cmd *cmd, const char *topic)
3515e9cd1aeSAssar Westerlund {
3525e9cd1aeSAssar Westerlund for (; cmd->name != NULL; ++cmd)
3535e9cd1aeSAssar Westerlund if (cmd->usage != NULL && strstr(cmd->usage, topic) != NULL)
3545e9cd1aeSAssar Westerlund printf ("%-20s%s\n", cmd->name, cmd->usage);
3555e9cd1aeSAssar Westerlund }
356c19800e8SDoug Rabson
357c19800e8SDoug Rabson /*
358c19800e8SDoug Rabson * Help to be used with slc.
359c19800e8SDoug Rabson */
360c19800e8SDoug Rabson
361c19800e8SDoug Rabson void
sl_slc_help(SL_cmd * cmds,int argc,char ** argv)362c19800e8SDoug Rabson sl_slc_help (SL_cmd *cmds, int argc, char **argv)
363c19800e8SDoug Rabson {
364c19800e8SDoug Rabson if(argc == 0) {
365c19800e8SDoug Rabson sl_help(cmds, 1, argv - 1 /* XXX */);
366c19800e8SDoug Rabson } else {
367c19800e8SDoug Rabson SL_cmd *c = sl_match (cmds, argv[0], 0);
368c19800e8SDoug Rabson if(c == NULL) {
369c19800e8SDoug Rabson fprintf (stderr, "No such command: %s. "
370c19800e8SDoug Rabson "Try \"help\" for a list of commands\n",
371c19800e8SDoug Rabson argv[0]);
372c19800e8SDoug Rabson } else {
373c19800e8SDoug Rabson if(c->func) {
374*ae771770SStanislav Sedov static char help[] = "--help";
375*ae771770SStanislav Sedov char *fake[3];
376c19800e8SDoug Rabson fake[0] = argv[0];
377*ae771770SStanislav Sedov fake[1] = help;
378*ae771770SStanislav Sedov fake[2] = NULL;
379c19800e8SDoug Rabson (*c->func)(2, fake);
380c19800e8SDoug Rabson fprintf(stderr, "\n");
381c19800e8SDoug Rabson }
382c19800e8SDoug Rabson if(c->help && *c->help)
383c19800e8SDoug Rabson fprintf (stderr, "%s\n", c->help);
384c19800e8SDoug Rabson if((++c)->name && c->func == NULL) {
385c19800e8SDoug Rabson int f = 0;
386c19800e8SDoug Rabson fprintf (stderr, "Synonyms:");
387c19800e8SDoug Rabson while (c->name && c->func == NULL) {
388c19800e8SDoug Rabson fprintf (stderr, "%s%s", f ? ", " : " ", (c++)->name);
389c19800e8SDoug Rabson f = 1;
390c19800e8SDoug Rabson }
391c19800e8SDoug Rabson fprintf (stderr, "\n");
392c19800e8SDoug Rabson }
393c19800e8SDoug Rabson }
394c19800e8SDoug Rabson }
395c19800e8SDoug Rabson }
396