source: trunk/user/ksh/ksh.c @ 427

Last change on this file since 427 was 427, checked in by alain, 6 years ago

Several processes KSH[i] can be launched by the INIT process.

File size: 17.1 KB
RevLine 
[407]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///////////////////////////////////////////////////////////////////////////////
[230]8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
[416]12#include <shared_syscalls.h>
[230]13
[407]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
[230]18
[407]19////////////////////////////////////////////////////////////////////////////////
20//         Structures
21////////////////////////////////////////////////////////////////////////////////
[230]22
[407]23// one entry in the registered commands array
24typedef struct log_entry_s
25{
26        char          buf[CMD_MAX_SIZE];
27        unsigned int  count;
28}
29log_entry_t;
[230]30
[407]31// one entry in the supported command types array
32typedef struct ksh_cmd_s
33{
34        char * name;
35        char * desc;
36        void   (*fn)( int , char ** );
37}
38ksh_cmd_t;
39
40
[230]41////////////////////////////////////////////////////////////////////////////////
42//         Global Variables
43////////////////////////////////////////////////////////////////////////////////
44
[407]45ksh_cmd_t       cmd[];                    // array of supported commands
[230]46
[407]47log_entry_t     log_entries[LOG_DEPTH];   // array of registered commands
[230]48
[407]49unsigned int    ptw;                      // write pointer in log_entries[]
50unsigned int    ptr;                      // read pointer in log_entries[]
[230]51
52////////////////////////////////////////////////////////////////////////////////
53//         Shell  Commands
54////////////////////////////////////////////////////////////////////////////////
55
[407]56/////////////////////////////////////////////
57static void cmd_cat( int argc , char **argv )
[230]58{
[407]59        char         * path;
[230]60
[407]61        if (argc != 2) 
62    {
[409]63                printf("  usage: cat pathname\n");
[230]64                return;
65        }
[407]66
[230]67        path = argv[1];
68
[409]69    printf("  error: not implemented yet\n");
[407]70
71/*
72        // open the file
73        fd = open( path , O_RDONLY , 0 );
74        if (fd < 0)
75    {
[409]76                printf("  error: cannot open %s\n", path);
[230]77                goto exit;
78        }
79
[407]80        // get file size
81        if (stat(path, &st) == -1)
82    {
[409]83                printf("  error: cannot stat %s\n", path);
[230]84                goto exit;
85        }
86        if (S_ISDIR(st.st_mode)) {
[409]87                printf("  error: %s is a directory\n", path);
[230]88                goto exit;
89        }
90        size = st.st_size;
91
[407]92        // mmap the file
[230]93        buf = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
94        if (buf == NULL || buf == (char *)-1) {
[409]95                printf("  error: cannot map %s\n", path);
[230]96                goto exit;
97        }
98
[407]99        // set terminating '0' XXX
[230]100        buf[size-1] = 0;
101
[407]102        // display the file content
[230]103        printf("%s", buf);
104
105exit:
[407]106        if (buf != NULL) munmap(buf, size);
107        if (fd >= 0) close(fd);
108*/
[230]109
[407]110}   // end cmd_cat()
111
112////////////////////////////////////////////
113static void cmd_cd( int argc , char **argv )
114{
115        char * path;
116
117        if (argc != 2)
118    {
[409]119                printf("  usage: cd pathname\n");
[407]120                return;
121        }
122
123        path = argv[1];
124
[409]125    printf("  error: not implemented yet\n");
[407]126/*
127        path = argv[1];
128
129        if (chdir(path) == -1)
130    {
[409]131                printf("  error: cannot cd to %s\n", path);
[407]132        }
133*/
134
135}   // end cmd_cd()
136
137/////////////////////////////////////////
[230]138static void cmd_cp(int argc, char **argv)
139{
[407]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];
[230]145
[407]146        if (argc != 3) 
147    {
[409]148                printf("  usage: cp src_pathname dst_pathname\n");
[230]149                return;
150        }
151
[409]152    printf("  error: not implemented yet\n");
[407]153
154/*
[230]155        srcpath = argv[1];
156        dstpath = argv[2];
157
[407]158        // open the src file
[230]159        src_fd = open(srcpath, O_RDONLY, 0);
160        if (src_fd < 0) {
[409]161                printf("  error: cannot open %s / err = %d\n", srcpath, errno);
[230]162                goto exit;
163        }
164
[407]165        // get file size
[230]166        if (stat(srcpath, &st) == -1) {
[409]167                printf("  error: cannot stat %s\n", srcpath);
[230]168                goto exit;
169        }
170        if (S_ISDIR(st.st_mode)) {
[409]171                printf("  error: %s is a directory\n", srcpath);
[230]172                goto exit;
173        }
174        size = st.st_size;
175
[407]176        // open the dst file
[230]177        dst_fd = open(dstpath, O_CREAT|O_TRUNC|O_RDWR, 0);
178        if (dst_fd < 0) {
[409]179                printf("  error: cannot open %s / err = %d\n", dstpath, errno);
[230]180                goto exit;
181        }
182        if (stat(dstpath, &st) == -1) {
[409]183                printf("  error: cannot stat %s\n", dstpath);
[230]184                goto exit;
185        }
186        if (S_ISDIR(st.st_mode)) {
[409]187                printf("  error: %s is a directory\n", dstpath);
[230]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
[407]198                // read the source
[230]199                ret = read(src_fd, buf, rlen);
200                if (ret == -1) {
[409]201                        printf("  error: cannot read from file %s\n", srcpath);
[230]202                        goto exit;
203                }
204                rlen = (size_t)ret;
205
[407]206                // write to the destination
[230]207                ret = write(dst_fd, buf, rlen);
208                if (ret == -1) {
[409]209                        printf("  error: cannot write to file %s\n", dstpath);
[230]210                        goto exit;
211                }
212                wlen = (size_t)ret;
213
[407]214                // check
[230]215                if (wlen != rlen) {
[409]216                        printf("  error: cannot write on device\n");
[230]217                        goto exit;
218                }
219
220                i += rlen;
221        }
222
223exit:
[407]224        if (src_fd >= 0) close(src_fd);
225        if (dst_fd >= 0) close(dst_fd);
226*/
[230]227
[407]228}   // end cmd_cp()
229
[427]230/////////////////////////////////////////
231static 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
[407]254//////////////////////////////////////////////
255static void cmd_help( int argc , char **argv )
[230]256{
[407]257        unsigned int i;
[230]258
[407]259        if (argc != 1) 
260    {
[409]261                printf("  usage: %s\n", argv[0]);
[230]262                return;
263        }
264
265        printf("available commands:\n");
[407]266        for (i = 0 ; cmd[i].name ; i++) 
267    {
[230]268                printf("\t%s\t : %s\n", cmd[i].name , cmd[i].desc);
269        }
[407]270}   // end cmd_help()
[230]271
[407]272//////////////////////////////////////////////
273static void cmd_kill( int argc , char **argv )
[230]274{
[407]275        unsigned int pid;
[230]276
[407]277        if (argc != 2) 
278    {
[409]279                printf("  usage: %s pid\n", argv[0]);
[230]280                return;
281        }
282
[427]283        pid = atoi( argv[1] );
[230]284
[427]285    if( pid == 0 )
286    {
287                printf("  error: ilegal pid format\n" );
288        }
289
[416]290        if( kill( pid , SIGKILL ) )
[407]291    {
[409]292                printf("  error: unable to kill process %x\n", pid );
[230]293        }
[407]294}   // end cmd_kill()
[230]295
[407]296//////////////////////////////////////////////
297static void cmd_load( int argc , char **argv )
[230]298{
[427]299        unsigned int         ret_pid;
300    unsigned int         new_pid;
[408]301        char               * pathname;
[427]302    unsigned int         background;
[407]303
[427]304        if( (argc < 2) || (argc > 3) ) 
[407]305    {
[427]306                printf("  usage: %s pathname [&] \n", argv[0] );
[407]307                return;
308        }
309
310        pathname = argv[1];
311
[427]312    if( argc == 3 ) background = (argv[2][0] == '&');
313
[407]314    // fork system call
[427]315        ret_pid = fork();
[407]316
[427]317    if (ret_pid == 0)  // it is the child process
[407]318    {
319        // exec system call
[427]320        if( exec( pathname , NULL , NULL ) )
[409]321        {
322            printf("  error: new process unable to exec <%s>\n", pathname );
323            exit(0);
324        }
[427]325
326        // get new process pid
327        new_pid = getpid();
328
329        // give new process terminal ownership
330        if( background == 0 ) fg( new_pid );
[407]331        } 
[427]332    else if ( ret_pid < 0 )  // it is a failure reported to parent
[407]333    {
[409]334        printf("  error: unable to fork\n");
[407]335    }
336}   // end cmd_load
337
338/////////////////////////////////////////////
339static void cmd_log( int argc , char **argv )
340{
341        unsigned int i;
342
[230]343        printf("--- registered commands ---\n");
[407]344        for (i = 0; i < LOG_DEPTH; i++) 
345    {
[230]346                printf(" - %zu\t: %s\n", i, &log_entries[i].buf);
347        }
348}
349
[407]350////////////////////////////////////////////
351static void cmd_ls( int argc , char **argv )
[230]352{
[407]353        char  * path;
[230]354
[407]355//  struct dirent * file;
356//  DIR *dir;
357
358        if (argc == 1)
359    {
[230]360                path = ".";
[407]361        }
362    else if (argc == 2) 
363    {
[230]364                path = argv[1];
[407]365        } 
366    else 
367    {
[409]368                printf("  usage: ls [path]\n");
[230]369                return;
370        }
371
[409]372    printf("  error: not implemented yet\n");
[407]373/*
374        dir = opendir( path );
[230]375        while ((file = readdir(dir)) != NULL)
376        {
377                printf(" %s\n", file->d_name);
378        }
379        closedir(dir);
[407]380*/
[230]381}
382
[407]383///////////////////////////////////////////////
384static void cmd_mkdir( int argc , char **argv )
[230]385{
[407]386        char * pathname;
[230]387
[407]388        if (argc != 2)
389    {
[409]390                printf("  usage: mkdir pathname\n");
[230]391                return;
392        }
393
[407]394    pathname = argv[1];
[230]395
[409]396    printf("  error: not implemented yet\n");
[407]397/*
398        if ( mkdir( path, 0x700) == -1 )
399    {
[409]400                printf("  error: cannot create directory %s\n", path);
[230]401        }
[407]402*/
[230]403}
404
[407]405////////////////////////////////////////////
406static void cmd_mv( int argc , char **argv )
[230]407{
[407]408
[230]409        if (argc < 3)
410        {
411                printf("  usage : %s src_pathname dst_pathname\n", argv[0]);
412                return;
413        }
414
[409]415    printf("  error: not implemented yet\n");
[407]416   
417/*
[230]418        int ret = giet_fat_rename(argv[1], argv[2]);
419        if (ret < 0)
420        {
[409]421                printf("  error : cannot move %s to %s / err = %d\n", argv[1], argv[2], ret );
[230]422        }
[407]423*/
424
[230]425}
426
[407]427/////////////////////////////////////////////
428static void cmd_pwd( int argc , char **argv )
[230]429{
[407]430        char buf[1024];
[230]431
[407]432        if (argc != 1)
433    {
[409]434                printf("  usage: pwd\n");
[407]435                return;
436        }
437
438        if ( getcwd( buf , 1024 ) ) 
439    {
[409]440                printf("  error: unable to get current directory\n");
[407]441        }
442    else 
443    {
444                printf("%s\n", buf);
445        }
446}
447
448////////////////////////////////////////////
449static void cmd_rm( int argc , char **argv )
450{
451        char * pathname;
452
453        if (argc != 2)
454    {
[409]455                printf("  usage: rm pathname\n");
[230]456                return;
457        }
458
[407]459        pathname = argv[1];
[230]460
[409]461    printf("  error: not implemented yet\n");
[407]462/*
463        if (remove(path) == -1)
464    {
[409]465                printf("  error: cannot remove %s\n", path);
[230]466        }
[407]467*/
468
[230]469}
470
[407]471///////////////////////////////////////////////
472static void cmd_rmdir( int argc , char **argv )
[230]473{
474        cmd_rm(argc, argv);
475}
476
[427]477/////////////////////////////////////////////////
478static void cmd_display( int argc , char **argv )
[230]479{
[427]480    unsigned int  cxy;
481    unsigned int  lid;
482    unsigned int  pid;
[230]483
[427]484    if( strcmp( argv[1] , "vmm" ) == 0 )
[407]485    {
[427]486        if( argc != 3 )
487        {
488                    printf("  usage: display vmm pid\n");
489                    return;
490            }
[230]491
[427]492            pid = atoi(argv[2]);
[230]493
[427]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 )
[407]500    {
[427]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        }
[407]514    }
[427]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    }
[230]554}
555
[407]556//////////////////////////////////////////////////////////////////
557// Array of commands
558//////////////////////////////////////////////////////////////////
[230]559
[407]560ksh_cmd_t cmd[] =
[230]561{
[427]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         }
[230]578};
579
[407]580////////////////////////////////////////////////////////////////////////////////////
581// This function analyses one command (with arguments), execute it, and return.
582////////////////////////////////////////////////////////////////////////////////////
[230]583static 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
[407]591        for (i = 0; i < len; i++) 
592    {
593                if (buf[i] == ' ') 
594        {
[230]595                        buf[i] = '\0';
[407]596                }
597        else if (i == 0 || buf[i - 1] == '\0') 
598        {
599                        if (argc < MAX_ARGS) 
600            {
[230]601                                argv[argc] = &buf[i];
602                                argc++;
603                        }
604                }
605        }
606
[407]607        if (argc > 0)
608    {
[230]609                int found = 0;
610
611                argv[argc] = NULL;
612
[407]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            {
[230]618                                cmd[i].fn(argc, argv);
619                                found = 1;
620                                break;
621                        }
622                }
623
[407]624                if (!found)
625        {
[230]626                        printf("\n  undefined command %s\n", argv[0]);
627                }
628        }
629}
630
[407]631///////////////////////////////////
632int main( int argc , char *argv[] )
[230]633{
[407]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
[230]638
[407]639        enum fsm_states
640    {
[230]641                NORMAL,
642                ESCAPE,
643                BRAKET,
644        };
645
[407]646    // log buffer initialisation
647        memset( &log_entries , 0, sizeof(log_entries));
648        ptw   = 0;
649        ptr   = 0;
[230]650
651        printf( "~~~ shell ~~~\n\n" );
652
653        // command buffer initialisation
[407]654        memset( buf, 0x20 , sizeof(buf) );
[230]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
[407]666        // The three states have the following semantic:
[230]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
[409]672
[407]673// @@@
[416]674// parse("load /bin/user/sort.elf");
[407]675// @@@
[408]676
[407]677   
[230]678        while (1)
679        {
[407]680                c = (char)getchar();
[230]681
[407]682        if( c == 0 ) continue;
683
[230]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                                        {
[418]735                                                putchar( c );
[230]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
Note: See TracBrowser for help on using the repository browser.