2 * Copyright (c) 1995 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 mandoc_template(SL_cmd
*cmds
,
44 const char *extra_string
)
47 char timestr
[64], cmd
[64];
51 printf(".\\\" Things to fix:\n");
52 printf(".\\\" * correct section, and operating system\n");
53 printf(".\\\" * remove Op from mandatory flags\n");
54 printf(".\\\" * use better macros for arguments (like .Pa for files)\n");
57 strftime(timestr
, sizeof(timestr
), "%b %d, %Y", localtime(&t
));
58 printf(".Dd %s\n", timestr
);
59 p
= strrchr(getprogname(), '/');
60 if(p
) p
++; else p
= getprogname();
61 strncpy(cmd
, p
, sizeof(cmd
));
62 cmd
[sizeof(cmd
)-1] = '\0';
65 printf(".Dt %s SECTION\n", cmd
);
66 printf(".Os OPERATING_SYSTEM\n");
68 printf(".Nm %s\n", p
);
70 printf("in search of a description\n");
71 printf(".Sh SYNOPSIS\n");
73 for(c
= cmds
; c
->name
; ++c
) {
74 /* if (c->func == NULL)
76 printf(".Op Fl %s", c
->name
);
80 if (extra_string
&& *extra_string
)
81 printf (".Ar %s\n", extra_string
);
82 printf(".Sh DESCRIPTION\n");
83 printf("Supported options:\n");
84 printf(".Bl -tag -width Ds\n");
86 for(c
= cmds
; c
->name
; ++c
) {
89 printf ("\n%s\n", prev
->usage
);
91 printf (".It Fl %s", c
->name
);
94 printf (", %s\n", c
->name
);
97 printf ("\n%s\n", prev
->usage
);
100 printf(".\\\".Sh ENVIRONMENT\n");
101 printf(".\\\".Sh FILES\n");
102 printf(".\\\".Sh EXAMPLES\n");
103 printf(".\\\".Sh DIAGNOSTICS\n");
104 printf(".\\\".Sh SEE ALSO\n");
105 printf(".\\\".Sh STANDARDS\n");
106 printf(".\\\".Sh HISTORY\n");
107 printf(".\\\".Sh AUTHORS\n");
108 printf(".\\\".Sh BUGS\n");
112 sl_match (SL_cmd
*cmds
, char *cmd
, int exactp
)
114 SL_cmd
*c
, *current
= NULL
, *partial_cmd
= NULL
;
115 int partial_match
= 0;
117 for (c
= cmds
; c
->name
; ++c
) {
120 if (strcmp (cmd
, c
->name
) == 0)
122 else if (strncmp (cmd
, c
->name
, strlen(cmd
)) == 0 &&
123 partial_cmd
!= current
) {
125 partial_cmd
= current
;
128 if (partial_match
== 1 && !exactp
)
135 sl_help (SL_cmd
*cmds
, int argc
, char **argv
)
139 if (getenv("SLMANDOC")) {
140 mandoc_template(cmds
, NULL
);
146 for (c
= cmds
; c
->name
; ++c
) {
149 printf ("\n\t%s%s", prev_c
->usage
? prev_c
->usage
: "",
150 prev_c
->usage
? "\n" : "");
152 printf ("%s", c
->name
);
154 printf (", %s", c
->name
);
157 printf ("\n\t%s%s", prev_c
->usage
? prev_c
->usage
: "",
158 prev_c
->usage
? "\n" : "");
160 c
= sl_match (cmds
, argv
[1], 0);
162 printf ("No such command: %s. "
163 "Try \"help\" for a list of all commands\n",
166 printf ("%s\t%s\n", c
->name
, c
->usage
);
167 if(c
->help
&& *c
->help
)
168 printf ("%s\n", c
->help
);
169 if((++c
)->name
&& c
->func
== NULL
) {
170 printf ("Synonyms:");
171 while (c
->name
&& c
->func
== NULL
)
172 printf ("\t%s", (c
++)->name
);
181 char *readline(char *prompt
);
182 void add_history(char *p
);
187 readline(char *prompt
)
190 printf ("%s", prompt
);
192 if(fgets(buf
, sizeof(buf
), stdin
) == NULL
)
194 buf
[strcspn(buf
, "\r\n")] = '\0';
206 sl_command(SL_cmd
*cmds
, int argc
, char **argv
)
209 c
= sl_match (cmds
, argv
[0], 0);
212 return (*c
->func
)(argc
, argv
);
221 sl_make_argv(char *line
, int *ret_argc
, char ***ret_argv
)
229 argv
= malloc(nargv
* sizeof(*argv
));
236 while(isspace((unsigned char)*p
))
243 } else if (*p
== '"') {
245 memmove(&p
[0], &p
[1], strlen(&p
[1]) + 1);
247 } else if (*p
== '\\') {
250 memmove(&p
[0], &p
[1], strlen(&p
[1]) + 1);
253 } else if (quote
|| !isspace((unsigned char)*p
)) {
260 if(argc
== nargv
- 1) {
263 tmp
= realloc (argv
, nargv
* sizeof(*argv
));
270 argv
[argc
++] = begining
;
271 while(isspace((unsigned char)*p
))
286 static jmp_buf sl_jmp
;
288 static void sl_sigint(int sig
)
293 static char *sl_readline(const char *prompt
)
297 old
= signal(SIGINT
, sl_sigint
);
300 s
= readline(rk_UNCONST(prompt
));
309 * return value of command */
311 sl_command_loop(SL_cmd
*cmds
, const char *prompt
, void **data
)
319 buf
= sl_readline(prompt
);
325 ret
= sl_make_argv(buf
, &argc
, &argv
);
327 fprintf(stderr
, "sl_loop: out of memory\n");
332 ret
= sl_command(cmds
, argc
, argv
);
334 printf ("Unrecognized command: %s\n", argv
[0]);
344 sl_loop(SL_cmd
*cmds
, const char *prompt
)
348 while((ret
= sl_command_loop(cmds
, prompt
, &data
)) >= 0)
354 sl_apropos (SL_cmd
*cmd
, const char *topic
)
356 for (; cmd
->name
!= NULL
; ++cmd
)
357 if (cmd
->usage
!= NULL
&& strstr(cmd
->usage
, topic
) != NULL
)
358 printf ("%-20s%s\n", cmd
->name
, cmd
->usage
);
362 * Help to be used with slc.
366 sl_slc_help (SL_cmd
*cmds
, int argc
, char **argv
)
369 sl_help(cmds
, 1, argv
- 1 /* XXX */);
371 SL_cmd
*c
= sl_match (cmds
, argv
[0], 0);
373 fprintf (stderr
, "No such command: %s. "
374 "Try \"help\" for a list of commands\n",
378 char *fake
[] = { NULL
, "--help", NULL
};
381 fprintf(stderr
, "\n");
383 if(c
->help
&& *c
->help
)
384 fprintf (stderr
, "%s\n", c
->help
);
385 if((++c
)->name
&& c
->func
== NULL
) {
387 fprintf (stderr
, "Synonyms:");
388 while (c
->name
&& c
->func
== NULL
) {
389 fprintf (stderr
, "%s%s", f
? ", " : " ", (c
++)->name
);
392 fprintf (stderr
, "\n");