Rev 3280 | Rev 3345 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3280 | Rev 3316 | ||
---|---|---|---|
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
2 | * Copyright (C) 1998 by Wes Peters <wes@softweyr.com> |
2 | * Copyright (C) 1998 by Wes Peters <wes@softweyr.com> |
3 | * Copyright (c) 1988, 1993 The Regents of the University of California. |
3 | * Copyright (c) 1988, 1993 The Regents of the University of California. |
4 | * All rights reserved by all copyright holders. |
4 | * All rights reserved by all copyright holders. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions are met: |
7 | * modification, are permitted provided that the following conditions are met: |
8 | * |
8 | * |
9 | * Redistributions of source code must retain the above copyright notice, this |
9 | * Redistributions of source code must retain the above copyright notice, this |
10 | * list of conditions and the following disclaimer. |
10 | * list of conditions and the following disclaimer. |
11 | * |
11 | * |
12 | * Redistributions in binary form must reproduce the above copyright notice, |
12 | * Redistributions in binary form must reproduce the above copyright notice, |
13 | * this list of conditions and the following disclaimer in the documentation |
13 | * this list of conditions and the following disclaimer in the documentation |
14 | * and/or other materials provided with the distribution. |
14 | * and/or other materials provided with the distribution. |
15 | * |
15 | * |
16 | * Neither the name of the original program's authors nor the names of its |
16 | * Neither the name of the original program's authors nor the names of its |
17 | * contributors may be used to endorse or promote products derived from this |
17 | * contributors may be used to endorse or promote products derived from this |
18 | * software without specific prior written permission. |
18 | * software without specific prior written permission. |
19 | * |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
30 | * POSSIBILITY OF SUCH DAMAGE. |
30 | * POSSIBILITY OF SUCH DAMAGE. |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /* NOTES: |
33 | /* NOTES: |
34 | * 1 - Various functions were adapted from FreeBSD (copyright holders noted above) |
34 | * 1 - Various functions were adapted from FreeBSD (copyright holders noted above) |
35 | * these functions are identified with comments. |
35 | * these functions are identified with comments. |
36 | * |
36 | * |
37 | * 2 - Some of these have since appeared in libc. They remain here for various |
37 | * 2 - Some of these have since appeared in libc. They remain here for various |
38 | * reasons, such as the eventual integration of garbage collection for things |
38 | * reasons, such as the eventual integration of garbage collection for things |
39 | * that allocate memory and don't automatically free it. |
39 | * that allocate memory and don't automatically free it. |
40 | * |
40 | * |
41 | * 3 - Things that expect a pointer to an allocated string do _no_ sanity checking |
41 | * 3 - Things that expect a pointer to an allocated string do _no_ sanity checking |
42 | * if developing on a simulator with no debugger, take care :) |
42 | * if developing on a simulator with no debugger, take care :) |
43 | */ |
43 | */ |
44 | 44 | ||
45 | #include <stdio.h> |
45 | #include <stdio.h> |
46 | #include <string.h> |
46 | #include <string.h> |
47 | #include <stdarg.h> |
47 | #include <stdarg.h> |
48 | #include <stdlib.h> |
48 | #include <stdlib.h> |
49 | 49 | ||
50 | #include "config.h" |
50 | #include "config.h" |
51 | #include "errors.h" |
51 | #include "errors.h" |
52 | #include "util.h" |
52 | #include "util.h" |
53 | 53 | ||
54 | /* Functions that just have no other home */ |
- | |
55 | - | ||
56 | /* Tests if string is an --option/-option */ |
- | |
57 | unsigned int is_option(char *string) |
- | |
58 | { |
- | |
59 | size_t i; |
- | |
60 | - | ||
61 | /* We have rubbish */ |
- | |
62 | if (NULL == string) |
- | |
63 | return 0; |
- | |
64 | - | ||
65 | /* - or -- means an option (or end of options) */ |
- | |
66 | for (i=0; i < strlen(string); i++) { |
- | |
67 | if (string[i] == '-' && i <= 1) |
- | |
68 | return 1; |
- | |
69 | } |
- | |
70 | - | ||
71 | return 0; |
- | |
72 | } |
- | |
73 | - | ||
74 | /* Returns the option count in an array of arguments */ |
- | |
75 | unsigned int count_options(char *argv[]) |
- | |
76 | { |
- | |
77 | unsigned int i; |
- | |
78 | unsigned int n = 0; |
- | |
79 | - | ||
80 | for (i=0; argv[i] != NULL; i++) { |
- | |
81 | if (is_option((char *)argv[i])) |
- | |
82 | n++; |
- | |
83 | } |
- | |
84 | - | ||
85 | return n; |
- | |
86 | } |
- | |
87 | 54 | ||
88 | /* some platforms do not have strdup, implement it here */ |
55 | /* some platforms do not have strdup, implement it here */ |
89 | char * cli_strdup(const char *s1) |
56 | char * cli_strdup(const char *s1) |
90 | { |
57 | { |
91 | size_t len = strlen(s1) + 1; |
58 | size_t len = strlen(s1) + 1; |
92 | void *ret = malloc(len); |
59 | void *ret = malloc(len); |
93 | 60 | ||
94 | if (ret == NULL) |
61 | if (ret == NULL) |
95 | return (char *) NULL; |
62 | return (char *) NULL; |
96 | 63 | ||
97 | return (char *) memcpy(ret, s1, len); |
64 | return (char *) memcpy(ret, s1, len); |
98 | } |
65 | } |
99 | 66 | ||
100 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
67 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
101 | char * cli_strtok_r(char *s, const char *delim, char **last) |
68 | char * cli_strtok_r(char *s, const char *delim, char **last) |
102 | { |
69 | { |
103 | char *spanp, *tok; |
70 | char *spanp, *tok; |
104 | int c, sc; |
71 | int c, sc; |
105 | 72 | ||
106 | if (s == NULL && (s = *last) == NULL) |
73 | if (s == NULL && (s = *last) == NULL) |
107 | return (NULL); |
74 | return (NULL); |
108 | 75 | ||
109 | cont: |
76 | cont: |
110 | c = *s++; |
77 | c = *s++; |
111 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
78 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
112 | if (c == sc) |
79 | if (c == sc) |
113 | goto cont; |
80 | goto cont; |
114 | } |
81 | } |
115 | 82 | ||
116 | if (c == 0) { /* no non-delimiter characters */ |
83 | if (c == 0) { /* no non-delimiter characters */ |
117 | *last = NULL; |
84 | *last = NULL; |
118 | return (NULL); |
85 | return (NULL); |
119 | } |
86 | } |
120 | 87 | ||
121 | tok = s - 1; |
88 | tok = s - 1; |
122 | 89 | ||
123 | for (;;) { |
90 | for (;;) { |
124 | c = *s++; |
91 | c = *s++; |
125 | spanp = (char *)delim; |
92 | spanp = (char *)delim; |
126 | do { |
93 | do { |
127 | if ((sc = *spanp++) == c) { |
94 | if ((sc = *spanp++) == c) { |
128 | if (c == 0) |
95 | if (c == 0) |
129 | s = NULL; |
96 | s = NULL; |
130 | else |
97 | else |
131 | s[-1] = '\0'; |
98 | s[-1] = '\0'; |
132 | *last = s; |
99 | *last = s; |
133 | return (tok); |
100 | return (tok); |
134 | } |
101 | } |
135 | } while (sc != 0); |
102 | } while (sc != 0); |
136 | } |
103 | } |
137 | } |
104 | } |
138 | 105 | ||
139 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
106 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
140 | char * cli_strtok(char *s, const char *delim) |
107 | char * cli_strtok(char *s, const char *delim) |
141 | { |
108 | { |
142 | static char *last; |
109 | static char *last; |
143 | 110 | ||
144 | return (cli_strtok_r(s, delim, &last)); |
111 | return (cli_strtok_r(s, delim, &last)); |
145 | } |
112 | } |
146 | - | ||
147 | - | ||
148 | 113 |