System.Mod 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. MODULE System; (** AUTHOR "TF"; PURPOSE "Access to System Functions"; *)
  2. IMPORT
  3. Machine, Modules, Objects, Commands, Options, ProcessInfo, Kernel, Streams, Dates, Strings, Plugins, Files, SystemVersion, Heaps, Reflection;
  4. CONST
  5. MaxTimers = 16;
  6. DateTimeFormat = "dd.mm.yyyy hh:nn:ss";
  7. CR = 0DX; LF = 0AX; TAB = 9X;
  8. TraceCommands = 1;
  9. TraceFreeDownTo = 2;
  10. Trace = {};
  11. OberonKernel = "Oberon-Kernel";
  12. TYPE
  13. Module = POINTER TO RECORD
  14. next: Module;
  15. checked, imports: BOOLEAN;
  16. m: Modules.Module
  17. END;
  18. VAR
  19. timers : ARRAY MaxTimers OF Dates.DateTime;
  20. PROCEDURE Find(root: Module; m: Modules.Module): Module;
  21. BEGIN
  22. WHILE (root # NIL) & (root.m # m) DO root := root.next END;
  23. RETURN root
  24. END Find;
  25. PROCEDURE CopyModules(): Module;
  26. VAR first, last, c: Module; m: Modules.Module;
  27. BEGIN
  28. NEW(first); first.next := NIL; last := first;
  29. m := Modules.root;
  30. WHILE m # NIL DO
  31. NEW(c); c.checked := FALSE; c.imports := FALSE; c.m := m;
  32. c.next := NIL; last.next := c; last := c;
  33. m := m.next
  34. END;
  35. RETURN first.next
  36. END CopyModules;
  37. PROCEDURE Imports(root, m: Module; CONST name: ARRAY OF CHAR): BOOLEAN;
  38. VAR i: LONGINT;
  39. BEGIN
  40. IF ~m.checked THEN
  41. IF m.m.name # name THEN
  42. i := 0;
  43. WHILE i # LEN(m.m.module) DO
  44. IF (m.m.module[i].name = name) OR Imports(root, Find(root, m.m.module[i]), name) THEN
  45. m.imports := TRUE; i := LEN(m.m.module)
  46. ELSE
  47. INC(i)
  48. END
  49. END
  50. ELSE
  51. m.imports := TRUE
  52. END;
  53. m.checked := TRUE
  54. END;
  55. RETURN m.imports
  56. END Imports;
  57. PROCEDURE LockOberon;
  58. VAR c: PROCEDURE;
  59. BEGIN
  60. IF Modules.ModuleByName (OberonKernel) # NIL THEN
  61. GETPROCEDURE (OberonKernel, "LockOberon", c);
  62. IF c # NIL THEN c END
  63. END;
  64. END LockOberon;
  65. PROCEDURE UnlockOberon;
  66. VAR c: PROCEDURE;
  67. BEGIN
  68. IF Modules.ModuleByName (OberonKernel) # NIL THEN
  69. GETPROCEDURE (OberonKernel, "UnlockOberon", c);
  70. IF c # NIL THEN c END
  71. END;
  72. END UnlockOberon;
  73. (** List all currently loaded modules *)
  74. PROCEDURE ListModules*(context : Commands.Context);
  75. VAR m: Modules.Module; options: Options.Options; details: BOOLEAN; first, reverse: BOOLEAN;
  76. PROCEDURE List(m: Modules.Module);
  77. BEGIN
  78. IF m = NIL THEN RETURN END;
  79. IF reverse THEN List(m.next) END;
  80. IF ~first & options.GetFlag("l") THEN context.out.Ln ELSE context.out.String(" ") END;
  81. first := FALSE;
  82. context.out.String(m.name);
  83. IF options.GetFlag("crc") THEN context.out.String(" crc="); context.out.Hex(m.crc,-8); context.out.String("") END;
  84. IF~reverse THEN List(m.next) END;
  85. END List;
  86. BEGIN
  87. NEW(options);
  88. options.Add("c", "crc", Options.Flag);
  89. options.Add("l", "ln", Options.Flag);
  90. options.Add("b", "base", Options.Flag);
  91. options.Add("r", "reverse", Options.Flag);
  92. IF options.Parse(context.arg, context.error) THEN
  93. reverse := ~options.GetFlag("r");
  94. first := FALSE;
  95. List(Modules.root);
  96. END;
  97. END ListModules;
  98. (** List all loaded plugins. *)
  99. PROCEDURE ListPlugins*(context : Commands.Context);
  100. VAR r, p : Plugins.Table; i, j : LONGINT;
  101. BEGIN
  102. Plugins.main.GetAll(r);
  103. IF r # NIL THEN
  104. FOR i := 0 TO LEN(r^)-1 DO
  105. context.out.Int(i, 1); context.out.Char(" ");
  106. context.out.String(r[i].name); context.out.Char(" ");
  107. context.out.String(r[i].desc); context.out.Ln;
  108. r[i](Plugins.Registry).GetAll(p);
  109. IF p # NIL THEN
  110. FOR j := 0 TO LEN(p^)-1 DO
  111. context.out.Char(TAB); context.out.Int(j, 1); context.out.Char(" ");
  112. context.out.String(p[j].name); context.out.Char(" ");
  113. context.out.String(p[j].desc); context.out.Ln;
  114. context.out.Update;
  115. END;
  116. END
  117. END
  118. END;
  119. END ListPlugins;
  120. (** List all commands of the specified module. *)
  121. PROCEDURE ListCommands*(context : Commands.Context); (** module *)
  122. VAR m : Modules.Module; moduleName : Modules.Name; i : LONGINT;
  123. BEGIN
  124. context.arg.SkipWhitespace;
  125. context.arg.String(moduleName);
  126. m := Modules.ModuleByName(moduleName);
  127. IF m # NIL THEN
  128. FOR i := 0 TO LEN(m.command)-1 DO
  129. context.out.String(m.name); context.out.Char(".");
  130. context.out.String(m.command[i].name);
  131. context.out.Ln;
  132. END
  133. ELSE
  134. context.error.String("Module not found"); context.error.Ln
  135. END;
  136. END ListCommands;
  137. PROCEDURE List*(context : Commands.Context);
  138. VAR string : ARRAY 32 OF CHAR;
  139. BEGIN
  140. context.arg.SkipWhitespace;
  141. context.arg.String(string);
  142. IF (string = "plugins") THEN ListPlugins(context);
  143. ELSIF (string = "modules") THEN ListModules(context);
  144. ELSIF (string = "commands") THEN ListCommands(context);
  145. ELSE
  146. context.error.String('Usage: System.List ("plugins"|"modules"|("commands" moduleName))');
  147. context.error.Ln;
  148. END;
  149. END List;
  150. PROCEDURE ModuleIsLoaded(CONST name : Modules.Name) : BOOLEAN;
  151. BEGIN
  152. RETURN Modules.ModuleByName(name) # NIL;
  153. END ModuleIsLoaded;
  154. (** Show all modules that import 'basemodule' (transitively) and are currently loaded. *)
  155. PROCEDURE WhoImports*(context : Commands.Context); (** basemodule ~ *)
  156. VAR name : Modules.Name; root, m : Module;
  157. BEGIN
  158. context.arg.SkipWhitespace;
  159. context.arg.String(name);
  160. IF ModuleIsLoaded(name) THEN
  161. root := CopyModules();
  162. m := root;
  163. WHILE m # NIL DO
  164. IF Imports(root, m, name) THEN
  165. context.out.String(m.m.name); context.out.Ln;
  166. END;
  167. m := m.next;
  168. END;
  169. ELSE
  170. context.error.String("Module "); context.error.String(name); context.error.String(" is not loaded."); context.error.Ln;
  171. END;
  172. END WhoImports;
  173. (** Check whether the specified module is currenlty loaded. *)
  174. PROCEDURE IsLoaded*(context : Commands.Context);
  175. VAR name : Modules.Name;
  176. BEGIN
  177. context.arg.SkipWhitespace;
  178. context.arg.String(name);
  179. context.out.String("Module "); context.out.String(name);
  180. IF ModuleIsLoaded(name) THEN
  181. context.out.String(" is loaded.");
  182. ELSE
  183. context.out.String(" is not loaded.");
  184. END;
  185. context.out.Ln;
  186. END IsLoaded;
  187. PROCEDURE ModuleState*(context: Commands.Context);
  188. VAR name: Modules.Name; module: Modules.Module; msg: ARRAY 256 OF CHAR; res: WORD;
  189. BEGIN
  190. context.arg.SkipWhitespace;
  191. context.arg.String(name);
  192. module := Modules.ThisModule(name, res, msg);
  193. context.result := res;
  194. IF (res = Modules.Ok) THEN
  195. context.out.String(name);
  196. context.out.String(" crc "); context.out.Hex(module.crc,-8);
  197. context.out.String(" state: "); context.out.Ln;
  198. Reflection.ModuleState(context.out, module);
  199. ELSE
  200. context.error.String("Could not load module "); context.error.String(name);
  201. context.error.String(", res: "); context.error.Int(res, 0);
  202. IF (msg # "") THEN
  203. context.error.String(" ("); context.error.String(msg); context.error.String(")");
  204. END;
  205. context.error.Ln;
  206. END;
  207. END ModuleState;
  208. (** Load the specified module *)
  209. PROCEDURE Load*(context : Commands.Context); (** modulename ~ *)
  210. VAR name : Modules.Name; module : Modules.Module; msg : ARRAY 256 OF CHAR; res : WORD;
  211. BEGIN
  212. context.arg.SkipWhitespace;
  213. context.arg.String(name);
  214. IF ModuleIsLoaded(name) THEN
  215. context.result := Modules.Ok;
  216. context.out.String(name); context.out.String(" is already loaded."); context.out.Ln;
  217. ELSE
  218. module := Modules.ThisModule(name, res, msg);
  219. context.result := res;
  220. IF (res = Modules.Ok) THEN
  221. context.out.String(name); context.out.String(" loaded."); context.out.Ln;
  222. ELSE
  223. context.error.String("Could not load module "); context.error.String(name);
  224. context.error.String(", res: "); context.error.Int(res, 0);
  225. IF (msg # "") THEN
  226. context.error.String(" ("); context.error.String(msg); context.error.String(")");
  227. END;
  228. context.error.Ln;
  229. END;
  230. END;
  231. END Load;
  232. (** Free all modules that import basemodule (transitively). *)
  233. PROCEDURE FreeDownTo*(context : Commands.Context); (** basemodule ~ *)
  234. VAR
  235. modulename : ARRAY 128 OF CHAR;
  236. root, m: Module; res: WORD;
  237. timer: Kernel.Timer; msg: ARRAY 64 OF CHAR;
  238. nbrOfUnloadedModules : LONGINT;
  239. BEGIN
  240. context.arg.SkipWhitespace;
  241. context.arg.String(modulename);
  242. LockOberon;
  243. NEW(timer); timer.Sleep(200); (* temporary workaround for race with System.FreeOberon *)
  244. root := CopyModules();
  245. nbrOfUnloadedModules := 0;
  246. m := root;
  247. WHILE m # NIL DO
  248. IF Imports(root, m, modulename) THEN
  249. IF TraceFreeDownTo IN Trace THEN
  250. context.out.String(m.m.name); context.out.Ln;
  251. END;
  252. Modules.FreeModule(m.m.name, res, msg);
  253. IF res # 0 THEN
  254. context.error.String(msg);
  255. ELSE
  256. INC(nbrOfUnloadedModules);
  257. END
  258. END;
  259. m := m.next
  260. END;
  261. UnlockOberon; (* in case Oberon still running *)
  262. context.out.String("Unloaded "); context.out.Int(nbrOfUnloadedModules, 0); context.out.String(" modules."); context.out.Ln;
  263. END FreeDownTo;
  264. (** Unload modules from memory *)
  265. PROCEDURE Free*(context : Commands.Context); (** {modulename} ~ *)
  266. VAR name, msg : ARRAY 64 OF CHAR; res : WORD;
  267. BEGIN
  268. WHILE context.arg.GetString(name) DO
  269. IF name # "" THEN
  270. context.out.String("Unloading "); context.out.String(name); context.out.String("... ");
  271. Modules.FreeModule(name, res, msg);
  272. IF res # 0 THEN context.out.String(msg)
  273. ELSE context.out.String("done.")
  274. END;
  275. context.out.Ln;
  276. END;
  277. END;
  278. END Free;
  279. PROCEDURE Kill*(context : Commands.Context); (** pid { pid } ~ *)
  280. VAR process : Objects.Process; pid : LONGINT;
  281. BEGIN {EXCLUSIVE}
  282. WHILE context.arg.GetInteger(pid, FALSE) DO
  283. context.out.Int(pid, 0);
  284. process := ProcessInfo.GetProcess(pid);
  285. IF process # NIL THEN
  286. Objects.TerminateThis(process, FALSE);
  287. context.out.String(" Process killed")
  288. ELSE
  289. context.out.String(" Process not found")
  290. END;
  291. context.out.Ln;
  292. END;
  293. END Kill;
  294. PROCEDURE ShowProcesses*(context : Commands.Context); (** [options] ~ *)
  295. VAR
  296. options : Options.Options;
  297. processes : ARRAY ProcessInfo.MaxNofProcesses OF Objects.Process;
  298. nofProcesses : LONGINT;
  299. string : ARRAY 16 OF CHAR;
  300. i : LONGINT;
  301. BEGIN
  302. NEW(options);
  303. options.Add("s", "sort", Options.String);
  304. IF options.Parse(context.arg, context.error) THEN
  305. ProcessInfo.GetProcesses(processes, nofProcesses);
  306. IF options.GetString("sort", string) THEN
  307. IF (string = "id") THEN
  308. ProcessInfo.Sort(processes, nofProcesses, ProcessInfo.SortByID);
  309. ELSIF (string = "priority") THEN
  310. ProcessInfo.Sort(processes, nofProcesses, ProcessInfo.SortByPriority);
  311. ELSIF (string = "mode") THEN
  312. ProcessInfo.Sort(processes, nofProcesses, ProcessInfo.SortByMode);
  313. ELSE
  314. context.error.String("Sort option "); context.error.String(string);
  315. context.error.String(" unknown... ignore."); context.error.Ln;
  316. END;
  317. END;
  318. FOR i := 0 TO nofProcesses - 1 DO ProcessInfo.ShowProcess(processes[i], context.out); END;
  319. context.out.Int(nofProcesses, 0); context.out.String(" processes"); context.out.Ln;
  320. ProcessInfo.Clear(processes);
  321. END;
  322. END ShowProcesses;
  323. PROCEDURE ShowStacks*(context : Commands.Context);
  324. VAR processes : ARRAY ProcessInfo.MaxNofProcesses OF Objects.Process; nofProcesses, i : LONGINT;
  325. BEGIN
  326. ProcessInfo.GetProcesses(processes, nofProcesses);
  327. FOR i := 0 TO nofProcesses - 1 DO ProcessInfo.ShowStack(processes[i], context.out); END;
  328. ProcessInfo.Clear(processes);
  329. END ShowStacks;
  330. PROCEDURE ShowStack*(context : Commands.Context); (** pid ~ *)
  331. VAR process : Objects.Process; pid : LONGINT;
  332. BEGIN
  333. context.arg.SkipWhitespace;
  334. context.arg.Int(pid, FALSE);
  335. process := ProcessInfo.GetProcess(pid);
  336. IF (process # NIL) THEN
  337. context.out.String("Stack of process ID = "); context.out.Int(pid, 0); context.out.Ln;
  338. ProcessInfo.ShowStack(process, context.out);
  339. ELSE
  340. context.error.String("Process ID = "); context.error.Int(pid, 0); context.error.String(" not found.");
  341. context.error.Ln;
  342. END;
  343. END ShowStack;
  344. (** Inspect free Heaps space *)
  345. PROCEDURE Watch*(context : Commands.Context);
  346. VAR total, free, largest: SIZE;
  347. BEGIN
  348. Heaps.GetHeapInfo(total,free,largest);
  349. context.out.String("Heaps: total="); context.out.Int(total,0);
  350. context.out.String(" bytes; free="); context.out.Int(free,0);
  351. context.out.String(" bytes; largest free block size="); context.out.Int(largest,0);
  352. context.out.String(" bytes"); context.out.Ln;
  353. END Watch;
  354. (* Changes the extension, Usage: RenameExtension extFrom extTo~ *)
  355. PROCEDURE RenameExtension*(context : Commands.Context);
  356. VAR
  357. enumerator : Files.Enumerator;
  358. oe, ne, temp: ARRAY 16 OF CHAR;
  359. name, file, ext : Files.FileName; flags : SET; time, date, size, res : LONGINT;
  360. BEGIN
  361. context.arg.SkipWhitespace; context.arg.String(oe);
  362. context.arg.SkipWhitespace; context.arg.String(ne);
  363. NEW(enumerator);
  364. temp := "*.";
  365. Strings.Append(temp, oe);
  366. enumerator.Open(temp, {});
  367. temp := ".";
  368. Strings.Append(temp, ne);
  369. context.out.String("-- Renaming Extension --"); context.out.Ln;
  370. WHILE enumerator.HasMoreEntries() DO
  371. IF enumerator.GetEntry(name, flags, time, date, size) THEN
  372. Strings.GetExtension(name, file, ext);
  373. Strings.Append(file, temp);
  374. context.out.String("Renaming: "); context.out.String(name); context.out.String(" to: "); context.out.String(file);
  375. Files.Rename(name, file, res);
  376. IF res = 0 THEN context.out.String(" done"); ELSE context.out.String(" Error!"); END;
  377. context.out.Ln;
  378. END;
  379. END;
  380. context.out.String("-- all done --"); context.out.Ln;
  381. enumerator.Close;
  382. END RenameExtension;
  383. PROCEDURE IsDelimiter(ch : CHAR) : BOOLEAN;
  384. BEGIN
  385. RETURN (ch = " ") OR (ch = CR) OR (ch = LF) OR (ch = TAB) OR (ch = ";") OR (ch = 0X);
  386. END IsDelimiter;
  387. PROCEDURE DoFile*(context: Commands.Context);
  388. VAR
  389. newContext: Commands.Context;
  390. file: Files.File;
  391. r: Streams.Reader;
  392. filename: Files.FileName;
  393. res: WORD;
  394. msg: ARRAY 256 OF CHAR;
  395. BEGIN
  396. IF context.arg.GetString(filename) THEN
  397. file := Files.Old(filename);
  398. IF file # NIL THEN
  399. r := NEW Files.Reader(file, 0);
  400. NEW(newContext, context.in, r, context.out, context.error, context.caller);
  401. Commands.Activate("System.DoCommands", newContext, {Commands.Wait}, res, msg);
  402. context.result := res;
  403. ELSE
  404. context.error.String("Error: no such file: "); context.error.String(filename); context.error.Ln;
  405. END;
  406. ELSE
  407. context.error.String("Error: mo filename provided."); context.error.String(filename); context.error.Ln;
  408. END;
  409. END DoFile;
  410. (** Sequentially execute a list of commands .
  411. IMPORTANT: This command is specially handled by command interpreters that support it. It is the only command
  412. in the system for which two tilde characters (only separated by whitespace) are used to delimit the parameter string.
  413. If you change the name of this module or this command, you have to adapt:
  414. - WMTextView.TextView.FindCommandRange *)
  415. PROCEDURE DoCommands*(context : Commands.Context); (** command {"~" command} "~" *)
  416. VAR
  417. newContext : Commands.Context;
  418. commands : Strings.StringArray;
  419. command, parameters, paramString : Strings.String;
  420. temp : Strings.String;
  421. msg : ARRAY 128 OF CHAR;
  422. cur, available, i, j, k, res : LONGINT;
  423. PROCEDURE CreateContext(paramString : Strings.String) : Commands.Context;
  424. VAR c : Commands.Context; arg : Streams.StringReader; dummy : ARRAY 1 OF CHAR;
  425. BEGIN
  426. IF (paramString = NIL) THEN
  427. NEW(arg, 1); dummy := ""; arg.SetRaw(dummy, 0, 1);
  428. ELSE
  429. NEW(arg, LEN(paramString)); arg.SetRaw(paramString^, 0, LEN(paramString));
  430. END;
  431. NEW(c, context.in, arg, context.out, context.error, context.caller);
  432. RETURN c;
  433. END CreateContext;
  434. PROCEDURE Resize(VAR t: Strings.String; len: LONGINT);
  435. VAR new: Strings.String; i: LONGINT;
  436. BEGIN
  437. NEW(new, len);
  438. IF t # NIL THEN
  439. FOR i := 0 TO LEN(t)-1 DO new[i] := t[i] END;
  440. END;
  441. t := new;
  442. END Resize;
  443. BEGIN
  444. cur := context.arg.Available();
  445. IF (cur < 1) THEN RETURN; END;
  446. NEW(temp, cur + 1);
  447. available := 0;
  448. WHILE cur > 0 DO
  449. Resize(temp, available+cur+1);
  450. context.arg.Bytes(temp^, available, cur, i); (* ignore i *)
  451. INC(available, cur);
  452. cur := context.arg.Available();
  453. END;
  454. RemoveComments(temp^, available);
  455. Strings.Truncate (temp^, available);
  456. commands := Strings.Split(temp^, "~");
  457. NEW(command, LEN(temp)); NEW(parameters, LEN(temp));
  458. i := 0;
  459. LOOP
  460. Strings.TrimWS(commands[i]^);
  461. IF (commands[i]^ = "") THEN
  462. (* This means that two tilde characters were only separated by whitespace. One delimits
  463. the last command we have executed and the other one delimits the System.DoCommands parameters *)
  464. EXIT;
  465. END;
  466. (* extract command *)
  467. j := 0; k := 0;
  468. WHILE ~IsDelimiter(commands[i][j]) DO command[k] := commands[i][j]; INC(k); INC(j); END;
  469. command[k] := 0X;
  470. IF k = 0 THEN EXIT; END; (* end of string *)
  471. (* extract parameters *)
  472. k := 0;
  473. IF (commands[i][j] # "~") & (commands[i][j] # 0X) THEN
  474. INC(j); WHILE (commands[i][j] # 0X) & (commands[i][j] # "~") DO parameters[k] := commands[i][j]; INC(k); INC(j); END;
  475. parameters[k] := 0X;
  476. END;
  477. IF k > 0 THEN
  478. NEW(paramString, k+1);
  479. FOR j := 0 TO k DO paramString[j] := parameters[j]; END;
  480. ELSE
  481. paramString := NIL;
  482. END;
  483. newContext := CreateContext(paramString);
  484. IF TraceCommands IN Trace THEN
  485. context.out.String("System.DoCommands: Execute command '"); context.out.String(command^);
  486. context.out.String("' parameters: ");
  487. IF (paramString = NIL) THEN context.out.String("None");
  488. ELSE
  489. context.out.String("'"); context.out.String(paramString^); context.out.String("'");
  490. END;
  491. context.out.Ln;
  492. END;
  493. Commands.Activate(command^, newContext, {Commands.Wait}, res, msg);
  494. IF res # Commands.Ok THEN
  495. context.result := res;
  496. context.error.String("System.DoCommands: Command: '");
  497. context.error.String(command^); context.error.String("', parameters: ");
  498. IF paramString = NIL THEN
  499. context.error.String("None");
  500. ELSE
  501. context.error.String("'"); context.error.String(paramString^); context.error.String("'");
  502. END;
  503. context.error.String(" failed: ");
  504. context.error.String(msg); context.error.String(" (res: "); context.error.Int(res, 0); context.error.String(")");
  505. context.error.Ln;
  506. EXIT;
  507. END;
  508. INC(i);
  509. IF i >= LEN(commands) THEN EXIT; END;
  510. END;
  511. END DoCommands;
  512. (** remove Oberon style comments (parantheses and asterisks) from a string of a certain length.
  513. - comments may be nested arbitrarily
  514. - the operation is performed in situ: comments are replaced with whitespace characters
  515. **)
  516. PROCEDURE RemoveComments(VAR string: ARRAY OF CHAR; length: LONGINT);
  517. VAR
  518. pos, level: LONGINT;
  519. BEGIN
  520. level := 0;
  521. pos := 0;
  522. WHILE pos <= length - 1 DO
  523. IF (string[pos] = '(') & (pos + 1 <= length - 1) & (string[pos + 1] = '*') THEN
  524. (* a comment opened -> replace *)
  525. INC(level);
  526. string[pos] := ' '; string[pos + 1] := ' '; INC(pos, 2)
  527. ELSIF (string[pos] = '*') & (pos + 1 <= length - 1) & (string[pos + 1] = ')') THEN
  528. (* a comment is closed -> replace *)
  529. DEC(level);
  530. string[pos] := ' '; string[pos + 1] := ' '; INC(pos, 2)
  531. ELSIF level <= 0 THEN
  532. (* character outside any comment -> leave as is *)
  533. INC(pos)
  534. ELSE
  535. (* character within a comment -> replace *)
  536. string[pos] := ' '; INC(pos)
  537. END
  538. END
  539. END RemoveComments;
  540. PROCEDURE Repeat*(context : Commands.Context); (* nofTimes command [command parameters] ~ *)
  541. VAR
  542. command, msg : ARRAY 128 OF CHAR;
  543. parameterPosition : LONGINT;
  544. nofTimes, res : LONGINT;
  545. BEGIN
  546. nofTimes := 0; command := "";
  547. context.arg.SkipWhitespace; context.arg.Int(nofTimes, FALSE);
  548. context.arg.SkipWhitespace; context.arg.String(command);
  549. IF (nofTimes > 0) & (command # "") THEN
  550. res := Commands.Ok;
  551. parameterPosition := context.arg.Pos();
  552. WHILE (nofTimes > 0) & (res = Commands.Ok) DO
  553. context.arg.SetPos(parameterPosition);
  554. Commands.Activate(command, context, {Commands.Wait}, res, msg);
  555. DEC(nofTimes);
  556. END;
  557. IF (res # Commands.Ok) THEN
  558. context.result := res;
  559. context.out.String("Error in command '"); context.out.String(command); context.out.String("', res: ");
  560. context.out.Int(res, 0); context.out.Ln;
  561. END;
  562. END;
  563. END Repeat;
  564. (** Time interval measurement
  565. - start/starth [number]: Set timer <number> to current time (number = 0 if omitted)
  566. - elapsed/elapsedh [number]: Display time difference between timer <number> and the current time (number = 0 if omitted)
  567. - diff/diffh number1 number2: Display time difference between the two timers
  568. *)
  569. PROCEDURE Timer*(context : Commands.Context); (** [ ["start"["h"] [number]] | ["elapsed"["h"] [number]] | ["diff"["h"] number1 number2] ] ~ *)
  570. VAR
  571. string : ARRAY 128 OF CHAR; nbr1, nbr2 : LONGINT;
  572. PROCEDURE ShowUsage;
  573. BEGIN
  574. context.out.String('Usage: System.Timer [ ["start" [number]] | ["elapsed" [number]] | ["diff" number1 number2] ]');
  575. context.out.Ln;
  576. END ShowUsage;
  577. PROCEDURE Valid(number : LONGINT) : BOOLEAN;
  578. BEGIN
  579. RETURN (0 <= number) & (number < MaxTimers);
  580. END Valid;
  581. BEGIN {EXCLUSIVE}
  582. context.arg.SkipWhitespace; context.arg.String(string);
  583. context.arg.SkipWhitespace; context.arg.Int(nbr1, FALSE);
  584. context.arg.SkipWhitespace; context.arg.Int(nbr2, FALSE);
  585. IF ~Valid(nbr1) THEN ShowUsage; RETURN; END;
  586. IF (string = "start") THEN
  587. timers[nbr1] := Dates.Now();
  588. ELSIF (string = "elapsed") THEN
  589. Strings.ShowTimeDifference(timers[nbr1], Dates.Now(), context.out);
  590. ELSIF Valid(nbr2) THEN
  591. IF (string = "diff") THEN
  592. Strings.ShowTimeDifference(timers[nbr1], timers[nbr2], context.out);
  593. ELSE
  594. ShowUsage;
  595. END;
  596. ELSE
  597. ShowUsage;
  598. END;
  599. END Timer;
  600. (** If no parameter is specified, this command displays the system time on Kernel Log. *)
  601. PROCEDURE Time*(context : Commands.Context); (** ~ *)
  602. VAR datetime : Dates.DateTime; string : ARRAY 32 OF CHAR;
  603. BEGIN
  604. datetime := Dates.Now();
  605. Strings.FormatDateTime(DateTimeFormat, datetime, string);
  606. context.out.String(string); context.out.Ln;
  607. END Time;
  608. (** Display the content of the specified file *)
  609. PROCEDURE ShowFile*(context : Commands.Context); (** filename ~ *)
  610. VAR filename : Files.FileName; file : Files.File; reader : Files.Reader; ch : CHAR;
  611. BEGIN
  612. IF context.arg.GetString(filename) THEN
  613. file := Files.Old(filename);
  614. IF (file # NIL) THEN
  615. Files.OpenReader(reader, file, 0);
  616. REPEAT
  617. reader.Char(ch);
  618. context.out.Char(ch);
  619. UNTIL (reader.res # Streams.Ok);
  620. ELSE
  621. context.error.String("Could not open file "); context.error.String(filename); context.error.Ln;
  622. context.result := Commands.CommandError;
  623. END;
  624. END;
  625. END ShowFile;
  626. (** Display a string on the context output stream *)
  627. PROCEDURE Show*(context : Commands.Context); (** string ~ *)
  628. VAR ch : CHAR;
  629. BEGIN
  630. REPEAT
  631. ch := context.arg.Get();
  632. IF (ch # 0X) THEN context.out.Char(ch); END;
  633. UNTIL (context.arg.res # Streams.Ok);
  634. END Show;
  635. (** Print carriage return on the context output stream *)
  636. PROCEDURE Ln*(context : Commands.Context); (** ~ *)
  637. BEGIN
  638. context.out.Ln;
  639. END Ln;
  640. (** Block for ms milliseconds *)
  641. PROCEDURE Wait*(context : Commands.Context); (** ms ~ *)
  642. VAR timer : Kernel.Timer; milliseconds : LONGINT;
  643. BEGIN
  644. IF context.arg.GetInteger(milliseconds, FALSE) & (milliseconds > 0) THEN
  645. NEW(timer);
  646. timer.Sleep(milliseconds);
  647. END;
  648. END Wait;
  649. PROCEDURE Reboot*;
  650. BEGIN
  651. Modules.Shutdown(Modules.Reboot);
  652. END Reboot;
  653. PROCEDURE PowerDown*;
  654. BEGIN
  655. Modules.Shutdown(Modules.PowerDown);
  656. END PowerDown;
  657. (** Invoke garbage collector *)
  658. PROCEDURE CollectGarbage*(context : Commands.Context);
  659. BEGIN
  660. context.out.String("Collecting garbage... ");
  661. Kernel.GC;
  662. context.out.String("done."); context.out.Ln;
  663. END CollectGarbage;
  664. PROCEDURE Version*(context : Commands.Context);
  665. BEGIN
  666. context.out.String(Machine.version);context.out.String(" Kernel CRC="); context.out.Hex(SystemVersion.BootCRC, 8); context.out.Ln;
  667. END Version;
  668. END System.
  669. System.Free System ~
  670. System.Kill 57 ~
  671. System.Time ~
  672. System.Show Hello World ~
  673. System.DoCommands
  674. System.Timer start ~
  675. System.Show System Time ~ System.Time ~ System.Ln ~
  676. System.Show System Time again ~ System.Time ~ System.Ln ~
  677. System.Wait 2000 ~
  678. System.Show Time elapsed: ~ System.Timer elapsed ~ System.Ln ~
  679. ~
  680. System.CollectGarbage ~
  681. System.ListModules -r ~
  682. System.ModuleState Heaps ~