Commands.Mod 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. (* Aos, Copyright 2001, Pieter Muller, ETH Zurich *)
  2. MODULE Commands; (** AUTHOR "pjm"; PURPOSE "Commands and parameters"; *)
  3. IMPORT Objects, Modules, Streams, KernelLog, Trace, Machine;
  4. CONST
  5. (** Activate flags. *)
  6. Wait* = 0; (** Wait until the activated command returns. *)
  7. InheritContext*= 1; (** Inherit context (as far as not overwritten) of the caller *)
  8. Silent*= 2;
  9. Ok* = 0;
  10. CommandNotFound* = 3901;
  11. CommandError* = 3902;
  12. CommandParseError* = 3903;
  13. CommandTrapped* = 3904;
  14. (* Separates module name from procedure name *)
  15. Delimiter* = ".";
  16. (* Runner states *)
  17. Started = 0; Loaded = 1; Finished = 2;
  18. TYPE
  19. Context* = OBJECT
  20. VAR
  21. in-, arg- : Streams.Reader;
  22. out-, error- : Streams.Writer;
  23. caller-: OBJECT;
  24. result*: LONGINT;
  25. PROCEDURE &Init*(in, arg : Streams.Reader; out, error : Streams.Writer; caller: OBJECT);
  26. BEGIN
  27. IF (in = NIL) THEN in := GetEmptyReader(); END;
  28. IF (arg = NIL) THEN arg := GetEmptyReader()END;
  29. IF (out = NIL) THEN NEW(out, KernelLog.Send, 128); END;
  30. IF (error = NIL) THEN NEW(error, KernelLog.Send, 128); END;
  31. SELF.in := in; SELF.arg := arg; SELF.out := out; SELF.error := error; SELF.caller := caller; SELF.result := Ok;
  32. ASSERT((in # NIL) & (arg # NIL) & (out # NIL) & (error # NIL));
  33. END Init;
  34. END Context;
  35. (*see StreamUtilities.Mod: reader that can daisychained with another reader that extracts a copy of the data flow to a monitor stream*)
  36. ReaderMonitor* = OBJECT(Streams.Reader)
  37. VAR in: Streams.Reader; tracer: Streams.Writer; receive: Streams.Receiver; pos0: LONGINT;
  38. PROCEDURE &Init(in: Streams.Reader; tracer: Streams.Writer);
  39. BEGIN
  40. SELF.tracer := tracer;
  41. InitReader(Receiver, 1024);
  42. SELF.in := in;
  43. pos0 := in.Pos();
  44. END Init;
  45. PROCEDURE Receiver(VAR buf: ARRAY OF CHAR; ofs, size, min: LONGINT; VAR len, res: LONGINT);
  46. BEGIN
  47. ASSERT((size > 0) & (min <= size) & (min >= 0));
  48. in.Bytes(buf, ofs, size, len);
  49. tracer.Bytes(buf, ofs, len);
  50. IF len < size THEN (* end of data indication *)
  51. tracer.String("~"); tracer.Ln;
  52. END;
  53. res:=in.res
  54. END Receiver;
  55. PROCEDURE CanSetPos(): BOOLEAN;
  56. BEGIN RETURN in.CanSetPos()
  57. END CanSetPos;
  58. PROCEDURE SetPos(pos: LONGINT);
  59. BEGIN Reset; pos0 := pos; in.SetPos(pos)
  60. END SetPos;
  61. PROCEDURE Pos(): LONGINT;
  62. BEGIN RETURN Pos^()+pos0;
  63. END Pos;
  64. END ReaderMonitor;
  65. (* Procedure types that can be called be runner thread *)
  66. CommandProc = PROCEDURE;
  67. CommandContextProc = PROCEDURE(context : Context);
  68. TYPE
  69. Runner = OBJECT
  70. VAR
  71. moduleName, commandName : Modules.Name;
  72. context : Context;
  73. tracer: Streams.Writer; r: ReaderMonitor;
  74. proc : CommandProc;
  75. commandProc : CommandContextProc;
  76. msg : ARRAY 128 OF CHAR; res : LONGINT;
  77. module : Modules.Module;
  78. state : LONGINT;
  79. exception : BOOLEAN;
  80. PROCEDURE &Init*(CONST moduleName, commandName : Modules.Name; context : Context);
  81. BEGIN
  82. SELF.moduleName := moduleName; SELF.commandName := commandName;
  83. IF (context = NIL) THEN NEW(context, NIL, NIL, NIL, NIL, NIL); END;
  84. IF trace THEN
  85. Streams.OpenWriter(tracer, Trace.Send);
  86. NEW(r , context.arg, tracer); context.arg:=r;
  87. tracer.String("Commands.Activate ");
  88. tracer.String(moduleName); tracer.String(Delimiter); tracer.String(commandName); tracer.Char(" ");
  89. END;
  90. SELF.context := context;
  91. res := CommandError; COPY("Error starting command", msg);
  92. exception := FALSE;
  93. state := Started;
  94. END Init;
  95. PROCEDURE Join(this : LONGINT; VAR res : LONGINT; VAR msg : ARRAY OF CHAR);
  96. BEGIN {EXCLUSIVE}
  97. AWAIT(state >= this);
  98. res := SELF.res; COPY(SELF.msg, msg);
  99. END Join;
  100. BEGIN {ACTIVE, SAFE}
  101. IF ~exception THEN
  102. exception := TRUE; (* catch exceptions from now on *)
  103. module := Modules.ThisModule(moduleName, res, msg);
  104. IF (res = Ok) THEN
  105. IF commandName # "" THEN
  106. GETPROCEDURE(moduleName, commandName, proc);
  107. IF (proc = NIL) THEN
  108. GETPROCEDURE(moduleName, commandName, commandProc);
  109. END;
  110. IF (proc = NIL) & (commandProc = NIL) THEN
  111. res := CommandNotFound;
  112. msg := "Command ";
  113. Modules.Append(moduleName, msg); Modules.Append(Delimiter, msg); Modules.Append(commandName, msg);
  114. Modules.Append(" not found", msg);
  115. END;
  116. END;
  117. END;
  118. BEGIN {EXCLUSIVE} state := Loaded; END;
  119. IF (res = Ok) THEN
  120. ASSERT((proc # NIL) OR (commandProc # NIL) OR (commandName = ""));
  121. IF (proc # NIL) THEN
  122. proc();
  123. context.out.Update; context.error.Update;
  124. ELSIF (commandProc # NIL) THEN
  125. ASSERT(context # NIL);
  126. commandProc(context);
  127. context.out.Update; context.error.Update;
  128. res := context.result;
  129. IF res # Ok THEN msg := "Command not successful"; END;
  130. END;
  131. END;
  132. ELSE
  133. res := CommandTrapped; COPY("Exception during command execution", msg);
  134. END;
  135. IF trace THEN
  136. tracer.String(" ~"); tracer.Ln; tracer.Update
  137. END;
  138. BEGIN {EXCLUSIVE} state := Finished; END;
  139. END Runner;
  140. VAR
  141. emptyString : ARRAY 1 OF CHAR;
  142. silentWriter: Streams.Writer;
  143. trace: BOOLEAN;
  144. defaultContext: Context; (* Fallback. Note that this context would be shared by different users -- may be used for tracing though *)
  145. (* Create a ready on a empty string *)
  146. PROCEDURE GetEmptyReader() : Streams.Reader;
  147. VAR reader : Streams.StringReader;
  148. BEGIN
  149. NEW(reader, 1); reader.SetRaw(emptyString, 0, 1);
  150. RETURN reader;
  151. END GetEmptyReader;
  152. PROCEDURE SendNothing(CONST buf: ARRAY OF CHAR; ofs, len: LONGINT; propagate: BOOLEAN; VAR res: LONGINT);
  153. END SendNothing;
  154. (** Splits a command string of the form moduleName.commandProcName into its components. Can be used to check whether a
  155. command string is syntactically correct, i.e. is of the form 'ModuleName "." [ProcedureName]' *)
  156. PROCEDURE Split*(CONST cmdstr : ARRAY OF CHAR; VAR moduleName, procedureName : Modules.Name; VAR res : LONGINT; VAR msg : ARRAY OF CHAR);
  157. VAR i, j : LONGINT; maxlen, cmdlen : LONGINT;
  158. BEGIN
  159. res := CommandParseError;
  160. moduleName := ""; procedureName := ""; msg := "";
  161. maxlen := LEN(moduleName); cmdlen := LEN(cmdstr);
  162. i := 0; WHILE (i < cmdlen) & (i < maxlen-1) & (cmdstr[i] # Delimiter) & (cmdstr[i] # 0X) DO moduleName[i] := cmdstr[i]; INC(i); END;
  163. IF (i >= maxlen) THEN
  164. COPY("Module name too long", msg);
  165. ELSIF (i >= cmdlen) THEN
  166. COPY("Command string not 0X terminated", msg);
  167. ELSIF (cmdstr[i] # Delimiter) THEN
  168. COPY('Expected ModuleName "." [ProcedureName]', msg);
  169. ELSE
  170. (* We allow cmdstr[i] = 0X. That means the module will be loaded but not command procedure will be started *)
  171. moduleName[i] := 0X;
  172. INC(i); (* Skip Delimiter *)
  173. j := 0;
  174. WHILE (i < cmdlen) & (j < maxlen-1) & (cmdstr[i] # 0X) DO procedureName[j] := cmdstr[i]; INC(j); INC(i); END;
  175. IF (i >= cmdlen) THEN
  176. COPY("Command string not 0X terminated", msg);
  177. ELSIF (j >= maxlen-1) THEN
  178. COPY("Command name too long", msg);
  179. ELSE
  180. procedureName[j] := 0X;
  181. res := Ok; COPY("", msg);
  182. END;
  183. END;
  184. END Split;
  185. (** Can be called by a command to retrieve the context associated with its active object. *)
  186. PROCEDURE GetContext*() : Context;
  187. VAR object: ANY;
  188. BEGIN
  189. object := Objects.ActiveObject();
  190. IF (object # NIL) & (object IS Runner) & (object(Runner).state = Loaded) THEN RETURN object(Runner).context;
  191. ELSE RETURN defaultContext
  192. END;
  193. END GetContext;
  194. (** Activate a command in its own active object.
  195. Returns res = Ok if successful, otherwise msg contains error message.
  196. The command can call GetConext() to get its context, which is also passed directly. *)
  197. PROCEDURE Activate*(CONST cmd : ARRAY OF CHAR; context : Context; flags : SET; VAR res : LONGINT; VAR msg : ARRAY OF CHAR);
  198. VAR moduleName, commandName : Modules.Name; run : Runner;
  199. BEGIN
  200. Split(cmd, moduleName, commandName, res, msg);
  201. IF (res = Ok) THEN
  202. NEW(run, moduleName, commandName, context);
  203. run.Join(Loaded, res, msg); (* Avoid race condition described in Modules.Mod *)
  204. IF (res = Ok) & (Wait IN flags) THEN run.Join(Finished, res, msg); END
  205. END;
  206. END Activate;
  207. (** Activate a string of commands, including their parameters.
  208. The string is parsed from left to right and Activate is called for every command.
  209. Parsing stops at the end of the string, or when Activate returns an error.
  210. The flags are applied to every command, i.e., for sequential execution,
  211. use the Wait flag (the caller waits until all commands return).
  212. Syntax:
  213. cmds = [mode " " ] cmd {";" cmd} .
  214. mode = "PAR" | "SEQ" .
  215. cmd = mod ["." proc] [" " params] .
  216. params = {<any character except ";">} .
  217. *)
  218. PROCEDURE Call*(cmds : ARRAY OF CHAR; flags : SET; VAR res : LONGINT; VAR msg : ARRAY OF CHAR);
  219. VAR outer, context : Context; arg : Streams.StringReader; i, j, k : LONGINT; mode : ARRAY 5 OF CHAR;
  220. par : POINTER TO ARRAY OF CHAR;
  221. BEGIN
  222. IF trace THEN Trace.String("Commands.Call "); Trace.String(cmds); Trace.String("~ "); Trace.Ln END;
  223. NEW(par,LEN(cmds));
  224. i := 0; WHILE (i # 4) & (i # LEN(cmds)) DO mode[i] := cmds[i]; INC(i); END;
  225. mode[i] := 0X; (* copy at most first 4 characters *)
  226. IF mode = "PAR " THEN EXCL(flags, Wait);
  227. ELSIF mode = "SEQ " THEN INCL(flags, Wait);
  228. ELSE i := 0; (* reset to start *)
  229. END;
  230. LOOP
  231. k := 0;
  232. WHILE (cmds[i] # " ") & (cmds[i] # 09X) & (cmds[i] # 0DX) & (cmds[i] # 0AX) & (cmds[i] # 0X) & (cmds[i] # ";") DO cmds[k] := cmds[i]; INC(k); INC(i); END;
  233. IF k = 0 THEN EXIT; END; (* end of string *)
  234. j := 0;
  235. IF (cmds[i] # ";") & (cmds[i] # 0X) THEN (* parameters *)
  236. INC(i); WHILE (cmds[i] # 0X) & (cmds[i] # ";") DO par[j] := cmds[i]; INC(i); INC(j); END;
  237. END;
  238. IF cmds[i] = ";" THEN INC(i); END;
  239. par[j] := 0X; cmds[k] := 0X;
  240. NEW(arg, j+1); arg.SetRaw(par^, 0, j+1);
  241. IF InheritContext IN flags THEN
  242. outer := GetContext();
  243. NEW(context, outer.in, arg, outer.out, outer.error, outer.caller);
  244. ELSIF Silent IN flags THEN
  245. NEW(context,NIL, arg, silentWriter, silentWriter, NIL);
  246. ELSE
  247. NEW(context, NIL, arg, NIL, NIL, NIL)
  248. END;
  249. Activate(cmds, context, flags, res, msg);
  250. IF (res # Ok) THEN EXIT; END;
  251. END;
  252. END Call;
  253. PROCEDURE Init;
  254. VAR s: ARRAY 4 OF CHAR;
  255. BEGIN
  256. emptyString[0] := 0X;
  257. Machine.GetConfig("TraceCommands", s);
  258. trace := (s[0] = "1");
  259. NEW(silentWriter, SendNothing, 128);
  260. NEW(defaultContext,NIL,NIL,NIL,NIL,NIL);
  261. END Init;
  262. BEGIN
  263. Init;
  264. END Commands.