Rev 3275 | Rev 3341 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3275 | Rev 3304 | ||
---|---|---|---|
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
2 | * All rights reserved. |
2 | * All rights reserved. |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are met: |
5 | * modification, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * Redistributions of source code must retain the above copyright notice, this |
7 | * Redistributions of source code must retain the above copyright notice, this |
8 | * list of conditions and the following disclaimer. |
8 | * list of conditions and the following disclaimer. |
9 | * |
9 | * |
10 | * Redistributions in binary form must reproduce the above copyright notice, |
10 | * Redistributions in binary form must reproduce the above copyright notice, |
11 | * this list of conditions and the following disclaimer in the documentation |
11 | * this list of conditions and the following disclaimer in the documentation |
12 | * and/or other materials provided with the distribution. |
12 | * and/or other materials provided with the distribution. |
13 | * |
13 | * |
14 | * Neither the name of the original program's authors nor the names of its |
14 | * Neither the name of the original program's authors nor the names of its |
15 | * contributors may be used to endorse or promote products derived from this |
15 | * contributors may be used to endorse or promote products derived from this |
16 | * software without specific prior written permission. |
16 | * software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | * POSSIBILITY OF SUCH DAMAGE. |
28 | * POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
29 | */ |
30 | 30 | ||
31 | #include <stdio.h> |
31 | #include <stdio.h> |
32 | #include <stdlib.h> |
32 | #include <stdlib.h> |
33 | #include <string.h> |
33 | #include <string.h> |
34 | #include <unistd.h> |
34 | #include <unistd.h> |
35 | #include "config.h" |
35 | #include "config.h" |
36 | #include "scli.h" |
36 | #include "scli.h" |
37 | #include "input.h" |
37 | #include "input.h" |
38 | #include "util.h" |
38 | #include "util.h" |
39 | #include "errors.h" |
39 | #include "errors.h" |
40 | #include "cmds/cmds.h" |
40 | #include "cmds/cmds.h" |
41 | 41 | ||
42 | #ifndef HELENOS |
- | |
43 | #include <linux/limits.h> |
- | |
44 | #endif |
- | |
45 | - | ||
46 | /* See scli.h */ |
42 | /* See scli.h */ |
47 | static cliuser_t usr; |
43 | static cliuser_t usr; |
48 | 44 | ||
49 | /* Modified by the 'quit' module, which is compiled before this */ |
45 | /* Modified by the 'quit' module, which is compiled before this */ |
50 | extern unsigned int cli_quit; |
46 | extern unsigned int cli_quit; |
51 | 47 | ||
52 | /* Globals that are modified during start-up that modules/builtins should |
48 | /* Globals that are modified during start-up that modules/builtins should |
53 | * be aware of. */ |
49 | * be aware of. */ |
54 | volatile unsigned int cli_interactive = 1; |
50 | volatile unsigned int cli_interactive = 1; |
55 | volatile unsigned int cli_verbocity = 1; |
51 | volatile unsigned int cli_verbocity = 1; |
56 | 52 | ||
57 | /* The official name of this program |
53 | /* The official name of this program |
58 | * (change to your liking in configure.ac and re-run autoconf) */ |
54 | * (change to your liking in configure.ac and re-run autoconf) */ |
59 | const char *progname = PACKAGE_NAME; |
55 | const char *progname = PACKAGE_NAME; |
60 | 56 | ||
61 | /* (re)allocates memory to store the current working directory, gets |
57 | /* (re)allocates memory to store the current working directory, gets |
62 | * and updates the current working directory, formats the prompt string */ |
58 | * and updates the current working directory, formats the prompt string */ |
63 | int cli_set_prompt(cliuser_t *usr) |
59 | int cli_set_prompt(cliuser_t *usr) |
64 | { |
60 | { |
65 | usr->prompt = (char *) realloc(usr->prompt, PATH_MAX); |
61 | usr->prompt = (char *) realloc(usr->prompt, PATH_MAX); |
66 | if (NULL == usr->prompt) { |
62 | if (NULL == usr->prompt) { |
67 | cli_error(CL_ENOMEM, "Can not allocate prompt"); |
63 | cli_error(CL_ENOMEM, "Can not allocate prompt"); |
68 | return 1; |
64 | return 1; |
69 | } |
65 | } |
70 | memset(usr->prompt, 0, sizeof(usr->prompt)); |
66 | memset(usr->prompt, 0, sizeof(usr->prompt)); |
71 | 67 | ||
72 | usr->cwd = (char *) realloc(usr->cwd, PATH_MAX); |
68 | usr->cwd = (char *) realloc(usr->cwd, PATH_MAX); |
73 | if (NULL == usr->cwd) { |
69 | if (NULL == usr->cwd) { |
74 | cli_error(CL_ENOMEM, "Can not allocate cwd"); |
70 | cli_error(CL_ENOMEM, "Can not allocate cwd"); |
75 | return 1; |
71 | return 1; |
76 | } |
72 | } |
77 | memset(usr->cwd, 0, sizeof(usr->cwd)); |
73 | memset(usr->cwd, 0, sizeof(usr->cwd)); |
78 | 74 | ||
79 | usr->cwd = getcwd(usr->cwd, PATH_MAX - 1); |
75 | usr->cwd = getcwd(usr->cwd, PATH_MAX - 1); |
80 | if (NULL == usr->cwd) |
76 | if (NULL == usr->cwd) |
81 | snprintf(usr->cwd, PATH_MAX, "(unknown)"); |
77 | snprintf(usr->cwd, PATH_MAX, "(unknown)"); |
82 | 78 | ||
83 | if (usr->lasterr != 0) |
79 | if (usr->lasterr != 0) |
84 | snprintf(usr->prompt, |
80 | snprintf(usr->prompt, |
85 | PATH_MAX, |
81 | PATH_MAX, |
86 | "[%lld] %s # ", |
82 | "[%lld] %s # ", |
87 | usr->lasterr, usr->cwd); |
83 | usr->lasterr, usr->cwd); |
88 | else |
84 | else |
89 | snprintf(usr->prompt, |
85 | snprintf(usr->prompt, |
90 | PATH_MAX, |
86 | PATH_MAX, |
91 | "%s # ", |
87 | "%s # ", |
92 | usr->cwd); |
88 | usr->cwd); |
93 | 89 | ||
94 | return 0; |
90 | return 0; |
95 | } |
91 | } |
96 | 92 | ||
97 | int cli_init(cliuser_t *usr) |
93 | int cli_init(cliuser_t *usr) |
98 | { |
94 | { |
99 | usr->line = (char *) NULL; |
95 | usr->line = (char *) NULL; |
100 | usr->name = "root"; |
96 | usr->name = "root"; |
101 | usr->home = "/"; |
97 | usr->home = "/"; |
102 | usr->cwd = (char *) NULL; |
98 | usr->cwd = (char *) NULL; |
103 | usr->prompt = (char *) NULL; |
99 | usr->prompt = (char *) NULL; |
104 | chdir(usr->home); |
100 | chdir(usr->home); |
105 | usr->lasterr = 0; |
101 | usr->lasterr = 0; |
106 | return (int) cli_set_prompt(usr); |
102 | return (int) cli_set_prompt(usr); |
107 | } |
103 | } |
108 | 104 | ||
109 | /* Destructor */ |
105 | /* Destructor */ |
110 | void cli_finit(cliuser_t *usr) |
106 | void cli_finit(cliuser_t *usr) |
111 | { |
107 | { |
112 | if (NULL != usr->line) |
108 | if (NULL != usr->line) |
113 | free(usr->line); |
109 | free(usr->line); |
114 | if (NULL != usr->prompt) |
110 | if (NULL != usr->prompt) |
115 | free(usr->prompt); |
111 | free(usr->prompt); |
116 | if (NULL != usr->cwd) |
112 | if (NULL != usr->cwd) |
117 | free(usr->cwd); |
113 | free(usr->cwd); |
118 | } |
114 | } |
119 | 115 | ||
120 | int main(int argc, char *argv[]) |
116 | int main(int argc, char *argv[]) |
121 | { |
117 | { |
122 | int ret = 0; |
118 | int ret = 0; |
123 | int i = 0; |
119 | int i = 0; |
124 | 120 | ||
125 | if (cli_init(&usr)) |
121 | if (cli_init(&usr)) |
126 | exit(EXIT_FAILURE); |
122 | exit(EXIT_FAILURE); |
127 | 123 | ||
128 | printf("Welcome to %s - %s\nType `help' at any time for usage information.\n", |
124 | printf("Welcome to %s - %s\nType `help' at any time for usage information.\n", |
129 | progname, PACKAGE_STRING); |
125 | progname, PACKAGE_STRING); |
130 | 126 | ||
131 | while (!cli_quit) { |
127 | while (!cli_quit) { |
132 | cli_set_prompt(&usr); |
128 | cli_set_prompt(&usr); |
133 | get_input(&usr); |
129 | get_input(&usr); |
134 | if (NULL != usr.line) { |
130 | if (NULL != usr.line) { |
135 | ret = tok_input(&usr); |
131 | ret = tok_input(&usr); |
136 | usr.lasterr = ret; |
132 | usr.lasterr = ret; |
137 | } |
133 | } |
138 | i++; |
134 | i++; |
139 | } |
135 | } |
140 | goto finit; |
136 | goto finit; |
141 | 137 | ||
142 | finit: |
138 | finit: |
143 | cli_finit(&usr); |
139 | cli_finit(&usr); |
144 | return ret; |
140 | return ret; |
145 | } |
141 | } |
146 | 142 |