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

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