Rev 3481 | Rev 3813 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3481 | Rev 3483 | ||
---|---|---|---|
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 | #include <stdarg.h> |
49 | #include <stdarg.h> |
50 | 50 | ||
51 | #include "config.h" |
51 | #include "config.h" |
52 | #include "errors.h" |
52 | #include "errors.h" |
53 | #include "util.h" |
53 | #include "util.h" |
54 | 54 | ||
55 | extern volatile int cli_errno; |
55 | extern volatile int cli_errno; |
56 | 56 | ||
57 | /* some platforms do not have strdup, implement it here. |
57 | /* some platforms do not have strdup, implement it here. |
58 | * Returns a pointer to an allocated string or NULL on failure */ |
58 | * Returns a pointer to an allocated string or NULL on failure */ |
59 | char * cli_strdup(const char *s1) |
59 | char * cli_strdup(const char *s1) |
60 | { |
60 | { |
61 | size_t len = strlen(s1) + 1; |
61 | size_t len = strlen(s1) + 1; |
62 | void *ret = malloc(len); |
62 | void *ret = malloc(len); |
63 | 63 | ||
64 | if (ret == NULL) { |
64 | if (ret == NULL) { |
65 | cli_errno = CL_ENOMEM; |
65 | cli_errno = CL_ENOMEM; |
66 | return (char *) NULL; |
66 | return (char *) NULL; |
67 | } |
67 | } |
68 | 68 | ||
69 | cli_errno = CL_EOK; |
69 | cli_errno = CL_EOK; |
70 | return (char *) memcpy(ret, s1, len); |
70 | return (char *) memcpy(ret, s1, len); |
71 | } |
71 | } |
72 | 72 | ||
73 | /* |
- | |
74 | * Take a previously allocated string (s1), re-size it to accept s2 and copy |
- | |
75 | * the contents of s2 into s1. |
- | |
76 | * Return -1 on failure, or the length of the copied string on success. |
- | |
77 | */ |
- | |
78 | size_t cli_redup(char **s1, const char *s2) |
- | |
79 | { |
- | |
80 | size_t len; |
- | |
81 | - | ||
82 | if (s2 == NULL) |
- | |
83 | return -1; |
- | |
84 | - | ||
85 | len = strlen(s2) + 1; |
- | |
86 | - | ||
87 | *s1 = realloc(*s1, len); |
- | |
88 | - | ||
89 | if (*s1 == NULL) { |
- | |
90 | cli_errno = CL_ENOMEM; |
- | |
91 | return -1; |
- | |
92 | } |
- | |
93 | - | ||
94 | *s1[len] = '\0'; |
- | |
95 | - | ||
96 | memcpy(*s1, s2, len); |
- | |
97 | cli_errno = CL_EOK; |
- | |
98 | - | ||
99 | return len; |
- | |
100 | } |
- | |
101 | - | ||
102 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
73 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
103 | char * cli_strtok_r(char *s, const char *delim, char **last) |
74 | char * cli_strtok_r(char *s, const char *delim, char **last) |
104 | { |
75 | { |
105 | char *spanp, *tok; |
76 | char *spanp, *tok; |
106 | int c, sc; |
77 | int c, sc; |
107 | 78 | ||
108 | if (s == NULL && (s = *last) == NULL) { |
79 | if (s == NULL && (s = *last) == NULL) { |
109 | cli_errno = CL_EFAIL; |
80 | cli_errno = CL_EFAIL; |
110 | return (NULL); |
81 | return (NULL); |
111 | } |
82 | } |
112 | 83 | ||
113 | cont: |
84 | cont: |
114 | c = *s++; |
85 | c = *s++; |
115 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
86 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
116 | if (c == sc) |
87 | if (c == sc) |
117 | goto cont; |
88 | goto cont; |
118 | } |
89 | } |
119 | 90 | ||
120 | if (c == 0) { /* no non-delimiter characters */ |
91 | if (c == 0) { /* no non-delimiter characters */ |
121 | *last = NULL; |
92 | *last = NULL; |
122 | return (NULL); |
93 | return (NULL); |
123 | } |
94 | } |
124 | 95 | ||
125 | tok = s - 1; |
96 | tok = s - 1; |
126 | 97 | ||
127 | for (;;) { |
98 | for (;;) { |
128 | c = *s++; |
99 | c = *s++; |
129 | spanp = (char *)delim; |
100 | spanp = (char *)delim; |
130 | do { |
101 | do { |
131 | if ((sc = *spanp++) == c) { |
102 | if ((sc = *spanp++) == c) { |
132 | if (c == 0) |
103 | if (c == 0) |
133 | s = NULL; |
104 | s = NULL; |
134 | else |
105 | else |
135 | s[-1] = '\0'; |
106 | s[-1] = '\0'; |
136 | *last = s; |
107 | *last = s; |
137 | return (tok); |
108 | return (tok); |
138 | } |
109 | } |
139 | } while (sc != 0); |
110 | } while (sc != 0); |
140 | } |
111 | } |
141 | } |
112 | } |
142 | 113 | ||
143 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
114 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
144 | char * cli_strtok(char *s, const char *delim) |
115 | char * cli_strtok(char *s, const char *delim) |
145 | { |
116 | { |
146 | static char *last; |
117 | static char *last; |
147 | 118 | ||
148 | return (cli_strtok_r(s, delim, &last)); |
119 | return (cli_strtok_r(s, delim, &last)); |
149 | } |
120 | } |
150 | 121 | ||
151 | /* Count and return the # of elements in an array */ |
122 | /* Count and return the # of elements in an array */ |
152 | unsigned int cli_count_args(char **args) |
123 | unsigned int cli_count_args(char **args) |
153 | { |
124 | { |
154 | unsigned int i; |
125 | unsigned int i; |
155 | 126 | ||
156 | for (i=0; args[i] != NULL; i++); |
127 | for (i=0; args[i] != NULL; i++); |
157 | return i; |
128 | return i; |
158 | } |
129 | } |
159 | 130 | ||
160 | /* (re)allocates memory to store the current working directory, gets |
131 | /* (re)allocates memory to store the current working directory, gets |
161 | * and updates the current working directory, formats the prompt |
132 | * and updates the current working directory, formats the prompt |
162 | * string */ |
133 | * string */ |
163 | unsigned int cli_set_prompt(cliuser_t *usr) |
134 | unsigned int cli_set_prompt(cliuser_t *usr) |
164 | { |
135 | { |
165 | usr->prompt = (char *) realloc(usr->prompt, PATH_MAX); |
136 | usr->prompt = (char *) realloc(usr->prompt, PATH_MAX); |
166 | if (NULL == usr->prompt) { |
137 | if (NULL == usr->prompt) { |
167 | cli_error(CL_ENOMEM, "Can not allocate prompt"); |
138 | cli_error(CL_ENOMEM, "Can not allocate prompt"); |
168 | cli_errno = CL_ENOMEM; |
139 | cli_errno = CL_ENOMEM; |
169 | return 1; |
140 | return 1; |
170 | } |
141 | } |
171 | memset(usr->prompt, 0, sizeof(usr->prompt)); |
142 | memset(usr->prompt, 0, sizeof(usr->prompt)); |
172 | 143 | ||
173 | usr->cwd = (char *) realloc(usr->cwd, PATH_MAX); |
144 | usr->cwd = (char *) realloc(usr->cwd, PATH_MAX); |
174 | if (NULL == usr->cwd) { |
145 | if (NULL == usr->cwd) { |
175 | cli_error(CL_ENOMEM, "Can not allocate cwd"); |
146 | cli_error(CL_ENOMEM, "Can not allocate cwd"); |
176 | cli_errno = CL_ENOMEM; |
147 | cli_errno = CL_ENOMEM; |
177 | return 1; |
148 | return 1; |
178 | } |
149 | } |
179 | memset(usr->cwd, 0, sizeof(usr->cwd)); |
150 | memset(usr->cwd, 0, sizeof(usr->cwd)); |
180 | 151 | ||
181 | usr->cwd = getcwd(usr->cwd, PATH_MAX - 1); |
152 | usr->cwd = getcwd(usr->cwd, PATH_MAX - 1); |
182 | 153 | ||
183 | if (NULL == usr->cwd) |
154 | if (NULL == usr->cwd) |
184 | snprintf(usr->cwd, PATH_MAX, "(unknown)"); |
155 | snprintf(usr->cwd, PATH_MAX, "(unknown)"); |
185 | 156 | ||
186 | asprintf(&usr->prompt, "%s # ", usr->cwd); |
157 | asprintf(&usr->prompt, "%s # ", usr->cwd); |
187 | 158 | ||
188 | return 0; |
159 | return 0; |
189 | } |
160 | } |
190 | 161 | ||
191 | 162 | ||
192 | 163 |