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