2 * Copyright (c) 1995, 1996, 1997 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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the Kungliga Tekniska
20 * Högskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 sl_match (SL_cmd
*cmds
, char *cmd
, int exactp
)
49 SL_cmd
*c
, *current
= NULL
, *partial_cmd
= NULL
;
50 int partial_match
= 0;
52 for (c
= cmds
; c
->name
; ++c
) {
55 if (strcmp (cmd
, c
->name
) == 0)
57 else if (strncmp (cmd
, c
->name
, strlen(cmd
)) == 0 &&
58 partial_cmd
!= current
) {
60 partial_cmd
= current
;
63 if (partial_match
== 1 && !exactp
)
70 sl_help (SL_cmd
*cmds
, int argc
, char **argv
)
76 for (c
= cmds
; c
->name
; ++c
) {
79 printf ("\n\t%s%s", prev_c
->usage
? prev_c
->usage
: "",
80 prev_c
->usage
? "\n" : "");
82 printf ("%s", c
->name
);
84 printf (", %s", c
->name
);
87 printf ("\n\t%s%s", prev_c
->usage
? prev_c
->usage
: "",
88 prev_c
->usage
? "\n" : "");
90 c
= sl_match (cmds
, argv
[1], 0);
92 printf ("No such command: %s. Try \"help\" for a list of all commands\n",
95 printf ("%s\t%s", c
->name
, c
->usage
);
96 if((++c
)->name
&& c
->func
== NULL
) {
97 printf ("\nSynonyms:");
98 while (c
->name
&& c
->func
== NULL
)
99 printf ("\t%s", (c
++)->name
);
108 char *readline(char *prompt
);
109 void add_history(char *p
);
114 readline(char *prompt
)
117 printf ("%s", prompt
);
119 if(fgets(buf
, sizeof(buf
), stdin
) == NULL
)
121 if (buf
[strlen(buf
) - 1] == '\n')
122 buf
[strlen(buf
) - 1] = '\0';
134 sl_command(SL_cmd
*cmds
, int argc
, char **argv
)
137 c
= sl_match (cmds
, argv
[0], 0);
140 return (*c
->func
)(argc
, argv
);
144 sl_loop (SL_cmd
*cmds
, char *prompt
)
151 ptr
= malloc(max_count
* sizeof(*ptr
));
153 printf ("sl_loop: failed to allocate %u bytes of memory\n",
154 (int) max_count
* sizeof(*ptr
));
164 buf
= readline(prompt
);
175 for(p
= strtok_r (buf
, " \t", &foo
);
177 p
= strtok_r (NULL
, " \t", &foo
)) {
178 if(count
== max_count
) {
180 ptr
= realloc (ptr
, max_count
* sizeof(*ptr
));
182 printf ("sl_loop: failed to allocate %u "
184 (unsigned) max_count
* sizeof(*ptr
));
192 c
= sl_match (cmds
, ptr
[0], 0);
194 ret
= (*c
->func
)(count
, ptr
);
200 printf ("Unrecognized command: %s\n", ptr
[0]);