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

4.115
Last change on this file since fa028bb was fa028bb, checked in by Sebastian Huber <sebastian.huber@…>, on 11/17/14 at 10:55:27

shell: Do chroot() after successful login

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