1b528cefcSMark Murray /* 2b528cefcSMark Murray * Copyright (c) 1995, 1996, 1997, 1998 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 #ifdef HAVE_CONFIG_H 35b528cefcSMark Murray #include <config.h> 36b528cefcSMark Murray RCSID("$Id: sl.c,v 1.25 1999/12/02 16:58:55 joda Exp $"); 37b528cefcSMark Murray #endif 38b528cefcSMark Murray 39b528cefcSMark Murray #include "sl_locl.h" 40b528cefcSMark Murray 41b528cefcSMark Murray static SL_cmd * 42b528cefcSMark Murray sl_match (SL_cmd *cmds, char *cmd, int exactp) 43b528cefcSMark Murray { 44b528cefcSMark Murray SL_cmd *c, *current = NULL, *partial_cmd = NULL; 45b528cefcSMark Murray int partial_match = 0; 46b528cefcSMark Murray 47b528cefcSMark Murray for (c = cmds; c->name; ++c) { 48b528cefcSMark Murray if (c->func) 49b528cefcSMark Murray current = c; 50b528cefcSMark Murray if (strcmp (cmd, c->name) == 0) 51b528cefcSMark Murray return current; 52b528cefcSMark Murray else if (strncmp (cmd, c->name, strlen(cmd)) == 0 && 53b528cefcSMark Murray partial_cmd != current) { 54b528cefcSMark Murray ++partial_match; 55b528cefcSMark Murray partial_cmd = current; 56b528cefcSMark Murray } 57b528cefcSMark Murray } 58b528cefcSMark Murray if (partial_match == 1 && !exactp) 59b528cefcSMark Murray return partial_cmd; 60b528cefcSMark Murray else 61b528cefcSMark Murray return NULL; 62b528cefcSMark Murray } 63b528cefcSMark Murray 64b528cefcSMark Murray void 65b528cefcSMark Murray sl_help (SL_cmd *cmds, int argc, char **argv) 66b528cefcSMark Murray { 67b528cefcSMark Murray SL_cmd *c, *prev_c; 68b528cefcSMark Murray 69b528cefcSMark Murray if (argc == 1) { 70b528cefcSMark Murray prev_c = NULL; 71b528cefcSMark Murray for (c = cmds; c->name; ++c) { 72b528cefcSMark Murray if (c->func) { 73b528cefcSMark Murray if(prev_c) 74b528cefcSMark Murray printf ("\n\t%s%s", prev_c->usage ? prev_c->usage : "", 75b528cefcSMark Murray prev_c->usage ? "\n" : ""); 76b528cefcSMark Murray prev_c = c; 77b528cefcSMark Murray printf ("%s", c->name); 78b528cefcSMark Murray } else 79b528cefcSMark Murray printf (", %s", c->name); 80b528cefcSMark Murray } 81b528cefcSMark Murray if(prev_c) 82b528cefcSMark Murray printf ("\n\t%s%s", prev_c->usage ? prev_c->usage : "", 83b528cefcSMark Murray prev_c->usage ? "\n" : ""); 84b528cefcSMark Murray } else { 85b528cefcSMark Murray c = sl_match (cmds, argv[1], 0); 86b528cefcSMark Murray if (c == NULL) 87b528cefcSMark Murray printf ("No such command: %s. " 88b528cefcSMark Murray "Try \"help\" for a list of all commands\n", 89b528cefcSMark Murray argv[1]); 90b528cefcSMark Murray else { 91b528cefcSMark Murray printf ("%s\t%s\n", c->name, c->usage); 92b528cefcSMark Murray if(c->help && *c->help) 93b528cefcSMark Murray printf ("%s\n", c->help); 94b528cefcSMark Murray if((++c)->name && c->func == NULL) { 95b528cefcSMark Murray printf ("Synonyms:"); 96b528cefcSMark Murray while (c->name && c->func == NULL) 97b528cefcSMark Murray printf ("\t%s", (c++)->name); 98b528cefcSMark Murray printf ("\n"); 99b528cefcSMark Murray } 100b528cefcSMark Murray } 101b528cefcSMark Murray } 102b528cefcSMark Murray } 103b528cefcSMark Murray 104b528cefcSMark Murray #ifdef HAVE_READLINE 105b528cefcSMark Murray 106b528cefcSMark Murray char *readline(char *prompt); 107b528cefcSMark Murray void add_history(char *p); 108b528cefcSMark Murray 109b528cefcSMark Murray #else 110b528cefcSMark Murray 111b528cefcSMark Murray static char * 112b528cefcSMark Murray readline(char *prompt) 113b528cefcSMark Murray { 114b528cefcSMark Murray char buf[BUFSIZ]; 115b528cefcSMark Murray printf ("%s", prompt); 116b528cefcSMark Murray fflush (stdout); 117b528cefcSMark Murray if(fgets(buf, sizeof(buf), stdin) == NULL) 118b528cefcSMark Murray return NULL; 119b528cefcSMark Murray if (buf[strlen(buf) - 1] == '\n') 120b528cefcSMark Murray buf[strlen(buf) - 1] = '\0'; 121b528cefcSMark Murray return strdup(buf); 122b528cefcSMark Murray } 123b528cefcSMark Murray 124b528cefcSMark Murray static void 125b528cefcSMark Murray add_history(char *p) 126b528cefcSMark Murray { 127b528cefcSMark Murray } 128b528cefcSMark Murray 129b528cefcSMark Murray #endif 130b528cefcSMark Murray 131b528cefcSMark Murray int 132b528cefcSMark Murray sl_command(SL_cmd *cmds, int argc, char **argv) 133b528cefcSMark Murray { 134b528cefcSMark Murray SL_cmd *c; 135b528cefcSMark Murray c = sl_match (cmds, argv[0], 0); 136b528cefcSMark Murray if (c == NULL) 137b528cefcSMark Murray return -1; 138b528cefcSMark Murray return (*c->func)(argc, argv); 139b528cefcSMark Murray } 140b528cefcSMark Murray 141b528cefcSMark Murray struct sl_data { 142b528cefcSMark Murray int max_count; 143b528cefcSMark Murray char **ptr; 144b528cefcSMark Murray }; 145b528cefcSMark Murray 146b528cefcSMark Murray int 147b528cefcSMark Murray sl_make_argv(char *line, int *ret_argc, char ***ret_argv) 148b528cefcSMark Murray { 149b528cefcSMark Murray char *foo = NULL; 150b528cefcSMark Murray char *p; 151b528cefcSMark Murray int argc, nargv; 152b528cefcSMark Murray char **argv; 153b528cefcSMark Murray 154b528cefcSMark Murray nargv = 10; 155b528cefcSMark Murray argv = malloc(nargv * sizeof(*argv)); 156b528cefcSMark Murray if(argv == NULL) 157b528cefcSMark Murray return ENOMEM; 158b528cefcSMark Murray argc = 0; 159b528cefcSMark Murray 160b528cefcSMark Murray for(p = strtok_r (line, " \t", &foo); 161b528cefcSMark Murray p; 162b528cefcSMark Murray p = strtok_r (NULL, " \t", &foo)) { 163b528cefcSMark Murray if(argc == nargv - 1) { 164b528cefcSMark Murray char **tmp; 165b528cefcSMark Murray nargv *= 2; 166b528cefcSMark Murray tmp = realloc (argv, nargv * sizeof(*argv)); 167b528cefcSMark Murray if (tmp == NULL) { 168b528cefcSMark Murray free(argv); 169b528cefcSMark Murray return ENOMEM; 170b528cefcSMark Murray } 171b528cefcSMark Murray argv = tmp; 172b528cefcSMark Murray } 173b528cefcSMark Murray argv[argc++] = p; 174b528cefcSMark Murray } 175b528cefcSMark Murray argv[argc] = NULL; 176b528cefcSMark Murray *ret_argc = argc; 177b528cefcSMark Murray *ret_argv = argv; 178b528cefcSMark Murray return 0; 179b528cefcSMark Murray } 180b528cefcSMark Murray 181b528cefcSMark Murray /* return values: 0 on success, -1 on fatal error, or return value of command */ 182b528cefcSMark Murray int 183b528cefcSMark Murray sl_command_loop(SL_cmd *cmds, char *prompt, void **data) 184b528cefcSMark Murray { 185b528cefcSMark Murray int ret = 0; 186b528cefcSMark Murray char *buf; 187b528cefcSMark Murray int argc; 188b528cefcSMark Murray char **argv; 189b528cefcSMark Murray 190b528cefcSMark Murray ret = 0; 191b528cefcSMark Murray buf = readline(prompt); 192b528cefcSMark Murray if(buf == NULL) 193b528cefcSMark Murray return 1; 194b528cefcSMark Murray 195b528cefcSMark Murray if(*buf) 196b528cefcSMark Murray add_history(buf); 197b528cefcSMark Murray ret = sl_make_argv(buf, &argc, &argv); 198b528cefcSMark Murray if(ret) { 199b528cefcSMark Murray fprintf(stderr, "sl_loop: out of memory\n"); 200b528cefcSMark Murray free(buf); 201b528cefcSMark Murray return -1; 202b528cefcSMark Murray } 203b528cefcSMark Murray if (argc >= 1) { 204b528cefcSMark Murray ret = sl_command(cmds, argc, argv); 205b528cefcSMark Murray if(ret == -1) { 206b528cefcSMark Murray printf ("Unrecognized command: %s\n", argv[0]); 207b528cefcSMark Murray ret = 0; 208b528cefcSMark Murray } 209b528cefcSMark Murray } 210b528cefcSMark Murray free(buf); 211b528cefcSMark Murray free(argv); 212b528cefcSMark Murray return ret; 213b528cefcSMark Murray } 214b528cefcSMark Murray 215b528cefcSMark Murray int 216b528cefcSMark Murray sl_loop(SL_cmd *cmds, char *prompt) 217b528cefcSMark Murray { 218b528cefcSMark Murray void *data = NULL; 219b528cefcSMark Murray int ret; 220b528cefcSMark Murray while((ret = sl_command_loop(cmds, prompt, &data)) == 0) 221b528cefcSMark Murray ; 222b528cefcSMark Murray return ret; 223b528cefcSMark Murray } 224