home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / watool10.zip / WAT.CMD next >
OS/2 REXX Batch file  |  1995-10-02  |  15KB  |  474 lines

  1. /* REXX  -  Workstation Automation Tools for OS/2 - WATools v1.0
  2.                Written by Russell Gosse, Team OS/2
  3.                   Email: rgosse@interlog.com
  4.  
  5.             See WAT.DOC for description and reference.
  6.             See RETCODE.TXT for return code descriptions.
  7. */
  8.  
  9. !version = '1.0 - 09/18/95'
  10.  
  11. if Arg() = 0 then do
  12.    say
  13.    say 'Workstation Automation Tools - WATools, '!version
  14.    say '   Written by:  Russell Gosse, Team OS/2'
  15.    say
  16.    say 'This is a function that is meant to be called by another REXX exec!'
  17.    say 'Please read WAT.TXT for details.  Thanks.'
  18.    say
  19.    return !no_function
  20. end
  21.  
  22. /*--- Main Routine -------------------------------------------------*/
  23.  
  24. !no_function         = '-1'
  25. !bad_parm            = '-2'
  26. !out_of_range        = '-3'
  27. !no_sessions         = '-4'
  28. !not_connected       = '-5'
  29. !unknown_function    = '-6'
  30. !session_in_use      = '-7'
  31. !unsupported         = '-8'
  32. !system_error        = '-9'
  33. !session_not_found   = '-10'
  34. !session_busy        = '-11'
  35. !send_error          = '-12'
  36. !bad_mnemonic        = '-13'
  37. !timeout             = '-14'
  38. !no_hllapi_loaded    = '-99'
  39.  
  40. parse arg !func , !p1 , !p2 , !p3 , !p4 , !p5 , !p6 , !p7 , !p8
  41.  
  42. !func = translate(!func)
  43. if !func \= 'INIT' then do
  44.    signal on syntax name _NoHllapi
  45.    if Hllapi('Query_Sessions') = '' then return !no_sessions
  46.    signal off syntax 
  47. end
  48. signal on halt name _Terminate
  49. signal on syntax name _Invalid_Func
  50. interpret 'retval = '!func||'()'             
  51. return retval
  52.  
  53. /*--- Functions Used Internally ----------------------------------------*/
  54.  
  55. _Terminate:
  56.    call reset
  57. return
  58.                                       
  59. _Invalid_Func: 
  60. return !unknown_function
  61.  
  62. _NoHllapi:
  63. return !no_hllapi_loaded
  64.  
  65. _No_Connection: 
  66.     !conn = ''
  67. return
  68.  
  69. _SendKey: Procedure
  70.    arg string
  71.    !rc = Hllapi('SendKey',string)
  72.    select
  73.       when !rc = 4 then !rc = !session_busy
  74.       when !rc = 5 then !rc = !send_error
  75.       when !rc = 6 then !rc = !bad_mnemonic
  76.       otherwise nop
  77.    end
  78. return !rc
  79.  
  80. _CheckPos: 
  81.    arg pos
  82.    !new_pos = ''
  83.    !rc = 0
  84.    if words(pos) = 2 then do
  85.       col = word(pos,1)
  86.       row = word(pos,2)
  87.       if datatype(row,'W') \= 1 then !rc = !bad_parm
  88.       if datatype(col,'W') \= 1 then !rc = !bad_parm
  89.       if row <= 0 | row > !rows then !rc = !out_of_range
  90.       if col <= 0 | col > !cols then !rc = !out_of_range
  91.       if !rc = 0 then !new_pos = Hllapi('Convert_Pos',!conn,col,row)
  92.    end
  93.    else do
  94.       if datatype(pos,'W') \= 1 then !rc = !bad_parm
  95.       if pos <= 0 | pos > !scrsize then !rc = !out_of_range
  96.       if !rc = 0 then !new_pos = pos
  97.    end
  98. return !rc
  99.  
  100. _SessInfo: Procedure Expose !conn !scrsize !rows !cols !sesstype
  101.    arg sessid
  102.    if sessid = '*' then sessid = ' '
  103.    !conn = ''
  104.    !scrsize = ''
  105.    !rows = ''; !cols = ''
  106.    signal on syntax name _No_Connection
  107.    info = Hllapi('Query_Session_Status',sessid)
  108.    signal off syntax
  109.    !conn = substr(info,1,1)
  110.    !sesstype = substr(info,10,1)
  111.    select
  112.       when !sesstype = 'D' then !sesstype = 'H3'
  113.       when !sesstype = 'E' then !sesstype = 'P3'
  114.       when !sesstype = 'F' then !sesstype = 'H5'
  115.       when !sesstype = 'G' then !sesstype = 'P5'
  116.       otherwise nop
  117.    end
  118.    parse var info 12 bin_row 14 bin_col 16
  119.    !rows = c2d(reverse(bin_row))
  120.    !cols = c2d(reverse(bin_col))
  121.    !scrsize = !rows * !cols
  122. return
  123.  
  124. /*--- Functions Callable By User ---------------------------------------*/
  125.  
  126. Init:                   /* Initializes CM/2 REXX EHLLAPI functions */
  127.    !rc = 0
  128.    if RxFuncQuery('HLLAPI') then       /* See if HLLAPI is registered */
  129.       !rc = RxFuncAdd('HLLAPI','SAAHLAPI','HLLAPISRV')
  130.    if !rc = 0 then                     /* check if CM/2 sessions active */
  131.       if Hllapi('Query_Sessions') = '' then !rc = !no_sessions
  132. return !rc
  133.  
  134. Conn:                   /* Starts API connection to CM/2 */
  135.    sessid = !p1
  136.    title = 'Emulator - 'sessid' - WAT: Connected'
  137.    call Hllapi 'Disconnect'
  138.    !rc = Hllapi('Connect',sessid)
  139.    select
  140.       when !rc = 0 then do
  141.          call Hllapi 'Connect_PM',sessid
  142.          call Hllapi 'Change_Switch_Name',sessid,'S',title
  143.          call Hllapi 'Change_Window_Name',sessid,'S',title
  144.          call Hllapi 'Disconnect_PM',sessid
  145.       end
  146.       when !rc = 1 then !rc = !not_connected
  147.       when !rc = 9 then !rc = !system_error
  148.       when !rc = 10 then !rc = !unsupported
  149.       when !rc = 11 then !rc = !session_in_use
  150.       otherwise
  151.          nop
  152.    end
  153. return !rc
  154.  
  155. Disc:                   /* Disconnects API connection to CM/2 session id. */
  156.    !rc = 0
  157.    call _SessInfo '*'
  158.    if !conn \= '' then do
  159.       call Hllapi 'Connect_PM',!conn
  160.       call Hllapi 'Change_Switch_Name',!conn,'R'
  161.       call Hllapi 'Change_Window_Name',!conn,'R'
  162.       call Hllapi 'Disconnect'
  163.       call Hllapi 'Disconnect_PM',!conn
  164.    end
  165. return !rc
  166.  
  167. Reset:                  /* Resets EHLLAPI environment */
  168.    !rc = Hllapi('Reset_System')
  169.    if !rc = 9 then !rc = !system_error
  170. return !rc
  171.  
  172. Info:                   /* Returns information about specified session id. */
  173.    sessid = !p1
  174.    !rc = ''
  175.    if sessid = '' then do
  176.       call _SessInfo '*'
  177.       if !conn = '' then !rc = !not_connected
  178.    end
  179.    else do
  180.       call _SessInfo sessid
  181.       if !conn = '' then !rc = !session_not_found
  182.    end
  183.    if !rc = '' then do
  184.       !rc = !sesstype !rows !cols
  185.    end
  186. return !rc
  187.  
  188. List:                   /* Lists active CM/2 session ids. */
  189.    type = !p1
  190.    p = 0; h = 0; a = 0
  191.    !allsess = ''; !printer = ''; !host = ''
  192.    !sess_info = Hllapi('Query_Sessions')  /* check if CM/2 sessions active */
  193.    sessions = length(!sess_info) / 12
  194.    if sessions \= '' then do
  195.       do i = 1 to sessions
  196.          sessid = substr(!sess_info,(i*12)-11,1)
  197.          if c2d(reverse(substr(!sess_info,(i*12)-1,2))) = 0 then do
  198.             p = p + 1
  199.             !printer = !printer sessid
  200.          end
  201.          else do
  202.             h = h + 1
  203.             !host = !host sessid
  204.          end
  205.          a = a + 1
  206.          !allsess = !allsess sessid
  207.       end
  208.       !allsess = space(a !allsess)
  209.       !host = space(h !host)
  210.       !printer = space(p !printer)
  211.    end
  212.    if type = 'P' then !rc = !printer
  213.    else
  214.       if type = 'H' then !rc = !host
  215.       else
  216.          !rc = !allsess
  217. return !rc          
  218.  
  219. PFKey:                  /* Sends PFkey to connected CM/2 session */
  220.    key = !p1
  221.    !rc = 0
  222.    if key = '' | key < 1 | key > 24 then !rc = !bad_parm
  223.    else do
  224.       call _SessInfo '*'
  225.       if !conn = '' then !rc = !not_connected
  226.       else do
  227.          if key >= 10 & key <= 15 then
  228.             key = translate(d2x(key),'abcdef','ABCDEF')
  229.          else 
  230.             if key > 15 then
  231.                select
  232.                   when key = 16 then key = 'g'
  233.                   when key = 17 then key = 'h'
  234.                   when key = 18 then key = 'i'
  235.                   when key = 19 then key = 'j'
  236.                   when key = 20 then key = 'k'
  237.                   when key = 21 then key = 'l'
  238.                   when key = 22 then key = 'm'
  239.                   when key = 23 then key = 'n'
  240.                   when key = 24 then key = 'o'
  241.                   otherwise
  242.                      nop
  243.                end
  244.           string = '@'||key
  245.       end
  246.    end
  247.    if !rc = 0 then !rc = _SendKey(string)
  248. return !rc
  249.  
  250. Send:                   /* Writes a string of text */
  251.    string = !p1
  252.    quicksend = translate(!p1)
  253.    call _SessInfo '*'
  254.    if !conn = '' then !rc = !not_connected
  255.    else do
  256.       !rc = 0
  257.       quick_list = 'CLEAR ENTER TABF TABB EOF HOME END PA1 PA2 PRINTPS'
  258.       if pos(quicksend,quick_list) > 0 then do
  259.          select
  260.             when quicksend = 'CLEAR' then string = '@C'
  261.             when quicksend = 'ENTER' then string = '@E'
  262.             when quicksend = 'TABF' then string = '@T'
  263.             when quicksend = 'TABB' then string = '@B'
  264.             when quicksend = 'EOF' then string = '@F'
  265.             when quicksend = 'HOME' then string = '@0'
  266.             when quicksend = 'END' then string = '@q'
  267.             when quicksend = 'PA1' then string = '@x'
  268.             when quicksend = 'PA2' then string = '@y'
  269.             when quicksend = 'PRINTPS' then string = '@A@T'
  270.             otherwise nop
  271.          end
  272.       end
  273.       if !rc = 0 then !rc = _SendKey(string)
  274.    end
  275. return !rc
  276.  
  277. CursorPos:              /* Retrieves or sets cursor postion */
  278.    pos = !p1
  279.    call _SessInfo '*'
  280.    if !conn = '' then !rc = !not_connected
  281.    else do
  282.       if pos = '' then do
  283.          !rc = Hllapi('Query_Cursor_Pos')
  284.       end
  285.       else do 
  286.          !rc = _CheckPos(pos)
  287.          if !rc = 0 then do
  288.             !rc = Hllapi('Set_Cursor_Pos',!new_pos)
  289.             if !rc = 7 then !rc = !out_of_range
  290.             else
  291.                if !rc = 1 then !rc = !not_connected
  292.       end
  293.    end
  294. return !rc
  295.  
  296. Read:                   /* Reads string of text from CM/2 session */
  297.    pos = !p1; len = !p2
  298.    call _SessInfo '*'
  299.    if !conn = '' then !rc = !not_connected
  300.    else do
  301.       call Hllapi 'Set_Session_Parms','NOEAB NOATTRB NOEAD NOXLATE'
  302.       if pos = '' & len = '' then
  303.          !rc = Hllapi('Copy_PS')
  304.       else do
  305.       if pos \= '' then !rc = _CheckPos(pos)
  306.       else
  307.          !new_pos = Hllapi('Query_Cursor_Pos')
  308.       if !rc = 0 then do
  309.          if len = '' then len = !scrsize - !new_pos
  310.          if !new_pos + len > !scrsize then !rc = !out_of_range
  311.          else do
  312.             !rc = Hllapi('Copy_PS_To_Str',!new_pos,len)
  313.             if !rc = 9 then !rc = !system_error
  314.             else
  315.                if !rc = 7 then !rc = !out_of_range
  316.          end
  317.       end
  318.    end
  319. return !rc
  320.  
  321. ReadWord:               /* Reads a word from the specified row */
  322.    row = !p1; word = !p2
  323.    call _SessInfo '*'
  324.    if !conn = '' then !rc = !not_connected
  325.    else do
  326.       if row = '' | word = '' then !rc = !bad_parm
  327.       else do
  328.          call Hllapi 'Set_Session_Parms','NOEAB NOATTRB NOEAD NOXLATE'
  329.          if row < 1 | row > !rows then !rc = !out_of_range
  330.          else do
  331.             pos = Hllapi('Convert_Pos',!conn,1,row)
  332.             if pos = 0 then !rc = !out_of_range
  333.             else do
  334.                row_text = Hllapi('Copy_PS_To_Str',pos,80)
  335.                if words(row_text) < word then !rc = !bad_parm
  336.                else
  337.                   !rc = word(row_text,word)
  338.             end
  339.          end
  340.       end
  341.    end
  342. return !rc
  343.  
  344. ReadRow:                /* Reads entire row from connect session */
  345.    row = !p1
  346.    call _SessInfo '*'
  347.    if !conn = '' then !rc = !not_connected
  348.    else do
  349.       if row = '' then !rc = !bad_parm
  350.       else do
  351.          call Hllapi 'Set_Session_Parms','NOEAB NOATTRB NOEAD NOXLATE'
  352.          if row < 1 | row > !rows then !rc = !out_of_range
  353.          else do
  354.             pos = Hllapi('Convert_Pos',!conn,1,row)
  355.             if pos = 0 then !rc = !out_of_range
  356.             else do
  357.                !rc = Hllapi('Copy_PS_To_Str',pos,80)
  358.             end
  359.          end
  360.       end
  361.    end
  362. return !rc
  363.  
  364. Find:                   /* Finds string of text */
  365.    text = !p1; pos = !p2; direction = translate(!p3)
  366.    call _SessInfo '*'
  367.    if !conn = '' then !rc = !not_connected
  368.    else do
  369.       if text = '' then !rc = !bad_parm
  370.       else
  371.          if direction \= '' & direction \= 'B' & direction \= 'F' then
  372.             !rc = !bad_parm
  373.          else do
  374.             if pos = '' then do
  375.                !rc = 0
  376.                !new_pos = 1
  377.                direction = 'SRCHALL'
  378.             end
  379.             else do
  380.                if direction = 'B' then direction = 'SRCHFROM SRCHBKWD'
  381.                else
  382.                   direction = 'SRCHFROM SRCHFRWD'
  383.                !rc = _CheckPos(pos)
  384.             end
  385.             if !rc = 0 then do
  386.                call Hllapi 'Set_Session_Parms',direction
  387.                !rc = Hllapi('Search_PS',text,!new_pos)
  388.                call Hllapi 'Set_Session_Parms','SRCHALL SRCHFRWD'
  389.             end
  390.          end     
  391.    end
  392. return !rc
  393.  
  394. WaitFor:                /* Waits for text string to appear */
  395.    text = !p1; timeout = !p2;
  396.    default_timeout = 30
  397.    call _SessInfo '*'
  398.    if !conn = '' then !rc = !not_connected
  399.    else
  400.       if text = ''then !rc = !bad_parm
  401.       else
  402.          if timeout \= '' & datatype(timeout,'W') \= 1 then !rc = !bad_parm
  403.          else do
  404.             !rc = 0
  405.             if timeout = '' | timeout = 0 then timeout = default_timeout
  406.             rc = Time('R')
  407.             elapsed = 0
  408.             do until elapsed > timeout
  409.                if Hllapi('Wait') \= 0 then do
  410.                   !rc = !timeout
  411.                   leave
  412.                end
  413.             if Hllapi('Search_PS',text,1) \= 0 then leave
  414.             elapsed = time('E')
  415.             end
  416.          end
  417.          if elapsed >= timeout | !rc = !timeout then 
  418.             !rc = !timeout
  419. return !rc
  420.  
  421. Wait:                   /* Waits for host to unlock session */
  422.    timeout = !p1
  423.    !rc = 0
  424.    default_timeout = 30
  425.    call _SessInfo '*'
  426.    if !conn = '' then !rc = !not_connected
  427.    else do
  428.       if timeout = '' then timeout = default_timeout
  429.       if timeout \= '' & (datatype(timeout,'W') \= 1 | timeout <= 0) then 
  430.          !rc = !bad_parm
  431.       else do
  432.          call Hllapi 'Set_Session_Parms','IPAUSE'
  433.          call Hllapi 'Start_Host_Notify',!conn,'P'
  434.          if Hllapi('Wait') \= 0 then !rc = !timeout
  435.          else do
  436.             rc = Time('R')      
  437.             do until ((pause_value = 0) | (elapsed_time > timeout))
  438.                pause_value = Hllapi('Pause',4,!conn||'#')  
  439.                call Hllapi 'Query_Host_Update',!conn
  440.                elapsed_time = Time('E')
  441.             end
  442.             call Hllapi 'Stop_Host_Notify',!conn
  443.             call Hllapi 'Set_Session_Parms','FPAUSE'
  444.             if elapsed_time > timeout then
  445.                !rc = !timeout
  446.          end
  447.       end
  448.    end
  449. return !rc
  450.  
  451. ConvertPos:             /* Converts cursor postion value */
  452.    pos = !p1
  453.    !rc = 0
  454.    call _SessInfo '*'
  455.    if !conn = '' then !rc = !not_connected
  456.    else do
  457.       if words(pos) = 2 then do
  458.          col = word(pos,1)
  459.          row = word(pos,2)
  460.          if datatype(row,'W') \= 1 then !rc = !bad_parm
  461.          if datatype(col,'W') \= 1 then !rc = !bad_parm
  462.          if row <= 0 | row > !rows then !rc = !out_of_range
  463.          if col <= 0 | col > !cols then !rc = !out_of_range
  464.          if !rc = 0 then !rc = Hllapi('Convert_Pos',!conn,col,row)
  465.       end
  466.       else do
  467.          if datatype(pos,'W') \= 1 then !rc = !bad_parm
  468.          if pos <= 0 | pos > !scrsize then !rc = !out_of_range
  469.          if !rc = 0 then !rc = Hllapi('Convert_Pos',!conn,pos)
  470.       end
  471. return !rc
  472.  
  473. /* End of WATools Functions */
  474.