Subversion Repositories HelenOS

Rev

Rev 3376 | Rev 3420 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3376 Rev 3377
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
/*
73
/*
74
 * Take a previously allocated string (s1), re-size it to accept s2 and copy
74
 * Take a previously allocated string (s1), re-size it to accept s2 and copy
75
 * the contents of s2 into s1.
75
 * the contents of s2 into s1.
76
 * Return -1 on failure, or the length of the copied string on success.
76
 * Return -1 on failure, or the length of the copied string on success.
77
 */
77
 */
78
int cli_redup(char **s1, const char *s2)
78
int cli_redup(char **s1, const char *s2)
79
{
79
{
80
    size_t len = strlen(s2) + 1;
80
    size_t len = strlen(s2) + 1;
81
 
81
 
82
    if (! len)
82
    if (! len)
83
        return -1;
83
        return -1;
84
 
84
 
85
    *s1 = realloc(*s1, len);
85
    *s1 = realloc(*s1, len);
86
 
86
 
87
    if (*s1 == NULL) {
87
    if (*s1 == NULL) {
88
        cli_errno = CL_ENOMEM;
88
        cli_errno = CL_ENOMEM;
89
        return -1;
89
        return -1;
90
    }
90
    }
91
 
91
 
92
    memset(*s1, 0, sizeof(*s1));
92
    memset(*s1, 0, sizeof(*s1));
93
    memcpy(*s1, s2, len);
93
    memcpy(*s1, s2, len);
94
    cli_errno = CL_EOK;
94
    cli_errno = CL_EOK;
95
    return (int) len;
95
    return (int) len;
96
}
96
}
97
 
97
 
98
/* An asprintf() for formatting paths, similar to asprintf() but ensures
98
/* An asprintf() for formatting paths, similar to asprintf() but ensures
99
 * the returned allocated string is <= PATH_MAX. On failure, an attempt
99
 * the returned allocated string is <= PATH_MAX. On failure, an attempt
100
 * is made to return the original string (if not null) unmodified.
100
 * is made to return the original string (if not null) unmodified.
101
 *
101
 *
102
 * Returns: Length of the new string on success, 0 if the string was handed
102
 * Returns: Length of the new string on success, 0 if the string was handed
103
 * back unmodified, -1 on failure. On failure, cli_errno is set.
103
 * back unmodified, -1 on failure. On failure, cli_errno is set.
104
 *
104
 *
105
 * We do not use POSIX_PATH_MAX, as it is typically much smaller than the
105
 * We do not use POSIX_PATH_MAX, as it is typically much smaller than the
106
 * PATH_MAX defined by the kernel.
106
 * PATH_MAX defined by the kernel.
107
 *
107
 *
108
 * Use this like:
108
 * Use this like:
109
 * if (1 > cli_psprintf(&char, "%s/%s", foo, bar)) {
109
 * if (1 > cli_psprintf(&char, "%s/%s", foo, bar)) {
110
 *   cli_error(cli_errno, "Failed to format path");
110
 *   cli_error(cli_errno, "Failed to format path");
111
 *   stop_what_your_doing_as_your_out_of_memory();
111
 *   stop_what_your_doing_as_your_out_of_memory();
112
 * }
112
 * }
113
 */
113
 */
114
 
114
 
