rexx.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. {
  2. This file is part of the Free Pascal run time library.
  3. A file in Amiga system run time library.
  4. Copyright (c) 1998-2003 by Nils Sjoholm
  5. member of the Amiga RTL development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. History:
  14. Added overlay functions for PAnsiChar->Strings, functions
  15. and procedures.
  16. 14 Jul 2000.
  17. Removed amigaoverlays, use smartlink instead.
  18. 05 Nov 2002.
  19. Added the defines use_amiga_smartlink and
  20. use_auto_openlib. Implemented autoopening of
  21. the library.
  22. 14 Jan 2003.
  23. Changed integer > smallint,
  24. cardinal > longword.
  25. 09 Feb 2003.
  26. [email protected]
  27. }
  28. {$PACKRECORDS 2}
  29. {$IFNDEF FPC_DOTTEDUNITS}
  30. UNIT rexx;
  31. {$ENDIF FPC_DOTTEDUNITS}
  32. INTERFACE
  33. {$IFDEF FPC_DOTTEDUNITS}
  34. USES Amiga.Core.Exec;
  35. {$ELSE FPC_DOTTEDUNITS}
  36. USES exec;
  37. {$ENDIF FPC_DOTTEDUNITS}
  38. { === rexx/storage.h ==================================================
  39. *
  40. * Copyright (c) 1986, 1987 by William S. Hawes (All Rights Reserved)
  41. *
  42. * =====================================================================
  43. * Header file to define ARexx data structures.
  44. }
  45. { The NexxStr structure is used to maintain the internal strings in REXX.
  46. * It includes the buffer area for the string and associated attributes.
  47. * This is actually a variable-length structure; it is allocated for a
  48. * specific length string, and the length is never modified thereafter
  49. * (since it's used for recycling).
  50. }
  51. Type
  52. pNexxStr = ^tNexxStr;
  53. tNexxStr = record
  54. ns_Ivalue : Longint; { integer value }
  55. ns_Length : Word; { length in bytes (excl null) }
  56. ns_Flags : Byte; { attribute flags }
  57. ns_Hash : Byte; { hash code }
  58. ns_Buff : Array [0..7] of Byte;
  59. { buffer area for strings }
  60. end; { size: 16 bytes (minimum) }
  61. Const
  62. NXADDLEN = 9; { offset plus null byte }
  63. { String attribute flag bit definitions }
  64. NSB_KEEP = 0; { permanent string? }
  65. NSB_STRING = 1; { string form valid? }
  66. NSB_NOTNUM = 2; { non-numeric? }
  67. NSB_NUMBER = 3; { a valid number? }
  68. NSB_BINARY = 4; { integer value saved? }
  69. NSB_FLOAT = 5; { floating point format? }
  70. NSB_EXT = 6; { an external string? }
  71. NSB_SOURCE = 7; { part of the program source? }
  72. { The flag form of the string attributes }
  73. NSF_KEEP = 1;
  74. NSF_STRING = 2;
  75. NSF_NOTNUM = 4;
  76. NSF_NUMBER = 8;
  77. NSF_BINARY = 16;
  78. NSF_FLOAT = 32;
  79. NSF_EXT = 64;
  80. NSF_SOURCE = 128;
  81. { Combinations of flags }
  82. NSF_INTNUM = NSF_NUMBER + NSF_BINARY + NSF_STRING;
  83. NSF_DPNUM = NSF_NUMBER + NSF_FLOAT;
  84. NSF_ALPHA = NSF_NOTNUM + NSF_STRING;
  85. NSF_OWNED = NSF_SOURCE + NSF_EXT + NSF_KEEP;
  86. KEEPSTR = NSF_STRING + NSF_SOURCE + NSF_NOTNUM;
  87. KEEPNUM = NSF_STRING + NSF_SOURCE + NSF_NUMBER + NSF_BINARY;
  88. { The RexxArg structure is identical to the NexxStr structure, but
  89. * is allocated from system memory rather than from internal storage.
  90. * This structure is used for passing arguments to external programs.
  91. * It is usually passed as an "argstring", a pointer to the string buffer.
  92. }
  93. Type
  94. pRexxArg = ^tRexxArg;
  95. tRexxArg = record
  96. ra_Size : Longint; { total allocated length }
  97. ra_Length : Word; { length of string }
  98. ra_Flags : Byte; { attribute flags }
  99. ra_Hash : Byte; { hash code }
  100. ra_Buff : Array [0..7] of Byte;
  101. { buffer area }
  102. end; { size: 16 bytes (minimum) }
  103. { The RexxMsg structure is used for all communications with REXX
  104. * programs. It is an EXEC message with a parameter block appended.
  105. }
  106. pRexxMsg = ^tRexxMsg;
  107. tRexxMsg = record
  108. rm_Node : tMessage; { EXEC message structure }
  109. rm_TaskBlock : Pointer; { global structure (private) }
  110. rm_LibBase : Pointer; { library base (private) }
  111. rm_Action : Longint; { command (action) code }
  112. rm_Result1 : Longint; { primary result (return code) }
  113. rm_Result2 : Longint; { secondary result }
  114. rm_Args : Array [0..15] of STRPTR;
  115. { argument block (ARG0-ARG15) }
  116. rm_PassPort : pMsgPort; { forwarding port }
  117. rm_CommAddr : STRPTR; { host address (port name) }
  118. rm_FileExt : STRPTR; { file extension }
  119. rm_Stdin : Longint; { input stream (filehandle) }
  120. rm_Stdout : Longint; { output stream (filehandle) }
  121. rm_avail : Longint; { future expansion }
  122. end; { size: 128 bytes }
  123. Const
  124. MAXRMARG = 15; { maximum arguments }
  125. { Command (action) codes for message packets }
  126. RXCOMM = $01000000; { a command-level invocation }
  127. RXFUNC = $02000000; { a function call }
  128. RXCLOSE = $03000000; { close the REXX server }
  129. RXQUERY = $04000000; { query for information }
  130. RXADDFH = $07000000; { add a function host }
  131. RXADDLIB = $08000000; { add a function library }
  132. RXREMLIB = $09000000; { remove a function library }
  133. RXADDCON = $0A000000; { add/update a ClipList string }
  134. RXREMCON = $0B000000; { remove a ClipList string }
  135. RXTCOPN = $0C000000; { open the trace console }
  136. RXTCCLS = $0D000000; { close the trace console }
  137. { Command modifier flag bits }
  138. RXFB_NOIO = 16; { suppress I/O inheritance? }
  139. RXFB_RESULT = 17; { result string expected? }
  140. RXFB_STRING = 18; { program is a "string file"? }
  141. RXFB_TOKEN = 19; { tokenize the command line? }
  142. RXFB_NONRET = 20; { a "no-return" message? }
  143. { The flag form of the command modifiers }
  144. RXFF_NOIO = $00010000;
  145. RXFF_RESULT = $00020000;
  146. RXFF_STRING = $00040000;
  147. RXFF_TOKEN = $00080000;
  148. RXFF_NONRET = $00100000;
  149. RXCODEMASK = $FF000000;
  150. RXARGMASK = $0000000F;
  151. { The RexxRsrc structure is used to manage global resources. Each node
  152. * has a name string created as a RexxArg structure, and the total size
  153. * of the node is saved in the "rr_Size" field. The REXX systems library
  154. * provides functions to allocate and release resource nodes. If special
  155. * deletion operations are required, an offset and base can be provided in
  156. * "rr_Func" and "rr_Base", respectively. This "autodelete" function will
  157. * be called with the base in register A6 and the node in A0.
  158. }
  159. Type
  160. pRexxRsrc = ^tRexxRsrc;
  161. tRexxRsrc = record
  162. rr_Node : tNode;
  163. rr_Func : smallint; { "auto-delete" offset }
  164. rr_Base : Pointer; { "auto-delete" base }
  165. rr_Size : Longint; { total size of node }
  166. rr_Arg1 : Longint; { available ... }
  167. rr_Arg2 : Longint; { available ... }
  168. end; { size: 32 bytes }
  169. Const
  170. { Resource node types }
  171. RRT_ANY = 0; { any node type ... }
  172. RRT_LIB = 1; { a function library }
  173. RRT_PORT = 2; { a public port }
  174. RRT_FILE = 3; { a file IoBuff }
  175. RRT_HOST = 4; { a function host }
  176. RRT_CLIP = 5; { a Clip List node }
  177. { The RexxTask structure holds the fields used by REXX to communicate with
  178. * external processes, including the client task. It includes the global
  179. * data structure (and the base environment). The structure is passed to
  180. * the newly-created task in its "wake-up" message.
  181. }
  182. GLOBALSZ = 200; { total size of GlobalData }
  183. Type
  184. pRexxTask = ^tRexxTask;
  185. tRexxTask = record
  186. rt_Global : Array [0..GLOBALSZ-1] of Byte;
  187. { global data structure }
  188. rt_MsgPort : tMsgPort; { global message port }
  189. rt_Flags : Byte; { task flag bits }
  190. rt_SigBit : Shortint; { signal bit }
  191. rt_ClientID : Pointer; { the client's task ID }
  192. rt_MsgPkt : Pointer; { the packet being processed }
  193. rt_TaskID : Pointer; { our task ID }
  194. rt_RexxPort : Pointer; { the REXX public port }
  195. rt_ErrTrap : Pointer; { Error trap address }
  196. rt_StackPtr : Pointer; { stack pointer for traps }
  197. rt_Header1 : tList; { Environment list }
  198. rt_Header2 : tList; { Memory freelist }
  199. rt_Header3 : tList; { Memory allocation list }
  200. rt_Header4 : tList; { Files list }
  201. rt_Header5 : tList; { Message Ports List }
  202. end;
  203. Const
  204. { Definitions for RexxTask flag bits }
  205. RTFB_TRACE = 0; { external trace flag }
  206. RTFB_HALT = 1; { external halt flag }
  207. RTFB_SUSP = 2; { suspend task? }
  208. RTFB_TCUSE = 3; { trace console in use? }
  209. RTFB_WAIT = 6; { waiting for reply? }
  210. RTFB_CLOSE = 7; { task completed? }
  211. { Definitions for memory allocation constants }
  212. MEMQUANT = 16; { quantum of memory space }
  213. MEMMASK = $FFFFFFF0; { mask for rounding the size }
  214. MEMQUICK = 1; { EXEC flags: MEMF_PUBLIC }
  215. MEMCLEAR = $00010000; { EXEC flags: MEMF_CLEAR }
  216. { The SrcNode is a temporary structure used to hold values destined for
  217. * a segment array. It is also used to maintain the memory freelist.
  218. }
  219. Type
  220. pSrcNode = ^tSrcNode;
  221. tSrcNode = record
  222. sn_Succ : pSrcNode; { next node }
  223. sn_Pred : pSrcNode; { previous node }
  224. sn_Ptr : Pointer; { pointer value }
  225. sn_Size : Longint; { size of object }
  226. end; { size: 16 bytes }
  227. { === rexx/rexxio.h ====================================================
  228. *
  229. * Copyright (c) 1986, 1987 by William S. Hawes. All Rights Reserved.
  230. *
  231. * ======================================================================
  232. * Header file for ARexx Input/Output related structures
  233. }
  234. Const
  235. RXBUFFSZ = 204; { buffer length }
  236. {
  237. * The IoBuff is a resource node used to maintain the File List. Nodes
  238. * are allocated and linked into the list whenever a file is opened.
  239. }
  240. Type
  241. pIoBuff = ^tIoBuff;
  242. tIoBuff = record
  243. iobNode : tRexxRsrc; { structure for files/strings }
  244. iobRpt : Pointer; { read/write pointer }
  245. iobRct : Longint; { character count }
  246. iobDFH : Longint; { DOS filehandle }
  247. iobLock : Longint; { DOS lock }
  248. iobBct : Longint; { buffer length }
  249. iobArea : Array [0..RXBUFFSZ-1] of Byte;
  250. { buffer area }
  251. end; { size: 256 bytes }
  252. Const
  253. { Access mode definitions }
  254. RXIO_EXIST = -1; { an external filehandle }
  255. RXIO_STRF = 0; { a "string file" }
  256. RXIO_READ = 1; { read-only access }
  257. RXIO_WRITE = 2; { write mode }
  258. RXIO_APPEND = 3; { append mode (existing file) }
  259. {
  260. * Offset anchors for SeekF()
  261. }
  262. RXIO_BEGIN = -1; { relative to start }
  263. RXIO_CURR = 0; { relative to current position }
  264. RXIO_END = 1; { relative to end }
  265. {
  266. * A message port structure, maintained as a resource node. The ReplyList
  267. * holds packets that have been received but haven't been replied.
  268. }
  269. Type
  270. pRexxMsgPort = ^tRexxMsgPort;
  271. tRexxMsgPort = record
  272. rmp_Node : tRexxRsrc; { linkage node }
  273. rmp_Port : tMsgPort; { the message port }
  274. rmp_ReplyList : tList; { messages awaiting reply }
  275. end;
  276. Const
  277. {
  278. * DOS Device types
  279. }
  280. DT_DEV = 0; { a device }
  281. DT_DIR = 1; { an ASSIGNed directory }
  282. DT_VOL = 2; { a volume }
  283. {
  284. * Private DOS packet types
  285. }
  286. ACTION_STACK = 2002; { stack a line }
  287. ACTION_QUEUE = 2003; { queue a line }
  288. { === rexx/rxslib.h ===================================================
  289. *
  290. * Copyright (c) 1986, 1987, 1989 by William S. Hawes (All Rights Reserved)
  291. *
  292. * =====================================================================
  293. * The header file for the REXX Systems Library
  294. }
  295. { Some macro definitions }
  296. Const
  297. RXSNAME : PAnsiChar = 'rexxsyslib.library';
  298. RXSID : PAnsiChar = 'rexxsyslib 1.06 (07 MAR 88)';
  299. RXSDIR : PAnsiChar = 'REXX';
  300. RXSTNAME : PAnsiChar = 'ARexx';
  301. { The REXX systems library structure. This should be considered as }
  302. { semi-private and read-only, except for documented exceptions. }
  303. Type
  304. pRxsLib = ^tRxsLib;
  305. tRxsLib = record
  306. rl_Node : tLibrary; { EXEC library node }
  307. rl_Flags : Byte; { global flags }
  308. rl_pad : Byte;
  309. rl_SysBase : Pointer; { EXEC library base }
  310. rl_DOSBase : Pointer; { DOS library base }
  311. rl_IeeeDPBase : Pointer; { IEEE DP math library base }
  312. rl_SegList : Longint; { library seglist }
  313. rl_NIL : Longint; { global NIL: filehandle }
  314. rl_Chunk : Longint; { allocation quantum }
  315. rl_MaxNest : Longint; { maximum expression nesting }
  316. rl_NULL : pNexxStr; { static string: NULL }
  317. rl_FALSE : pNexxStr; { static string: FALSE }
  318. rl_TRUE : pNexxStr; { static string: TRUE }
  319. rl_REXX : pNexxStr; { static string: REXX }
  320. rl_COMMAND : pNexxStr; { static string: COMMAND }
  321. rl_STDIN : pNexxStr; { static string: STDIN }
  322. rl_STDOUT : pNexxStr; { static string: STDOUT }
  323. rl_STDERR : pNexxStr; { static string: STDERR }
  324. rl_Version : STRPTR; { version/configuration string }
  325. rl_TaskName : STRPTR; { name string for tasks }
  326. rl_TaskPri : Longint; { starting priority }
  327. rl_TaskSeg : Longint; { startup seglist }
  328. rl_StackSize : Longint; { stack size }
  329. rl_RexxDir : STRPTR; { REXX directory }
  330. rl_CTABLE : STRPTR; { character attribute table }
  331. rl_Notice : STRPTR; { copyright notice }
  332. rl_RexxPort : tMsgPort; { REXX public port }
  333. rl_ReadLock : Word; { lock count }
  334. rl_TraceFH : Longint; { global trace console }
  335. rl_TaskList : tList; { REXX task list }
  336. rl_NumTask : smallint; { task count }
  337. rl_LibList : tList; { Library List header }
  338. rl_NumLib : smallint; { library count }
  339. rl_ClipList : tList; { ClipList header }
  340. rl_NumClip : smallint; { clip node count }
  341. rl_MsgList : tList; { pending messages }
  342. rl_NumMsg : smallint; { pending count }
  343. rl_PgmList : tList; { cached programs }
  344. rl_NumPgm : smallint; { program count }
  345. rl_TraceCnt : Word; { usage count for trace console }
  346. rl_avail : smallint;
  347. end;
  348. Const
  349. { Global flag bit definitions for RexxMaster }
  350. RLFB_TRACE = RTFB_TRACE; { interactive tracing? }
  351. RLFB_HALT = RTFB_HALT; { halt execution? }
  352. RLFB_SUSP = RTFB_SUSP; { suspend execution? }
  353. RLFB_STOP = 6; { deny further invocations }
  354. RLFB_CLOSE = 7; { close the master }
  355. RLFMASK = 1 + 2 + 4;
  356. { Initialization constants }
  357. RXSVERS = 34; { main version }
  358. RXSREV = 7; { revision }
  359. RXSALLOC = $800000; { maximum allocation }
  360. RXSCHUNK = 1024; { allocation quantum }
  361. RXSNEST = 32; { expression nesting limit }
  362. RXSTPRI = 0; { task priority }
  363. RXSSTACK = 4096; { stack size }
  364. RXSLISTH = 5; { number of list headers }
  365. { Character attribute flag bits used in REXX. }
  366. CTB_SPACE = 0; { white space characters }
  367. CTB_DIGIT = 1; { decimal digits 0-9 }
  368. CTB_ALPHA = 2; { alphabetic characters }
  369. CTB_REXXSYM = 3; { REXX symbol characters }
  370. CTB_REXXOPR = 4; { REXX operator characters }
  371. CTB_REXXSPC = 5; { REXX special symbols }
  372. CTB_UPPER = 6; { UPPERCASE alphabetic }
  373. CTB_LOWER = 7; { lowercase alphabetic }
  374. { Attribute flags }
  375. CTF_SPACE = 1;
  376. CTF_DIGIT = 2;
  377. CTF_ALPHA = 4;
  378. CTF_REXXSYM = 8;
  379. CTF_REXXOPR = 16;
  380. CTF_REXXSPC = 32;
  381. CTF_UPPER = 64;
  382. CTF_LOWER = 128;
  383. VAR RexxSysBase : pLibrary = nil;
  384. const
  385. REXXSYSLIBNAME : PAnsiChar = 'rexxsyslib.library';
  386. PROCEDURE ClearRexxMsg(msgptr : pRexxMsg location 'a0'; count : ULONG location 'd0'); syscall RexxSysBase 156;
  387. FUNCTION CreateArgstring(const argstring : PAnsiChar location 'a0'; length : ULONG location 'd0') : PAnsiChar; syscall RexxSysBase 126;
  388. FUNCTION CreateRexxMsg(const port : pMsgPort location 'a0'; const extension : PAnsiChar location 'a1'; host : PAnsiChar location 'd0') : pRexxMsg; syscall RexxSysBase 144;
  389. PROCEDURE DeleteArgstring(argstring : PAnsiChar location 'a0'); syscall RexxSysBase 132;
  390. PROCEDURE DeleteRexxMsg(packet : pRexxMsg location 'a0'); syscall RexxSysBase 150;
  391. FUNCTION FillRexxMsg(msgptr : pRexxMsg location 'a0'; count : ULONG location 'd0'; mask : ULONG location 'd1') : LongBool; syscall RexxSysBase 162;
  392. FUNCTION IsRexxMsg(const msgptr : pRexxMsg location 'a0') : LongBool; syscall RexxSysBase 168;
  393. FUNCTION LengthArgstring(const argstring : PAnsiChar location 'a0') : ULONG; syscall RexxSysBase 138;
  394. PROCEDURE LockRexxBase(resource : ULONG location 'd0'); syscall RexxSysBase 450;
  395. PROCEDURE UnlockRexxBase(resource : ULONG location 'd0'); syscall RexxSysBase 456;
  396. FUNCTION CreateArgstring(const argstring : ShortString; length : ULONG) : PAnsiChar;
  397. IMPLEMENTATION
  398. FUNCTION CreateArgstring(const argstring : ShortString; length : ULONG) : PAnsiChar;
  399. begin
  400. CreateArgstring := CreateArgstring(PAnsiChar(RawByteString(argstring)),length);
  401. end;
  402. const
  403. { Change VERSION and LIBVERSION to proper values }
  404. VERSION : string[2] = '0';
  405. LIBVERSION : longword = 0;
  406. initialization
  407. RexxSysBase := OpenLibrary(REXXSYSLIBNAME,LIBVERSION);
  408. finalization
  409. if Assigned(RexxSysBase) then
  410. CloseLibrary(RexxSysBase);
  411. END. (* UNIT REXXSYSLIB *)