source: rtems/cpukit/libmisc/shell/shell.c @ f3b9811

4.104.115
Last change on this file since f3b9811 was ff90595, checked in by Joel Sherrill <joel.sherrill@…>, on 09/03/08 at 18:57:51

2008-09-03 Cindy Cicalese <cicalese@…>

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