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