115
int cli_psprintf(char **s1, const char *fmt, ...)
115
int cli_psprintf(char **s1, const char *fmt, ...)
116
{
116
{
117
    va_list ap;
117
    va_list ap;
118
    size_t needed, base = PATH_MAX + 1;
118
    size_t needed, base = PATH_MAX + 1;
119
    int skipped = 0;
119
    int skipped = 0;
120
    char *orig = NULL;
120
    char *orig = NULL;
121
    char *tmp = (char *) malloc(base);
121
    char *tmp = (char *) malloc(base);
122
 
122
 
123
    /* Don't even touch s1, not enough memory */
123
    /* Don't even touch s1, not enough memory */
124
    if (NULL == tmp) {
124
    if (NULL == tmp) {
125
        cli_errno = CL_ENOMEM;
125
        cli_errno = CL_ENOMEM;
126
        return -1;
126
        return -1;
127
    }
127
    }
128
 
128
 
129
    /* If re-allocating s1, save a copy in case we fail */
129
    /* If re-allocating s1, save a copy in case we fail */
130
    if (NULL != *s1)
130
    if (NULL != *s1)
131
        orig = cli_strdup(*s1);
131
        orig = cli_strdup(*s1);
132
 
132
 
133
    /* Print the string to tmp so we can determine the size that
133
    /* Print the string to tmp so we can determine the size that
134
     * we actually need */
134
     * we actually need */
135
    memset(tmp, 0, sizeof(tmp));
135
    memset(tmp, 0, sizeof(tmp));
136
    va_start(ap, fmt);
136
    va_start(ap, fmt);
137
    /* vsnprintf will return the # of bytes not written */
137
    /* vsnprintf will return the # of bytes not written */
138
    skipped = vsnprintf(tmp, base, fmt, ap);
138
    skipped = vsnprintf(tmp, base, fmt, ap);
139
    va_end(ap);
139
    va_end(ap);
140
 
140
 
141
    /* realloc/alloc s1 to be just the size that we need */
141
    /* realloc/alloc s1 to be just the size that we need */
142
    needed = strlen(tmp) + 1;
142
    needed = strlen(tmp) + 1;
143
    *s1 = realloc(*s1, needed);
143
    *s1 = realloc(*s1, needed);
144
 
144
 
145
    if (NULL == *s1) {
145
    if (NULL == *s1) {
146
        /* No string lived here previously, or we failed to
146
        /* No string lived here previously, or we failed to
147
         * make a copy of it, either way there's nothing we
147
         * make a copy of it, either way there's nothing we
148
         * can do. */
148
         * can do. */
149
        if (NULL == *orig) {
149
        if (NULL == *orig) {
150
            cli_errno = CL_ENOMEM;
150
            cli_errno = CL_ENOMEM;
151
            return -1;
151
            return -1;
152
        }
152
        }
153
        /* We can't even allocate enough size to restore the
153
        /* We can't even allocate enough size to restore the
154
         * saved copy, just give up */
154
         * saved copy, just give up */
155
        *s1 = realloc(*s1, strlen(orig) + 1);
155
        *s1 = realloc(*s1, strlen(orig) + 1);
156
        if (NULL == *s1) {
156
        if (NULL == *s1) {
157
            free(tmp);
157
            free(tmp);
158
            free(orig);
158
            free(orig);
159
            cli_errno = CL_ENOMEM;
159
            cli_errno = CL_ENOMEM;
160
            return -1;
160
            return -1;
161
        }
161
        }
162
        /* Give the string back as we found it */
162
        /* Give the string back as we found it */
163
        memset(*s1, 0, sizeof(*s1));
163
        memset(*s1, 0, sizeof(*s1));
164
        memcpy(*s1, orig, strlen(orig) + 1);
164
        memcpy(*s1, orig, strlen(orig) + 1);
165
        free(tmp);
165
        free(tmp);
166
        free(orig);
166
        free(orig);
167
        cli_errno = CL_ENOMEM;
167
        cli_errno = CL_ENOMEM;
168
        return 0;
168
        return 0;
169
    }
169
    }
170
 
170
 
171
    /* Ok, great, we have enough room */
171
    /* Ok, great, we have enough room */
172
    memset(*s1, 0, sizeof(*s1));
172
    memset(*s1, 0, sizeof(*s1));
173
    memcpy(*s1, tmp, needed);
173
    memcpy(*s1, tmp, needed);
174
    free(tmp);
174
    free(tmp);
175
 
175
 
176
    /* Free tmp only if s1 was reallocated instead of allocated */
176
    /* Free tmp only if s1 was reallocated instead of allocated */
177
    if (NULL != orig)
177
    if (NULL != orig)
178
        free(orig);
178
        free(orig);
179
 
179
 
180
    if (skipped) {
180
    if (skipped) {
181
        /* s1 was bigger than PATH_MAX when expanded, however part
181
        /* s1 was bigger than PATH_MAX when expanded, however part
182
         * of the string was printed. Tell the caller not to use it */
182
         * of the string was printed. Tell the caller not to use it */
183
        cli_errno = CL_ETOOBIG;
183
        cli_errno = CL_ETOOBIG;
184
        return -1;
184
        return -1;
185
    }
185
    }
186
 
186
 
187
    /* Success! */
187
    /* Success! */
188
    cli_errno = CL_EOK;
188
    cli_errno = CL_EOK;
189
    return (int) needed;
189
    return (int) needed;
190
}
190
}
191
   
191
   
