1 | /////////////////////////////////////////////////////////////////////////////// |
---|
2 | // File : ksh.c |
---|
3 | // Date : October 2017 |
---|
4 | // Author : Alain Greiner |
---|
5 | /////////////////////////////////////////////////////////////////////////////// |
---|
6 | // This single thread application implement a simple shell for ALMOS-MKH. |
---|
7 | /////////////////////////////////////////////////////////////////////////////// |
---|
8 | |
---|
9 | #include <stdio.h> |
---|
10 | #include <stdlib.h> |
---|
11 | #include <string.h> |
---|
12 | #include <shared_syscalls.h> |
---|
13 | |
---|
14 | #define CMD_MAX_SIZE (256) // max number of characters in one command |
---|
15 | #define LOG_DEPTH (256) // max number of registered commands |
---|
16 | #define MAX_ARGS (32) // max number of arguments in a command |
---|
17 | #define FIFO_SIZE (1024) // FIFO depth for recursive ls |
---|
18 | |
---|
19 | //////////////////////////////////////////////////////////////////////////////// |
---|
20 | // Structures |
---|
21 | //////////////////////////////////////////////////////////////////////////////// |
---|
22 | |
---|
23 | // one entry in the registered commands array |
---|
24 | typedef struct log_entry_s |
---|
25 | { |
---|
26 | char buf[CMD_MAX_SIZE]; |
---|
27 | unsigned int count; |
---|
28 | } |
---|
29 | log_entry_t; |
---|
30 | |
---|
31 | // one entry in the supported command types array |
---|
32 | typedef struct ksh_cmd_s |
---|
33 | { |
---|
34 | char * name; |
---|
35 | char * desc; |
---|
36 | void (*fn)( int , char ** ); |
---|
37 | } |
---|
38 | ksh_cmd_t; |
---|
39 | |
---|
40 | |
---|
41 | //////////////////////////////////////////////////////////////////////////////// |
---|
42 | // Global Variables |
---|
43 | //////////////////////////////////////////////////////////////////////////////// |
---|
44 | |
---|
45 | ksh_cmd_t cmd[]; // array of supported commands |
---|
46 | |
---|
47 | log_entry_t log_entries[LOG_DEPTH]; // array of registered commands |
---|
48 | |
---|
49 | unsigned int ptw; // write pointer in log_entries[] |
---|
50 | unsigned int ptr; // read pointer in log_entries[] |
---|
51 | |
---|
52 | //////////////////////////////////////////////////////////////////////////////// |
---|
53 | // Shell Commands |
---|
54 | //////////////////////////////////////////////////////////////////////////////// |
---|
55 | |
---|
56 | ///////////////////////////////////////////// |
---|
57 | static void cmd_cat( int argc , char **argv ) |
---|
58 | { |
---|
59 | char * path; |
---|
60 | |
---|
61 | if (argc != 2) |
---|
62 | { |
---|
63 | printf(" usage: cat pathname\n"); |
---|
64 | return; |
---|
65 | } |
---|
66 | |
---|
67 | path = argv[1]; |
---|
68 | |
---|
69 | printf(" error: not implemented yet\n"); |
---|
70 | |
---|
71 | /* |
---|
72 | // open the file |
---|
73 | fd = open( path , O_RDONLY , 0 ); |
---|
74 | if (fd < 0) |
---|
75 | { |
---|
76 | printf(" error: cannot open %s\n", path); |
---|
77 | goto exit; |
---|
78 | } |
---|
79 | |
---|
80 | // get file size |
---|
81 | if (stat(path, &st) == -1) |
---|
82 | { |
---|
83 | printf(" error: cannot stat %s\n", path); |
---|
84 | goto exit; |
---|
85 | } |
---|
86 | if (S_ISDIR(st.st_mode)) { |
---|
87 | printf(" error: %s is a directory\n", path); |
---|
88 | goto exit; |
---|
89 | } |
---|
90 | size = st.st_size; |
---|
91 | |
---|
92 | // mmap the file |
---|
93 | buf = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
---|
94 | if (buf == NULL || buf == (char *)-1) { |
---|
95 | printf(" error: cannot map %s\n", path); |
---|
96 | goto exit; |
---|
97 | } |
---|
98 | |
---|
99 | // set terminating '0' XXX |
---|
100 | buf[size-1] = 0; |
---|
101 | |
---|
102 | // display the file content |
---|
103 | printf("%s", buf); |
---|
104 | |
---|
105 | exit: |
---|
106 | if (buf != NULL) munmap(buf, size); |
---|
107 | if (fd >= 0) close(fd); |
---|
108 | */ |
---|
109 | |
---|
110 | } // end cmd_cat() |
---|
111 | |
---|
112 | //////////////////////////////////////////// |
---|
113 | static void cmd_cd( int argc , char **argv ) |
---|
114 | { |
---|
115 | char * path; |
---|
116 | |
---|
117 | if (argc != 2) |
---|
118 | { |
---|
119 | printf(" usage: cd pathname\n"); |
---|
120 | return; |
---|
121 | } |
---|
122 | |
---|
123 | path = argv[1]; |
---|
124 | |
---|
125 | printf(" error: not implemented yet\n"); |
---|
126 | /* |
---|
127 | path = argv[1]; |
---|
128 | |
---|
129 | if (chdir(path) == -1) |
---|
130 | { |
---|
131 | printf(" error: cannot cd to %s\n", path); |
---|
132 | } |
---|
133 | */ |
---|
134 | |
---|
135 | } // end cmd_cd() |
---|
136 | |
---|
137 | ///////////////////////////////////////// |
---|
138 | static void cmd_cp(int argc, char **argv) |
---|
139 | { |
---|
140 | // int src_fd = -1, dst_fd = -1; |
---|
141 | // char *srcpath, *dstpath; |
---|
142 | // struct stat st; |
---|
143 | // size_t size, i; |
---|
144 | // char buf[1024]; |
---|
145 | |
---|
146 | if (argc != 3) |
---|
147 | { |
---|
148 | printf(" usage: cp src_pathname dst_pathname\n"); |
---|
149 | return; |
---|
150 | } |
---|
151 | |
---|
152 | printf(" error: not implemented yet\n"); |
---|
153 | |
---|
154 | /* |
---|
155 | srcpath = argv[1]; |
---|
156 | dstpath = argv[2]; |
---|
157 | |
---|
158 | // open the src file |
---|
159 | src_fd = open(srcpath, O_RDONLY, 0); |
---|
160 | if (src_fd < 0) { |
---|
161 | printf(" error: cannot open %s / err = %d\n", srcpath, errno); |
---|
162 | goto exit; |
---|
163 | } |
---|
164 | |
---|
165 | // get file size |
---|
166 | if (stat(srcpath, &st) == -1) { |
---|
167 | printf(" error: cannot stat %s\n", srcpath); |
---|
168 | goto exit; |
---|
169 | } |
---|
170 | if (S_ISDIR(st.st_mode)) { |
---|
171 | printf(" error: %s is a directory\n", srcpath); |
---|
172 | goto exit; |
---|
173 | } |
---|
174 | size = st.st_size; |
---|
175 | |
---|
176 | // open the dst file |
---|
177 | dst_fd = open(dstpath, O_CREAT|O_TRUNC|O_RDWR, 0); |
---|
178 | if (dst_fd < 0) { |
---|
179 | printf(" error: cannot open %s / err = %d\n", dstpath, errno); |
---|
180 | goto exit; |
---|
181 | } |
---|
182 | if (stat(dstpath, &st) == -1) { |
---|
183 | printf(" error: cannot stat %s\n", dstpath); |
---|
184 | goto exit; |
---|
185 | } |
---|
186 | if (S_ISDIR(st.st_mode)) { |
---|
187 | printf(" error: %s is a directory\n", dstpath); |
---|
188 | goto exit; |
---|
189 | } |
---|
190 | |
---|
191 | i = 0; |
---|
192 | while (i < size) |
---|
193 | { |
---|
194 | size_t rlen = (size - i < 1024 ? size - i : 1024); |
---|
195 | size_t wlen; |
---|
196 | ssize_t ret; |
---|
197 | |
---|
198 | // read the source |
---|
199 | ret = read(src_fd, buf, rlen); |
---|
200 | if (ret == -1) { |
---|
201 | printf(" error: cannot read from file %s\n", srcpath); |
---|
202 | goto exit; |
---|
203 | } |
---|
204 | rlen = (size_t)ret; |
---|
205 | |
---|
206 | // write to the destination |
---|
207 | ret = write(dst_fd, buf, rlen); |
---|
208 | if (ret == -1) { |
---|
209 | printf(" error: cannot write to file %s\n", dstpath); |
---|
210 | goto exit; |
---|
211 | } |
---|
212 | wlen = (size_t)ret; |
---|
213 | |
---|
214 | // check |
---|
215 | if (wlen != rlen) { |
---|
216 | printf(" error: cannot write on device\n"); |
---|
217 | goto exit; |
---|
218 | } |
---|
219 | |
---|
220 | i += rlen; |
---|
221 | } |
---|
222 | |
---|
223 | exit: |
---|
224 | if (src_fd >= 0) close(src_fd); |
---|
225 | if (dst_fd >= 0) close(dst_fd); |
---|
226 | */ |
---|
227 | |
---|
228 | } // end cmd_cp() |
---|
229 | |
---|
230 | ///////////////////////////////////////// |
---|
231 | static void cmd_fg(int argc, char **argv) |
---|
232 | { |
---|
233 | unsigned int pid; |
---|
234 | |
---|
235 | if (argc != 2) |
---|
236 | { |
---|
237 | printf(" usage: %s pid\n", argv[0]); |
---|
238 | return; |
---|
239 | } |
---|
240 | |
---|
241 | pid = atoi( argv[1] ); |
---|
242 | |
---|
243 | if( pid == 0 ) |
---|
244 | { |
---|
245 | printf(" error: ilegal pid format\n" ); |
---|
246 | } |
---|
247 | |
---|
248 | if( fg( pid ) ) |
---|
249 | { |
---|
250 | printf(" error: cannot find process %x\n", pid ); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | ////////////////////////////////////////////// |
---|
255 | static void cmd_help( int argc , char **argv ) |
---|
256 | { |
---|
257 | unsigned int i; |
---|
258 | |
---|
259 | if (argc != 1) |
---|
260 | { |
---|
261 | printf(" usage: %s\n", argv[0]); |
---|
262 | return; |
---|
263 | } |
---|
264 | |
---|
265 | printf("available commands:\n"); |
---|
266 | for (i = 0 ; cmd[i].name ; i++) |
---|
267 | { |
---|
268 | printf("\t%s\t : %s\n", cmd[i].name , cmd[i].desc); |
---|
269 | } |
---|
270 | } // end cmd_help() |
---|
271 | |
---|
272 | ////////////////////////////////////////////// |
---|
273 | static void cmd_kill( int argc , char **argv ) |
---|
274 | { |
---|
275 | unsigned int pid; |
---|
276 | |
---|
277 | if (argc != 2) |
---|
278 | { |
---|
279 | printf(" usage: %s pid\n", argv[0]); |
---|
280 | return; |
---|
281 | } |
---|
282 | |
---|
283 | pid = atoi( argv[1] ); |
---|
284 | |
---|
285 | if( pid == 0 ) |
---|
286 | { |
---|
287 | printf(" error: ilegal pid format\n" ); |
---|
288 | } |
---|
289 | |
---|
290 | if( kill( pid , SIGKILL ) ) |
---|
291 | { |
---|
292 | printf(" error: unable to kill process %x\n", pid ); |
---|
293 | } |
---|
294 | } // end cmd_kill() |
---|
295 | |
---|
296 | ////////////////////////////////////////////// |
---|
297 | static void cmd_load( int argc , char **argv ) |
---|
298 | { |
---|
299 | unsigned int ret_pid; |
---|
300 | unsigned int new_pid; |
---|
301 | char * pathname; |
---|
302 | unsigned int background; |
---|
303 | |
---|
304 | if( (argc < 2) || (argc > 3) ) |
---|
305 | { |
---|
306 | printf(" usage: %s pathname [&] \n", argv[0] ); |
---|
307 | return; |
---|
308 | } |
---|
309 | |
---|
310 | pathname = argv[1]; |
---|
311 | |
---|
312 | if( argc == 3 ) background = (argv[2][0] == '&'); |
---|
313 | |
---|
314 | // fork system call |
---|
315 | ret_pid = fork(); |
---|
316 | |
---|
317 | if (ret_pid == 0) // it is the child process |
---|
318 | { |
---|
319 | // exec system call |
---|
320 | if( exec( pathname , NULL , NULL ) ) |
---|
321 | { |
---|
322 | printf(" error: new process unable to exec <%s>\n", pathname ); |
---|
323 | exit(0); |
---|
324 | } |
---|
325 | |
---|
326 | // get new process pid |
---|
327 | new_pid = getpid(); |
---|
328 | |
---|
329 | // give new process terminal ownership |
---|
330 | if( background == 0 ) fg( new_pid ); |
---|
331 | } |
---|
332 | else if ( ret_pid < 0 ) // it is a failure reported to parent |
---|
333 | { |
---|
334 | printf(" error: unable to fork\n"); |
---|
335 | } |
---|
336 | } // end cmd_load |
---|
337 | |
---|
338 | ///////////////////////////////////////////// |
---|
339 | static void cmd_log( int argc , char **argv ) |
---|
340 | { |
---|
341 | unsigned int i; |
---|
342 | |
---|
343 | printf("--- registered commands ---\n"); |
---|
344 | for (i = 0; i < LOG_DEPTH; i++) |
---|
345 | { |
---|
346 | printf(" - %zu\t: %s\n", i, &log_entries[i].buf); |
---|
347 | } |
---|
348 | } |
---|
349 | |
---|
350 | //////////////////////////////////////////// |
---|
351 | static void cmd_ls( int argc , char **argv ) |
---|
352 | { |
---|
353 | char * path; |
---|
354 | |
---|
355 | // struct dirent * file; |
---|
356 | // DIR *dir; |
---|
357 | |
---|
358 | if (argc == 1) |
---|
359 | { |
---|
360 | path = "."; |
---|
361 | } |
---|
362 | else if (argc == 2) |
---|
363 | { |
---|
364 | path = argv[1]; |
---|
365 | } |
---|
366 | else |
---|
367 | { |
---|
368 | printf(" usage: ls [path]\n"); |
---|
369 | return; |
---|
370 | } |
---|
371 | |
---|
372 | printf(" error: not implemented yet\n"); |
---|
373 | /* |
---|
374 | dir = opendir( path ); |
---|
375 | while ((file = readdir(dir)) != NULL) |
---|
376 | { |
---|
377 | printf(" %s\n", file->d_name); |
---|
378 | } |
---|
379 | closedir(dir); |
---|
380 | */ |
---|
381 | } |
---|
382 | |
---|
383 | /////////////////////////////////////////////// |
---|
384 | static void cmd_mkdir( int argc , char **argv ) |
---|
385 | { |
---|
386 | char * pathname; |
---|
387 | |
---|
388 | if (argc != 2) |
---|
389 | { |
---|
390 | printf(" usage: mkdir pathname\n"); |
---|
391 | return; |
---|
392 | } |
---|
393 | |
---|
394 | pathname = argv[1]; |
---|
395 | |
---|
396 | printf(" error: not implemented yet\n"); |
---|
397 | /* |
---|
398 | if ( mkdir( path, 0x700) == -1 ) |
---|
399 | { |
---|
400 | printf(" error: cannot create directory %s\n", path); |
---|
401 | } |
---|
402 | */ |
---|
403 | } |
---|
404 | |
---|
405 | //////////////////////////////////////////// |
---|
406 | static void cmd_mv( int argc , char **argv ) |
---|
407 | { |
---|
408 | |
---|
409 | if (argc < 3) |
---|
410 | { |
---|
411 | printf(" usage : %s src_pathname dst_pathname\n", argv[0]); |
---|
412 | return; |
---|
413 | } |
---|
414 | |
---|
415 | printf(" error: not implemented yet\n"); |
---|
416 | |
---|
417 | /* |
---|
418 | int ret = giet_fat_rename(argv[1], argv[2]); |
---|
419 | if (ret < 0) |
---|
420 | { |
---|
421 | printf(" error : cannot move %s to %s / err = %d\n", argv[1], argv[2], ret ); |
---|
422 | } |
---|
423 | */ |
---|
424 | |
---|
425 | } |
---|
426 | |
---|
427 | ///////////////////////////////////////////// |
---|
428 | static void cmd_pwd( int argc , char **argv ) |
---|
429 | { |
---|
430 | char buf[1024]; |
---|
431 | |
---|
432 | if (argc != 1) |
---|
433 | { |
---|
434 | printf(" usage: pwd\n"); |
---|
435 | return; |
---|
436 | } |
---|
437 | |
---|
438 | if ( getcwd( buf , 1024 ) ) |
---|
439 | { |
---|
440 | printf(" error: unable to get current directory\n"); |
---|
441 | } |
---|
442 | else |
---|
443 | { |
---|
444 | printf("%s\n", buf); |
---|
445 | } |
---|
446 | } |
---|
447 | |
---|
448 | //////////////////////////////////////////// |
---|
449 | static void cmd_rm( int argc , char **argv ) |
---|
450 | { |
---|
451 | char * pathname; |
---|
452 | |
---|
453 | if (argc != 2) |
---|
454 | { |
---|
455 | printf(" usage: rm pathname\n"); |
---|
456 | return; |
---|
457 | } |
---|
458 | |
---|
459 | pathname = argv[1]; |
---|
460 | |
---|
461 | printf(" error: not implemented yet\n"); |
---|
462 | /* |
---|
463 | if (remove(path) == -1) |
---|
464 | { |
---|
465 | printf(" error: cannot remove %s\n", path); |
---|
466 | } |
---|
467 | */ |
---|
468 | |
---|
469 | } |
---|
470 | |
---|
471 | /////////////////////////////////////////////// |
---|
472 | static void cmd_rmdir( int argc , char **argv ) |
---|
473 | { |
---|
474 | cmd_rm(argc, argv); |
---|
475 | } |
---|
476 | |
---|
477 | ///////////////////////////////////////////////// |
---|
478 | static void cmd_display( int argc , char **argv ) |
---|
479 | { |
---|
480 | unsigned int cxy; |
---|
481 | unsigned int lid; |
---|
482 | unsigned int pid; |
---|
483 | |
---|
484 | if( strcmp( argv[1] , "vmm" ) == 0 ) |
---|
485 | { |
---|
486 | if( argc != 3 ) |
---|
487 | { |
---|
488 | printf(" usage: display vmm pid\n"); |
---|
489 | return; |
---|
490 | } |
---|
491 | |
---|
492 | pid = atoi(argv[2]); |
---|
493 | |
---|
494 | if( display_vmm( pid ) ) |
---|
495 | { |
---|
496 | printf(" error: illegal arguments pid = %x\n", pid ); |
---|
497 | } |
---|
498 | } |
---|
499 | else if( strcmp( argv[1] , "sched" ) == 0 ) |
---|
500 | { |
---|
501 | if( argc != 4 ) |
---|
502 | { |
---|
503 | printf(" usage: display sched cxy lid\n"); |
---|
504 | return; |
---|
505 | } |
---|
506 | |
---|
507 | cxy = atoi(argv[2]); |
---|
508 | lid = atoi(argv[3]); |
---|
509 | |
---|
510 | if( display_sched( cxy , lid ) ) |
---|
511 | { |
---|
512 | printf(" error: illegal arguments cxy = %x / lid = %d\n", cxy, lid ); |
---|
513 | } |
---|
514 | } |
---|
515 | else if( strcmp( argv[1] , "process" ) == 0 ) |
---|
516 | { |
---|
517 | if( argc != 3 ) |
---|
518 | { |
---|
519 | printf(" usage: display process cxy\n"); |
---|
520 | return; |
---|
521 | } |
---|
522 | |
---|
523 | cxy = atoi(argv[2]); |
---|
524 | |
---|
525 | if( display_process( cxy ) ) |
---|
526 | { |
---|
527 | printf(" error: illegal argument cxy = %x\n", cxy ); |
---|
528 | } |
---|
529 | } |
---|
530 | else if( strcmp( argv[1] , "vfs" ) == 0 ) |
---|
531 | { |
---|
532 | if( argc != 2 ) |
---|
533 | { |
---|
534 | printf(" usage: display vfs\n"); |
---|
535 | return; |
---|
536 | } |
---|
537 | |
---|
538 | display_vfs(); |
---|
539 | } |
---|
540 | else if( strcmp( argv[1] , "chdev" ) == 0 ) |
---|
541 | { |
---|
542 | if( argc != 2 ) |
---|
543 | { |
---|
544 | printf(" usage: display chdev\n"); |
---|
545 | return; |
---|
546 | } |
---|
547 | |
---|
548 | display_chdev(); |
---|
549 | } |
---|
550 | else |
---|
551 | { |
---|
552 | printf(" usage display (vmm/sched/process/vfs/chdev) [cxy] [lid]\n"); |
---|
553 | } |
---|
554 | } |
---|
555 | |
---|
556 | ////////////////////////////////////////////////////////////////// |
---|
557 | // Array of commands |
---|
558 | ////////////////////////////////////////////////////////////////// |
---|
559 | |
---|
560 | ksh_cmd_t cmd[] = |
---|
561 | { |
---|
562 | { "cat", "display file content", cmd_cat }, |
---|
563 | { "cd", "change current directory", cmd_cd }, |
---|
564 | { "cp", "replicate a file in file system", cmd_cp }, |
---|
565 | { "fg", "put a process in foreground", cmd_fg }, |
---|
566 | { "display", "display vmm/sched/process/vfs/chdev", cmd_display }, |
---|
567 | { "load", "load an user application", cmd_load }, |
---|
568 | { "help", "list available commands", cmd_help }, |
---|
569 | { "kill", "kill an application (all threads)", cmd_kill }, |
---|
570 | { "log", "list registered commands", cmd_log }, |
---|
571 | { "ls", "list directory entries", cmd_ls }, |
---|
572 | { "mkdir", "create a new directory", cmd_mkdir }, |
---|
573 | { "mv", "move a file in file system", cmd_mv }, |
---|
574 | { "pwd", "print current working directory", cmd_pwd }, |
---|
575 | { "rm", "remove a file from file system", cmd_rm }, |
---|
576 | { "rmdir", "remove a directory from file system", cmd_rmdir }, |
---|
577 | { NULL, NULL, NULL } |
---|
578 | }; |
---|
579 | |
---|
580 | //////////////////////////////////////////////////////////////////////////////////// |
---|
581 | // This function analyses one command (with arguments), execute it, and return. |
---|
582 | //////////////////////////////////////////////////////////////////////////////////// |
---|
583 | static void parse(char *buf) |
---|
584 | { |
---|
585 | int argc = 0; |
---|
586 | char *argv[MAX_ARGS]; |
---|
587 | int i; |
---|
588 | int len = strlen(buf); |
---|
589 | |
---|
590 | // build argc/argv |
---|
591 | for (i = 0; i < len; i++) |
---|
592 | { |
---|
593 | if (buf[i] == ' ') |
---|
594 | { |
---|
595 | buf[i] = '\0'; |
---|
596 | } |
---|
597 | else if (i == 0 || buf[i - 1] == '\0') |
---|
598 | { |
---|
599 | if (argc < MAX_ARGS) |
---|
600 | { |
---|
601 | argv[argc] = &buf[i]; |
---|
602 | argc++; |
---|
603 | } |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | if (argc > 0) |
---|
608 | { |
---|
609 | int found = 0; |
---|
610 | |
---|
611 | argv[argc] = NULL; |
---|
612 | |
---|
613 | // try to match typed command |
---|
614 | for (i = 0; cmd[i].name; i++) |
---|
615 | { |
---|
616 | if (strcmp(argv[0], cmd[i].name) == 0) |
---|
617 | { |
---|
618 | cmd[i].fn(argc, argv); |
---|
619 | found = 1; |
---|
620 | break; |
---|
621 | } |
---|
622 | } |
---|
623 | |
---|
624 | if (!found) |
---|
625 | { |
---|
626 | printf("\n undefined command %s\n", argv[0]); |
---|
627 | } |
---|
628 | } |
---|
629 | } |
---|
630 | |
---|
631 | /////////////////////////////////// |
---|
632 | int main( int argc , char *argv[] ) |
---|
633 | { |
---|
634 | char c; // read character |
---|
635 | char buf[CMD_MAX_SIZE]; // buffer for one command |
---|
636 | unsigned int count = 0; // pointer in buf |
---|
637 | unsigned int i; // index for loops |
---|
638 | |
---|
639 | enum fsm_states |
---|
640 | { |
---|
641 | NORMAL, |
---|
642 | ESCAPE, |
---|
643 | BRAKET, |
---|
644 | }; |
---|
645 | |
---|
646 | // log buffer initialisation |
---|
647 | memset( &log_entries , 0, sizeof(log_entries)); |
---|
648 | ptw = 0; |
---|
649 | ptr = 0; |
---|
650 | |
---|
651 | printf( "~~~ shell ~~~\n\n" ); |
---|
652 | |
---|
653 | // command buffer initialisation |
---|
654 | memset( buf, 0x20 , sizeof(buf) ); |
---|
655 | count = 0; |
---|
656 | |
---|
657 | // display first prompt |
---|
658 | printf("# "); |
---|
659 | |
---|
660 | // This lexical analyser writes one command line in the buf buffer. |
---|
661 | // It is implemented as a 3 states FSM to handle the following sequences: |
---|
662 | // - ESC [ A : up arrow |
---|
663 | // - ESC [ B : down arrow |
---|
664 | // - ESC [ C : right arrow |
---|
665 | // - ESC [ D : left arrow |
---|
666 | // The three states have the following semantic: |
---|
667 | // - NORMAL : no (ESC) character has been found |
---|
668 | // - ESCAPE : the character (ESC) has been found |
---|
669 | // - BRAKET : the wo characters (ESC,[) have been found |
---|
670 | unsigned int state = NORMAL; |
---|
671 | |
---|
672 | |
---|
673 | // @@@ |
---|
674 | // parse("load /bin/user/sort.elf"); |
---|
675 | // @@@ |
---|
676 | |
---|
677 | |
---|
678 | while (1) |
---|
679 | { |
---|
680 | c = (char)getchar(); |
---|
681 | |
---|
682 | if( c == 0 ) continue; |
---|
683 | |
---|
684 | switch (state) |
---|
685 | { |
---|
686 | case NORMAL: |
---|
687 | { |
---|
688 | if ((c == '\b') || (c == 0x7F)) // backspace => remove one character |
---|
689 | { |
---|
690 | if (count > 0) { |
---|
691 | printf("\b \b"); |
---|
692 | count--; |
---|
693 | } |
---|
694 | } |
---|
695 | else if (c == '\n') // new line => call parser to execute command |
---|
696 | { |
---|
697 | if (count > 0) |
---|
698 | { |
---|
699 | // complete command |
---|
700 | buf[count] = '\0'; |
---|
701 | |
---|
702 | // register command in log arrays |
---|
703 | strcpy(log_entries[ptw].buf, buf); |
---|
704 | log_entries[ptw].count = count; |
---|
705 | ptw = (ptw + 1) % LOG_DEPTH; |
---|
706 | ptr = ptw; |
---|
707 | |
---|
708 | // execute command |
---|
709 | printf("\n"); |
---|
710 | parse((char *)&buf); |
---|
711 | |
---|
712 | // reinitialise buffer and display prompt |
---|
713 | for ( i = 0 ; i < sizeof(buf) ; i++ ) buf[i] = 0x20; |
---|
714 | count = 0; |
---|
715 | printf("# "); |
---|
716 | } |
---|
717 | } |
---|
718 | else if (c == '\t') // tabulation => do nothing |
---|
719 | { |
---|
720 | } |
---|
721 | else if (c == 0x1B) // ESC => start an escape sequence |
---|
722 | { |
---|
723 | state = ESCAPE; |
---|
724 | } |
---|
725 | else if (c == 0x03) // ^C => cancel current command |
---|
726 | { |
---|
727 | for (i = 0; i < count; i++) printf("\b \b"); |
---|
728 | for (i = 0; i < sizeof(buf); i++) buf[i] = 0x20; |
---|
729 | count = 0; |
---|
730 | } |
---|
731 | else // register character in command buffer |
---|
732 | { |
---|
733 | if (count < sizeof(buf) - 1) |
---|
734 | { |
---|
735 | putchar( c ); |
---|
736 | buf[count] = c; |
---|
737 | count++; |
---|
738 | } |
---|
739 | } |
---|
740 | break; |
---|
741 | } |
---|
742 | case ESCAPE: |
---|
743 | { |
---|
744 | if (c == '[') // valid sequence => continue |
---|
745 | { |
---|
746 | state = BRAKET; |
---|
747 | } |
---|
748 | else // invalid sequence => do nothing |
---|
749 | { |
---|
750 | state = NORMAL; |
---|
751 | } |
---|
752 | break; |
---|
753 | } |
---|
754 | case BRAKET: |
---|
755 | { |
---|
756 | if (c == 'D') // valid LEFT sequence => move buf pointer left |
---|
757 | { |
---|
758 | if (count > 0) |
---|
759 | { |
---|
760 | printf("\b"); |
---|
761 | count--; |
---|
762 | } |
---|
763 | |
---|
764 | // get next user char |
---|
765 | state = NORMAL; |
---|
766 | } |
---|
767 | else if (c == 'C') // valid RIGHT sequence => move buf pointer right |
---|
768 | { |
---|
769 | if (count < sizeof(buf) - 1) |
---|
770 | { |
---|
771 | printf("%c", buf[count]); |
---|
772 | count++; |
---|
773 | } |
---|
774 | |
---|
775 | // get next user char |
---|
776 | state = NORMAL; |
---|
777 | } |
---|
778 | else if (c == 'A') // valid UP sequence => move log pointer backward |
---|
779 | { |
---|
780 | // cancel current command |
---|
781 | for (i = 0; i < count; i++) printf("\b \b"); |
---|
782 | count = 0; |
---|
783 | |
---|
784 | // copy log command into buf |
---|
785 | ptr = (ptr - 1) % LOG_DEPTH; |
---|
786 | strcpy(buf, log_entries[ptr].buf); |
---|
787 | count = log_entries[ptr].count; |
---|
788 | |
---|
789 | // display log command |
---|
790 | printf("%s", buf); |
---|
791 | |
---|
792 | // get next user char |
---|
793 | state = NORMAL; |
---|
794 | } |
---|
795 | else if (c == 'B') // valid DOWN sequence => move log pointer forward |
---|
796 | { |
---|
797 | // cancel current command |
---|
798 | for (i = 0 ; i < count; i++) printf("\b \b"); |
---|
799 | count = 0; |
---|
800 | |
---|
801 | // copy log command into buf |
---|
802 | ptr = (ptr + 1) % LOG_DEPTH; |
---|
803 | strcpy(buf, log_entries[ptr].buf); |
---|
804 | count = log_entries[ptr].count; |
---|
805 | |
---|
806 | // display log command |
---|
807 | printf("%s", buf); |
---|
808 | |
---|
809 | // get next user char |
---|
810 | state = NORMAL; |
---|
811 | } |
---|
812 | else // other character => do nothing |
---|
813 | { |
---|
814 | // get next user char |
---|
815 | state = NORMAL; |
---|
816 | } |
---|
817 | break; |
---|
818 | } |
---|
819 | } |
---|
820 | } |
---|
821 | } |
---|
822 | |
---|