System.Mod 23 KB

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