Subversion Repositories HelenOS

Rev

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

Rev 3483 Rev 3813
1
/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
1
/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> - All rights reserved
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
 *
2
 *
6
 * Redistribution and use in source and binary forms, with or without
3
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
4
 * modification, are permitted provided that the following conditions are met:
8
 *
5
 *
9
 * Redistributions of source code must retain the above copyright notice, this
6
 * Redistributions of source code must retain the above copyright notice, this
10
 * list of conditions and the following disclaimer.
7
 * list of conditions and the following disclaimer.
11
 *
8
 *
12
 * Redistributions in binary form must reproduce the above copyright notice,
9
 * Redistributions in binary form must reproduce the above copyright notice,
13
 * this list of conditions and the following disclaimer in the documentation
10
 * this list of conditions and the following disclaimer in the documentation
14
 * and/or other materials provided with the distribution.
11
 * and/or other materials provided with the distribution.
15
 *
12
 *
16
 * Neither the name of the original program's authors nor the names of its
13
 * 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
14
 * contributors may be used to endorse or promote products derived from this
18
 * software without specific prior written permission.
15
 * software without specific prior written permission.
19
 *
16
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
 * 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
18
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
 * 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
26
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
 * POSSIBILITY OF SUCH DAMAGE.
27
 * POSSIBILITY OF SUCH DAMAGE.
31
 */
28
 */
32
 
29
 
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>
30
#include <stdio.h>
46
#include <string.h>
31
#include <string.h>
47
#include <stdarg.h>
32
#include <stdarg.h>
48
#include <stdlib.h>
33
#include <stdlib.h>
49
#include <stdarg.h>
34
#include <stdarg.h>
50
 
35
 
51
#include "config.h"
36
#include "config.h"
52
#include "errors.h"
37
#include "errors.h"
53
#include "util.h"
38
#include "util.h"
54
 
39
 
55
extern volatile int cli_errno;
40
extern volatile int cli_errno;
56
 
41
 
57
/* some platforms do not have strdup, implement it here.
-
 
58
 * Returns a pointer to an allocated string or NULL on failure */
-
 
59
char * cli_strdup(const char *s1)
-
 
60
{
-
 
61
    size_t len = strlen(s1) + 1;
-
 
62
    void *ret = malloc(len);
-
 
63
 
-
 
64
    if (ret == NULL) {
-
 
65
        cli_errno = CL_ENOMEM;
-
 
66
        return (char *) NULL;
-
 
67
    }
-
 
68
 
-
 
69
    cli_errno = CL_EOK;
-
 
70
    return (char *) memcpy(ret, s1, len);
-
 
71
}
-
 
72
 
-
 
73
/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
-
 
74
char * cli_strtok_r(char *s, const char *delim, char **last)
-
 
75
{
-
 
76
    char *spanp, *tok;
-
 
77
    int c, sc;
-
 
78
 
-
 
79
    if (s == NULL && (s = *last) == NULL) {
-
 
80
        cli_errno = CL_EFAIL;
-
 
81
        return (NULL);
-
 
82
    }
-
 
83
 
-
 
84
cont:
-
 
85
    c = *s++;
-
 
86
    for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
-
 
87
        if (c == sc)
-
 
88
            goto cont;
-
 
89
    }
-
 
90
 
-
 
91
    if (c == 0) {       /* no non-delimiter characters */
-
 
92
        *last = NULL;
-
 
93
        return (NULL);
-
 
94
    }
-
 
95
 
-
 
96
    tok = s - 1;
-
 
97
 
-
 
98
    for (;;) {
-
 
99
        c = *s++;
-
 
100
        spanp = (char *)delim;
-
 
101
        do {
-
 
102
            if ((sc = *spanp++) == c) {
-
 
103
                if (c == 0)
-
 
104
                    s = NULL;
-
 
105
                else
-
 
106
                    s[-1] = '\0';
-
 
107
                *last = s;
-
 
108
                return (tok);
-
 
109
            }
-
 
110
        } while (sc != 0);
-
 
111
    }
-
 
112
}
-
 
113
 
-
 
114
/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
-
 
115
char * cli_strtok(char *s, const char *delim)
-
 
116
{
-
 
117
    static char *last;
-
 
118
 
-
 
119
    return (cli_strtok_r(s, delim, &last));
-
 
120
}
-
 
121
 
-
 
122
/* Count and return the # of elements in an array */
42
/* Count and return the # of elements in an array */
123
unsigned int cli_count_args(char **args)
43
unsigned int cli_count_args(char **args)
124
{
44
{
125
    unsigned int i;
45
    unsigned int i;
126
 
46
 
127
    for (i=0; args[i] != NULL; i++);
47
    for (i=0; args[i] != NULL; i++);
128
    return i;
48
    return i;
129
}
49
}
130
 
50
 
131
/* (re)allocates memory to store the current working directory, gets
51
/* (re)allocates memory to store the current working directory, gets
132
 * and updates the current working directory, formats the prompt
52
 * and updates the current working directory, formats the prompt
133
 * string */
53
 * string */
134
unsigned int cli_set_prompt(cliuser_t *usr)
54
unsigned int cli_set_prompt(cliuser_t *usr)
135
{
55
{
136
    usr->prompt = (char *) realloc(usr->prompt, PATH_MAX);
56
    usr->prompt = (char *) realloc(usr->prompt, PATH_MAX);
137
    if (NULL == usr->prompt) {
57
    if (NULL == usr->prompt) {
138
        cli_error(CL_ENOMEM, "Can not allocate prompt");
58
        cli_error(CL_ENOMEM, "Can not allocate prompt");
139
        cli_errno = CL_ENOMEM;
59
        cli_errno = CL_ENOMEM;
140
        return 1;
60
        return 1;
141
    }
61
    }
142
    memset(usr->prompt, 0, sizeof(usr->prompt));
62
    memset(usr->prompt, 0, sizeof(usr->prompt));
143
 
63
 
144
    usr->cwd = (char *) realloc(usr->cwd, PATH_MAX);
64
    usr->cwd = (char *) realloc(usr->cwd, PATH_MAX);
145
    if (NULL == usr->cwd) {
65
    if (NULL == usr->cwd) {
146
        cli_error(CL_ENOMEM, "Can not allocate cwd");
66
        cli_error(CL_ENOMEM, "Can not allocate cwd");
147
        cli_errno = CL_ENOMEM;
67
        cli_errno = CL_ENOMEM;
148
        return 1;
68
        return 1;
149
    }
69
    }
150
    memset(usr->cwd, 0, sizeof(usr->cwd));
70
    memset(usr->cwd, 0, sizeof(usr->cwd));
151
 
71
 
152
    usr->cwd = getcwd(usr->cwd, PATH_MAX - 1);
72
    usr->cwd = getcwd(usr->cwd, PATH_MAX - 1);
153
 
73
 
154
    if (NULL == usr->cwd)
74
    if (NULL == usr->cwd)
155
        snprintf(usr->cwd, PATH_MAX, "(unknown)");
75
        snprintf(usr->cwd, PATH_MAX, "(unknown)");
156
 
76
 
157
    asprintf(&usr->prompt, "%s # ", usr->cwd);
77
    asprintf(&usr->prompt, "%s # ", usr->cwd);
158
 
78
 
159
    return 0;
79
    return 0;
160
}
80
}
161
 
81
 
162
 
82
 
163
 
83