FSTools.Mod 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. (* Aos, Copyright 2001, Pieter Muller, ETH Zurich *)
  2. MODULE FSTools; (** AUTHOR "be"; PURPOSE "Files Tools"; *)
  3. (**
  4. * Usage:
  5. *
  6. * FSTools.Mount prefix alias [volpar] ["|" fspar] ~ Mount the specified volume.
  7. * FSTools.Unmount prefix [\f] ~ Unmount the specified volume. Use /f to force unmounting.
  8. *
  9. * FSTools.SetDefault prefix ~ Set the specified volume as default volume.
  10. * FSTools.Watch ~ Diplays a list of all mounted file systems
  11. *
  12. * FSTools.CopyFiles [-ioq] {sourcefile " => " destfile} ~ Copy the specified files to
  13. * FSTools.RenameFiles [-i] {oldname " => " newname} ~ Rename files
  14. * FSTools.DeleteFiles [-i] {file} ~ Delete the specified files
  15. * FSTools.Directory [-ts] ~ Show Directory (t: show creation times, s: show file sizes)
  16. *
  17. * FSTools.Safe ~ disallow pattern matching
  18. * FSTools.Unsafe ~ allow pattern matching
  19. *
  20. * Options i, o and q:
  21. *
  22. * i: ignore errors, e.g. continue with deletion of files if a file could not be deleted
  23. * o: force overwriting existing files
  24. * q: quiet mode
  25. *
  26. * Examples:
  27. *
  28. * FSTools.Mount FAT FatFS IDE0#4~
  29. * FSTools.Unmount FAT~
  30. *
  31. * FSTools.CopyFiles AOS:Configuration.XML => FAT:Configuration.XML AOS:Test.Mod => FAT:Test.Mod ~
  32. * FSTools.RenameFiles Configuration.XML => Configuration.Bak ~
  33. * FSTools.DeleteFiles Test.Mod Bimbo.Mod ~
  34. * FSTools.Directory -s ~
  35. *
  36. * Pattern matching:
  37. *
  38. * Supported by: CopyFiles, RenameFiles, DeleteFiles and Directory
  39. *
  40. * WARNING: If no prefix is specified, the source mask if checked against all files on all mounted volumes, i.e. the command
  41. * FSTools.DeleteFiles * ~ would DELETE ALL FILES ON ALL MOUNTED partitions.
  42. *
  43. * The source mask may contain an arbitrary number of '*' (matches any string) and '?' (matches any character) characters.
  44. * For operations that have a target, the target mask semantics is the following:
  45. *
  46. * - '?' characters are not allowed in the target mask
  47. * - '*' characters are not allowed in the prefix and path
  48. * - every occurence of the character '*' is replaced by ...
  49. * ... the source file name if there is no '.' character on the left side of the '*' character
  50. * ... the source file extension if there is at least one '.' character on the left side of the '*' character
  51. *
  52. * Notes:
  53. * - Files treats the right-most '.*' as file extension, e.g. the file extension of 'AosBimbo.Test.00.Bak.Mod' is '.Mod'
  54. *
  55. *)
  56. IMPORT Modules, Commands, Options, Streams, Files, Configuration, Dates, Strings;
  57. CONST
  58. MaxNameLen = 512; (* Maximum file name length including path and 0X-termination *)
  59. InitialFilelistSize = 1024;
  60. (* Layout for Directory operation *)
  61. Column1 = 30;
  62. FormatDateTime = "dd.mm.yyyy hh:nn:ss";
  63. Error = -1;
  64. CR = 0DX; LF = 0AX;
  65. TYPE
  66. String = Strings.String;
  67. FileList = POINTER TO ARRAY OF String;
  68. EnumProc = PROCEDURE(context : Commands.Context);
  69. VAR
  70. unsafeMode : BOOLEAN;
  71. PROCEDURE ExpandAlias(CONST alias : ARRAY OF CHAR; VAR genvol, genfs: ARRAY OF CHAR);
  72. VAR t: ARRAY 64 OF CHAR; i, j, res: LONGINT;
  73. BEGIN
  74. genvol[0] := 0X; genfs[0] := 0X;
  75. t := "Files.Alias.";
  76. i := 0; WHILE t[i] # 0X DO INC(i) END;
  77. j := 0; WHILE alias[j] # 0X DO t[i] := alias[j]; INC(i); INC(j) END;
  78. t[i] := 0X;
  79. Configuration.Get(t, t, res);
  80. i := 0;
  81. WHILE (t[i] # 0X) & (t[i] # ";") DO genvol[i] := t[i]; INC(i) END;
  82. genvol[i] := 0X;
  83. IF (t[i] = ";") THEN
  84. j := 0; INC(i);
  85. WHILE (t[i] # 0X) DO genfs[j] := t[i]; INC(j); INC(i) END;
  86. genfs[j] := 0X
  87. END
  88. END ExpandAlias;
  89. PROCEDURE GetFileSystemFactory(CONST name : ARRAY OF CHAR; error : Streams.Writer) : Files.FileSystemFactory;
  90. VAR
  91. factory : Files.FileSystemFactory;
  92. moduleName, procedureName : Modules.Name; msg : ARRAY 128 OF CHAR; res : WORD;
  93. BEGIN
  94. factory := NIL;
  95. Commands.Split(name, moduleName, procedureName, res, msg);
  96. IF (res = Commands.Ok) THEN
  97. GETPROCEDURE(moduleName, procedureName, factory);
  98. IF factory = NIL THEN
  99. error.String('failed to get file system factory with name "'); error.String(name); error.String('"!'); error.Ln;
  100. END;
  101. ELSE
  102. error.String(msg); error.Ln;
  103. END;
  104. RETURN factory;
  105. END GetFileSystemFactory;
  106. PROCEDURE Mount*(context : Commands.Context); (** prefix alias [volpar] ["|" fspar] ~ *)
  107. VAR
  108. factory : Files.FileSystemFactory;
  109. parvol, parfs: Files.Parameters; i, res: LONGINT;
  110. alias, genvol, genfs : ARRAY 64 OF CHAR; prefix: Files.Prefix;
  111. BEGIN
  112. IF context.arg.GetString(prefix) & context.arg.GetString(alias) THEN
  113. ExpandAlias(alias, genvol, genfs);
  114. IF (Files.This(prefix) # NIL) THEN
  115. context.error.String(prefix); context.error.String("; already used"); context.error.Ln;
  116. context.result := Commands.CommandError;
  117. ELSIF (genvol = "") OR (genfs = "") THEN
  118. context.error.String(prefix); context.error.String(": unknown alias "); context.error.String(alias); context.error.Ln;
  119. context.result := Commands.CommandError;
  120. ELSE
  121. IF genvol # "NIL" THEN
  122. NEW(parvol, context.in, context.arg, context.out, context.error, context.caller);
  123. parvol.vol := NIL; res := 0;
  124. COPY(prefix, parvol.prefix);
  125. factory := GetFileSystemFactory(genvol, context.error);
  126. IF (factory # NIL) THEN
  127. factory(parvol);
  128. END;
  129. IF (factory = NIL) OR (parvol.vol = NIL) THEN res := 1; END;
  130. ELSE
  131. i := 0
  132. END;
  133. IF (res = Commands.Ok) THEN
  134. NEW(parfs, context.in, context.arg, context.out, context.error, context.caller);
  135. IF (parvol # NIL) THEN parfs.vol := parvol.vol; ELSE parfs.vol := NIL; END;
  136. COPY(prefix, parfs.prefix);
  137. factory := GetFileSystemFactory(genfs, context.error);
  138. IF (factory # NIL) THEN
  139. factory(parfs);
  140. IF (Files.This(prefix) = NIL) THEN
  141. res := 1
  142. ELSE
  143. context.out.String(prefix); context.out.String(": mounted"); context.out.Ln;
  144. END;
  145. ELSE
  146. res := 1;
  147. END;
  148. IF (res # 0) & (parvol # NIL) & (parvol.vol # NIL) THEN
  149. parvol.vol.Finalize() (* unmount volume *)
  150. END
  151. ELSE
  152. context.result := Commands.CommandError;
  153. END
  154. END;
  155. ELSE
  156. context.error.String('Expected parameters: prefix alias ([volpar] ["|" fspar]'); context.error.Ln;
  157. context.result := Commands.CommandParseError;
  158. END;
  159. END Mount;
  160. PROCEDURE Unmount*(context : Commands.Context); (** prefix[\f] *)
  161. VAR prefix: Files.Prefix; fs: Files.FileSystem; i: LONGINT; force: BOOLEAN; option : ARRAY 8 OF CHAR; ch : CHAR;
  162. BEGIN
  163. context.arg.SkipWhitespace;
  164. i := 0; ch := context.arg.Peek();
  165. WHILE (i < LEN(prefix)-1) & (ch # ":") & (ch # "\") & (ch > " ") & (context.arg.res = Streams.Ok) DO
  166. context.arg.Char(ch); (* consume ch *)
  167. prefix[i] := ch;
  168. INC(i);
  169. ch := context.arg.Peek();
  170. END;
  171. prefix[i] := 0X;
  172. IF (ch = ":") THEN context.arg.Char(ch); (* consume ":" *) END;
  173. context.arg.SkipWhitespace; context.arg.String(option);
  174. force := option = "\F";
  175. fs := Files.This(prefix);
  176. IF fs # NIL THEN
  177. IF (fs.vol = NIL) OR force OR ~(Files.Boot IN fs.vol.flags) THEN
  178. Files.Remove(fs);
  179. context.out.String(prefix); context.out.Char(":");
  180. context.out.String(" unmounted"); context.out.Ln;
  181. ELSE
  182. context.error.String(prefix); context.error.Char(":");
  183. context.error.String(" can't unmount boot volume. Use \f parameter to force unmounting."); context.error.Ln;
  184. context.result := Commands.CommandError;
  185. END
  186. ELSE
  187. context.error.String(prefix); context.error.Char(":"); context.error.String(" not found"); context.error.Ln;
  188. context.result := Commands.CommandError;
  189. END
  190. END Unmount;
  191. PROCEDURE SetDefault*(context : Commands.Context); (** prefix *)
  192. VAR prefix: Files.Prefix; fs: Files.FileSystem; i: LONGINT; ft: Files.FileSystemTable;
  193. BEGIN
  194. context.arg.SkipWhitespace; context.arg.String(prefix);
  195. fs := Files.This(prefix);
  196. IF fs # NIL THEN
  197. Files.Promote(fs);
  198. Files.GetList(ft);
  199. IF ft # NIL THEN
  200. context.out.String("Path: ");
  201. FOR i := 0 TO LEN(ft)-1 DO
  202. context.out.String(ft[i].prefix); context.out.String(" "); context.out.Ln;
  203. END
  204. END
  205. ELSE
  206. context.error.String(prefix); context.error.String(": not found"); context.error.Ln;
  207. context.result := Commands.CommandError;
  208. END;
  209. END SetDefault;
  210. (* using the NIST standard for Kibi, Mebi & Gibi: http://physics.nist.gov/cuu/Units/binary.html *)
  211. PROCEDURE WriteK( k: LONGINT; out : Streams.Writer);
  212. VAR suffix: ARRAY 3 OF CHAR;
  213. BEGIN
  214. IF k < 10*1024 THEN COPY("Ki", suffix)
  215. ELSIF k < 10*1024*1024 THEN COPY("Mi", suffix); k := k DIV 1024
  216. ELSE COPY("Gi", suffix); k := k DIV (1024*1024)
  217. END;
  218. out.Int(k, 1); out.String(suffix); out.String("B");
  219. END WriteK;
  220. PROCEDURE Watch*(context : Commands.Context); (** ~ *)
  221. VAR prefix : Files.Prefix; free, total, i: LONGINT; fs: Files.FileSystem; ft: Files.FileSystemTable; found : BOOLEAN;
  222. BEGIN
  223. prefix := "";
  224. context.arg.SkipWhitespace; context.arg.String(prefix);
  225. found := FALSE;
  226. Files.GetList(ft);
  227. IF ft # NIL THEN
  228. FOR i := 0 TO LEN(ft)-1 DO
  229. fs := ft[i];
  230. IF (prefix = "") OR (prefix = fs.prefix) THEN
  231. found := TRUE;
  232. context.out.String(fs.prefix); context.out.String(": "); context.out.String(fs.desc);
  233. IF fs.vol # NIL THEN
  234. context.out.String(" on "); context.out.String(fs.vol.name);
  235. IF Files.ReadOnly IN fs.vol.flags THEN context.out.String(" (read-only)") END;
  236. IF Files.Removable IN fs.vol.flags THEN context.out.String(" (removable)") END;
  237. IF Files.Boot IN fs.vol.flags THEN context.out.String(" (boot)") END;
  238. context.out.Ln; context.out.String(" ");
  239. free := ENTIER(fs.vol.Available()/1024.0D0 * fs.vol.blockSize);
  240. total := ENTIER(fs.vol.size/1024.0D0 * fs.vol.blockSize);
  241. WriteK(free, context.out); context.out.String(" of ");
  242. WriteK(total, context.out); context.out.String(" free")
  243. END;
  244. context.out.Ln
  245. END;
  246. END;
  247. END;
  248. IF ~found THEN
  249. IF (prefix = "") THEN
  250. context.error.String("No file systems found.");
  251. ELSE
  252. context.error.String("File system "); context.error.String(prefix); context.error.String(" not found.");
  253. END;
  254. context.error.Ln; context.result := Commands.CommandError;
  255. END;
  256. END Watch;
  257. (** File operations *)
  258. (* Simple text formatting (assuming the use of fixed fonts) *)
  259. PROCEDURE Align(out : Streams.Writer; CONST string : ARRAY OF CHAR);
  260. VAR spaces, i : LONGINT;
  261. BEGIN
  262. spaces := Column1 - Strings.Length(string); IF spaces < 0 THEN spaces := 0; END;
  263. FOR i := 0 TO spaces-1 DO out.Char(" "); END;
  264. END Align;
  265. PROCEDURE Directory*(context : Commands.Context); (** [Options] [pattern] *)
  266. VAR
  267. options : Options.Options;
  268. string, pattern : ARRAY 256 OF CHAR;
  269. enum : Files.Enumerator;
  270. flags, fileflags : SET;
  271. count, total : LONGINT;
  272. time, date, size : LONGINT;
  273. name : ARRAY MaxNameLen OF CHAR;
  274. dt : Dates.DateTime;
  275. BEGIN
  276. NEW(options);
  277. options.Add("s", "size", Options.Flag);
  278. options.Add("t", "time", Options.Flag);
  279. IF options.Parse(context.arg, context.error) THEN
  280. flags := {};
  281. IF options.GetFlag("size") THEN INCL(flags, Files.EnumSize); END;
  282. IF options.GetFlag("time") THEN INCL(flags, Files.EnumTime); END;
  283. IF ~context.arg.GetString(pattern) THEN
  284. pattern := "";
  285. END;
  286. NEW(enum); enum.Open(pattern, flags);
  287. count := 0; total := 0;
  288. WHILE enum.GetEntry(name, fileflags, time, date, size) DO
  289. INC(count);
  290. context.out.String(name);
  291. IF Files.EnumSize IN flags THEN
  292. Align(context.out, name); context.out.Int(size, 10); context.out.Char("B");
  293. INC(total, size)
  294. END;
  295. IF Files.EnumTime IN flags THEN
  296. IF Files.EnumSize IN flags THEN context.out.String(" "); ELSE Align(context.out, name); END;
  297. dt := Dates.OberonToDateTime(date, time);
  298. Strings.FormatDateTime(FormatDateTime, dt, string);
  299. context.out.String(string);
  300. END;
  301. context.out.Ln;
  302. END;
  303. enum.Close;
  304. IF count > 1 THEN
  305. context.out.Int(count, 0); context.out.String(" files ");
  306. IF Files.EnumSize IN flags THEN
  307. context.out.String("use "); WriteK((total+1023) DIV 1024, context.out);
  308. END
  309. END;
  310. context.out.Ln;
  311. ELSE
  312. context.result := Commands.CommandParseError;
  313. END;
  314. END Directory;
  315. PROCEDURE EnumerateDirectory(
  316. enum : Files.Enumerator;
  317. enumProc : EnumProc;
  318. options : Options.Options;
  319. context : Commands.Context;
  320. CONST filemask : ARRAY OF CHAR;
  321. CONST arguments : ARRAY OF CHAR);
  322. VAR
  323. name : Files.FileName;
  324. flags : SET; time, date, size : LONGINT;
  325. subDirEnum : Files.Enumerator;
  326. PROCEDURE PrepareContext(context : Commands.Context; CONST currentFile, arguments : ARRAY OF CHAR);
  327. CONST PlaceHolder = "<#filename#>";
  328. VAR thisArguments : Strings.String; position : LONGINT;
  329. BEGIN
  330. NEW(thisArguments, Strings.Length(arguments) + 1024);
  331. COPY(arguments, thisArguments^);
  332. (* replace PlaceHolder string by current file's name *)
  333. position := Strings.Pos(PlaceHolder, arguments);
  334. WHILE (position >= 0) DO
  335. Strings.Delete(thisArguments^, position, Strings.Length(PlaceHolder));
  336. Strings.Insert(name, thisArguments^, position);
  337. position := Strings.Pos(PlaceHolder, thisArguments^);
  338. END;
  339. context.arg(Streams.StringReader).InitStringReader(Strings.Length(thisArguments^));
  340. context.arg(Streams.StringReader).Set(thisArguments^);
  341. END PrepareContext;
  342. BEGIN
  343. ASSERT((enum # NIL) & (enumProc # NIL) & (options # NIL) & (context # NIL));
  344. WHILE enum.GetEntry(name, flags, time, date, size) DO
  345. IF ~(Files.Directory IN flags) & Strings.Match(filemask, name) THEN
  346. PrepareContext(context, name, arguments);
  347. enumProc(context);
  348. context.out.Update;
  349. context.error.Update;
  350. ELSIF options.GetFlag("subdirectories") THEN
  351. IF options.GetFlag("directories") THEN
  352. Strings.Append(name, Files.PathDelimiter);
  353. PrepareContext(context, name, arguments);
  354. enumProc(context);
  355. Strings.Append(name, filemask);
  356. END;
  357. NEW(subDirEnum);
  358. subDirEnum.Open(name, {});
  359. EnumerateDirectory(subDirEnum, enumProc, options, context, filemask, arguments);
  360. subDirEnum.Close;
  361. END;
  362. END;
  363. enum.Close;
  364. END EnumerateDirectory;
  365. PROCEDURE Enumerate*(context : Commands.Context); (** [Options] pattern commandProc ~ *)
  366. VAR
  367. options : Options.Options;
  368. pattern, path, filemask : Files.FileName;
  369. commandProcStr, msg : ARRAY 128 OF CHAR;
  370. arguments : Strings.String;
  371. enumProc : EnumProc;
  372. moduleName, procedureName : Modules.Name;
  373. enum : Files.Enumerator;
  374. enumContext : Commands.Context;
  375. arg : Streams.StringReader;
  376. res : WORD;
  377. BEGIN
  378. NEW(options);
  379. options.Add("s", "subdirectories", Options.Flag);
  380. options.Add("d", "directories", Options.Flag);
  381. IF options.Parse(context.arg, context.error) THEN
  382. IF context.arg.GetString(pattern) & context.arg.GetString(commandProcStr) THEN
  383. Commands.Split(commandProcStr, moduleName, procedureName, res, msg);
  384. IF (res = Commands.Ok) THEN
  385. GETPROCEDURE(moduleName, procedureName, enumProc);
  386. IF (enumProc # NIL) THEN
  387. Files.SplitPath(pattern, path, filemask);
  388. NEW(enum);
  389. enum.Open(path, {});
  390. NEW(arg, 4096);
  391. NEW(arguments, context.arg.Available()); Strings.Truncate(arguments^, 0);
  392. context.arg.Bytes(arguments^, 0, context.arg.Available(), res); (* ignore res *)
  393. NEW(enumContext, context.in, arg, context.out, context.error, context.caller);
  394. EnumerateDirectory(enum, enumProc, options, enumContext, filemask, arguments^);
  395. enum.Close;
  396. ELSE
  397. context.error.String("Procedure "); context.error.String(commandProcStr); context.error.String(" not found");
  398. context.error.Ln; context.result := Commands.CommandError;
  399. END;
  400. ELSE
  401. context.error.String("Command procedure error, res: "); context.error.Int(res, 0);
  402. context.error.String(" ("); context.error.String(msg); context.error.String(")");
  403. context.error.Ln; context.result := Commands.CommandError;
  404. END;
  405. ELSE
  406. context.error.String("FSTools.Enumerate [Options] pattern ~"); context.error.Ln;
  407. context.result := Commands.CommandParseError;
  408. END;
  409. ELSE
  410. context.result := Commands.CommandParseError;
  411. END;
  412. END Enumerate;
  413. (** Create a new file and optionally fill it with content
  414. Option c: Transform <LF> into <CR><LF>
  415. Option r: Remove whitespace at beginning of line
  416. Option a: Append to file instead of creating new file
  417. *)
  418. PROCEDURE CreateFile*(context : Commands.Context); (** [Options] filename [content] ~ *)
  419. VAR
  420. options : Options.Options; cr, removeWhitespace : BOOLEAN;
  421. file : Files.File; filename : Files.FileName; writer : Files.Writer; ch : CHAR; pos: LONGINT;
  422. BEGIN
  423. NEW(options);
  424. options.Add("c", "cr", Options.Flag);
  425. options.Add("r", "remove", Options.Flag);
  426. options.Add("a", "append", Options.Flag);
  427. IF options.Parse(context.arg, context.error) THEN
  428. IF context.arg.GetString(filename) THEN
  429. cr := options.GetFlag("cr");
  430. removeWhitespace := options.GetFlag("remove");
  431. file := NIL;
  432. IF options.GetFlag("append") THEN
  433. file := Files.Old(filename);
  434. END;
  435. IF file = NIL THEN
  436. file := Files.New(filename);
  437. pos := 0;
  438. ELSE
  439. pos := file.Length();
  440. END;
  441. Files.OpenWriter(writer, file, pos);
  442. IF removeWhitespace THEN context.arg.SkipWhitespace; END;
  443. WHILE (context.arg.res = Streams.Ok) DO
  444. ch := context.arg.Get();
  445. IF (ch = LF) THEN
  446. IF cr THEN writer.Char(CR); END;
  447. IF removeWhitespace THEN context.arg.SkipWhitespace; END;
  448. END;
  449. IF ch # 0X THEN
  450. writer.Char(ch);
  451. END;
  452. END;
  453. writer.Update;
  454. Files.Register(file);
  455. context.out.String("Created file "); context.out.String(filename); context.out.Ln;
  456. ELSE
  457. context.out.String("FSTools.CreateFile filename [content] ~"); context.out.Ln;
  458. context.result := Commands.CommandParseError;
  459. END;
  460. ELSE
  461. context.result := Commands.CommandParseError;
  462. END;
  463. END CreateFile;
  464. PROCEDURE CopyTo*(context : Commands.Context); (** targetpath sourcepath {filename} ~ *)
  465. VAR targetPath, sourcePath, targetFullname, sourceFullname, filename : Files.FileName; overwrite : BOOLEAN; nofFilesCopied, nofErrors, res : LONGINT;
  466. BEGIN
  467. context.arg.SkipWhitespace; context.arg.String(targetPath);
  468. context.arg.SkipWhitespace; context.arg.String(sourcePath);
  469. nofFilesCopied := 0; nofErrors := 0;
  470. WHILE context.arg.GetString(filename) DO
  471. COPY(targetPath, targetFullname); Strings.Append(targetFullname, filename);
  472. COPY(sourcePath, sourceFullname); Strings.Append(sourceFullname, filename);
  473. overwrite := TRUE;
  474. Files.CopyFile(sourceFullname, targetFullname, overwrite, res);
  475. IF (res = Files.Ok) THEN
  476. INC(nofFilesCopied);
  477. ELSE
  478. INC(nofErrors);
  479. context.error.String("Error: Could not copy file "); context.error.String(sourceFullname);
  480. context.error.String(" to "); context.error.String(targetFullname); context.error.String(", res: ");
  481. context.error.Int(res, 0); context.error.Ln;
  482. context.result := Commands.CommandError;
  483. RETURN;
  484. END;
  485. END;
  486. context.out.Int(nofFilesCopied, 0); context.out.String(" files copied");
  487. IF (nofErrors > 0) THEN
  488. context.out.String(" ("); context.out.Int(nofErrors, 0); context.out.String(" errors)");
  489. context.result := Commands.CommandError;
  490. END;
  491. context.out.Ln;
  492. END CopyTo;
  493. (** Copy files *)
  494. PROCEDURE CopyFiles*(context : Commands.Context); (** [Options] {source => destination} ~ *)
  495. VAR
  496. source, destination : FileList;
  497. overwritten, error, ignoreErrors, quiet : BOOLEAN;
  498. nofFiles, res, n : LONGINT;
  499. options: Options.Options;
  500. BEGIN
  501. NEW(options);
  502. options.Add("o", "overwrite", Options.Flag); (* overwrite target file if it exists *)
  503. options.Add("i", "ignore", Options.Flag); (* continue on errors *)
  504. options.Add("n", "nolist", Options.Flag); (* only allow two arguments *)
  505. options.Add("q", "quiet", Options.Flag); (* do not print copied file names *)
  506. IF options.Parse(context.arg, context.error) THEN
  507. ignoreErrors := options.GetFlag("ignore");
  508. IF options.GetFlag("nolist") THEN (* source target *)
  509. nofFiles := GetSimpleFileLists(context, source, destination);
  510. ELSE (* {source => target} *)
  511. nofFiles := GetFileLists(context, source, destination);
  512. END;
  513. IF nofFiles # Error THEN
  514. quiet := options.GetFlag("quiet");
  515. IF ~quiet THEN context.out.String("Copying files..."); context.out.Ln; context.out.Update END;
  516. n := 0;
  517. WHILE(n < LEN(source)) & (source[n] # NIL) & (n < LEN(destination)) & (destination[n] # NIL) & (ignoreErrors OR ~error) DO
  518. IF ~quiet THEN
  519. context.out.String(" Copy "); context.out.String(source[n]^); context.out.String(" => ");
  520. context.out.String(destination[n]^); context.out.String(" ... ");
  521. context.out.Update;
  522. END;
  523. overwritten := options.GetFlag("overwrite");
  524. Files.CopyFile(source[n]^, destination[n]^, overwritten, res);
  525. IF res = Files.Ok THEN
  526. IF ~quiet THEN
  527. context.out.String("done");
  528. IF overwritten THEN context.out.String(" (overwritten)"); END;
  529. context.out.Char("."); context.out.Ln;
  530. context.out.Update;
  531. END;
  532. INC(n);
  533. ELSE
  534. IF quiet THEN
  535. context.out.String(" Copy "); context.out.String(source[n]^); context.out.String(" => ");
  536. context.out.String(destination[n]^);
  537. END;
  538. context.error.String("failed "); ShowRes(context.error, res); context.error.Ln;
  539. context.error.Update;
  540. error := TRUE;
  541. IF ~ignoreErrors THEN context.result := Commands.CommandError END;
  542. END;
  543. END;
  544. END;
  545. IF nofFiles # Error THEN
  546. context.out.Int(n, 0); context.out.String(" of "); context.out.Int(nofFiles, 0); context.out.String(" files copied."); context.out.Ln;
  547. ELSE
  548. context.out.String("No files copied."); context.out.Ln;
  549. IF ~ignoreErrors THEN context.result := Commands.CommandError END;
  550. END;
  551. END;
  552. END CopyFiles;
  553. PROCEDURE GenerateName(CONST prefix: ARRAY OF CHAR; index: LONGINT; VAR str: ARRAY OF CHAR);
  554. VAR startTime: Dates.DateTime; num: ARRAY 32 OF CHAR;
  555. BEGIN
  556. startTime := Dates.Now();
  557. Strings.FormatDateTime("_yyyymmdd__hhnnss",startTime,str);
  558. Strings.Concat(prefix,str,str);
  559. IF index # 0 THEN
  560. Strings.IntToStr(index,num);
  561. Strings.Append(str,"_");
  562. Strings.Concat(str,num,str);
  563. END;
  564. Strings.Concat(str,".bak",str);
  565. END GenerateName;
  566. PROCEDURE Backup*(context: Commands.Context);
  567. VAR index: LONGINT; fileList: FileList; nofFiles, n, res: LONGINT; str: Files.FileName; overwritten: BOOLEAN;
  568. BEGIN
  569. overwritten := FALSE;
  570. nofFiles := GetFileList(context, fileList);
  571. n := 0;
  572. WHILE (fileList[n] # NIL) DO
  573. index := -1;
  574. REPEAT
  575. INC(index);
  576. GenerateName(fileList[n]^, index, str);
  577. UNTIL Files.Old(str) = NIL;
  578. Files.CopyFile(fileList[n]^, str, overwritten, res);
  579. context.out.String("backed up "); context.out.String(fileList[n]^); context.out.String(" in "); context.out.String(str); context.out.Ln;
  580. ASSERT(~overwritten);
  581. INC(n);
  582. END;
  583. END Backup;
  584. (** Delete files *)
  585. PROCEDURE DeleteFiles*(context : Commands.Context); (** [Options] {file} ~ *)
  586. VAR
  587. filelist : FileList;
  588. error, ignoreErrors, silent : BOOLEAN;
  589. nofFiles, res, n, ndone : LONGINT;
  590. options : Options.Options;
  591. BEGIN
  592. NEW(options);
  593. options.Add("i", "ignore", Options.Flag);
  594. options.Add("s", "silent", Options.Flag);
  595. IF options.Parse(context.arg, context.error) THEN
  596. ignoreErrors := options.GetFlag("ignore");
  597. silent := options.GetFlag("silent");
  598. nofFiles := GetFileList(context, filelist);
  599. IF (nofFiles > 0) THEN
  600. context.out.String("Deleting files..."); context.out.Ln;
  601. n := 0; ndone := 0;
  602. WHILE(filelist[n] # NIL) & (ignoreErrors OR ~error) DO
  603. res := 0;
  604. IF ~silent THEN context.out.String(" Delete "); context.out.String(filelist[n]^); context.out.String(" ... "); context.out.Update; END;
  605. Files.Delete(filelist[n]^, res);
  606. IF res = Files.Ok THEN
  607. IF ~silent THEN context.out.String("done."); context.out.Ln; END;
  608. INC(ndone);
  609. ELSE
  610. IF silent THEN
  611. context.out.String(" Delete "); context.out.String(filelist[n]^); context.out.String(" ... "); context.out.Update;
  612. END;
  613. context.out.String("failed "); ShowRes(context.out, res); context.out.Ln;
  614. error := TRUE;
  615. IF ~ignoreErrors THEN context.result := Commands.CommandError END;
  616. END;
  617. INC(n);
  618. context.out.Update;
  619. END;
  620. context.out.Int(ndone, 0); context.out.String(" of "); context.out.Int(nofFiles, 0); context.out.String(" files deleted."); context.out.Ln;
  621. ELSIF (nofFiles = 0) THEN
  622. context.out.String("No files matching the mask found."); context.out.Ln;
  623. ELSE
  624. context.error.String("Syntax Error: No files deleted"); context.error.Ln;
  625. IF ~ignoreErrors THEN context.result := Commands.CommandError END;
  626. END;
  627. END;
  628. END DeleteFiles;
  629. (** Rename files. *)
  630. PROCEDURE RenameFiles*(context : Commands.Context); (** [Options] {source => destination} ~ *)
  631. VAR
  632. source, target : FileList;
  633. error, ignoreErrors : BOOLEAN;
  634. nofFiles, res, n : LONGINT;
  635. options : Options.Options;
  636. BEGIN
  637. NEW(options);
  638. options.Add("i", "ignore", Options.Flag); (* continue on errors *)
  639. options.Add("n", "nolist", Options.Flag);
  640. IF options.Parse(context.arg, context.error) THEN
  641. ignoreErrors := options.GetFlag("ignore");
  642. IF options.GetFlag("nolist") THEN
  643. nofFiles := GetSimpleFileLists(context, source, target);
  644. ELSE
  645. nofFiles := GetFileLists(context, source, target);
  646. END;
  647. IF nofFiles # Error THEN
  648. context.out.String("Renaming files..."); context.out.Ln;
  649. n := 0;
  650. WHILE(source[n] # NIL) & (target[n] # NIL) & (ignoreErrors OR ~error) DO
  651. res := 0;
  652. context.out.String(" Rename "); context.out.String(source[n]^); context.out.String(" => "); context.out.String(target[n]^); context.out.String(" ... ");
  653. Files.Rename(source[n]^, target[n]^, res);
  654. IF res # Files.Ok THEN
  655. context.error.String("failed "); ShowRes(context.error, res); context.error.Ln;
  656. error := TRUE;
  657. IF ~ignoreErrors THEN context.result := Commands.CommandError END;
  658. ELSE
  659. context.out.String("done."); context.out.Ln;
  660. INC(n);
  661. END;
  662. END;
  663. END;
  664. IF nofFiles # Error THEN
  665. context.out.Int(n, 0); context.out.String(" of "); context.out.Int(nofFiles, 0); context.out.String(" files renamed."); context.out.Ln;
  666. ELSE
  667. context.out.String("No files renamed."); context.out.Ln;
  668. IF ~ignoreErrors THEN context.result := Commands.CommandError END;
  669. END;
  670. END;
  671. END RenameFiles;
  672. PROCEDURE CreateDirectory*(context : Commands.Context); (* path ~ *)
  673. VAR path : Files.FileName; res : WORD;
  674. BEGIN
  675. IF context.arg.GetString(path) THEN
  676. Files.CreateDirectory(path, res);
  677. IF (res # Files.Ok) THEN
  678. context.out.String("Could not create directory '"); context.out.String(path); context.out.String("', res: ");
  679. ShowRes(context.out, res); context.out.Ln;
  680. context.result := Commands.CommandError;
  681. END;
  682. ELSE
  683. context.error.String("Usage: FSTools.CreateDirectory <path> ~"); context.error.Ln;
  684. context.result := Commands.CommandParseError;
  685. END;
  686. END CreateDirectory;
  687. PROCEDURE DeleteDirectory*(context : Commands.Context); (* path ~ *)
  688. VAR path : Files.FileName; res : WORD;
  689. BEGIN
  690. IF context.arg.GetString(path) THEN
  691. Files.RemoveDirectory(path, FALSE, res);
  692. IF (res # Files.Ok) THEN
  693. context.out.String("Could not delete directory '"); context.out.String(path); context.out.String("', res: ");
  694. ShowRes(context.out, res); context.out.Ln;
  695. context.result := Commands.CommandError;
  696. END;
  697. ELSE
  698. context.error.String("Usage: FSTools.DeleteDirectory <path> ~"); context.error.Ln;
  699. context.result := Commands.CommandParseError;
  700. END;
  701. END DeleteDirectory;
  702. (** Compare filenames of two directories and display files that are not present in both directories *)
  703. PROCEDURE CompareDirectories*(context : Commands.Context); (** directory1 directory2 ~ *)
  704. VAR
  705. fileList1, fileList2 : FileList;
  706. length1, length2 : LONGINT;
  707. dirname1, dirname2 : Files.FileName;
  708. index1, index2 : LONGINT;
  709. differences : LONGINT;
  710. PROCEDURE GetSortedFileList(CONST dirname : ARRAY OF CHAR; VAR index : LONGINT) : FileList;
  711. VAR mask : Files.FileName; fileList : FileList;
  712. BEGIN
  713. COPY(dirname, mask);
  714. Strings.Append(mask, Files.PathDelimiter);
  715. Strings.Append(mask, "*");
  716. NEW(fileList, 128);
  717. InsertFiles(mask, fileList, index);
  718. IF (index > 0) THEN SortFileList(fileList, index); END;
  719. ASSERT(fileList # NIL);
  720. RETURN fileList;
  721. END GetSortedFileList;
  722. PROCEDURE CompareEntries(CONST entry1, entry2 : ARRAY OF CHAR) : LONGINT;
  723. VAR result : LONGINT; prefix : Files.Prefix; filename1, filename2, pathname, path : Files.FileName;
  724. BEGIN
  725. Files.SplitName(entry1, prefix, pathname);
  726. Files.SplitPath(pathname, path, filename1);
  727. Files.SplitName(entry2, prefix, pathname);
  728. Files.SplitPath(pathname, path, filename2);
  729. IF (filename1 < filename2) THEN result := -1;
  730. ELSIF (filename1 > filename2) THEN result := 1;
  731. ELSE result := 0;
  732. END;
  733. RETURN result;
  734. END CompareEntries;
  735. BEGIN
  736. context.arg.SkipWhitespace; context.arg.String(dirname1);
  737. context.arg.SkipWhitespace; context.arg.String(dirname2);
  738. differences := 0;
  739. length1 := 0;
  740. fileList1 := GetSortedFileList(dirname1, length1);
  741. length2 := 0;
  742. fileList2 := GetSortedFileList(dirname2, length2);
  743. context.out.String(dirname1); context.out.String(": "); context.out.Int(length1, 0); context.out.String(" entries"); context.out.Ln;
  744. context.out.String(dirname2); context.out.String(": "); context.out.Int(length2, 0); context.out.String(" entries"); context.out.Ln;
  745. index1 := 0; index2 := 0;
  746. WHILE (index1 < length1) DO
  747. WHILE (index2 < length2) & (CompareEntries(fileList1[index1]^, fileList2[index2]^) > 0) DO
  748. context.out.String(fileList2[index2]^); context.out.Ln;
  749. INC(differences);
  750. INC(index2);
  751. END;
  752. IF (index2 < length2) & (CompareEntries(fileList1[index1]^, fileList2[index2]^) = 0)THEN
  753. INC(index2);
  754. ELSE
  755. INC(differences);
  756. context.out.String(fileList1[index1]^); context.out.Ln;
  757. END;
  758. INC(index1);
  759. END;
  760. WHILE (index2 < length2) DO
  761. context.out.String(fileList2[index2]^); context.out.Ln;
  762. INC(differences);
  763. INC(index2);
  764. END;
  765. IF (differences = 0) THEN
  766. context.out.String("Directories contain the same entries"); context.out.Ln;
  767. END;
  768. END CompareDirectories;
  769. (** Compare two files by byte-wise comparison of contents *)
  770. PROCEDURE CompareFiles*(context : Commands.Context); (* filename1 filename2 ~ *)
  771. VAR filename : Files.FileName; file1, file2 : Files.File; reader1, reader2 : Files.Reader; ch1, ch2 : CHAR;
  772. BEGIN
  773. context.arg.SkipWhitespace; context.arg.String(filename);
  774. file1 := Files.Old(filename);
  775. IF (file1# NIL) THEN
  776. context.arg.SkipWhitespace; context.arg.String(filename);
  777. file2 := Files.Old(filename);
  778. IF (file2 # NIL) THEN
  779. IF (file1.Length() = file2.Length()) THEN
  780. NEW(reader1, file1, 0);
  781. NEW(reader2, file2, 0);
  782. REPEAT
  783. reader1.Char(ch1);
  784. reader2.Char(ch2);
  785. UNTIL (ch1 # ch2) OR (reader1.res # Files.Ok) OR (reader2.res # Files.Ok);
  786. IF (ch1 = ch2) & (reader1.res = reader2.res) & (reader1.res = Streams.EOF) THEN
  787. context.out.String("Files are equal"); context.out.Ln;
  788. ELSE
  789. context.out.String("Content mismatch"); context.out.Ln;
  790. END;
  791. ELSE
  792. context.out.String("Length mismatch"); context.out.Ln;
  793. END;
  794. ELSE
  795. context.error.String("File "); context.error.String(filename); context.error.String(" not found");
  796. context.error.Ln; context.result := Commands.CommandError;
  797. END;
  798. ELSE
  799. context.error.String("File "); context.error.String(filename); context.error.String(" not found");
  800. context.error.Ln; context.result := Commands.CommandParseError;
  801. END;
  802. END CompareFiles;
  803. PROCEDURE SortFileList(filelist : FileList; length : LONGINT );
  804. VAR i, j : LONGINT; temp : Strings.String;
  805. BEGIN
  806. (* bubble sort *)
  807. FOR i := 0 TO length-1 DO
  808. FOR j := 0 TO length-2 DO
  809. IF filelist[j]^ > filelist[j+1]^ THEN
  810. temp := filelist[j+1];
  811. filelist[j+1] := filelist[j];
  812. filelist[j] := temp;
  813. END;
  814. END;
  815. END;
  816. END SortFileList;
  817. PROCEDURE ResizeFilelist(VAR filelist : FileList);
  818. VAR temp : FileList; i : LONGINT;
  819. BEGIN
  820. NEW(temp, 2 * LEN(filelist));
  821. FOR i := 0 TO LEN(filelist)-1 DO
  822. temp[i] := filelist[i];
  823. END;
  824. filelist := temp;
  825. END ResizeFilelist;
  826. (* Checks whether a file list entry contains mask characters and adds the corresponding files if it does *)
  827. PROCEDURE InsertFiles(CONST mask : ARRAY OF CHAR; VAR filelist : FileList; VAR index : LONGINT);
  828. VAR
  829. enum : Files.Enumerator;
  830. fileflags : SET;
  831. time, date, size : LONGINT;
  832. name : ARRAY MaxNameLen OF CHAR;
  833. BEGIN
  834. NEW(enum); enum.Open(mask, {});
  835. WHILE enum.GetEntry(name, fileflags, time, date, size) DO
  836. IF (fileflags * {Files.Directory} = {}) THEN
  837. IF index >= LEN(filelist) THEN ResizeFilelist(filelist); END;
  838. filelist[index] := Strings.NewString(name);
  839. INC(index);
  840. END;
  841. END;
  842. enum.Close;
  843. END InsertFiles;
  844. (* Count the number of occurences of the character 'ch' in the string 'string'. Case-Sensitive! *)
  845. PROCEDURE CountCharacters(CONST string : ARRAY OF CHAR; ch : CHAR) : LONGINT;
  846. VAR count, i : LONGINT;
  847. BEGIN
  848. count := 0;
  849. FOR i := 0 TO LEN(string)-1 DO
  850. IF string[i] = ch THEN INC(count); END;
  851. END;
  852. RETURN count;
  853. END CountCharacters;
  854. (* Split full name into prefix, path, filename and file extension *)
  855. PROCEDURE SplitFullName(CONST fullname : ARRAY OF CHAR; VAR prefix, path, filename, extension : ARRAY OF CHAR);
  856. VAR pathname, name : ARRAY 1024 OF CHAR;
  857. BEGIN
  858. Files.SplitName(fullname, prefix, pathname);
  859. Files.SplitPath(pathname, path, name);
  860. Files.SplitExtension(name, filename, extension);
  861. END SplitFullName;
  862. PROCEDURE IsValidTargetMask(context : Commands.Context; CONST mask : ARRAY OF CHAR) : BOOLEAN;
  863. VAR
  864. prefix : ARRAY Files.PrefixLength OF CHAR;
  865. filename, extension : ARRAY Files.NameLength OF CHAR;
  866. path : ARRAY 512 OF CHAR;
  867. BEGIN
  868. SplitFullName(mask, prefix, path, filename, extension);
  869. IF (CountCharacters(mask, "?") > 0) THEN
  870. context.error.String("Syntax Error in "); context.error.String(mask); context.error.String(": '?' matching characters not implemented for target mask"); context.error.Ln;
  871. context.result := Commands.CommandError;
  872. RETURN FALSE;
  873. END;
  874. IF (CountCharacters(prefix, "*") # 0) OR (CountCharacters(path, "*") # 0) THEN
  875. context.error.String("Syntax Error in "); context.error.String(mask); context.error.String(": Target prefix/path may not contain '*' characters"); context.error.Ln;
  876. context.result := Commands.CommandError;
  877. RETURN FALSE;
  878. END;
  879. RETURN TRUE;
  880. END IsValidTargetMask;
  881. (* If the user does not specify a prefix or path for a mask, the mask will include all directories and subdirectories.
  882. Since this is too dangerous for file operations as delete, we only allow pattern operations if a prefix
  883. or path is specified within the pattern or the unsafe mode is set *)
  884. PROCEDURE AllowMaskInSafeMode(CONST mask : ARRAY OF CHAR) : BOOLEAN;
  885. VAR prefix : Files.Prefix; pathname, path, filename : Files.FileName;
  886. BEGIN
  887. Files.SplitName(mask, prefix, pathname);
  888. Files.SplitPath(pathname, path, filename);
  889. RETURN (prefix # "") OR ((path # "") & (path # Files.PathDelimiter));
  890. END AllowMaskInSafeMode;
  891. PROCEDURE GetTargetName(CONST sourceMask, targetMask, sourceName : ARRAY OF CHAR) : String;
  892. VAR
  893. targetName : ARRAY 1024 OF CHAR;
  894. srcPrefix, srcPath, srcFilename, srcExtension : ARRAY 512 OF CHAR;
  895. isExtension : BOOLEAN;
  896. i, j, index : LONGINT;
  897. BEGIN
  898. SplitFullName(sourceName, srcPrefix, srcPath, srcFilename, srcExtension);
  899. index := 0;
  900. FOR i := 0 TO LEN(targetMask)-1 DO
  901. IF (targetMask[i] = ".") & (targetMask[i+1]#".") & (targetMask[i+1]#"/") THEN
  902. isExtension := TRUE;
  903. targetName[index] := targetMask[i];
  904. INC(index);
  905. ELSIF targetMask[i] = "*" THEN
  906. IF isExtension THEN
  907. j := 0; WHILE (j < LEN(srcExtension)) & (srcExtension[j] # 0X) DO targetName[index] := srcExtension[j]; INC(index); INC(j); END;
  908. ELSE
  909. j := 0; WHILE (j < LEN(srcFilename)) & (srcFilename[j] # 0X) DO targetName[index] := srcFilename[j]; INC(index); INC(j); END;
  910. END;
  911. ELSE
  912. targetName[index] := targetMask[i];
  913. INC(index);
  914. END;
  915. END;
  916. IF index < LEN(targetName) THEN targetName[index] := 0X; END;
  917. RETURN Strings.NewString(targetName);
  918. END GetTargetName;
  919. PROCEDURE InsertFilesAndFixDestination(context : Commands.Context; CONST sourceMask, targetMask : ARRAY OF CHAR; VAR source, target : FileList; VAR index : LONGINT) : BOOLEAN;
  920. VAR
  921. enum : Files.Enumerator;
  922. fileflags : SET;
  923. time, date, size : LONGINT;
  924. name : ARRAY MaxNameLen OF CHAR;
  925. BEGIN
  926. IF ~IsValidTargetMask(context, targetMask) THEN RETURN FALSE; END;
  927. NEW(enum); enum.Open(sourceMask, {});
  928. WHILE enum.GetEntry(name, fileflags, time, date, size) DO
  929. IF (fileflags * {Files.Directory} = {}) THEN
  930. IF index >= LEN(source) THEN ResizeFilelist(source); ResizeFilelist(target); END;
  931. source[index] := Strings.NewString(name);
  932. target[index] := GetTargetName(sourceMask, targetMask, name);
  933. INC(index);
  934. END;
  935. END;
  936. enum.Close;
  937. RETURN TRUE;
  938. END InsertFilesAndFixDestination;
  939. PROCEDURE IsMask(CONST string : ARRAY OF CHAR) : BOOLEAN;
  940. BEGIN
  941. RETURN Strings.ContainsChar(string, "*", FALSE) OR Strings.ContainsChar(string, "?", FALSE);
  942. END IsMask;
  943. PROCEDURE GetFileList(context : Commands.Context; VAR filelist : FileList) : LONGINT;
  944. VAR filename : ARRAY MaxNameLen OF CHAR; done, error : BOOLEAN; count : LONGINT;
  945. BEGIN
  946. NEW(filelist, InitialFilelistSize);
  947. WHILE ~done & ~error DO
  948. IF context.arg.GetString(filename) THEN
  949. IF IsMask(filename) THEN
  950. IF ~(AllowMaskInSafeMode(filename) OR unsafeMode) THEN
  951. ShowUnsafeMessage(context.out); RETURN 0;
  952. END;
  953. InsertFiles(filename, filelist, count);
  954. ELSE
  955. IF count >= LEN(filelist) THEN ResizeFilelist(filelist); END;
  956. filelist[count] := Strings.NewString(filename);
  957. INC(count);
  958. END;
  959. ELSIF context.arg.res = Streams.EOF THEN
  960. done := TRUE;
  961. ELSE
  962. context.error.String("Command parsing error (res: "); context.error.Int(context.arg.res, 0); context.error.String(")");
  963. error := TRUE; context.result := Commands.CommandError;
  964. END;
  965. END;
  966. IF error THEN count := Error; END;
  967. RETURN count;
  968. END GetFileList;
  969. PROCEDURE GetSimpleFileLists(context : Commands.Context; VAR source, target : FileList) : LONGINT;
  970. VAR sourceFilename, targetFilename : Files.FileName; count : LONGINT;
  971. BEGIN
  972. IF context.arg.GetString(sourceFilename) & context.arg.GetString(targetFilename) THEN
  973. count := 1;
  974. IF IsMask(sourceFilename) OR IsMask(targetFilename) THEN
  975. IF ~(AllowMaskInSafeMode(sourceFilename) OR unsafeMode) THEN ShowUnsafeMessage(context.out); RETURN 0; END;
  976. IF ~InsertFilesAndFixDestination(context, sourceFilename, targetFilename, source, target, count) THEN END;
  977. ELSE
  978. NEW(source, 1); NEW(target, 1);
  979. source[0] := Strings.NewString(sourceFilename);
  980. target[0] := Strings.NewString(targetFilename);
  981. END;
  982. ELSE
  983. count := Error;
  984. context.error.String("Expected two filenames as arguments"); context.error.Ln;
  985. context.result := Commands.CommandError;
  986. END;
  987. RETURN count;
  988. END GetSimpleFileLists;
  989. PROCEDURE GetFileLists(context : Commands.Context; VAR source, target : FileList) : LONGINT;
  990. VAR
  991. filename : ARRAY MaxNameLen OF CHAR; done, error : BOOLEAN; count : LONGINT;
  992. sourceString, targetString : String;
  993. BEGIN
  994. NEW(source, InitialFilelistSize); NEW(target, InitialFilelistSize);
  995. WHILE ~done & ~error DO
  996. IF context.arg.GetString(filename) THEN
  997. sourceString := Strings.NewString(filename);
  998. IF context.arg.GetString(filename) & Strings.Match(filename, "=>") THEN
  999. IF context.arg.GetString(filename) THEN
  1000. targetString := Strings.NewString(filename);
  1001. IF IsMask(sourceString^) OR IsMask(targetString^) THEN
  1002. IF ~(AllowMaskInSafeMode(sourceString^) OR unsafeMode) THEN ShowUnsafeMessage(context.out); RETURN 0; END;
  1003. IF ~InsertFilesAndFixDestination(context, sourceString^, targetString^, source, target, count) THEN END;
  1004. ELSE
  1005. IF count >= LEN(source) THEN ResizeFilelist(source); ResizeFilelist(target); END;
  1006. source[count] := sourceString;
  1007. target[count] := targetString;
  1008. INC(count);
  1009. END;
  1010. ELSE
  1011. context.error.String("Command parsing error (res: "); context.error.Int(context.arg.res, 0); context.error.String(")");
  1012. context.error.Ln;
  1013. error := TRUE;
  1014. END;
  1015. ELSE
  1016. context.error.String("Command parsing error: Exspected => token, found: "); context.error.String(filename);
  1017. context.error.Ln;
  1018. error := TRUE;
  1019. END;
  1020. ELSIF context.arg.res = Streams.EOF THEN
  1021. done := TRUE;
  1022. ELSE
  1023. context.error.String("Command parsing error (res: "); context.error.Int(context.arg.res, 0); context.error.String(")");
  1024. context.error.Ln;
  1025. error := TRUE;
  1026. END;
  1027. END;
  1028. IF error THEN count := Error; context.result := Commands.CommandError END;
  1029. RETURN count;
  1030. END GetFileLists;
  1031. PROCEDURE Safe*(context : Commands.Context);
  1032. BEGIN
  1033. unsafeMode := FALSE;
  1034. context.out.String("FSTools: SAFE mode."); context.out.Ln;
  1035. END Safe;
  1036. PROCEDURE Unsafe*(context : Commands.Context);
  1037. BEGIN
  1038. unsafeMode := TRUE;
  1039. context.out.String("FSTools: UNSAFE mode now. BE CAREFUL!"); context.out.Ln;
  1040. END Unsafe;
  1041. PROCEDURE ShowUnsafeMessage(out : Streams.Writer);
  1042. BEGIN
  1043. out.String("FSTools: Pattern matching is disabled in SAFE mode. Press FSTools.Unsafe ~ to enable pattern matching."); out.Ln;
  1044. END ShowUnsafeMessage;
  1045. PROCEDURE ShowRes(out : Streams.Writer; res : WORD);
  1046. BEGIN
  1047. out.String("(");
  1048. CASE res OF
  1049. Files.VolumeReadOnly: out.String("Target volume is read-only");
  1050. |Files.FsNotFound: out.String("File system not found");
  1051. |Files.FileAlreadyExists: out.String("File already exists");
  1052. |Files.BadFileName: out.String("Bad file name");
  1053. |Files.FileNotFound: out.String("File not found");
  1054. ELSE
  1055. out.String("res: "); out.Int(res, 0);
  1056. END;
  1057. out.String(")");
  1058. END ShowRes;
  1059. (** Close files -- paradox: open (old) file and call Close method. Intended for systems in a host environment to explicitely release a file handle. *)
  1060. PROCEDURE CloseFiles*(context : Commands.Context); (** [Options] {file} ~ *)
  1061. VAR
  1062. filelist : FileList;
  1063. nofFiles, res, n, ndone : LONGINT;
  1064. file: Files.File;
  1065. BEGIN
  1066. nofFiles := GetFileList(context, filelist);
  1067. n := 0; ndone := 0;
  1068. WHILE (n<nofFiles) & (filelist[n] # NIL) DO
  1069. file := Files.Old(filelist[n]^);
  1070. IF file # NIL THEN file.Close END;
  1071. INC(n);
  1072. END;
  1073. END CloseFiles;
  1074. (* returns if a file or directory exists. If yes, then fullname is set to filename *)
  1075. PROCEDURE Exists*(CONST name: ARRAY OF CHAR; VAR fullName: ARRAY OF CHAR; VAR flags: SET): BOOLEAN;
  1076. BEGIN
  1077. RETURN Files.Exists(name, fullName, flags);
  1078. END Exists;
  1079. END FSTools.
  1080. System.Free FSTools ~
  1081. FSTools.DeleteFiles X:*.Bak ~
  1082. FSTools.SplitFile BootManager.Bin 0200H BootManagerMBR.Bin BootManagerTail.Bin ~
  1083. FSTools.Directory Test.Mod ~