Changeset 625 for trunk/user/ksh
- Timestamp:
- Apr 10, 2019, 10:09:39 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/user/ksh/ksh.c
r624 r625 54 54 #define LOG_DEPTH (32) // max number of registered commands 55 55 #define MAX_ARGS (32) // max number of arguments in a command 56 #define PATH_MAX_SIZE (256) // max number of characters in a pathname 56 57 57 58 #define DEBUG_MAIN 0 58 59 #define DEBUG_INTER 0 59 #define DEBUG_ PARSE060 #define DEBUG_EXECUTE 0 60 61 #define DEBUG_CMD_CAT 0 61 62 #define DEBUG_CMD_CP 0 … … 90 91 ////////////////////////////////////////////////////////////////////////////////////////// 91 92 92 ksh_cmd_t command[]; // array of supported commands 93 94 log_entry_t log_entries[LOG_DEPTH]; // array of registered commands 95 96 unsigned int ptw; // write pointer in log_entries[] 97 unsigned int ptr; // read pointer in log_entries[] 98 99 pthread_attr_t attr; // interactive thread attributes 100 101 sem_t semaphore; // block interactive thread when zero 102 103 pthread_t trdid; // interactive thread identifier 93 ksh_cmd_t command[]; // array of supported commands 94 95 log_entry_t log_entries[LOG_DEPTH]; // array of registered commands 96 97 unsigned int ptw; // write pointer in log_entries[] 98 unsigned int ptr; // read pointer in log_entries[] 99 100 pthread_attr_t attr; // interactive thread attributes 101 102 sem_t semaphore; // block interactive thread when zero 103 104 pthread_t trdid; // interactive thread identifier 105 106 char pathname[PATH_MAX_SIZE]; // pathname for a file 107 108 char pathnew[PATH_MAX_SIZE]; // used by the rename command 104 109 105 110 ////////////////////////////////////////////////////////////////////////////////////////// … … 110 115 static void cmd_cat( int argc , char **argv ) 111 116 { 112 char * path;113 117 struct stat st; 114 118 int fd; … … 128 132 } 129 133 130 path = argv[1];134 strcpy( pathname , argv[1] ); 131 135 132 136 // open the file 133 fd = open( path , O_RDONLY , 0 );137 fd = open( pathname , O_RDONLY , 0 ); 134 138 if (fd < 0) 135 139 { 136 printf(" error: cannot open file <%s>\n", path );140 printf(" error: cannot open file <%s>\n", pathname ); 137 141 138 142 sem_post( &semaphore ); … … 141 145 142 146 #if DEBUG_CMD_CAT 143 snprintf( string , 64 , "[ksh] %s : file %s open", __FUNCTION__, path );147 snprintf( string , 64 , "[ksh] %s : file %s open", __FUNCTION__, pathname ); 144 148 display_string( string ); 145 149 #endif 146 150 147 151 // get file stats 148 if ( stat( path , &st ) == -1)149 { 150 printf(" error: cannot stat <%s>\n", path );152 if ( stat( pathname , &st ) == -1) 153 { 154 printf(" error: cannot stat <%s>\n", pathname ); 151 155 152 156 close(fd); … … 157 161 if ( S_ISDIR(st.st_mode) ) 158 162 { 159 printf(" error: <%s> is a directory\n", path );163 printf(" error: <%s> is a directory\n", pathname ); 160 164 161 165 close(fd); … … 174 178 if( size == 0 ) 175 179 { 176 printf(" error: size = 0 for <%s>\n", path );180 printf(" error: size = 0 for <%s>\n", pathname ); 177 181 178 182 close(fd); … … 186 190 if ( buf == NULL ) 187 191 { 188 printf(" error: cannot map file <%s>\n", path );192 printf(" error: cannot map file <%s>\n", pathname ); 189 193 190 194 close(fd); … … 196 200 snprintf( string , 64 , "[ksh] %s : maped file %d to buffer %x", __FUNCTION__, fd , buf ); 197 201 display_string( string ); 198 // unsigned int pid = getpid();199 // unsigned int cxy = pid >> 16;200 // display_vmm( cxy , pid );201 202 #endif 202 203 … … 207 208 if( munmap( buf , size ) ) 208 209 { 209 printf(" error: cannot unmap file <%s>\n", path );210 printf(" error: cannot unmap file <%s>\n", pathname ); 210 211 } 211 212 … … 213 214 snprintf( string , 64 , "[ksh] %s : unmaped file %d from buffer %x", __FUNCTION__, fd , buf ); 214 215 display_string( string ); 215 // display_vmm( cxy , pid );216 216 #endif 217 217 … … 219 219 if( close( fd ) ) 220 220 { 221 printf(" error: cannot close file <%s>\n", path );221 printf(" error: cannot close file <%s>\n", pathname ); 222 222 } 223 223 … … 230 230 static void cmd_cd( int argc , char **argv ) 231 231 { 232 char * path;233 234 232 if (argc != 2) 235 233 { … … 238 236 else 239 237 { 240 path = argv[1];238 strcpy( pathname , argv[1] ); 241 239 242 240 // call the relevant syscall 243 if( chdir( path ) )244 { 245 printf(" error: cannot found <%s> directory\n", path );241 if( chdir( pathname ) ) 242 { 243 printf(" error: cannot found <%s> directory\n", pathname ); 246 244 } 247 245 } … … 257 255 int src_fd; 258 256 int dst_fd; 259 char * srcpath;260 char * dstpath;261 257 int size; // source file size 262 258 int bytes; // number of transfered bytes … … 276 272 } 277 273 278 srcpath = argv[1];279 dstpath = argv[2];280 281 274 // open the src file 282 src_fd = open( srcpath , O_RDONLY , 0 ); 275 strcpy( pathname , argv[1] ); 276 src_fd = open( pathname , O_RDONLY , 0 ); 283 277 284 278 if ( src_fd < 0 ) 285 279 { 286 280 dst_fd = -1; 287 printf(" error: cannot open <%s>\n", srcpath);281 printf(" error: cannot open <%s>\n", argv[1] ); 288 282 goto cmd_cp_exit; 289 283 } 290 284 291 285 #if DEBUG_CMD_CP 292 snprintf( string , 64 , "[ksh] %s : file %s open", __FUNCTION__, srcpath);286 snprintf( string , 64 , "[ksh] %s : file %s open", __FUNCTION__, argv[1] ); 293 287 display_string( string ); 294 288 #endif 295 289 296 290 // get file stats 297 if ( stat( srcpath, &st ) )291 if ( stat( pathname , &st ) ) 298 292 { 299 293 dst_fd = -1; 300 printf(" error: cannot stat <%s>\n", srcpath);294 printf(" error: cannot stat <%s>\n", argv[1] ); 301 295 goto cmd_cp_exit; 302 296 } 303 297 304 298 #if DEBUG_CMD_CP 305 snprintf( string , 64 , "[ksh] %s : got stats for %s", __FUNCTION__, srcpath);299 snprintf( string , 64 , "[ksh] %s : got stats for %s", __FUNCTION__, argv[1] ); 306 300 display_string( string ); 307 301 #endif … … 310 304 { 311 305 dst_fd = -1; 312 printf(" error: <%s> is a directory\n", srcpath);306 printf(" error: <%s> is a directory\n", argv[1] ); 313 307 goto cmd_cp_exit; 314 308 } … … 318 312 319 313 // open the dst file 320 dst_fd = open( dstpath , O_CREAT|O_TRUNC|O_RDWR , 0 ); 314 strcpy( pathname , argv[2] ); 315 dst_fd = open( pathname , O_CREAT|O_TRUNC|O_RDWR , 0 ); 321 316 322 317 if ( dst_fd < 0 ) 323 318 { 324 printf(" error: cannot open <%s>\n", dstpath);319 printf(" error: cannot open <%s>\n", argv[2] ); 325 320 goto cmd_cp_exit; 326 321 } 327 322 328 323 #if DEBUG_CMD_CP 329 snprintf( string , 64 , "[ksh] %s : file %s open", __FUNCTION__, dstpath);330 display_string( string ); 331 #endif 332 333 if ( stat( dstpath, &st ) )334 { 335 printf(" error: cannot stat <%s>\n", dstpath);324 snprintf( string , 64 , "[ksh] %s : file %s open", __FUNCTION__, argv[2] ); 325 display_string( string ); 326 #endif 327 328 if ( stat( pathname , &st ) ) 329 { 330 printf(" error: cannot stat <%s>\n", argv[2] ); 336 331 goto cmd_cp_exit; 337 332 } 338 333 339 334 #if DEBUG_CMD_CP 340 snprintf( string , 64 , "[ksh] %s : got stats for %s", __FUNCTION__, dstpath);335 snprintf( string , 64 , "[ksh] %s : got stats for %s", __FUNCTION__, argv[2] ); 341 336 display_string( string ); 342 337 #endif … … 344 339 if ( S_ISDIR(st.st_mode ) ) 345 340 { 346 printf(" error: <%s> is a directory\n", dstpath);341 printf(" error: <%s> is a directory\n", argv[2] ); 347 342 goto cmd_cp_exit; 348 343 } … … 357 352 if ( read( src_fd , buf , len ) != len ) 358 353 { 359 printf(" error: cannot read from file <%s>\n", srcpath);354 printf(" error: cannot read from file <%s>\n", argv[1] ); 360 355 goto cmd_cp_exit; 361 356 } 362 357 363 358 #if DEBUG_CMD_CP 364 snprintf( string , 64 , "[ksh] %s : read %d bytes from %s", __FUNCTION__, len, srcpath);359 snprintf( string , 64 , "[ksh] %s : read %d bytes from %s", __FUNCTION__, len, argv[1] ); 365 360 display_string( string ); 366 361 #endif … … 369 364 if ( write( dst_fd , buf , len ) != len ) 370 365 { 371 printf(" error: cannot write to file <%s>\n", dstpath);366 printf(" error: cannot write to file <%s>\n", argv[2] ); 372 367 goto cmd_cp_exit; 373 368 } 374 369 375 370 #if DEBUG_CMD_CP 376 snprintf( string , 64 , "[ksh] %s : write %d bytes to %s", __FUNCTION__, len, dstpath);371 snprintf( string , 64 , "[ksh] %s : write %d bytes to %s", __FUNCTION__, len, argv[2] ); 377 372 display_string( string ); 378 373 #endif … … 662 657 int ret_exec; // return value from exec 663 658 unsigned int ksh_pid; // KSH process PID 664 char * pathname; // path to .elf file665 659 unsigned int background; // background execution if non zero 666 660 unsigned int placement; // placement specified if non zero … … 677 671 else 678 672 { 679 pathname = argv[1];673 strcpy( pathname , argv[1] ); 680 674 681 675 if( argc == 2 ) … … 707 701 } 708 702 709 /*710 // take semaphore to block the interactive thread711 if ( sem_wait( &semaphore ) )712 {713 printf("\n[ksh error] cannot found semafore\n" );714 exit( 1 );715 }716 */717 703 // get KSH process PID 718 704 ksh_pid = getpid(); … … 767 753 display_string( string ); 768 754 #endif 755 // when the new process is launched in background, the KSH process 756 // takes the TXT ownership, and release the semaphore to get the next command. 757 // Otherwise, the child process keep the TXT ownership, and the semaphore will 758 // be released by the KSH main thread when the child process exit 769 759 770 760 if( background ) // KSH must keep TXT ownership … … 776 766 sem_post( &semaphore ); 777 767 } 778 else // KSH loosed TXT ownership779 {780 // semaphore will be released by the KSH main thread781 // when the loaded process exit782 }783 768 } 784 769 } … … 812 797 static void cmd_ls( int argc , char **argv ) 813 798 { 814 char * pathname = NULL;815 799 struct dirent * entry; 816 800 DIR * dir; … … 830 814 // get target directory path 831 815 if ( argc == 1 ) strcpy( pathname , "." ); 832 else pathname = argv[1];816 else strcpy( pathname , argv[1] ); 833 817 834 818 // open target directory … … 874 858 static void cmd_mkdir( int argc , char **argv ) 875 859 { 876 char * pathname;877 878 860 if (argc != 2) 879 861 { … … 882 864 else 883 865 { 884 pathname = argv[1];866 strcpy( pathname , argv[1] ); 885 867 886 868 mkdir( pathname , 0x777 ); … … 895 877 static void cmd_mv( int argc , char **argv ) 896 878 { 897 char * old_path;898 char * new_path;899 900 879 if (argc != 3) 901 880 { … … 904 883 else 905 884 { 906 old_path = argv[1];907 new_path = argv[2];885 strcpy( pathname , argv[1] ); 886 strcpy( pathnew , argv[2] ); 908 887 909 888 // call the relevant syscall 910 if( rename( old_path , new_path) )911 { 912 printf(" error: unable to rename <%s> to <%s>\n", old_path, new_path);889 if( rename( pathname , pathnew ) ) 890 { 891 printf(" error: unable to rename <%s> to <%s>\n", pathname , pathnew ); 913 892 } 914 893 } … … 967 946 static void cmd_pwd( int argc , char **argv ) 968 947 { 969 char buf[1024];970 971 948 if (argc != 1) 972 949 { … … 975 952 else 976 953 { 977 if ( getcwd( buf , 1024) )954 if ( getcwd( pathname , PATH_MAX_SIZE ) ) 978 955 { 979 956 printf(" error: unable to get current directory\n"); … … 981 958 else 982 959 { 983 printf("%s\n", buf);960 printf("%s\n", pathname ); 984 961 } 985 962 } … … 993 970 static void cmd_rm( int argc , char **argv ) 994 971 { 995 char * pathname;996 997 972 if (argc != 2) 998 973 { … … 1001 976 else 1002 977 { 1003 pathname = argv[1];978 strcpy( pathname , argv[1] ); 1004 979 1005 980 if ( unlink( pathname ) ) … … 1018 993 { 1019 994 // same as cmd_rm() 1020 cmd_rm (argc, argv);995 cmd_rm (argc , argv ); 1021 996 } 1022 997 … … 1103 1078 // This function analyses one command (with arguments), executes it, and returns. 1104 1079 //////////////////////////////////////////////////////////////////////////////////// 1105 static void __attribute__ ((noinline)) parse( char * buf )1080 static void __attribute__ ((noinline)) execute( char * buf ) 1106 1081 { 1107 1082 int argc = 0; … … 1110 1085 int len = strlen(buf); 1111 1086 1112 #if DEBUG_PARSE 1113 char string[64]; 1114 snprintf( string , 64 , "\n[ksh] %s : <%s>", __FUNCTION__ , buf ); 1115 display_string( string ); 1087 #if DEBUG_EXECUTE 1088 printf("\n[ksh] %s : command <%s>\n", 1089 __FUNCTION__ , buf ); 1116 1090 #endif 1117 1091 … … 1134 1108 } 1135 1109 1136 #if DEBUG_PARSE 1137 snprintf( string , 64 , "\n[ksh] %s : argc = %d for <%s>", __FUNCTION__ , argc , argv[0] ); 1138 display_string( string ); 1139 #endif 1140 1141 // analyse command type 1142 if (argc > 0) 1143 { 1144 int found = 0; 1145 1146 // try to match typed command 1147 for ( i = 0 ; command[i].name ; i++ ) 1148 { 1149 if (strcmp(argv[0], command[i].name) == 0) 1150 { 1151 command[i].fn(argc, argv); 1152 found = 1; 1153 break; 1154 } 1110 // check command 1111 if (argc == 0) 1112 { 1113 // release semaphore to get next command 1114 sem_post( &semaphore ); 1115 } 1116 1117 #if DEBUG_EXECUTE 1118 printf("\n[ksh] %s : argc %d / arg0 %s / arg1 %s\n", 1119 __FUNCTION__ , argc , argv[0], argv[1] ); 1120 #endif 1121 1122 // scan the list of commands to match typed command 1123 int found = 0; 1124 for ( i = 0 ; (command[i].name != NULL) && (found == 0) ; i++ ) 1125 { 1126 if (strcmp(argv[0], command[i].name) == 0) 1127 { 1128 command[i].fn(argc, argv); 1129 found = 1; 1155 1130 } 1156 1157 if (!found) // undefined command 1158 { 1159 printf(" error : undefined command <%s>\n", argv[0]); 1160 1161 // release semaphore to get next command 1162 sem_post( &semaphore ); 1163 } 1164 } 1165 } // end parse() 1131 } 1132 1133 // check undefined command 1134 if (!found) 1135 { 1136 printf(" error : undefined command <%s>\n", argv[0]); 1137 1138 // release semaphore to get next command 1139 sem_post( &semaphore ); 1140 } 1141 } // end execute() 1166 1142 1167 1143 /////////////////////////////// … … 1177 1153 1178 1154 #if DEBUG_INTER 1179 char string[64]; 1180 #endif 1181 1182 /* To lauch one command without interactive mode 1155 char string[128]; 1156 #endif 1157 1158 /* 1159 // To lauch one or several commands without interactive mode 1160 1161 // 1. first command 1183 1162 if( sem_wait( &semaphore ) ) 1184 1163 { … … 1188 1167 else 1189 1168 { 1190 printf("\n[ksh] load bin/user/ sort.elf\n");1169 printf("\n[ksh] load bin/user/pgcd.elf\n"); 1191 1170 } 1192 1171 1193 strcpy( cmd , "load bin/user/sort.elf" ); 1194 parse( cmd ); 1172 strcpy( cmd , "load bin/user/pgcd.elf" ); 1173 execute( cmd ); 1174 1175 // 2. second command 1176 if( sem_wait( &semaphore ) ) 1177 { 1178 printf("\n[ksh error] cannot found semafore\n" ); 1179 exit( 1 ); 1180 } 1181 else 1182 { 1183 printf("\n[ksh] ls home\n"); 1184 } 1185 1186 strcpy( cmd , "ls home" ); 1187 execute( cmd ); 1188 1189 // end non-interactive mode 1195 1190 */ 1196 1191 … … 1227 1222 { 1228 1223 // initialize command buffer 1229 memset( cmd, 0x20 , sizeof(cmd) ); // TODO useful ?1224 // memset( cmd , 0x20 , sizeof(cmd) ); // TODO useful ? 1230 1225 count = 0; 1231 1226 state = NORMAL; … … 1234 1229 #if DEBUG_INTER 1235 1230 unsigned int pid = getpid(); 1236 snprintf( string , 64, "\n[ksh] %s : request a new command", __FUNCTION__ );1231 snprintf( string , 128 , "\n[ksh] %s : request a new command", __FUNCTION__ ); 1237 1232 display_string( string ); 1238 1233 #endif … … 1263 1258 cmd[count] = 0; 1264 1259 count++; 1265 1266 // register command in log 1260 #if DEBUG_INTER 1261 snprintf( string , 128 , "[ksh] %s : get command <%s> / &log = %x / ptw = %d / &ptw = %x", 1262 __FUNCTION__, cmd , log_entries[ptw].buf , ptw , &ptw ); 1263 display_string( string ); 1264 display_vmm( 0 , 2 ); 1265 #endif 1266 // register command in log_entries[] array 1267 1267 strncpy( log_entries[ptw].buf , cmd , count ); 1268 1268 log_entries[ptw].count = count; … … 1271 1271 1272 1272 #if DEBUG_INTER 1273 snprintf( string , 64 , "[ksh] %s : parse andexecute <%s>", __FUNCTION__, cmd );1273 snprintf( string , 128 , "[ksh] %s : execute <%s>", __FUNCTION__, cmd ); 1274 1274 display_string( string ); 1275 1275 #endif … … 1277 1277 putchar( c ); 1278 1278 1279 // call parser to analyse andexecute command1280 parse( cmd );1279 // execute command 1280 execute( cmd ); 1281 1281 } 1282 1282 else // no command registered … … 1391 1391 1392 1392 #if DEBUG_INTER 1393 snprintf( string , 64, "\n[ksh] %s : complete <%s> command", __FUNCTION__, cmd );1393 snprintf( string , 128 , "\n[ksh] %s : complete <%s> command", __FUNCTION__, cmd ); 1394 1394 display_string( string ); 1395 1395 #endif
Note: See TracChangeset
for help on using the changeset viewer.