# -*- shell-script -*- # pre.sh - Code common to bashdb and bashdb-trace that has to run first # # Copyright (C) 2002-2004, 2008-2012 # 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. # We put definitions common to both the script debugger and # dbg-trace.sh. In contrast to other routines, this code is sourced # early -- before most of the debugger script is run. # Note: initializations which are mostly used in only one sub-part # (e.g. variables for break/watch/actions) are in the corresponding # file: either in lib or (less good) command. # This function is overwritten by when lib/fns.sh gets loaded proc _Dbg_msg { echo >&2 $ifsjoin(Argv)> !2 $@ } # This function is overwritten by when lib/fns.sh gets loaded proc _Dbg_errmsg { echo >&2 "$(_Dbg_pname): $ifsjoin(Argv)> !2 "${_Dbg_pname}: $@" } # Used by "show version" as well as --version proc _Dbg_do_show_version { [[ -n $1 ]] && global label := $[_Dbg_printf_nocr "%-12s: " version] _Dbg_msg "$(label)$_Dbg_debugger_name, release $_Dbg_release" } # Expand filename given as $1. # we echo the expanded name or return $1 unchanged if a bad filename. # Return is 0 if good or 1 if bad. # File globbing is handled. # Note we don't check that the file exists, just that the format is # valid; we do check that we can "search" the directory implied in the # filename. proc _Dbg_expand_filename { typeset -r filename="$1" # Break out basename and dirname typeset basename="$(filename##*/)" typeset -x dirname="$(filename%/*)" # No slash given in filename? Then use . for dirname [[ $dirname == $basename ]] && [[ $filename != '/' ]] && global dirname := ''.'' # Dirname is ''? Then use / for dirname global dirname := $(dirname:-/) # Handle tilde expansion in dirname global dirname := $[echo $dirname] typeset long_path [[ $basename == '.' ]] && global basename := '''' if global long_path := $[ shell {cd $dirname ; pwd}] { if [[ "$long_path" == '/' ]] { echo "/$basename" } else { echo "$long_path/$basename" } return 0 } else { echo $filename return 1 } } # Create temporary file based on $1 # file $1 proc _Dbg_tempname { echo "$_Dbg_tmpdir/$(_Dbg_debugger_name)_$1_$Pid" } # Process command-line options source ${_Dbg_libdir}/init/opts.sh global OPTLIND := '1' _Dbg_parse_options @Argv if [[ ! -d $_Dbg_tmpdir ]] && [[ ! -w $_Dbg_tmpdir ]] { echo "$(_Dbg_pname): cannot write to temp directory $_Dbg_tmpdir." > !2 echo "$(_Dbg_pname): Use -T try directory location." > !2 exit 1 } # Save the initial working directory so we can reset it on a restart. typeset -x _Dbg_init_cwd=$PWD typeset -i _Dbg_running=1 # True we are not finished running the program typeset -i _Dbg_brkpt_num=0 # If nonzero, the breakpoint number that we # are currently stopped at. # Sets whether or not to display command before executing it. typeset _Dbg_set_trace_commands='off' # Known normal IFS consisting of a space, tab and newline typeset -x _Dbg_space_IFS=' ' # Number of statements to run before entering the debugger. Is used # intially to get out of sourced dbg-main.inc script and in top-level # debugger script to not stop in remaining debugger statements before # the sourcing the script to be debugged. typeset -i _Dbg_step_ignore=1 (CommandList children: [ (FuncDef name: _Dbg_msg body: (BraceGroup children: [ (SimpleCommand words: [{(echo)} {($ VSub_At "$@")}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[93])] ) ] spids: [88] ) spids: [84 87] ) (FuncDef name: _Dbg_errmsg body: (BraceGroup children: [ (SimpleCommand words: [{(echo)} {(DQ (${ VSub_Name _Dbg_pname) (": ") ($ VSub_At "$@"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[113])] ) ] spids: [108] ) spids: [104 107] ) (FuncDef name: _Dbg_do_show_version body: (BraceGroup children: [ (AndOr children: [ (DBracket expr: (BoolUnary op_id:BoolUnary_n child:{($ VSub_Number "$1")}) ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:label) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(_Dbg_printf_nocr)} {(DQ ("%-12s: "))} {(version)})] ) left_token: spids: [149 157] ) } spids: [148] ) ] spids: [148] ) ] op_id: Op_DAmp ) (C {(_Dbg_msg)} { (DQ (${ VSub_Name label) ($ VSub_Name "$_Dbg_debugger_name") (", release ") ($ VSub_Name "$_Dbg_release") ) } ) ] spids: [135] ) spids: [131 134] ) (FuncDef name: _Dbg_expand_filename body: (BraceGroup children: [ (C {(typeset)} {(-r)} {(Lit_VarLike "filename=") (DQ ($ VSub_Number "$1"))}) (C {(typeset)} {(Lit_VarLike "basename=") (DQ (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_DPound arg_word:{("*") (Lit_Slash /)}) spids: [222 227] ) ) } ) (C {(typeset)} {(-x)} {(Lit_VarLike "dirname=") (DQ (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [237 242] ) ) } ) (AndOr children: [ (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$dirname")} right: {($ VSub_Name "$basename")} ) ) (AndOr children: [ (DBracket expr: (BoolBinary op_id: BoolBinary_GlobNEqual left: {($ VSub_Name "$filename")} right: {(SQ )} ) ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:dirname) op: Equal rhs: {(SQ <.>)} spids: [277] ) ] spids: [277] ) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:dirname) op: Equal rhs: { (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_ColonHyphen arg_word:{(Lit_Slash /)}) spids: [289 293] ) } spids: [288] ) ] spids: [288] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:dirname) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(echo)} {($ VSub_Name "$dirname")})] ) left_token: spids: [302 306] ) } spids: [301] ) ] spids: [301] ) (C {(typeset)} {(long_path)}) (AndOr children: [ (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$basename")} right: {(SQ <.>)} ) ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:basename) op:Equal rhs:{(SQ )} spids:[330])] spids: [330] ) ] op_id: Op_DAmp ) (If arms: [ (if_arm cond: [ (Sentence child: (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:long_path) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Subshell child: (CommandList children: [ (Sentence child: (C {(cd)} {(DQ ($ VSub_Name "$dirname"))}) terminator: ) (C {(pwd)}) ] ) redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [352] ) ] spids: [340 350] ) ] ) left_token: spids: [338 355] ) } spids: [337] ) ] spids: [337] ) terminator: ) ] action: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ ($ VSub_Name "$long_path"))} right: {(SQ )} ) ) terminator: ) ] action: [(C {(echo)} {(DQ (/) ($ VSub_Name "$basename"))})] spids: [-1 380] ) ] else_action: [ (C {(echo)} {(DQ ($ VSub_Name "$long_path") (/) ($ VSub_Name "$basename"))}) ] spids: [391 403] ) (ControlFlow token: arg_word:{(0)}) ] spids: [-1 359] ) ] else_action: [ (C {(echo)} {($ VSub_Name "$filename")}) (ControlFlow token: arg_word:{(1)}) ] spids: [411 424] ) ] spids: [200] ) spids: [196 199] ) (FuncDef name: _Dbg_tempname body: (BraceGroup children: [ (C {(echo)} { (DQ ($ VSub_Name "$_Dbg_tmpdir") (/) (${ VSub_Name _Dbg_debugger_name) (_) ($ VSub_Number "$1") (_) ($ VSub_Dollar "$$") ) } ) ] spids: [439] ) spids: [435 438] ) (C {(.)} {(${ VSub_Name _Dbg_libdir) (/init/opts.sh)}) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:OPTLIND) op:Equal rhs:{(1)} spids:[469])] spids: [469] ) (C {(_Dbg_parse_options)} {(DQ ($ VSub_At "$@"))}) (If arms: [ (if_arm cond: [ (Sentence child: (AndOr children: [ (DBracket expr: (LogicalNot child: (BoolUnary op_id:BoolUnary_d child:{($ VSub_Name "$_Dbg_tmpdir")}) ) ) (DBracket expr: (LogicalNot child: (BoolUnary op_id:BoolUnary_w child:{($ VSub_Name "$_Dbg_tmpdir")}) ) ) ] op_id: Op_DAmp ) terminator: ) ] action: [ (SimpleCommand words: [ {(echo)} { (DQ (${ VSub_Name _Dbg_pname) (": cannot write to temp directory ") ($ VSub_Name "$_Dbg_tmpdir") (.) ) } ] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[519])] ) (SimpleCommand words: [{(echo)} {(DQ (${ VSub_Name _Dbg_pname) (": Use -T try directory location."))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[532])] ) (C {(exit)} {(1)}) ] spids: [-1 505] ) ] spids: [-1 540] ) (C {(typeset)} {(-x)} {(Lit_VarLike "_Dbg_init_cwd=") ($ VSub_Name "$PWD")}) (C {(typeset)} {(-i)} {(Lit_VarLike "_Dbg_running=") (1)}) (C {(typeset)} {(-i)} {(Lit_VarLike "_Dbg_brkpt_num=") (0)}) (C {(typeset)} {(Lit_VarLike "_Dbg_set_trace_commands=") (SQ )}) (C {(typeset)} {(-x)} {(Lit_VarLike "_Dbg_space_IFS=") (SQ <" \n">)}) (C {(typeset)} {(-i)} {(Lit_VarLike "_Dbg_step_ignore=") (1)}) ] )