1 /* $NetBSD: sl.c,v 1.1.1.2 2014/04/24 12:45:53 pettai Exp $ */
4 * Copyright (c) 1995 - 2006 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 mandoc_template(SL_cmd
*cmds
,
43 const char *extra_string
)
46 char timestr
[64], cmd
[64];
50 printf(".\\\" Things to fix:\n");
51 printf(".\\\" * correct section, and operating system\n");
52 printf(".\\\" * remove Op from mandatory flags\n");
53 printf(".\\\" * use better macros for arguments (like .Pa for files)\n");
56 strftime(timestr
, sizeof(timestr
), "%b %d, %Y", localtime(&t
));
57 printf(".Dd %s\n", timestr
);
58 p
= strrchr(getprogname(), '/');
59 if(p
) p
++; else p
= getprogname();
60 strncpy(cmd
, p
, sizeof(cmd
));
61 cmd
[sizeof(cmd
)-1] = '\0';
64 printf(".Dt %s SECTION\n", cmd
);
65 printf(".Os OPERATING_SYSTEM\n");
67 printf(".Nm %s\n", p
);
69 printf("in search of a description\n");
70 printf(".Sh SYNOPSIS\n");
72 for(c
= cmds
; c
->name
; ++c
) {
73 /* if (c->func == NULL)
75 printf(".Op Fl %s", c
->name
);
79 if (extra_string
&& *extra_string
)
80 printf (".Ar %s\n", extra_string
);
81 printf(".Sh DESCRIPTION\n");
82 printf("Supported options:\n");
83 printf(".Bl -tag -width Ds\n");
85 for(c
= cmds
; c
->name
; ++c
) {
88 printf ("\n%s\n", prev
->usage
);
90 printf (".It Fl %s", c
->name
);
93 printf (", %s\n", c
->name
);
96 printf ("\n%s\n", prev
->usage
);
99 printf(".\\\".Sh ENVIRONMENT\n");
100 printf(".\\\".Sh FILES\n");
101 printf(".\\\".Sh EXAMPLES\n");
102 printf(".\\\".Sh DIAGNOSTICS\n");
103 printf(".\\\".Sh SEE ALSO\n");
104 printf(".\\\".Sh STANDARDS\n");
105 printf(".\\\".Sh HISTORY\n");
106 printf(".\\\".Sh AUTHORS\n");
107 printf(".\\\".Sh BUGS\n");
111 sl_match (SL_cmd
*cmds
, char *cmd
, int exactp
)
113 SL_cmd
*c
, *current
= NULL
, *partial_cmd
= NULL
;
114 int partial_match
= 0;
116 for (c
= cmds
; c
->name
; ++c
) {
119 if (strcmp (cmd
, c
->name
) == 0)
121 else if (strncmp (cmd
, c
->name
, strlen(cmd
)) == 0 &&
122 partial_cmd
!= current
) {
124 partial_cmd
= current
;
127 if (partial_match
== 1 && !exactp
)
134 sl_help (SL_cmd
*cmds
, int argc
, char **argv
)
138 if (getenv("SLMANDOC")) {
139 mandoc_template(cmds
, NULL
);
145 for (c
= cmds
; c
->name
; ++c
) {
148 printf ("\n\t%s%s", prev_c
->usage
? prev_c
->usage
: "",
149 prev_c
->usage
? "\n" : "");
151 printf ("%s", c
->name
);
153 printf (", %s", c
->name
);
156 printf ("\n\t%s%s", prev_c
->usage
? prev_c
->usage
: "",
157 prev_c
->usage
? "\n" : "");
159 c
= sl_match (cmds
, argv
[1], 0);
161 printf ("No such command: %s. "
162 "Try \"help\" for a list of all commands\n",
165 printf ("%s\t%s\n", c
->name
, c
->usage
);
166 if(c
->help
&& *c
->help
)
167 printf ("%s\n", c
->help
);
168 if((++c
)->name
&& c
->func
== NULL
) {
169 printf ("Synonyms:");
170 while (c
->name
&& c
->func
== NULL
)
171 printf ("\t%s", (c
++)->name
);
180 char *readline(char *prompt
);
181 void add_history(char *p
);
186 readline(char *prompt
)
189 printf ("%s", prompt
);
191 if(fgets(buf
, sizeof(buf
), stdin
) == NULL
)
193 buf
[strcspn(buf
, "\r\n")] = '\0';
205 sl_command(SL_cmd
*cmds
, int argc
, char **argv
)
208 c
= sl_match (cmds
, argv
[0], 0);
211 return (*c
->func
)(argc
, argv
);
220 sl_make_argv(char *line
, int *ret_argc
, char ***ret_argv
)
228 argv
= malloc(nargv
* sizeof(*argv
));
235 while(isspace((unsigned char)*p
))
242 } else if (*p
== '"') {
244 memmove(&p
[0], &p
[1], strlen(&p
[1]) + 1);
246 } else if (*p
== '\\') {
249 memmove(&p
[0], &p
[1], strlen(&p
[1]) + 1);
252 } else if (quote
|| !isspace((unsigned char)*p
)) {
259 if(argc
== nargv
- 1) {
262 tmp
= realloc (argv
, nargv
* sizeof(*argv
));
269 argv
[argc
++] = begining
;
270 while(isspace((unsigned char)*p
))
285 static jmp_buf sl_jmp
;
287 static void sl_sigint(int sig
)
292 static char *sl_readline(const char *prompt
)
296 old
= signal(SIGINT
, sl_sigint
);
299 s
= readline(rk_UNCONST(prompt
));
308 * return value of command */
310 sl_command_loop(SL_cmd
*cmds
, const char *prompt
, void **data
)
317 buf
= sl_readline(prompt
);
323 ret
= sl_make_argv(buf
, &argc
, &argv
);
325 fprintf(stderr
, "sl_loop: out of memory\n");
330 ret
= sl_command(cmds
, argc
, argv
);
332 printf ("Unrecognized command: %s\n", argv
[0]);
342 sl_loop(SL_cmd
*cmds
, const char *prompt
)
346 while((ret
= sl_command_loop(cmds
, prompt
, &data
)) >= 0)
352 sl_apropos (SL_cmd
*cmd
, const char *topic
)
354 for (; cmd
->name
!= NULL
; ++cmd
)
355 if (cmd
->usage
!= NULL
&& strstr(cmd
->usage
, topic
) != NULL
)
356 printf ("%-20s%s\n", cmd
->name
, cmd
->usage
);
360 * Help to be used with slc.
364 sl_slc_help (SL_cmd
*cmds
, int argc
, char **argv
)
367 sl_help(cmds
, 1, argv
- 1 /* XXX */);
369 SL_cmd
*c
= sl_match (cmds
, argv
[0], 0);
371 fprintf (stderr
, "No such command: %s. "
372 "Try \"help\" for a list of commands\n",
376 static char help
[] = "--help";
382 fprintf(stderr
, "\n");
384 if(c
->help
&& *c
->help
)
385 fprintf (stderr
, "%s\n", c
->help
);
386 if((++c
)->name
&& c
->func
== NULL
) {
388 fprintf (stderr
, "Synonyms:");
389 while (c
->name
&& c
->func
== NULL
) {
390 fprintf (stderr
, "%s%s", f
? ", " : " ", (c
++)->name
);
393 fprintf (stderr
, "\n");