Changeset 55d9d8c in rtems


Ignore:
Timestamp:
04/14/20 06:29:53 (4 years ago)
Author:
Chris Johns <chrisj@…>
Branches:
4.11
Children:
a100457
Parents:
4d906d6a
git-author:
Chris Johns <chrisj@…> (04/14/20 06:29:53)
git-committer:
Chris Johns <chrisj@…> (04/14/20 08:48:32)
Message:

libmisc/shell: Updating joel script handling fixes from RTEMS 5

Updates #3877

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libmisc/shell/shell.c

    r4d906d6a r55d9d8c  
    4444
    4545#if SHELL_STD_DEBUG
    46 static FILE* default_stdout;
    47 #define shell_std_debug(...) fprintf(default_stdout, __VA_ARGS__)
     46#include <rtems/bspIo.h>
     47#define shell_std_debug(...) \
     48  do { printk("shell[%08x]: ", rtems_task_self()); printk(__VA_ARGS__); } while (0)
    4849#else
    4950#define shell_std_debug(...)
     
    125126      if ( shell_env->output )
    126127        free((void *)shell_env->output);
    127       free( ptr );
     128      free( shell_env );
    128129    }
    129130
     
    160161  struct passwd pwd;
    161162  struct passwd *pwd_res;
    162 
    163 #if SHELL_STD_DEBUG
    164   default_stdout = stdout;
    165 #endif
    166163
    167164  pthread_key_create(&rtems_shell_current_env_key, rtems_shell_env_free);
     
    190187
    191188/*
    192  * Create a current shell key.
     189 * Set the shell env into the current thread's shell key.
    193190 */
    194191static bool rtems_shell_set_shell_env(
     
    214211  eno = pthread_setspecific(rtems_shell_current_env_key, handle);
    215212  if (eno != 0) {
    216     rtems_error(0, "pthread_setspecific(shell_current_env_key)");
     213    rtems_error(0, "pthread_setspecific(shell_current_env_key): set");
    217214    return false;
    218215  }
    219216
    220217  return true;
     218}
     219
     220/*
     221 * Clear the current thread's shell key.
     222 */
     223static void rtems_shell_clear_shell_env(void)
     224{
     225  int eno;
     226
     227  /*
     228   * Run the destructor manually.
     229   */
     230  rtems_shell_env_free(pthread_getspecific(rtems_shell_current_env_key));
     231
     232  /*
     233   * Clear the key
     234   */
     235  eno = pthread_setspecific(rtems_shell_current_env_key, NULL);
     236  if (eno != 0)
     237    rtems_error(0, "pthread_setspecific(shell_current_env_key): clear");
     238
     239  /*
     240   * Clear stdin and stdout file pointers of they will be closed
     241   */
     242  stdin = NULL;
     243  stdout = NULL;
    221244}
    222245
     
    229252  handle = (rtems_shell_env_key_handle*)
    230253    pthread_getspecific(rtems_shell_current_env_key);
    231   return handle->env;
     254  return handle == NULL ? NULL : handle->env;
    232255}
    233256
     
    240263  rtems_shell_env_t *env = rtems_shell_get_current_env();
    241264  if (env != NULL) {
     265    shell_std_debug("dup: existing parent\n");
    242266    *copy = *env;
    243267  }
     
    250274    copy->parent_stdin  = stdin;
    251275    copy->parent_stderr = stderr;
     276    shell_std_debug("dup: global: copy: %p out: %d (%p) in: %d (%p)\n",
     277                    copy,
     278                    fileno(copy->parent_stdout), copy->parent_stdout,
     279                    fileno(copy->parent_stdin), copy->parent_stdin);
    252280  }
    253281  /*
     
    279307  int          cmd = -1;
    280308  int          inserting = 1;
    281 
    282   output = (out && isatty(fileno(in)));
     309  int          in_fileno = fileno(in);
     310  int          out_fileno = fileno(out);
     311
     312  /*
     313   * Only this task can use this file descriptor because calling
     314   * fileno will block if another thread call made a call on this
     315   * descriptor.
     316   */
     317  output = (out && isatty(in_fileno));
    283318
    284319  col = last_col = 0;
    285320
    286   tcdrain(fileno(in));
     321  tcdrain(in_fileno);
    287322  if (out)
    288     tcdrain(fileno(out));
     323    tcdrain(out_fileno);
    289324
    290325  if (output && prompt)
     
    632667      fd = fopen("/etc/issue","r");
    633668      if (fd) {
    634         while ((c=fgetc(fd))!=EOF) {
     669        while ((c = fgetc(fd)) != EOF) {
    635670          if (c=='@')  {
    636             switch(c=fgetc(fd)) {
     671            switch (c = fgetc(fd)) {
    637672              case 'L':
    638673                fprintf(out,"%s", env->devname);
     
    813848  if (!rtems_shell_init_user_env()) {
    814849    rtems_error(0, "rtems_shell_init_user_env");
     850    rtems_shell_clear_shell_env();
    815851    return false;
    816852  }
    817853
    818   shell_std_debug("shell: out: %d %d %s\n",
    819                   fileno(stdout), fileno(shell_env->parent_stdout),
    820                   shell_env->output);
    821   shell_std_debug("     :  in: %d %d %s\n",
    822                   fileno(stdin), fileno(shell_env->parent_stdin),
    823                   shell_env->input);
    824 
    825   if (shell_env->output != NULL) {
    826     if (strcmp(shell_env->output, "stdout") == 0) {
    827       if (shell_env->parent_stdout != NULL)
    828         stdout = shell_env->parent_stdout;
     854  shell_std_debug("env: %p\n", shell_env);
     855
     856  if (shell_env->output == NULL || strcmp(shell_env->output, "stdout") == 0) {
     857    if (shell_env->parent_stdout != NULL)
     858      stdout = shell_env->parent_stdout;
     859  }
     860  else if (strcmp(shell_env->output, "stderr") == 0) {
     861    if (shell_env->parent_stderr != NULL)
     862      stdout = shell_env->parent_stderr;
     863    else
     864      stdout = stderr;
     865  } else if (strcmp(shell_env->output, "/dev/null") == 0) {
     866    fclose (stdout);
     867  } else {
     868    FILE *output = fopen(shell_env->output,
     869                         shell_env->output_append ? "a" : "w");
     870    if (output == NULL) {
     871      fprintf(stderr, "shell: open output %s failed: %s\n",
     872              shell_env->output, strerror(errno));
     873      rtems_shell_clear_shell_env();
     874      return false;
    829875    }
    830     else if (strcmp(shell_env->output, "stderr") == 0) {
    831       if (shell_env->parent_stderr != NULL)
    832         stdout = shell_env->parent_stderr;
    833       else
    834         stdout = stderr;
    835     } else if (strcmp(shell_env->output, "/dev/null") == 0) {
    836       fclose (stdout);
    837     } else {
    838       FILE *output = fopen(shell_env->output,
    839                            shell_env->output_append ? "a" : "w");
    840       if (output == NULL) {
    841         fprintf(stderr, "shell: open output %s failed: %s\n",
    842                 shell_env->output, strerror(errno));
    843         return false;
    844       }
    845       stdout = output;
    846       stdoutToClose = output;
     876    stdout = output;
     877    stdoutToClose = output;
     878  }
     879
     880  if (shell_env->input == NULL || strcmp(shell_env->input, "stdin") == 0) {
     881    if (shell_env->parent_stdin != NULL)
     882      stdin = shell_env->parent_stdin;
     883  } else {
     884    FILE *input = fopen(shell_env->input, "r");
     885    if (input == NULL) {
     886      fprintf(stderr, "shell: open input %s failed: %s\n",
     887              shell_env->input, strerror(errno));
     888      if (stdoutToClose != NULL)
     889        fclose(stdoutToClose);
     890      rtems_shell_clear_shell_env();
     891      return false;
    847892    }
    848   }
    849 
    850   if (shell_env->input != NULL) {
    851     if (strcmp(shell_env->input, "stdin") == 0) {
    852       if (shell_env->parent_stdin != NULL)
    853         stdin = shell_env->parent_stdin;
    854     } else {
    855       FILE *input = fopen(shell_env->input, "r");
    856       if (input == NULL) {
    857         fprintf(stderr, "shell: open input %s failed: %s\n",
    858                 shell_env->input, strerror(errno));
    859         if (stdoutToClose != NULL)
    860           fclose(stdoutToClose);
    861         return false;
    862       }
    863       stdin = input;
    864       stdinToClose = input;
    865       shell_env->forever = false;
    866       input_file = true;
    867     }
     893    stdin = input;
     894    stdinToClose = input;
     895    shell_env->forever = false;
     896    input_file = true;
    868897  }
    869898
     
    881910      if (tcsetattr (fileno(stdin), TCSADRAIN, &term) < 0) {
    882911        fprintf(stderr,
    883                 "shell:cannot set terminal attributes(%s)\n",shell_env->devname);
     912                "shell: cannot set terminal attributes(%s)\n",shell_env->devname);
    884913      }
    885914    }
     
    888917    if (!prompt)
    889918        fprintf(stderr,
    890                 "shell:cannot allocate prompt memory\n");
    891   }
    892 
    893   shell_std_debug("shell: child out: %d in %d\n", fileno(stdout), fileno(stdin));
    894   shell_std_debug("shell: child out: %p\n", stdout);
     919                "shell: cannot allocate prompt memory\n");
     920  }
     921
     922  shell_std_debug("child out: %d (%p)\n", fileno(stdout), stdout);
     923  shell_std_debug("child  in: %d (%p)\n", fileno(stdin), stdin);
    895924
    896925   /* Do not buffer if interactive else leave buffered */
     
    9741003            continue; /* empty line */
    9751004
    976           if (cmd == -2)
     1005          if (cmd == -2) {
     1006            result = false;
    9771007            break; /*EOF*/
     1008          }
    9781009
    9791010          line++;
     
    10281059        fflush( stderr );
    10291060      }
     1061      shell_std_debug("end: %d %d\n", result, shell_env->forever);
    10301062    } while (result && shell_env->forever);
    10311063
     
    10381070  if (prompt)
    10391071    free (prompt);
     1072
     1073  shell_std_debug("child in-to-close: %p\n", stdinToClose);
     1074  shell_std_debug("child out-to-close: %p\n", stdoutToClose);
    10401075
    10411076  if (stdinToClose) {
     
    10521087  if ( stdoutToClose )
    10531088    fclose( stdoutToClose );
     1089  rtems_shell_clear_shell_env();
    10541090  return result;
    10551091}
     
    11051141  }
    11061142
     1143  shell_std_debug("run: env: %p\n", shell_env);
     1144
    11071145  shell_env->devname       = devname;
    11081146  shell_env->taskname      = task_name;
     
    11251163  getcwd(shell_env->cwd, sizeof(shell_env->cwd));
    11261164
     1165  shell_std_debug("run out: %d (%p)\n",
     1166                  fileno(shell_env->parent_stdout), shell_env->parent_stdout);
     1167  shell_std_debug("run  in: %d (%p)\n",
     1168                  fileno(shell_env->parent_stdin), shell_env->parent_stdin);
     1169
    11271170  sc = rtems_task_start(task_id, rtems_shell_task,
    11281171                        (rtems_task_argument) shell_env);
     
    11391182    sc = rtems_event_receive (RTEMS_EVENT_1, RTEMS_WAIT, 0, &out);
    11401183  }
     1184
     1185  shell_std_debug("run: end: sc:%d ec:%d\n", sc, *exit_code);
    11411186
    11421187  return sc;
     
    11911236  rtems_status_code sc;
    11921237
     1238  shell_std_debug("script: in: %s out: %s\n", input, output);
     1239
    11931240  if ( wait )
    11941241    to_wake = rtems_task_self();
    11951242
    1196   return rtems_shell_run(
     1243  sc = rtems_shell_run(
    11971244    task_name,       /* task_name */
    11981245    task_stacksize,  /* task_stacksize */
     
    12161263  }
    12171264
     1265  shell_std_debug("script: end: %d\n", sc);
     1266
    12181267  return sc;
    12191268}
Note: See TracChangeset for help on using the changeset viewer.