00001
00007
00008
00009 #include <kernel.h>
00010 #include <shell.h>
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <tty.h>
00014 #include <proc.h>
00015
00016 struct centry commandtab[] =
00017 {
00018 { "exit", TRUE, xsh_exit },
00019 { "kill", TRUE, xsh_kill },
00020 { "sleep", TRUE, xsh_sleep },
00021 { "gpiostat", FALSE, xsh_gpiostat },
00022 { "help", FALSE, xsh_help },
00023 { "led", FALSE, xsh_led },
00024 { "memstat", FALSE, xsh_memstat },
00025 { "ps", FALSE, xsh_ps },
00026 { "reset", FALSE, xsh_reset },
00027 { "uartstat", FALSE, xsh_uartstat },
00028 { "?", FALSE, xsh_help },
00029 { "testsuite", TRUE, xsh_testsuite },
00030 { "test", FALSE, xsh_test }
00031 };
00032
00033 ulong ncommand = sizeof( commandtab ) / sizeof( struct centry );
00034
00040 process shell(ushort descrp)
00041 {
00042 char buf[SHELL_BUFLEN];
00043 ushort buflen;
00044 char tokbuf[SHELL_BUFLEN+SHELL_MAXTOK];
00045 short ntok;
00046 char *tok[SHELL_MAXTOK];
00047 char *outname;
00048 char *inname;
00049 bool background;
00050 ushort stdin;
00051 ushort stdout;
00052 ushort stderr;
00053 syscall child;
00054 ushort i, j;
00055
00056
00057 fprintf(descrp, SHELL_BANNER);
00058
00059 fprintf(descrp, SHELL_START);
00060
00061
00062 while (TRUE)
00063 {
00064
00065 fprintf(descrp, SHELL_PROMPT);
00066
00067
00068 control(descrp, TTY_IOC_SETIFLAG, TTY_IFLAG_ECHO, NULL);
00069 buflen = read(descrp, buf, SHELL_BUFLEN - 1);
00070
00071
00072 if ( control(descrp, TTY_IOC_CBREAK, NULL, NULL) )
00073 {
00074 buflen = 0;
00075 continue;
00076 }
00077
00078
00079 if ( control(descrp, TTY_IOC_EOF, NULL, NULL) ) { break; }
00080
00081
00082 buf[buflen] = '\0';
00083
00084
00085 if ( (ntok = lexan(buf, buflen, &tokbuf[0], &tok[0])) == SYSERR )
00086 {
00087 fprintf(descrp, SHELL_SYNTAXERR);
00088 continue;
00089 }
00090
00091
00092 if (0 == ntok) { continue; }
00093
00094
00095 outname = NULL;
00096 inname = NULL;
00097 background = FALSE;
00098
00099
00100 if ('&' == *tok[ntok-1])
00101 {
00102 ntok--;
00103 background = TRUE;
00104 }
00105
00106
00107 for ( i = 0; i < ntok; i++ )
00108 {
00109
00110 if ('&' == *tok[i])
00111 {
00112 ntok = -1;
00113 break;
00114 }
00115
00116
00117 if ('>' == *tok[i])
00118 {
00119
00120 if (outname != NULL || i >= ntok-1)
00121 {
00122 ntok = -1;
00123 break;
00124 }
00125
00126 outname = tok[i+1];
00127 ntok -= 2;
00128
00129
00130 for ( j = i; j < ntok; j++)
00131 { tok[j] = tok[j+2]; }
00132 continue;
00133 }
00134
00135
00136 if ('<' == *tok[i])
00137 {
00138
00139 if(inname != NULL || i >= ntok-1)
00140 {
00141 ntok = -1;
00142 break;
00143 }
00144 inname = tok[i+1];
00145 ntok -= 2;
00146
00147
00148 for ( j = i; j < ntok; j++)
00149 { tok[j] = tok[j+2]; }
00150
00151 continue;
00152 }
00153 }
00154
00155
00156 if (ntok <= 0)
00157 {
00158 fprintf(descrp, SHELL_SYNTAXERR);
00159 continue;
00160 }
00161
00162
00163 stdin = descrp;
00164 stdout = descrp;
00165 stderr = descrp;
00166
00167
00168 for (i = 0; i < ncommand; i++)
00169 {
00170 if (strcmp(commandtab[i].name, tok[0]) == 0) { break; }
00171 }
00172
00173
00174 if (i >= ncommand)
00175 {
00176 fprintf(descrp, "%s: command not found\n", tok[0]);
00177 continue;
00178 }
00179
00180
00181 if (commandtab[i].builtin)
00182 {
00183 if (inname != NULL || outname != NULL || background)
00184 {
00185 fprintf(descrp, SHELL_SYNTAXERR);
00186 }
00187 else
00188 {
00189 (*commandtab[i].procedure)(stdin, stdout, stderr, ntok, tok);
00190 }
00191 continue;
00192 }
00193
00194
00195 child = create(commandtab[i].procedure, SHELL_CMDSTK, SHELL_CMDPRIO,
00196 commandtab[i].name, 5,
00197 stdin, stdout, stderr, ntok, tok);
00198
00199
00200 if (SYSERR == child)
00201 {
00202 fprintf(descrp, SHELL_CHILDERR);
00203 continue;
00204 }
00205
00206 if (background)
00207 {
00208
00209 ready(child, RESCHED_NO);
00210 }
00211 else
00212 {
00213
00214 while ( receive(FALSE) != NULL );
00215 ready(child, RESCHED_YES);
00216
00217
00218 while ( receive(TRUE) != child );
00219 sleep(10);
00220 }
00221 }
00222
00223
00224 fprintf(descrp, SHELL_EXIT);
00225 sleep(10);
00226 return OK;
00227 }