PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/yekki/3rdparty/native/win/shell/UGREP.KSH

http://soulmates.googlecode.com/
Korn Shell | 388 lines | 194 code | 48 blank | 146 comment | 19 complexity | 6e99cc719604b7eee27d813700f0396c MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, BSD-3-Clause-No-Nuclear-License-2014, AGPL-1.0
  1. # UltraGrep - A find/grep dlg example.
  2. #
  3. # This shell script accepts user input from a dialog box, then runs
  4. # 'find <directory> -name <filemask> | xargs grep <grep options> <pattern>'
  5. # The output from this command is piped into a results dialog box, which
  6. # the user can then browse through.
  7. #
  8. # Useful for searching through a tree of source files for keywords, e.g.
  9. # 'find c:/msvc20/samples/win32 -name "*.cpp" | xargs grep GetOpenFileName'
  10. # Dialog resources are included at the end of this file
  11. RES=$(whence $0)
  12. # Here are some constants for the controls used in the 2 dialogs
  13. IDOK=1
  14. IDCANCEL=2
  15. IDC_PATTERN=10
  16. IDC_FILES=11
  17. IDC_DIRECTORY=12
  18. IDC_BROWSE=13
  19. IDC_NOT_PATTERN=15
  20. IDC_ENTIRE_LINE=16
  21. IDC_IGNORE_CASE=17
  22. IDC_DIR_ALWAYS=14
  23. IDC_DIR_NEVER=18
  24. IDC_DIR_TO=19
  25. IDC_DIR_TO_EDIT=20
  26. IDC_LISTBOX=101
  27. IDC_EDIT=102
  28. # Here are some strings we use to store the directory setting in the Registry
  29. DirKey='HKEY_CURRENT_USER\Software\Mortice Kern Systems\Toolkit\UltraGrep'
  30. DirKeyName='Starting Directory'
  31. # Make these variables into integers to make math faster
  32. typeset -i count width height dlgwidth dlgheight
  33. count=0
  34. # This function is called from the second dialog loop, when a user double
  35. # clicks on a line or presses the 'Edit File' button.
  36. function edit_file
  37. {
  38. # Get the current listbox selection
  39. dlg getcursel -c $IDC_LISTBOX index
  40. if [ index -ne -1 ]
  41. then
  42. # If there is a current selection, get the filename and
  43. # line number for that selection, and run 'vi' on the
  44. # file. The -c option causes vi to jump to the specified
  45. # line number.
  46. dlg gettext -c $IDC_LISTBOX -i $index filename lineno
  47. start vi -c"$lineno" "$filename"
  48. fi
  49. }
  50. # This function resizes the controls in the results dialog, so when the
  51. # user resized the dialog, the controls move to fill the space.
  52. function resize_results
  53. {
  54. dlg getpos -w dlgwidth -h dlgheight
  55. dlg getpos -c $IDC_LISTBOX -x xpos -y ypos -w width -h height
  56. width=$dlgwidth-30
  57. height=$dlgheight-85
  58. dlg setpos -c $IDC_LISTBOX -x $xpos -y $ypos -w $width -h $height
  59. width=$width-66
  60. height=$height+25
  61. dlg setpos -c $IDOK -x $width -y $height
  62. width=$width-90
  63. dlg setpos -c $IDC_EDIT -x $width -y $height
  64. }
  65. # This is where the program really begins. Load the first dialog to query
  66. # the user.
  67. if ! dlg load -x -1 -y -1 $RES DIALOG_1
  68. then
  69. exit
  70. fi
  71. # Add some common file types to the 'pattern' combo box
  72. dlg addtext -c $IDC_FILES "*"
  73. dlg addtext -c $IDC_FILES "*.awk"
  74. dlg addtext -c $IDC_FILES "*.c*"
  75. dlg addtext -c $IDC_FILES "*.c"
  76. dlg addtext -c $IDC_FILES "*.h"
  77. dlg addtext -c $IDC_FILES "*.h*"
  78. dlg addtext -c $IDC_FILES "*.ksh"
  79. dlg addtext -c $IDC_FILES "*.mk"
  80. dlg addtext -c $IDC_FILES "*.txt"
  81. dlg setcursel -c $IDC_FILES -i 0
  82. # Set the default starting directory. We might have saved an old value in
  83. # the registry, so try to get it.
  84. directory="$(registry -p -r -k "$DirKey" -n "$DirKeyName" 2>nul)"
  85. if [ $? -ne 0 ]
  86. then
  87. # Set the default directory to the current directory
  88. directory="$PWD"
  89. fi
  90. dlg settext -c $IDC_DIRECTORY "$directory"
  91. dlg checkbutton -c $IDC_DIR_ALWAYS 1
  92. # This is the event loop for the first dialog. We keep going until the
  93. # user hits 'Ok' or 'Cancel'.
  94. while dlg event msg control; do
  95. case $control in
  96. $IDC_BROWSE)
  97. # The user hit the 'Browse' button, so bring
  98. # up a directory browser
  99. directory="$(filebox -D)"
  100. if [ $? -eq 0 ]
  101. then
  102. dlg settext -c 12 "$directory"
  103. fi
  104. # Get rid of any pending messages
  105. dlg event flush ;;
  106. $IDCANCEL)
  107. # The user hit 'Cancel'
  108. dlg close
  109. exit ;;
  110. $IDOK)
  111. # The user hit 'OK'. We need to validate all of the input
  112. # before we can continue.
  113. # Get the search pattern
  114. dlg gettext -c $IDC_PATTERN pattern
  115. if [ -z "$pattern" ]
  116. then
  117. msgbox -i exclamation "UltraGrep" "You must specify a pattern to grep for."
  118. dlg setfocus -c $IDC_PATTERN
  119. continue
  120. fi
  121. # Get the file pattern
  122. dlg gettext -c $IDC_FILES files
  123. if [ -z "$files" ]
  124. then
  125. msgbox -i exclamation "UltraGrep" "You must specify a filename pattern to search for."
  126. dlg setfocus -c $IDC_FILES
  127. continue
  128. fi
  129. # Get the starting directory
  130. dlg gettext -c $IDC_DIRECTORY directory
  131. if [ -z "$directory" ]
  132. then
  133. msgbox -i exclamation "UltraGrep" "You must specify a starting directory."
  134. dlg setfocus -c $IDC_DIRECTORY
  135. continue
  136. elif [ ! -d "$directory" ]
  137. then
  138. msgbox -i exclamation "UltraGrep" "The directory you specified does not exist."
  139. dlg setfocus -c $IDC_DIRECTORY
  140. continue
  141. fi
  142. # Save the directory settting in the registry
  143. registry -s -k "$DirKey" -n "$DirKeyName" -v "$directory" 2>nul
  144. # Get the options for grep
  145. if { dlg isbuttonchecked -c $IDC_NOT_PATTERN result; test result -eq 1; }
  146. then
  147. grepopts=-v
  148. fi
  149. if { dlg isbuttonchecked -c 16 $IDC_ENTIRE_LINE; test result -eq 1; }
  150. then
  151. grepopts="$grepopts -x"
  152. fi
  153. if { dlg isbuttonchecked -c 17 $IDC_IGNORE_CASE; test result -eq 1; }
  154. then
  155. grepopts="$grepopts -i"
  156. fi
  157. # Get the options for find
  158. if { dlg isbuttonchecked -c $IDC_DIR_NEVER result; test result -eq 1; }
  159. then
  160. findopts=-prune
  161. elif { dlg isbuttonchecked -c $IDC_DIR_TO result; test result -eq 1; }
  162. then
  163. dlg gettext -c $IDC_DIR_TO_EDIT result
  164. if [ -z $result ]
  165. then
  166. msgbox -i exclamation "UltraGrep" "You must specifiy a valid level number."
  167. dlg setfocus -c $IDC_DIR_TO_EDIT
  168. continue
  169. elif [ $result -lt 0 ]
  170. then
  171. msgbox -i exclamation "UltraGrep" "You must specifiy a valid level number."
  172. dlg setfocus -c $IDC_DIR_TO_EDIT
  173. continue
  174. else
  175. findopts="-level $result"
  176. fi
  177. fi
  178. break ;;
  179. esac
  180. done
  181. # Close the query dialog
  182. dlg close
  183. # Load up the results dialog
  184. if ! dlg load -x -1 -y -1 $RES DIALOG_2
  185. then
  186. exit
  187. fi
  188. # Add the columns to the list box, and size the controls to
  189. # fit the size of the dialog.
  190. dlg columns -c $IDC_LISTBOX File 200 "Line #" 50 Result 200
  191. resize_results
  192. dlg settext "UltraGrep - Searching..."
  193. # This is where the actual command gets run. We want the user to be able
  194. # to cancel the find by pressing the 'Cancel' button on the results dialog,
  195. # so run it as a co-process, using dlg -P -W to read the output.
  196. # To get grep to output the filename every time, it needs 2 input files, so
  197. # give it 'nul' as the second file.
  198. exec 2>nul
  199. find "$directory" $findopts -name "$files" -exec grep -n $grepopts "$pattern" {} nul \; |&
  200. # Set the shell's input field separator so we can parse the output from
  201. # grep easily.
  202. IFS=:
  203. # Start the event loop for the results dialog. Remember we started the
  204. # command as a co-process, so we use the -P and -W flags on 'dlg event'
  205. # to read the results from the co-process. This loop will continue until
  206. # our co-process finishes, or the user hits 'Cancel'.
  207. while dlg event -P -W msg control; do
  208. case $msg in
  209. coprocess)
  210. # We got some data from the co-process, which will be output
  211. # from our grep command.
  212. # The output from grep looks like this:
  213. # <filename>:<line number>:<matching line>
  214. #
  215. # If we specified a full directory name to search from
  216. # (e.g. "c:/msvc20/samples/win32"), the colon in the drive
  217. # specifier is going to confuse the shell, because we've
  218. # set IFS=:. So if the filename has a drive specifier,
  219. # we need to parse the output from grep slightly differently.
  220. if [ "${directory#?:}" = "$directory" ]
  221. then
  222. read -p -r filename lineno match || break
  223. dlg addtext -c $IDC_LISTBOX "$filename" "$lineno" "$match"
  224. else
  225. read -p -r drive filename lineno match || break
  226. # Reconstruct the filename
  227. filename="$drive":"$filename"
  228. dlg addtext -c $IDC_LISTBOX "$filename" "$lineno" "$match"
  229. fi
  230. count=$count+1 ;;
  231. child-exit)
  232. # Our co-process is done.
  233. break ;;
  234. break)
  235. break ;;
  236. command)
  237. case $control in
  238. $IDOK)
  239. # The user pressed 'Cancel'
  240. break ;;
  241. esac ;;
  242. esac
  243. done
  244. # Make sure our co-process process is dead.
  245. kill %1
  246. wait
  247. # Change the title and the name of the 'Cancel' button on the results
  248. # dialog.
  249. dlg settext "UltraGrep Results - $count file(s) found"
  250. dlg settext -c $IDOK Close
  251. dlg enabled -c $IDC_EDIT 1
  252. # Now we start another event loop on the results dialog, which allows the
  253. # user to browse through the results of the command.
  254. while dlg event msg control; do
  255. case $msg in
  256. command)
  257. case $control in
  258. $IDOK)
  259. break ;;
  260. $IDC_EDIT)
  261. edit_file ;;
  262. esac ;;
  263. double)
  264. # The user double clicked on the list box, which
  265. # is the same as clicking 'Edit File'
  266. edit_file ;;
  267. size)
  268. # The user resized the frame so resize
  269. # our child windows to fit
  270. resize_results ;;
  271. esac
  272. done
  273. dlg close
  274. # That's all folks.
  275. # Dialog resources follow this line
  276. #[]mks internal resource : dialog : DIALOG_1
  277. #[ ]begin : size 1012
  278. #[ ]MP #(D 3 !< %0#Y (X %4 ; !T '( 80!' '( 90!P " !-
  279. #[ ]M %, ( !3 &$ ;@!S " 4P!E '( :0!F "4 + H )@ ( /__
  280. #[ ]M__^" "8 4P!E &$ <@!C &@ ( !& &\ <@ Z @5 -0 (
  281. #[ ]M 'P # * /__@0 )0 L 'P!C @ _____X( 20!N "
  282. #[ ]M1@!I &P 90!S " 30!A '0 8P!H &D ;@!G " = !H &4 ( F % 80!T
  283. #[ ]M '0 90!R &X .@ @(A4 !R !P 0 \ L __^%
  284. #[ ]M E "P V #( " #_____@@!3 '0 80!R '0 :0!N &< ( F $8 <@!O
  285. #[ ]M &T .@ "!4 ^ #4 <P , P __^! $ 5
  286. #[ ]MO@ & #( #@ ! /__@ !/ $L !4 "^ !@ ,@ . ( __^
  287. #[ ]M $, 80!N &, 90!L 5 O@ T #( #@ - /__@ F $(
  288. #[ ]M<@!O '< <P!E "X +@ N < % !P!/ (@ -P#_____@ !-
  289. #[ ]M &$ = !C &@ :0!N &< ( !/ ' = !I &\ ;@!S , 5
  290. #[ ]M#0!; 'X # / /__@ F $0 :0!S ' ; !A 'D ( !L &D ;@!E ', ( !.
  291. #[ ]M $\ 5 @ &T 80!T &, : !I &X 9P @ ' 80!T '0 90!R &X ,
  292. #[ ]M 5 #0!H '\ # 0 /__@ F $T 80!T &, : @ &\ ;@ @ &4 ;@!T
  293. #[ ]M &D <@!E " ; !I &X 90!S " ;P!N &P >0 P !4 - '4
  294. #[ ]M/ , !$ __^ "8 20!G &X ;P!R &4 ( !C &$ <P!E < 5
  295. #[ ]M DP!/ %X -P#_____@ !$ &4 <P!C &4 ;@!D " 1 !I '( 90!C '0
  296. #[ ]M;P!R &D 90!S ) -0 )H 6@ \ P #@#__X )@!! &P =P!A
  297. #[ ]M 'D <P "0 4 ": &@ / , !( __^ "8 3@!E '8 90!R
  298. #[ ]M D % F@!V !4 # 3 /__@ F %0 ;P "!4 "S
  299. #[ ]M '< $ + !0 __^! E QP!X "4 " #_____@@!L &4
  300. #[ ]6=@!E &P <P @ &\ ;@!L 'D
  301. #[ ]end
  302. #[]mks internal resource : dialog : DIALOG_2
  303. #[ ]begin : size 242
  304. #[ ]MP #,D # 8 #P L =( %4 ; !T '( 80!' '( 90!P " 4@!E
  305. #[ ]M ', =0!L '0 <P @ 30!3 " 4P!A &X <P @ %, 90!R &D 9@
  306. #[ ]M 5 ]0"^ #( #@ ! /__@ !# &$ ;@!C &4 ; %8
  307. #[ ]M +P O@ R X 9@#__X )@!% &0 :0!T " 1@!I &P 90 % (%0
  308. #[ ]M 4 !0 B :\ 90!3 'D <P!, &D <P!T %8 :0!E '< ,P R 1P!E
  309. #[ ]1 &X 90!R &D 8P Q A
  310. #[ ]end
  311. #[]mks internal resource : icon : \0 : width 32 height 32 colors 16
  312. #[ ]begin : size 744
  313. #[ ]M* " ! 0 $ " @
  314. #[ ]M ( ( " @ " @ " (" # P, @(" _P _P /__ /\
  315. #[ ]M #_ /\ __\ /___P#_________^O /__O[\ _W]_?W]_<*^@#_\/O[
  316. #[ ]M /_W]_?W]__Z\ __^_OP #_?_#P\/#PKZ /_P^_L __</____"OKP#__[
  317. #[ ]M^_ /]_____\*^OH __#[^P #_]P___P(B(B /__O[\ _W____ B(B(@
  318. #[ ]M#_\/O[ /_W#_\ __^_OP #_?__P(/KZ^O#__P^_L __</ B"O
  319. #[ ]MKZ\/___[^_ /]_\/(@^OKP?W]_#[^P #P\*^B(*^O!_?W]_O[\ ^OKZ
  320. #[ ]M\B#Z\ \/#P\/O[ *^OKZ(@KP_____P^_OP #Z^OKR(/#P____#[^_L
  321. #[ ]M /?___\/O[^P( /]_#__P^_O[ B( #_______]___#[^_L*
  322. #[ ]M(B( _______W\/\/O[^PJJHB(/_______W_P^_O["JJJHB+_______?P#[^_
  323. #[ ]ML*JJJJHB#P\/#P\/#_O[^PJJJJJJHK^_O[^_O[^_O[#ZJJJJJJ#[^_O[^_O[
  324. #[ ]M^_L/_ZJJJJH"O[^_O[^_O[^PJO_ZJJJ@HOO[^_O[^_O["JJO_ZJJ"J(
  325. #[ ]M *^JJO_ZH*JJ *^JJO_PJJH *^JJO#_J@(
  326. #[ ]M *^JH*K_ B *^@JJH/(@ !\ ? 'P
  327. #[ ]M !\ ? 'P !\ ? 'P !\ ? 'P !\ ?
  328. #[ ]M'P !\ / !P , !
  329. #[ ]8 /__ #__X ___ /__X #___
  330. #[ ]end
  331. #[]mks internal resource : icon : \1 : width 16 height 16 colors 16
  332. #[ ]begin : size 296
  333. #[ ]M* ! @ 0 $ #
  334. #[ ]M ( ( " @ " @ " (" # P, @(" _P _P /__ /\
  335. #[ ]M #_ /\ __\ /___P#_B(B(H/B_ /^/_XJ@^/L _X_X(B#XOP#_CX /C[
  336. #[ ]M /^("JH/^+\ B((*H/_X^P"JH@H(B(B_ *JB (__B_L /C_B_L"#___^/
  337. #[ ]MB_L*(O___XB_L*HBB(B(B_L*JJ*_O[^_L/JJH/O[^_L*KZH* "JJ\*(
  338. #[ ]M JH/H@ #6 4 P#_ /_^P #_P P ,'_P #__\ P(" $" @
  339. #[ ]: /\ #_ #[ /\'_P#___^ +P
  340. #[ ]end