Changeset 611 for trunk/user/ksh
- Timestamp:
- Jan 9, 2019, 3:02:51 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/user/ksh/ksh.c
r610 r611 43 43 #include <signal.h> 44 44 #include <unistd.h> 45 #include <dirent.h> 45 46 #include <almosmkh.h> 46 47 #include <semaphore.h> … … 54 55 #define MAX_ARGS (32) // max number of arguments in a command 55 56 56 #define MAIN_DEBUG 0 57 58 #define CMD_CAT_DEBUG 0 59 #define CMD_CP_DEBUG 0 60 #define CMD_LOAD_DEBUG 0 57 #define DEBUG_MAIN 0 58 59 #define DEBUG_CMD_CAT 0 60 #define DEBUG_CMD_CP 0 61 #define DEBUG_CMD_LOAD 0 62 #define DEBUG_CMD_LS 0 61 63 62 64 ////////////////////////////////////////////////////////////////////////////////////////// … … 105 107 { 106 108 char * path; 107 st at_t st; // stat structure109 struct stat st; 108 110 int fd; 109 111 int size; … … 116 118 size = 0; 117 119 printf(" usage: cat pathname\n"); 118 gotoexit;120 goto cmd_cat_exit; 119 121 } 120 122 … … 127 129 buf = NULL; 128 130 size = 0; 129 printf(" error: cannot open %s\n", path);130 goto exit;131 } 132 133 #if CMD_CAT_DEBUG131 printf(" error: cannot open file <%s>\n", path); 132 goto cmd_cat_exit; 133 } 134 135 #if DEBUG_CMD_CAT 134 136 long long unsigned cycle; 135 137 get_cycle( &cycle ); … … 143 145 buf = NULL; 144 146 size = 0; 145 printf(" error: cannot stat %s\n", path);146 goto exit;147 printf(" error: cannot stat <%s>\n", path); 148 goto cmd_cat_exit; 147 149 } 148 150 … … 151 153 buf = NULL; 152 154 size = 0; 153 printf(" error: %sis a directory\n", path);154 goto exit;155 printf(" error: <%s> is a directory\n", path); 156 goto cmd_cat_exit; 155 157 } 156 158 … … 158 160 size = st.st_size; 159 161 160 #if CMD_CAT_DEBUG162 #if DEBUG_CMD_CAT 161 163 get_cycle( &cycle ); 162 164 printf("\n[%s] get size %d / cycle %d\n", … … 169 171 if ( buf == NULL ) 170 172 { 171 printf(" error: cannot map %s\n", path );172 goto exit;173 } 174 175 #if CMD_CAT_DEBUG173 printf(" error: cannot map file <%s>\n", path ); 174 goto cmd_cat_exit; 175 } 176 177 #if DEBUG_CMD_CAT 176 178 get_cycle( &cycle ); 177 179 printf("\n[%s] map file %d to buffer %x / cycle %d\n", … … 188 190 return; 189 191 190 exit:192 cmd_cat_exit: 191 193 192 194 if (buf != NULL) munmap(buf, size); … … 226 228 static void cmd_cp(int argc, char **argv) 227 229 { 228 int src_fd;229 int dst_fd;230 char * srcpath;231 char * dstpath;232 int size; // source file size233 int bytes; // number of transfered bytes234 char buf[4096];235 st at_tst;230 int src_fd; 231 int dst_fd; 232 char * srcpath; 233 char * dstpath; 234 int size; // source file size 235 int bytes; // number of transfered bytes 236 char buf[4096]; 237 struct stat st; 236 238 237 239 if (argc != 3) … … 240 242 dst_fd = -1; 241 243 printf(" usage: cp src_pathname dst_pathname\n"); 242 goto exit;244 goto cmd_cp_exit; 243 245 } 244 246 … … 252 254 { 253 255 dst_fd = -1; 254 printf(" error: cannot open %s\n", srcpath );255 goto exit;256 } 257 258 #if CMD_CP_DEBUG256 printf(" error: cannot open <%s>\n", srcpath ); 257 goto cmd_cp_exit; 258 } 259 260 #if DEBUG_CMD_CP 259 261 long long unsigned cycle; 260 262 get_cycle( &cycle ); … … 267 269 { 268 270 dst_fd = -1; 269 printf(" error: cannot stat %s\n", srcpath);270 goto exit;271 } 272 273 #if CMD_CP_DEBUG271 printf(" error: cannot stat <%s>\n", srcpath); 272 goto cmd_cp_exit; 273 } 274 275 #if DEBUG_CMD_CP 274 276 get_cycle( &cycle ); 275 277 printf("\n[%s] stats file <%s> done / cycle %d\n", … … 280 282 { 281 283 dst_fd = -1; 282 printf(" error: %sis a directory\n", srcpath);283 goto exit;284 printf(" error: <%s> is a directory\n", srcpath); 285 goto cmd_cp_exit; 284 286 } 285 287 … … 292 294 if ( dst_fd < 0 ) 293 295 { 294 printf(" error: cannot open %s\n", dstpath );295 goto exit;296 } 297 298 #if CMD_CP_DEBUG296 printf(" error: cannot open <%s>\n", dstpath ); 297 goto cmd_cp_exit; 298 } 299 300 #if DEBUG_CMD_CP 299 301 get_cycle( &cycle ); 300 302 printf("\n[%s] open file <%s> done / cycle %d\n", … … 304 306 if ( stat( dstpath , &st ) ) 305 307 { 306 printf(" error: cannot stat %s\n", dstpath );307 goto exit;308 } 309 310 #if CMD_CP_DEBUG308 printf(" error: cannot stat <%s>\n", dstpath ); 309 goto cmd_cp_exit; 310 } 311 312 #if DEBUG_CMD_CP 311 313 get_cycle( &cycle ); 312 314 printf("\n[%s] stats file <%s> done / cycle %d\n", … … 316 318 if ( S_ISDIR(st.st_mode ) ) 317 319 { 318 printf(" error: %sis a directory\n", dstpath );319 goto exit;320 printf(" error: <%s> is a directory\n", dstpath ); 321 goto cmd_cp_exit; 320 322 } 321 323 … … 329 331 if ( read( src_fd , buf , len ) != len ) 330 332 { 331 printf(" error: cannot read from file %s\n", srcpath);332 goto exit;333 printf(" error: cannot read from file <%s>\n", srcpath); 334 goto cmd_cp_exit; 333 335 } 334 336 335 #if CMD_CP_DEBUG337 #if DEBUG_CMD_CP 336 338 get_cycle( &cycle ); 337 339 printf("\n[%s] %d bytes read from <%s> / cycle %d\n", … … 342 344 if ( write( dst_fd , buf , len ) != len ) 343 345 { 344 printf(" error: cannot write to file %s\n", dstpath);345 goto exit;346 printf(" error: cannot write to file <%s>\n", dstpath); 347 goto cmd_cp_exit; 346 348 } 347 349 348 #if CMD_CP_DEBUG350 #if DEBUG_CMD_CP 349 351 get_cycle( &cycle ); 350 352 printf("\n[%s] %d bytes writen to <%s> / cycle %d\n", … … 355 357 } 356 358 357 exit:359 cmd_cp_exit: 358 360 359 361 if (src_fd >= 0) close(src_fd); … … 370 372 if( argc < 2 ) 371 373 { 372 printf(" usage: display vmm cxy pid \n" 373 " display sched cxy lid \n" 374 " display process cxy \n" 375 " display txt txtid \n" 376 " display vfs \n" 377 " display chdev \n" 378 " display dqdt \n" 379 " display locks pid trdid \n"); 374 printf(" usage: display vmm cxy pid\n" 375 " display sched cxy lid\n" 376 " display process cxy\n" 377 " display txt txtid\n" 378 " display vfs\n" 379 " display chdev\n" 380 " display dqdt\n" 381 " display locks pid trdid\n" 382 " display mapper path page_id nbytes\n"); 380 383 } 381 384 //////////////////////////////////// … … 500 503 { 501 504 printf(" error: illegal arguments pid = %x / trdid = %x\n", pid, trdid ); 505 } 506 } 507 } 508 /////////////////////////////////////////// 509 else if( strcmp( argv[1] , "mapper" ) == 0 ) 510 { 511 if( argc != 5 ) 512 { 513 printf(" usage: display mapper path page_id nbytes\n"); 514 } 515 else 516 { 517 unsigned int page_id = atoi(argv[3]); 518 unsigned int nbytes = atoi(argv[4]); 519 520 if( display_mapper( argv[2] , page_id, nbytes ) ) 521 { 522 printf(" error: cannot display page %d of mapper %s\n", page_id, argv[2] ); 502 523 } 503 524 } … … 643 664 ksh_pid = getpid(); 644 665 645 #if CMD_LOAD_DEBUG666 #if DEBUG_CMD_LOAD 646 667 long long unsigned cycle; 647 668 get_cycle( &cycle ); 648 printf("\n[ KSH] %s : ksh_pid %x / path %s / bg %d / place %d (%x) / cycle %d\n",669 printf("\n[ksh] %s : ksh_pid %x / path %s / bg %d / place %d (%x) / cycle %d\n", 649 670 __FUNCTION__, ksh_pid, argv[1], background, placement, cxy, (int)cycle ); 650 671 #endif … … 663 684 { 664 685 665 #if CMD_LOAD_DEBUG686 #if DEBUG_CMD_LOAD 666 687 get_cycle( &cycle ); 667 printf("\n[ KSH] %s : child_pid %x after fork, before exec / cycle %d\n",688 printf("\n[ksh] %s : child_pid %x after fork, before exec / cycle %d\n", 668 689 __FUNCTION__ , getpid(), (int)cycle ); 669 690 #endif … … 672 693 ret_exec = execve( pathname , NULL , NULL ); 673 694 674 #if CMD_LOAD_DEBUG695 #if DEBUG_CMD_LOAD 675 696 get_cycle( &cycle ); 676 printf("\n[ KSH] %s : child_pid %x after exec / ret_exec %d / cycle %d\n",697 printf("\n[ksh] %s : child_pid %x after exec / ret_exec %d / cycle %d\n", 677 698 __FUNCTION__ , getpid(), ret_exec, (int)cycle ); 678 699 #endif … … 688 709 { 689 710 690 #if CMD_LOAD_DEBUG711 #if DEBUG_CMD_LOAD 691 712 get_cycle( &cycle ); 692 printf("\n[ KSH] %s : ksh_pid %x after fork / ret_fork %x / cycle %d\n",713 printf("\n[ksh] %s : ksh_pid %x after fork / ret_fork %x / cycle %d\n", 693 714 __FUNCTION__, getpid(), ret_fork, (int)cycle ); 694 715 #endif … … 733 754 static void cmd_ls( int argc , char **argv ) 734 755 { 735 char * path; 736 737 // struct dirent * file; 738 // DIR *dir; 739 740 if (argc > 2 ) 756 char * pathname; 757 struct dirent * entry; 758 DIR * dir; 759 760 if (argc != 2 ) 741 761 { 742 762 printf(" usage: ls [path]\n"); … … 744 764 else 745 765 { 746 if ( argc == 1 ) path = "."; 747 else path = argv[1]; 748 749 printf(" error: not implemented yet\n"); 750 /* 751 dir = opendir( path ); 752 while ((file = readdir(dir)) != NULL) 753 { 754 printf(" %s\n", file->d_name); 755 } 756 closedir(dir); 757 */ 758 } 766 767 // handle case with no argument 768 // TODO if ( argc == 1 ) path = "."; 769 770 // get target directory path 771 pathname = argv[1]; 772 773 // open target directory 774 dir = opendir( pathname ); 775 776 #if DEBUG_CMD_LS 777 printf("\n[ksh] %s : directory <%s> open / DIR %x\n", 778 __FUNCTION__, pathname , dir ); 779 #endif 780 781 if( dir == NULL) 782 { 783 printf(" error : directory <%s> not found\n", pathname ); 784 goto cmd_ls_exit; 785 } 786 787 // loop on directory entries 788 while ( (entry = readdir(dir)) != NULL ) 789 { 790 printf(" - %s\n", entry->d_name); 791 } 792 793 // close target directory 794 closedir( dir ); 795 796 #if DEBUG_CMD_LS 797 printf("\n[ksh] %s : directory <%s> closed\n", 798 __FUNCTION__, pathname ); 799 #endif 800 801 } 802 803 cmd_ls_exit: 759 804 760 805 // release semaphore to get next command … … 887 932 if ( unlink( pathname ) ) 888 933 { 889 printf(" error: unable to remove %s\n", pathname );934 printf(" error: unable to remove <%s>\n", pathname ); 890 935 } 891 936 } … … 1048 1093 1049 1094 1050 / /To lauch one command without interactive mode1095 /* To lauch one command without interactive mode 1051 1096 1052 1097 if( sem_wait( &semaphore ) ) … … 1057 1102 else 1058 1103 { 1059 printf("\n[ksh] cp /home/Makefile /home/bloup\n");1104 printf("\n[ksh] ls bin/user\n"); 1060 1105 } 1061 1106 1062 strcpy( buf , " cp /home/Makefile /home/bloup" );1107 strcpy( buf , "ls bin/user" ); 1063 1108 parse( buf ); 1064 1109 1065 //1110 */ 1066 1111 1067 1112 enum fsm_states … … 1270 1315 get_core( &cxy , &lid ); 1271 1316 1272 #if MAIN_DEBUG1317 #if DEBUG_MAIN 1273 1318 printf("\n[ksh] main started on core[%x,%d]\n", cxy , lid ); 1274 1319 #endif … … 1281 1326 } 1282 1327 1283 #if MAIN_DEBUG1328 #if DEBUG_MAIN 1284 1329 printf("\n[ksh] main initialized semaphore\n" ); 1285 1330 #endif … … 1294 1339 &interactive, // entry function 1295 1340 NULL ); 1296 #if MAIN_DEBUG1341 #if DEBUG_MAIN 1297 1342 printf("\n[ksh] main launched interactive thread => wait children termination\n" ); 1298 1343 #endif … … 1307 1352 child_pid = wait( &status ); 1308 1353 1309 #if MAIN_DEBUG1354 #if DEBUG_MAIN 1310 1355 if( WIFEXITED (status) ) printf("\n[ksh] child process %x exit\n" , child_pid ); 1311 1356 if( WIFSIGNALED(status) ) printf("\n[ksh] child process %x killed\n" , child_pid );
Note: See TracChangeset
for help on using the changeset viewer.