Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.7KB

  1. ; pgm - execute programs loaded from filesystem
  2. ;
  3. ; Implements a shell hook that searches the filesystem for a file with the same
  4. ; name as the cmd, loads that file in memory and executes it, sending the
  5. ; program a pointer to *unparsed* arguments in HL.
  6. ;
  7. ; We expect the loaded program to return a status code in A. 0 means success,
  8. ; non-zero means error. Programs should avoid having error code overlaps with
  9. ; the shell so that we know where the error comes from.
  10. ;
  11. ; *** Requirements ***
  12. ; fs
  13. ;
  14. ; *** Defines ***
  15. ; PGM_CODEADDR: Memory address where to place the code we load.
  16. ;
  17. ; *** Variables ***
  18. .equ PGM_HANDLE PGM_RAMSTART
  19. .equ PGM_RAMEND PGM_HANDLE+FS_HANDLE_SIZE
  20. ; Routine suitable to plug into SHELL_CMDHOOK. HL points to the full cmdline.
  21. ; which has been processed to replace the first ' ' with a null char.
  22. pgmShellHook:
  23. call fsIsOn
  24. jr nz, .noFile
  25. ; (HL) is suitable for a direct fsFindFN call
  26. call fsFindFN
  27. jr nz, .noFile
  28. ; We have a file! Advance HL to args
  29. xor a
  30. call findchar
  31. inc hl ; beginning of args
  32. ; Alright, ready to run!
  33. jp pgmRun
  34. .noFile:
  35. ld a, SHELL_ERR_IO_ERROR
  36. ret
  37. ; Loads code in file that FS_PTR is currently pointing at and place it in
  38. ; PGM_CODEADDR. Then, jump to PGM_CODEADDR. We expect HL to point to unparsed
  39. ; arguments being given to the program.
  40. pgmRun:
  41. call fsIsValid
  42. jr nz, .ioError
  43. push hl ; unparsed args
  44. ld ix, PGM_HANDLE
  45. call fsOpen
  46. ld hl, 0 ; addr that we read in file handle
  47. ld de, PGM_CODEADDR ; addr in mem we write to
  48. .loop:
  49. call fsGetC ; we use Z at end of loop
  50. ld (de), a ; Z preserved
  51. inc hl ; Z preserved in 16-bit
  52. inc de ; Z preserved in 16-bit
  53. jr z, .loop
  54. pop hl ; recall args
  55. ; ready to jump!
  56. jp PGM_CODEADDR
  57. .ioError:
  58. ld a, SHELL_ERR_IO_ERROR
  59. ret