home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / OS2 / CMDLINE1.ZIP / CMDLINE.CMD < prev    next >
OS/2 REXX Batch file  |  1994-02-09  |  14KB  |  426 lines

  1. /* BEGINNING OF CmdLine CODE BY ALBERT CROSBY */
  2. /*
  3.        CmdLine.CMD Version 1.0
  4.        (c) 1994 by Albert Crosby <acrosby@comp.uark.edu>
  5.  
  6.        This code may be distributed freely and used in other programs.
  7.        Please give credit where credit is due.
  8.  
  9.        CmdLine.CMD is REXX code that creates a full featured version
  10.        of the OS/2 command line parser that may be called from your
  11.        programs.
  12. */
  13.  
  14. /* This is a CmdLine function for REXX.  It supports:
  15.        *       OS/2 style command history. (1)
  16.        *       Keeps insert state. (1)
  17.        *       Command line _can_ include control chars.
  18.        *       Allows for "hidden" input, for passwords.
  19.        *       A call can be restricted from accessing the history.
  20.        *       A call can be restricted from updating the history.
  21.        *       A predefined value can be given to extended keys. (1) (2)
  22.  
  23.    NOTE:
  24.        (1) These functions work ONLY if CmdLine is included in the source
  25.            file for your program. 
  26.        (2) Format: !history.nn="string" where nn is the DECIMAL value for
  27.            the second character returned when the extended key is pressed.
  28. */
  29.  
  30. /* The following two lines are used in case CmdLine is called as an 
  31.    external function */
  32.  
  33. parse source . . name
  34. if translate(filespec("name",name))="CMDLINE.CMD" then signal extproc
  35.  
  36. CmdLine: procedure expose !history.
  37. extproc: /* CmdLine called as an external proc or command line */
  38.  
  39. /* Parameters can be any combination of:
  40.    Hidden : Characters are displayed as "*", no history, not kept.
  41.    Forget : Do not add the result of this call to the history list.
  42.    No History : Do not allow access to the history list.
  43.    Clear : Clear the history list with this call (no input action made.)
  44.            Also clears any predefined keys!
  45.    Insert : Set insert mode ON.
  46.    Overwrite : Set overwrite mode OFF.
  47.    SameLine : Keep cursor on sameline after input. (Default: off)
  48.    Required : null values are not accepted. (Default: off)7
  49.    Valid : Next parameter specifies the valid charachters (no translation)
  50.            unless specified elsewhere. (1)
  51.    Upper : Translate input to upper case. (1)
  52.    Lower : Translate input to lower case. (1)
  53.    Width : Next parameter specifies the maximum width. (1)
  54.    Autoskip : Do not wait for enter after last char on a field with a width.
  55.    X : Next parameter specifies the initial X (column) position.
  56.    Y : Next parameter specifies the initial Y (row) position.
  57.    Prompt : Displays the next parameter as a prompt in front of the
  58.             entry field.
  59.    
  60.  
  61.    Only the first letter matters.  Enter each desired parameter seperated
  62.    by commas.
  63.  
  64.    NOTES:
  65.       (1)  Upper, Lower, Width, and VALID preclude access to the history 
  66.            list.
  67. */
  68.  
  69. hidden=0
  70. history=1
  71. keep=1
  72. sameline=0
  73. required=0
  74. reset=0
  75. valid=xrange()
  76. upper=0
  77. lower=0
  78. width=0
  79. autoskip=0
  80. parse value SysCurPos() with x y
  81. do i=1 to arg()
  82.    cmd=translate(left(arg(i),1))
  83.    parm=""
  84.    if pos("=",arg(i))\=0 then
  85.       parse value arg(i) with ."="parm
  86.    select
  87.       when cmd="X" then
  88.          do
  89.          parse value SysCurPos() with x y
  90.          if parm="" then
  91.             do;i=i+1;parm=arg(i);end
  92.          if datatype(parm,"W") then
  93.             Call SysCurPos parm,y
  94.          end
  95.       when cmd="Y" then
  96.          do
  97.          parse value SysCurPos() with x y
  98.          if parm="" then
  99.             do;i=i+1;parm=arg(i);end
  100.          if datatype(parm,"W") then
  101.             Call SysCurPos x,parm
  102.          end
  103.       when cmd="T" then
  104.          do
  105.          if parm="" then
  106.             do;i=i+1;parm=arg(i);end
  107.          call charout, parm
  108.          end
  109.       when cmd="H" then
  110.          do
  111.          hidden=1
  112.          keep=0
  113.          history=0
  114.          end
  115.       when cmd="C" then
  116.          reset=1
  117.       when cmd="O" then
  118.          !history.insert=0
  119.       when cmd="I" then
  120.          !history.insert=1
  121.       when cmd="F" then
  122.          keep=0
  123.       when cmd="S" then
  124.          sameline=1
  125.       when cmd="R" then
  126.          required=1
  127.       when cmd="V" then
  128.          do
  129.          if parm="" then
  130.             do;i=i+1;parm=arg(i);end
  131.          valid=parm
  132.          history=0
  133.          keep=0
  134.          end
  135.       when cmd="U" then
  136.          do; upper=1; lower=0; history=0; keep=0; end
  137.       when cmd="L" then
  138.          do; upper=0; lower=1; history=0; keep=0; end
  139.       when cmd="A" then
  140.          autoskip=1
  141.       when cmd="W" then
  142.          do
  143.          if parm="" then
  144.             do;i=i+1;parm=arg(i);end
  145.          width=parm
  146.          if \datatype(width,"Whole") then width=0
  147.          if width<0 then width=0
  148.          history=0
  149.          keep=0
  150.          end
  151.     otherwise nop
  152.     end
  153. end
  154.  
  155. if width=0 then autoskip=0
  156.  
  157. if reset then
  158.    do
  159.    drop !history.
  160.    return ""
  161.    end
  162.  
  163. if symbol("!history.0")="LIT" then
  164.    !history.0=0
  165. if symbol("!history.insert")="LIT" then
  166.    !history.insert=1
  167.  
  168. historical=-1
  169. key=SysGetKey("NoEcho")
  170. word=""
  171. pos=0
  172. do forever /* while key\=d2c(13)*/
  173.    if key=d2c(13) then /* Enter key */
  174.       if required & word="" then nop;
  175.       else leave
  176.    else if (key=d2c(8)) then /* Backspace */
  177.       do
  178.       if length(word)>0 then
  179.       do
  180.       word=delstr(word,pos,1)
  181.       call rubout 1
  182.       pos=pos-1
  183.       if pos<length(word) then
  184.          do
  185.          if \hidden then call charout, substr(word,pos+1)||" "
  186.          else call charout, copies("*",length(substr(word,pos+1)))||" "
  187.          call charout, copies(d2c(8),length(word)-pos+1)
  188.          end
  189.       end
  190.       end
  191.    else if key=d2c(27) then /* Escape */
  192.       do
  193.       if pos<length(word) then
  194.          if \hidden then call charout, substr(word,pos+1)
  195.          else call charout, copies("*",length(substr(word,pos+1)))
  196.       call rubout length(word)
  197.       word=""
  198.       pos=0
  199.       end
  200.    else if key=d2c(10) | key=d2c(9) then /* Ctrl-Enter and TAB */
  201.       nop; /* Ignored */
  202.    else if key=d2c(224) | key=d2c(0) then /* Extended key handler */
  203.       do
  204.       key2=SysGetKey("NoEcho")
  205.       select
  206.          when key2=d2c(59) then /* F1 */
  207.             if (history) & (!history.0<>0) then
  208.                do
  209.                if symbol('search')='LIT' then
  210.                   search=word
  211.                if symbol('LastFind')='LIT' then
  212.                   search=word
  213.                else if LastFind\=word
  214.                   then search=word
  215.                if historical=-1 then
  216.                   start=!history.0
  217.                else start=historical-1
  218.                if start=0 then start=!history.0
  219.                found=0
  220.                do i=start to 1 by -1
  221.                   if abbrev(!history.i,search) then
  222.                      do
  223.                      found=1
  224.                      historical=i
  225.                      LastFind=!history.i
  226.                      leave
  227.                      end
  228.                end
  229.                if found then
  230.                   do
  231.                   if pos<length(word) then
  232.                      if \hidden then call charout, substr(word,pos+1)
  233.                      else call charout, copies("*",length(substr(word,pos+1)))
  234.                   call rubout length(word)
  235.                   word=!history.historical
  236.                   pos=length(word)
  237.                   if \hidden then call charout, word
  238.                   else call charout, copies("*",length(word))
  239.                   end
  240.                end
  241.          when key2=d2c(72) then /* Up arrow */
  242.             if (history) & (!history.0<>0) then
  243.                do
  244.                if historical=-1 then
  245.                   historical=!history.0
  246.                else historical=historical-1
  247.                if historical=0 then
  248.                   historical=!history.0
  249.                if pos<length(word) then
  250.                   if \hidden then call charout, substr(word,pos+1)
  251.                   else call charout, copies("*",length(substr(word,pos+1)))
  252.                call rubout length(word)
  253.                word=!history.historical
  254.                pos=length(word)
  255.                if \hidden then call charout, word
  256.                else call charout, copies("*",length(word))
  257.                end
  258.          when key2=d2c(80) then /* Down arrow */
  259.             if (history) & (!history.0<>0) then
  260.                do
  261.                if historical=-1 then
  262.                   historical=1
  263.                else historical=historical+1
  264.                if historical>!history.0 then
  265.                   historical=1
  266.                if pos<length(word) then
  267.                   if \hidden then call charout, substr(word,pos+1)
  268.                   else call charout, copies("*",length(substr(word,pos+1)))
  269.                call rubout length(word)
  270.                word=!history.historical
  271.                pos=length(word)
  272.                if \hidden then call charout, word
  273.                else call charout, copies("*",length(word))
  274.                end
  275.          when key2=d2c(75) then /* Left arrow */
  276.             if pos>0 then
  277.                do
  278.                call Charout, d2c(8)
  279.                pos=pos-1
  280.                end
  281.          when key2=d2c(77) then /* Right arrow */
  282.             if pos<length(word) then
  283.                do
  284.                if \hidden then call Charout, substr(word,pos+1,1)
  285.                else call charout, "*"
  286.                pos=pos+1
  287.                end
  288.          when key2=d2c(115) then /* Ctrl-Left arrow */
  289.             if pos>0 then
  290.                do
  291.                call charout, d2c(8)
  292.                pos=pos-1
  293.                do forever
  294.                   if pos=0 then leave
  295.                   if substr(word,pos+1,1)\==" " & substr(word,pos,1)==" " then
  296.                         leave
  297.                   else
  298.                      do
  299.                      call charout, d2c(8)
  300.                      pos=pos-1
  301.                      end
  302.                end
  303.                end
  304.          when key2=d2c(116) then /* Ctrl-Right arrow */
  305.             if pos<length(word) then
  306.                do
  307.                if \hidden then call Charout, substr(word,pos+1,1)
  308.                else call charout, "*"
  309.                pos=pos+1
  310.                do forever
  311.                   if pos=length(word) then
  312.                      leave
  313.                   if substr(word,pos,1)==" " & substr(word,pos+1,1)\==" " then
  314.                      leave
  315.                   else
  316.                      do
  317.                      if \hidden then call Charout, substr(word,pos+1,1)
  318.                      else call charout, "*"
  319.                      pos=pos+1
  320.                      end
  321.                end
  322.                end
  323.          when key2=d2c(83) then /* Delete key */
  324.             if pos<length(word) then
  325.                do
  326.                word=delstr(word,pos+1,1)
  327.                if \hidden then call Charout, substr(word,pos+1)||" "
  328.                else call Charout, copies("*",length(substr(word,pos+1)))||" "
  329.                call charout, copies(d2c(8),length(word)-pos+1)
  330.                end
  331.          when key2=d2c(82) then /* Insert key */
  332.             !history.insert=\!history.insert
  333.          when key2=d2c(79) then /* End key */
  334.             if pos<length(word) then
  335.                do
  336.                if \hidden then call Charout, substr(word,pos+1)
  337.                else call Charout, copies("*",length(substr(word,pos+1)))
  338.                pos=length(word)
  339.                end
  340.          when key2=d2c(71) then /* Home key */
  341.             if pos\=0 then
  342.                do
  343.                call Charout, copies(d2c(8),pos)
  344.                pos=0
  345.                end
  346.          when key2=d2c(117) then /* Control-End key */
  347.             if pos<length(word) then
  348.                do
  349.                call Charout, copies(" ",length(word)-pos)
  350.                call Charout, copies(d2c(8),length(word)-pos)
  351.                word=left(word,pos)
  352.                end
  353.          when key2=d2c(119) then /* Control-Home key */
  354.             if pos>0 then
  355.                do
  356.                if pos<length(word) then
  357.                   if \hidden then call charout, substr(word,pos+1)
  358.                   else call charout, copies("*",length(substr(word,pos+1)))
  359.                call rubout length(word)
  360.                word=substr(word,pos+1)
  361.                if \hidden then call Charout, word
  362.                else call Charout, copies("*",length(word))
  363.                call Charout, copies(d2c(8),length(word))
  364.                pos=0
  365.                end
  366.       otherwise 
  367.          if history & symbol('!history.key.'||c2d(key2))\='LIT' then /* Is there a defined string? */
  368.             do
  369.                if pos<length(word) then
  370.                   if \hidden then call charout, substr(word,pos+1)
  371.                   else call charout, copies("*",length(substr(word,pos+1)))
  372.                call rubout length(word)
  373.                i=c2d(key2)
  374.                word=!history.key.i
  375.                pos=length(word)
  376.                if \hidden then call charout, word
  377.                else call charout, copies("*",length(word))
  378.             end
  379.       end
  380.       end
  381.    else if width=0 | length(word)<width then /* The key is a normal key & within width */
  382.       do
  383.       if upper then key=translate(key);
  384.       if lower then key=translate(key,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  385.       if pos(key,valid)\=0 then
  386.          do;
  387.          if \hidden then call Charout, key;
  388.          else call charout, "*"
  389.          if !history.insert then
  390.             word=insert(key,word,pos);
  391.          else word=overlay(key,word,pos+1)
  392.          pos=pos+1; 
  393.          if pos<length(word) then
  394.             do
  395.             if \hidden then 
  396.                call Charout, substr(word,pos+1)
  397.             else call Charout, copies("*", length(substr(word,pos+1)))
  398.             call Charout, copies(d2c(8),length(word)-pos)
  399.             end
  400.          end
  401.       else beep(400,4)
  402.       end
  403.    if autoskip & length(word)=width then leave
  404.    key=SysGetKey("NoEcho")
  405. end
  406. if \sameline then say
  407. if (keep) & (word\=="") then
  408.    do
  409.    historical=!history.0
  410.    if word\=!history.historical then
  411.       do
  412.       !history.0=!history.0+1
  413.       historical=!history.0
  414.       !history.historical=word
  415.       end
  416.    end
  417. return word
  418.  
  419. rubout: procedure
  420. arg n
  421. do i=1 to n
  422.    call Charout, d2c(8)||" "||d2c(8)
  423. end
  424. return
  425. /* END OF CmdLine CODE BY ALBERT CROSBY */
  426.