# -*- shell-script -*- # "Examine" debugger command. # # Copyright (C) 2002-2004, 2006, 2008, 2011, 2016 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. if [[ $0 == ${BASH_SOURCE[0]} ]] { global dirname := $(BASH_SOURCE[0]%/*) [[ $dirname == $0 ]] && global top_dir := ''..'' || global top_dir := "$(dirname)/.." for lib_file in [help alias] { source $top_dir/lib/$(lib_file).sh; } } _Dbg_help_add 'examine' \ "**examine** *expr* Print value of an expression via \"typeset\", \"let\", and failing these, \"eval\". To see the structure of a variable do not prepeand a leading $. Arithmetic expressions also do not need leading $ for their value is to be substituted. However if *expr* falls into neither these, categories variables witih *expr* need $ to have their value substituted. Examples: --------- # code: # typeset -a typeset -a x=(2 3 4) # typeset -ir p=1 bashdb<1> examine x # note no $ declare -a x='([0]="2" [1]="3" [2]="4")' bashdb<2> examine $x # note with $ 1 # because this is how bash evaluates $x bashdb<3> x p declare -ir p="1" bashdb<3> x p+2 3 bashdb<3> x $p+2 3 See also: --------- **eval** and **pr**." proc _Dbg_do_examine { typeset -r _Dbg_expr=$(@:-"$_Dbg_last_x_args") typeset _Dbg_result typeset isblank=$_Dbg_expr if [[ -z $isblank ]] { _Dbg_msg $_Dbg_expr } elif _Dbg_defined $_Dbg_expr { global _Dbg_result := $[typeset -p $_Dbg_expr] _Dbg_msg $_Dbg_result } elif _Dbg_is_function $_Dbg_expr $_Dbg_set_debug { global _Dbg_result := $[typeset -f $_Dbg_expr] _Dbg_msg $_Dbg_result } else { typeset -i _Dbg_rc eval let _Dbg_result=$_Dbg_expr !2 >/dev/null; global _Dbg_rc := $Status if (( _Dbg_rc != 0 )) { _Dbg_do_print $_Dbg_expr } else { _Dbg_msg $_Dbg_result } } global _Dbg_last_x_args := $_Dbg_x_args return 0 } _Dbg_alias_add 'x' 'examine' # Demo it. if [[ $0 == ${BASH_SOURCE[0]} ]] { for _Dbg_file in [fns msg] { source $top_dir/lib/$(_Dbg_file).sh } source $top_dir/command/help.sh global _Dbg_args := ''examine'' _Dbg_do_help x _Dbg_do_examine top_dir } (CommandList children: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Number "$0")} right: { (BracedVarSub token: bracket_op: (ArrayIndex expr:(ArithWord w:{(Lit_Digits 0)})) spids: [69 74] ) } ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:dirname) op: Equal rhs: { (BracedVarSub token: bracket_op: (ArrayIndex expr:(ArithWord w:{(Lit_Digits 0)})) suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [84 92] ) } spids: [83] ) ] spids: [83] ) (AndOr children: [ (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$dirname")} right: {($ VSub_Number "$0")} ) ) (AndOr children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:top_dir) op: Equal rhs: {(SQ <..>)} spids: [107] ) ] spids: [107] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:top_dir) op: Equal rhs: {(${ VSub_Name dirname) (/..)} spids: [114] ) ] spids: [114] ) ] op_id: Op_DPipe ) ] op_id: Op_DAmp ) (ForEach iter_name: lib_file iter_words: [{(help)} {(alias)}] do_arg_iter: False body: (DoGroup children: [ (Sentence child: (C {(source)} {($ VSub_Name "$top_dir") (/lib/) (${ VSub_Name lib_file) (.sh)}) terminator: ) ] spids: [133 145] ) spids: [126 131] ) ] spids: [-1 80] ) ] spids: [-1 147] ) (C {(_Dbg_help_add)} {(SQ )} { (DQ ("**examine** *expr*\n") ("\n") ("Print value of an expression via ") (EscapedLiteralPart token:) (typeset) (EscapedLiteralPart token:) (", ") (EscapedLiteralPart token:) (let) (EscapedLiteralPart token:) (", and failing these,\n") (EscapedLiteralPart token:) (eval) (EscapedLiteralPart token:) (".\n") ("\n") ("To see the structure of a variable do not prepeand a leading ") (Lit_Other "$") (".\n") ("\n") ("Arithmetic expressions also do not need leading ") (Lit_Other "$") (" for\n") ("their value is to be substituted.\n") ("\n") ("However if *expr* falls into neither these, categories variables\n") ("witih *expr* need ") (Lit_Other "$") (" to have their value substituted.\n") ("\n") ("Examples:\n") ("---------\n") ("\n") (" # code:\n") (" # typeset -a typeset -a x=(2 3 4)\n") (" # typeset -ir p=1\n") (" bashdb<1> examine x # note no ") (Lit_Other "$") ("\n") (" declare -a x='([0]=") ) (2) (DQ (" [1]=")) (3) (DQ (" [2]=")) (4) (DQ (")'\n") (" bashdb<2> examine ") ($ VSub_Name "$x") (" # note with ") (Lit_Other "$") ("\n") (" 1 # because this is how bash evaluates ") ($ VSub_Name "$x") ("\n") (" bashdb<3> x p\n") (" declare -ir p=") ) (1) (DQ ("\n") (" bashdb<3> x p+2\n") (" 3\n") (" bashdb<3> x ") ($ VSub_Name "$p") ("+2\n") (" 3\n") ("\n") ("See also:\n") ("---------\n") ("\n") ("**eval** and **pr**.") ) } ) (FuncDef name: _Dbg_do_examine body: (BraceGroup children: [ (C {(typeset)} {(-r)} {(Lit_VarLike "_Dbg_expr=") (BracedVarSub token: suffix_op: (StringUnary op_id: VTest_ColonHyphen arg_word: {(DQ ($ VSub_Name "$_Dbg_last_x_args"))} ) spids: [250 256] ) } ) (C {(typeset)} {(_Dbg_result)}) (C {(typeset)} {(Lit_VarLike "isblank=") ($ VSub_Name "$_Dbg_expr")}) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolUnary op_id:BoolUnary_z child:{($ VSub_Name "$isblank")}) ) terminator: ) ] action: [(C {(_Dbg_msg)} {(DQ ($ VSub_Name "$_Dbg_expr"))})] spids: [-1 282] ) (if_arm cond: [ (Sentence child: (C {(_Dbg_defined)} {($ VSub_Name "$_Dbg_expr")}) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:_Dbg_result) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(typeset)} {(-p)} {($ VSub_Name "$_Dbg_expr")})] ) left_token: spids: [304 310] ) } spids: [303] ) ] spids: [303] ) (C {(_Dbg_msg)} {(DQ ($ VSub_Name "$_Dbg_result"))}) ] spids: [292 300] ) (if_arm cond: [ (Sentence child: (C {(_Dbg_is_function)} {(DQ ($ VSub_Name "$_Dbg_expr"))} {($ VSub_Name "$_Dbg_set_debug")} ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:_Dbg_result) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(typeset)} {(-f)} {($ VSub_Name "$_Dbg_expr")})] ) left_token: spids: [335 341] ) } spids: [334] ) ] spids: [334] ) (C {(_Dbg_msg)} {(DQ ($ VSub_Name "$_Dbg_result"))}) ] spids: [320 331] ) ] else_action: [ (C {(typeset)} {(-i)} {(_Dbg_rc)}) (Sentence child: (SimpleCommand words: [{(eval)} {(let)} {(Lit_VarLike "_Dbg_result=") ($ VSub_Name "$_Dbg_expr")}] redirects: [(Redir op_id:Redir_Great fd:2 arg_word:{(/dev/null)} spids:[368])] ) terminator: ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:_Dbg_rc) op: Equal rhs: {($ VSub_QMark "$?")} spids: [372] ) ] spids: [372] ) (If arms: [ (if_arm cond: [ (Sentence child: (DParen child: (ArithBinary op_id: Arith_NEqual left: (ArithVarRef name:_Dbg_rc) right: (ArithWord w:{(Lit_Digits 0)}) ) ) terminator: ) ] action: [(C {(_Dbg_do_print)} {(DQ ($ VSub_Name "$_Dbg_expr"))})] spids: [-1 391] ) ] else_action: [(C {(_Dbg_msg)} {(DQ ($ VSub_Name "$_Dbg_result"))})] spids: [401 411] ) ] spids: [351 414] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:_Dbg_last_x_args) op: Equal rhs: {(DQ ($ VSub_Name "$_Dbg_x_args"))} spids: [417] ) ] spids: [417] ) (ControlFlow token: arg_word:{(0)}) ] spids: [242] ) spids: [238 241] ) (C {(_Dbg_alias_add)} {(SQ )} {(SQ )}) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Number "$0")} right: { (BracedVarSub token: bracket_op: (ArrayIndex expr:(ArithWord w:{(Lit_Digits 0)})) spids: [452 457] ) } ) ) terminator: ) ] action: [ (ForEach iter_name: _Dbg_file iter_words: [{(fns)} {(msg)}] do_arg_iter: False body: (DoGroup children: [ (C {(source)} {($ VSub_Name "$top_dir") (/lib/) (${ VSub_Name _Dbg_file) (.sh)}) ] spids: [478 491] ) spids: [471 476] ) (C {(source)} {($ VSub_Name "$top_dir") (/command/help.sh)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:_Dbg_args) op: Equal rhs: {(SQ )} spids: [500] ) ] spids: [500] ) (C {(_Dbg_do_help)} {(x)}) (C {(_Dbg_do_examine)} {(top_dir)}) ] spids: [-1 463] ) ] spids: [-1 515] ) ] )