FreeCalypso > hg > fc-sim-tools
comparison libcommon/dispatch.c @ 14:b7ee2e85686b
command dispatch and scripting factored out into libcommon
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sun, 14 Mar 2021 07:34:35 +0000 |
| parents | simtool/dispatch.c@ddd767f6e15b |
| children |
comparison
equal
deleted
inserted
replaced
| 13:7c26eac6ab00 | 14:b7ee2e85686b |
|---|---|
| 1 /* | |
| 2 * This module implements common command dispatch for our SIM tools. | |
| 3 */ | |
| 4 | |
| 5 #include <ctype.h> | |
| 6 #include <stdio.h> | |
| 7 #include <string.h> | |
| 8 #include <strings.h> | |
| 9 #include <stdlib.h> | |
| 10 #include "cmdtab.h" | |
| 11 | |
| 12 extern struct cmdtab cmdtab[]; | |
| 13 | |
| 14 static FILE * | |
| 15 handle_output_redir(str) | |
| 16 char *str; | |
| 17 { | |
| 18 char *cp, *fn; | |
| 19 FILE *outf; | |
| 20 | |
| 21 for (cp = str; isspace(*cp); cp++) | |
| 22 ; | |
| 23 if (!*cp || *cp == '#') { | |
| 24 fprintf(stderr, "error: no filename after '>'\n"); | |
| 25 return(0); | |
| 26 } | |
| 27 for (fn = cp; *cp && !isspace(*cp); cp++) | |
| 28 ; | |
| 29 if (*cp) | |
| 30 *cp++ = '\0'; | |
| 31 while (isspace(*cp)) | |
| 32 cp++; | |
| 33 if (*cp && *cp != '#') { | |
| 34 fprintf(stderr, "error: invalid syntax after '>'\n"); | |
| 35 return(0); | |
| 36 } | |
| 37 outf = fopen(fn, "w"); | |
| 38 if (!outf) | |
| 39 perror(fn); | |
| 40 return outf; | |
| 41 } | |
| 42 | |
| 43 simtool_dispatch_cmd(cmd, is_script) | |
| 44 char *cmd; | |
| 45 { | |
| 46 char *argv[20]; | |
| 47 char *cp, **ap; | |
| 48 struct cmdtab *tp; | |
| 49 FILE *outf; | |
| 50 int rc; | |
| 51 | |
| 52 for (cp = cmd; isspace(*cp); cp++) | |
| 53 ; | |
| 54 if (!*cp || *cp == '#') | |
| 55 return(0); | |
| 56 if (is_script) | |
| 57 printf("Script command: %s\n", cp); | |
| 58 if (*cp == '!') | |
| 59 return system(cp + 1); | |
| 60 argv[0] = cp; | |
| 61 while (*cp && !isspace(*cp)) | |
| 62 cp++; | |
| 63 if (*cp) | |
| 64 *cp++ = '\0'; | |
| 65 for (tp = cmdtab; tp->cmd; tp++) | |
| 66 if (!strcmp(tp->cmd, argv[0])) | |
| 67 break; | |
| 68 if (!tp->func) { | |
| 69 fprintf(stderr, "error: no such command\n"); | |
| 70 return(-1); | |
| 71 } | |
| 72 for (ap = argv + 1; ; ) { | |
| 73 while (isspace(*cp)) | |
| 74 cp++; | |
| 75 if (!*cp || *cp == '#' || *cp == '>') | |
| 76 break; | |
| 77 if (ap - argv - 1 >= tp->maxargs) { | |
| 78 fprintf(stderr, "error: too many arguments\n"); | |
| 79 return(-1); | |
| 80 } | |
| 81 if (*cp == '"') { | |
| 82 *ap++ = ++cp; | |
| 83 for (;;) { | |
| 84 if (!*cp) { | |
| 85 unterm_qstring: fprintf(stderr, | |
| 86 "error: unterminated quoted string\n"); | |
| 87 return(-1); | |
| 88 } | |
| 89 if (*cp == '"') | |
| 90 break; | |
| 91 if (*cp++ == '\\') { | |
| 92 if (!*cp) | |
| 93 goto unterm_qstring; | |
| 94 cp++; | |
| 95 } | |
| 96 } | |
| 97 *cp++ = '\0'; | |
| 98 } else { | |
| 99 *ap++ = cp; | |
| 100 while (*cp && !isspace(*cp)) | |
| 101 cp++; | |
| 102 if (*cp) | |
| 103 *cp++ = '\0'; | |
| 104 } | |
| 105 } | |
| 106 if (ap - argv - 1 < tp->minargs) { | |
| 107 fprintf(stderr, "error: too few arguments\n"); | |
| 108 return(-1); | |
| 109 } | |
| 110 *ap = 0; | |
| 111 if (*cp == '>') { | |
| 112 if (!tp->allow_redir) { | |
| 113 fprintf(stderr, | |
| 114 "error: command does not support output redirection\n"); | |
| 115 return(-1); | |
| 116 } | |
| 117 outf = handle_output_redir(cp + 1); | |
| 118 if (!outf) | |
| 119 return(-1); | |
| 120 } else | |
| 121 outf = stdout; | |
| 122 rc = tp->func(ap - argv, argv, outf); | |
| 123 if (outf != stdout) | |
| 124 fclose(outf); | |
| 125 return rc; | |
| 126 } | |
| 127 | |
| 128 dispatch_ready_argv(argc, argv) | |
| 129 char **argv; | |
| 130 { | |
| 131 struct cmdtab *tp; | |
| 132 | |
| 133 for (tp = cmdtab; tp->cmd; tp++) | |
| 134 if (!strcmp(tp->cmd, argv[0])) | |
| 135 break; | |
| 136 if (!tp->func) { | |
| 137 fprintf(stderr, "error: no such command\n"); | |
| 138 return(-1); | |
| 139 } | |
| 140 if (argc - 1 > tp->maxargs) { | |
| 141 fprintf(stderr, "error: too many arguments\n"); | |
| 142 return(-1); | |
| 143 } | |
| 144 if (argc - 1 < tp->minargs) { | |
| 145 fprintf(stderr, "error: too few arguments\n"); | |
| 146 return(-1); | |
| 147 } | |
| 148 return tp->func(argc, argv, stdout); | |
| 149 } |
