FreeCalypso > hg > freecalypso-hwlab
comparison lcdtest/dispatch.c @ 12:5eaf832d57d0
lcdtest program started, skeleton compiles
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 06 Apr 2018 06:15:11 +0000 |
parents | |
children | 238a77db4134 |
comparison
equal
deleted
inserted
replaced
11:03017bfeb3ce | 12:5eaf832d57d0 |
---|---|
1 #include <ctype.h> | |
2 #include <stdio.h> | |
3 #include <string.h> | |
4 #include <strings.h> | |
5 #include <stdlib.h> | |
6 | |
7 extern int cmd_wd(); | |
8 extern int cmd_wi(); | |
9 extern int cmd_wr(); | |
10 | |
11 extern int reset_pulse(); | |
12 | |
13 cmd_exit() | |
14 { | |
15 exit(0); | |
16 } | |
17 | |
18 static struct cmdtab { | |
19 char *cmd; | |
20 int minargs; | |
21 int maxargs; | |
22 int (*func)(); | |
23 } cmdtab[] = { | |
24 {"exit", 0, 0, cmd_exit}, | |
25 {"quit", 0, 0, cmd_exit}, | |
26 {"reset", 0, 0, reset_pulse}, | |
27 {"wd", 1, 1, cmd_wd}, | |
28 {"wi", 1, 1, cmd_wi}, | |
29 {"wr", 2, 2, cmd_wr}, | |
30 {0, 0, 0, 0} | |
31 }; | |
32 | |
33 dispatch_cmd(cmd) | |
34 char *cmd; | |
35 { | |
36 char *argv[10]; | |
37 char *cp, **ap; | |
38 struct cmdtab *tp; | |
39 | |
40 for (cp = cmd; isspace(*cp); cp++) | |
41 ; | |
42 if (!*cp || *cp == '#') | |
43 return(0); | |
44 argv[0] = cp; | |
45 while (*cp && !isspace(*cp)) | |
46 cp++; | |
47 if (*cp) | |
48 *cp++ = '\0'; | |
49 for (tp = cmdtab; tp->cmd; tp++) | |
50 if (!strcmp(tp->cmd, argv[0])) | |
51 break; | |
52 if (!tp->func) { | |
53 fprintf(stderr, "error: no such command\n"); | |
54 return(-1); | |
55 } | |
56 for (ap = argv + 1; ; ) { | |
57 while (isspace(*cp)) | |
58 cp++; | |
59 if (!*cp || *cp == '#') | |
60 break; | |
61 if (ap - argv - 1 >= tp->maxargs) { | |
62 fprintf(stderr, "error: too many arguments\n"); | |
63 return(-1); | |
64 } | |
65 *ap++ = cp; | |
66 while (*cp && !isspace(*cp)) | |
67 cp++; | |
68 if (*cp) | |
69 *cp++ = '\0'; | |
70 } | |
71 if (ap - argv - 1 < tp->minargs) { | |
72 fprintf(stderr, "error: too few arguments\n"); | |
73 return(-1); | |
74 } | |
75 *ap = 0; | |
76 return tp->func(ap - argv, argv); | |
77 } |