FreeCalypso > hg > freecalypso-sw
comparison rvinterf/g23sh/usercmd.c @ 336:922efdd65dce
g23sh written, compiles
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Tue, 22 Apr 2014 07:21:04 +0000 |
parents | rvinterf/tmsh/usercmd.c@4c6bee98e002 |
children |
comparison
equal
deleted
inserted
replaced
335:40b8557b9d04 | 336:922efdd65dce |
---|---|
1 /* | |
2 * This module implements g23sh user command dispatch. | |
3 */ | |
4 | |
5 #include <sys/types.h> | |
6 #include <ctype.h> | |
7 #include <stdio.h> | |
8 #include <string.h> | |
9 #include <strings.h> | |
10 #include <stdlib.h> | |
11 | |
12 extern char usercmd[]; | |
13 | |
14 extern void cmd_sendsp(); | |
15 | |
16 void | |
17 cmd_exit() | |
18 { | |
19 tty_cleanup(); | |
20 exit(0); | |
21 } | |
22 | |
23 static struct cmdtab { | |
24 char *cmd; | |
25 int minargs; | |
26 int maxargs; | |
27 void (*func)(); | |
28 } cmdtab[] = { | |
29 {"exit", 0, 0, cmd_exit}, | |
30 {"quit", 0, 0, cmd_exit}, | |
31 {"sp", 2, 2, cmd_sendsp}, | |
32 {0, 0, 0, 0} | |
33 }; | |
34 | |
35 void | |
36 dispatch_user_cmd() | |
37 { | |
38 char *argv[10]; | |
39 char *cp, **ap; | |
40 struct cmdtab *tp; | |
41 | |
42 for (cp = usercmd; isspace(*cp); cp++) | |
43 ; | |
44 if (!*cp || *cp == '#') | |
45 return; | |
46 argv[0] = cp; | |
47 while (*cp && !isspace(*cp)) | |
48 cp++; | |
49 if (*cp) | |
50 *cp++ = '\0'; | |
51 for (tp = cmdtab; tp->cmd; tp++) | |
52 if (!strcmp(tp->cmd, argv[0])) | |
53 break; | |
54 if (!tp->func) { | |
55 printf("error: no such command\n"); | |
56 return; | |
57 } | |
58 for (ap = argv + 1; ; ) { | |
59 while (isspace(*cp)) | |
60 cp++; | |
61 if (!*cp || *cp == '#') | |
62 break; | |
63 if (ap - argv - 1 >= tp->maxargs) { | |
64 printf("error: too many arguments\n"); | |
65 return; | |
66 } | |
67 if (*cp == '"') { | |
68 *ap++ = ++cp; | |
69 while (*cp && *cp != '"') | |
70 cp++; | |
71 if (*cp != '"') { | |
72 printf("error: unterminated quoted string\n"); | |
73 return; | |
74 } | |
75 *cp++ = '\0'; | |
76 } else { | |
77 *ap++ = cp; | |
78 while (*cp && !isspace(*cp)) | |
79 cp++; | |
80 if (*cp) | |
81 *cp++ = '\0'; | |
82 } | |
83 } | |
84 if (ap - argv - 1 < tp->minargs) { | |
85 printf("error: too few arguments\n"); | |
86 return; | |
87 } | |
88 *ap = 0; | |
89 tp->func(ap - argv, argv); | |
90 } |