BUILDARG.DEF 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. (* ========================================================= *)
  2. (* Preliminary library module for Gardens Point Modula *)
  3. (* ========================================================= *)
  4. (* !LIBRARY! *) DEFINITION MODULE BuildArgs;
  5. TYPE ArgPtr; (* ==> "pointer to array of strings" in UNIX *)
  6. (* The first set of procedures are simple & efficient and *)
  7. (* have several restrictions to their use. However, they *)
  8. (* suffice for most purposes, when used as shown below. *)
  9. PROCEDURE Arg1(a1 : ARRAY OF CHAR) : ArgPtr;
  10. PROCEDURE Arg2(a1, a2 : ARRAY OF CHAR) : ArgPtr;
  11. PROCEDURE Arg3(a1, a2, a3 : ARRAY OF CHAR) : ArgPtr;
  12. PROCEDURE Arg4(a1, a2, a3, a4 : ARRAY OF CHAR) : ArgPtr;
  13. (* preconditions : a's may be literals or variable arrays. *)
  14. (* These procedures safely copy array parameters into a *)
  15. (* dynamically allocated block, adding NUL termination if *)
  16. (* necessary. Actual param variables may thus be reused. *)
  17. (* usage example:
  18. ...
  19. FROM BuildArgs IMPORT Arg3;
  20. FROM UxProcesses IMPORT Execp; (* execs from $PATH *)
  21. VAR cmd, fNm : ARRAY [0 .. 39] OF CHAR;
  22. ...
  23. Execp("foo", Arg3("foo", cmd, fNm));
  24. Error("Can't exec 'foo'");
  25. ...
  26. *)
  27. (* The next set of procedures allow argument blocks of any *)
  28. (* size to be built, and allow for explicit reclaiming of *)
  29. (* memory space from used blocks, where that is necessary. *)
  30. (* These procedures safely copy array parameters into a *)
  31. (* dynamically allocated block, adding NUL termination if *)
  32. (* necessary. Actual param variables may thus be reused. *)
  33. TYPE ArgBlock; (* args + builder state information *)
  34. PROCEDURE NewArgBlock(VAR b : ArgBlock; max : CARDINAL);
  35. (* postcondition : buffer space for max args is allocated
  36. and the block state is initialized *)
  37. PROCEDURE DisposeArgBlock(VAR b : ArgBlock);
  38. (* postcondition : buffer space is reclaimed, b is NIL *)
  39. PROCEDURE AppendArg(b : ArgBlock;
  40. a : ARRAY OF CHAR);
  41. (* precondition : b has been initialized by NewArgBlock;
  42. is not fully occupied;
  43. postcondition : the block designated by b is updated so
  44. that a is its final argument. On block
  45. overflow an index error is raised *)
  46. PROCEDURE ArgsOf(b : ArgBlock) : ArgPtr;
  47. (* extracts the args from the valid ArgBlock buffers *)
  48. (* usage example:
  49. ...
  50. WHILE condition DO
  51. NewArgBlock(blk,64);
  52. WHILE xxx DO (* build block *)
  53. ....
  54. AppendArg(blk,str);
  55. END;
  56. Foo(ArgsOf(blk)); (* use block *)
  57. DisposeArgBlock(blk); (* reclaim space *)
  58. END;
  59. ...
  60. *)
  61. END BuildArgs.