# -*- shell-script -*- # # Copyright (C) 2002-2004, 2006, 2008-2009, 2012, 2015 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. setglobal _Dbg_ansi_term_bold = '"'" setglobal _Dbg_ansi_term_italic = '"'" setglobal _Dbg_ansi_term_underline = '"'" setglobal _Dbg_ansi_term_normal = '"'" # Called when a dangerous action is about to be done to make sure it's # okay. `prompt' is printed, and "yes", or "no" is solicited. The # user response is returned in variable $_Dbg_response and $? is set # to 0. _Dbg_response is set to 'error' and $? set to 1 on an error. # proc _Dbg_confirm { if sh-expr ' $# < 1 || $# > 2 ' { setglobal _Dbg_response = ''error'' return 0 } setglobal _Dbg_confirm_prompt = $1 typeset _Dbg_confirm_default = $(2:-'no') while : { if ! read $_Dbg_edit -p $_Dbg_confirm_prompt _Dbg_response args \ <&$_Dbg_input_desc !2 2>>$_Dbg_prompt_output { break } match $_Dbg_response { with 'y' | 'yes' | 'yeah' | 'ya' | 'ja' | 'si' | 'oui' | 'ok' | 'okay' setglobal _Dbg_response = ''y'' return 0 with 'n' | 'no' | 'nope' | 'nyet' | 'nein' | 'non' setglobal _Dbg_response = ''n'' return 0 with * if [[ $_Dbg_response =~ '^[ \t]*$' ]] { set +x return 0 } else { _Dbg_msg "I don't understand \"$_Dbg_response\"." _Dbg_msg "Please try again entering 'yes' or 'no'." setglobal _Dbg_response = '''' } } } } # Print an error message proc _Dbg_errmsg { typeset -r prefix = ''**'' if [[ -n $_Dbg_set_highlight ]] { _Dbg_msg "$prefix $(_Dbg_ansi_term_underline)$ifsjoin(Argv)$(_Dbg_ansi_term_normal)" } else { _Dbg_msg "$prefix $ifsjoin(Argv)" } } # Print an error message without the ending carriage return proc _Dbg_errmsg_no_cr { typeset -r prefix = ''**'' _Dbg_msg_no_cr "$prefix $ifsjoin(Argv)" } # print message to output device proc _Dbg_msg { if sh-expr ' _Dbg_logging ' { builtin echo -e @Argv >>$_Dbg_logfid } if sh-expr ' ! _Dbg_logging_redirect ' { if [[ -n $_Dbg_tty ]] && [[ $_Dbg_tty != '&1' ]] { builtin echo -e @Argv >>$_Dbg_tty } else { builtin echo -e @Argv } } } # print message to output device without a carriage return at the end proc _Dbg_msg_nocr { if sh-expr ' _Dbg_logging ' { builtin echo -n -e @Argv >>$_Dbg_logfid } if sh-expr ' ! _Dbg_logging_redirect ' { if [[ -n $_Dbg_tty ]] { builtin echo -n -e @Argv >>$_Dbg_tty } else { builtin echo -n -e @Argv } } } # print message to output device proc _Dbg_printf { _Dbg_printf_nocr @Argv _Dbg_msg '' } # print message to output device without a carriage return at the end proc _Dbg_printf_nocr { typeset format = $1 shift if sh-expr ' _Dbg_logging ' { builtin printf $format @Argv >>$_Dbg_logfid } if sh-expr ' ! _Dbg_logging_redirect ' { if [[ -n $_Dbg_tty ]] { builtin printf $format @Argv >>$_Dbg_tty } else { builtin printf $format @Argv } } } typeset _Dbg_dashes = ''---------------------------------------------------'' # print message to output device proc _Dbg_section { if [[ -n $_Dbg_set_highlight ]] { _Dbg_msg "$(_Dbg_ansi_term_bold)$ifsjoin(Argv)$(_Dbg_ansi_term_normal)" } else { var -r msg = @Argv _Dbg_msg "$msg\n$(_Dbg_dashes:0:${#msg})" } } proc _Dbg_msg_rst { var -r msg = @Argv if [[ -n $_Dbg_set_highlight ]] && sh-expr ' _Dbg_working_term_highlight ' { typeset opts = ""--rst --width=$_Dbg_set_linewidth"" typeset highlight_cmd = ""$(_Dbg_libdir)/lib/term-highlight.py"" typeset formatted_msg = '' setglobal formatted_msg = $[echo $msg | $highlight_cmd $opts] if sh-expr ' $? == 0 ' && [[ -n $formatted_msg ]] { _Dbg_msg $formatted_msg return } } _Dbg_msg $msg } # Common funnel for "Undefined command" message proc _Dbg_undefined_cmd { if sh-expr ' $# == 2 ' { _Dbg_errmsg "Undefined $1 subcommand \"$2\". Try \"help $1\"." } else { _Dbg_errmsg "Undefined command \"$1\". Try \"help\"." } }