| 1 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // File : ksh.c |
|---|
| 3 | // Date : October 2017 |
|---|
| 4 | // Author : Alain Greiner |
|---|
| 5 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 6 | // This single thread application implement a simple shell for ALMOS-MKH. |
|---|
| 7 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 8 | |
|---|
| 9 | #include <stdio.h> |
|---|
| 10 | #include <stdlib.h> |
|---|
| 11 | #include <string.h> |
|---|
| 12 | #include <shared_syscalls.h> |
|---|
| 13 | |
|---|
| 14 | #define CMD_MAX_SIZE (256) // max number of characters in one command |
|---|
| 15 | #define LOG_DEPTH (32) // max number of registered commands |
|---|
| 16 | #define MAX_ARGS (32) // max number of arguments in a command |
|---|
| 17 | #define FIFO_SIZE (1024) // FIFO depth for recursive ls |
|---|
| 18 | |
|---|
| 19 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 20 | // Structures |
|---|
| 21 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 22 | |
|---|
| 23 | // one entry in the registered commands array |
|---|
| 24 | typedef struct log_entry_s |
|---|
| 25 | { |
|---|
| 26 | char buf[CMD_MAX_SIZE]; |
|---|
| 27 | unsigned int count; |
|---|
| 28 | } |
|---|
| 29 | log_entry_t; |
|---|
| 30 | |
|---|
| 31 | // one entry in the supported command types array |
|---|
| 32 | typedef struct ksh_cmd_s |
|---|
| 33 | { |
|---|
| 34 | char * name; |
|---|
| 35 | char * desc; |
|---|
| 36 | void (*fn)( int , char ** ); |
|---|
| 37 | } |
|---|
| 38 | ksh_cmd_t; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 42 | // Global Variables |
|---|
| 43 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 44 | |
|---|
| 45 | ksh_cmd_t cmd[]; // array of supported commands |
|---|
| 46 | |
|---|
| 47 | log_entry_t log_entries[LOG_DEPTH]; // array of registered commands |
|---|
| 48 | |
|---|
| 49 | unsigned int ptw; // write pointer in log_entries[] |
|---|
| 50 | unsigned int ptr; // read pointer in log_entries[] |
|---|
| 51 | |
|---|
| 52 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 53 | // Shell Commands |
|---|
| 54 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 55 | |
|---|
| 56 | ///////////////////////////////////////////// |
|---|
| 57 | static void cmd_cat( int argc , char **argv ) |
|---|
| 58 | { |
|---|
| 59 | char * path; |
|---|
| 60 | |
|---|
| 61 | if (argc != 2) |
|---|
| 62 | { |
|---|
| 63 | printf(" usage: cat pathname\n"); |
|---|
| 64 | return; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | path = argv[1]; |
|---|
| 68 | |
|---|
| 69 | printf(" error: not implemented yet\n"); |
|---|
| 70 | |
|---|
| 71 | /* |
|---|
| 72 | // open the file |
|---|
| 73 | fd = open( path , O_RDONLY , 0 ); |
|---|
| 74 | if (fd < 0) |
|---|
| 75 | { |
|---|
| 76 | printf(" error: cannot open %s\n", path); |
|---|
| 77 | goto exit; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | // get file size |
|---|
| 81 | if (stat(path, &st) == -1) |
|---|
| 82 | { |
|---|
| 83 | printf(" error: cannot stat %s\n", path); |
|---|
| 84 | goto exit; |
|---|
| 85 | } |
|---|
| 86 | if (S_ISDIR(st.st_mode)) { |
|---|
| 87 | printf(" error: %s is a directory\n", path); |
|---|
| 88 | goto exit; |
|---|
| 89 | } |
|---|
| 90 | size = st.st_size; |
|---|
| 91 | |
|---|
| 92 | // mmap the file |
|---|
| 93 | buf = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
|---|
| 94 | if (buf == NULL || buf == (char *)-1) { |
|---|
| 95 | printf(" error: cannot map %s\n", path); |
|---|
| 96 | goto exit; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | // set terminating '0' XXX |
|---|
| 100 | buf[size-1] = 0; |
|---|
| 101 | |
|---|
| 102 | // display the file content |
|---|
| 103 | printf("%s", buf); |
|---|
| 104 | |
|---|
| 105 | exit: |
|---|
| 106 | if (buf != NULL) munmap(buf, size); |
|---|
| 107 | if (fd >= 0) close(fd); |
|---|
| 108 | */ |
|---|
| 109 | |
|---|
| 110 | } // end cmd_cat() |
|---|
| 111 | |
|---|
| 112 | //////////////////////////////////////////// |
|---|
| 113 | static void cmd_cd( int argc , char **argv ) |
|---|
| 114 | { |
|---|
| 115 | char * path; |
|---|
| 116 | |
|---|
| 117 | if (argc != 2) |
|---|
| 118 | { |
|---|
| 119 | printf(" usage: cd pathname\n"); |
|---|
| 120 | return; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | path = argv[1]; |
|---|
| 124 | |
|---|
| 125 | printf(" error: not implemented yet\n"); |
|---|
| 126 | /* |
|---|
| 127 | path = argv[1]; |
|---|
| 128 | |
|---|
| 129 | if (chdir(path) == -1) |
|---|
| 130 | { |
|---|
| 131 | printf(" error: cannot cd to %s\n", path); |
|---|
| 132 | } |
|---|
| 133 | */ |
|---|
| 134 | |
|---|
| 135 | } // end cmd_cd() |
|---|
| 136 | |
|---|
| 137 | ///////////////////////////////////////// |
|---|
| 138 | static void cmd_cp(int argc, char **argv) |
|---|
| 139 | { |
|---|
| 140 | // int src_fd = -1, dst_fd = -1; |
|---|
| 141 | // char *srcpath, *dstpath; |
|---|
| 142 | // struct stat st; |
|---|
| 143 | // size_t size, i; |
|---|
| 144 | // char buf[1024]; |
|---|
| 145 | |
|---|
| 146 | if (argc != 3) |
|---|
| 147 | { |
|---|
| 148 | printf(" usage: cp src_pathname dst_pathname\n"); |
|---|
| 149 | return; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | printf(" error: not implemented yet\n"); |
|---|
| 153 | |
|---|
| 154 | /* |
|---|
| 155 | srcpath = argv[1]; |
|---|
| 156 | dstpath = argv[2]; |
|---|
| 157 | |
|---|
| 158 | // open the src file |
|---|
| 159 | src_fd = open(srcpath, O_RDONLY, 0); |
|---|
| 160 | if (src_fd < 0) { |
|---|
| 161 | printf(" error: cannot open %s / err = %d\n", srcpath, errno); |
|---|
| 162 | goto exit; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | // get file size |
|---|
| 166 | if (stat(srcpath, &st) == -1) { |
|---|
| 167 | printf(" error: cannot stat %s\n", srcpath); |
|---|
| 168 | goto exit; |
|---|
| 169 | } |
|---|
| 170 | if (S_ISDIR(st.st_mode)) { |
|---|
| 171 | printf(" error: %s is a directory\n", srcpath); |
|---|
| 172 | goto exit; |
|---|
| 173 | } |
|---|
| 174 | size = st.st_size; |
|---|
| 175 | |
|---|
| 176 | // open the dst file |
|---|
| 177 | dst_fd = open(dstpath, O_CREAT|O_TRUNC|O_RDWR, 0); |
|---|
| 178 | if (dst_fd < 0) { |
|---|
| 179 | printf(" error: cannot open %s / err = %d\n", dstpath, errno); |
|---|
| 180 | goto exit; |
|---|
| 181 | } |
|---|
| 182 | if (stat(dstpath, &st) == -1) { |
|---|
| 183 | printf(" error: cannot stat %s\n", dstpath); |
|---|
| 184 | goto exit; |
|---|
| 185 | } |
|---|
| 186 | if (S_ISDIR(st.st_mode)) { |
|---|
| 187 | printf(" error: %s is a directory\n", dstpath); |
|---|
| 188 | goto exit; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | i = 0; |
|---|
| 192 | while (i < size) |
|---|
| 193 | { |
|---|
| 194 | size_t rlen = (size - i < 1024 ? size - i : 1024); |
|---|
| 195 | size_t wlen; |
|---|
| 196 | ssize_t ret; |
|---|
| 197 | |
|---|
| 198 | // read the source |
|---|
| 199 | ret = read(src_fd, buf, rlen); |
|---|
| 200 | if (ret == -1) { |
|---|
| 201 | printf(" error: cannot read from file %s\n", srcpath); |
|---|
| 202 | goto exit; |
|---|
| 203 | } |
|---|
| 204 | rlen = (size_t)ret; |
|---|
| 205 | |
|---|
| 206 | // write to the destination |
|---|
| 207 | ret = write(dst_fd, buf, rlen); |
|---|
| 208 | if (ret == -1) { |
|---|
| 209 | printf(" error: cannot write to file %s\n", dstpath); |
|---|
| 210 | goto exit; |
|---|
| 211 | } |
|---|
| 212 | wlen = (size_t)ret; |
|---|
| 213 | |
|---|
| 214 | // check |
|---|
| 215 | if (wlen != rlen) { |
|---|
| 216 | printf(" error: cannot write on device\n"); |
|---|
| 217 | goto exit; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | i += rlen; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | exit: |
|---|
| 224 | if (src_fd >= 0) close(src_fd); |
|---|
| 225 | if (dst_fd >= 0) close(dst_fd); |
|---|
| 226 | */ |
|---|
| 227 | |
|---|
| 228 | } // end cmd_cp() |
|---|
| 229 | |
|---|
| 230 | ///////////////////////////////////////////////// |
|---|
| 231 | static void cmd_display( int argc , char **argv ) |
|---|
| 232 | { |
|---|
| 233 | unsigned int cxy; |
|---|
| 234 | unsigned int lid; |
|---|
| 235 | unsigned int pid; |
|---|
| 236 | unsigned int txt_id; |
|---|
| 237 | |
|---|
| 238 | if( strcmp( argv[1] , "vmm" ) == 0 ) |
|---|
| 239 | { |
|---|
| 240 | if( argc != 3 ) |
|---|
| 241 | { |
|---|
| 242 | printf(" usage: display vmm pid\n"); |
|---|
| 243 | return; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | pid = atoi(argv[2]); |
|---|
| 247 | |
|---|
| 248 | if( display_vmm( pid ) ) |
|---|
| 249 | { |
|---|
| 250 | printf(" error: illegal arguments pid = %x\n", pid ); |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | else if( strcmp( argv[1] , "sched" ) == 0 ) |
|---|
| 254 | { |
|---|
| 255 | if( argc != 4 ) |
|---|
| 256 | { |
|---|
| 257 | printf(" usage: display sched cxy lid\n"); |
|---|
| 258 | return; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | cxy = atoi(argv[2]); |
|---|
| 262 | lid = atoi(argv[3]); |
|---|
| 263 | |
|---|
| 264 | if( display_sched( cxy , lid ) ) |
|---|
| 265 | { |
|---|
| 266 | printf(" error: illegal arguments cxy = %x / lid = %d\n", cxy, lid ); |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | else if( strcmp( argv[1] , "process" ) == 0 ) |
|---|
| 270 | { |
|---|
| 271 | if( argc != 3 ) |
|---|
| 272 | { |
|---|
| 273 | printf(" usage: display process cxy\n"); |
|---|
| 274 | return; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | cxy = atoi(argv[2]); |
|---|
| 278 | |
|---|
| 279 | if( display_cluster_processes( cxy ) ) |
|---|
| 280 | { |
|---|
| 281 | printf(" error: illegal argument cxy = %x\n", cxy ); |
|---|
| 282 | } |
|---|
| 283 | } |
|---|
| 284 | else if( strcmp( argv[1] , "txt" ) == 0 ) |
|---|
| 285 | { |
|---|
| 286 | if( argc != 3 ) |
|---|
| 287 | { |
|---|
| 288 | printf(" usage: display txt txt_id\n"); |
|---|
| 289 | return; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | txt_id = atoi(argv[2]); |
|---|
| 293 | |
|---|
| 294 | if( display_txt_processes( txt_id ) ) |
|---|
| 295 | { |
|---|
| 296 | printf(" error: illegal argument txt_id = %x\n", txt_id ); |
|---|
| 297 | } |
|---|
| 298 | } |
|---|
| 299 | else if( strcmp( argv[1] , "vfs" ) == 0 ) |
|---|
| 300 | { |
|---|
| 301 | if( argc != 2 ) |
|---|
| 302 | { |
|---|
| 303 | printf(" usage: display vfs\n"); |
|---|
| 304 | return; |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | display_vfs(); |
|---|
| 308 | } |
|---|
| 309 | else if( strcmp( argv[1] , "chdev" ) == 0 ) |
|---|
| 310 | { |
|---|
| 311 | if( argc != 2 ) |
|---|
| 312 | { |
|---|
| 313 | printf(" usage: display chdev\n"); |
|---|
| 314 | return; |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | display_chdev(); |
|---|
| 318 | } |
|---|
| 319 | else |
|---|
| 320 | { |
|---|
| 321 | printf(" usage: display (vmm/sched/process/vfs/chdev/txt) [arg2] [arg3]\n"); |
|---|
| 322 | } |
|---|
| 323 | } // end cmd_display() |
|---|
| 324 | |
|---|
| 325 | ///////////////////////////////////////// |
|---|
| 326 | static void cmd_fg(int argc, char **argv) |
|---|
| 327 | { |
|---|
| 328 | unsigned int pid; |
|---|
| 329 | |
|---|
| 330 | if (argc != 2) |
|---|
| 331 | { |
|---|
| 332 | printf(" usage: %s pid\n", argv[0]); |
|---|
| 333 | return; |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | pid = atoi( argv[1] ); |
|---|
| 337 | |
|---|
| 338 | if( pid == 0 ) |
|---|
| 339 | { |
|---|
| 340 | printf(" error: ilegal pid format\n" ); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | if( fg( pid ) ) |
|---|
| 344 | { |
|---|
| 345 | printf(" error: cannot find process %x\n", pid ); |
|---|
| 346 | } |
|---|
| 347 | } // end cmd_fg() |
|---|
| 348 | |
|---|
| 349 | ////////////////////////////////////////////// |
|---|
| 350 | static void cmd_help( int argc , char **argv ) |
|---|
| 351 | { |
|---|
| 352 | unsigned int i; |
|---|
| 353 | |
|---|
| 354 | if (argc != 1) |
|---|
| 355 | { |
|---|
| 356 | printf(" usage: %s\n", argv[0]); |
|---|
| 357 | return; |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | printf("available commands:\n"); |
|---|
| 361 | for (i = 0 ; cmd[i].name ; i++) |
|---|
| 362 | { |
|---|
| 363 | printf("\t%s\t : %s\n", cmd[i].name , cmd[i].desc); |
|---|
| 364 | } |
|---|
| 365 | } // end cmd_help() |
|---|
| 366 | |
|---|
| 367 | ////////////////////////////////////////////// |
|---|
| 368 | static void cmd_kill( 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: ilegal pid format\n" ); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | if( kill( pid , SIGKILL ) ) |
|---|
| 386 | { |
|---|
| 387 | printf(" error: unable to kill process %x\n", pid ); |
|---|
| 388 | } |
|---|
| 389 | } // end cmd_kill() |
|---|
| 390 | |
|---|
| 391 | ////////////////////////////////////////////// |
|---|
| 392 | static void cmd_load( int argc , char **argv ) |
|---|
| 393 | { |
|---|
| 394 | int ret_fork; // return value from fork |
|---|
| 395 | int ret_exec; // return value from exec |
|---|
| 396 | unsigned int ksh_pid; // KSH process PID |
|---|
| 397 | unsigned int new_pid; // new process PID |
|---|
| 398 | unsigned int rcv_pid; // terminating process PID |
|---|
| 399 | char * pathname; // path to .elf file |
|---|
| 400 | unsigned int background; // background execution if non zero |
|---|
| 401 | int status; // new process exit status |
|---|
| 402 | |
|---|
| 403 | if( (argc < 2) || (argc > 3) ) |
|---|
| 404 | { |
|---|
| 405 | printf(" usage: %s pathname [&]\n", argv[0] ); |
|---|
| 406 | return; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | pathname = argv[1]; |
|---|
| 410 | |
|---|
| 411 | if( argc == 3 ) background = (argv[2][0] == '&'); |
|---|
| 412 | else background = 0; |
|---|
| 413 | |
|---|
| 414 | // get KSH process PID |
|---|
| 415 | ksh_pid = getpid(); |
|---|
| 416 | |
|---|
| 417 | // KSH process fork CHILD process |
|---|
| 418 | ret_fork = fork(); |
|---|
| 419 | |
|---|
| 420 | if ( ret_fork < 0 ) // it is a failure reported to KSH |
|---|
| 421 | { |
|---|
| 422 | printf(" error: ksh process unable to fork\n"); |
|---|
| 423 | } |
|---|
| 424 | else if (ret_fork == 0) // it is the CHILD process |
|---|
| 425 | { |
|---|
| 426 | // give back to KSH terminal ownership if required |
|---|
| 427 | if( background ) fg( ksh_pid ); |
|---|
| 428 | |
|---|
| 429 | // CHILD process exec NEW process |
|---|
| 430 | ret_exec = exec( pathname , NULL , NULL ); |
|---|
| 431 | |
|---|
| 432 | if( ret_exec ) |
|---|
| 433 | { |
|---|
| 434 | printf(" error: new process unable to exec <%s>\n", pathname ); |
|---|
| 435 | exit(0); |
|---|
| 436 | } |
|---|
| 437 | } |
|---|
| 438 | else // it is the parent KSH : ret_fork is the new process PID |
|---|
| 439 | { |
|---|
| 440 | new_pid = ret_fork; |
|---|
| 441 | |
|---|
| 442 | // wait new process completion |
|---|
| 443 | rcv_pid = wait( &status ); |
|---|
| 444 | |
|---|
| 445 | if( rcv_pid == new_pid ) |
|---|
| 446 | { |
|---|
| 447 | printf("\n\n exit %s / status = %x\n\n", pathname, (status &0xFF) ); |
|---|
| 448 | } |
|---|
| 449 | else |
|---|
| 450 | { |
|---|
| 451 | printf("\n\n abnormal termination for %s \n\n", pathname ); |
|---|
| 452 | } |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | } // end cmd_load |
|---|
| 456 | |
|---|
| 457 | ///////////////////////////////////////////// |
|---|
| 458 | static void cmd_log( int argc , char **argv ) |
|---|
| 459 | { |
|---|
| 460 | unsigned int i; |
|---|
| 461 | |
|---|
| 462 | printf("--- registered commands ---\n"); |
|---|
| 463 | for (i = 0; i < LOG_DEPTH; i++) |
|---|
| 464 | { |
|---|
| 465 | printf(" - %d\t: %s\n", i, &log_entries[i].buf); |
|---|
| 466 | } |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | //////////////////////////////////////////// |
|---|
| 470 | static void cmd_ls( int argc , char **argv ) |
|---|
| 471 | { |
|---|
| 472 | char * path; |
|---|
| 473 | |
|---|
| 474 | // struct dirent * file; |
|---|
| 475 | // DIR *dir; |
|---|
| 476 | |
|---|
| 477 | if (argc == 1) |
|---|
| 478 | { |
|---|
| 479 | path = "."; |
|---|
| 480 | } |
|---|
| 481 | else if (argc == 2) |
|---|
| 482 | { |
|---|
| 483 | path = argv[1]; |
|---|
| 484 | } |
|---|
| 485 | else |
|---|
| 486 | { |
|---|
| 487 | printf(" usage: ls [path]\n"); |
|---|
| 488 | return; |
|---|
| 489 | } |
|---|
| 490 | |
|---|
| 491 | printf(" error: not implemented yet\n"); |
|---|
| 492 | /* |
|---|
| 493 | dir = opendir( path ); |
|---|
| 494 | while ((file = readdir(dir)) != NULL) |
|---|
| 495 | { |
|---|
| 496 | printf(" %s\n", file->d_name); |
|---|
| 497 | } |
|---|
| 498 | closedir(dir); |
|---|
| 499 | */ |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | /////////////////////////////////////////////// |
|---|
| 503 | static void cmd_mkdir( int argc , char **argv ) |
|---|
| 504 | { |
|---|
| 505 | char * pathname; |
|---|
| 506 | |
|---|
| 507 | if (argc != 2) |
|---|
| 508 | { |
|---|
| 509 | printf(" usage: mkdir pathname\n"); |
|---|
| 510 | return; |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | pathname = argv[1]; |
|---|
| 514 | |
|---|
| 515 | printf(" error: not implemented yet\n"); |
|---|
| 516 | /* |
|---|
| 517 | if ( mkdir( path, 0x700) == -1 ) |
|---|
| 518 | { |
|---|
| 519 | printf(" error: cannot create directory %s\n", path); |
|---|
| 520 | } |
|---|
| 521 | */ |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | //////////////////////////////////////////// |
|---|
| 525 | static void cmd_mv( int argc , char **argv ) |
|---|
| 526 | { |
|---|
| 527 | |
|---|
| 528 | if (argc < 3) |
|---|
| 529 | { |
|---|
| 530 | printf(" usage : %s src_pathname dst_pathname\n", argv[0]); |
|---|
| 531 | return; |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | printf(" error: not implemented yet\n"); |
|---|
| 535 | |
|---|
| 536 | /* |
|---|
| 537 | int ret = giet_fat_rename(argv[1], argv[2]); |
|---|
| 538 | if (ret < 0) |
|---|
| 539 | { |
|---|
| 540 | printf(" error : cannot move %s to %s / err = %d\n", argv[1], argv[2], ret ); |
|---|
| 541 | } |
|---|
| 542 | */ |
|---|
| 543 | |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | ///////////////////////////////////////////// |
|---|
| 547 | static void cmd_pwd( int argc , char **argv ) |
|---|
| 548 | { |
|---|
| 549 | char buf[1024]; |
|---|
| 550 | |
|---|
| 551 | if (argc != 1) |
|---|
| 552 | { |
|---|
| 553 | printf(" usage: pwd\n"); |
|---|
| 554 | return; |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | if ( getcwd( buf , 1024 ) ) |
|---|
| 558 | { |
|---|
| 559 | printf(" error: unable to get current directory\n"); |
|---|
| 560 | } |
|---|
| 561 | else |
|---|
| 562 | { |
|---|
| 563 | printf("%s\n", buf); |
|---|
| 564 | } |
|---|
| 565 | } |
|---|
| 566 | |
|---|
| 567 | //////////////////////////////////////////// |
|---|
| 568 | static void cmd_rm( int argc , char **argv ) |
|---|
| 569 | { |
|---|
| 570 | char * pathname; |
|---|
| 571 | |
|---|
| 572 | if (argc != 2) |
|---|
| 573 | { |
|---|
| 574 | printf(" usage: rm pathname\n"); |
|---|
| 575 | return; |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | pathname = argv[1]; |
|---|
| 579 | |
|---|
| 580 | printf(" error: not implemented yet\n"); |
|---|
| 581 | /* |
|---|
| 582 | if (remove(path) == -1) |
|---|
| 583 | { |
|---|
| 584 | printf(" error: cannot remove %s\n", path); |
|---|
| 585 | } |
|---|
| 586 | */ |
|---|
| 587 | |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | /////////////////////////////////////////////// |
|---|
| 591 | static void cmd_rmdir( int argc , char **argv ) |
|---|
| 592 | { |
|---|
| 593 | cmd_rm(argc, argv); |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | ////////////////////////////////////////////////////////////////// |
|---|
| 597 | // Array of commands |
|---|
| 598 | ////////////////////////////////////////////////////////////////// |
|---|
| 599 | |
|---|
| 600 | ksh_cmd_t cmd[] = |
|---|
| 601 | { |
|---|
| 602 | { "cat", "display file content", cmd_cat }, |
|---|
| 603 | { "cd", "change current directory", cmd_cd }, |
|---|
| 604 | { "cp", "replicate a file in file system", cmd_cp }, |
|---|
| 605 | { "fg", "put a process in foreground", cmd_fg }, |
|---|
| 606 | { "display", "display vmm/sched/process/vfs/chdev/txt", cmd_display }, |
|---|
| 607 | { "load", "load an user application", cmd_load }, |
|---|
| 608 | { "help", "list available commands", cmd_help }, |
|---|
| 609 | { "kill", "kill an application (all threads)", cmd_kill }, |
|---|
| 610 | { "log", "list registered commands", cmd_log }, |
|---|
| 611 | { "ls", "list directory entries", cmd_ls }, |
|---|
| 612 | { "mkdir", "create a new directory", cmd_mkdir }, |
|---|
| 613 | { "mv", "move a file in file system", cmd_mv }, |
|---|
| 614 | { "pwd", "print current working directory", cmd_pwd }, |
|---|
| 615 | { "rm", "remove a file from file system", cmd_rm }, |
|---|
| 616 | { "rmdir", "remove a directory from file system", cmd_rmdir }, |
|---|
| 617 | { NULL, NULL, NULL } |
|---|
| 618 | }; |
|---|
| 619 | |
|---|
| 620 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 621 | // This function analyses one command (with arguments), execute it, and return. |
|---|
| 622 | //////////////////////////////////////////////////////////////////////////////////// |
|---|
| 623 | static void parse(char *buf) |
|---|
| 624 | { |
|---|
| 625 | int argc = 0; |
|---|
| 626 | char *argv[MAX_ARGS]; |
|---|
| 627 | int i; |
|---|
| 628 | int len = strlen(buf); |
|---|
| 629 | |
|---|
| 630 | // build argc/argv |
|---|
| 631 | for (i = 0; i < len; i++) |
|---|
| 632 | { |
|---|
| 633 | if (buf[i] == ' ') |
|---|
| 634 | { |
|---|
| 635 | buf[i] = '\0'; |
|---|
| 636 | } |
|---|
| 637 | else if (i == 0 || buf[i - 1] == '\0') |
|---|
| 638 | { |
|---|
| 639 | if (argc < MAX_ARGS) |
|---|
| 640 | { |
|---|
| 641 | argv[argc] = &buf[i]; |
|---|
| 642 | argc++; |
|---|
| 643 | } |
|---|
| 644 | } |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | if (argc > 0) |
|---|
| 648 | { |
|---|
| 649 | int found = 0; |
|---|
| 650 | |
|---|
| 651 | argv[argc] = NULL; |
|---|
| 652 | |
|---|
| 653 | // try to match typed command |
|---|
| 654 | for (i = 0; cmd[i].name; i++) |
|---|
| 655 | { |
|---|
| 656 | if (strcmp(argv[0], cmd[i].name) == 0) |
|---|
| 657 | { |
|---|
| 658 | cmd[i].fn(argc, argv); |
|---|
| 659 | found = 1; |
|---|
| 660 | break; |
|---|
| 661 | } |
|---|
| 662 | } |
|---|
| 663 | |
|---|
| 664 | if (!found) |
|---|
| 665 | { |
|---|
| 666 | printf(" undefined command <%s>\n", argv[0]); |
|---|
| 667 | } |
|---|
| 668 | } |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | /////////////////////////////////// |
|---|
| 672 | int main( int argc , char *argv[] ) |
|---|
| 673 | { |
|---|
| 674 | char c; // read character |
|---|
| 675 | char buf[CMD_MAX_SIZE]; // buffer for one command |
|---|
| 676 | unsigned int count = 0; // pointer in buf |
|---|
| 677 | unsigned int i; // index for loops |
|---|
| 678 | |
|---|
| 679 | enum fsm_states |
|---|
| 680 | { |
|---|
| 681 | NORMAL, |
|---|
| 682 | ESCAPE, |
|---|
| 683 | BRAKET, |
|---|
| 684 | }; |
|---|
| 685 | |
|---|
| 686 | // log buffer initialisation |
|---|
| 687 | memset( &log_entries , 0, sizeof(log_entries)); |
|---|
| 688 | ptw = 0; |
|---|
| 689 | ptr = 0; |
|---|
| 690 | |
|---|
| 691 | printf( "~~~ shell ~~~\n\n" ); |
|---|
| 692 | |
|---|
| 693 | // command buffer initialisation |
|---|
| 694 | memset( buf, 0x20 , sizeof(buf) ); |
|---|
| 695 | count = 0; |
|---|
| 696 | |
|---|
| 697 | // display first prompt |
|---|
| 698 | printf("# "); |
|---|
| 699 | |
|---|
| 700 | // This lexical analyser writes one command line in the buf buffer. |
|---|
| 701 | // It is implemented as a 3 states FSM to handle the following sequences: |
|---|
| 702 | // - ESC [ A : up arrow |
|---|
| 703 | // - ESC [ B : down arrow |
|---|
| 704 | // - ESC [ C : right arrow |
|---|
| 705 | // - ESC [ D : left arrow |
|---|
| 706 | // The three states have the following semantic: |
|---|
| 707 | // - NORMAL : no (ESC) character has been found |
|---|
| 708 | // - ESCAPE : the character (ESC) has been found |
|---|
| 709 | // - BRAKET : the wo characters (ESC,[) have been found |
|---|
| 710 | |
|---|
| 711 | unsigned int state = NORMAL; |
|---|
| 712 | |
|---|
| 713 | // @@@ |
|---|
| 714 | // parse("load /bin/user/sort.elf"); |
|---|
| 715 | // @@@ |
|---|
| 716 | |
|---|
| 717 | char string[64]; |
|---|
| 718 | |
|---|
| 719 | while (1) |
|---|
| 720 | { |
|---|
| 721 | c = (char)getchar(); |
|---|
| 722 | |
|---|
| 723 | if( c == 0 ) continue; |
|---|
| 724 | |
|---|
| 725 | switch (state) |
|---|
| 726 | { |
|---|
| 727 | case NORMAL: |
|---|
| 728 | { |
|---|
| 729 | if ((c == '\b') || (c == 0x7F)) // backspace => remove one character |
|---|
| 730 | { |
|---|
| 731 | if (count > 0) { |
|---|
| 732 | printf("\b \b"); |
|---|
| 733 | count--; |
|---|
| 734 | } |
|---|
| 735 | } |
|---|
| 736 | else if (c == '\n') // new line => call parser to execute command |
|---|
| 737 | { |
|---|
| 738 | if (count > 0) |
|---|
| 739 | { |
|---|
| 740 | // complete command |
|---|
| 741 | buf[count] = '\0'; |
|---|
| 742 | |
|---|
| 743 | // register command in log arrays |
|---|
| 744 | strcpy(log_entries[ptw].buf, buf); |
|---|
| 745 | log_entries[ptw].count = count; |
|---|
| 746 | ptw = (ptw + 1) % LOG_DEPTH; |
|---|
| 747 | ptr = ptw; |
|---|
| 748 | |
|---|
| 749 | // execute command |
|---|
| 750 | printf("\n"); |
|---|
| 751 | parse((char *)&buf); |
|---|
| 752 | |
|---|
| 753 | // reinitialise buffer and display prompt |
|---|
| 754 | for ( i = 0 ; i < sizeof(buf) ; i++ ) buf[i] = 0x20; |
|---|
| 755 | count = 0; |
|---|
| 756 | printf("# "); |
|---|
| 757 | } |
|---|
| 758 | } |
|---|
| 759 | else if (c == '\t') // tabulation => do nothing |
|---|
| 760 | { |
|---|
| 761 | } |
|---|
| 762 | else if (c == (char)0x1B) // ESC => start an escape sequence |
|---|
| 763 | { |
|---|
| 764 | state = ESCAPE; |
|---|
| 765 | } |
|---|
| 766 | else if (c == 0x03) // ^C => cancel current command |
|---|
| 767 | { |
|---|
| 768 | for (i = 0; i < count; i++) printf("\b \b"); |
|---|
| 769 | for (i = 0; i < sizeof(buf); i++) buf[i] = 0x20; |
|---|
| 770 | count = 0; |
|---|
| 771 | } |
|---|
| 772 | else // register character in command buffer |
|---|
| 773 | { |
|---|
| 774 | if (count < sizeof(buf) - 1) |
|---|
| 775 | { |
|---|
| 776 | putchar( c ); |
|---|
| 777 | buf[count] = c; |
|---|
| 778 | count++; |
|---|
| 779 | } |
|---|
| 780 | } |
|---|
| 781 | break; |
|---|
| 782 | } |
|---|
| 783 | case ESCAPE: |
|---|
| 784 | { |
|---|
| 785 | if (c == '[') // valid sequence => continue |
|---|
| 786 | { |
|---|
| 787 | state = BRAKET; |
|---|
| 788 | } |
|---|
| 789 | else // invalid sequence => do nothing |
|---|
| 790 | { |
|---|
| 791 | state = NORMAL; |
|---|
| 792 | } |
|---|
| 793 | break; |
|---|
| 794 | } |
|---|
| 795 | case BRAKET: |
|---|
| 796 | { |
|---|
| 797 | if (c == 'D') // valid LEFT sequence => move buf pointer left |
|---|
| 798 | { |
|---|
| 799 | if (count > 0) |
|---|
| 800 | { |
|---|
| 801 | printf("\b"); |
|---|
| 802 | count--; |
|---|
| 803 | } |
|---|
| 804 | |
|---|
| 805 | // get next user char |
|---|
| 806 | state = NORMAL; |
|---|
| 807 | } |
|---|
| 808 | else if (c == 'C') // valid RIGHT sequence => move buf pointer right |
|---|
| 809 | { |
|---|
| 810 | if (count < sizeof(buf) - 1) |
|---|
| 811 | { |
|---|
| 812 | printf("%c", buf[count]); |
|---|
| 813 | count++; |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | // get next user char |
|---|
| 817 | state = NORMAL; |
|---|
| 818 | } |
|---|
| 819 | else if (c == 'A') // valid UP sequence => move log pointer backward |
|---|
| 820 | { |
|---|
| 821 | // cancel current command |
|---|
| 822 | for (i = 0; i < count; i++) printf("\b \b"); |
|---|
| 823 | count = 0; |
|---|
| 824 | |
|---|
| 825 | // copy log command into buf |
|---|
| 826 | ptr = (ptr - 1) % LOG_DEPTH; |
|---|
| 827 | strcpy(buf, log_entries[ptr].buf); |
|---|
| 828 | count = log_entries[ptr].count; |
|---|
| 829 | |
|---|
| 830 | // display log command |
|---|
| 831 | printf("%s", buf); |
|---|
| 832 | |
|---|
| 833 | // get next user char |
|---|
| 834 | state = NORMAL; |
|---|
| 835 | } |
|---|
| 836 | else if (c == 'B') // valid DOWN sequence => move log pointer forward |
|---|
| 837 | { |
|---|
| 838 | // cancel current command |
|---|
| 839 | for (i = 0 ; i < count; i++) printf("\b \b"); |
|---|
| 840 | count = 0; |
|---|
| 841 | |
|---|
| 842 | // copy log command into buf |
|---|
| 843 | ptr = (ptr + 1) % LOG_DEPTH; |
|---|
| 844 | strcpy(buf, log_entries[ptr].buf); |
|---|
| 845 | count = log_entries[ptr].count; |
|---|
| 846 | |
|---|
| 847 | // display log command |
|---|
| 848 | printf("%s", buf); |
|---|
| 849 | |
|---|
| 850 | // get next user char |
|---|
| 851 | state = NORMAL; |
|---|
| 852 | } |
|---|
| 853 | else // other character => do nothing |
|---|
| 854 | { |
|---|
| 855 | // get next user char |
|---|
| 856 | state = NORMAL; |
|---|
| 857 | } |
|---|
| 858 | break; |
|---|
| 859 | } |
|---|
| 860 | } |
|---|
| 861 | } |
|---|
| 862 | } // end main() |
|---|
| 863 | |
|---|