source: rtems/cpukit/libmisc/shell/shell.c @ 8c422e2

4.104.114.95
Last change on this file since 8c422e2 was 8c422e2, checked in by Chris Johns <chrisj@…>, on 04/08/08 at 04:53:26

2008-04-08 Chris Johns <chrisj@…>

  • libmisc/shell/shell.c: Copy the cmd line to a buffer to split into argv parts. Was using the command line history buffer so the history was being corrupted.
  • Property mode set to 100644
File size: 25.7 KB
Line 
1/*
2 *
3 *  Instantatiate a new terminal shell.
4 *
5 *  Author:
6 *
7 *   WORK: fernando.ruiz@ctv.es
8 *   HOME: correo@fernando-ruiz.com
9 *
10 *  The license and distribution terms for this file may be
11 *  found in the file LICENSE in this distribution or at
12 *  http://www.rtems.com/license/LICENSE.
13 *
14 *  $Id$
15 */
16
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <stdio.h>
22#include <time.h>
23
24#include <rtems.h>
25#include <rtems/error.h>
26#include <rtems/libio.h>
27#include <rtems/libio_.h>
28#include <rtems/system.h>
29#include <rtems/shell.h>
30#include <rtems/shellconfig.h>
31#include "internal.h"
32
33#include <termios.h>
34#include <string.h>
35#include <stdlib.h>
36#include <ctype.h>
37#include <sys/stat.h>
38#include <unistd.h>
39#include <errno.h>
40#include <pwd.h>
41
42rtems_shell_env_t  rtems_global_shell_env;
43rtems_shell_env_t *rtems_current_shell_env;
44
45/*
46 *  Initialize the shell user/process environment information
47 */
48rtems_shell_env_t *rtems_shell_init_env(
49  rtems_shell_env_t *shell_env_arg
50)
51{
52  rtems_shell_env_t *shell_env;
53 
54  if (rtems_global_shell_env.magic != 0x600D600d) {
55    rtems_global_shell_env.magic         = 0x600D600d;
56    rtems_global_shell_env.devname       = "";
57    rtems_global_shell_env.taskname      = "GLOBAL";
58    rtems_global_shell_env.tcflag        = 0;
59    rtems_global_shell_env.exit_shell    = 0;
60    rtems_global_shell_env.forever       = TRUE;
61    rtems_global_shell_env.input         = 0;
62    rtems_global_shell_env.output        = 0;
63    rtems_global_shell_env.output_append = 0;
64  }
65
66  shell_env = shell_env_arg;
67
68  if ( !shell_env ) {
69    shell_env = malloc(sizeof(rtems_shell_env_t));
70    if ( !shell_env )
71      return NULL;
72    *shell_env = rtems_global_shell_env;
73    shell_env->taskname = NULL;
74  }
75
76  return shell_env;
77}
78
79/*
80 *  Get a line of user input with modest features
81 */
82int rtems_shell_line_editor(
83  char       *cmds[],
84  int         count,
85  int         size,
86  const char *prompt,
87  FILE       *in,
88  FILE       *out
89)
90{
91  unsigned int extended_key;
92  char         c;
93  int          col;
94  int          last_col;
95  int          output;
96  char         line[size];
97  char         new_line[size];
98  int          up;
99  int          cmd = -1;
100  int          inserting = 1;
101 
102  output = (out && isatty(fileno(in)));
103
104  col = last_col = 0;
105 
106  tcdrain(fileno(in));
107  if (out)
108    tcdrain(fileno(out));
109
110  if (output && prompt)
111    fprintf(out, "\r%s", prompt);
112 
113  line[0] = 0;
114  new_line[0] = 0;
115 
116  for (;;) {
117   
118    if (output)
119      fflush(out);
120
121    extended_key = rtems_shell_getchar(in);
122
123    if (extended_key == EOF)
124      return -2;
125   
126    c = extended_key & RTEMS_SHELL_KEYS_NORMAL_MASK;
127   
128    /*
129     * Make the extended_key usable as a boolean.
130     */
131    extended_key &= ~RTEMS_SHELL_KEYS_NORMAL_MASK;
132   
133    up = 0;
134   
135    if (extended_key)
136    {
137      switch (c)
138      {
139        case RTEMS_SHELL_KEYS_END:
140          if (output)
141            fprintf(out,line + col);
142          col = (int) strlen (line);
143          break;
144
145        case RTEMS_SHELL_KEYS_HOME:
146          if (output) {
147            if (prompt)
148              fprintf(out,"\r%s", prompt);
149          }
150          col = 0;
151          break;
152
153        case RTEMS_SHELL_KEYS_LARROW:
154          if (col > 0)
155          {
156            col--;
157            if (output)
158              fputc('\b', out);
159          }
160          break;
161
162          case RTEMS_SHELL_KEYS_RARROW:
163            if ((col < size) && (line[col] != '\0'))
164            {
165              if (output)
166                fprintf(out, "%c", line[col]);
167              col++;
168            }
169            break;
170
171        case RTEMS_SHELL_KEYS_UARROW:
172          if ((cmd >= (count - 1)) || (strlen(cmds[cmd + 1]) == 0)) {
173            if (output)
174              fputc('\x7', out);
175            break;
176          }
177
178          up = 1;
179
180          /* drop through */
181        case RTEMS_SHELL_KEYS_DARROW:
182         
183        {
184          int last_cmd = cmd;
185          int clen = strlen (line);
186
187          if (prompt)
188            clen += strlen(prompt);
189         
190          if (up) {
191            cmd++;
192          } else {
193            if (cmd < 0) {
194              if (output)
195                fprintf(out, "\x7");
196              break;
197            }
198            else
199              cmd--;
200          }
201
202          if ((last_cmd < 0) || (strcmp(cmds[last_cmd], line) != 0))
203            memcpy (new_line, line, size);
204
205          if (cmd < 0)
206            memcpy (line, new_line, size);
207          else
208            memcpy (line, cmds[cmd], size);
209         
210          col = strlen (line);
211         
212          if (output) {
213            fprintf(out,"\r%*c", clen, ' ');
214            fprintf(out,"\r%s%s", prompt, line);
215          }
216          else {
217            if (output)
218              fputc('\x7', out);
219          }
220        }
221        break;
222
223        case RTEMS_SHELL_KEYS_DEL:
224          if (line[col] != '\0')
225          {
226            int end;
227            int bs;
228            strcpy (&line[col], &line[col + 1]);
229            if (output) {
230              fprintf(out,"\r%s%s ", prompt, line);
231              end = (int) strlen (line);
232              for (bs = 0; bs < ((end - col) + 1); bs++)
233                fputc('\b', out);
234            }
235          }
236          break;
237
238        case RTEMS_SHELL_KEYS_INS:
239          inserting = inserting ? 0 : 1;
240          break;
241      }
242    }
243    else
244    {
245      switch (c)
246      {
247        case 1:/*Control-a*/
248          if (output) {
249            if (prompt)
250              fprintf(out,"\r%s", prompt);
251          }
252          col = 0;
253          break;
254         
255        case 5:/*Control-e*/
256          if (output)
257            fprintf(out,line + col);
258          col = (int) strlen (line);
259          break;
260
261        case 11:/*Control-k*/
262          if (line[col]) {
263            if (output) {
264              int end = strlen(line);
265              int bs;
266              fprintf(out,"%*c", end - col, ' ');
267              for (bs = 0; bs < (end - col); bs++)
268                fputc('\b', out);
269            }
270            line[col] = '\0';
271          }
272          break;
273         
274        case 0x04:/*Control-d*/
275          if (strlen(line))
276            break;
277        case EOF:
278          if (output)
279            fputc('\n', out);
280          return -2;
281       
282        case '\f':
283          if (output) {
284            int end;
285            int bs;
286            fputc('\f',out);
287            fprintf(out,"\r%s%s", prompt, line);
288            end = (int) strlen (line);
289            for (bs = 0; bs < (end - col); bs++)
290              fputc('\b', out);
291          }
292          break;
293
294        case '\b':
295        case '\x7e':
296        case '\x7f':
297          if (col > 0)
298          {
299            int bs;
300            col--;
301            strcpy (line + col, line + col + 1);
302            if (output) {
303              fprintf(out,"\b%s \b", line + col);
304              for (bs = 0; bs < ((int) strlen (line) - col); bs++)
305                fputc('\b', out);
306            }
307          }
308          break;
309
310        case '\n':
311        case '\r':
312        {
313          /*
314           * Process the command.
315           */
316          if (output)
317            fprintf(out,"\n");
318         
319          /*
320           * Only process the command if we have a command and it is not
321           * repeated in the history.
322           */
323          if (strlen(line) == 0) {
324            cmd = -1;
325          } else {
326            if ((cmd < 0) || (strcmp(line, cmds[cmd]) != 0)) {
327              if (count > 1)
328                memmove(cmds[1], cmds[0], (count - 1) * size);
329              memmove (cmds[0], line, size);
330              cmd = 0;
331            }
332          }
333        }
334        return cmd;
335
336        default:
337          if ((col < (size - 1)) && (c >= ' ') && (c <= 'z')) {
338            int end = strlen (line);
339            if (inserting && (col < end) && (end < size)) {
340              int ch, bs;
341              for (ch = end + 1; ch > col; ch--)
342                line[ch] = line[ch - 1];
343              if (output) {
344                fprintf(out, line + col);
345                for (bs = 0; bs < (end - col + 1); bs++)
346                  fputc('\b', out);
347              }
348            }
349            line[col++] = c;
350            if (col > end)
351              line[col] = '\0';
352            if (output)
353              fputc(c, out);
354          }
355          break;
356      }
357    }
358  }
359  return -2;
360}
361
362int rtems_shell_scanline(
363  char *line,
364  int   size,
365  FILE *in,
366  FILE *out
367)
368{
369  int c;
370  int col;
371  int doEcho;
372
373  doEcho = (out && isatty(fileno(in)));
374
375  col = 0;
376  if (*line) {
377    col = strlen(line);
378    if (doEcho) fprintf(out,"%s",line);
379  }
380  tcdrain(fileno(in));
381  if (out)
382    tcdrain(fileno(out));
383  for (;;) {
384    line[col] = 0;
385    c = fgetc(in);
386    switch (c) {
387      case EOF:
388        return 0;
389      case '\n':
390      case '\r':
391        if (doEcho)
392          fputc('\n',out);
393        return 1;
394      case  127:
395      case '\b':
396        if (col) {
397          if (doEcho) {
398            fputc('\b',out);
399            fputc(' ',out);
400            fputc('\b',out);
401          }
402          col--;
403        } else {
404          if (doEcho) fputc('\a',out);
405        }
406        break;
407     default:
408       if (!iscntrl(c)) {
409         if (col<size-1) {
410            line[col++] = c;
411            if (doEcho) fputc(c,out);
412          } else {
413            if (doEcho) fputc('\a',out);
414          }
415       } else {
416        if (doEcho)
417          if (c=='\a') fputc('\a',out);
418       }
419       break;
420     }
421  }
422}
423 
424/* ----------------------------------------------- *
425 * - The shell TASK
426 * Poor but enough..
427 * TODO: Redirection. Tty Signals. ENVVARs. Shell language.
428 * ----------------------------------------------- */
429
430void rtems_shell_init_issue(void) {
431  static char issue_inited=FALSE;
432  struct stat buf;
433
434  if (issue_inited)
435    return;
436  issue_inited = TRUE;
437
438  /* dummy call to init /etc dir */
439  getpwnam("root");
440
441  if (stat("/etc/issue",&buf)) {
442    rtems_shell_write_file("/etc/issue",
443                           "Welcome to @V\\n"
444                           "Login into @S\\n");
445  }
446
447  if (stat("/etc/issue.net",&buf)) {
448     rtems_shell_write_file("/etc/issue.net",
449                            "Welcome to %v\n"
450                            "running on %m\n");
451  }
452}
453
454int rtems_shell_login(FILE * in,FILE * out) {
455  FILE          *fd;
456  int            c;
457  time_t         t;
458  int            times;
459  char           name[128];
460  char           pass[128];
461  struct passwd *passwd;
462
463  rtems_shell_init_issue();
464  setuid(0);
465  setgid(0);
466  rtems_current_user_env->euid =
467  rtems_current_user_env->egid =0;
468
469  if (out) {
470    if ((rtems_current_shell_env->devname[5]!='p')||
471        (rtems_current_shell_env->devname[6]!='t')||
472        (rtems_current_shell_env->devname[7]!='y')) {
473      fd = fopen("/etc/issue","r");
474      if (fd) {
475        while ((c=fgetc(fd))!=EOF) {
476          if (c=='@')  {
477            switch(c=fgetc(fd)) {
478              case 'L':
479                fprintf(out,"%s",rtems_current_shell_env->devname);
480                break;
481              case 'B':
482                fprintf(out,"0");
483                break;
484              case 'T':
485              case 'D':
486                time(&t);
487                fprintf(out,"%s",ctime(&t));
488                break;
489              case 'S':
490                fprintf(out,"RTEMS");
491                break;
492              case 'V':
493                fprintf(out,"%s\n%s",_RTEMS_version, _Copyright_Notice);
494                break;
495              case '@':
496                fprintf(out,"@");
497                break;
498              default :
499                fprintf(out,"@%c",c);
500                break;
501            }
502          } else if (c=='\\')  {
503            switch(c=fgetc(fd)) {
504              case '\\': fprintf(out,"\\"); break;
505              case 'b':  fprintf(out,"\b"); break;
506              case 'f':  fprintf(out,"\f"); break;
507              case 'n':  fprintf(out,"\n"); break;
508              case 'r':  fprintf(out,"\r"); break;
509              case 's':  fprintf(out," ");  break;
510              case 't':  fprintf(out,"\t"); break;
511              case '@':  fprintf(out,"@");  break;
512            }
513          } else {
514            fputc(c,out);
515          }
516        }
517        fclose(fd);
518      }
519    } else {
520      fd = fopen("/etc/issue.net","r");
521      if (fd) {
522        while ((c=fgetc(fd))!=EOF) {
523          if (c=='%')  {
524            switch(c=fgetc(fd)) {
525              case 't':
526                fprintf(out,"%s",rtems_current_shell_env->devname);
527                break;
528              case 'h':
529                fprintf(out,"0");
530                break;
531              case 'D':
532                fprintf(out," ");
533                break;
534              case 'd':
535                time(&t);
536                fprintf(out,"%s",ctime(&t));
537                break;
538              case 's':
539                fprintf(out,"RTEMS");
540                break;
541              case 'm':
542                fprintf(out,"(" CPU_NAME "/" CPU_MODEL_NAME ")");
543                break;
544              case 'r':
545                fprintf(out,_RTEMS_version);
546                break;
547              case 'v':
548                fprintf(out,"%s\n%s",_RTEMS_version,_Copyright_Notice);
549                break;
550              case '%':fprintf(out,"%%");
551                break;
552              default:
553                fprintf(out,"%%%c",c);
554                break;
555            }
556          } else {
557            fputc(c,out);
558          }
559        }
560        fclose(fd);
561      }
562    }
563  }
564
565  times=0;
566  strcpy(name,"");
567  strcpy(pass,"");
568  for (;;) {
569    times++;
570    if (times>3) break;
571    if (out) fprintf(out,"\nlogin: ");
572    if (!rtems_shell_scanline(name,sizeof(name),in,out )) break;
573    if (out) fprintf(out,"Password: ");
574    if (!rtems_shell_scanline(pass,sizeof(pass),in,NULL)) break;
575    if (out) fprintf(out,"\n");
576    if ((passwd=getpwnam(name))) {
577      if (strcmp(passwd->pw_passwd,"!")) { /* valid user */
578        setuid(passwd->pw_uid);
579        setgid(passwd->pw_gid);
580        rtems_current_user_env->euid =
581        rtems_current_user_env->egid = 0;
582        chown(rtems_current_shell_env->devname,passwd->pw_uid,0);
583        rtems_current_user_env->euid = passwd->pw_uid;
584        rtems_current_user_env->egid = passwd->pw_gid;
585        if (!strcmp(passwd->pw_passwd,"*")) {
586          /* /etc/shadow */
587          return 0;
588        } else {
589          /* crypt() */
590          return 0;
591        }
592      }
593    }
594    if (out)
595      fprintf(out,"Login incorrect\n");
596    strcpy(name,"");
597    strcpy(pass,"");
598  }
599  return -1;
600}
601
602#if defined(SHELL_DEBUG)
603void rtems_shell_print_env(
604  rtems_shell_env_t * shell_env
605)
606{
607  if ( !shell_env ) {
608    printk( "shell_env is NULL\n" );
609    return;
610  }
611  printk( "shell_env=%p\n"
612    "shell_env->magic=0x%08x\t"
613    "shell_env->devname=%s\n"
614    "shell_env->taskname=%s\t"
615    "shell_env->tcflag=%d\n"
616    "shell_env->exit_shell=%d\t"
617    "shell_env->forever=%d\n",
618    shell_env->magic,
619    shell_env->devname,
620    ((shell_env->taskname) ? shell_env->taskname : "NOT SET"),
621    shell_env->tcflag,
622    shell_env->exit_shell,
623    shell_env->forever
624  );
625}
626#endif
627
628rtems_task rtems_shell_task(rtems_task_argument task_argument)
629{
630  rtems_shell_env_t *shell_env = (rtems_shell_env_t*) task_argument;
631  rtems_id           wake_on_end = shell_env->wake_on_end;
632  rtems_shell_main_loop( shell_env );
633  if (wake_on_end != RTEMS_INVALID_ID)
634    rtems_event_send (wake_on_end, RTEMS_EVENT_1);
635  rtems_task_delete( RTEMS_SELF );
636}
637
638void rtems_shell_get_prompt(
639  rtems_shell_env_t *shell_env,
640  char              *prompt,
641  int                size)
642{
643  char curdir[256];
644 
645  /* XXX: show_prompt user adjustable */
646  getcwd(curdir,sizeof(curdir));
647  snprintf(prompt, size - 1, "%s%s[%s] %c ",
648          ((shell_env->taskname) ? shell_env->taskname : ""),
649          ((shell_env->taskname) ? " " : ""),
650          curdir,
651          geteuid()?'$':'#');
652}
653
654#define RTEMS_SHELL_MAXIMUM_ARGUMENTS (128)
655#define RTEMS_SHELL_CMD_SIZE          (128)
656#define RTEMS_SHELL_CMD_COUNT         (32)
657#define RTEMS_SHELL_PROMPT_SIZE       (128)
658
659rtems_boolean rtems_shell_main_loop(
660  rtems_shell_env_t *shell_env_arg
661                                    )
662{
663  rtems_shell_env_t *shell_env;
664  rtems_shell_cmd_t *shell_cmd;
665  rtems_status_code  sc;
666  struct termios     term;
667  char              *prompt = NULL;
668  int                cmd;
669  int                cmd_count = 1; /* assume a script and so only 1 command line */
670  char              *cmds[RTEMS_SHELL_CMD_COUNT];
671  char              *cmd_argv;
672  int                argc;
673  char              *argv[RTEMS_SHELL_MAXIMUM_ARGUMENTS];
674  rtems_boolean      result = TRUE;
675  rtems_boolean      input_file = FALSE;
676  int                line = 0;
677  FILE              *stdinToClose = NULL;
678  FILE              *stdoutToClose = NULL;
679
680  memset(cmds, 0, sizeof(cmds));
681 
682  rtems_shell_initialize_command_set();
683
684  shell_env               =
685    rtems_current_shell_env = rtems_shell_init_env( shell_env_arg );
686 
687  /*
688   * @todo chrisj
689   * Remove the use of task variables. Change to have a single
690   * allocation per shell and then set into a notepad register
691   * in the TCB. Provide a function to return the pointer.
692   * Task variables are a virus to embedded systems software.
693   */
694  sc = rtems_task_variable_add(RTEMS_SELF,(void*)&rtems_current_shell_env,free);
695  if (sc != RTEMS_SUCCESSFUL) {
696    rtems_error(sc,"rtems_task_variable_add(current_shell_env):");
697    return FALSE;
698  }
699
700  setuid(0);
701  setgid(0);
702
703  rtems_current_user_env->euid = rtems_current_user_env->egid = 0;
704
705  fileno(stdout);
706
707  /* fprintf( stderr,
708     "-%s-%s-\n", shell_env->input, shell_env->output );
709  */
710
711  if (shell_env->output && strcmp(shell_env->output, "stdout") != 0) {
712    if (strcmp(shell_env->output, "stderr") == 0) {
713      stdout = stderr;
714    } else if (strcmp(shell_env->output, "/dev/null") == 0) {
715      fclose (stdout);
716    } else {
717      FILE *output = fopen(shell_env_arg->output,
718                           shell_env_arg->output_append ? "a" : "w");
719      if (!output) {
720        fprintf(stderr, "shell: open output %s failed: %s\n",
721                shell_env_arg->output, strerror(errno));
722        return FALSE;
723      }
724      stdout = output;
725      stdoutToClose = output;
726    }
727  }
728 
729  if (shell_env->input && strcmp(shell_env_arg->input, "stdin") != 0) {
730    FILE *input = fopen(shell_env_arg->input, "r");
731    if (!input) {
732      fprintf(stderr, "shell: open input %s failed: %s\n",
733              shell_env_arg->input, strerror(errno));
734      return FALSE;
735    }
736    stdin = input;
737    stdinToClose = input;
738    shell_env->forever = FALSE;
739    input_file = TRUE;
740  }
741  else {
742    /* make a raw terminal,Linux Manuals */
743    if (tcgetattr(fileno(stdin), &term) >= 0) {
744      term.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
745      term.c_oflag &= ~OPOST;
746      term.c_oflag |= (OPOST|ONLCR); /* But with cr+nl on output */
747      term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
748      if (shell_env->tcflag)
749        term.c_cflag = shell_env->tcflag;
750      term.c_cflag  |= CLOCAL | CREAD;
751      term.c_cc[VMIN]  = 1;
752      term.c_cc[VTIME] = 0;
753      if (tcsetattr (fileno(stdin), TCSADRAIN, &term) < 0) {
754        fprintf(stderr,
755                "shell:cannot set terminal attributes(%s)\n",shell_env->devname);
756      }
757    }
758    cmd_count = RTEMS_SHELL_CMD_COUNT;
759    prompt = malloc(RTEMS_SHELL_PROMPT_SIZE);
760    if (!prompt)
761        fprintf(stderr,
762                "shell:cannot allocate prompt memory\n");
763  }
764
765  setvbuf(stdin,NULL,_IONBF,0); /* Not buffered*/
766  setvbuf(stdout,NULL,_IONBF,0); /* Not buffered*/
767
768  rtems_shell_initialize_command_set();
769 
770  /*
771   * Allocate the command line buffers.
772   */
773  cmd_argv = malloc (RTEMS_SHELL_CMD_SIZE);
774  if (!cmd_argv) {
775    fprintf(stderr, "no memory for command line buffers\n" );
776  }
777 
778  cmds[0] = calloc (cmd_count, RTEMS_SHELL_CMD_SIZE);
779  if (!cmds[0]) {
780    fprintf(stderr, "no memory for command line buffers\n" );
781  }
782
783  if (cmd_argv && cmds[0]) {
784
785    memset (cmds[0], 0, cmd_count * RTEMS_SHELL_CMD_SIZE);
786
787    for (cmd = 1; cmd < cmd_count; cmd++) {
788      cmds[cmd] = cmds[cmd - 1] + RTEMS_SHELL_CMD_SIZE;
789    }
790   
791    do {
792      /* Set again root user and root filesystem, side effect of set_priv..*/
793      sc = rtems_libio_set_private_env();
794      if (sc != RTEMS_SUCCESSFUL) {
795        rtems_error(sc,"rtems_libio_set_private_env():");
796        result = FALSE;
797        break;
798      }
799      if (input_file || !rtems_shell_login(stdin,stdout))  {
800        const char *c;
801        memset (cmds[0], 0, cmd_count * RTEMS_SHELL_CMD_SIZE);
802        if (!input_file) {
803          rtems_shell_cat_file(stdout,"/etc/motd");
804          fprintf(stdout, "\n"
805                  "RTEMS SHELL (Ver.1.0-FRC):%s. " \
806                  __DATE__". 'help' to list commands.\n",
807                  shell_env->devname);
808        }
809       
810        chdir("/"); /* XXX: chdir to getpwent homedir */
811        shell_env->exit_shell = FALSE;
812
813        for (;;) {
814          int cmd;
815         
816          /* Prompt section */
817          if (prompt) {
818            rtems_shell_get_prompt(shell_env, prompt,
819                                   RTEMS_SHELL_PROMPT_SIZE);
820          }
821       
822          /* getcmd section */
823          cmd = rtems_shell_line_editor(cmds, cmd_count,
824                                        RTEMS_SHELL_CMD_SIZE, prompt,
825                                        stdin, stdout);
826
827          if (cmd == -1)
828            continue; /* empty line */
829         
830          if (cmd == -2)
831            break; /*EOF*/
832
833          line++;
834
835          /* evaluate cmd section */
836          c = cmds[cmd];
837          while (*c) {
838            if (!isblank(*c))
839              break;
840            c++;
841          }
842
843          if (*c == '\0')   /* empty line */
844            continue;
845
846          if (*c == '#') {  /* comment character */
847            cmds[cmd][0] = 0;
848            continue;
849          }
850
851          if (!strcmp(cmds[cmd],"bye") || !strcmp(cmds[cmd],"exit")) {
852            fprintf(stdout, "Shell exiting\n" );
853            break;
854          } else if (!strcmp(cmds[cmd],"shutdown")) { /* exit application */
855            fprintf(stdout, "System shutting down at user request\n" );
856            exit(0);
857          }
858
859          /* exec cmd section */
860          /* TODO:
861           *  To avoid user crash catch the signals.
862           *  Open a new stdio files with posibility of redirection *
863           *  Run in a new shell task background. (unix &)
864           *  Resuming. A little bash.
865           */
866          memcpy (cmd_argv, cmds[cmd], RTEMS_SHELL_CMD_SIZE);
867          if (!rtems_shell_make_args(cmd_argv, &argc, argv,
868                                     RTEMS_SHELL_MAXIMUM_ARGUMENTS)) {
869            shell_cmd = rtems_shell_lookup_cmd(argv[0]);
870            if ( argv[0] == NULL ) {
871              shell_env->errorlevel = -1;
872            } else if ( shell_cmd == NULL ) {
873              shell_env->errorlevel = rtems_shell_script_file(argc, argv);
874            } else {
875              shell_env->errorlevel = shell_cmd->command(argc, argv);
876            }
877          }
878
879          /* end exec cmd section */
880          if (shell_env->exit_shell)
881            break;
882        }
883
884        fflush( stdout );
885        fflush( stderr );
886      }
887    } while (result && shell_env->forever);
888
889  }
890
891  if (cmds[0])
892    free (cmds[0]);
893  if (cmd_argv)
894    free (cmd_argv);
895 
896  if ( stdinToClose )
897    fclose( stdinToClose );
898  if ( stdoutToClose )
899    fclose( stdoutToClose );
900  return result;
901}
902
903/* ----------------------------------------------- */
904static rtems_status_code   rtems_shell_run (
905  char                *task_name,
906  uint32_t             task_stacksize,
907  rtems_task_priority  task_priority,
908  char                *devname,
909  tcflag_t             tcflag,
910  int                  forever,
911  const char*          input,
912  const char*          output,
913  int                  output_append,
914  rtems_id             wake_on_end
915)
916{
917  rtems_id           task_id;
918  rtems_status_code  sc;
919  rtems_shell_env_t *shell_env;
920  rtems_name         name;
921
922  if ( task_name )
923    name = rtems_build_name(
924      task_name[0], task_name[1], task_name[2], task_name[3]);
925  else
926    name = rtems_build_name( 'S', 'E', 'N', 'V' );
927
928  sc = rtems_task_create(
929    name,
930    task_priority,
931    task_stacksize,
932    RTEMS_DEFAULT_MODES,
933    RTEMS_LOCAL | RTEMS_FLOATING_POINT,
934    &task_id
935  );
936  if (sc != RTEMS_SUCCESSFUL) {
937    rtems_error(sc,"creating task %s in shell_init()",task_name);
938    return sc;
939  }
940
941  shell_env = rtems_shell_init_env( NULL );
942  if ( !shell_env )  {
943   rtems_error(RTEMS_NO_MEMORY,
944               "allocating shell_env %s in shell_init()",task_name);
945   return RTEMS_NO_MEMORY;
946  }
947  shell_env->devname       = devname;
948  shell_env->taskname      = task_name;
949  shell_env->tcflag        = tcflag;
950  shell_env->exit_shell    = FALSE;
951  shell_env->forever       = forever;
952  shell_env->input         = strdup (input);
953  shell_env->output        = strdup (output);
954  shell_env->output_append = output_append;
955  shell_env->wake_on_end   = wake_on_end;
956
957  return rtems_task_start(task_id, rtems_shell_task,
958                          (rtems_task_argument) shell_env);
959}
960
961rtems_status_code   rtems_shell_init (
962  char                *task_name,
963  uint32_t             task_stacksize,
964  rtems_task_priority  task_priority,
965  char                *devname,
966  tcflag_t             tcflag,
967  int                  forever
968)
969{
970  return rtems_shell_run (task_name, task_stacksize, task_priority,
971                          devname, tcflag, forever,
972                          "stdin", "stdout", 0, RTEMS_INVALID_ID);
973}
974
975rtems_status_code   rtems_shell_script (
976  char                *task_name,
977  uint32_t             task_stacksize,
978  rtems_task_priority  task_priority,
979  const char*          input,
980  const char*          output,
981  int                  output_append,
982  int                  wait
983)
984{
985  rtems_id          current_task = RTEMS_INVALID_ID;
986  rtems_status_code sc;
987
988  if (wait) {
989    sc = rtems_task_ident (RTEMS_SELF, RTEMS_LOCAL, &current_task);
990    if (sc != RTEMS_SUCCESSFUL)
991      return sc;
992  }
993 
994  sc = rtems_shell_run (task_name, task_stacksize, task_priority,
995                        NULL, 0, 0, input, output, output_append,
996                        current_task);
997  if (sc != RTEMS_SUCCESSFUL)
998    return sc;
999
1000  if (wait) {
1001    rtems_event_set out;
1002    sc = rtems_event_receive (RTEMS_EVENT_1, RTEMS_WAIT, 0, &out);
1003  }
1004
1005  return sc;
1006}
Note: See TracBrowser for help on using the repository browser.