summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/rexx/examples/callrexx.pas
blob: 73fdaa6f59f7acda78bb533c381b28d941b35979 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Uses
{$IFDEF OS2}
  DosCalls,
{$ENDIF OS2}
  RexxSAA;

Var
  Arg: RxString;                       // argument string for REXX
  RexxRetVal: RxString;                // return value from REXX
  RC: Cardinal;                        // return code from REXX
Const
  Str: PChar = 'These words will be swapped'; // text to swap
  RexxRc: Integer = 0;                // return code from function
Begin
  Write('This program will call the REXX interpreter ');
  WriteLn('to reverse the order of the');
  Write(#9'words in a string.  ');
  WriteLn('The interpreter is invoked with an initial');
  Write(#9'environment name of "FNC" ');
  WriteLn('and a file name of "backward.fnc"');

  // By setting the strlength of the output RXSTRING to zero, we
  // force the interpreter to allocate memory and return it to us.
  // We could provide a buffer for the interpreter to use instead.

  RexxRetVal.StrLength:=0;          // initialize return to empty

  MakeRxString(Arg, Str, StrLen(Str)); // create input argument

  // Here we call the interpreter.  We don't really need to use
  // all the casts in this call; they just help illustrate
  // the data types used.
  RC:=RexxStart(1,                // number of arguments
                addr(arg),        // array of arguments
                'backward.fnc',   // name of REXX file
                0,                // No INSTORE used
                'FNC',            // Command env. name
                RXSUBROUTINE,     // Code for how invoked
                0,                // No EXITs on this call
                rexxrc,           // Rexx program output
                rexxretval );     // Rexx program output

  WriteLn('Interpreter Return Code: ', RC);
  WriteLn('Function Return Code:    ', rexxrc);
  WriteLn('Original String:         ', arg.strptr);
  WriteLn('Backwards String:        ', StrPas(rexxretval.strptr));

{$IFDEF OS2}
  DosFreeMem(RexxRetVal.StrPtr);   // Release storage
{$ELSE OS2}
  {$WARNING Memory allocated for RexxRetVal.StrPtr^ should be freed using native API!}
{$ENDIF OS2}
End.