Changeset 55d9d8c in rtems
- Timestamp:
- 04/14/20 06:29:53 (4 years ago)
- 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)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpukit/libmisc/shell/shell.c
r4d906d6a r55d9d8c 44 44 45 45 #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) 48 49 #else 49 50 #define shell_std_debug(...) … … 125 126 if ( shell_env->output ) 126 127 free((void *)shell_env->output); 127 free( ptr);128 free( shell_env ); 128 129 } 129 130 … … 160 161 struct passwd pwd; 161 162 struct passwd *pwd_res; 162 163 #if SHELL_STD_DEBUG164 default_stdout = stdout;165 #endif166 163 167 164 pthread_key_create(&rtems_shell_current_env_key, rtems_shell_env_free); … … 190 187 191 188 /* 192 * Create a currentshell key.189 * Set the shell env into the current thread's shell key. 193 190 */ 194 191 static bool rtems_shell_set_shell_env( … … 214 211 eno = pthread_setspecific(rtems_shell_current_env_key, handle); 215 212 if (eno != 0) { 216 rtems_error(0, "pthread_setspecific(shell_current_env_key) ");213 rtems_error(0, "pthread_setspecific(shell_current_env_key): set"); 217 214 return false; 218 215 } 219 216 220 217 return true; 218 } 219 220 /* 221 * Clear the current thread's shell key. 222 */ 223 static 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; 221 244 } 222 245 … … 229 252 handle = (rtems_shell_env_key_handle*) 230 253 pthread_getspecific(rtems_shell_current_env_key); 231 return handle ->env;254 return handle == NULL ? NULL : handle->env; 232 255 } 233 256 … … 240 263 rtems_shell_env_t *env = rtems_shell_get_current_env(); 241 264 if (env != NULL) { 265 shell_std_debug("dup: existing parent\n"); 242 266 *copy = *env; 243 267 } … … 250 274 copy->parent_stdin = stdin; 251 275 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); 252 280 } 253 281 /* … … 279 307 int cmd = -1; 280 308 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)); 283 318 284 319 col = last_col = 0; 285 320 286 tcdrain( fileno(in));321 tcdrain(in_fileno); 287 322 if (out) 288 tcdrain( fileno(out));323 tcdrain(out_fileno); 289 324 290 325 if (output && prompt) … … 632 667 fd = fopen("/etc/issue","r"); 633 668 if (fd) { 634 while ((c =fgetc(fd))!=EOF) {669 while ((c = fgetc(fd)) != EOF) { 635 670 if (c=='@') { 636 switch (c=fgetc(fd)) {671 switch (c = fgetc(fd)) { 637 672 case 'L': 638 673 fprintf(out,"%s", env->devname); … … 813 848 if (!rtems_shell_init_user_env()) { 814 849 rtems_error(0, "rtems_shell_init_user_env"); 850 rtems_shell_clear_shell_env(); 815 851 return false; 816 852 } 817 853 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; 829 875 } 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; 847 892 } 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; 868 897 } 869 898 … … 881 910 if (tcsetattr (fileno(stdin), TCSADRAIN, &term) < 0) { 882 911 fprintf(stderr, 883 "shell: cannot set terminal attributes(%s)\n",shell_env->devname);912 "shell: cannot set terminal attributes(%s)\n",shell_env->devname); 884 913 } 885 914 } … … 888 917 if (!prompt) 889 918 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); 895 924 896 925 /* Do not buffer if interactive else leave buffered */ … … 974 1003 continue; /* empty line */ 975 1004 976 if (cmd == -2) 1005 if (cmd == -2) { 1006 result = false; 977 1007 break; /*EOF*/ 1008 } 978 1009 979 1010 line++; … … 1028 1059 fflush( stderr ); 1029 1060 } 1061 shell_std_debug("end: %d %d\n", result, shell_env->forever); 1030 1062 } while (result && shell_env->forever); 1031 1063 … … 1038 1070 if (prompt) 1039 1071 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); 1040 1075 1041 1076 if (stdinToClose) { … … 1052 1087 if ( stdoutToClose ) 1053 1088 fclose( stdoutToClose ); 1089 rtems_shell_clear_shell_env(); 1054 1090 return result; 1055 1091 } … … 1105 1141 } 1106 1142 1143 shell_std_debug("run: env: %p\n", shell_env); 1144 1107 1145 shell_env->devname = devname; 1108 1146 shell_env->taskname = task_name; … … 1125 1163 getcwd(shell_env->cwd, sizeof(shell_env->cwd)); 1126 1164 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 1127 1170 sc = rtems_task_start(task_id, rtems_shell_task, 1128 1171 (rtems_task_argument) shell_env); … … 1139 1182 sc = rtems_event_receive (RTEMS_EVENT_1, RTEMS_WAIT, 0, &out); 1140 1183 } 1184 1185 shell_std_debug("run: end: sc:%d ec:%d\n", sc, *exit_code); 1141 1186 1142 1187 return sc; … … 1191 1236 rtems_status_code sc; 1192 1237 1238 shell_std_debug("script: in: %s out: %s\n", input, output); 1239 1193 1240 if ( wait ) 1194 1241 to_wake = rtems_task_self(); 1195 1242 1196 returnrtems_shell_run(1243 sc = rtems_shell_run( 1197 1244 task_name, /* task_name */ 1198 1245 task_stacksize, /* task_stacksize */ … … 1216 1263 } 1217 1264 1265 shell_std_debug("script: end: %d\n", sc); 1266 1218 1267 return sc; 1219 1268 }
Note: See TracChangeset
for help on using the changeset viewer.