Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for choice.rex

(view source code of choice.rex as plain text)

  1. /* Rexx replacement for DOS' CHOICE command             */
  2. /* Version 2.00 for OS/2 and Windows                    */
  3. /* Written by Rob van der Woude                         */
  4. /* http://www.robvanderwoude.com                        */
  5. /* Needs Quercus System's RexxLib for timeout function, */
  6. /* but will not fail if RexxLib isn't available -- in   */
  7. /* that case the timeout option will just be ignored.   */
  8.  
  9.  
  10. /* Initialize RexxUtil */
  11. If RxFuncQuery( "sysloadfuncs" ) <> 0 Then Do
  12. 	Call RxFuncAdd "sysloadfuncs", "RexxUtil", "sysloadfuncs"
  13. 	Call sysloadfuncs
  14. End
  15.  
  16.  
  17. /* Parse command line */
  18. Parse Arg commandline
  19.  
  20. /* /? switch */
  21. If commandline = "/?" Then Call Syntax
  22. If commandline =  "?" Then Call Syntax
  23.  
  24. /* /S (case sensitive) switch */
  25. Select
  26. 	When Pos( "/S", commandline ) > 0 Then case = 1
  27. 	When Pos( "/s", commandline ) > 0 Then case = 1
  28. 	Otherwise case = 0
  29. End
  30.  
  31. /* /C (choices) switch */
  32. Parse Value commandline With "/C"options .
  33. If options = "" Then Do
  34. 	Parse Value commandline With "/c"options .
  35. 	If options = "" Then Do
  36. 		options = "YN"
  37. 	End
  38. End
  39. If Left( options, 1 ) = ":" Then Do
  40. 	options = SubStr( options, 2 )
  41. End
  42. If case = 0 Then options = Translate( options )
  43.  
  44. /* /T (default/timeout) switch */
  45. Parse Value commandline With "/T"defchoice","timeout .
  46. If timeout = "" Then Parse Value commandline With "/t"defchoice","timeout .
  47. If Left( defchoice, 1 ) = ":" Then defchoice = SubStr( defchoice, 2 )
  48. If timeout <> "" Then Do
  49. 	If DataType( timeout, "W" ) <> 1 Then Call Syntax
  50. 	/* Initialize Quercus System's RexxLib */
  51. 	If RxFuncQuery( "RexxLibRegister" ) <> 0 Then Do
  52. 		Call RxFuncAdd "RexxLibRegister", "RexxLib", "RexxLibRegister"
  53. 		Call RexxLibRegister
  54. 	End
  55. 	/* Ignore timeout switch if RexxLib isn't available */
  56. 	If RxFuncQuery( "RexxLibRegister" ) <> 0 Then Do
  57. 		defchoice = ""
  58. 		timeout   = ""
  59. 	End
  60. End
  61.  
  62. /* /N (don't display choices) switch */
  63. Select
  64. 	When Pos( "/N", commandline ) > 0 Then dispchoices = 0
  65. 	When Pos( "/n", commandline ) > 0 Then dispchoices = 0
  66. 	Otherwise dispchoices = 1
  67. End
  68.  
  69. /* /R (redirect) switch */
  70. Select
  71. 	When Pos( "/R", commandline ) > 0 Then redirect = 1
  72. 	When Pos( "/r", commandline ) > 0 Then redirect = 1
  73. 	Otherwise redirect = 0
  74. End
  75.  
  76. /* Message/prompt */
  77. lastslash = LastPos( "/", commandline )
  78. If lastslash > 0 Then Do
  79. 	Parse Value SubStr( commandline, lastslash ) With "/". message
  80. End
  81. Else Do
  82. 	message = ""
  83. End
  84.  
  85.  
  86. /* Prepare message/prompt */
  87. prompt = message
  88. If dispchoices = 1 Then prompt = prompt||"? ["||options||"]"
  89.  
  90. /* Check if vertical scrolling will occur */
  91. Parse Value SysTextScreenSize( ) With rows cols
  92.  
  93. /* Check where answer should be displayed */
  94. Parse Value SysCurPos( ) With row col
  95. promptlen = Length( prompt )
  96. If row > rows - 2 Then row = row - 1
  97.  
  98. /* Display message/prompt */
  99. Say prompt
  100. Call SysCurPos row, col + promptlen
  101. Call SysCurState "OFF"
  102.  
  103. valid = 0
  104. Do i = 0 by 1 Until valid > 0
  105. 	If i > 0 Then Call Beep 220, 240
  106. 	/* Simple SysGetKey to retrieve input */
  107. 	Select
  108. 		When redirect = 1 Then Do
  109. 			Parse Pull choice
  110. 			If case = 0 Then Do
  111. 				choice = Translate( choice )
  112. 			End
  113. 			If choice <> "" Then Do
  114. 				choice = Left( choice, 1 )
  115. 			End
  116. 		End
  117. 		When timeout <> "" Then Do
  118. 			choice = ""
  119. 			Do j = 1 To timeout
  120. 				choice = InKey( "N", "F" )
  121. 				If choice <> "" Then leave
  122. 				Call SysSleep 1
  123. 			End
  124. 			If choice = "" Then Do
  125. 				choice = defchoice
  126. 			End
  127. 		End
  128. 		Otherwise Do
  129. 			choice = SysGetKey( "NOECHO" )
  130. 		End
  131. 	End
  132. 	If case = 0 Then Do
  133. 		choice = Translate( choice )
  134. 	End
  135. 	valid = Pos( choice, options )
  136. End
  137. Say choice
  138. Exit valid
  139.  
  140.  
  141. Syntax: Procedure
  142. 	Say
  143. 	Say "Choice,rex  Version 2.00 for OS/2 and Windows"
  144. 	Say "Rexx replacement for MS-DOS' CHOICE command"
  145. 	Say
  146. 	Say "Usage:  CHOICE.REX  [ /C[:]choices ]  [ /N ]  [ /R ]  [ /S ]"
  147. 	Say "                    [ /T[:]default,timeout ] [ prompt ]"
  148. 	Say
  149. 	Say "                    /C[:]choices            available keys (default YN)"
  150. 	Say "                    /N                      don't display choices and ?"
  151. 	Say "                    /R                      use with redirected input"
  152. 	Say "                    /S                      case sensitive keys"
  153. 	Say "                    /T[:]default, timeout   chooses default after timeout"
  154. 	Say "                                            (needs Quercus System's RexxLib)"
  155. 	Say
  156. 	Say "Do not combine /R and /T switches."
  157. 	Say "/T swith will be ignored if Quercus System's RexxLib isn't available."
  158. 	Say "Since RexxLib is available only for OS/2 this means /T is not valid in Windows."
  159. 	Say
  160. 	Say "Written by Rob van der Woude"
  161. 	Say "http://www.robvanderwoude.com"
  162. 	Exit 0
  163. Return
  164.  

page last uploaded: 2017-04-06, 13:33