Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for choice2.cmd

(view source code of choice2.cmd as plain text)

  1. /* Rexx replacement for DOS' CHOICE command            */
  2. /* Version 1.02 for OS/2                               */
  3. /* Written by Rob van der Woude                        */
  4. /* http://www.robvanderwoude.com                       */
  5. /* Needs Quercus System's RexxLib for timeout function */
  6.  
  7.  
  8. /* Initialize RexxUtil */
  9. if RxFuncQuery( "SysLoadFuncs" ) <> 0 then do
  10. 	call RxFuncAdd "SysLoadFuncs", "RexxUtil", "SysLoadFuncs"
  11. 	call SysLoadFuncs
  12. end
  13.  
  14.  
  15. /* Parse command line */
  16. parse arg commandline
  17. /* /? switch */
  18. if commandline = "/?" then call Syntax
  19. if commandline =  "?" then call Syntax
  20. /* /S (case sensitive) switch */
  21. select
  22. 	when pos( "/S", commandline ) > 0 then case = 1
  23. 	when pos( "/s", commandline ) > 0 then case = 1
  24. 	otherwise case = 0
  25. end
  26. /* /C (choices) switch */
  27. parse value commandline with "/C"options .
  28. if options = "" then do
  29. 	parse value commandline with "/c"options .
  30. 	if options = "" then do
  31. 		options = "YN"
  32. 	end
  33. end
  34. if left( options, 1 ) = ":" then do
  35. 	options = substr( options, 2 )
  36. end
  37. if case = 0 then options = translate( options )
  38. /* /T (default/timeout) switch */
  39. parse value commandline with "/T"defchoice","timeout .
  40. if timeout = "" then parse value commandline with "/t"defchoice","timeout .
  41. if left( defchoice, 1 ) = ":" then defchoice = substr( defchoice, 2 )
  42. if timeout <> "" then do
  43. 	if datatype( timeout, "W" ) <> 1 then call Syntax
  44. 	/* Initialize Quercus System's RexxLib */
  45. 	if RxFuncQuery( "RexxLibRegister" ) <> 0 then do
  46. 		call RxFuncAdd "RexxLibRegister", "RexxLib", "RexxLibRegister"
  47. 		call RexxLibRegister
  48. 	end
  49. 	/* Quercus System's RexxLib */
  50. 	if RxFuncQuery( "RexxLibRegister" ) <> 0 then do
  51. 		defchoice = ""
  52. 		timeout   = ""
  53. 	end
  54. end
  55. /* /N (don't display choices) switch */
  56. select
  57. 	when pos( "/N", commandline ) > 0 then dispchoices = 0
  58. 	when pos( "/n", commandline ) > 0 then dispchoices = 0
  59. 	otherwise dispchoices = 1
  60. end
  61. /* /R (redirect) switch */
  62. select
  63. 	when pos( "/R", commandline ) > 0 then redirect = 1
  64. 	when pos( "/r", commandline ) > 0 then redirect = 1
  65. 	otherwise redirect = 0
  66. end
  67. /* Message/prompt */
  68. lastslash = lastpos( "/", commandline )
  69. if lastslash > 0 then do
  70. 	parse value substr( commandline, lastslash ) with "/". message
  71. end
  72. else do
  73. 	message = ""
  74. end
  75.  
  76.  
  77. /* Prepare message/prompt */
  78. prompt = message
  79. if dispchoices = 1 then prompt = prompt||"? ["||options||"]"
  80. /* Check if vertical scrolling will occur */
  81. parse value SysTextScreenSize( ) with rows cols
  82. /* Check where answer should be displayed */
  83. parse value SysCurPos( ) with row col
  84. promptlen = length( prompt )
  85. if row > rows - 2 then row = row - 1
  86. /* Display message/prompt */
  87. say prompt
  88. call SysCurPos row, col + promptlen
  89. call SysCurState "OFF"
  90.  
  91. valid = 0
  92. do i = 0 by 1 until valid > 0
  93. 	if i > 0 then call beep 220, 240
  94. 	/* Simple SysGetKey to retrieve input */
  95. 	select
  96. 		when redirect = 1 then do
  97. 			parse pull choice
  98. 			if case = 0 then do
  99. 				choice = translate( choice )
  100. 			end
  101. 			if choice <> "" then do
  102. 				choice = left( choice, 1 )
  103. 			end
  104. 		end
  105. 		when timeout <> "" then do
  106. 			choice = ""
  107. 			do j = 1 to timeout
  108. 				choice = INKEY( "N", "F" )
  109. 				if choice <> "" then leave
  110. 				call SysSleep 1
  111. 			end
  112. 			if choice = "" then do
  113. 				choice = defchoice
  114. 			end
  115. 		end
  116. 		otherwise do
  117. 			choice = SysGetKey( "NOECHO" )
  118. 		end
  119. 	end
  120. 	if case = 0 then do
  121. 		choice = translate( choice )
  122. 	end
  123. 	valid = pos( choice, options )
  124. end
  125. say choice
  126. EXIT valid
  127.  
  128.  
  129. Syntax: procedure
  130. 	say
  131. 	say "CHOICE,  Version 1.01 for OS/2"
  132. 	say "OS/2 replacement for MS-DOS' CHOICE command"
  133. 	say "Written by Rob van der Woude"
  134. 	say "http://www.robvanderwoude.com"
  135. 	say
  136. 	say "Usage:  CHOICE  [ /C[:]choices ]  [ /N ]  [ /R ]  [ /S ]"
  137. 	say "                [ /T[:]default,timeout ] [ prompt ]"
  138. 	say
  139. 	say "/C[:]choices            available keys; default is YN"
  140. 	say "/N                      don't display choices and ?"
  141. 	say "/R                      use with redirected input"
  142. 	say "/S                      case sensitive keys"
  143. 	say "/T[:]default, timeout   chooses default after timeout"
  144. 	say "                        (needs Quercus System's RexxLib)"
  145. 	say
  146. 	say "Do not combine /R and /T switches"
  147. 	EXIT 0
  148. return
  149.  

page last modified: 2024-04-16; loaded in 0.0206 seconds