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