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.

74 lines
2.1KB

  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. ; We can mutate it because the shell doesn't do anything with it afterwards.
  22. pgmShellHook:
  23. call fsIsOn
  24. jr nz, .noFile
  25. ; first first space and replace it with zero so that we have something
  26. ; suitable for fsFindFN.
  27. push hl ; remember beginning
  28. ld a, ' '
  29. call findchar
  30. jr nz, .noarg ; if we have no arg, we want DE to point to the
  31. ; null char. Also, we have no replacement to
  32. ; make
  33. ; replace space with nullchar
  34. xor a
  35. ld (hl), a
  36. inc hl ; make HL point to the beginning of args
  37. .noarg:
  38. ex de, hl ; DE now points to the beginning of args or to \0 if
  39. ; no args
  40. pop hl ; HL points to cmdname, properly null-terminated
  41. call fsFindFN
  42. jr nz, .noFile
  43. ; We have a file! Load it and run it.
  44. ex de, hl ; but first, make HL point to unparsed args.
  45. jp pgmRun
  46. .noFile:
  47. ld a, SHELL_ERR_IO_ERROR
  48. ret
  49. ; Loads code in file that FS_PTR is currently pointing at and place it in
  50. ; PGM_CODEADDR. Then, jump to PGM_CODEADDR. We expect HL to point to unparsed
  51. ; arguments being given to the program.
  52. pgmRun:
  53. call fsIsValid
  54. jr nz, .ioError
  55. push hl ; unparsed args
  56. ld ix, PGM_HANDLE
  57. call fsOpen
  58. ld hl, PGM_CODEADDR
  59. .loop:
  60. call fsGetC ; we use Z at end of loop
  61. ld (hl), a ; Z preserved
  62. inc hl ; Z preserved in 16-bit
  63. jr z, .loop
  64. pop hl ; recall args
  65. ; ready to jump!
  66. jp PGM_CODEADDR
  67. .ioError:
  68. ld a, SHELL_ERR_IO_ERROR
  69. ret