shell.b 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. implement InitShell;
  2. include "sys.m";
  3. include "draw.m";
  4. sys: Sys;
  5. InitShell: module
  6. {
  7. init: fn(nil: ref Draw->Context, nil: list of string);
  8. };
  9. Sh: module
  10. {
  11. init: fn(ctxt: ref Draw->Context, argv: list of string);
  12. };
  13. init(nil: ref Draw->Context, nil: list of string)
  14. {
  15. shell := load Sh "/dis/sh.dis";
  16. sys = load Sys Sys->PATH;
  17. if(sys != nil)
  18. sys->print("init: starting shell\n");
  19. # sys->bind("#I", "/net", sys->MAFTER); # IP
  20. sys->bind("#p", "/prog", sys->MREPL); # prog device
  21. sys->bind("#d", "/fd", Sys->MREPL);
  22. sys->bind("#i", "/dev", sys->MREPL); # draw device
  23. sys->bind("#t", "/dev", sys->MAFTER); # serial line
  24. sys->bind("#c", "/dev", sys->MAFTER); # console device
  25. sys->bind("#W","/dev",sys->MAFTER); # Flash
  26. # sys->bind("#O", "/dev", sys->MAFTER); # Modem
  27. # sys->bind("#T","/dev",sys->MAFTER); # Touchscreen
  28. srv();
  29. spawn shell->init(nil, nil);
  30. }
  31. srv()
  32. {
  33. remotedebug := sysenv("remotedebug");
  34. if(remotedebug != "1")
  35. return;
  36. sys->print("srv...");
  37. if(echoto("#t/eia0ctl", "b38400") < 0)
  38. return;
  39. fd := sys->open("/dev/eia0", Sys->ORDWR);
  40. if (fd == nil) {
  41. sys->print("eia data open: %r\n");
  42. return;
  43. }
  44. if (sys->export(fd, "/", Sys->EXPASYNC) < 0) {
  45. sys->print("export: %r\n");
  46. return;
  47. }
  48. sys->print("ok\n");
  49. }
  50. sysenv(param: string): string
  51. {
  52. fd := sys->open("#c/sysenv", sys->OREAD);
  53. if (fd == nil)
  54. return(nil);
  55. buf := array[4096] of byte;
  56. nb := sys->read(fd, buf, len buf);
  57. (nfl,fl) := sys->tokenize(string buf, "\n");
  58. while (fl != nil) {
  59. pair := hd fl;
  60. (npl, pl) := sys->tokenize(pair, "=");
  61. if (npl > 1) {
  62. if ((hd pl) == param)
  63. return hd tl pl;
  64. }
  65. fl = tl fl;
  66. }
  67. return nil ;
  68. }
  69. echoto(fname, str: string): int
  70. {
  71. fd := sys->open(fname, Sys->OWRITE);
  72. if(fd == nil) {
  73. sys->print("%s: %r\n", fname);
  74. return -1;
  75. }
  76. x := array of byte str;
  77. if(sys->write(fd, x, len x) == -1) {
  78. sys->print("write: %r\n");
  79. return -1;
  80. }
  81. return 0;
  82. }