InterpreterShell.Mod 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. MODULE InterpreterShell; (** AUTHOR "be"; PURPOSE "Simple command shell" **)
  2. (**
  3. * Simple echo-based command shell.
  4. *
  5. * History:
  6. *
  7. * 16.05.2006 Added command history, backspace key handling, factored out serial port related code into ShellSerials.Mod (staubesv)
  8. *
  9. *)
  10. IMPORT Modules, Commands, Streams, Pipes, Strings, Files, Interpreter := FoxInterpreter, Diagnostics, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree, Printout := FoxPrintout, InterpreterSymbols := FoxInterpreterSymbols;
  11. CONST
  12. (* Notify procedure command codes *)
  13. ExitShell* = 1;
  14. Clear* = 2;
  15. Version = "InterpreterShell v1.0";
  16. DefaultAliasFile = "Shell.Alias";
  17. NestingLevelIndicator = ">";
  18. MaxLen = 512;
  19. CmdLen = 64;
  20. ParamLen = MaxLen;
  21. CR = 0DX; LF = 0AX; TAB = 9X;
  22. Backspace = 08X;
  23. Space = 20X;
  24. Delete = 7FX;
  25. Escape = 1BX;
  26. EscapeChar1 = Escape;
  27. EscapeChar2 = '[';
  28. (* Non-ASCII characters *)
  29. CursorUp = 0C1X;
  30. CursorDown = 0C2X;
  31. (* symbols *)
  32. start = {};
  33. inputFile = {0}; (* 01H *)
  34. pipe = {1}; (* 02H *)
  35. outputFile = {2}; (* 04H *)
  36. outputFileAppend = {3}; (* 08H *)
  37. ampersand = {4}; (* 10H *)
  38. whitespace = {5}; (* 20H *)
  39. eoln = {6}; (* 40H *)
  40. char = {7}; (* 80H *)
  41. EndOfParam = pipe + inputFile + outputFile + outputFileAppend + ampersand + eoln;
  42. (* errors *)
  43. ErrFileNotFound = 1;
  44. ErrInvalidFilename = 2;
  45. ErrAlreadyPiped = 3;
  46. ErrPipeAtBeginning = 4;
  47. ErrInvalidCommand = 5;
  48. ErrEolnExpected = 6;
  49. TYPE
  50. CommandsString = POINTER TO RECORD
  51. prev, next: CommandsString;
  52. string: ARRAY MaxLen OF CHAR;
  53. END;
  54. CommandHistory = OBJECT
  55. VAR
  56. first, current: CommandsString;
  57. PROCEDURE GetNextCommand(VAR cmd : ARRAY OF CHAR);
  58. BEGIN
  59. IF first = NIL THEN RETURN END;
  60. IF current = NIL THEN current := first ELSE current := current.next END;
  61. COPY(current.string, cmd);
  62. END GetNextCommand;
  63. PROCEDURE GetPreviousCommand(VAR cmd : ARRAY OF CHAR);
  64. BEGIN
  65. IF first = NIL THEN RETURN END;
  66. IF current = NIL THEN current := first.prev ELSE current := current.prev END;
  67. COPY(current.string, cmd);
  68. END GetPreviousCommand;
  69. PROCEDURE AddCommand(CONST cmd : ARRAY OF CHAR);
  70. VAR command: CommandsString;
  71. BEGIN
  72. IF (cmd = "") THEN (* Don't add to history *) RETURN; END;
  73. command := first;
  74. IF command # NIL THEN
  75. WHILE (command.string # cmd) & (command.next # first) DO command := command.next END;
  76. IF command.string # cmd THEN command := NIL END
  77. END;
  78. IF command # NIL THEN
  79. IF first = command THEN first := command.next END;
  80. command.prev.next := command.next;
  81. command.next.prev := command.prev;
  82. ELSE
  83. NEW (command);
  84. COPY (cmd, command.string);
  85. END;
  86. IF first = NIL THEN
  87. first := command; first.next := first; first.prev := first
  88. ELSE
  89. command.prev := first.prev; command.next := first;
  90. first.prev.next := command; first.prev := command;
  91. END;
  92. current := NIL;
  93. END AddCommand;
  94. PROCEDURE &Init*;
  95. BEGIN first := NIL; current := NIL;
  96. END Init;
  97. END CommandHistory;
  98. TYPE
  99. Command = POINTER TO RECORD
  100. command: ARRAY CmdLen OF CHAR; (* command (e.g. <module>"."<command> *)
  101. parameters: ARRAY ParamLen OF CHAR; (* parameters *)
  102. context: Commands.Context; (* context (in, out & err streams *)
  103. pipe : Pipes.Pipe;
  104. next: Command;
  105. END;
  106. Alias = POINTER TO RECORD
  107. alias,
  108. command: ARRAY CmdLen OF CHAR;
  109. parameters: ARRAY ParamLen OF CHAR;
  110. next: Alias;
  111. END;
  112. NotifyProcedure* = PROCEDURE {DELEGATE} (command : LONGINT);
  113. TYPE
  114. (*
  115. Blocker* = OBJECT (Streams.Writer)
  116. VAR
  117. interpreter: Interpreter.Interpreter; i: LONGINT;
  118. parser: Interpreter.Parser;
  119. reader-: Streams.Reader;
  120. writer-: Streams.Writer;
  121. scanner: Scanner.Scanner;
  122. diagnostics: Diagnostics.StreamDiagnostics;
  123. PROCEDURE & InitBlocker(context: Commands.Context);
  124. VAR pipe: Pipes.Pipe;
  125. BEGIN
  126. TRACE(1);
  127. NEW(diagnostics, context.error);
  128. TRACE(2);
  129. NEW(pipe, 256);
  130. TRACE(3);
  131. NEW(reader, pipe.Receive, 256);
  132. TRACE(4);
  133. NEW(writer, pipe.Send, 256);
  134. END InitBlocker;
  135. PROCEDURE Statements;
  136. VAR statement: SyntaxTree.Statement; statements: SyntaxTree.StatementSequence;
  137. BEGIN
  138. NEW(scanner, "", reader, 0, diagnostics);
  139. TRACE(6);
  140. NEW(parser, scanner, diagnostics);
  141. TRACE(7);
  142. statements := SyntaxTree.NewStatementSequence();
  143. LOOP
  144. WHILE parser.Statement(statements, NIL) DO TRACE("parser statement");
  145. IF parser.Optional(Scanner.Semicolon) THEN END;
  146. END;
  147. TRACE("failure");
  148. END;
  149. END Statements;
  150. BEGIN{ACTIVE}
  151. Statements
  152. END Blocker;
  153. *)
  154. Shell* = OBJECT
  155. VAR
  156. echo, dead, close: BOOLEAN;
  157. context: Commands.Context;
  158. command: ARRAY MaxLen OF CHAR;
  159. res: LONGINT;
  160. nestingLevel : LONGINT; (* how many shells run in this shell? *)
  161. aliases: Alias;
  162. prompt: ARRAY 32 OF CHAR;
  163. (* Connection to the entiry hosting this shell instance *)
  164. upcall : NotifyProcedure;
  165. commandHistory : CommandHistory;
  166. PROCEDURE &Init*(in: Streams.Reader; out, err: Streams.Writer; echo: BOOLEAN; CONST prompt: ARRAY OF CHAR);
  167. BEGIN
  168. ASSERT((in # NIL) & (out # NIL) & (err # NIL));
  169. NEW(context, in, NIL, out, err, SELF);
  170. close := FALSE; dead := FALSE; command[0] := 0X; res := 0; SELF.echo := echo; COPY(prompt, SELF.prompt);
  171. NEW(commandHistory);
  172. END Init;
  173. PROCEDURE Exit*;
  174. BEGIN
  175. close := TRUE;
  176. END Exit;
  177. PROCEDURE DeleteStringFromDisplay(CONST x : ARRAY OF CHAR);
  178. VAR i, len : LONGINT;
  179. BEGIN
  180. len := Strings.Length(x);
  181. FOR i := 0 TO len-1 DO context.out.Char(Backspace); END;
  182. FOR i := 0 TO len-1 DO context.out.Char(Space); END;
  183. FOR i := 0 TO len-1 DO context.out.Char(Backspace); END;
  184. END DeleteStringFromDisplay;
  185. PROCEDURE ReadCommand(w: Streams.Writer (*VAR command : ARRAY OF CHAR*));
  186. VAR
  187. ch: CHAR;
  188. currentIndex : LONGINT;
  189. PROCEDURE IsAsciiCharacter(ch : CHAR) : BOOLEAN;
  190. BEGIN
  191. RETURN ORD(ch) <= 127;
  192. END IsAsciiCharacter;
  193. PROCEDURE IsControlCharacter(ch : CHAR) : BOOLEAN;
  194. BEGIN
  195. RETURN ORD(ch) < 32;
  196. END IsControlCharacter;
  197. PROCEDURE HandleEscapeSequence;
  198. BEGIN
  199. ch := context.in.Get();
  200. ch := CHR(ORD(ch)+128);
  201. IF (ch = CursorDown) OR (ch = CursorUp) THEN (* Command History Keys *)
  202. command[currentIndex+1] := 0X;
  203. DeleteStringFromDisplay(command);
  204. IF ch = CursorUp THEN
  205. commandHistory.GetPreviousCommand(command);
  206. ELSE
  207. commandHistory.GetNextCommand(command);
  208. END;
  209. currentIndex := Strings.Length(command)-1;
  210. IF echo & (command # "") THEN context.out.String(command); context.out.Update; END;
  211. ELSE
  212. (* ignore escaped character *)
  213. END;
  214. END HandleEscapeSequence;
  215. BEGIN
  216. command := ""; currentIndex := -1;
  217. LOOP
  218. ch := context.in.Get();
  219. IF IsAsciiCharacter(ch) THEN
  220. IF IsControlCharacter(ch) OR (ch = Delete) THEN
  221. IF (ch = CR) OR (ch = LF) OR (ch = Streams.EOT) OR (context.in.res # Streams.Ok) THEN
  222. EXIT
  223. ELSIF (ch = Backspace) OR (ch = Delete)THEN
  224. IF currentIndex >= 0 THEN (* There is a character at the left of the cursor *)
  225. command[currentIndex] := 0X;
  226. DEC(currentIndex);
  227. IF echo THEN
  228. context.out.Char(Backspace); context.out.Char(Space); context.out.Char(Backspace); context.out.Update;
  229. END;
  230. END;
  231. ELSIF (ORD(ch) = 03H) THEN
  232. (* IF runner # NIL THEN AosActive.TerminateThis(runner); END; *)
  233. ELSIF (ch = EscapeChar1) THEN (* Escape sequence *)
  234. IF context.in.Peek() = EscapeChar2 THEN ch := context.in.Get(); HandleEscapeSequence;
  235. ELSIF context.in.Peek () = Escape THEN
  236. command[currentIndex+1] := 0X;
  237. DeleteStringFromDisplay (command); context.out.Update;
  238. ch := context.in.Get (); command := ""; currentIndex := -1;
  239. END;
  240. ELSE
  241. (* ignore other control characters *)
  242. END;
  243. ELSE
  244. IF currentIndex <= LEN(command) - 2 (* Always need space for 0X *) THEN
  245. INC(currentIndex);
  246. command[currentIndex] := ch;
  247. IF echo THEN context.out.Char(ch); context.out.Update; END;
  248. END;
  249. END;
  250. ELSE
  251. (* ignore non-ascii characters *)
  252. END;
  253. END;
  254. command[currentIndex+1] := 0X;
  255. IF ch = CR THEN
  256. commandHistory.AddCommand(command);
  257. IF (context.in.Available() > 0) & (context.in.Peek() = LF) THEN ch := context.in.Get() END;
  258. IF echo THEN context.out.Ln; context.out.Update END
  259. END;
  260. w.String(command);
  261. END ReadCommand;
  262. (*
  263. PROCEDURE ReadCommand(w: Streams.Writer);
  264. VAR
  265. ch: CHAR;
  266. currentIndex : LONGINT;
  267. PROCEDURE IsAsciiCharacter(ch : CHAR) : BOOLEAN;
  268. BEGIN
  269. RETURN ORD(ch) <= 127;
  270. END IsAsciiCharacter;
  271. PROCEDURE IsControlCharacter(ch : CHAR) : BOOLEAN;
  272. BEGIN
  273. RETURN ORD(ch) < 32;
  274. END IsControlCharacter;
  275. PROCEDURE HandleEscapeSequence;
  276. BEGIN
  277. ch := context.in.Get();
  278. ch := CHR(ORD(ch)+128);
  279. IF (ch = CursorDown) OR (ch = CursorUp) THEN (* Command History Keys *)
  280. command[currentIndex+1] := 0X;
  281. DeleteStringFromDisplay(command);
  282. IF ch = CursorUp THEN
  283. commandHistory.GetPreviousCommand(command);
  284. ELSE
  285. commandHistory.GetNextCommand(command);
  286. END;
  287. currentIndex := Strings.Length(command)-1;
  288. IF echo & (command # "") THEN context.out.String(command); context.out.Update; END;
  289. ELSE
  290. (* ignore escaped character *)
  291. END;
  292. END HandleEscapeSequence;
  293. BEGIN
  294. command := ""; currentIndex := -1;
  295. LOOP
  296. ch := context.in.Get();
  297. TRACE(ch);
  298. IF IsAsciiCharacter(ch) THEN
  299. IF IsControlCharacter(ch) OR (ch = Delete) THEN
  300. IF (ch = Streams.EOT) OR (context.in.res # Streams.Ok) THEN
  301. EXIT
  302. ELSIF (ch = Backspace) OR (ch = Delete)THEN
  303. IF currentIndex >= 0 THEN (* There is a character at the left of the cursor *)
  304. IF command[currentIndex] = CR THEN
  305. context.out.Char(Backspace); context.out.Char(Space); context.out.Char(Backspace); context.out.Update;
  306. END;
  307. command[currentIndex] := 0X;
  308. DEC(currentIndex);
  309. IF echo THEN
  310. context.out.Char(Backspace); context.out.Char(Space); context.out.Char(Backspace); context.out.Update;
  311. END;
  312. END;
  313. ELSIF (ORD(ch) = 03H) THEN
  314. (* IF runner # NIL THEN AosActive.TerminateThis(runner); END; *)
  315. ELSIF (ch = EscapeChar1) THEN (* Escape sequence *)
  316. IF context.in.Peek() = EscapeChar2 THEN ch := context.in.Get(); HandleEscapeSequence;
  317. ELSIF context.in.Peek() =
  318. ELSIF context.in.Peek() = 0DX THEN (* command *)
  319. ch := context.in.Get();
  320. INC(currentIndex); command[currentIndex] := ch;
  321. EXIT;
  322. ELSIF context.in.Peek () = Escape THEN
  323. command[currentIndex+1] := 0X;
  324. DeleteStringFromDisplay (command); context.out.Update;
  325. ch := context.in.Get (); command := ""; currentIndex := -1;
  326. END;
  327. ELSIF (ch =CR) OR (ch = LF) THEN
  328. INC(currentIndex); command[currentIndex] := ch;
  329. IF (ch = CR) & (context.in.Available() > 0) & (context.in.Peek() = LF) THEN
  330. ch := context.in.Get();
  331. INC(currentIndex); command[currentIndex] := ch;
  332. END;
  333. IF echo THEN context.out.Ln; context.out.Update END;
  334. ELSE
  335. INC(currentIndex);
  336. command[currentIndex] := ch;
  337. IF echo THEN context.out.Char(ch); context.out.Update; END;
  338. END;
  339. ELSE
  340. IF currentIndex <= LEN(command) - 2 (* Always need space for 0X *) THEN
  341. INC(currentIndex);
  342. command[currentIndex] := ch;
  343. IF echo THEN context.out.Char(ch); context.out.Update; END;
  344. END;
  345. END;
  346. ELSE
  347. (* ignore non-ascii characters *)
  348. END;
  349. END;
  350. command[currentIndex+1] := 0X;
  351. IF ch = CR THEN
  352. commandHistory.AddCommand(command);
  353. IF (context.in.Available() > 0) & (context.in.Peek() = LF) THEN ch := context.in.Get() END;
  354. (* IF echo THEN context.out.Ln; context.out.Update END; *)
  355. w.String(command);
  356. END;
  357. END ReadCommand;
  358. *)
  359. (*
  360. PROCEDURE Parse(VAR cmd: Command; VAR wait: BOOLEAN): LONGINT;
  361. VAR sym: SET; pos: LONGINT; c, next: CHAR;
  362. PROCEDURE Init;
  363. BEGIN
  364. pos := 0; c := 0X; next := command[pos]; sym := start; Scan
  365. END Init;
  366. PROCEDURE Scan;
  367. BEGIN
  368. IF (sym # eoln) THEN
  369. c := next; INC(pos); next := command[pos];
  370. CASE c OF
  371. | "<": sym := inputFile
  372. | "|": sym := pipe
  373. | ">": IF (next = ">") THEN sym := outputFileAppend; INC(pos); next := command[pos]; ELSE sym := outputFile END
  374. | "&": sym := ampersand
  375. | " ", 09X: sym := whitespace
  376. | 0X: sym := eoln
  377. ELSE sym := char
  378. END
  379. END
  380. END Scan;
  381. PROCEDURE Match(symbol: SET): BOOLEAN;
  382. BEGIN IF (symbol = sym) THEN Scan; RETURN TRUE ELSE RETURN FALSE END
  383. END Match;
  384. PROCEDURE Skip;
  385. BEGIN
  386. WHILE (sym = whitespace) & (sym # eoln) DO Scan END
  387. END Skip;
  388. PROCEDURE Token(VAR s: ARRAY OF CHAR; cond: SET): BOOLEAN;
  389. VAR i: LONGINT; quote: BOOLEAN;
  390. BEGIN
  391. quote := FALSE;
  392. WHILE (sym * cond = {}) OR (quote & (sym # eoln)) DO
  393. s[i] := c; INC(i); IF (c = '"') OR (c = "'") THEN quote := ~quote END; Scan
  394. END;
  395. s[i] := 0X;
  396. RETURN ~quote
  397. END Token;
  398. PROCEDURE Cmd(): Command;
  399. VAR i: LONGINT; cmd: Command; arg : Streams.StringReader;
  400. BEGIN Skip;
  401. IF (sym = char) THEN
  402. NEW(cmd);
  403. i := 0;
  404. WHILE (sym = char) DO cmd.command[i] := c; INC(i); Scan END; cmd.command[i] := 0X; Skip;
  405. IF (cmd.command # "") THEN
  406. IF (sym * EndOfParam = {}) THEN
  407. IF ~Token(cmd.parameters, EndOfParam) THEN cmd := NIL END
  408. END;
  409. REPEAT UNTIL ~ReplaceAlias(cmd);
  410. NEW(arg, LEN(cmd.parameters)); arg.SetRaw(cmd.parameters, 0, LEN(cmd.parameters));
  411. NEW(cmd.context, context.in, arg, context.out, context.error, SELF);
  412. ELSE cmd := NIL (* invalid command (empty string) *)
  413. END
  414. ELSE cmd := NIL
  415. END;
  416. RETURN cmd
  417. END Cmd;
  418. PROCEDURE CmdLine(VAR command: Command): LONGINT;
  419. VAR cmd, prev: Command; fn: Files.FileName; f: Files.File; fr: Files.Reader; fw: Files.Writer;
  420. r: Streams.Reader; w: Streams.Writer; append, piped: BOOLEAN; s: ARRAY 64 OF CHAR;
  421. BEGIN
  422. cmd := NIL; prev := NIL; command := NIL; res := 0; piped := FALSE;
  423. Init;
  424. REPEAT
  425. cmd := Cmd();
  426. IF (cmd # NIL) THEN
  427. IF (command = NIL) THEN command := cmd END;
  428. IF piped THEN
  429. piped := FALSE;
  430. IF (prev # NIL) THEN
  431. IF (prev.context.out = context.out) & (cmd.context.in = context.in) THEN
  432. NEW(prev.pipe, 1024);
  433. Streams.OpenReader(r, prev.pipe.Receive); Streams.OpenWriter(w, prev.pipe.Send);
  434. prev.context.Init(r, prev.context.arg, w, prev.context.error, SELF);
  435. prev.next := cmd
  436. ELSE res := ErrAlreadyPiped (* already piped *)
  437. END
  438. ELSE res := ErrPipeAtBeginning (* pipe cannot be first symbol *)
  439. END
  440. END;
  441. IF Match(inputFile) THEN (* "<" filename *)
  442. IF (cmd.context.in = context.in) THEN
  443. Skip;
  444. IF Token(fn, -char) & (fn # "") THEN
  445. f := Files.Old(fn);
  446. IF (f # NIL) THEN
  447. Files.OpenReader(fr, f, 0);
  448. cmd.context.Init(fr, cmd.context.arg, cmd.context.out, cmd.context.error, SELF)
  449. ELSE res := ErrFileNotFound (* file not found *)
  450. END
  451. ELSE res := ErrInvalidFilename (* invalid filename *)
  452. END
  453. ELSE res := ErrAlreadyPiped (* error: already piped *)
  454. END
  455. ELSIF Match(pipe) THEN (* "|" command *)
  456. piped := TRUE
  457. END;
  458. prev := cmd
  459. ELSE res := ErrInvalidCommand (* invalid command *)
  460. END
  461. UNTIL (res # 0) OR (cmd = NIL) OR ~piped;
  462. IF (res = 0) THEN
  463. IF (sym * (outputFile+outputFileAppend) # {}) THEN (* ">"[">"] filename *)
  464. append := (sym = outputFileAppend);
  465. Scan; Skip;
  466. IF Token (fn, EndOfParam (*-char *)) & (fn # "") THEN
  467. Skip; f := NIL;
  468. IF append THEN f := Files.Old(fn) END;
  469. IF (f = NIL) THEN f := Files.New(fn); Files.Register(f) END;
  470. IF (f # NIL) THEN
  471. IF append THEN
  472. Files.OpenWriter(fw, f, f.Length());
  473. ELSE
  474. Files.OpenWriter(fw, f, 0);
  475. END;
  476. cmd.context.Init(cmd.context.in, cmd.context.arg, fw, cmd.context.error, SELF);
  477. fw.Update;
  478. ELSE res := ErrFileNotFound (* cannot open output file *)
  479. END
  480. ELSE res := ErrInvalidFilename (* invalid filename *)
  481. END
  482. END
  483. END;
  484. IF (res = 0) THEN
  485. wait := ~Match(ampersand);
  486. WHILE (sym # eoln) & Match(whitespace) DO END;
  487. IF ~Match(eoln) THEN res := ErrEolnExpected END (* end of line expected *)
  488. END;
  489. IF (res # 0) THEN
  490. context.error.String("Error at position "); context.error.Int(pos, 0); context.error.String(": ");
  491. CASE res OF
  492. | ErrFileNotFound: COPY("file not found.", s)
  493. | ErrInvalidFilename: COPY("invalid file name.", s)
  494. | ErrAlreadyPiped: COPY("two input streams.", s)
  495. | ErrPipeAtBeginning: COPY("syntax error.", s)
  496. | ErrInvalidCommand: COPY("invalid command.", s)
  497. | ErrEolnExpected: COPY("too many arguments.", s)
  498. ELSE COPY("unknown error.", s)
  499. END;
  500. context.error.String(s); context.error.Ln; context.error.Update;
  501. command := NIL
  502. END;
  503. RETURN res
  504. END CmdLine;
  505. BEGIN
  506. wait := TRUE;
  507. RETURN CmdLine(cmd)
  508. END Parse;
  509. *)
  510. PROCEDURE ReadAlias(cmd : Command; verbose : BOOLEAN);
  511. VAR s: ARRAY MaxLen OF CHAR; alias, p, q: Alias; i, k: LONGINT; c: CHAR;
  512. BEGIN
  513. IF (cmd.parameters # "") THEN
  514. COPY(cmd.parameters, s);
  515. NEW(alias);
  516. i := 0; c := s[i];
  517. WHILE (c # 0X) & (c # "=") DO alias.alias[i] := c; INC(i); c := s[i] END;
  518. IF (c = "=") THEN
  519. k := 0; INC(i); c := s[i];
  520. WHILE (c # 0X) & (c # " ") & (c # TAB) DO alias.command[k] := c; INC(k); INC(i); c := s[i] END;
  521. END;
  522. IF verbose THEN context.out.String(alias.alias); END;
  523. IF (alias.command # "") THEN (* add an alias *)
  524. WHILE (c # 0X) & ((c = " ") OR (c = TAB)) DO INC(i); c := s[i] END;
  525. k := 0;
  526. WHILE (c # 0X) DO alias.parameters[k] := c; INC(k); INC(i); c := s[i] END;
  527. p := aliases; q := NIL;
  528. WHILE (p # NIL) & (p.alias < alias.alias) DO q := p; p := p.next END;
  529. IF (q = NIL) THEN aliases := alias; aliases.next := p
  530. ELSE q.next := alias; alias.next := p
  531. END;
  532. IF verbose THEN
  533. context.out.String(" = "); context.out.String(alias.command); context.out.Char(" "); context.out.String(alias.parameters);
  534. END;
  535. ELSE (* remove an alias *)
  536. p := aliases; q := NIL;
  537. WHILE (p # NIL) & (p.alias < alias.alias) DO q := p; p := p.next END;
  538. IF (p # NIL) & (p.alias = alias.alias) THEN
  539. IF (q = NIL) THEN aliases := aliases.next
  540. ELSE q.next := p.next
  541. END
  542. END;
  543. IF verbose THEN context.out.String(" removed"); END;
  544. END;
  545. IF verbose THEN context.out.Ln; END;
  546. ELSE (* list aliases *)
  547. p := aliases;
  548. WHILE (p # NIL) DO
  549. IF verbose THEN
  550. context.out.String(p.alias); context.out.String(" = "); context.out.String(p.command); context.out.Char(" ");
  551. context.out.String(p.parameters); context.out.Ln;
  552. END;
  553. p := p.next
  554. END
  555. END
  556. END ReadAlias;
  557. (*
  558. PROCEDURE ReplaceAlias(cmd: Command): BOOLEAN;
  559. VAR a: Alias; d, i: LONGINT;
  560. BEGIN
  561. a := aliases;
  562. WHILE (a # NIL) & (a.alias < cmd.command) DO a := a.next END;
  563. IF (a # NIL) & (a.alias = cmd.command) THEN
  564. COPY(a.command, cmd.command);
  565. IF (a.parameters # "") THEN
  566. IF (cmd.parameters = "") THEN COPY(a.parameters, cmd.parameters)
  567. ELSE
  568. d := Strings.Length(a.parameters) + 1;
  569. FOR i := Strings.Length(cmd.parameters) TO 0 BY -1 DO
  570. cmd.parameters[i+d] := cmd.parameters[i]
  571. END;
  572. FOR i := 0 TO d-2 DO cmd.parameters[i] := a.parameters[i] END;
  573. cmd.parameters[d-1] := " "
  574. END
  575. END;
  576. RETURN TRUE
  577. ELSE
  578. RETURN FALSE
  579. END
  580. END ReplaceAlias;
  581. PROCEDURE ShowHelp;
  582. BEGIN
  583. context.out.String("--- Help --- "); context.out.Ln;
  584. context.out.String("alias: Show list of aliases"); context.out.Ln;
  585. context.out.String("alias 'string'='command': Create alias for command"); context.out.Ln;
  586. context.out.String("alias 'string': Remove alias"); context.out.Ln;
  587. context.out.String("batch: start a new instance of Shell"); context.out.Ln;
  588. context.out.String("clear: Clear screen"); context.out.Ln;
  589. context.out.String("version: Show BimboShell version"); context.out.Ln;
  590. context.out.String("help: Show this help text"); context.out.Ln;
  591. context.out.String("exit: Exit Shell"); context.out.Ln;
  592. context.out.Update;
  593. END ShowHelp;
  594. PROCEDURE Execute(cmd: Command; wait: BOOLEAN; VAR exit: BOOLEAN);
  595. VAR
  596. c: Command; flags: SET;
  597. res : LONGINT; msg: ARRAY MaxLen OF CHAR; oldContext: Commands.Context;
  598. moduleName, commandName : Modules.Name; errormsg : ARRAY 128 OF CHAR;
  599. BEGIN
  600. IF (cmd.command = "alias") THEN
  601. ReadAlias(cmd, TRUE)
  602. ELSIF (cmd.command = "loadalias") THEN
  603. LoadAliasesFromFile(cmd.parameters);
  604. ELSIF (cmd.command = "batch") THEN
  605. context.out.String(Version); context.out.Ln; context.out.Update;
  606. oldContext := context; context := cmd.context;
  607. INC(nestingLevel);
  608. Run;
  609. context := oldContext
  610. ELSIF (cmd.command = "exit") THEN
  611. DEC(nestingLevel);
  612. exit := TRUE
  613. ELSIF (cmd.command = "version") THEN
  614. context.out.String(Version); context.out.Ln; context.out.Update;
  615. ELSIF (cmd.command = "help") THEN
  616. ShowHelp;
  617. ELSIF (cmd.command = "clear") THEN
  618. IF upcall # NIL THEN upcall(Clear); END;
  619. ELSE
  620. c := cmd; res := 0;
  621. WHILE (c # NIL) & (res = 0) DO
  622. IF (c.next = NIL) & wait THEN flags := {Commands.Wait}
  623. ELSE flags := {}
  624. END;
  625. Commands.Split(c.command, moduleName, commandName, res, errormsg);
  626. IF (res # Commands.Ok) THEN
  627. context.error.String(errormsg); context.error.Ln;
  628. ELSE
  629. Commands.Activate(c.command, c.context, flags, res, msg);
  630. (* IF wait & (cmd.pipe # NIL) THEN
  631. KernelLog.String("Pipe closed"); KernelLog.Ln;
  632. cmd.pipe.Close;
  633. END; *)
  634. IF (res # 0) THEN
  635. context.error.String("Error in command: "); context.error.String(cmd.command);
  636. context.error.String(", params: ");
  637. IF c.parameters # "" THEN
  638. context.error.String(c.parameters);
  639. ELSE
  640. context.error.String("None");
  641. END;
  642. context.error.String(", res: "); context.error.Int(res, 0);
  643. context.error.String(" ("); context.error.String(msg); context.error.Char(")");
  644. context.error.Ln
  645. ELSE c := c.next
  646. END;
  647. END;
  648. END
  649. END;
  650. context.out.Update; context.error.Update
  651. END Execute;
  652. *)
  653. TYPE
  654. StringType = POINTER TO ARRAY OF CHAR;
  655. Reader* = OBJECT (Streams.Reader)
  656. VAR length : LONGINT;
  657. data : StringType;
  658. rofs: LONGINT;
  659. PROCEDURE &Init*(initialSize : LONGINT);
  660. BEGIN
  661. IF initialSize < 256 THEN initialSize := 256 END;
  662. NEW(data, initialSize); length := 0; rofs := 0;
  663. InitReader( Receive, initialSize )
  664. END Init;
  665. PROCEDURE Add*(CONST buf: ARRAY OF CHAR; ofs, len: LONGINT; propagate: BOOLEAN; VAR res: LONGINT);
  666. VAR i,pos : LONGINT; n: StringType;
  667. BEGIN{EXCLUSIVE}
  668. IF length + len + 1 >= LEN(data) THEN
  669. NEW(n, LEN(data) + len + 1); FOR i := 0 TO length - 1 DO n[i] := data[i] END;
  670. data := n
  671. END;
  672. pos := (rofs + length) MOD LEN(data);
  673. WHILE (len > 0) & (buf[ofs] # 0X) DO
  674. data[pos] := buf[ofs];
  675. pos := (pos+1) MOD LEN(data);
  676. INC(ofs); INC(length); DEC(len)
  677. END;
  678. END Add;
  679. PROCEDURE Receive( VAR buf: ARRAY OF CHAR; ofs, size, min: LONGINT; VAR len, res: LONGINT );
  680. VAR o,pos: LONGINT;
  681. BEGIN{EXCLUSIVE}
  682. AWAIT(length >= min);
  683. pos := rofs;
  684. len := 0;
  685. WHILE (length > 0) & (size >0) DO
  686. buf[ofs] := data[pos];
  687. pos := (pos + 1) MOD LEN(data);
  688. INC(ofs); DEC(length); INC(len); DEC(size);
  689. END;
  690. rofs := pos;
  691. IF ofs < size THEN
  692. buf[ofs] := 0X; (* safety / trace *)
  693. END;
  694. END Receive;
  695. END Reader;
  696. PROCEDURE Run;
  697. VAR cmdList: Command; wait, exit: BOOLEAN; i : LONGINT; interpreter: Interpreter.Interpreter; s: Scanner.StringMaker; w: Streams.Writer; r: Streams.Reader;
  698. scanner: Scanner.Scanner; parser: Interpreter.Parser;
  699. diagnostics: Diagnostics.StreamDiagnostics; seq: SyntaxTree.StatementSequence;
  700. str: Scanner.StringType; len: LONGINT; container: Interpreter.Container; scope: Interpreter.Scope; e: SyntaxTree.Expression; value: Interpreter.Value;
  701. reader: Reader;
  702. runner: OBJECT
  703. VAR
  704. r: Streams.Reader;
  705. scanner: Scanner.Scanner; parser: Interpreter.Parser;
  706. stm: SyntaxTree.Statement;
  707. diagnostics: Diagnostics.Diagnostics;
  708. seq: SyntaxTree.StatementSequence;
  709. expression: SyntaxTree.Expression;
  710. interpreter: Interpreter.Interpreter;
  711. container: Interpreter.Container; scope: Interpreter.Scope;
  712. context: Commands.Context;
  713. value: Interpreter.Value;
  714. PROCEDURE &Init(r: Streams.Reader; diag: Diagnostics.Diagnostics; ctxt: Commands.Context);
  715. BEGIN
  716. SELF.r := r; diagnostics := diag; SELF.r := r;
  717. context := ctxt;
  718. END Init;
  719. BEGIN{ACTIVE}
  720. ASSERT(diagnostics # NIL);
  721. context.out.Ln;
  722. context.out.String(">");
  723. context.out.Update;
  724. NEW(scanner,"", r, 0, diagnostics);
  725. scanner.SetCase(Scanner.Lowercase);
  726. NEW(parser, scanner, diagnostics); (* silent *)
  727. parser.SetLax;
  728. NEW(container);
  729. NEW(scope, Interpreter.global, container);
  730. NEW(interpreter, scope, diagnostics, context);
  731. LOOP
  732. (*diagnostics.Information("interpreter",Diagnostics.Invalid,Diagnostics.Invalid,"start statement");*)
  733. seq := SyntaxTree.NewStatementSequence();
  734. IF parser.Optional(Scanner.Questionmark) THEN
  735. expression := parser.Expression();
  736. IF interpreter.GetValue(expression, value) THEN
  737. value.WriteValue(context.out);
  738. ELSE
  739. context.out.String("NIL")
  740. END;
  741. context.out.Ln;
  742. context.out.String(">");
  743. context.out.Update;
  744. WHILE parser.Optional(Scanner.Escape) DO
  745. END;
  746. ELSIF parser.Statement(seq, NIL) THEN
  747. (*Printout.Info("executing ", seq);*)
  748. interpreter.StatementSequence(seq);
  749. context.out.Update;
  750. context.out.Ln;
  751. context.out.String(">");
  752. context.out.Update;
  753. IF interpreter.error THEN interpreter.Reset END;
  754. WHILE parser.Optional(Scanner.Escape) OR parser.Optional(Scanner.Semicolon) DO
  755. (*TRACE(parser.Token());*)
  756. END;
  757. IF interpreter.error THEN interpreter.Reset END;
  758. ELSE
  759. diagnostics.Error("",Diagnostics.Invalid, Diagnostics.Invalid, "no statement");
  760. IF ~parser.error THEN
  761. parser.NextSymbol;
  762. END;
  763. (*NEW(scanner, "",r, 0, diagnostics);
  764. NEW(parser, scanner, diagnostics); (* silent *)*)
  765. END;
  766. END;
  767. END;
  768. BEGIN
  769. NEW(s,0);
  770. w := s.GetWriter();
  771. NEW(diagnostics, context.out);
  772. exit := FALSE;
  773. (*NEW(container);
  774. NEW(scope, Interpreter.global, container);
  775. NEW(interpreter, scope, diagnostics, context);
  776. *)
  777. NEW(reader, 1024);
  778. (*NEW(w, reader.Add,1024);*)
  779. NEW(runner, reader, diagnostics, context);
  780. (*seq := parser.StatementSequence(NIL);*)
  781. WHILE ~close & ~exit & (context.in.res = Streams.Ok) DO
  782. s.Clear;
  783. ReadCommand(w);w.Char(Escape);w.Ln; w.Update;(*
  784. context.out.Ln; context.out.String("------------");
  785. context.out.Ln; context.out.Update;
  786. *)
  787. str := s.GetString(len);
  788. reader.Add(str^,0,len,TRUE,res);
  789. (*
  790. NEW(scanner, "", s.GetReader(), 0, diagnostics);
  791. NEW(parser, scanner, NIL); (* silent *)
  792. *)
  793. (*
  794. e := parser.Expression();
  795. interpreter.Reset;
  796. IF ~parser.error & parser.Optional(Scanner.EndOfText) THEN
  797. IF interpreter.GetValue(e,value) THEN
  798. value(InterpreterSymbols.Value).WriteValue(context.out); context.out.Update;
  799. END;
  800. ELSE
  801. str := s.GetString(len);
  802. NEW(scanner, "", s.GetReader(), 0, diagnostics);
  803. NEW(parser, scanner, diagnostics);
  804. *)
  805. (*
  806. seq := parser.StatementSequence(NIL);
  807. IF parser.Mandatory(Scanner.EndOfText) THEN
  808. interpreter.StatementSequence(seq);
  809. IF ~interpreter.error THEN
  810. context.out.String("[ok]");
  811. END;
  812. END;
  813. *)
  814. (*END;*)
  815. END;
  816. context.out.Update; context.error.Update
  817. END Run;
  818. PROCEDURE AwaitDeath*;
  819. BEGIN {EXCLUSIVE}
  820. AWAIT(dead)
  821. END AwaitDeath;
  822. PROCEDURE SetUpcall*(proc : NotifyProcedure);
  823. BEGIN
  824. ASSERT((proc # NIL) & (upcall = NIL));
  825. upcall := proc;
  826. END SetUpcall;
  827. PROCEDURE ParseAliases(r : Files.Reader);
  828. VAR cmd : Command;
  829. BEGIN
  830. NEW(cmd);
  831. LOOP
  832. cmd.parameters := "";
  833. r.Ln(cmd.parameters);
  834. IF r.res # Streams.Ok THEN EXIT; END;
  835. ReadAlias(cmd, FALSE);
  836. END;
  837. END ParseAliases;
  838. (* Read aliases from specified file. Returns NIL if file not found or parsing failed. *)
  839. PROCEDURE LoadAliasesFromFile(filename : ARRAY OF CHAR);
  840. VAR in : Files.Reader; f : Files.File;
  841. BEGIN
  842. IF filename = "" THEN COPY(DefaultAliasFile, filename); END;
  843. f := Files.Old(filename);
  844. IF f # NIL THEN
  845. Files.OpenReader(in, f, 0);
  846. IF in # NIL THEN
  847. context.out.String("Loading aliases from "); context.out.String(filename); context.out.String("...");
  848. ParseAliases(in);
  849. context.out.String("done."); context.out.Ln;
  850. END;
  851. ELSE
  852. context.out.String("Loading aliases failed: File "); context.out.String(filename);
  853. context.out.String(" not found."); context.out.Ln;
  854. END;
  855. context.out.Update;
  856. END LoadAliasesFromFile;
  857. BEGIN {ACTIVE, SAFE}
  858. context.out.String(Version); context.out.Ln;
  859. context.out.String("Enter statement sequence in lower case with lax syntax"); context.out.Ln;
  860. context.out.Update;
  861. Run;
  862. IF (upcall # NIL) THEN upcall(ExitShell); END;
  863. BEGIN {EXCLUSIVE} dead := TRUE; END;
  864. END Shell;
  865. END InterpreterShell.
  866. SystemTools.Free WMInterpreterShell InterpreterShell FoxInterpreter FoxInterpreterSymbols Test ~
  867. WMInterpreterShell.Open ~