Fixed possible memory corruption in commands exec and netexec; fixed command netcp...
[ZeXOS.git] / kernel / core / console.c
blob06806f8932bf07e64c8a26734e00c85597fbe0c5
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <system.h>
24 #include <string.h>
25 #include <console.h>
26 #include <tty.h>
27 #include <config.h>
28 #include <net/hostname.h>
30 static consolelog_t clog_list;
31 static consolelog_t *clog_curr;
33 static unsigned clog_num;
35 extern task_t *_curr_task;
37 int gets ()
39 if (!currtty->active)
40 return 0;
42 char key = getkey ();
44 if (key) {
45 //DPRINT ("currtty->task %s | _curr_task %s\n", currtty->task->name, _curr_task->name);
47 int i = 0;
48 video_color (2, 0);
50 char c[1];
51 c[0] = key;
53 if (key == 8 && currtty->shell_len == 0 || key == 27)
54 return i;
56 if (currtty->shell_len < 64)
57 tty_write (currtty, c, 1);
59 if (key == '\n') { // we press enter
60 currtty->shell[currtty->shell_len] = '\0';
61 currtty->shell_len = 0;
62 i = 1;
63 } else {
64 /* if you has wroted some text, and backspace is pressed, lets move cursor back */
65 if (key == '\b' && currtty->shell_len > 0) {
66 currtty->shell_len --; // first we have to descrease text length
67 currtty->shell[currtty->shell_len] = '\0'; // we can put \0 character under cursor
68 //printf ("len: %d\n", currtty->shell_len);
70 else if (currtty->shell_len < 64) { /* you type normal text */
71 currtty->shell[currtty->shell_len] = key;
72 currtty->shell_len ++;
76 key = 0;
77 video_color (15, 0);
79 return i;
82 return 0;
85 unsigned consolelog_list ()
87 consolelog_t *c;
89 for (c = clog_list.next; c != &clog_list; c = c->next)
90 printf ("%s\n", c->cmd);
92 printf ("-> %d\n", clog_num);
94 return 1;
97 unsigned consolelog_prev ()
99 if (!currtty->active)
100 return 0;
102 if (!currtty->logged)
103 return 0;
105 if (!clog_curr)
106 return 0;
108 /* go to previous entry of log list */
109 consolelog_t *prev = clog_curr;
111 if (!prev)
112 return 0;
114 unsigned short l = currtty->shell_len;
116 /* delete all current characters from line () */
117 while (l) {
118 tty_write (currtty, "\b", 1);
120 l --;
123 memcpy (currtty->shell, prev->cmd, prev->len);
124 currtty->shell[prev->len] = '\0';
126 currtty->shell_len = prev->len;
128 video_color (2, 0);
130 tty_write (currtty, currtty->shell, prev->len);
132 clog_curr = prev->prev;
134 return 1;
137 unsigned consolelog_next ()
139 if (!currtty->active)
140 return 0;
142 if (!currtty->logged)
143 return 0;
145 if (!clog_curr)
146 return 0;
148 /* go to next entry of log list */
149 consolelog_t *next = clog_curr->next;
151 if (!next)
152 return 0;
154 unsigned short l = currtty->shell_len;
156 /* delete all current characters from line () */
157 while (l) {
158 tty_write (currtty, "\b", 1);
160 l --;
163 memcpy (currtty->shell, next->cmd, next->len);
164 currtty->shell[next->len] = '\0';
166 currtty->shell_len = next->len;
168 video_color (2, 0);
170 tty_write (currtty, currtty->shell, next->len);
172 clog_curr = next;
174 return 1;
177 unsigned int console_refresh ()
179 currtty->shell_len = 0;
181 if (currtty->user) {
182 if (currtty->user->attrib == USER_GROUP_ADMIN) {
183 video_color (12, 0);
184 printf ("%s@%s ", currtty->user->name, hostname_get ());
185 video_color (9, 0);
186 printf ("%s # ", (char *) env_get ("PWD"));
187 } else {
188 video_color (14, 0);
189 printf ("%s@%s ", currtty->user->name, hostname_get ());
190 video_color (9, 0);
191 printf ("%s $ ", (char *) env_get ("PWD"));
195 video_color (15, 0);
197 return 1;
200 unsigned int init_console ()
202 clog_num = 0;
203 clog_curr = 0;
205 clog_list.next = &clog_list;
206 clog_list.prev = &clog_list;
208 return 1;
211 void console (int i)
213 unsigned len = strlen (currtty->shell);
215 /* commands are active only, if you type one or more chars */
216 if (len) {
217 /* parse command */
218 commands (i);
220 /* save this command to consolelog now */
221 consolelog_t *ctx;
223 /* when we reach limit, lets delete first one from list */
224 if (clog_num > CONFIG_UI_CONSOLELOG) {
225 ctx = clog_list.next;
227 if (ctx != &clog_list && ctx) {
228 kfree (ctx->cmd);
230 ctx->next->prev = ctx->prev;
231 ctx->prev->next = ctx->next;
233 kfree (ctx);
235 clog_num --;
239 // alloc and init context
240 ctx = (consolelog_t *) kmalloc (sizeof (consolelog_t));
242 if (!ctx)
243 return;
245 ctx->cmd = (char *) kmalloc (sizeof (char) * (len + 1));
247 if (!ctx->cmd)
248 return;
250 memcpy (ctx->cmd, currtty->shell, len);
251 ctx->cmd[len] = '\0';
253 ctx->len = len;
255 ctx->next = &clog_list;
256 ctx->prev = clog_list.prev;
257 ctx->prev->next = ctx;
258 ctx->next->prev = ctx;
260 /* increase number of list content */
261 clog_num ++;
263 /* go to last entry */
264 clog_curr = ctx;
267 /* clean console */
268 for(i = 0; i < currtty->shell_len; i ++)
269 currtty->shell[i] = '\0';
271 console_refresh ();