shell.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // Max length of a single command
  2. #define SHELL_CMD_MAX_LENGTH 128
  3. #define SHELL_BIN_PATH "/bin/"
  4. word shell_cmd[SHELL_CMD_MAX_LENGTH];
  5. word shell_cmd_idx = 0;
  6. word* shell_tokens[SHELL_CMD_MAX_LENGTH >> 1]; // Array of tokens in the command
  7. word shell_num_tokens = 0;
  8. word shell_path[MAX_PATH_LENGTH];
  9. /**
  10. * Print the shell prompt
  11. */
  12. void shell_print_prompt()
  13. {
  14. GFX_PrintConsole(shell_path);
  15. GFX_PrintConsole("> ");
  16. }
  17. /**
  18. * Clear the current command buffer
  19. */
  20. void shell_clear_command()
  21. {
  22. shell_cmd[0] = 0;
  23. shell_cmd_idx = 0;
  24. shell_tokens[0] = 0;
  25. shell_num_tokens = 0;
  26. }
  27. /**
  28. * Append character to command buffer
  29. */
  30. void shell_append_command(char c)
  31. {
  32. if (shell_cmd_idx < SHELL_CMD_MAX_LENGTH)
  33. {
  34. shell_cmd[shell_cmd_idx] = c;
  35. shell_cmd_idx++;
  36. shell_cmd[shell_cmd_idx] = 0; // Terminate
  37. }
  38. }
  39. /**
  40. * Parse the command buffer into tokens
  41. */
  42. void shell_parse_command()
  43. {
  44. // Strip trailing spaces
  45. word i = shell_cmd_idx - 1;
  46. while (i >= 0 && shell_cmd[i] == ' ')
  47. {
  48. shell_cmd[i] = 0;
  49. i--;
  50. }
  51. shell_num_tokens = 0;
  52. shell_tokens[shell_num_tokens] = strtok(shell_cmd, " ");
  53. while((word)shell_tokens[shell_num_tokens] != -1)
  54. {
  55. shell_num_tokens++;
  56. shell_tokens[shell_num_tokens] = strtok(-1, " ");
  57. }
  58. }
  59. /**
  60. * Print help
  61. */
  62. void shell_print_help()
  63. {
  64. GFX_PrintConsole(
  65. "************\n"
  66. "*BDOS Shell*\n"
  67. "************\n"
  68. "Available commands:\n"
  69. "- cd <path>\n"
  70. "- clear\n"
  71. "- format <blk size> <blk cnt>\n"
  72. "- sync\n"
  73. "- help\n"
  74. "\n"
  75. "Programs are run from:\n"
  76. "- /bin\n"
  77. "- <current dir>\n"
  78. );
  79. }
  80. /**
  81. * Attempt to run program from FS
  82. * If run_from_path is 1, the program is run from the SHELL_BIN_PATH
  83. * Returns 1 if program was found, 0 otherwise
  84. */
  85. word shell_run_program(word run_from_path)
  86. {
  87. // Create absolute path to program
  88. char absolute_path[MAX_PATH_LENGTH];
  89. // Check if program is relative or absolute
  90. if (shell_tokens[0][0] == '/')
  91. {
  92. strcpy(absolute_path, shell_tokens[0]);
  93. }
  94. else
  95. {
  96. if (run_from_path)
  97. {
  98. strcpy(absolute_path, SHELL_BIN_PATH);
  99. strcat(absolute_path, shell_tokens[0]);
  100. }
  101. else
  102. {
  103. // Append to current path
  104. strcpy(absolute_path, shell_path);
  105. if (strlen(absolute_path) + strlen(shell_tokens[0]) < MAX_PATH_LENGTH)
  106. {
  107. // If not root, append slash
  108. if (strcmp(shell_path, "/") != 0)
  109. {
  110. strcat(absolute_path, "/");
  111. }
  112. strcat(absolute_path, shell_tokens[0]);
  113. }
  114. else
  115. {
  116. GFX_PrintConsole("Path too long\n");
  117. return 0;
  118. }
  119. }
  120. }
  121. // Get filesize of the program
  122. struct brfs_dir_entry* dir = brfs_stat(absolute_path);
  123. if ((word)dir == -1)
  124. {
  125. //GFX_PrintConsole("Program not found\n");
  126. return 0;
  127. }
  128. word filesize = dir->filesize;
  129. // Attempt to open file
  130. word fp = brfs_open_file(absolute_path);
  131. if (fp == -1)
  132. {
  133. GFX_PrintConsole("Could not open file\n");
  134. return 0;
  135. }
  136. // Set cursor to start of file
  137. brfs_set_cursor(fp, 0);
  138. // Read program into memory
  139. word* program = (word*) RUN_ADDR;
  140. if (!brfs_read(fp, program, filesize))
  141. {
  142. GFX_PrintConsole("Could not read file\n");
  143. return 0;
  144. }
  145. // Run program
  146. // Indicate that a user program is running
  147. bdos_userprogram_running = 1;
  148. uprintln("Running program...");
  149. // jump to the program
  150. asm(
  151. "; backup registers\n"
  152. "push r1\n"
  153. "push r2\n"
  154. "push r3\n"
  155. "push r4\n"
  156. "push r5\n"
  157. "push r6\n"
  158. "push r7\n"
  159. "push r8\n"
  160. "push r9\n"
  161. "push r10\n"
  162. "push r11\n"
  163. "push r12\n"
  164. "push r13\n"
  165. "push r14\n"
  166. "push r15\n"
  167. //"ccache\n"
  168. "savpc r1\n"
  169. "push r1\n"
  170. "jump 0x400000\n"
  171. "; restore registers\n"
  172. "pop r15\n"
  173. "pop r14\n"
  174. "pop r13\n"
  175. "pop r12\n"
  176. "pop r11\n"
  177. "pop r10\n"
  178. "pop r9\n"
  179. "pop r8\n"
  180. "pop r7\n"
  181. "pop r6\n"
  182. "pop r5\n"
  183. "pop r4\n"
  184. "pop r3\n"
  185. "pop r2\n"
  186. "pop r1\n"
  187. );
  188. // Indicate that no user program is running anymore
  189. bdos_userprogram_running = 0;
  190. bdos_restore();
  191. // Close file
  192. brfs_close_file(fp);
  193. return 1;
  194. }
  195. /**
  196. * List the contents of a directory (TMP function until separate ls program is implemented)
  197. * dir_path: full path of the directory
  198. */
  199. void shell_list_directory(char* dir_path)
  200. {
  201. // Find data block address of parent directory path
  202. word dir_fat_idx = brfs_get_fat_idx_of_dir(dir_path);
  203. if (dir_fat_idx == -1)
  204. {
  205. GFX_PrintConsole("Directory not found\n");
  206. return;
  207. }
  208. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  209. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->words_per_block);
  210. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  211. word i;
  212. for (i = 0; i < dir_entries_max; i++)
  213. {
  214. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  215. if (dir_entry->filename[0] != 0)
  216. {
  217. char decompressed_filename[17];
  218. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  219. GFX_PrintConsole(decompressed_filename);
  220. GFX_PrintConsole(" FAT: ");
  221. GFX_PrintDecConsole((dir_entry->fat_idx));
  222. GFX_PrintConsole(" Flg: ");
  223. GFX_PrintDecConsole((dir_entry->flags));
  224. GFX_PrintConsole(" Size: ");
  225. GFX_PrintDecConsole((dir_entry->filesize));
  226. GFX_PrintConsole("\n");
  227. }
  228. }
  229. }
  230. /**
  231. * Process dots in path so that ".." goes up one directory and "." is skipped
  232. */
  233. void shell_process_dots(char* path)
  234. {
  235. // Create a copy of the path
  236. char path_copy[MAX_PATH_LENGTH];
  237. strcpy(path_copy, path);
  238. // Split path by '/' and traverse directories
  239. word num_tokens = 0;
  240. char* tokens[MAX_PATH_LENGTH >> 1];
  241. tokens[num_tokens] = strtok(path_copy+1, "/");
  242. while((word)tokens[num_tokens] != -1)
  243. {
  244. num_tokens++;
  245. tokens[num_tokens] = strtok(-1, "/");
  246. }
  247. if (num_tokens == 0)
  248. {
  249. return;
  250. }
  251. strcpy(path, "/");
  252. // Traverse path
  253. word i = 0;
  254. while (i < num_tokens)
  255. {
  256. if (strcmp(tokens[i], ".") == 0)
  257. {
  258. // Skip
  259. }
  260. else if (strcmp(tokens[i], "..") == 0)
  261. {
  262. // Remove all characters after last slash
  263. word j = strlen(path) - 1;
  264. while (j > 0 && path[j] != '/')
  265. {
  266. path[j] = 0;
  267. j--;
  268. }
  269. }
  270. else
  271. {
  272. // Copy token
  273. if (strlen(path) > 1)
  274. {
  275. strcat(path, "/");
  276. }
  277. strcat(path, tokens[i]);
  278. }
  279. i++;
  280. }
  281. }
  282. /**
  283. * Change directory
  284. */
  285. void shell_change_directory()
  286. {
  287. if (shell_num_tokens != 2)
  288. {
  289. GFX_PrintConsole("Usage: cd <path>\n");
  290. return;
  291. }
  292. // Remove trailing slashes, skipping the first character
  293. word i = strlen(shell_tokens[1]) - 1;
  294. while (i >= 1 && shell_tokens[1][i] == '/')
  295. {
  296. shell_tokens[1][i] = 0;
  297. i--;
  298. }
  299. // Check if path is root
  300. if (strcmp(shell_tokens[1], "/") == 0)
  301. {
  302. strcpy(shell_path, "/");
  303. return;
  304. }
  305. char absolute_path[MAX_PATH_LENGTH];
  306. // Create absolute path
  307. // Check if argument is relative or absolute
  308. if (shell_tokens[1][0] == '/')
  309. {
  310. strcpy(absolute_path, shell_tokens[1]);
  311. }
  312. else
  313. {
  314. // Append to current path
  315. strcpy(absolute_path, shell_path);
  316. if (strlen(absolute_path) + strlen(shell_tokens[1]) < MAX_PATH_LENGTH)
  317. {
  318. // If not root, append slash
  319. if (strcmp(shell_path, "/") != 0)
  320. {
  321. strcat(absolute_path, "/");
  322. }
  323. strcat(absolute_path, shell_tokens[1]);
  324. }
  325. else
  326. {
  327. GFX_PrintConsole("Path too long\n");
  328. }
  329. }
  330. // Get dir entry of the path
  331. struct brfs_dir_entry* dir = brfs_stat(absolute_path);
  332. // Check if valid and is a directory
  333. if ((word)dir != -1 && dir->flags & (1 << 0))
  334. {
  335. // Set new path
  336. strcpy(shell_path, absolute_path);
  337. // Process dots in path
  338. shell_process_dots(shell_path);
  339. }
  340. else
  341. {
  342. GFX_PrintConsole("Directory not found\n");
  343. }
  344. }
  345. /**
  346. * Format the filesystem
  347. */
  348. void shell_format_filesystem()
  349. {
  350. if (shell_num_tokens < 3)
  351. {
  352. GFX_PrintConsole("Usage: format <blk size> <blk cnt>\n");
  353. return;
  354. }
  355. word block_size = strToInt(shell_tokens[1]);
  356. word block_count = strToInt(shell_tokens[2]);
  357. word full_format = 1;
  358. brfs_format(block_count, block_size, "FPGC", full_format);
  359. brfs_write_to_flash();
  360. }
  361. /**
  362. * Handle the command
  363. */
  364. void shell_handle_command()
  365. {
  366. if (strcmp(shell_tokens[0], "cd") == 0)
  367. {
  368. shell_change_directory();
  369. }
  370. else if (strcmp(shell_tokens[0], "clear") == 0)
  371. {
  372. // clear screen by clearing window tables and resetting the cursor
  373. GFX_clearWindowtileTable();
  374. GFX_clearWindowpaletteTable();
  375. GFX_cursor = 0;
  376. }
  377. else if (strcmp(shell_tokens[0], "ls") == 0)
  378. {
  379. if (shell_num_tokens > 1)
  380. {
  381. shell_list_directory(shell_tokens[1]);
  382. }
  383. else
  384. {
  385. shell_list_directory(shell_path);
  386. }
  387. }
  388. else if (strcmp(shell_tokens[0], "help") == 0)
  389. {
  390. shell_print_help();
  391. }
  392. else if (strcmp(shell_tokens[0], "sync") == 0)
  393. {
  394. brfs_write_to_flash();
  395. }
  396. else if (strcmp(shell_tokens[0], "read") == 0)
  397. {
  398. brfs_read_from_flash();
  399. }
  400. else if (strcmp(shell_tokens[0], "dump") == 0)
  401. {
  402. // Dump FAT
  403. brfs_dump_section(brfs_ram_storage+SUPERBLOCK_SIZE, strToInt(shell_tokens[1]), 16);
  404. }
  405. else if (strcmp(shell_tokens[0], "format") == 0)
  406. {
  407. shell_format_filesystem();
  408. }
  409. // Attempt to run program both from local dir and from SHELL_BIN_PATH
  410. else if (!shell_run_program(0))
  411. {
  412. if (!shell_run_program(1))
  413. {
  414. GFX_PrintConsole("Command not found\n");
  415. }
  416. }
  417. }
  418. /**
  419. * Initialize shell
  420. */
  421. void shell_init()
  422. {
  423. shell_clear_command();
  424. // Set path to root
  425. strcpy(shell_path, "/");
  426. shell_print_prompt();
  427. }
  428. /**
  429. * Main shell loop
  430. */
  431. void shell_loop()
  432. {
  433. // Read command from HID FIFO
  434. if (HID_FifoAvailable())
  435. {
  436. char c = HID_FifoRead();
  437. if (c == '\n')
  438. {
  439. GFX_PrintcConsole('\n');
  440. shell_parse_command();
  441. if (shell_num_tokens != 0)
  442. {
  443. shell_handle_command();
  444. }
  445. shell_clear_command();
  446. shell_print_prompt();
  447. }
  448. else if (c == 0x8) // backspace
  449. {
  450. if (shell_cmd_idx > 0)
  451. {
  452. shell_cmd_idx--;
  453. shell_cmd[shell_cmd_idx] = 0;
  454. GFX_PrintcConsole(0x8);
  455. }
  456. }
  457. else
  458. {
  459. // Append character to command buffer and print it
  460. shell_append_command(c);
  461. GFX_PrintcConsole(c);
  462. }
  463. }
  464. }