annotate rvinterf/asyncshell/parse.c @ 1027:1178befeda76

fc-shell: tch command framework started
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 30 May 2016 23:19:30 +0000
parents 6eee1e547778
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1013
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module implements the parser helper function that allows
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
3 * the same code to be reused between interactive and one-shot
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
4 * versions of the same command.
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
5 */
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
6
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <stdio.h>
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <ctype.h>
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <string.h>
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <strings.h>
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <stdlib.h>
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
12
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
13 parse_interactive_command_into_argv(argstr, argv, min_arg, max_arg, argcp)
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
14 char *argstr, **argv;
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
15 int min_arg, max_arg, *argcp;
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
16 {
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
17 char *cp, **ap;
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
18
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
19 cp = argstr;
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
20 for (ap = argv; ; ) {
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
21 while (isspace(*cp))
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
22 cp++;
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
23 if (!*cp || *cp == '#')
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
24 break;
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
25 if (ap - argv >= max_arg) {
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
26 printf("error: too many arguments\n");
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
27 return(-1);
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
28 }
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
29 if (*cp == '"') {
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
30 *ap++ = ++cp;
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
31 while (*cp && *cp != '"')
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
32 cp++;
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
33 if (*cp != '"') {
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
34 printf("error: unterminated quoted string\n");
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
35 return(-1);
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
36 }
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
37 *cp++ = '\0';
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
38 } else {
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
39 *ap++ = cp;
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
40 while (*cp && !isspace(*cp))
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
41 cp++;
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
42 if (*cp)
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
43 *cp++ = '\0';
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
44 }
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
45 }
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
46 if (ap - argv < min_arg) {
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
47 printf("error: too few arguments\n");
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
48 return(-1);
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
49 }
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
50 *ap = 0;
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
51 *argcp = ap - argv;
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
52 return(0);
6eee1e547778 fc-shell: arbitrary send command implemented in interactive mode
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
53 }