# search.sh - Some search commands # # Copyright (C) 2009, 2010 Rocky Bernstein # # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; see the file COPYING. If not, write to # the Free Software Foundation, 59 Temple Place, Suite 330, Boston, # MA 02111 USA. _Dbg_help_add 'reverse' \ '?search Search backword and list line of a script.' proc _Dbg_do_reverse { typeset delim_search_pat=$1 if (( $# < 1 )) { _Dbg_errmsg "Need a search pattern" return 1 } shift matchstr $delim_search_pat { [?] { } [?]* { typeset -a word global word := '('$(_Dbg_split '?' $delim_search_pat)) global _Dbg_last_search_pat := $(word[0]) } # Error * { global _Dbg_last_search_pat := $delim_search_pat } } typeset -i i for (( i=_Dbg_listline-1; i > 0 ; i-- )) ; do _Dbg_get_source_line $i "$_Dbg_frame_last_filename" eval "$_seteglob" if [[ $_Dbg_source_line == *$_Dbg_last_search_pat* ]] ; then eval "$_resteglob" _Dbg_do_list $i 1 _Dbg_listline=$i _Dbg_last_cmd='search' return 0 fi eval "$_resteglob" done _Dbg_msg "search pattern: $_Dbg_last_search_pat not found." return 1 } _Dbg_help_add 'search' \ '/search/ -- Search forward and list line of a script.' # /search/ proc _Dbg_do_search { typeset delim_search_pat=$(1) if (( $# < 1 )) { _Dbg_errmsg "Need a search pattern" return 1 } shift typeset search_pat matchstr $delim_search_pat { / { } /* { typeset -a word global word := '('$(_Dbg_split '/' $delim_search_pat)) global _Dbg_last_search_pat := $(word[0]) } * { global _Dbg_last_search_pat := $delim_search_pat } } typeset max_line global max_line := $[_Dbg_get_maxline $_Dbg_frame_last_filename] typeset -i i for (( i=_Dbg_listline+1; i < max_line ; i++ )) ; do _Dbg_get_source_line $i "$_Dbg_frame_last_filename" eval "$_seteglob" if [[ $_Dbg_source_line == *$_Dbg_last_search_pat* ]] ; then eval "$_resteglob" _Dbg_do_list $i 1 _Dbg_listline=$i # _Dbg_last_cmd='/' return 0 fi eval "$_resteglob" done _Dbg_errmsg "search pattern: $_Dbg_last_search_pat not found." return 1 } (CommandList children: [ (C {(_Dbg_help_add)} {(SQ )} {(SQ <"?search\n"> <"\n"> <"Search backword and list line of a script.">)} ) (FuncDef name: _Dbg_do_reverse body: (BraceGroup children: [ (C {(typeset)} {(Lit_VarLike "delim_search_pat=") ($ VSub_Number "$1")}) (If arms: [ (if_arm cond: [ (Sentence child: (DParen child: (ArithBinary op_id: Arith_Less left: (ArithWord w:{($ VSub_Pound "$#")}) right: (ArithWord w:{(Lit_Digits 1)}) ) ) terminator: ) ] action: [ (C {(_Dbg_errmsg)} {(DQ ("Need a search pattern"))}) (ControlFlow token: arg_word:{(1)}) ] spids: [-1 101] ) ] spids: [-1 116] ) (C {(shift)}) (Case to_match: {(DQ ($ VSub_Name "$delim_search_pat"))} arms: [ (case_arm pat_list: [{(Lit_Other "[") (Lit_Other "?") (Lit_Other "]")}] spids: [132 136 139 -1] ) (case_arm pat_list: [{(Lit_Other "[") (Lit_Other "?") (Lit_Other "]") (Lit_Other "*")}] action: [ (C {(typeset)} {(-a)} {(word)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:word) op: Equal rhs: { (ArrayLiteralPart words: [ { (CommandSubPart command_list: (CommandList children: [ (C {(_Dbg_split)} {(SQ <"?">)} {($ VSub_Name "$delim_search_pat")} ) ] ) left_token: spids: [159 167] ) } ] ) } spids: [157] ) ] spids: [157] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:_Dbg_last_search_pat) op: Equal rhs: { (BracedVarSub token: bracket_op: (ArrayIndex expr:(ArithWord w:{(Lit_Digits 0)})) spids: [172 177] ) } spids: [171] ) ] spids: [171] ) ] spids: [142 147 180 -1] ) (case_arm pat_list: [{(Lit_Other "*")}] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:_Dbg_last_search_pat) op: Equal rhs: {($ VSub_Name "$delim_search_pat")} spids: [192] ) ] spids: [192] ) ] spids: [187 189 -1 196] ) ] spids: [123 129 196] ) (C {(typeset)} {(-i)} {(i)}) (ForExpr init: (BinaryAssign op_id: Arith_Equal left: (LhsName name:i) right: (ArithBinary op_id: Arith_Minus left: (ArithVarRef name:_Dbg_listline) right: (ArithWord w:{(Lit_Digits 1)}) ) ) cond: (ArithBinary op_id: Arith_Great left: (ArithVarRef name:i) right: (ArithWord w:{(Lit_Digits 0)}) ) update: (UnaryAssign op_id:Node_PostDMinus child:(LhsName name:i)) body: (DoGroup children: [ (C {(_Dbg_get_source_line)} {($ VSub_Name "$i")} {(DQ ($ VSub_Name "$_Dbg_frame_last_filename"))} ) (C {(eval)} {(DQ ($ VSub_Name "$_seteglob"))}) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$_Dbg_source_line")} right: {(Lit_Other "*") ($ VSub_Name "$_Dbg_last_search_pat") (Lit_Other "*") } ) ) terminator: ) ] action: [ (C {(eval)} {(DQ ($ VSub_Name "$_resteglob"))}) (C {(_Dbg_do_list)} {($ VSub_Name "$i")} {(1)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:_Dbg_listline) op: Equal rhs: {($ VSub_Name "$i")} spids: [285] ) ] spids: [285] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:_Dbg_last_cmd) op: Equal rhs: {(SQ )} spids: [289] ) ] spids: [289] ) (ControlFlow token: arg_word: {(0)} ) ] spids: [-1 268] ) ] spids: [-1 300] ) (C {(eval)} {(DQ ($ VSub_Name "$_resteglob"))}) ] spids: [233 310] ) ) (C {(_Dbg_msg)} {(DQ ("search pattern: ") ($ VSub_Name "$_Dbg_last_search_pat") (" not found."))} ) (ControlFlow token: arg_word:{(1)}) ] spids: [77] ) spids: [73 76] ) (C {(_Dbg_help_add)} {(SQ )} {(SQ <"/search/ -- Search forward and list line of a script.">)}) (FuncDef name: _Dbg_do_search body: (BraceGroup children: [ (C {(typeset)} {(Lit_VarLike "delim_search_pat=") (${ VSub_Number 1)}) (If arms: [ (if_arm cond: [ (Sentence child: (DParen child: (ArithBinary op_id: Arith_Less left: (ArithWord w:{($ VSub_Pound "$#")}) right: (ArithWord w:{(Lit_Digits 1)}) ) ) terminator: ) ] action: [ (C {(_Dbg_errmsg)} {(DQ ("Need a search pattern"))}) (ControlFlow token: arg_word:{(1)}) ] spids: [-1 376] ) ] spids: [-1 391] ) (C {(shift)}) (C {(typeset)} {(search_pat)}) (Case to_match: {(DQ ($ VSub_Name "$delim_search_pat"))} arms: [ (case_arm pat_list:[{(/)}] spids:[411413416-1]) (case_arm pat_list: [{(/) (Lit_Other "*")}] action: [ (C {(typeset)} {(-a)} {(word)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:word) op: Equal rhs: { (ArrayLiteralPart words: [ { (CommandSubPart command_list: (CommandList children: [ (C {(_Dbg_split)} {(SQ )} {($ VSub_Name "$delim_search_pat")} ) ] ) left_token: spids: [434 442] ) } ] ) } spids: [432] ) ] spids: [432] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:_Dbg_last_search_pat) op: Equal rhs: { (BracedVarSub token: bracket_op: (ArrayIndex expr:(ArithWord w:{(Lit_Digits 0)})) spids: [447 452] ) } spids: [446] ) ] spids: [446] ) ] spids: [419 422 455 -1] ) (case_arm pat_list: [{(Lit_Other "*")}] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:_Dbg_last_search_pat) op: Equal rhs: {($ VSub_Name "$delim_search_pat")} spids: [463] ) ] spids: [463] ) ] spids: [458 460 -1 467] ) ] spids: [402 408 467] ) (C {(typeset)} {(max_line)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:max_line) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (C {(_Dbg_get_maxline)} {(DQ ($ VSub_Name "$_Dbg_frame_last_filename"))}) ] ) left_token: spids: [477 483] ) } spids: [476] ) ] spids: [476] ) (C {(typeset)} {(-i)} {(i)}) (ForExpr init: (BinaryAssign op_id: Arith_Equal left: (LhsName name:i) right: (ArithBinary op_id: Arith_Plus left: (ArithVarRef name:_Dbg_listline) right: (ArithWord w:{(Lit_Digits 1)}) ) ) cond: (ArithBinary op_id: Arith_Less left: (ArithVarRef name:i) right: (ArithVarRef name:max_line) ) update: (UnaryAssign op_id:Node_PostDPlus child:(LhsName name:i)) body: (DoGroup children: [ (C {(_Dbg_get_source_line)} {($ VSub_Name "$i")} {(DQ ($ VSub_Name "$_Dbg_frame_last_filename"))} ) (C {(eval)} {(DQ ($ VSub_Name "$_seteglob"))}) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$_Dbg_source_line")} right: {(Lit_Other "*") ($ VSub_Name "$_Dbg_last_search_pat") (Lit_Other "*") } ) ) terminator: ) ] action: [ (C {(eval)} {(DQ ($ VSub_Name "$_resteglob"))}) (C {(_Dbg_do_list)} {($ VSub_Name "$i")} {(1)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:_Dbg_listline) op: Equal rhs: {($ VSub_Name "$i")} spids: [573] ) ] spids: [573] ) (ControlFlow token: arg_word: {(0)} ) ] spids: [-1 556] ) ] spids: [-1 586] ) (C {(eval)} {(DQ ($ VSub_Name "$_resteglob"))}) ] spids: [521 596] ) ) (C {(_Dbg_errmsg)} {(DQ ("search pattern: ") ($ VSub_Name "$_Dbg_last_search_pat") (" not found."))} ) (ControlFlow token: arg_word:{(1)}) ] spids: [350] ) spids: [346 349] ) ] )