| 1 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 2 | // File   :  ksh.c | 
|---|
| 3 | // Date   :  October 2017 | 
|---|
| 4 | // Author :  Alain Greiner | 
|---|
| 5 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 6 | // This application implements a minimal shell for ALMOS-MKH. | 
|---|
| 7 | // | 
|---|
| 8 | // This user KSH process contains two POSIX threads: | 
|---|
| 9 | // - the "main" thread contains the infinite loop implementing | 
|---|
| 10 | //   the children processes termination monitoring using the wait syscall. | 
|---|
| 11 | // - the "interactive" thread contains the infinite loop implementing | 
|---|
| 12 | //   the command interpreter attached to the TXT terminal. | 
|---|
| 13 | //   This "interactive" thread block and deschedules when the KSH process | 
|---|
| 14 | //   loses the TXT terminal ownership. It is reactivated when the KSH | 
|---|
| 15 | //   process returns in background. | 
|---|
| 16 | // | 
|---|
| 17 | // Note: the children processes are created by the <load> command, and are | 
|---|
| 18 | // attached to the same TXT terminal as the KSH process itself. | 
|---|
| 19 | // . A child process can be lauched in foreground: the KSH process loses | 
|---|
| 20 | //   the TXT terminal ownership, that is transfered to the new process. | 
|---|
| 21 | // . A child process can be lauched in background: the KSH process keeps | 
|---|
| 22 | //   the TXT terminal ownership. that is transfered to the new process. | 
|---|
| 23 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 24 |  | 
|---|
| 25 | #include <stdio.h> | 
|---|
| 26 | #include <stdlib.h> | 
|---|
| 27 | #include <string.h> | 
|---|
| 28 | #include <sys/wait.h> | 
|---|
| 29 | #include <signal.h> | 
|---|
| 30 | #include <unistd.h> | 
|---|
| 31 | #include <almosmkh.h> | 
|---|
| 32 | #include <semaphore.h> | 
|---|
| 33 |  | 
|---|
| 34 | #define CMD_MAX_SIZE   (256)    // max number of characters in one command | 
|---|
| 35 | #define LOG_DEPTH      (32)     // max number of registered commands  | 
|---|
| 36 | #define MAX_ARGS           (32)     // max number of arguments in a command | 
|---|
| 37 | #define FIFO_SIZE      (1024)   // FIFO depth for recursive ls | 
|---|
| 38 |  | 
|---|
| 39 | #define KSH_DEBUG      0 | 
|---|
| 40 |  | 
|---|
| 41 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 42 | //         Structures | 
|---|
| 43 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 44 |  | 
|---|
| 45 | // one entry in the registered commands array | 
|---|
| 46 | typedef struct log_entry_s  | 
|---|
| 47 | { | 
|---|
| 48 |         char          buf[CMD_MAX_SIZE]; | 
|---|
| 49 |         unsigned int  count; | 
|---|
| 50 | } | 
|---|
| 51 | log_entry_t; | 
|---|
| 52 |  | 
|---|
| 53 | // one entry in the supported command types array | 
|---|
| 54 | typedef struct ksh_cmd_s  | 
|---|
| 55 | { | 
|---|
| 56 |         char * name; | 
|---|
| 57 |         char * desc; | 
|---|
| 58 |         void   (*fn)( int , char ** ); | 
|---|
| 59 | } | 
|---|
| 60 | ksh_cmd_t; | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 64 | //         Global Variables | 
|---|
| 65 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 66 |  | 
|---|
| 67 | ksh_cmd_t       cmd[];                    // array of supported commands | 
|---|
| 68 |  | 
|---|
| 69 | log_entry_t     log_entries[LOG_DEPTH];   // array of registered commands | 
|---|
| 70 |  | 
|---|
| 71 | unsigned int    ptw;                      // write pointer in log_entries[] | 
|---|
| 72 | unsigned int    ptr;                      // read pointer in log_entries[] | 
|---|
| 73 |  | 
|---|
| 74 | pthread_attr_t  attr;                     // interactive thread attributes | 
|---|
| 75 |  | 
|---|
| 76 | sem_t           semaphore;                // block interactive thread when zero | 
|---|
| 77 |  | 
|---|
| 78 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 79 | //         Shell  Commands | 
|---|
| 80 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 81 |  | 
|---|
| 82 | ///////////////////////////////////////////// | 
|---|
| 83 | static void cmd_cat( int argc , char **argv ) | 
|---|
| 84 | { | 
|---|
| 85 |         char         * path; | 
|---|
| 86 |  | 
|---|
| 87 |         if (argc != 2)  | 
|---|
| 88 |     { | 
|---|
| 89 |                 printf("  usage: cat pathname\n"); | 
|---|
| 90 |                 return; | 
|---|
| 91 |         } | 
|---|
| 92 |  | 
|---|
| 93 |         path = argv[1]; | 
|---|
| 94 |  | 
|---|
| 95 |     printf("  error: not implemented yet\n"); | 
|---|
| 96 |  | 
|---|
| 97 | /* | 
|---|
| 98 |         // open the file | 
|---|
| 99 |         fd = open( path , O_RDONLY , 0 ); | 
|---|
| 100 |         if (fd < 0)  | 
|---|
| 101 |     { | 
|---|
| 102 |                 printf("  error: cannot open %s\n", path); | 
|---|
| 103 |                 goto exit; | 
|---|
| 104 |         } | 
|---|
| 105 |  | 
|---|
| 106 |         // get file size  | 
|---|
| 107 |         if (stat(path, &st) == -1) | 
|---|
| 108 |     { | 
|---|
| 109 |                 printf("  error: cannot stat %s\n", path); | 
|---|
| 110 |                 goto exit; | 
|---|
| 111 |         } | 
|---|
| 112 |         if (S_ISDIR(st.st_mode)) { | 
|---|
| 113 |                 printf("  error: %s is a directory\n", path); | 
|---|
| 114 |                 goto exit; | 
|---|
| 115 |         } | 
|---|
| 116 |         size = st.st_size; | 
|---|
| 117 |  | 
|---|
| 118 |         // mmap the file  | 
|---|
| 119 |         buf = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); | 
|---|
| 120 |         if (buf == NULL || buf == (char *)-1) { | 
|---|
| 121 |                 printf("  error: cannot map %s\n", path); | 
|---|
| 122 |                 goto exit; | 
|---|
| 123 |         } | 
|---|
| 124 |  | 
|---|
| 125 |         // set terminating '0' | 
|---|
| 126 |         buf[size-1] = 0; | 
|---|
| 127 |  | 
|---|
| 128 |         // display the file content | 
|---|
| 129 |         printf("%s", buf); | 
|---|
| 130 |  | 
|---|
| 131 | exit: | 
|---|
| 132 |         if (buf != NULL) munmap(buf, size); | 
|---|
| 133 |         if (fd >= 0) close(fd); | 
|---|
| 134 | */ | 
|---|
| 135 |  | 
|---|
| 136 |     // release semaphore to get next command | 
|---|
| 137 |     sem_post( &semaphore ); | 
|---|
| 138 |  | 
|---|
| 139 | }   // end cmd_cat() | 
|---|
| 140 |  | 
|---|
| 141 | //////////////////////////////////////////// | 
|---|
| 142 | static void cmd_cd( int argc , char **argv ) | 
|---|
| 143 | { | 
|---|
| 144 |         char * path; | 
|---|
| 145 |  | 
|---|
| 146 |         if (argc != 2) | 
|---|
| 147 |     { | 
|---|
| 148 |                 printf("  usage: cd pathname\n"); | 
|---|
| 149 |                 return; | 
|---|
| 150 |         } | 
|---|
| 151 |  | 
|---|
| 152 |         path = argv[1]; | 
|---|
| 153 |  | 
|---|
| 154 |     printf("  error: not implemented yet\n"); | 
|---|
| 155 |  | 
|---|
| 156 |     // release semaphore to get next command | 
|---|
| 157 |     sem_post( &semaphore ); | 
|---|
| 158 |  | 
|---|
| 159 | }   // end cmd_cd() | 
|---|
| 160 |  | 
|---|
| 161 | ///////////////////////////////////////// | 
|---|
| 162 | static void cmd_cp(int argc, char **argv) | 
|---|
| 163 | { | 
|---|
| 164 | //      int src_fd = -1, dst_fd = -1; | 
|---|
| 165 | //      char *srcpath, *dstpath; | 
|---|
| 166 | //      struct stat st; | 
|---|
| 167 | //      size_t size, i; | 
|---|
| 168 | //      char buf[1024]; | 
|---|
| 169 |  | 
|---|
| 170 |         if (argc != 3)  | 
|---|
| 171 |     { | 
|---|
| 172 |                 printf("  usage: cp src_pathname dst_pathname\n"); | 
|---|
| 173 |                 return; | 
|---|
| 174 |         } | 
|---|
| 175 |  | 
|---|
| 176 |     printf("  error: not implemented yet\n"); | 
|---|
| 177 |  | 
|---|
| 178 | /* | 
|---|
| 179 |         srcpath = argv[1]; | 
|---|
| 180 |         dstpath = argv[2]; | 
|---|
| 181 |  | 
|---|
| 182 |         // open the src file  | 
|---|
| 183 |         src_fd = open(srcpath, O_RDONLY, 0); | 
|---|
| 184 |         if (src_fd < 0) { | 
|---|
| 185 |                 printf("  error: cannot open %s / err = %d\n", srcpath, errno); | 
|---|
| 186 |                 goto exit; | 
|---|
| 187 |         } | 
|---|
| 188 |  | 
|---|
| 189 |         // get file size  | 
|---|
| 190 |         if (stat(srcpath, &st) == -1) { | 
|---|
| 191 |                 printf("  error: cannot stat %s\n", srcpath); | 
|---|
| 192 |                 goto exit; | 
|---|
| 193 |         } | 
|---|
| 194 |         if (S_ISDIR(st.st_mode)) { | 
|---|
| 195 |                 printf("  error: %s is a directory\n", srcpath); | 
|---|
| 196 |                 goto exit; | 
|---|
| 197 |         } | 
|---|
| 198 |         size = st.st_size; | 
|---|
| 199 |  | 
|---|
| 200 |         // open the dst file  | 
|---|
| 201 |         dst_fd = open(dstpath, O_CREAT|O_TRUNC|O_RDWR, 0); | 
|---|
| 202 |         if (dst_fd < 0) { | 
|---|
| 203 |                 printf("  error: cannot open %s / err = %d\n", dstpath, errno); | 
|---|
| 204 |                 goto exit; | 
|---|
| 205 |         } | 
|---|
| 206 |         if (stat(dstpath, &st) == -1) { | 
|---|
| 207 |                 printf("  error: cannot stat %s\n", dstpath); | 
|---|
| 208 |                 goto exit; | 
|---|
| 209 |         } | 
|---|
| 210 |         if (S_ISDIR(st.st_mode)) { | 
|---|
| 211 |                 printf("  error: %s is a directory\n", dstpath); | 
|---|
| 212 |                 goto exit; | 
|---|
| 213 |         } | 
|---|
| 214 |  | 
|---|
| 215 |         i = 0; | 
|---|
| 216 |         while (i < size) | 
|---|
| 217 |         { | 
|---|
| 218 |                 size_t rlen = (size - i < 1024 ? size - i : 1024); | 
|---|
| 219 |                 size_t wlen; | 
|---|
| 220 |                 ssize_t ret; | 
|---|
| 221 |  | 
|---|
| 222 |                 // read the source  | 
|---|
| 223 |                 ret = read(src_fd, buf, rlen); | 
|---|
| 224 |                 if (ret == -1) { | 
|---|
| 225 |                         printf("  error: cannot read from file %s\n", srcpath); | 
|---|
| 226 |                         goto exit; | 
|---|
| 227 |                 } | 
|---|
| 228 |                 rlen = (size_t)ret; | 
|---|
| 229 |  | 
|---|
| 230 |                 // write to the destination  | 
|---|
| 231 |                 ret = write(dst_fd, buf, rlen); | 
|---|
| 232 |                 if (ret == -1) { | 
|---|
| 233 |                         printf("  error: cannot write to file %s\n", dstpath); | 
|---|
| 234 |                         goto exit; | 
|---|
| 235 |                 } | 
|---|
| 236 |                 wlen = (size_t)ret; | 
|---|
| 237 |  | 
|---|
| 238 |                 // check  | 
|---|
| 239 |                 if (wlen != rlen) { | 
|---|
| 240 |                         printf("  error: cannot write on device\n"); | 
|---|
| 241 |                         goto exit; | 
|---|
| 242 |                 } | 
|---|
| 243 |  | 
|---|
| 244 |                 i += rlen; | 
|---|
| 245 |         } | 
|---|
| 246 |  | 
|---|
| 247 | exit: | 
|---|
| 248 |         if (src_fd >= 0) close(src_fd); | 
|---|
| 249 |         if (dst_fd >= 0) close(dst_fd); | 
|---|
| 250 | */ | 
|---|
| 251 |  | 
|---|
| 252 |     // release semaphore to get next command | 
|---|
| 253 |     sem_post( &semaphore ); | 
|---|
| 254 |  | 
|---|
| 255 | }   // end cmd_cp() | 
|---|
| 256 |  | 
|---|
| 257 | ///////////////////////////////////////////////// | 
|---|
| 258 | static void cmd_display( int argc , char **argv ) | 
|---|
| 259 | { | 
|---|
| 260 |     unsigned int  cxy; | 
|---|
| 261 |     unsigned int  lid; | 
|---|
| 262 |     unsigned int  pid; | 
|---|
| 263 |     unsigned int  txt_id; | 
|---|
| 264 |  | 
|---|
| 265 |     if( strcmp( argv[1] , "vmm" ) == 0 ) | 
|---|
| 266 |     { | 
|---|
| 267 |         if( argc != 4 ) | 
|---|
| 268 |         { | 
|---|
| 269 |                     printf("  usage: display vmm cxy pid\n"); | 
|---|
| 270 |                     return; | 
|---|
| 271 |             } | 
|---|
| 272 |  | 
|---|
| 273 |             cxy = atoi(argv[2]); | 
|---|
| 274 |             pid = atoi(argv[3]); | 
|---|
| 275 |  | 
|---|
| 276 |         if( display_vmm( cxy , pid ) ) | 
|---|
| 277 |         { | 
|---|
| 278 |             printf("  error: no process %x in cluster %x\n", pid , cxy ); | 
|---|
| 279 |         } | 
|---|
| 280 |     } | 
|---|
| 281 |     else if( strcmp( argv[1] , "sched" ) == 0 ) | 
|---|
| 282 |     { | 
|---|
| 283 |         if( argc != 4 ) | 
|---|
| 284 |         { | 
|---|
| 285 |                     printf("  usage: display sched cxy lid\n"); | 
|---|
| 286 |                     return; | 
|---|
| 287 |             } | 
|---|
| 288 |  | 
|---|
| 289 |             cxy = atoi(argv[2]); | 
|---|
| 290 |             lid = atoi(argv[3]); | 
|---|
| 291 |  | 
|---|
| 292 |         if( display_sched( cxy , lid ) ) | 
|---|
| 293 |         { | 
|---|
| 294 |             printf("  error: illegal arguments cxy = %x / lid = %d\n", cxy, lid ); | 
|---|
| 295 |         } | 
|---|
| 296 |     } | 
|---|
| 297 |     else if( strcmp( argv[1] , "process" ) == 0 ) | 
|---|
| 298 |     { | 
|---|
| 299 |         if( argc != 3 ) | 
|---|
| 300 |         { | 
|---|
| 301 |                     printf("  usage: display process cxy\n"); | 
|---|
| 302 |                     return; | 
|---|
| 303 |             } | 
|---|
| 304 |  | 
|---|
| 305 |             cxy = atoi(argv[2]); | 
|---|
| 306 |  | 
|---|
| 307 |         if( display_cluster_processes( cxy ) ) | 
|---|
| 308 |         { | 
|---|
| 309 |             printf("  error: illegal argument cxy = %x\n", cxy ); | 
|---|
| 310 |         } | 
|---|
| 311 |     } | 
|---|
| 312 |     else if( strcmp( argv[1] , "txt" ) == 0 ) | 
|---|
| 313 |     { | 
|---|
| 314 |         if( argc != 3 ) | 
|---|
| 315 |         { | 
|---|
| 316 |                     printf("  usage: display txt txt_id\n"); | 
|---|
| 317 |                     return; | 
|---|
| 318 |             } | 
|---|
| 319 |  | 
|---|
| 320 |             txt_id = atoi(argv[2]); | 
|---|
| 321 |  | 
|---|
| 322 |         if( display_txt_processes( txt_id ) ) | 
|---|
| 323 |         { | 
|---|
| 324 |             printf("  error: illegal argument txt_id = %d\n", txt_id ); | 
|---|
| 325 |         } | 
|---|
| 326 |     } | 
|---|
| 327 |     else if( strcmp( argv[1] , "vfs" ) == 0 ) | 
|---|
| 328 |     { | 
|---|
| 329 |         if( argc != 2 ) | 
|---|
| 330 |         { | 
|---|
| 331 |                     printf("  usage: display vfs\n"); | 
|---|
| 332 |                     return; | 
|---|
| 333 |             } | 
|---|
| 334 |  | 
|---|
| 335 |         display_vfs(); | 
|---|
| 336 |     } | 
|---|
| 337 |     else if( strcmp( argv[1] , "chdev" ) == 0 ) | 
|---|
| 338 |     { | 
|---|
| 339 |         if( argc != 2 ) | 
|---|
| 340 |         { | 
|---|
| 341 |                     printf("  usage: display chdev\n"); | 
|---|
| 342 |                     return; | 
|---|
| 343 |             } | 
|---|
| 344 |  | 
|---|
| 345 |         display_chdev(); | 
|---|
| 346 |     } | 
|---|
| 347 |     else if( strcmp( argv[1] , "dqdt" ) == 0 ) | 
|---|
| 348 |     { | 
|---|
| 349 |         if( argc != 2 ) | 
|---|
| 350 |         { | 
|---|
| 351 |                     printf("  usage: display dqdt\n"); | 
|---|
| 352 |                     return; | 
|---|
| 353 |             } | 
|---|
| 354 |  | 
|---|
| 355 |         display_dqdt(); | 
|---|
| 356 |     } | 
|---|
| 357 |     else | 
|---|
| 358 |     { | 
|---|
| 359 |         printf("  usage: display (vmm/sched/process/vfs/chdev/txt) [arg2] [arg3]\n"); | 
|---|
| 360 |     } | 
|---|
| 361 |  | 
|---|
| 362 |     // release semaphore to get next command | 
|---|
| 363 |     sem_post( &semaphore ); | 
|---|
| 364 |  | 
|---|
| 365 | } // end cmd_display() | 
|---|
| 366 |  | 
|---|
| 367 | ///////////////////////////////////////// | 
|---|
| 368 | static void cmd_fg(int argc, char **argv) | 
|---|
| 369 | { | 
|---|
| 370 |         unsigned int pid; | 
|---|
| 371 |  | 
|---|
| 372 |         if (argc != 2)  | 
|---|
| 373 |     { | 
|---|
| 374 |                 printf("  usage: %s pid\n", argv[0]); | 
|---|
| 375 |                 return; | 
|---|
| 376 |         } | 
|---|
| 377 |  | 
|---|
| 378 |     pid = atoi( argv[1] );    | 
|---|
| 379 |  | 
|---|
| 380 |     if( pid == 0 ) | 
|---|
| 381 |     { | 
|---|
| 382 |                 printf("  error: PID cannot be 0\n" ); | 
|---|
| 383 |         } | 
|---|
| 384 |  | 
|---|
| 385 |     if( fg( pid ) ) | 
|---|
| 386 |     { | 
|---|
| 387 |                 printf("  error: cannot find process %x\n", pid ); | 
|---|
| 388 |         } | 
|---|
| 389 |  | 
|---|
| 390 |     // release semaphore to get next command | 
|---|
| 391 |     sem_post( &semaphore ); | 
|---|
| 392 |  | 
|---|
| 393 | }  // end cmd_fg() | 
|---|
| 394 |  | 
|---|
| 395 | ////////////////////////////////////////////// | 
|---|
| 396 | static void cmd_help( int argc , char **argv ) | 
|---|
| 397 | { | 
|---|
| 398 |         unsigned int i; | 
|---|
| 399 |  | 
|---|
| 400 |         if (argc != 1)  | 
|---|
| 401 |     { | 
|---|
| 402 |                 printf("  usage: %s\n", argv[0]); | 
|---|
| 403 |                 return; | 
|---|
| 404 |         } | 
|---|
| 405 |  | 
|---|
| 406 |         printf("available commands:\n"); | 
|---|
| 407 |         for (i = 0 ; cmd[i].name ; i++)  | 
|---|
| 408 |     { | 
|---|
| 409 |                 printf("\t%s\t : %s\n", cmd[i].name , cmd[i].desc); | 
|---|
| 410 |         } | 
|---|
| 411 |  | 
|---|
| 412 |     // release semaphore to get next command | 
|---|
| 413 |     sem_post( &semaphore ); | 
|---|
| 414 |  | 
|---|
| 415 | }   // end cmd_help() | 
|---|
| 416 |  | 
|---|
| 417 | ////////////////////////////////////////////// | 
|---|
| 418 | static void cmd_kill( int argc , char **argv ) | 
|---|
| 419 | { | 
|---|
| 420 |         unsigned int pid; | 
|---|
| 421 |  | 
|---|
| 422 |         if (argc != 2)  | 
|---|
| 423 |     { | 
|---|
| 424 |                 printf("  usage: %s pid\n", argv[0]); | 
|---|
| 425 |                 return; | 
|---|
| 426 |         } | 
|---|
| 427 |  | 
|---|
| 428 |         pid = atoi( argv[1] ); | 
|---|
| 429 |  | 
|---|
| 430 |     if( pid == 0 ) | 
|---|
| 431 |     { | 
|---|
| 432 |                 printf("  error: kernel process 0 cannot be killed\n" ); | 
|---|
| 433 |         } | 
|---|
| 434 |  | 
|---|
| 435 |         if( kill( pid , SIGKILL ) ) | 
|---|
| 436 |     { | 
|---|
| 437 |                 printf("  error: process %x cannot be killed\n", pid ); | 
|---|
| 438 |         } | 
|---|
| 439 |  | 
|---|
| 440 |     // release semaphore to get next command | 
|---|
| 441 |     sem_post( &semaphore ); | 
|---|
| 442 |  | 
|---|
| 443 | }   // end cmd_kill() | 
|---|
| 444 |  | 
|---|
| 445 | ////////////////////////////////////////////// | 
|---|
| 446 | static void cmd_load( int argc , char **argv ) | 
|---|
| 447 | { | 
|---|
| 448 |         int                  ret_fork;           // return value from fork  | 
|---|
| 449 |         int                  ret_exec;           // return value from exec | 
|---|
| 450 |     unsigned int         ksh_pid;            // KSH process PID | 
|---|
| 451 |         char               * pathname;           // path to .elf file | 
|---|
| 452 |     unsigned int         background;         // background execution if non zero | 
|---|
| 453 |  | 
|---|
| 454 |         if( (argc < 2) || (argc > 3) )   | 
|---|
| 455 |     { | 
|---|
| 456 |                 printf("  usage: %s pathname [&]\n", argv[0] ); | 
|---|
| 457 |                 return; | 
|---|
| 458 |         } | 
|---|
| 459 |  | 
|---|
| 460 |         pathname = argv[1]; | 
|---|
| 461 |  | 
|---|
| 462 |     if( argc == 3 ) background = (argv[2][0] == '&'); | 
|---|
| 463 |     else            background = 0; | 
|---|
| 464 |  | 
|---|
| 465 |     // get KSH process PID | 
|---|
| 466 |     ksh_pid = getpid(); | 
|---|
| 467 |  | 
|---|
| 468 | #if KSH_DEBUG | 
|---|
| 469 | printf("\n[KSH_DEBUG] in %s : ksh PID %x / path %s / background %d\n", | 
|---|
| 470 | __FUNCTION__, ksh_pid, argv[1], background ); | 
|---|
| 471 | #endif | 
|---|
| 472 |  | 
|---|
| 473 |     // KSH process fork CHILD process | 
|---|
| 474 |         ret_fork = fork(); | 
|---|
| 475 |  | 
|---|
| 476 |     if ( ret_fork < 0 )     // it is a failure reported to KSH | 
|---|
| 477 |     { | 
|---|
| 478 |         printf("  error: ksh process unable to fork\n"); | 
|---|
| 479 |         return; | 
|---|
| 480 |     } | 
|---|
| 481 |     else if (ret_fork == 0) // it is the CHILD process  | 
|---|
| 482 |     { | 
|---|
| 483 |         // CHILD process exec NEW process | 
|---|
| 484 |         ret_exec = execve( pathname , NULL , NULL ); | 
|---|
| 485 |  | 
|---|
| 486 |         // this is only executed in case of exec failure | 
|---|
| 487 |         if( ret_exec ) | 
|---|
| 488 |         { | 
|---|
| 489 |             printf("  error: child process unable to exec <%s>\n", pathname ); | 
|---|
| 490 |             exit( 0 ); | 
|---|
| 491 |         }    | 
|---|
| 492 |         }  | 
|---|
| 493 |     else                    // it is the KSH process : ret_fork is the new process PID | 
|---|
| 494 |     { | 
|---|
| 495 |  | 
|---|
| 496 | #if KSH_DEBUG | 
|---|
| 497 | int sem_value; | 
|---|
| 498 | sem_getvalue( &semaphore , &sem_value ); | 
|---|
| 499 | printf("\n[KSH_DEBUG] in %s : child PID %x / background %d / sem_value %d\n", | 
|---|
| 500 | ret_fork, background, sem_value  ); | 
|---|
| 501 | #endif | 
|---|
| 502 |  | 
|---|
| 503 |         if( background )        // child in background =>  KSH keeps TXT ownership | 
|---|
| 504 |         { | 
|---|
| 505 |             // execve() tranfered TXT ownership to child => give it back to KSH | 
|---|
| 506 |             fg( ksh_pid ); | 
|---|
| 507 |  | 
|---|
| 508 |             // release semaphore to get next command | 
|---|
| 509 |             sem_post( &semaphore ); | 
|---|
| 510 |         } | 
|---|
| 511 |     } | 
|---|
| 512 | }   // end cmd_load | 
|---|
| 513 |  | 
|---|
| 514 | ///////////////////////////////////////////// | 
|---|
| 515 | static void cmd_log( int argc , char **argv ) | 
|---|
| 516 | { | 
|---|
| 517 |         unsigned int i; | 
|---|
| 518 |  | 
|---|
| 519 |         printf("--- registered commands ---\n"); | 
|---|
| 520 |         for (i = 0; i < LOG_DEPTH; i++)  | 
|---|
| 521 |     { | 
|---|
| 522 |                 printf(" - %d\t: %s\n", i, &log_entries[i].buf); | 
|---|
| 523 |         } | 
|---|
| 524 |  | 
|---|
| 525 |     // release semaphore to get next command | 
|---|
| 526 |     sem_post( &semaphore ); | 
|---|
| 527 |  | 
|---|
| 528 | } // end cmd_log() | 
|---|
| 529 |  | 
|---|
| 530 |  | 
|---|
| 531 | //////////////////////////////////////////// | 
|---|
| 532 | static void cmd_ls( int argc , char **argv ) | 
|---|
| 533 | { | 
|---|
| 534 |         char  * path; | 
|---|
| 535 |  | 
|---|
| 536 | //  struct dirent * file; | 
|---|
| 537 | //  DIR *dir; | 
|---|
| 538 |  | 
|---|
| 539 |         if (argc == 1) | 
|---|
| 540 |     { | 
|---|
| 541 |                 path = "."; | 
|---|
| 542 |         } | 
|---|
| 543 |     else if (argc == 2)  | 
|---|
| 544 |     { | 
|---|
| 545 |                 path = argv[1]; | 
|---|
| 546 |         }  | 
|---|
| 547 |     else  | 
|---|
| 548 |     { | 
|---|
| 549 |                 printf("  usage: ls [path]\n"); | 
|---|
| 550 |                 return; | 
|---|
| 551 |         } | 
|---|
| 552 |  | 
|---|
| 553 |     printf("  error: not implemented yet\n"); | 
|---|
| 554 | /* | 
|---|
| 555 |         dir = opendir( path ); | 
|---|
| 556 |         while ((file = readdir(dir)) != NULL) | 
|---|
| 557 |         { | 
|---|
| 558 |                 printf(" %s\n", file->d_name); | 
|---|
| 559 |         } | 
|---|
| 560 |         closedir(dir); | 
|---|
| 561 | */ | 
|---|
| 562 |  | 
|---|
| 563 |     // release semaphore to get next command | 
|---|
| 564 |     sem_post( &semaphore ); | 
|---|
| 565 |  | 
|---|
| 566 | } // end cmd_ls() | 
|---|
| 567 |  | 
|---|
| 568 | /////////////////////////////////////////////// | 
|---|
| 569 | static void cmd_mkdir( int argc , char **argv ) | 
|---|
| 570 | { | 
|---|
| 571 |         char * pathname; | 
|---|
| 572 |  | 
|---|
| 573 |         if (argc != 2) | 
|---|
| 574 |     { | 
|---|
| 575 |                 printf("  usage: mkdir pathname\n"); | 
|---|
| 576 |                 return; | 
|---|
| 577 |         } | 
|---|
| 578 |  | 
|---|
| 579 |     pathname = argv[1]; | 
|---|
| 580 |  | 
|---|
| 581 |     printf("  error: not implemented yet\n"); | 
|---|
| 582 |  | 
|---|
| 583 |     // release semaphore to get next command | 
|---|
| 584 |     sem_post( &semaphore ); | 
|---|
| 585 |  | 
|---|
| 586 | } // end cmd_mkdir() | 
|---|
| 587 |  | 
|---|
| 588 | //////////////////////////////////////////// | 
|---|
| 589 | static void cmd_mv( int argc , char **argv ) | 
|---|
| 590 | { | 
|---|
| 591 |  | 
|---|
| 592 |         if (argc < 3) | 
|---|
| 593 |         { | 
|---|
| 594 |                 printf("  usage : %s src_pathname dst_pathname\n", argv[0]); | 
|---|
| 595 |                 return; | 
|---|
| 596 |         } | 
|---|
| 597 |  | 
|---|
| 598 |     printf("  error: not implemented yet\n"); | 
|---|
| 599 |      | 
|---|
| 600 |     // release semaphore to get next command | 
|---|
| 601 |     sem_post( &semaphore ); | 
|---|
| 602 |  | 
|---|
| 603 | }  // end cmd_mv | 
|---|
| 604 |  | 
|---|
| 605 | ///////////////////////////////////////////// | 
|---|
| 606 | static void cmd_pwd( int argc , char **argv ) | 
|---|
| 607 | { | 
|---|
| 608 |         char buf[1024]; | 
|---|
| 609 |  | 
|---|
| 610 |         if (argc != 1) | 
|---|
| 611 |     { | 
|---|
| 612 |                 printf("  usage: pwd\n"); | 
|---|
| 613 |                 return; | 
|---|
| 614 |         } | 
|---|
| 615 |  | 
|---|
| 616 |         if ( getcwd( buf , 1024 ) )  | 
|---|
| 617 |     { | 
|---|
| 618 |                 printf("  error: unable to get current directory\n"); | 
|---|
| 619 |         } | 
|---|
| 620 |     else  | 
|---|
| 621 |     { | 
|---|
| 622 |                 printf("%s\n", buf); | 
|---|
| 623 |         } | 
|---|
| 624 |  | 
|---|
| 625 |     // release semaphore to get next command | 
|---|
| 626 |     sem_post( &semaphore ); | 
|---|
| 627 |  | 
|---|
| 628 | }  // end cmd_pwd() | 
|---|
| 629 |  | 
|---|
| 630 | //////////////////////////////////////////// | 
|---|
| 631 | static void cmd_rm( int argc , char **argv ) | 
|---|
| 632 | { | 
|---|
| 633 |         char * pathname; | 
|---|
| 634 |  | 
|---|
| 635 |         if (argc != 2) | 
|---|
| 636 |     { | 
|---|
| 637 |                 printf("  usage: rm pathname\n"); | 
|---|
| 638 |                 return; | 
|---|
| 639 |         } | 
|---|
| 640 |  | 
|---|
| 641 |         pathname = argv[1]; | 
|---|
| 642 |  | 
|---|
| 643 |     printf("  error: not implemented yet\n"); | 
|---|
| 644 |  | 
|---|
| 645 |     // release semaphore to get next command | 
|---|
| 646 |     sem_post( &semaphore ); | 
|---|
| 647 |  | 
|---|
| 648 | }  // end_cmd_rm() | 
|---|
| 649 |  | 
|---|
| 650 | /////////////////////////////////////////////// | 
|---|
| 651 | static void cmd_rmdir( int argc , char **argv ) | 
|---|
| 652 | { | 
|---|
| 653 |     // same as cmd_rm() | 
|---|
| 654 |         cmd_rm(argc, argv); | 
|---|
| 655 | } | 
|---|
| 656 |  | 
|---|
| 657 | /////////////////////////////////////////////// | 
|---|
| 658 | static void cmd_trace( int argc , char **argv ) | 
|---|
| 659 | { | 
|---|
| 660 |     unsigned int cxy; | 
|---|
| 661 |     unsigned int lid; | 
|---|
| 662 |  | 
|---|
| 663 |         if (argc != 3) | 
|---|
| 664 |     { | 
|---|
| 665 |                 printf("  usage: trace cxy lid \n"); | 
|---|
| 666 |                 return; | 
|---|
| 667 |         } | 
|---|
| 668 |  | 
|---|
| 669 |     cxy = atoi(argv[1]); | 
|---|
| 670 |     lid = atoi(argv[2]); | 
|---|
| 671 |  | 
|---|
| 672 |     if( trace( 1 , cxy , lid ) ) | 
|---|
| 673 |     { | 
|---|
| 674 |         printf("  error: core[%x,%d] not found\n", cxy, lid ); | 
|---|
| 675 |     } | 
|---|
| 676 |  | 
|---|
| 677 |     // release semaphore to get next command | 
|---|
| 678 |     sem_post( &semaphore ); | 
|---|
| 679 |  | 
|---|
| 680 | }  // end cmd_trace | 
|---|
| 681 |  | 
|---|
| 682 | /////////////////////////////////////////////// | 
|---|
| 683 | static void cmd_untrace( int argc , char **argv ) | 
|---|
| 684 | { | 
|---|
| 685 |     unsigned int cxy; | 
|---|
| 686 |     unsigned int lid; | 
|---|
| 687 |  | 
|---|
| 688 |         if (argc != 3) | 
|---|
| 689 |     { | 
|---|
| 690 |                 printf("  usage: untrace cxy lid \n"); | 
|---|
| 691 |                 return; | 
|---|
| 692 |         } | 
|---|
| 693 |  | 
|---|
| 694 |     cxy = atoi(argv[1]); | 
|---|
| 695 |     lid = atoi(argv[2]); | 
|---|
| 696 |  | 
|---|
| 697 |     if( trace( 0 , cxy , lid ) ) | 
|---|
| 698 |     { | 
|---|
| 699 |         printf("  error: core[%x,%d] not found\n", cxy, lid ); | 
|---|
| 700 |     } | 
|---|
| 701 |  | 
|---|
| 702 |     // release semaphore to get next command | 
|---|
| 703 |     sem_post( &semaphore ); | 
|---|
| 704 |  | 
|---|
| 705 | }  // end cmd_untrace() | 
|---|
| 706 |  | 
|---|
| 707 | /////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 708 | // Array of commands | 
|---|
| 709 | /////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 710 |  | 
|---|
| 711 | ksh_cmd_t cmd[] = | 
|---|
| 712 | { | 
|---|
| 713 |         { "cat",     "display file content",                            cmd_cat     }, | 
|---|
| 714 |         { "cd",      "change current directory",                        cmd_cd      }, | 
|---|
| 715 |         { "cp",      "replicate a file in file system",                 cmd_cp      }, | 
|---|
| 716 |     { "fg",      "put a process in foreground",                     cmd_fg      }, | 
|---|
| 717 |     { "display", "display vmm/sched/process/vfs/chdev/txt",         cmd_display }, | 
|---|
| 718 |         { "load",    "load an user application",                        cmd_load    }, | 
|---|
| 719 |         { "help",    "list available commands",                         cmd_help    }, | 
|---|
| 720 |         { "kill",    "kill a process (all threads)",                    cmd_kill    }, | 
|---|
| 721 |         { "log",     "list registered commands",                        cmd_log     }, | 
|---|
| 722 |         { "ls",      "list directory entries",                          cmd_ls      }, | 
|---|
| 723 |         { "mkdir",   "create a new directory",                          cmd_mkdir   }, | 
|---|
| 724 |         { "mv",      "move a file in file system",                      cmd_mv      }, | 
|---|
| 725 |         { "pwd",     "print current working directory",                 cmd_pwd     }, | 
|---|
| 726 |         { "rm",      "remove a file from file system",                  cmd_rm      }, | 
|---|
| 727 |         { "rmdir",   "remove a directory from file system",             cmd_rmdir   }, | 
|---|
| 728 |         { "trace",   "activate trace for a given core",                 cmd_trace   }, | 
|---|
| 729 |         { "untrace", "desactivate trace for a given core",              cmd_untrace }, | 
|---|
| 730 |         { NULL,      NULL,                                                                              NULL        } | 
|---|
| 731 | }; | 
|---|
| 732 |  | 
|---|
| 733 | //////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 734 | // This function analyses one command (with arguments), executes it, and returns. | 
|---|
| 735 | //////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 736 | static void parse( char * buf ) | 
|---|
| 737 | { | 
|---|
| 738 |         int argc = 0; | 
|---|
| 739 |         char *argv[MAX_ARGS]; | 
|---|
| 740 |         int i; | 
|---|
| 741 |         int len = strlen(buf); | 
|---|
| 742 |  | 
|---|
| 743 |         // build argc/argv | 
|---|
| 744 |         for (i = 0; i < len; i++)  | 
|---|
| 745 |     { | 
|---|
| 746 |                 if (buf[i] == ' ')  | 
|---|
| 747 |         { | 
|---|
| 748 |                         buf[i] = '\0'; | 
|---|
| 749 |                 } | 
|---|
| 750 |         else if (i == 0 || buf[i - 1] == '\0')  | 
|---|
| 751 |         { | 
|---|
| 752 |                         if (argc < MAX_ARGS)  | 
|---|
| 753 |             { | 
|---|
| 754 |                                 argv[argc] = &buf[i]; | 
|---|
| 755 |                                 argc++; | 
|---|
| 756 |                         } | 
|---|
| 757 |                 } | 
|---|
| 758 |         } | 
|---|
| 759 |  | 
|---|
| 760 |         if (argc > 0) | 
|---|
| 761 |     { | 
|---|
| 762 |                 int found = 0; | 
|---|
| 763 |  | 
|---|
| 764 |                 argv[argc] = NULL; | 
|---|
| 765 |  | 
|---|
| 766 |                 // try to match typed command  | 
|---|
| 767 |                 for (i = 0; cmd[i].name; i++) | 
|---|
| 768 |         { | 
|---|
| 769 |                         if (strcmp(argv[0], cmd[i].name) == 0) | 
|---|
| 770 |             { | 
|---|
| 771 |                                 cmd[i].fn(argc, argv); | 
|---|
| 772 |                                 found = 1; | 
|---|
| 773 |                                 break; | 
|---|
| 774 |                         } | 
|---|
| 775 |                 } | 
|---|
| 776 |  | 
|---|
| 777 |                 if (!found)  // undefined command | 
|---|
| 778 |         { | 
|---|
| 779 |                         printf("  error : undefined command <%s>\n", argv[0]); | 
|---|
| 780 |  | 
|---|
| 781 |             // release semaphore to get next command | 
|---|
| 782 |             sem_post( &semaphore ); | 
|---|
| 783 |                 } | 
|---|
| 784 |         } | 
|---|
| 785 | }  // end parse() | 
|---|
| 786 |  | 
|---|
| 787 | ///////////////////////// | 
|---|
| 788 | static void interactive() | 
|---|
| 789 | { | 
|---|
| 790 |         char         c;                                           // read character | 
|---|
| 791 |         char         buf[CMD_MAX_SIZE];           // buffer for one command | 
|---|
| 792 |     unsigned int end_command;             // last character found in a command | 
|---|
| 793 |         unsigned int count;                               // pointer in command buffer | 
|---|
| 794 |         unsigned int i;                                           // index for loops | 
|---|
| 795 |         unsigned int state;                   // escape sequence state | 
|---|
| 796 |  | 
|---|
| 797 | // @@@ | 
|---|
| 798 | // parse( "load /bin/user/fft.elf" ); | 
|---|
| 799 | // @@@ | 
|---|
| 800 |  | 
|---|
| 801 |         enum fsm_states  | 
|---|
| 802 |     { | 
|---|
| 803 |                 NORMAL = 0, | 
|---|
| 804 |                 ESCAPE = 1, | 
|---|
| 805 |                 BRAKET = 2, | 
|---|
| 806 |         }; | 
|---|
| 807 |  | 
|---|
| 808 |         // This lexical analyser writes one command line in the command buffer. | 
|---|
| 809 |         // It is implemented as a 3 states FSM to handle the following escape sequences: | 
|---|
| 810 |         // - ESC [ A : up arrow | 
|---|
| 811 |         // - ESC [ B : down arrow | 
|---|
| 812 |         // - ESC [ C : right arrow | 
|---|
| 813 |         // - ESC [ D : left arrow | 
|---|
| 814 |         // The three states have the following semantic: | 
|---|
| 815 |         // - NORMAL : no (ESC) character has been found | 
|---|
| 816 |         // - ESCAPE : the character (ESC) has been found | 
|---|
| 817 |         // - BRAKET : the wo characters (ESC,[) have been found | 
|---|
| 818 |  | 
|---|
| 819 |     // external loop on the commands | 
|---|
| 820 |     // the in teractive thread should not exit this loop | 
|---|
| 821 |         while (1) | 
|---|
| 822 |         { | 
|---|
| 823 |             // initialize command buffer | 
|---|
| 824 |             memset( buf, 0x20 , sizeof(buf) );   // TODO useful ? | 
|---|
| 825 |             count = 0; | 
|---|
| 826 |             state = NORMAL; | 
|---|
| 827 |  | 
|---|
| 828 |         // block if the KSH process is not the TXT owner | 
|---|
| 829 |         // - if the command is not a "load" | 
|---|
| 830 |         //   the semaphore must be released by the cmd_***() | 
|---|
| 831 |         // - if the command is a load, it depends on  | 
|---|
| 832 |         //   the "background" argument | 
|---|
| 833 |         sem_wait( &semaphore ); | 
|---|
| 834 |  | 
|---|
| 835 |         // display prompt on a new line | 
|---|
| 836 |         printf("\n[ksh] "); | 
|---|
| 837 |   | 
|---|
| 838 |         end_command = 0; | 
|---|
| 839 |  | 
|---|
| 840 |         // internal loop on characters in one command | 
|---|
| 841 |         while( end_command == 0 ) | 
|---|
| 842 |         { | 
|---|
| 843 |             // get one character from TXT_RX | 
|---|
| 844 |                 c = (char)getchar(); | 
|---|
| 845 |  | 
|---|
| 846 |             if( c == 0 ) continue; | 
|---|
| 847 |  | 
|---|
| 848 |                     if( state == NORMAL )  // we are not in an escape sequence | 
|---|
| 849 |                     { | 
|---|
| 850 |                                 if ((c == '\b') || (c == 0x7F))  // backspace => remove one character | 
|---|
| 851 |                                 { | 
|---|
| 852 |                                     if (count > 0) | 
|---|
| 853 |                     { | 
|---|
| 854 |                                         printf("\b \b"); | 
|---|
| 855 |                                         count--; | 
|---|
| 856 |                                     } | 
|---|
| 857 |                                 } | 
|---|
| 858 |                                 else if (c == '\n')                  // new line => end of command | 
|---|
| 859 |                                 { | 
|---|
| 860 |                                     if (count > 0)               // analyse & execute command | 
|---|
| 861 |                                     { | 
|---|
| 862 |                                             // complete command with NUL character | 
|---|
| 863 |                                             buf[count] = 0; | 
|---|
| 864 |                         count++; | 
|---|
| 865 |  | 
|---|
| 866 |                                         // register command in log arrays | 
|---|
| 867 |                                             strcpy(log_entries[ptw].buf, buf); | 
|---|
| 868 |                                             log_entries[ptw].count = count; | 
|---|
| 869 |                                             ptw = (ptw + 1) % LOG_DEPTH; | 
|---|
| 870 |                                             ptr = ptw; | 
|---|
| 871 |  | 
|---|
| 872 |                         // echo character | 
|---|
| 873 |                         putchar( c ); | 
|---|
| 874 |  | 
|---|
| 875 |                                             // call parser to analyse and execute command | 
|---|
| 876 |                                             parse( buf ); | 
|---|
| 877 |                                     } | 
|---|
| 878 |                     else                         // no command registered | 
|---|
| 879 |                     { | 
|---|
| 880 |                         // release semaphore to get next command | 
|---|
| 881 |                         sem_post( &semaphore ); | 
|---|
| 882 |                     } | 
|---|
| 883 |  | 
|---|
| 884 |                     // exit internal loop on characters | 
|---|
| 885 |                     end_command = 1; | 
|---|
| 886 |                 } | 
|---|
| 887 |                             else if (c == '\t')             // tabulation => do nothing | 
|---|
| 888 |                                 { | 
|---|
| 889 |                             } | 
|---|
| 890 |                             else if (c == (char)0x1B)       // ESC => start an escape sequence | 
|---|
| 891 |                             { | 
|---|
| 892 |                     state = ESCAPE; | 
|---|
| 893 |                             } | 
|---|
| 894 |                             else                                               // normal character | 
|---|
| 895 |                                 { | 
|---|
| 896 |                                     if (count < sizeof(buf) - 1) | 
|---|
| 897 |                                     { | 
|---|
| 898 |                         // register character in command buffer | 
|---|
| 899 |                                             buf[count] = c; | 
|---|
| 900 |                                             count++; | 
|---|
| 901 |  | 
|---|
| 902 |                         // echo character | 
|---|
| 903 |                         putchar( c ); | 
|---|
| 904 |                                         } | 
|---|
| 905 |                                 } | 
|---|
| 906 |                         } | 
|---|
| 907 |                         else if( state == ESCAPE )   | 
|---|
| 908 |                         { | 
|---|
| 909 |                                 if (c == '[')           //  valid sequence => continue | 
|---|
| 910 |                                 { | 
|---|
| 911 |                                         state = BRAKET; | 
|---|
| 912 |                                 } | 
|---|
| 913 |                                 else                               // invalid sequence => do nothing | 
|---|
| 914 |                                 { | 
|---|
| 915 |                                         state = NORMAL; | 
|---|
| 916 |                                 } | 
|---|
| 917 |                         } | 
|---|
| 918 |                         else if( state == BRAKET ) | 
|---|
| 919 |                         { | 
|---|
| 920 |                                 if (c == 'D')   // valid  LEFT sequence => move buf pointer left | 
|---|
| 921 |                                 { | 
|---|
| 922 |                                         if (count > 0) | 
|---|
| 923 |                                         { | 
|---|
| 924 |                                                 printf("\b"); | 
|---|
| 925 |                                                 count--; | 
|---|
| 926 |                                         } | 
|---|
| 927 |  | 
|---|
| 928 |                                         // get next user char | 
|---|
| 929 |                                         state = NORMAL; | 
|---|
| 930 |                                 } | 
|---|
| 931 |                                 else if (c == 'C')   // valid  RIGHT sequence => move buf pointer right | 
|---|
| 932 |                                 { | 
|---|
| 933 |                                         if (count < sizeof(buf) - 1) | 
|---|
| 934 |                                         { | 
|---|
| 935 |                                                 printf("%c", buf[count]); | 
|---|
| 936 |                                                 count++; | 
|---|
| 937 |                                         } | 
|---|
| 938 |  | 
|---|
| 939 |                                         // get next user char | 
|---|
| 940 |                                         state = NORMAL; | 
|---|
| 941 |                                 } | 
|---|
| 942 |                                 else if (c == 'A')   // valid  UP sequence => move log pointer backward | 
|---|
| 943 |                                 { | 
|---|
| 944 |                                         // cancel current command | 
|---|
| 945 |                                         for (i = 0; i < count; i++) printf("\b \b"); | 
|---|
| 946 |                                         count = 0; | 
|---|
| 947 |  | 
|---|
| 948 |                                         // copy log command into buf | 
|---|
| 949 |                                         ptr = (ptr - 1) % LOG_DEPTH; | 
|---|
| 950 |                                         strcpy(buf, log_entries[ptr].buf); | 
|---|
| 951 |                                         count = log_entries[ptr].count - 1; | 
|---|
| 952 |  | 
|---|
| 953 |                                         // display log command | 
|---|
| 954 |                                         printf("%s", buf); | 
|---|
| 955 |  | 
|---|
| 956 |                                         // get next user char | 
|---|
| 957 |                                         state = NORMAL; | 
|---|
| 958 |                                 } | 
|---|
| 959 |                                 else if (c == 'B')   // valid  DOWN sequence => move log pointer forward | 
|---|
| 960 |                                 { | 
|---|
| 961 |                                         // cancel current command | 
|---|
| 962 |                                         for (i = 0 ; i < count; i++) printf("\b \b"); | 
|---|
| 963 |                                         count = 0; | 
|---|
| 964 |  | 
|---|
| 965 |                                         // copy log command into buf | 
|---|
| 966 |                                         ptr = (ptr + 1) % LOG_DEPTH; | 
|---|
| 967 |                                         strcpy(buf, log_entries[ptr].buf); | 
|---|
| 968 |                                         count = log_entries[ptr].count; | 
|---|
| 969 |  | 
|---|
| 970 |                                         // display log command | 
|---|
| 971 |                                         printf("%s", buf); | 
|---|
| 972 |  | 
|---|
| 973 |                                         // get next user char | 
|---|
| 974 |                                         state = NORMAL; | 
|---|
| 975 |                                 } | 
|---|
| 976 |                                 else                               // other character => do nothing | 
|---|
| 977 |                                 { | 
|---|
| 978 |                                         // get next user char | 
|---|
| 979 |                                         state = NORMAL; | 
|---|
| 980 |                                 } | 
|---|
| 981 |                         } | 
|---|
| 982 |                 }  // end internal while loop on characters | 
|---|
| 983 |         }  // end external while loop on commands | 
|---|
| 984 | }  // end interactive() | 
|---|
| 985 |  | 
|---|
| 986 | /////////////////////////////////// | 
|---|
| 987 | int main( int argc , char *argv[] ) | 
|---|
| 988 | { | 
|---|
| 989 |     unsigned int cxy;             // owner cluster identifier for this KSH process | 
|---|
| 990 |     unsigned int lid;             // core identifier for this KSH main thread | 
|---|
| 991 |     int          status;          // child process termination status | 
|---|
| 992 |     int          child_pid;       // child process identifier | 
|---|
| 993 |     int          parent_pid;      // parent process identifier | 
|---|
| 994 |     pthread_t    trdid;           // kernel allocated index for interactive thread | 
|---|
| 995 |     unsigned int is_owner;        // non-zero if KSH process is TXT owner | 
|---|
| 996 |  | 
|---|
| 997 |     // initialize log buffer | 
|---|
| 998 |         memset( &log_entries , 0, sizeof(log_entries)); | 
|---|
| 999 |         ptw   = 0; | 
|---|
| 1000 |         ptr   = 0; | 
|---|
| 1001 |  | 
|---|
| 1002 |     // get KSH process pid and core | 
|---|
| 1003 |     parent_pid = getpid(); | 
|---|
| 1004 |     get_core( &cxy , &lid ); | 
|---|
| 1005 |  | 
|---|
| 1006 |     // initializes the semaphore used to unblock the interactive thread | 
|---|
| 1007 |     sem_init( &semaphore , 0 , 1 ); | 
|---|
| 1008 |  | 
|---|
| 1009 |     // initialize interactive thread attributes | 
|---|
| 1010 |     attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED; | 
|---|
| 1011 |     attr.cxy        = cxy; | 
|---|
| 1012 |  | 
|---|
| 1013 |     // lauch the interactive thread  | 
|---|
| 1014 |     pthread_create( &trdid, | 
|---|
| 1015 |                     &attr, | 
|---|
| 1016 |                     &interactive,   // entry function | 
|---|
| 1017 |                     NULL );  | 
|---|
| 1018 |      | 
|---|
| 1019 |     // enter infinite loop monitoring children processes termination  | 
|---|
| 1020 |     while( 1 ) | 
|---|
| 1021 |     { | 
|---|
| 1022 |         // wait children termination | 
|---|
| 1023 |         child_pid = wait( &status ); | 
|---|
| 1024 |  | 
|---|
| 1025 | #if KSH_DEBUG | 
|---|
| 1026 | if( WIFEXITED  (status) ) printf("\n[KSH] child process %x exited\n" , child_pid ); | 
|---|
| 1027 | if( WIFSIGNALED(status) ) printf("\n[KSH] child process %x killed\n" , child_pid ); | 
|---|
| 1028 | if( WIFSTOPPED (status) ) printf("\n[KSH] child process %x stopped\n", child_pid ); | 
|---|
| 1029 | #endif | 
|---|
| 1030 |  | 
|---|
| 1031 |         // release semaphore if KSH process is TXT owner, to unblock interactive thread | 
|---|
| 1032 |         is_fg( parent_pid , &is_owner ); | 
|---|
| 1033 |         if( is_owner ) sem_post( &semaphore ); | 
|---|
| 1034 |  | 
|---|
| 1035 |     } | 
|---|
| 1036 | }  // end main() | 
|---|
| 1037 |  | 
|---|
| 1038 |  | 
|---|