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

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