comparison target-utils/libcommon/parseargs.c @ 13:f4fc449a64ea

target-utils libcommon infrastructure for interactive commands
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 03 May 2013 06:42:03 +0000
parents
children
comparison
equal deleted inserted replaced
12:165040ce4929 13:f4fc449a64ea
1 /*
2 * This module contains the parse_args() function, which parses the "rest"
3 * part of an entered command into an argc/argv-style argument array.
4 */
5
6 parse_args(unparsed, minargs, maxargs, argv, argcp)
7 char *unparsed;
8 int minargs, maxargs;
9 char **argv;
10 int *argcp;
11 {
12 int argc;
13 char *cp;
14
15 argc = 0;
16 for (cp = unparsed; ; ) {
17 while (*cp == ' ')
18 cp++;
19 if (!*cp)
20 break;
21 if (argc >= maxargs) {
22 printf("ERROR: too many arguments\n");
23 return(-1);
24 }
25 argv[argc++] = cp;
26 while (*cp && *cp != ' ')
27 cp++;
28 if (*cp)
29 *cp++ = '\0';
30 }
31 if (argc < minargs) {
32 printf("ERROR: too few arguments\n");
33 return(-1);
34 }
35 argv[argc] = 0;
36 if (argcp)
37 *argcp = argc;
38 return(0);
39 }