Rev 3269 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3265 | post | 1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
2 | * Copyright (C) 1998 by Wes Peters <wes@softweyr.com> |
||
3 | * Copyright (c) 1988, 1993 The Regents of the University of California. |
||
4 | * All rights reserved by all copyright holders. |
||
5 | * |
||
6 | * Redistribution and use in source and binary forms, with or without |
||
7 | * modification, are permitted provided that the following conditions are met: |
||
8 | * |
||
9 | * Redistributions of source code must retain the above copyright notice, this |
||
10 | * list of conditions and the following disclaimer. |
||
11 | * |
||
12 | * Redistributions in binary form must reproduce the above copyright notice, |
||
13 | * this list of conditions and the following disclaimer in the documentation |
||
14 | * and/or other materials provided with the distribution. |
||
15 | * |
||
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 |
||
18 | * software without specific prior written permission. |
||
19 | * |
||
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 |
||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
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 |
||
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 |
||
30 | * POSSIBILITY OF SUCH DAMAGE. |
||
31 | */ |
||
32 | |||
33 | /* NOTES: |
||
34 | * 1 - Various functions were adapted from FreeBSD (copyright holders noted above) |
||
35 | * these functions are identified with comments. |
||
36 | * |
||
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 |
||
39 | * that allocate memory and don't automatically free it. |
||
40 | * |
||
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 :) |
||
43 | */ |
||
44 | |||
45 | #include <stdio.h> |
||
46 | #include <string.h> |
||
47 | #include <stdarg.h> |
||
48 | #include <stdlib.h> |
||
49 | |||
50 | #include "config.h" |
||
51 | #include "errors.h" |
||
52 | #include "util.h" |
||
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 | |||
88 | /* some platforms do not have strdup, implement it here */ |
||
89 | char * cli_strdup(const char *s1) |
||
90 | { |
||
91 | size_t len = strlen(s1) + 1; |
||
92 | void *ret = malloc(len); |
||
93 | |||
94 | if (ret == NULL) |
||
95 | return (char *) NULL; |
||
96 | |||
97 | return (char *) memcpy(ret, s1, len); |
||
98 | } |
||
99 | |||
100 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
||
101 | char * cli_strtok_r(char *s, const char *delim, char **last) |
||
102 | { |
||
103 | char *spanp, *tok; |
||
104 | int c, sc; |
||
105 | |||
106 | if (s == NULL && (s = *last) == NULL) |
||
107 | return (NULL); |
||
108 | |||
109 | cont: |
||
110 | c = *s++; |
||
111 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
||
112 | if (c == sc) |
||
113 | goto cont; |
||
114 | } |
||
115 | |||
116 | if (c == 0) { /* no non-delimiter characters */ |
||
117 | *last = NULL; |
||
118 | return (NULL); |
||
119 | } |
||
120 | tok = s - 1; |
||
121 | |||
122 | for (;;) { |
||
123 | c = *s++; |
||
124 | spanp = (char *)delim; |
||
125 | do { |
||
126 | if ((sc = *spanp++) == c) { |
||
127 | if (c == 0) |
||
128 | s = NULL; |
||
129 | else |
||
130 | s[-1] = '\0'; |
||
131 | *last = s; |
||
132 | return (tok); |
||
133 | } |
||
134 | } while (sc != 0); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
||
139 | char * cli_strtok(char *s, const char *delim) |
||
140 | { |
||
141 | static char *last; |
||
142 | |||
143 | return (cli_strtok_r(s, delim, &last)); |
||
144 | } |
||
145 | |||
146 | /* for debugging, not really used */ |
||
147 | void cli_free(void **pointer) |
||
148 | { |
||
149 | if (*pointer != NULL) { |
||
150 | free(*pointer); |
||
151 | *pointer = NULL; |
||
152 | } |
||
153 | } |
||
154 |