192
/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
192
/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
193
char * cli_strtok_r(char *s, const char *delim, char **last)
193
char * cli_strtok_r(char *s, const char *delim, char **last)
194
{
194
{
195
    char *spanp, *tok;
195
    char *spanp, *tok;
196
    int c, sc;
196
    int c, sc;
197
 
197
 
198
    if (s == NULL && (s = *last) == NULL) {
198
    if (s == NULL && (s = *last) == NULL) {
199
        cli_errno = CL_EFAIL;
199
        cli_errno = CL_EFAIL;
200
        return (NULL);
200
        return (NULL);
201
    }
201
    }
202
 
202
 
203
cont:
203
cont:
204
    c = *s++;
204
    c = *s++;
205
    for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
205
    for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
206
        if (c == sc)
206
        if (c == sc)
207
            goto cont;
207
            goto cont;
208
    }
208
    }
209
 
209
 
210
    if (c == 0) {       /* no non-delimiter characters */
210
    if (c == 0) {       /* no non-delimiter characters */
211
        *last = NULL;
211
        *last = NULL;
212
        return (NULL);
212
        return (NULL);
213
    }
213
    }
214
 
214
 
215
    tok = s - 1;
215
    tok = s - 1;
216
 
216
 
217
    for (;;) {
217
    for (;;) {
218
        c = *s++;
218
        c = *s++;
219
        spanp = (char *)delim;
219
        spanp = (char *)delim;
220
        do {
220
        do {
221
            if ((sc = *spanp++) == c) {
221
            if ((sc = *spanp++) == c) {
222
                if (c == 0)
222
                if (c == 0)
223
                    s = NULL;
223
                    s = NULL;
224
                else
224
                else
225
                    s[-1] = '\0';
225
                    s[-1] = '\0';
226
                *last = s;
226
                *last = s;
227
                return (tok);
227
                return (tok);
228
            }
228
            }
229
        } while (sc != 0);
229
        } while (sc != 0);
230
    }
230
    }
231
}
231
}
232
 
232
 
233
/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
233
/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
234
char * cli_strtok(char *s, const char *delim)
234
char * cli_strtok(char *s, const char *delim)
235
{
235
{
236
    static char *last;
236
    static char *last;
237
 
237
 
238
    return (cli_strtok_r(s, delim, &last));
238
    return (cli_strtok_r(s, delim, &last));
239
}
239
}
240
 
240
 
241
/* Count and return the # of elements in an array */
241
/* Count and return the # of elements in an array */
242
unsigned int cli_count_args(char **args)
242
unsigned int cli_count_args(char **args)
243
{
243
{
244
    unsigned int i;
244
    unsigned int i;
245
 
245
 
246
    for (i=0; args[i] != NULL; i++);
246
    for (i=0; args[i] != NULL; i++);
247
    return i;
247
    return i;
248
}
248
}
249
 
249
 
-
 
250
/* (re)allocates memory to store the current working directory, gets
-
 
251
 * and updates the current working directory, formats the prompt
-
 
252
 * string */
-
 
253
unsigned int cli_set_prompt(cliuser_t *usr)
-
 
254
{
-
 
255
    usr->prompt = (char *) realloc(usr->prompt, PATH_MAX);
-
 
256
    if (NULL == usr->prompt) {
-
 
257
        cli_error(CL_ENOMEM, "Can not allocate prompt");
-
 
258
        cli_errno = CL_ENOMEM;
-
 
259
        return 1;
-
 
260
    }
-
 
261
    memset(usr->prompt, 0, sizeof(usr->prompt));
-
 
262
 
-
 
263
    usr->cwd = (char *) realloc(usr->cwd, PATH_MAX);
-
 
264
    if (NULL == usr->cwd) {
-
 
265
        cli_error(CL_ENOMEM, "Can not allocate cwd");
-
 
266
        cli_errno = CL_ENOMEM;
-
 
267
        return 1;
-
 
268
    }
-
 
269
    memset(usr->cwd, 0, sizeof(usr->cwd));
-
 
270
 
-
 
271
    usr->cwd = getcwd(usr->cwd, PATH_MAX - 1);
-
 
272
 
-
 
273
    if (NULL == usr->cwd)
-
 
274
        snprintf(usr->cwd, PATH_MAX, "(unknown)");
-
 
275
 
-
 
276
    if (1 < cli_psprintf(&usr->prompt, "%s # ", usr->cwd)) {
-
 
277
        cli_error(cli_errno, "Failed to set prompt");
-
 
278
        return 1;
-
 
279
    }
-
 
280
 
-
 
281
    return 0;
-
 
282
}
-
 
283
 
-
 
284
 
250
 
285