home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / clpsrv.zip / getclip.xed < prev    next >
Text File  |  1993-08-24  |  6KB  |  224 lines

  1. /*------------------------------------------------------------------
  2.  * getclip.xedit : macro to read OS/2 clipboard data from the
  3.  *                 ClipServ server
  4.  *------------------------------------------------------------------
  5.  * 08-13-92 originally by Patrick J. Mueller
  6.  * 08-24-93 updated to use REXX/SOCKETS 2.01
  7.  *------------------------------------------------------------------*/
  8.  
  9. parse source . . me .
  10.  
  11. trace off
  12.  
  13. /*------------------------------------------------------------------
  14.  * get parms
  15.  *------------------------------------------------------------------*/
  16. parse arg host port
  17.  
  18. if (host <> "") & (port = "") then
  19.    Usage()
  20.  
  21. else if (host <> "") | (port <> "") then
  22.    address command "GLOBALV SELECT CLIPSERV PUTP HOST PORT"
  23.  
  24. else
  25.    do
  26.    address command "GLOBALV SELECT CLIPSERV GET HOST PORT"
  27.    if (host = "") | (port = "") then
  28.       do
  29.       "EMSG" me": GLOBALV storage not correct, specify full parameters"
  30.       Usage()
  31.       end
  32.    end
  33.  
  34. /*------------------------------------------------------------------
  35.  * make sure the cursor is in the file
  36.  *------------------------------------------------------------------*/
  37. "COMMAND EXTRACT /CURSOR/"
  38. yCursor = cursor.3
  39. xCursor = cursor.4
  40.  
  41. if (xCursor = -1) | (yCursor = -1) then
  42.    do
  43.    "COMMAND EMSG" me ": Cursor not in file"
  44.    exit
  45.    end
  46.  
  47. /*------------------------------------------------------------------
  48.  * initialize socket function package
  49.  *------------------------------------------------------------------*/
  50. sockInfo = socket("INITIALIZE","GETCLIP")
  51. parse var sockInfo src .
  52. if (src <> 0) then
  53.    do
  54.    parse var sockInfo src token desc
  55.    msg1 = "Error" src "initializing REXX/SOCKETS function package."
  56.    msg2 = token ":" desc
  57.    rc = Error(-1,src,msg1,msg2)
  58.    end
  59.  
  60. /*------------------------------------------------------------------
  61.  * open socket
  62.  *------------------------------------------------------------------*/
  63. sockInfo = socket("SOCKET","AF_INET","SOCK_STREAM","IPPROTO_TCP")
  64. parse var sockInfo src sock .
  65. if (src <> 0) then
  66.    do
  67.    parse var sockInfo src token desc
  68.    msg1 = "Error" src "obtaining new socket."
  69.    msg2 = token ":" desc
  70.    rc = Error(-1,src,msg1,msg2)
  71.    end
  72.  
  73. signal on halt
  74. signal on syntax
  75.  
  76. /*------------------------------------------------------------------
  77.  * connect socket
  78.  *------------------------------------------------------------------*/
  79. sockInfo = socket("CONNECT",sock,"AF_INET" port host)
  80. parse var sockInfo src .
  81. if (src <> 0) then
  82.    do
  83.    parse var sockInfo src token desc
  84.    msg1 = "Error" src "connecting to ClipServ server."
  85.    msg2 = token ":" desc
  86.    rc = Error(sock,src,msg1,msg2)
  87.    end
  88.  
  89. /*------------------------------------------------------------------
  90.  * get the clipboard data
  91.  *------------------------------------------------------------------*/
  92. rc = socket("SETSOCKOPT",sock,"SOL_SOCKET","SO_ASCII","ON")
  93.  
  94. data = ""
  95. do forever
  96.    chunkInfo = socket("RECV",sock,1000)
  97.    parse var chunkInfo src length chunk
  98.    if (length = 0) | (src <> 0) then
  99.       leave
  100.  
  101.    data = data || chunk
  102. end
  103.  
  104. /*------------------------------------------------------------------
  105.  * close the socket
  106.  *------------------------------------------------------------------*/
  107. rc = socket("CLOSE",sock)
  108. rc = socket("TERMINATE","GETCLIP")
  109.  
  110. /*------------------------------------------------------------------
  111.  * now reformat the data
  112.  *------------------------------------------------------------------*/
  113. crlf        = d2c(13) || d2c(37)
  114. clipLine.0  = 0
  115. lineMax     = 0
  116.  
  117. do while (data <> "")
  118.    p = pos(crlf,data)
  119.  
  120.    if (p = 0) then
  121.       do
  122.       line = data
  123.       data = ""
  124.       end
  125.  
  126.    else
  127.       do
  128.       line = substr(data,1,p-1)
  129.       data = substr(data,p+2)
  130.       end
  131.  
  132.    if (line = "") & (data = "") then
  133.       leave
  134.  
  135.    o = clipLine.0 + 1
  136.    clipLine.0 = o
  137.    clipLine.o = line
  138.  
  139.    lineMax = max(lineMax,length(line))
  140. end
  141.  
  142. /*------------------------------------------------------------------
  143.  * blank fill lines on the right
  144.  *------------------------------------------------------------------*/
  145. do i = 1 to clipLine.0
  146.    clipLine.i = left(clipLine.i,lineMax)
  147. end
  148.  
  149. /*------------------------------------------------------------------
  150.  * insert lines
  151.  *------------------------------------------------------------------*/
  152. "COMMAND EXTRACT /CURSOR/LINE/"
  153. yCursor = cursor.3
  154. xCursor = cursor.4
  155. curLine = line.1
  156.  
  157. if (xCursor = -1) | (yCursor = -1) then
  158.    do
  159.    "COMMAND EMSG" me ": Cursor not in file"
  160.    exit
  161.    end
  162.  
  163. "COMMAND :"yCursor
  164.  
  165. do i = 1 to clipLine.0
  166.    "COMMAND EXTRACT /CURLINE/"
  167.  
  168.    oldLine = curline.3
  169.    newLine = overlay(clipLine.i,oldLine,xCursor)
  170.    "COMMAND REPLACE" newLine
  171.  
  172.    "COMMAND 1"
  173. end
  174.  
  175. "COMMAND :"curLine
  176. "COMMAND CURSOR FILE" yCursor xCursor
  177.  
  178. exit
  179.  
  180. /*------------------------------------------------------------------
  181.  * exit with a message and return code
  182.  *------------------------------------------------------------------*/
  183. Error:           procedure expose !.
  184.    parse source . . me .
  185.  
  186.    sock = arg(1)
  187.    retc = arg(2)
  188.  
  189.    if (sock <> -1) then
  190.       rc = socket("CLOSE",sock)
  191.  
  192.    rc = socket("TERMINATE","GETCLIP")
  193.  
  194.    do i = 3 to arg()
  195.       "EMSG" me ":" arg(i)
  196.    end
  197.  
  198.    exit retc
  199.  
  200. /*------------------------------------------------------------------
  201.  *  what to do when halted
  202.  *------------------------------------------------------------------*/
  203. Halt:
  204.    parse source . . me .
  205.    Error(sock,1,me "halted on line" sigl ".")
  206.  
  207. /*------------------------------------------------------------------
  208.  *  what to do when halted
  209.  *------------------------------------------------------------------*/
  210. Syntax:
  211.    parse source . . me .
  212.    Error(sock,1,me "has syntax error on line" sigl ".")
  213.  
  214. Usage:
  215.    "EMSG" me
  216.    "EMSG usage:"
  217.    "EMSG   " me
  218.    "EMSG   " me "serverAddress port"
  219.    "EMSG If an address and port are not specified, the values last used"
  220.    "EMSG will be used (stored in LASTING GLOBALV).  The first time you"
  221.    "EMSG run" me "you need to pass in the address and port"
  222.  
  223.    exit
  224.