OSDN Git Service

first commit
[bashlib/bashlib-develop.git] / scriptlib / bashlib.sh
1 #// Content-Type: text/plain; charset=utf-8
2
3 #// bashlib is provided under 3-clause BSD license.
4 #// Copyright (C) 2011 Sofrware Design Gallery "Sage Plaisir 21" All Rights Reserved.
5
6
7  
8 #//*********************************************************************
9 #// <<< [declare_associative_array] >>> 
10 #//*********************************************************************
11 if [ "${BASH_VERSINFO[0]}" -ge "4" ];then
12   declare_associative_array="declare -A"  #// bash ver4
13 else
14   declare_associative_array="declare"  #// bash ver3
15 fi
16
17
18  
19 #//*********************************************************************
20 #// <<< [CallMain_func] >>> 
21 #//*********************************************************************
22 function  CallMain_func()
23 {
24   local  is_not_err_handled=0
25
26   trap 'set +x ; ErrTrap_func' EXIT
27   trap 'set +x ; ErrTrap_func $LINENO ;  break' ERR  #// In function, it is necessary to bash -E option
28   trap 'DebugTrap_func  "$LINENO"  "$BASH_COMMAND"  "${PIPESTATUS[@]}"
29     #// resume ${PIPESTATUS[@]}
30     case "${#g_PipeStatus[@]}" in
31       "2")
32         return ${g_PipeStatus[0]} | true;;
33       "3")
34         return ${g_PipeStatus[0]} | return ${g_PipeStatus[1]} | true;;
35     esac' DEBUG
36   set +e
37
38   while TryStart_func; do
39
40     Main_func  ""  "AppKey4293"
41
42     g_DebugTrapFunc=""
43     if [ "$g_ExitStatus" != "0" ];then  is_not_err_handled=1  ;fi
44   TryEnd1_func; done ;TryEnd2_func $?
45   g_DebugTrapFunc=""
46
47   if [ "$g_ExitStatus" != "0" ];then
48     if [ "$g_Err_Desc" == "" ];then
49       ColorText_func  "<ERROR/>"  "Red" "Bold"
50     else
51       ColorText_func  "$g_Err_Desc"  "Red" "Bold"
52     fi
53     echo_e_func  "$g_Ret" >&2
54     echo "Exit Status = $g_ExitStatus"  >&2
55     echo_line_func
56     echo  "$g_Err_ErrCallStack"  >&2
57     if [ "$g_Err_Desc" == "" ];then
58       echo  "<ERROR/>"  >&2
59     else
60       echo  "$g_Err_Desc"  >&2
61     fi
62     if [ "$is_not_err_handled" == "1" ];then
63       echo  "エラー処理がされていません。ErrClass.clear_method または ErrClass.raiseOverwrite_method を呼び出してください"
64     fi
65     trap ':' EXIT
66     exit  "$g_ExitStatus"
67   fi
68   trap ':' EXIT
69 }
70
71
72  
73 #//*********************************************************************
74 #// <<< [DebugTrap_func] >>> 
75 #//*********************************************************************
76 function  DebugTrap_func()
77 {
78   if [ "$g_DebugTrapFunc" == "" ];then
79     shift  2
80     g_PipeStatus=( "$@" )
81   else
82     $g_DebugTrapFunc  "$@"
83   fi
84 }
85
86
87  
88 #//*********************************************************************
89 #// <<< [EchoOn_func] >>> 
90 #//*********************************************************************
91 function  EchoOn_func()
92 {
93   echo  "${BASH_LINENO[0]}: EchoOn_func at ${FUNCNAME[1]}() in ${BASH_SOURCE[1]}"
94
95   g_DebugTrapFunc="EchoOnTrap_func"
96
97   trap 'DebugTrap_func  "$LINENO"  "$BASH_COMMAND"  "${PIPESTATUS[@]}"
98     #// resume ${PIPESTATUS[@]}
99     case "${#g_PipeStatus[@]}" in
100       "2")
101         return ${g_PipeStatus[0]} | true;;
102       "3")
103         return ${g_PipeStatus[0]} | return ${g_PipeStatus[1]} | true;;
104     esac' DEBUG
105 }
106
107 function  EchoOnTrap_func()
108 {
109   local  LineNo="$1"
110   local  Command="$2"
111   shift  2
112   g_PipeStatus=( "$@" )
113
114   echo "$LineNo: $Command" >&2
115 }
116
117
118  
119 #//*********************************************************************
120 #// <<< [EchoOff_func] >>> 
121 #//*********************************************************************
122 function  EchoOff_func()
123 {
124   g_DebugTrapFunc=""
125 }
126
127
128  
129 #//*********************************************************************
130 #// <<< [echo_line_func] >>> 
131 #//*********************************************************************
132 function  echo_line_func()
133 {
134   echo "-------------------------------------------------------------------------------"
135 }
136
137
138  
139 #//*********************************************************************
140 #// <<< [Pause_func] >>> 
141 #//*********************************************************************
142 function  Pause_func()
143 {
144   local  Param="$1"
145   local  key
146   local  sec
147
148   if [ "$Param" != "" ];then
149     local  arguments
150     $declare_associative_array  option
151
152     GetLongOptions_func  arguments  option  "$@"  #//[out] arguments, option
153     Attr_func  option  "time_out" ; sec="$g_Ret"
154   fi
155
156   if [ "$sec" == "" ];then
157     read -p "続行するには Enter キーを押してください . . ."  key
158   else
159     for (( sec = $sec; sec >=0; sec -- )) ;do
160       echo -e -n "\r"
161       if [ "$sec" == "0" ];then
162         echo  "続行するには Enter キーを押してください . . .  "
163         break
164       fi
165
166       read -p "続行するには Enter キーを押してください . . . $sec " -t 1  key || key="...."
167       if [ "$key" == "" ];then  break  ;fi
168     done ; done_func $?
169   fi
170 }
171
172
173  
174 #//*********************************************************************
175 #// <<< [ColorText_func] >>> 
176 #//*********************************************************************
177 function  ColorText_func()
178 {
179   local  Text="$1"
180   shift  1
181   local  ColorNames=("$@")
182   local  i
183   local  n
184   local  str
185
186   #//=== initialize  g_ColorText_Codes
187   Attr_func  g_ColorText_Codes  "Black"
188   if [ "$g_Ret" == "" ];then
189
190     #//=== set escape sequence
191     SetAttr_func  g_ColorText_Codes  "Black"    30
192     SetAttr_func  g_ColorText_Codes  "Red"      31
193     SetAttr_func  g_ColorText_Codes  "Green"    32
194     SetAttr_func  g_ColorText_Codes  "Yellow"   33
195     SetAttr_func  g_ColorText_Codes  "Blue"     34
196     SetAttr_func  g_ColorText_Codes  "Magenta"  35
197     SetAttr_func  g_ColorText_Codes  "Cyan"     36
198     SetAttr_func  g_ColorText_Codes  "White"    37
199
200     SetAttr_func  g_ColorText_Codes  "BlackBack"    40
201     SetAttr_func  g_ColorText_Codes  "RedBack"      41
202     SetAttr_func  g_ColorText_Codes  "GreenBack"    42
203     SetAttr_func  g_ColorText_Codes  "YellowBack"   43
204     SetAttr_func  g_ColorText_Codes  "BlueBack"     44
205     SetAttr_func  g_ColorText_Codes  "MagentaBack"  45
206     SetAttr_func  g_ColorText_Codes  "CyanBack"     46
207     SetAttr_func  g_ColorText_Codes  "WhiteBack"    47
208
209     SetAttr_func  g_ColorText_Codes  "Bold"  1
210   fi
211
212   str="\e["
213   n=$(( ${#ColorNames[@]} - 1 ))
214   for (( i = 0;  i <= n;  i++ )) ;do
215     Attr_func  g_ColorText_Codes  "${ColorNames[$i]}"
216     if [ "$i" == "$n" ];then
217       str="${str}${g_Ret}m"
218     else
219       str="${str}${g_Ret};"
220     fi
221   done ; done_func $?
222
223   g_Ret="${str}${Text}\e[m"
224 }
225
226 $declare_associative_array  g_ColorText_Codes
227
228
229  
230 #//*********************************************************************
231 #// <<< [echo_e_func] >>>
232 #//*********************************************************************
233 function  echo_e_func()
234 {
235   local  Text="$@"
236   eval echo '$'"'$Text'"
237 }
238
239
240  
241 #//*********************************************************************
242 #// <<< [Input_func] >>> 
243 #//*********************************************************************
244 function  Input_func()
245 {
246   local  Prompt="$1"
247   local  key
248   local  ifs_bk
249
250   if [ "$Prompt" == "" ];then  Prompt=">"  ;fi
251
252   if [ "$g_AutoInput_ArgsNextIndex" -lt "${#g_AutoInput_Args[@]}" ];then
253     key="${g_AutoInput_Args[$g_AutoInput_ArgsNextIndex]}"
254     g_Ret="$key"
255     echo  "$Prompt$key"
256     g_AutoInput_ArgsNextIndex=$(( $g_AutoInput_ArgsNextIndex + 1 ))
257   else
258     ifs_bk="$IFS"
259     IFS="\\"  #// This can input space or tab at the end of string
260     read -p "$Prompt" -r g_Ret
261     IFS="$ifs_bk"
262   fi
263 }
264
265
266  
267 #//*********************************************************************
268 #// <<< [SetAutoInputFromMainArg_func] >>> 
269 #//*********************************************************************
270 g_AutoInput_Args=( )
271 g_AutoInput_ArgsNextIndex=0
272
273 function  SetAutoInputFromMainArg_func()
274 {
275   g_AutoInput_Args=( "${g_Arguments[@]}" )
276   g_AutoInput_ArgsNextIndex=1
277 }
278
279
280  
281 #//*********************************************************************
282 #// <<< [InputPath_func] >>> 
283 #//*********************************************************************
284 function  InputPath_func()
285 {
286   local  Prompt="$1"
287   local  input_path
288   local  base_path
289   local  is_file_exist_opt
290   local  is_folder_exist_opt
291   local  tab
292   local  len
293   local  arguments
294   $declare_associative_array  options
295
296   GetLongOptions_func  arguments  options  "$@"  #//[out] arguments, options
297
298
299   while true; do
300     Input_func  "$Prompt$base_path" ; input_path="$g_Ret"
301
302     if [ "$base_path$input_path" == "" ]; then
303       Attr_func  options  "AllowEnterOnly"
304       if [ "$g_Ret" == "1" ]; then
305         g_Ret=""
306         return  0
307       fi
308
309     else
310
311       StringClass.right_method  "$input_path"  1
312       if [ "$g_Ret" == "$Tab" ];then
313
314         StringClass.length_method  "$input_path"
315         len=$(( $g_Ret - 1 ))
316         StringClass.substring_method  "$input_path"  0  "$len" ; input_path="$g_Ret"
317         InputPathSelectSub_func  "$input_path"  "$base_path"  base_path  #//[out] base_path
318
319       else
320
321         if [ "$base_path" != "" ];then
322           input_path="$base_path$input_path"
323         fi
324         StringClass.substring_method  "$input_path"  0  1
325         if [ "$g_Ret" == "~" ];then
326           StringClass.replace_method  "$input_path"  "~"  "$HOME" ; input_path="$g_Ret"
327         fi
328         eval "input_path=\"$input_path\""
329         GetAbsPath_func  "$input_path"  "$g_StartInPath" ; input_path="$g_Ret"
330
331         Attr_func  options  "ChkFileExists" ; is_file_exist_opt="$g_Ret"
332         Attr_func  options  "ChkFolderExists" ; is_folder_exist_opt="$g_Ret"
333
334         if [ x"$is_file_exist_opt" == x""  -a  x"$is_folder_exist_opt" == x"" ];then
335           g_Ret="$input_path"
336           return
337         fi
338
339         if [ "$is_file_exist_opt" == "1" ];then
340           if [ -f "$input_path" ]; then
341             g_Ret="$input_path"
342             return  0
343           fi
344         fi
345
346         if [ "$is_folder_exist_opt" == "1" ];then
347           if [ -d "$input_path" ]; then
348             g_Ret="$input_path"
349             return  0
350           fi
351         fi
352
353         if [ "$is_file_exist_opt" == "1" ];then
354           if [ "$is_folder_exist_opt" == "1" ];then
355             echo  "ファイルまたはフォルダーが見つかりません。" >&2
356           else
357             echo  "ファイルが見つかりません。" >&2
358           fi
359         else
360           echo  "フォルダーが見つかりません。" >&2
361         fi
362
363         echo  "$input_path" >&2
364       fi
365     fi
366   done ; done_func $?
367 }
368
369 function  InputPathSelectSub_func()
370 {
371   local  PartOfPath="$1"
372   local  BasePath="$2"
373   local  out_SelectedPath="$3"
374   local  folder
375   local  paths
376   local  paths2
377   local  i
378   local  path
379   local  part_of_file
380   local  len
381   local  num
382   local  new_base_path
383
384   if [ "$BasePath" != "" ];then
385     PartOfPath="$BasePath$PartOfPath"
386   fi
387   LeftOfLastStr_func  "$PartOfPath"  "/" ; folder="$g_Ret"
388   if [ x"$folder" == x""  -o  x"$folder" == x"$PartOfPath" ];then
389     folder="."
390   else
391     new_base_path="$folder/"
392   fi
393   StringClass.substring_method  "$folder"  0  1
394   if [ "$g_Ret" == "~" ];then
395     StringClass.replace_method  "$folder"  "~"  "$HOME" ; folder="$g_Ret"
396   fi
397   eval "folder=\"$folder\""
398
399   RightOfLastStr_func  "$PartOfPath"  "/" ; part_of_file="$g_Ret"
400
401   unset paths
402   pushd  "$g_StartInPath" > /dev/null
403   paths=`ls -p1A "$folder"`
404   popd  > /dev/null
405   ArrayClass.fromLines_method  paths  "$paths"  #//[out] paths
406
407   i=0
408   StringClass.length_method  "$part_of_file" ; len="$g_Ret"
409   for path  in "${paths[@]}" ;do
410     StringClass.substring_method  "$path"  0  "$len"
411     if [ "$g_Ret" == "$part_of_file" ];then
412       paths2[$i]="$path"
413       i=$(( $i + 1 ))
414     fi
415   done ; done_func $?
416
417   if [ "$PartOfPath" == "" ];then
418     echo  "$g_StartInPath/"
419   else
420     GetAbsPath_func  "$PartOfPath"  "$g_StartInPath"
421     echo  "$g_Ret"
422   fi
423   for (( i = 0; i < ${#paths2[@]}; i ++ ));do
424     echo -n "$(( $i + 1 )). ${paths2[$i]}   "
425   done ; done_func $?
426   echo -n "88. クリア   99. 戻る"
427
428   while true; do
429     Input_func  "  >" ; num="$g_Ret"
430     if [ "$num" == "88" ];then
431       SetOutput_func  "$out_SelectedPath"  ""
432       break
433     elif [ x"$num" == x"99"  -o  x"$num" == x"" ];then
434       SetOutput_func  "$out_SelectedPath"  "$BasePath"
435       break
436     elif [ "$num" -le "$i"  -a  "$num" -ge "0" ];then
437       if [ "$new_base_path" == "" ];then
438         SetOutput_func  "$out_SelectedPath"  "${paths2[$(( $num - 1 ))]}"
439       else
440         SetOutput_func  "$out_SelectedPath"  "$new_base_path${paths2[$(( $num - 1 ))]}"
441       fi
442       break
443     fi
444   done ; done_func $?
445 }
446
447
448  
449 #//*********************************************************************
450 #// <<< [InputCommand_func] >>> 
451 #//*********************************************************************
452 function  InputCommand_func()
453 {
454   local  self="$1"
455   local  Prompt="$2" ; if [ "$Prompt" == "" ];then  Prompt="番号またはコマンド >"  ;fi
456   local  Opt="$3"
457   local  AppKey="$4"
458
459   local  replaces
460   local  key
461   local  num
462   local  func
463   local  is_prompt
464   local  current_folder=`pwd -P`
465   local  captions
466   local  caption
467   local  a1
468
469
470   SetAutoInputFromMainArg_func
471   if [ "${#g_Arguments[@]}" == "1" ];then  is_prompt=1  ;else  is_prompt=0  ;fi
472
473   Attr_func  $self  CommandReplace ; replaces="$g_Ret"
474   Attr_func  $self  MenuCaption ; captions="$g_Ret"
475
476   while true; do
477
478     #//=== show menu
479     if [ "$is_prompt" != "0" ];then
480       echo_line_func
481       echo  `pwd -P`">"
482       Attr_func  $self  Lead
483       echo  "$g_Ret"
484
485       AssociativeArrayClass.getKeys_method "$replaces" ; a1="$g_Ret"
486       for key  in $a1 ;do
487         if IsNumeric_func $key; then
488           Attr_func  $captions  $key ; caption="$g_Ret"
489           if [ "$caption" == "" ];then
490             Attr_func  $replaces  $key
491             echo  "$key. $g_Ret"
492           else
493             echo  "$key. $caption"
494           fi
495         fi
496       done ; done_func $?
497       echo  "99. 終了"
498
499       while true; do
500         Input_func  "$Prompt" ; num="$g_Ret"
501         if [ "$num" != "" ];then  break  ;fi
502       done ; done_func $?
503       echo_line_func
504     else
505       while true; do
506         Input_func  "$Prompt" ; num="$g_Ret"
507         if [ "$num" != "" ];then  break  ;fi
508       done ; done_func $?
509     fi
510     if [ "$num" == "99" ];then
511       a1=`basename "${g_Arguments[0]}"`
512       echo  "(ヒント)$a1 のパラメーターに、入力する内容を指定できます。"
513       break
514     fi
515
516     #//=== call
517     Attr_func  $replaces  "$num" ; func="$g_Ret"
518     if [ "$func" != "" ];then  num="$func" ; Attr_func  $replaces  $func ; func="$g_Ret"  ;fi
519     if [ "$func" == "" ];then  func="$num"  ;fi
520     echo  " ((( $func )))"
521
522     if [ "$is_prompt" != "0" ];then
523       while TryStart_func; do
524         "$func"  "$Opt"  "$AppKey"
525       TryEnd1_func; done ;TryEnd2_func $?
526       EchoOff_func
527       if [ "$g_ExitStatus" != "0" ];then
528         echo  "Exit Status = $g_ExitStatus"
529         if [ "$g_Err_Desc" == "" ];then
530           ColorText_func  "<ERROR/>"  "Red" "Bold"
531         else
532           ColorText_func  "$g_Err_Desc"  "Red" "Bold"
533         fi
534         echo_e_func  "$g_Ret"  >&2
535         if [ "$g_Err_Desc" == "" ];then
536           echo  "${g_Err_ErrCallStack}<ERROR/>"  >&2
537         else
538           echo  "${g_Err_ErrCallStack}${g_Err_Desc}"  >&2
539         fi
540         if [ "$is_not_err_handled" == "1" ];then
541           echo  "エラー処理がされていません。ErrClass.clear_method または ErrClass.raiseOverwrite_method を呼び出してください"  >&2
542         fi
543       fi
544       ErrClass.clear_method
545     else
546       "$func"  "$Opt"  "$AppKey"
547       break
548     fi
549     cd  "$current_folder"
550   done ; done_func $?
551 }
552
553
554  
555 #//*********************************************************************
556 #// <<< [sudo_func] >>> 
557 #//*********************************************************************
558 function  sudo_func()
559 {
560   g_TemporarySudo="sudo"
561   "$@"
562   unset  g_TemporarySudo
563 }
564
565
566  
567 #//*********************************************************************
568 #// <<< [mkdir_for_it_func] >>> 
569 #//*********************************************************************
570 function  mkdir_for_it_func()
571 {
572   local  FilePath="$1"
573   CheckArgCount_func  1 "$@"
574
575   folder_path=${FilePath%/*}  #// parent folder
576   if [ "$folder_path" == "$FilePath" ]; then  return 0  ;fi  #// if not found "/"
577   if [ ! -f "$folder_path" ]; then  mkdir -p  "$folder_path"  ;fi
578 }
579
580
581  
582 #//*********************************************************************
583 #// <<< [GetAbsPath_func] >>> 
584 #//*********************************************************************
585 function  GetAbsPath_func()
586 {
587   local  StepPath="$1"
588   local  BasePath="$2"
589   CheckMinArgCount_func  1 "$@"
590   CheckMaxArgCount_func  2 "$@"
591
592   local  abs_path
593   local  str
594
595   LeftOfStr_func "$StepPath" "/" ; str="$g_Ret"  #// if abs path, str=""
596   if [ "$str" == "" ]; then
597     g_Ret="$StepPath"
598   elif [ "$str" == "~" ]; then
599     StringClass.substring_method  "$StepPath"  1
600     g_Ret="$HOME$g_Ret"
601   else
602     if [ "$BasePath" == "" ];then  BasePath="$PWD"  ;fi
603
604     abs_path="$BasePath/$StepPath"
605
606     while true; do   #//  "*/../" -> ""
607       echo  "$abs_path" | grep  "[^/]*/\.\./" > /dev/null  || break
608       abs_path=`echo "$abs_path" | sed -e "s%[^/]*/\.\./%%"`
609     done ; done_func $?
610
611     while true; do   #//  "/*/.." -> ""
612       echo  "$abs_path" | grep  "[^/]*/[^/]*/\.\." > /dev/null  || break
613       abs_path=`echo "$abs_path" | sed -e "s%/[^/]*/\.\.$%%"`
614     done ; done_func $?
615
616     while true; do  #//  "/./" -> "/"
617       echo  "$abs_path" | grep  "/\./" > /dev/null  || break
618       StringClass.replace_method  "$abs_path"  "/./"  "/" ; abs_path="$g_Ret"
619     done ; done_func $?
620
621     while true; do  #//  "/." -> ""
622       StringClass.right_method  "$abs_path"  2
623       if [ "$g_Ret" != "/." ];then  break  ;fi
624       StringClass.replace_method  "$abs_path"  "/./"  "/" ; abs_path="$g_Ret"
625       abs_path=`echo "$abs_path" | sed -e "s%/\.$%%"`
626     done ; done_func $?
627
628     g_Ret="$abs_path"
629   fi
630 }
631
632
633  
634 #//*********************************************************************
635 #// <<< [GetParentAbsPath_func] >>> 
636 #//*********************************************************************
637 function  GetParentAbsPath_func()
638 {
639   local  Path="$1"
640   CheckArgCount_func  1 "$@"
641
642   local  str
643
644   LeftOfStr_func "$Path" "/" ; str="$g_Ret"  #// if abs path, str=""
645   if [ "$str" != "" ]; then
646     GetAbsPath_func "$Path" ; Path="$g_Ret"
647   fi
648
649   if [ x"$Path" == x"/"  -o  x"$Path" == x"" ]; then
650     g_Ret="$Path"
651   else
652     RightOfLastStr_func "$Path" "/"  #// if last char is "/", str=""
653     if [ "$g_Ret" == "" ]; then
654       LeftOfLastStr_func "$Path" "/" ; Path="$g_Ret"
655     fi
656     LeftOfLastStr_func  "$Path"  "/" ; Path="$g_Ret"  #// parent folder
657     if [ "$Path" == "" ];then  Path="/"  ;fi
658     g_Ret="$Path"
659   fi
660 }
661
662
663  
664 #//*********************************************************************
665 #// <<< [SearchParent_func] >>>
666 #//*********************************************************************
667 function  SearchParent_func()
668 {
669   local  path="$1"
670   local  folder
671   local  file
672
673   GetAbsPath_func  "$path"  "$PWD" ; path="$g_Ret"
674   if [ -e "$path" ];then  g_Ret="$path" ; return ;fi
675
676   folder=`dirname  "$path"`
677   file=` basename  "$path"`
678   while true; do
679     folder=`dirname  "$folder"`
680     if [ "$folder" == "/" ];then  break  ;fi
681     if [ -e "$folder/$file" ];then  g_Ret="$folder/$file" ; return ;fi
682   done ; done_func $?
683   if [ -e "/$file" ];then  g_Ret="/$file" ; return ;fi
684   g_Ret=""
685 }
686
687
688  
689 #//*********************************************************************
690 #// <<< [local_func] >>> 
691 #//*********************************************************************
692 function  local_func()
693 {
694   local  arguments
695   $declare_associative_array  option
696   local  args
697   local  locals
698   local  param
699   local  name
700   local  code
701   local  arg_num=0
702   local  out_names=( )
703   local  a1
704   local  a2
705
706   GetLongOptions_func  arguments  option  "$@"  #//[out] arguments, option
707
708   Attr_func  option  "arg"
709   ArrayClass.fromCSV_method  args  "$g_Ret"  #//[out] args
710
711   Attr_func  option  "local"
712   ArrayClass.fromCSV_method  locals  "$g_Ret"  #//[out] locals
713
714   for param  in ${args[@]}  ;do
715     StringClass.substring_method  "$param"  0 5 ; a1="$g_Ret"
716     StringClass.substring_method  "$param"  0 8 ; a2="$g_Ret"
717     if [ "$a1" == "(out)" ];then 
718       StringClass.substring_method  "$param"  5 ; param="$g_Ret"
719       out_names=( "${out_names[@]}" "${arguments[$arg_num]}" )
720     elif [ "$a2" == "(in_out)" ];then
721       StringClass.substring_method  "$param"  8 ; param="$g_Ret"
722       out_names=( "${out_names[@]}" "${arguments[$arg_num]}" )
723     fi
724     arg_num=$(( $arg_num + 1 ))
725     code="$code  local  $param=\""'$'"$arg_num\";"
726   done ; done_func $?
727
728   for param  in "${locals[@]}"  ;do
729     code="$code  local  $param;"
730   done ; done_func $?
731
732   locals=( "${args[@]}"  "${locals[@]}" )
733
734   for name  in ${out_names[@]} ;do
735     for param  in "${locals[@]}" ;do
736       if [ "$name" == "$param" ];then
737         Error_func  "<ERROR msg=\"出力引数がローカル変数名と衝突しています\" name=\"$name\"/>"
738       fi
739     done ; done_func $?
740   done ; done_func $?
741
742   echo  "$code"
743 }
744
745
746  
747 #//*********************************************************************
748 #// <<< [GetUsableCommands_func] >>> 
749 #//*********************************************************************
750 function  GetUsableCommands_func()
751 {
752   local  out_FoundCommands="$1"
753   shift  1
754   local  Commands=( "$@" )
755   local  command
756   local  is_found
757   local  founds=( )
758
759   for command  in ${Commands[@]} ;do
760     while TryStart_func; do
761       which $command > /dev/null
762     TryEnd1_func; done ;TryEnd2_func $?
763     if [ "$g_ExitStatus" == "0" ];then  founds=( "${founds[@]}"  "$command " )  ;fi
764     ErrClass.clear_method
765   done ; done_func $?
766   SetOutputAsArray_func  $out_FoundCommands  "${founds[@]}"
767 }
768
769
770  
771 #//*********************************************************************
772 #// <<< [GetUsableApplicationsForMac_func] >>> 
773 #//*********************************************************************
774 function  GetUsableApplicationsForMac_func()
775 {
776   local  code ; code=`\
777   local_func \
778     --arg="  (out)out_FoundCommands "  \
779     --local=" command__, commands__, apps__, app__, path__, file_name__, paths__, i__ " \
780     "$@" ` ; eval  "$code"
781     #// --arg="Applications ..."
782   apps__=( )
783   shift  1
784   local  Applications=( "$@" )
785
786   #// list up applications
787   while TryStart_func; do
788     paths__=`ls /Applications/*.app /Applications/Utilities/*.app 2>/dev/null | grep :$`
789   TryEnd1_func; done ;TryEnd2_func $?
790   if [ "$g_ExitStatus" != "0" ]; then
791     unset  $out_FoundCommands
792     unset  $out_FoundCommands  #// 配列をクリアするには2回必要?
793     ErrClass.clear_method
794     return  0
795   fi
796   ArrayClass.fromLines_method  paths__  "$paths__"  #//[out] paths__
797
798   i__=0
799   for path__  in "${paths__[@]}" ;do
800     file_name__=`basename "$path__"`
801     LeftOfLastStr_func  "$file_name__"  ".app:" ; apps__[$i__]="$g_Ret"
802     i__=$(( $i__ + 1 ))
803   done ; done_func $?
804
805   if [ "${#Applications[@]}" == "0" ];then
806     for (( i__ = 0; i__ < ${#apps__[@]}; i__ ++ )) ;do
807       apps__[$i__]="open -a \"$apps__[$i__]\" "
808     done ; done_func $?
809     SetOutputAsArray_func  $out_FoundCommands  "${apps__[@]}"
810     return  0
811   fi
812
813   #// pick up applications
814   i__=0
815   for command__  in "${Applications[@]}" ;do
816     for app__  in "${apps__[@]}" ;do
817       if [ "$command__" == "$app__" ];then
818         commands__[$i__]="open -a \"$app__\" "
819         i__=$(( $i__ + 1 ))
820         break
821       fi
822     done ; done_func $?
823   done ; done_func $?
824   SetOutputAsArray_func  $out_FoundCommands  "${commands__[@]}"
825 }
826
827
828  
829 #//*********************************************************************
830 #// <<< [GetLongOptions_func] >>> 
831 #//*********************************************************************
832 function  GetLongOptions_func()
833 {
834   local  out_Arguments="$1"
835   local  out_OptAssocArray="$2"
836   shift  2
837   local  Args__=("$@")
838   local  param__
839   local  name__
840   local  value__
841   local  ArgIndex__=0
842   local  locals="out_Arguments  out_OptAssocArray  Args__  param__  name__  value__"
843
844   CheckOutParamIsConflictToLocal_func  "$out_Arguments"      $locals
845   CheckOutParamIsConflictToLocal_func  "$out_OptAssocArray"  $locals
846
847   for param__  in "${Args__[@]}" ;do
848     StringClass.substring_method  "$param__"  0  2
849     if [ "$g_Ret" == "--" ];then
850       LeftOfStr_func  "$param__"  "=" ; name__="$g_Ret"
851       if [ "$name__" == "$param__" ];then
852         value__="1"
853       else
854         RightOfStr_func  "$param__"  "=" ; value__="$g_Ret"
855       fi
856       StringClass.substring_method  "$name__"  2 ; name__="$g_Ret"
857
858       SetAttr_func  $out_OptAssocArray  $name__  "$value__"
859     else
860       SetArrItem_func  $out_Arguments  $ArgIndex__  "$param__"
861       ArgIndex__=$(( $ArgIndex__ + 1 ))
862     fi
863   done ; done_func $?
864 }
865
866
867  
868 #//*********************************************************************
869 #// <<< [Attr_func] >>> 
870 #//*********************************************************************
871 function  Attr_func()
872 {
873   local  self="$1"
874   local  AttrName="$2"
875   local  tmp
876   CheckArgCount_func  2 "$@"
877
878   eval  tmp="\${${self}[\$AttrName]}"
879   g_Ret="$tmp"
880 }
881
882
883  
884 #//*********************************************************************
885 #// <<< [SetArrItem_func] >>> 
886 #//*********************************************************************
887 function  SetArrItem_func()
888 {
889   local  self__="$1"
890   local  Index__="$2"
891   local  Value__="$3"
892   CheckArgCount_func  3 "$@"
893
894   eval  "$self__[$Index__]=\"$Value__\""
895 }
896
897
898  
899 #//*********************************************************************
900 #// <<< [SetAttr_func] >>> 
901 #//*********************************************************************
902 function  SetAttr_func()
903 {
904   SetArrItem_func  "$@"
905 }
906
907
908  
909 #//*********************************************************************
910 #// <<< [SetAttr_as_ArrayName_func] >>> 
911 #//*********************************************************************
912 function  SetAttr_as_ArrayName_func()
913 {
914   local  self__="$1"
915   local  AttrName__="$2"
916   local  i__
917   local  Params__
918   CheckOutParamIsConflictToLocal_func  "$self__"  self__  AttrName__  i__  Params__
919
920   shift  2
921   Params__=("$@")
922
923   SetAttr_func  $self__  "$AttrName__"  "g_Arrays_$g_ArrayLength"
924
925   for (( i__ = 0; i__ < ${#Params__[@]}; i__ ++ ));do
926     eval  "g_Arrays_$g_ArrayLength"'[$i__]'='${Params__[$i__]}'
927   done ; done_func $?
928
929   g_ArrayLength=$(( $g_ArrayLength + 1 ))
930 }
931
932
933  
934 #//*********************************************************************
935 #// <<< [SetAttr_as_AssociativeArrayName_func] >>> 
936 #//*********************************************************************
937 function  SetAttr_as_AssociativeArrayName_func()
938 {
939   local  self__="$1"
940   local  AttrName__="$2"
941   local  i__
942   local  Params__
943   local  key__
944   local  value__
945   CheckOutParamIsConflictToLocal_func  "$self__"  self__  AttrName__  i__  Params__  key__  value__
946
947   shift  2
948   Params__=("$@")
949
950   SetAttr_func  $self__  "$AttrName__"  "g_AssociativeArrays_$g_AssociativeArrayLength"
951
952   for (( i__ = 0; i__ < ${#Params__[@]}; i__ = i__ + 2 ));do
953     key__=${Params__[$i__]}
954     value__=${Params__[$i__+1]}
955
956     SetAttr_func  g_AssociativeArrays_$g_AssociativeArrayLength  $key__  "$value__"
957   done ; done_func $?
958
959   g_AssociativeArrayLength=$(( $g_AssociativeArrayLength + 1 ))
960 }
961
962
963  
964 #//*********************************************************************
965 #// <<< [AssociativeArrayClass.getLength_method] >>> 
966 #//*********************************************************************
967 function  AssociativeArrayClass.getLength_method()
968 {
969   local  self="$1"
970   if [ "$self" == "" ];then  Error_func  "no object name"  ;fi
971
972   eval  g_Ret='${#'"$self"'[@]}'
973 }
974
975
976  
977 #//*********************************************************************
978 #// <<< [AssociativeArrayClass.getKeys_method] >>> 
979 #//*********************************************************************
980 function  AssociativeArrayClass.getKeys_method()
981 {
982   local  self="$1"
983   if [ "$self" == "" ];then  Error_func  "no object name"  ;fi
984
985   eval  g_Ret='${!'"$self"'[@]}'
986 }
987
988
989  
990 #//*********************************************************************
991 #// <<< [AssociativeArrayClass.getItems_method] >>> 
992 #//*********************************************************************
993 function  AssociativeArrayClass.getItems_method()
994 {
995   local  self="$1"
996   if [ "$self" == "" ];then  Error_func  "no object name"  ;fi
997
998   eval  g_Ret='${'"$self"'[@]}'
999 }
1000
1001
1002  
1003 #//*********************************************************************
1004 #// <<< [AssociativeArrayClass.destroy_method] >>> 
1005 #//*********************************************************************
1006 function  AssociativeArrayClass.destroy_method()
1007 {
1008   local  self="$1"
1009
1010   unset  $self
1011   unset  $self
1012 }
1013
1014
1015  
1016 #//*********************************************************************
1017 #// <<< [CopyArray_func] >>> 
1018 #//*********************************************************************
1019 function  CopyArray_func()
1020 {
1021   local  DstArray__="$1"
1022   local  SrcArray__="$2"
1023   local  key__
1024   local  keys__
1025   local  code__
1026   local  value__
1027   local  locals__="DstArray__ SrcArray__ key__ keys__ code__ value__"
1028   CheckOutParamIsConflictToLocal_func  "$DstArray__"   $locals__
1029   CheckOutParamIsConflictToLocal_func  "$SrcArray__"   $locals__
1030
1031   code__="$DstArray__=("
1032   eval  keys__=( '"${!'"$SrcArray__"'[@]}"' )
1033   for key__  in "${keys__[@]}" ;do
1034     eval  value__='${'$SrcArray__'[$key__]}'
1035     code__="$code__ [\"$key__\"]=\"$value__\""
1036   done ; done_func $?
1037   eval  "$code__ )"
1038 }
1039
1040
1041  
1042 #//*********************************************************************
1043 #// <<< [CopyArrayAttr_func] >>> 
1044 #//*********************************************************************
1045 function  CopyArrayAttr_func()
1046 {
1047   local  self__="$1"
1048   local  AttrName__="$2"
1049   local  out_Array="$3"
1050   local  array_name__
1051   local  i__
1052   local  n__
1053   local  locals="self__  AttrName__  out_Array__  array_name__  i__  n__"
1054   CheckOutParamIsConflictToLocal_func  "$self__"     $locals
1055   CheckOutParamIsConflictToLocal_func  "$out_Array"  $locals
1056
1057   Attr_func  $self__  "$AttrName__" ; array_name__="$g_Ret"
1058   unset  $out_Array
1059   unset  $out_Array  #// 配列をクリアするには2回必要?
1060
1061   eval  n__='${#'"$array_name__"'[@]}'
1062
1063   for (( i__ = 0; $i__ < $n__; i__ ++ ));do
1064     eval  "$out_Array"'[$i__]=${'"$array_name__"'[$i__]}'
1065   done ; done_func $?
1066 }
1067
1068
1069  
1070 #//*********************************************************************
1071 #// <<< [SetOutput_func] >>> 
1072 #//*********************************************************************
1073 function  SetOutput_func()
1074 {
1075   local  out_Var="$1"
1076   local  Value__="$2"
1077   CheckArgCount_func  2 "$@"
1078   CheckOutParamIsConflictToLocal_func  "$out_Var"  out_Var  Value__
1079
1080   eval  $out_Var="'$Value__'"
1081 }
1082
1083
1084  
1085 #//*********************************************************************
1086 #// <<< [SetOutputAsArray_func] >>> 
1087 #//*********************************************************************
1088 function  SetOutputAsArray_func()
1089 {
1090   local  out_Var="$1"
1091   CheckMinArgCount_func  2 "$@"
1092   CheckOutParamIsConflictToLocal_func  "$out_Var"  out_Var
1093
1094   shift  1
1095   eval  $out_Var="(" '"$@"' ")"
1096 }
1097
1098
1099  
1100 #//*********************************************************************
1101 #// <<< [AddIfNotExist_func] >>> 
1102 #//*********************************************************************
1103 function  AddIfNotExist_func()
1104 {
1105   local  WholeStr="$1"
1106   local  AddStr="$2"
1107   local  Separator="$3"
1108   CheckArgCount_func  3 "$@"
1109
1110   local  whole_str_plus="$WholeStr$Separator"
1111   local  str
1112
1113   if [ "$AddStr" == "" ];then  echo  "$WholeStr"  ;  return  0  ;fi
1114
1115   LeftOfStr_func  "$whole_str_plus"  "$AddStr$Separator" ; str="$g_Ret"
1116   if [ "$str" == "$whole_str_plus" ];then  #// not found
1117     g_Ret="$AddStr$Separator$WholeStr"
1118   else
1119     g_Ret="$WholeStr"
1120   fi
1121 }
1122
1123
1124  
1125 #//*********************************************************************
1126 #// <<< [IsNumeric_func] >>> 
1127 #//*********************************************************************
1128 function  IsNumeric_func()
1129 {
1130   local  Value="$1"
1131   CheckArgCount_func  1 "$@"
1132   if [ "$Value" == "-1" ];then
1133     return  0
1134   else
1135     expr  $Value + 1 > /dev/null  2>&1
1136   fi
1137 }
1138
1139
1140  
1141 #//*********************************************************************
1142 #// <<< [StringClass.length_method] >>> 
1143 #//*********************************************************************
1144 function  StringClass.length_method()
1145 {
1146   local  self="$1"
1147   CheckArgCount_func  1 "$@"
1148   g_Ret="${#self}"
1149 }
1150
1151
1152  
1153 #//*********************************************************************
1154 #// <<< [StringClass.substring_method] >>> 
1155 #//*********************************************************************
1156 function  StringClass.substring_method()
1157 {
1158   local  self="$1"
1159   local  StartIndex="$2"
1160   local  EndIndex="$3"
1161   CheckMinArgCount_func  2 "$@"
1162   CheckMaxArgCount_func  3 "$@"
1163
1164   if [ "$EndIndex" == "" ];then
1165     g_Ret="${self:$StartIndex}"
1166   else
1167     g_Ret="${self:$StartIndex:$(( $EndIndex - $StartIndex ))}"
1168   fi
1169 }
1170
1171
1172  
1173 #//*********************************************************************
1174 #// <<< [StringClass.trim_method] >>> 
1175 #//*********************************************************************
1176 function  StringClass.trim_method()
1177 {
1178   local  String="$1"
1179   local  field
1180   local  key
1181   local  index
1182   local  ch
1183
1184   index=0
1185   while true ;do
1186     ch="${String:$index:1}"
1187     if [ "$ch" != " "  ];then
1188       if [ "$ch" != "$Tab" ];then
1189         if [ "$ch" != "$LF" ];then
1190           break
1191         fi
1192       fi
1193     fi
1194     index=$(( $index + 1 ))
1195   done ; done_func $?
1196
1197   String="${String:$index}"
1198
1199   index=$(( ${#String} - 1 ))
1200   while true ;do
1201     ch="${String:$index:1}"
1202     if [ "$ch" != " " ];then
1203       if [ "$ch" != "$Tab" ];then
1204         if [ "$ch" != "$LF" ];then
1205           break
1206         fi
1207       fi
1208     fi
1209     index=$(( $index - 1 ))
1210   done ; done_func $?
1211   index=$(( $index + 1 ))
1212
1213   g_Ret="${String:0:$index}"
1214 }
1215
1216
1217  
1218 #//*********************************************************************
1219 #// <<< [StringClass.right_method] >>> 
1220 #//*********************************************************************
1221 function  StringClass.right_method()
1222 {
1223   local  String="$1"
1224   local  Length="$2"
1225   CheckArgCount_func  2 "$@"
1226
1227   g_Ret="${String:$(( ${#String} - $Length ))}"
1228 }
1229
1230
1231  
1232 #//*********************************************************************
1233 #// <<< [StringClass.cutLastOf_method] >>> 
1234 #//*********************************************************************
1235 function  StringClass.cutLastOf_method()
1236 {
1237   local  String="$1"
1238   local  LastStr="$2"
1239   CheckArgCount_func  2 "$@"
1240
1241   if [ "${String:$(( ${#String} - ${#LastStr} ))}" == "$LastStr" ];then
1242     g_Ret="${String:0:$(( ${#String} - ${#LastStr} ))}"
1243   else
1244     g_Ret="$String"
1245   fi
1246 }
1247
1248
1249  
1250 #//*********************************************************************
1251 #// <<< [LeftOfStr_func] >>> 
1252 #//*********************************************************************
1253 function  LeftOfStr_func()
1254 {
1255   local  String="$1"
1256   local  Key="$2"
1257   CheckArgCount_func  2 "$@"
1258
1259   StringClass.replace_method  "$Key"    '\'  '\\'
1260   StringClass.replace_method  "$g_Ret"  '*'  '\*'
1261
1262   g_Ret="${String%%$g_Ret*}"
1263 }
1264
1265
1266  
1267 #//*********************************************************************
1268 #// <<< [LeftOfLastStr_func] >>> 
1269 #//*********************************************************************
1270 function  LeftOfLastStr_func()
1271 {
1272   local  String="$1"
1273   local  Key="$2"
1274   CheckArgCount_func  2 "$@"
1275
1276   StringClass.replace_method  "$Key"    '\'  '\\'
1277   StringClass.replace_method  "$g_Ret"  '*'  '\*'
1278
1279   g_Ret="${String%$g_Ret*}"
1280 }
1281
1282
1283  
1284 #//*********************************************************************
1285 #// <<< [RightOfStr_func] >>> 
1286 #//*********************************************************************
1287 function  RightOfStr_func()
1288 {
1289   local  String="$1"
1290   local  Key="$2"
1291   CheckArgCount_func  2 "$@"
1292
1293   StringClass.replace_method  "$Key"    '\'  '\\'
1294   StringClass.replace_method  "$g_Ret"  '*'  '\*'
1295
1296   g_Ret="${String#*$g_Ret*}"
1297 }
1298
1299
1300  
1301 #//*********************************************************************
1302 #// <<< [RightOfLastStr_func] >>> 
1303 #//*********************************************************************
1304 function  RightOfLastStr_func()
1305 {
1306   local  String="$1"
1307   local  Key="$2"
1308   CheckArgCount_func  2 "$@"
1309
1310   StringClass.replace_method  "$Key"    '\'  '\\'
1311   StringClass.replace_method  "$g_Ret"  '*'  '\*'
1312
1313   g_Ret="${String##*$g_Ret}"
1314 }
1315
1316
1317  
1318 #//*********************************************************************
1319 #// <<< [StringClass.indexOf_method] >>> 
1320 #//*********************************************************************
1321 function  StringClass.indexOf_method()
1322 {
1323   local  self="$1"
1324   local  Keyword="$2"
1325   local  StartIndex="$3"
1326   CheckMinArgCount_func  2 "$@"
1327   CheckMaxArgCount_func  3 "$@"
1328
1329   if [ "$StartIndex" == "" ];then  StartIndex=0  ;fi
1330   if [ "$StartIndex" -le "0" ];then  #// -le:"<="
1331     part="${self%%$Keyword*}"
1332     if [ "$part" == "$self" ];then
1333       g_Ret=-1
1334     else
1335       g_Ret=$(( ${#part} ))
1336     fi
1337   else
1338     self="${self:$StartIndex}"
1339     part="${self%%$Keyword*}"
1340     if [ "$part" == "$self" ];then
1341       g_Ret=-1
1342     else
1343       g_Ret=$(( ${#part} + $StartIndex ))
1344     fi
1345   fi
1346 }
1347
1348
1349  
1350 #//*********************************************************************
1351 #// <<< [StringClass.lastIndexOf_method] >>> 
1352 #//*********************************************************************
1353 function  StringClass.lastIndexOf_method()
1354 {
1355   local  self="$1"
1356   local  Keyword="$2"
1357   local  StartIndex="$3"
1358   CheckMinArgCount_func  2 "$@"
1359   CheckMaxArgCount_func  3 "$@"
1360
1361   if [ "$StartIndex" == "" ];then  StartIndex=0  ;fi
1362   if [ "$StartIndex" -le "0" ];then  #// -le:"<="
1363     part="${self%$Keyword*}"
1364     if [ "$part" == "$self" ];then
1365       g_Ret=-1
1366     else
1367       g_Ret=$(( ${#part} ))
1368     fi
1369   else
1370     self="${self:0:$(( $StartIndex + ${#Keyword} ))}"
1371     part="${self%$Keyword*}"
1372     if [ "$part" == "$self" ];then
1373       g_Ret=-1
1374     else
1375       g_Ret=${#part}
1376     fi
1377   fi
1378 }
1379
1380
1381  
1382 #//*********************************************************************
1383 #// <<< [StringClass.replace_method] >>> 
1384 #//*********************************************************************
1385 function  StringClass.replace_method()
1386 {
1387   local  String="$1"
1388   local  From="$2"
1389   #// To="$3"
1390
1391   StringEscapeUtilsClass.escapeBashReplace_method  "$From"
1392   g_Ret="${String//$g_Ret/$3}"
1393 }
1394
1395
1396  
1397 #//*********************************************************************
1398 #// <<< [StringClass.toLowerCase_method] >>> 
1399 #//*********************************************************************
1400 function  StringClass.toLowerCase_method()
1401 {
1402   #// for bash 4
1403   #// local  self="$1"
1404   #// echo  "${self,,}"
1405
1406   #// for bash 3
1407   g_Ret=`echo  "$1" | sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`
1408 }
1409
1410
1411  
1412 #//*********************************************************************
1413 #// <<< [StringClass.toUpperCase_method] >>> 
1414 #//*********************************************************************
1415 function  StringClass.toUpperCase_method()
1416 {
1417   #// for bash 4
1418   #// local  self="$1"
1419   #// echo  "${self^^}"
1420
1421   #// for bash 3
1422   g_Ret=`echo  "$1" | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'`
1423 }
1424
1425
1426  
1427 #//*********************************************************************
1428 #// <<< [StringEscapeUtilsClass.escapeGrep_method] >>> 
1429 #//*********************************************************************
1430 function  StringEscapeUtilsClass.escapeGrep_method()
1431 {
1432   local  Str="$1"
1433     Str="${Str//\\/\\\\}"
1434     Str="${Str//./\.}"
1435     Str="${Str//$/\\$}"
1436     Str="${Str//^/\^}"
1437     Str="${Str//{/\{}"
1438     Str="${Str//\}/\}}"
1439     Str="${Str//[/\[}"
1440     Str="${Str//]/\]}"
1441     Str="${Str//\*/\*}"
1442     Str="${Str//+/\+}"
1443   g_Ret="${Str//\?/\?}"
1444 }
1445
1446
1447  
1448 #//*********************************************************************
1449 #// <<< [StringEscapeUtilsClass.escapeSed_method] >>> 
1450 #//*********************************************************************
1451 function  StringEscapeUtilsClass.escapeSed_method()
1452 {
1453   local  Str="$1"
1454     Str="${Str//\\/\\\\}"
1455     Str="${Str//./\.}"
1456     Str="${Str//$/\\$}"
1457     Str="${Str//^/\^}"
1458     Str="${Str//[/\[}"
1459     Str="${Str//]/\]}"
1460     Str="${Str//\*/\*}"
1461     Str="${Str//\?/\?}"
1462   g_Ret="${Str//\//\/}"
1463 }
1464
1465
1466  
1467 #//*********************************************************************
1468 #// <<< [StringEscapeUtilsClass.escapeBashReplace_method] >>> 
1469 #//*********************************************************************
1470 function  StringEscapeUtilsClass.escapeBashReplace_method()
1471 {
1472   local  Str="$1"
1473     Str="${Str//\\/\\\\}"
1474     Str="${Str//\}/\}}"
1475     Str="${Str//\(/\\(}"
1476     Str="${Str//\)/\\)}"
1477     Str="${Str//\*/\\*}"
1478     Str="${Str//\?/\\?}"
1479   g_Ret="${Str//\//\\/}"
1480 }
1481
1482
1483  
1484 #//*********************************************************************
1485 #// <<< [StringEscapeUtilsClass.escapeBashDoubleQuot_method] >>> 
1486 #//*********************************************************************
1487 function  StringEscapeUtilsClass.escapeBashDoubleQuot_method()
1488 {
1489   local  Str="$1"
1490     Str="${Str//\\/\\\\}"
1491     Str="${Str//$/\\$}"
1492   g_Ret="${Str//\//\\/}"
1493 }
1494
1495
1496  
1497 #//*********************************************************************
1498 #// <<< [StringEscapeUtilsClass.escapeBashParam_method] >>> 
1499 #//*********************************************************************
1500 function  StringEscapeUtilsClass.escapeBashParam_method()
1501 {
1502   local  Str="$1"
1503     Str="${Str//\\/\\\\}"
1504     Str="${Str// /\\ }"
1505   g_Ret="${Str//$/\\$}"
1506 }
1507
1508
1509  
1510 #//*********************************************************************
1511 #// <<< [ReplaceTextFile_func] >>> 
1512 #//*********************************************************************
1513 function  ReplaceTextFile_func()
1514 {
1515   local  Path="$1"
1516   local  From="$2"
1517   local  To="$3"
1518   local  Opt="$4"
1519
1520   CheckMinArgCount_func  2 "$@"
1521   CheckMaxArgCount_func  4 "$@"
1522
1523   local  str1
1524   local  str2
1525   local  slash
1526   local  slashes=( "%" "/" )
1527
1528   CheckWritable_func  "$Path"
1529
1530   if [ "$Opt" == "-i" ];then  Opt="i"  ;fi
1531
1532   sed_setIsAbleToNotCaseSensitive_func
1533   if [ "$g_sed_isAbleToNotCaseSensitive" == "0" ];then
1534     StringClass.indexOf_method  "$Opt"  "i"
1535     if [ "$g_Ret" -ge 0 ];then
1536       Error_func  "-i オプションは指定できません。"
1537     fi
1538   fi
1539
1540   for  slash  in  ${slashes[@]};do
1541
1542     LeftOfStr_func  "$From"  "$slash" ; str1="$g_Ret"
1543     LeftOfStr_func  "$To"    "$slash" ; str2="$g_Ret"
1544     if [ x"$str1" == x"$From"  -a  x"$str2" == x"$To" ];then  #// $From and $To do not have $slash.
1545
1546       RightOfStr_func  "$From"  "\n" ; str1="$g_Ret"
1547       RightOfStr_func  "$To"    "\n" ; str2="$g_Ret"
1548       if [ x"$str1" == x"$From"  -a  x"$str2" == x"$To" ];then  #// $str1 and $str2 are single line,
1549         sed -i.bak -e "s$slash$From$slash$To${slash}g$Opt"  "$Path" ;  rm "$Path.bak"
1550       else
1551         RightOfStr_func  "$str1"  "\n" ; str2="$g_Ret"
1552         if [ "$str1" != "$str2" ];then
1553           Error_func  "置き換える前の文字列は3行以上にできません。"
1554         fi
1555
1556         #// for GNU sed
1557         #// sed -i.bak -e '/\n/! {;$q;N;};'"s$slash$From$slash$To${slash}g$Opt;P;D;"  "$Path" ;  rm "$Path.bak"
1558
1559         #// for BSD sed in Mac OS X Snow Leopard
1560         To=`echo "$To" | sed  -e 's/\\\\n/\\\\\\'"$LF"'/g' `
1561         cp  "$Path"  "$Path.bak"  #// mv では、chmod 属性が消えてしまいます
1562         sed -e '/\n/! {;$q;N;};'"s$slash$From$slash$To${slash}g$Opt;P;D;"  "$Path.bak" > $Path
1563         rm  "$Path.bak"
1564       fi
1565       return
1566     fi
1567   done ; done_func $?
1568
1569   Error_func  "ReplaceTextFile_func は、置き換える前後の文字列に / と % の両方を含むことはできません"
1570 }
1571
1572
1573  
1574 #//*********************************************************************
1575 #// <<< [sed_setIsAbleToNotCaseSensitive_func] >>> 
1576 #//*********************************************************************
1577 g_sed_isAbleToNotCaseSensitive=""
1578 function  sed_setIsAbleToNotCaseSensitive_func()
1579 {
1580   if [ "$g_sed_isAbleToNotCaseSensitive" == "" ];then
1581     local  status=0
1582     echo -n "" | sed -e s/a/b/i > /dev/null  2>&1  || status=$?
1583     g_sed_isAbleToNotCaseSensitive=$(( 1 - $status ))
1584   fi
1585 }
1586
1587
1588  
1589 #//*********************************************************************
1590 #// <<< [ReplaceTextFileLineRange_func] >>> 
1591 #//*********************************************************************
1592 function  ReplaceTextFileLineRange_func()
1593 {
1594   local  Path="$1"
1595   local  From="$2"
1596   local  FromEnd="$3"
1597   local  To="$4"
1598   local  Opt="$5"
1599   local  str
1600   local  a1
1601
1602   CheckMinArgCount_func  3 "$@"
1603   CheckMaxArgCount_func  5 "$@"
1604
1605   CheckWritable_func  "$Path"
1606
1607   if [ "$Opt" == "-i" ];then  Opt="i"  ;fi
1608
1609   sed_setIsAbleToNotCaseSensitive_func
1610   if [ "$g_sed_isAbleToNotCaseSensitive" == "0" ];then
1611     StringClass.indexOf_method  "$Opt"  "i"
1612     if [ "$g_Ret" -ge 0 ];then
1613       Error_func  "i オプションは指定できません。"
1614     fi
1615   fi
1616
1617   LeftOfStr_func  "$From"  "%"
1618   if [ "$g_Ret" != "$From" ];then  Error_func  ;fi
1619
1620   LeftOfStr_func  "$FromEnd"  "%"
1621   if [ "$g_Ret" != "$FromEnd" ];then  Error_func  ;fi
1622
1623   Opt=`StringClass.toUpperCase_method  "$Opt"`
1624
1625   if [ "$To" != "" ];then
1626     #// for GNU sed
1627     #// To="c${To//\\n/\\${LF}}"
1628
1629     #// for BSD sed in Mac OS X Snow Leopard
1630     if [ "$To" == "\n" ];then
1631       a1=`echo  $'a\nb\nc' | sed -e '/b/c\'"${LF}" `
1632       if [ "$a1" == "a${LF}${LF}c" ];then
1633         To=""
1634       fi
1635     fi
1636
1637     To="c\\${LF}${To//\\n/\\${LF}}"
1638   else
1639     To="d"
1640   fi
1641
1642   #// for GNU sed
1643   #// if [ "$FromEnd" == "" ];then
1644   #//   sed -i.bak -e '\%'"$From"'%'"$Opt$To"  "$Path" ;  rm "$Path.bak"
1645   #// else
1646   #//   sed -i.bak -e '\%'"$From"'%'"$Opt"',\%'"$FromEnd"'%'"$Opt$To"  "$Path" ;  rm "$Path.bak"
1647   #// fi
1648
1649   #// for BSD sed in Mac OS X Snow Leopard
1650   cp  "$Path"  "$Path.bak"  #// mv では、chmod 属性が消えてしまいます
1651   if [ "$FromEnd" == "" ];then
1652     sed -e '\%'"$From"'%'"$Opt$To"  "$Path.bak" > $Path
1653   else
1654     sed -e '\%'"$From"'%'"$Opt"',\%'"$FromEnd"'%'"$Opt$To"  "$Path.bak" > $Path
1655   fi
1656   rm  "$Path.bak"
1657 }
1658
1659
1660  
1661 #//*********************************************************************
1662 #// <<< [MultiLine_func] >>> 
1663 #//*********************************************************************
1664 function  MultiLine_func()
1665 {
1666   local  LineFeedChar="$1"
1667   shift  1
1668
1669   local  Params=( "$@" )
1670   local  line
1671   local  lfs
1672
1673   g_Ret=""
1674   for line  in "$@" ;do
1675     if [ "$line" == "" ];then
1676       lfs="$lfs$LineFeedChar"
1677     else
1678       g_Ret="$g_Ret$lfs$line"
1679       lfs="$LineFeedChar"
1680     fi
1681   done ; done_func $?
1682 }
1683
1684
1685  
1686 #//*********************************************************************
1687 #// <<< [ArrayClass.getLength_method] >>>
1688 #//*********************************************************************
1689 function  ArrayClass.getLength_method()
1690 {
1691   local  self="$1"
1692
1693   eval  g_Ret='${#'"$self"'[@]}'
1694 }
1695
1696
1697  
1698 #//*********************************************************************
1699 #// <<< [ArrayClass.get_method] >>>
1700 #//*********************************************************************
1701 function  ArrayClass.get_method()
1702 {
1703   local  self="$1"
1704   local  Index="$2"
1705   local  over_index
1706   CheckArgCount_func  2 "$@"
1707
1708   eval  over_index='${#'"$self"'[@]}'
1709   if [ "$Index" -ge "$over_index"  -o  "$Index" -lt "0" ];then
1710     Error_func  "<ERROR msg=\"インデックスが範囲外です\" index=\"$Index\"/>"
1711   fi
1712
1713   eval  g_Ret='"${'"$self"'[$Index]}"'
1714 }
1715
1716
1717  
1718 #//*********************************************************************
1719 #// <<< [ArrayClass.set_method] >>>
1720 #//*********************************************************************
1721 function  ArrayClass.set_method()
1722 {
1723   local  self="$1"
1724   local  Index="$2"
1725   local  Value="$3"
1726   local  over_index
1727   local  num
1728   CheckArgCount_func  3 "$@"
1729
1730   eval  over_index='${#'"$self"'[@]}'
1731   if [ "$Index" -lt "0" ];then
1732     Error_func  "<ERROR msg=\"インデックスが範囲外です\" index=\"$Index\"/>"
1733   fi
1734
1735   for (( num = over_index;  $num < $Index;  num++ )) ;do
1736     eval  "$self"'[$num]=""'
1737   done ; done_func $?
1738
1739   eval  "$self"'[$Index]='"'$Value'"
1740 }
1741
1742
1743  
1744 #//*********************************************************************
1745 #// <<< [ArrayClass.remove_method] >>>
1746 #//*********************************************************************
1747 function  ArrayClass.remove_method()
1748 {
1749   local  self="$1"
1750   local  Index="$2"
1751   local  num
1752   local  over_index
1753   local  last_index
1754   CheckArgCount_func  2 "$@"
1755
1756   eval  over_index='${#'"$self"'[@]}'
1757   if [ "$Index" -ge "$over_index"  -o  "$Index" -lt "0" ];then
1758     Error_func  "<ERROR msg=\"インデックスが範囲外です\" index=\"$Index\"/>"
1759   fi
1760
1761   last_index=$(( $over_index - 1 ))
1762   for (( num = $Index;  $num < $last_index;  num++ )) ;do
1763     eval  "$self"'[$num]="${'"$self"'[$(( $num + 1 ))]}"'
1764   done ; done_func $?
1765
1766   eval  unset "$self"'[$last_index]'
1767 }
1768
1769
1770  
1771 #//*********************************************************************
1772 #// <<< [ArrayClass.clear_method] >>> 
1773 #//*********************************************************************
1774 function  ArrayClass.clear_method()
1775 {
1776   local  self="$1"
1777
1778   unset $self
1779   unset $self  #// 配列をクリアするには2回必要?
1780 }
1781
1782
1783  
1784 #//*********************************************************************
1785 #// <<< [ArrayClass.fromLines_method] >>> 
1786 #//*********************************************************************
1787 function  ArrayClass.fromLines_method()
1788 {
1789   local  out_Array="$1"
1790   local  Lines="$2"
1791   local  line
1792   local  i
1793   local  keys
1794
1795   CheckOutParamIsConflictToLocal_func  $out_Array  out_Array  Lines  line  i  keys
1796
1797   unset  $out_Array
1798   unset  $out_Array  #// 配列をクリアするには2回必要?
1799
1800   i=0
1801   while true; do
1802     line="${Lines%%$LF*}"
1803     if [ "$line" != "" ];then  eval $out_Array[$i]="'$line'"  ;fi
1804     if [ "$line" == "$Lines" ];then  break  ;fi
1805     i=$(( $i + 1 ))
1806     Lines=${Lines:$(( ${#line} + 1 ))}  #// next line
1807   done ; done_func $?
1808 }
1809
1810
1811  
1812 #//*********************************************************************
1813 #// <<< [ArrayClass.fromCSV_method] >>> 
1814 #//*********************************************************************
1815 function  ArrayClass.fromCSV_method()
1816 {
1817   local  out_Array="$1"
1818   local  CSV__="$2"
1819   local  index__=0
1820   local  item__
1821   local  char__
1822   local  next__
1823   CheckArgCount_func  2 "$@"
1824
1825   CheckOutParamIsConflictToLocal_func  "$out_Array" \
1826     CSV__  out_Array  index__  item__  __field  char__  next__
1827
1828   unset  $out_Array
1829   unset  $out_Array  #// 配列をクリアするには2回必要?
1830
1831   while true; do
1832     item__=( $CSV__ )  #// ${item__[0]} does not have space
1833     StringClass.substring_method  ${item__[0]}  0  1 ; char__="$g_Ret"
1834     if [ "$char__" == "\"" ];then
1835       RightOfStr_func  "$CSV__"  "\"" ; CSV__="$g_Ret"
1836       LeftOfStr_func  "$CSV__"  "\"" ; item__="$g_Ret"
1837       RightOfStr_func  "$CSV__"  "\"" ; CSV__="$g_Ret"
1838     elif [ "$char__" == "" ];then
1839       break
1840     else
1841       LeftOfStr_func  "$CSV__"  "," ; item__="$g_Ret"
1842       StringClass.trim_method  "$item__" ; item__="$g_Ret"
1843     fi
1844     RightOfStr_func  "$CSV__"  "," ; next__="$g_Ret"
1845
1846     SetArrItem_func  $out_Array  $index__  "$item__"
1847     index__=$(( $index__ + 1 ))
1848
1849     if [ x"$next__" == x""  -o  x"$next__" == x"$CSV__" ];then  break  ;fi
1850     CSV__="$next__"
1851   done ; done_func $?
1852 }
1853
1854
1855  
1856 #//*********************************************************************
1857 #// <<< [IsSameArrayOutOfOrder_func] >>>
1858 #//*********************************************************************
1859 function  IsSameArrayOutOfOrder_func()
1860 {
1861   local  ArrayACount="$1"
1862   local  ArrayAB=("$@")
1863
1864   local  a_index
1865   local  b_index
1866   local  is_same_arr
1867
1868   if ! IsNumeric_func  "$ArrayACount" ;then  Error_func  ;fi
1869
1870   if [ ${#ArrayAB[@]} != $(( $ArrayACount * 2 + 1 )) ];then  return 1 ;fi
1871
1872   for (( b_index = $ArrayACount + 1;  b_index < ${#ArrayAB[@]};  b_index ++ )) ;do
1873     for (( a_index = 1;  a_index <= $ArrayACount;  a_index ++ )) ;do
1874       if [ "${ArrayAB[$a_index]}" == "${ArrayAB[$b_index]}" ];then
1875         if [ "${is_same_arr[$a_index]}" == "" ];then
1876           is_same_arr[$a_index]=1
1877           break
1878         fi
1879       fi
1880     done ; done_func $?
1881   done ; done_func $?
1882
1883   for (( a_index=1;  a_index <= $ArrayACount;  a_index ++ )) ;do
1884     if [ "${is_same_arr[$a_index]}" == "" ];then
1885       return  1
1886     fi
1887   done ; done_func $?
1888   return  0
1889 }
1890
1891
1892  
1893 #//*********************************************************************
1894 #// <<< [Extract_func] >>> 
1895 #//*********************************************************************
1896 function  Extract_func()
1897 {
1898   local  PackagePath="$1"
1899   local  DstFolder="$2"
1900   local  Option1="$3"
1901
1902   local  ext1
1903   local  ext2
1904   local  i
1905   local  extract_folder
1906   local  status
1907   local  trans_option_1
1908   local  trans_option_2
1909   local  dst_name
1910   local  arguments
1911   $declare_associative_array  option
1912
1913   echo  "$PackagePath -> $DstFolder  $Option1"
1914
1915   CheckWritable_func  "$DstFolder"
1916   dst_name=`basename "$DstFolder"`
1917
1918   RightOfLastStr_func  "$PackagePath"  "." ; ext2="$g_Ret"
1919   LeftOfLastStr_func   "$PackagePath"  "."
1920   RightOfLastStr_func  "$g_Ret"  "." ; ext1="$g_Ret"
1921
1922   #// --transform option
1923   GetLongOptions_func  arguments  option  "$@"  #//[out] arguments, option
1924   Attr_func  option  "transform_from" ; trans_option_1="$g_Ret"
1925
1926
1927   if [ "$trans_option_1" == "" ];then
1928
1929     #// set one_folder_path
1930     ListUpIn_func  "$PackagePath" > /tmp/_Extract_func.txt  #// tar write error, if pipe was used
1931     one_folder_path=`head -n 1 /tmp/_Extract_func.txt`
1932
1933     StringClass.substring_method  "$one_folder_path"  0  1
1934     if [ "$g_Ret" == "/" ];then
1935       one_folder_path=""
1936     else
1937       StringClass.substring_method  "$one_folder_path"  0  2
1938       if [ "$g_Ret" == "./" ];then
1939         StringClass.indexOf_method  "$one_folder_path"  "/"  2
1940       else
1941         StringClass.indexOf_method  "$one_folder_path"  "/"
1942       fi
1943       if [ "$g_Ret" == "-1" ];then
1944         one_folder_path=""
1945       else
1946         StringClass.substring_method  "$one_folder_path"  0  $g_Ret ; one_folder_path="$g_Ret"
1947       fi
1948     fi
1949
1950
1951     #// set one_folder_path="", if there was two folders
1952     if [ "$one_folder_path" != "" ];then
1953       status=0
1954       cat /tmp/_Extract_func.txt | grep -v "^$one_folder_path/" || status=$?
1955       CheckPipeStatus_func  "${PIPESTATUS[@]}"
1956       if [ "$status" == "0" ];then  #// found out of one_folder_path
1957         one_folder_path=""
1958       fi
1959     fi
1960     rm  /tmp/_Extract_func.txt
1961
1962
1963     #// set trans_option
1964     if [ "$one_folder_path" != "" ];then
1965       if [ "$ext2" == "zip" ];then
1966         trans_option_1="$one_folder_path"
1967       else
1968         GetExtractTransformOptionName_func  "$DstFolder"
1969         if [ "$g_Ret" == "--transform" ];then
1970           trans_option_1="--transform=s%^$one_folder_path%$dst_name%"
1971         else
1972           trans_option_1="-s"
1973           trans_option_2="%^$one_folder_path/%$dst_name/%"
1974         fi
1975       fi
1976     fi
1977
1978
1979     #// set extract_folder
1980     if [ "$one_folder_path" == "" ];then
1981       extract_folder="$DstFolder"
1982     else
1983       GetParentAbsPath_func  "$DstFolder" ; extract_folder="$g_Ret"
1984     fi
1985
1986   else
1987
1988     #// modify trans_option
1989     StringClass.right_method  "$trans_option_1"  1
1990     if [ "$g_Ret" == "/" ];then
1991       StringClass.length_method  "$trans_option_1"
1992       StringClass.substring_method  "$trans_option_1"  0  $(( $g_Ret - 1 ))
1993       trans_option_1="$g_Ret"
1994     fi
1995     dst_name=`basename  "$DstFolder"`
1996
1997     if [ "$ext2" != "zip" ];then
1998       GetExtractTransformOptionName_func  "$DstFolder"
1999       if [ "$g_Ret" == "--transform" ];then
2000         trans_option_1="--transform=s%^$trans_option_1%$dst_name%"
2001       else
2002         trans_option_2="%^$trans_option_1%$dst_name%"
2003         trans_option_1="-s"
2004       fi
2005     fi
2006     extract_folder=`dirname  "$DstFolder"`
2007   fi
2008
2009
2010   #// extract
2011   if [ x"$ext1" != x"tar"  -a  x"$ext2" == x"gz" ];then
2012     ExtractGZ_func  "$PackagePath"  "$extract_folder"
2013   elif [ "$ext2" == "zip" ];then
2014     ExtractZip_func  "$PackagePath"  "$DstFolder"  "$trans_option_1"
2015   else
2016     if [ ! -e "$extract_folder" ];then  mkdir -p  "$extract_folder"  ;fi
2017     if [ "$trans_option_1" == "" ];then
2018       echo  "tar xvf  \"$PackagePath\" -C \"$extract_folder\""
2019       tar xvf  "$PackagePath" -C "$extract_folder"
2020     else
2021       echo  "tar xvf  \"$PackagePath\" -C \"$extract_folder\"  \"$trans_option_1\"  \"$trans_option_2\""
2022       tar xvf  "$PackagePath" -C "$extract_folder"  "$trans_option_1"  "$trans_option_2"
2023     fi
2024   fi
2025 }
2026
2027
2028 declare  g_ExtractTransformOptionName  #// "-s" or "--transform"
2029
2030 function  GetExtractTransformOptionName_func()
2031 {
2032   if [ "$g_ExtractTransformOptionName" == "" ];then
2033
2034     local  WorkDstFolder="$1"
2035     local  is_pushd
2036     local  status
2037     local  i
2038     local  a1
2039
2040     for (( i = 1; i < 100; i ++ ));do
2041       if [ "$i" == "99" ];then  Error_func  "$WorkDstFolder にフォルダーが作れません"  ;fi
2042       a1="$WorkDstFolder/_extract_trans_test_$i"
2043       if [ ! -e "$a1" ];then
2044         WorkDstFolder="$a1"
2045         break
2046       fi
2047     done ; done_func $?
2048
2049     while TryStart_func; do
2050
2051       mkdir -p  "$WorkDstFolder"
2052       pushd  "$WorkDstFolder" > /dev/null
2053       is_pushd=1
2054
2055       echo  "a" > a.txt
2056       tar cvjf  "a.tar.bz2"  "a.txt" > /dev/null
2057
2058       status=0
2059       tar xvf  "a.tar.bz2"  --transform="s%a.txt%b.txt%" > /dev/null 2>&1 || status=$?
2060
2061       if [ "$status" == "0"  -a  -e "b.txt" ];then
2062         g_ExtractTransformOptionName="--transform"
2063       else
2064         status=0
2065         tar xvf  "a.tar.bz2"  -s "%a.txt%b.txt%" > /dev/null 2>&1 || status=$?
2066
2067         if [ "$status" == "0"  -a  -e "b.txt" ];then
2068           g_ExtractTransformOptionName="-s"
2069         else
2070           Error_func
2071         fi
2072       fi
2073
2074     TryEnd1_func; done ;TryEnd2_func $?
2075       #// Finally
2076       if [ "$is_pushd" == "1" ];then  popd > /dev/null ;fi
2077       rm_func  "$WorkDstFolder"
2078     if [ "$g_ExitStatus" != "0" ]; then  ErrClass.raiseOverwrite_method  ;fi
2079     ErrClass.clear_method
2080   fi
2081   g_Ret="$g_ExtractTransformOptionName"
2082 }
2083
2084
2085  
2086 #//*********************************************************************
2087 #// <<< [ExtractGZ_func] >>> 
2088 #//*********************************************************************
2089 function  ExtractGZ_func()
2090 {
2091   local  GzPath="$1"
2092   local  ExtractedPath="$2"
2093   CheckArgCount_func  2 "$@"
2094
2095   if [ "$TMPDIR" == "" ]; then  temp_folder="$HOME"  ;else  temp_folder="$TMPDIR"  ;fi
2096
2097   if [ "$GzPath" == "${ExtractedPath}.gz" ]; then
2098     if [ -e "$temp_folder/_ExtractGZ_func.gz" -o -e "$temp_folder/_ExtractGZ_func" ]; then
2099       unset ERROR;${ERROR:?exist _ExtractGZ_func.gz is for temporary}  ;fi
2100
2101     cp -ap  "$GzPath"  "$temp_folder/_ExtractGZ_func.gz"
2102     echo  "gunzip  \"$temp_folder/_ExtractGZ_func.gz\""
2103     gunzip  "$temp_folder/_ExtractGZ_func.gz"
2104     mv  "$temp_folder/_ExtractGZ_func"  "$ExtractedPath"
2105   else
2106     cp -ap  "$GzPath"  "${ExtractedPath}.gz"
2107     rm_func "$ExtractedPath"
2108     echo  "gunzip  \"${ExtractedPath}.gz\""
2109     gunzip  "${ExtractedPath}.gz"
2110   fi
2111 }
2112
2113
2114  
2115 #//*********************************************************************
2116 #// <<< [ExtractZip_func] >>> 
2117 #//*********************************************************************
2118 function  ExtractZip_func()
2119 {
2120   local  PackagePath="$1"
2121   local  DstFolder="$2"
2122   local  TarnsformFrom="$3"
2123   local  dst_name ; dst_name=`basename "$DstFolder"`
2124   local  from_name ; from_name=`basename "$TarnsformFrom"`
2125
2126   if [ "$DstFolder" == "" ];then  DstFolder="$PWD" ;fi
2127
2128   if [ x"$TarnsformFrom" == x"" ];then
2129     mkdir_func  "$DstFolder"
2130     echo  "unzip -o -d  \"$DstFolder\"  \"$PackagePath\""
2131     unzip -o -d "$DstFolder"  "$PackagePath"
2132   elif [ x"$dst_name" == x"$from_name" ];then
2133     GetParentAbsPath_func  "$DstFolder"
2134     DstFolder="$g_Ret"
2135     mkdir_func  "$DstFolder"
2136     echo  "unzip -o -d  \"$DstFolder\"  \"$PackagePath\""
2137     unzip -o -d "$DstFolder"  "$PackagePath"
2138   else
2139     local  tmp_folder
2140     local  link_path
2141     local  num
2142
2143     tmp_folder="$DstFolder/_extracting_"
2144     for (( num = 1;  ;  num ++ )) ;do
2145       if [ "$num" == 1000 ];then  Error_func  ;fi
2146       if [ ! -e "$tmp_folder$num" ];then  break  ;fi
2147     done ; done_func $?
2148     tmp_folder="$tmp_folder$num"
2149     mkdir -p  "$tmp_folder"
2150
2151     link_path="$tmp_folder/$from_name"
2152     StringClass.substring_method  "$DstFolder"  0  1
2153     if [ "$g_Ret" == "/" ];then
2154       ln -s "$DstFolder"  "$link_path"
2155     else
2156       ln -s ".."  "$link_path"
2157     fi
2158
2159     echo  "unzip -o -d  \"$tmp_folder\"  \"$PackagePath\""
2160     unzip -o -d "$tmp_folder"  "$PackagePath"
2161
2162     rm  "$link_path"
2163     rmdir  "$tmp_folder"
2164   fi
2165 }
2166
2167
2168  
2169 #//*********************************************************************
2170 #// <<< [ListUpIn_func] >>> 
2171 #//*********************************************************************
2172 function  ListUpIn_func()
2173 {
2174   local  PackagePath="$1"
2175
2176   local  ext1
2177   local  ext2
2178   local  name
2179
2180   RightOfLastStr_func  "$PackagePath"  "." ; ext2="$g_Ret"
2181   LeftOfLastStr_func   "$PackagePath"  "."
2182   RightOfLastStr_func  "$g_Ret"  "." ; ext1="$g_Ret"
2183
2184   if [ x"$ext1" == x"tar"  -a  x"$ext2" == x"bz2" ];then
2185     bzip2 -dkc "$PackagePath" | tar t
2186   elif [ x"$ext1" == x"tar"  -a  x"$ext2" == x"gz" ];then
2187     tar tfz  "$PackagePath"
2188   elif [ x"$ext1" != x"tar"  -a  x"$ext2" == x"gz" ];then
2189     LeftOfLastStr_func  "$PackagePath"  "." ; name="$g_Ret"
2190     echo  "$name"
2191   elif [ "$ext2" == "zip" ];then
2192     unzip -l "$PackagePath" | sed -e "1,3d" -e '/^ ---/,$d' -e "s/.*:[0-9]* *//"
2193     CheckPipeStatus_func  "${PIPESTATUS[@]}"
2194   else
2195     Error_func  ".$ext.$ext2 形式は、サポートしていません。"
2196   fi
2197 }
2198
2199
2200  
2201 #//*********************************************************************
2202 #// <<< [ExpandWildcard_func] >>>
2203 #//*********************************************************************
2204 function  ExpandWildcard_func()
2205 {
2206   local  out_StepPaths
2207   local  list
2208   local  last
2209
2210   #//=== call ExpandWildcardSub_func
2211   list=`ExpandWildcardSub_func  "$@" `
2212   ArrayClass.fromLines_method  out_StepPaths  "$list"  #//[out] out_StepPaths
2213
2214   #//=== 最後の要素は、フォルダーのパスなので、out_StepPaths へ移動します。
2215   ArrayClass.getLength_method  out_StepPaths
2216   last=$(( $g_Ret - 1 ))
2217   ArrayClass.get_method  out_StepPaths  $last ; folder="$g_Ret"
2218
2219   StringClass.length_method  "$folder" ; length="$g_Ret"
2220   length=$(( $length + 1 ))
2221
2222   ArrayClass.remove_method  out_StepPaths  $last
2223
2224   #//=== change to step_path
2225   for (( last = $last - 1 ;  $last >= 0 ; last -- )) ;do
2226     ArrayClass.get_method  out_StepPaths  $last
2227     StringClass.substring_method  "$g_Ret"  $length
2228     if [ "$g_Ret" == "" ];then  g_Ret="."  ;fi
2229     ArrayClass.set_method  out_StepPaths  $last  "$g_Ret"
2230   done ; done_func $?
2231   g_Ret="$folder"
2232   g_Ret2=( "${out_StepPaths[@]}" )
2233 }
2234
2235
2236 function  ExpandWildcardSub_func()
2237 {
2238   local  WildcardPath="$1"
2239   local  arguments
2240   $declare_associative_array  option
2241   local  a1
2242   local  folder
2243   local  options
2244   local  file_name
2245   local  all_type
2246
2247   GetLongOptions_func  arguments  option  "$@"  #//[out] arguments, option
2248
2249   unset  $out_StepPaths
2250   unset  $out_StepPaths  #// 配列をクリアするには2回必要?
2251
2252   a1=`dirname  "$WildcardPath"`
2253   if [ "$a1" == "" ];then  a1="."  ;fi
2254   GetAbsPath_func  "$a1" ; folder="$g_Ret"
2255
2256   file_name=`basename  "$WildcardPath"`
2257
2258   Attr_func  option  "SubFolder"
2259   if [ "$g_Ret" == "" ];then
2260     options="${options} -maxdepth 1"
2261   fi
2262
2263   all_type="1"
2264
2265   Attr_func  option  "File"
2266   if [ "$g_Ret" == "1" ];then
2267     find  "$folder" $options -name "$file_name" -type f -print
2268     all_type=""
2269   fi
2270
2271   Attr_func  option  "Folder"
2272   if [ "$g_Ret" == "1" ];then
2273     find  "$folder" $options -name "$file_name" -type d -print
2274     all_type=""
2275   fi
2276
2277   Attr_func  option  "Link"
2278   if [ "$g_Ret" == "1" ];then
2279     find  "$folder" $options -name "$file_name" -type l -print
2280     all_type=""
2281   fi
2282
2283   if [ "$all_type" == "1" ];then
2284     find  "$folder" $options -name "$file_name" -print
2285   fi
2286
2287   echo  "$folder"
2288 }
2289
2290
2291  
2292 #//*********************************************************************
2293 #// <<< [rm_func] >>> 
2294 #//*********************************************************************
2295 function  rm_func()
2296 {
2297   local  Paths=( "$@" )
2298   local  path
2299
2300   for path  in "${Paths[@]}" ;do
2301     if [ "$path" != "" ];then
2302       CheckWritable_func  "$path"
2303
2304       if [ -d "$path" ]; then
2305         $g_TemporarySudo  chmod -R a+rw  "$path"
2306         $g_TemporarySudo  rm -rf  "$path"
2307       elif [ -f "$path" ]; then
2308         $g_TemporarySudo  chmod a+rw  "$path"
2309         $g_TemporarySudo  rm -f  "$path"
2310       elif [ -e "$path"  -o  -L "$path" ]; then
2311         $g_TemporarySudo  rm -f  "$path"
2312       fi
2313     fi
2314   done ; done_func $?
2315 }
2316
2317
2318  
2319 #//*********************************************************************
2320 #// <<< [mkdir_func] >>> 
2321 #//*********************************************************************
2322 function  mkdir_func()
2323 {
2324   local  Path="$1"
2325   CheckArgCount_func  1 "$@"
2326
2327   CheckWritable_func  "$Path"
2328
2329   if [ ! -d "$Path" ];then
2330     $g_TemporarySudo  mkdir -p "$Path"
2331   fi
2332 }
2333
2334
2335  
2336 #//*********************************************************************
2337 #// <<< [MakeSymbolicLink_func] >>> 
2338 #//*********************************************************************
2339 function  MakeSymbolicLink_func()
2340 {
2341   local  LinkSrcPath="$1"
2342   local  Target="$2"
2343   CheckArgCount_func  2 "$@"
2344
2345   CheckWritable_func  "$LinkSrcPath"
2346
2347   if [ -d "$LinkSrcPath" ];then
2348     if [ -L "$LinkSrcPath" ];then
2349       rm  "$LinkSrcPath"
2350       $g_TemporarySudo  ln -s  "$Target"  "$LinkSrcPath"
2351     else
2352       Error_func  "フォルダーが存在します"
2353     fi
2354   else
2355     $g_TemporarySudo  ln -sf  "$Target"  "$LinkSrcPath"
2356   fi
2357 }
2358
2359
2360  
2361 #//*********************************************************************
2362 #// <<< [chown_it_and_parent_func] >>> 
2363 #//*********************************************************************
2364 function  chown_it_and_parent_func()
2365 {
2366   local  Option=""
2367   local  Owner="$1"
2368   if [ "$Owner" == "-R" ];then
2369     Option="$Owner"
2370     shift
2371     Owner="$1"
2372   fi
2373   local  Path="$2"
2374   local  parent
2375
2376   CheckWritable_func  "$Path"
2377
2378   GetParentAbsPath_func  "$Path" ; parent="$g_Ret"
2379
2380   if [ -e "$Path" ];then
2381     $g_TemporarySudo  chown  $Option  "$Owner"  "$Path"
2382   fi
2383
2384   if [ ! -d "$parent" ];then
2385     $g_TemporarySudo  mkdir -p  "$parent"
2386   fi
2387   $g_TemporarySudo  chown  $Option  "$Owner"  "$parent"
2388 }
2389
2390
2391  
2392 #//*********************************************************************
2393 #// <<< [AppKeyClass.newWritable_method] >>> 
2394 #//*********************************************************************
2395 function  AppKeyClass.newWritable_method()
2396 {
2397   local  AppKey="$1"
2398   shift  1
2399   local  Args=( "$@" )
2400   local  index
2401   local  count
2402
2403   if [ "$AppKey" != "AppKey4293" ]; then  Error_func  "AppKeyClass.newWritable_method needs \$2 of Main_func"  ;fi
2404
2405   unset g_WritableFolders8920
2406   unset g_WritableFolders8920  #// 配列をクリアするには2回必要?
2407
2408   count=0
2409   for (( index = 0; index < ${#Args[@]}; index ++ ));do
2410     if [ "${Args[$index]}" != "" ];then
2411       GetAbsPath_func  "${Args[$index]}"
2412       g_WritableFolders8920[$count]="$g_Ret"
2413       count=$(( $count + 1 ))
2414     fi
2415   done ; done_func $?
2416 }
2417
2418
2419  
2420 #//*********************************************************************
2421 #// <<< [CheckWritable_func] >>> 
2422 #//*********************************************************************
2423 function  CheckWritable_func()
2424 {
2425   local  Path="$1"
2426   CheckArgCount_func  1 "$@"
2427
2428   local  is_break=""
2429   local  writable
2430
2431   GetAbsPath_func  "$Path"  "$PWD" ; Path="$g_Ret"
2432
2433   if [ "$g_WritableFolders8920" == "" ]; then \
2434     Error_func  "<ERROR msg=\"Not call AppKeyClass.newWritable_method\" Path=\"$Path\"/>"  ;fi
2435   for  writable  in  ${g_WritableFolders8920[@]};do
2436     if [ "${Path%%$writable*}" == "" ]; then  is_break=1 ; break  ;fi
2437   done ; done_func $?
2438   if [ ! "$is_break" == "1" ]; then
2439     Error_func  "<ERROR msg=\"AppKeyClass.newWritable_method による書き込み許可がされていません。\"" \
2440        "Path=\"$Path\" Writable=\"$g_WritableFolders8920\"/>"
2441   fi
2442 }
2443
2444
2445  
2446 #//*********************************************************************
2447 #// <<< [EchoTestStart_func] >>> 
2448 #//*********************************************************************
2449 function  EchoTestStart_func()
2450 {
2451   local  args=("$@")
2452   if [ "${#args[@]}" == "0" ];then
2453     echo  ""
2454     echo  "((( ${FUNCNAME[1]} )))"
2455   else
2456     echo  ""
2457     echo  "((( $@ )))"
2458   fi
2459 }
2460
2461
2462  
2463 #//*********************************************************************
2464 #// <<< [Step] >>> 
2465 #//*********************************************************************
2466 function  Step()
2467 {
2468   g_DebugTrapFunc="StepRunning_func"
2469
2470   trap 'DebugTrap_func  "$LINENO"  "$BASH_COMMAND"  "${PIPESTATUS[@]}"
2471     #// resume ${PIPESTATUS[@]}
2472     case "${#g_PipeStatus[@]}" in
2473       "2")
2474         return ${g_PipeStatus[0]} | true;;
2475       "3")
2476         return ${g_PipeStatus[0]} | return ${g_PipeStatus[1]} | true;;
2477     esac' DEBUG
2478 }
2479
2480 function  StepRunning_func()
2481 {
2482   local  LineNo__="$1"
2483   local  Command__="$2"
2484   shift  2
2485   g_PipeStatus=( "$@" )
2486
2487   local  key__
2488   local  a1__
2489
2490   if [ "$step_running_guided" == "" ]; then
2491     ErrClass.getCallTree_method  "$LINENO"  2  1
2492     echo  "$g_Ret"  >&2
2493     echo  "--- デバッガ情報 -------------------------"  >&2
2494     echo  "ステップ実行 … Enter キーを押してください"  >&2
2495     echo  "変数の値を表示 … 変数名を入力"              >&2
2496     echo  "------------------------------------------"  >&2
2497   fi
2498
2499   echo  "${FUNCNAME[2]}() ${BASH_SOURCE[2]}:${BASH_LINENO[1]}"  >&2
2500   key__="goto_in_while"
2501   while [ "$key__" != "" ]; do
2502     read -p "$LineNo__: $Command__ " key__  #// break at the line
2503
2504     #// inspect variable's value
2505     if [ "$key__" != "" ]; then
2506
2507       case "$key__" in
2508        "LineNo__" | "Command__" | "key__" | "a1__" )
2509         echo  "変数 $key__ の値の表示はサポートしていません。";;
2510
2511        *)
2512         CheckOutParamIsConflictToLocal_func  key__  
2513
2514         key__=${key__/$/}  #// cut first $, if exists
2515         WS  $key__  #// call WS function
2516         ;;
2517       esac
2518     fi
2519   done ; done_func $?
2520
2521   step_running_guided=1
2522 }
2523
2524
2525  
2526 #//*********************************************************************
2527 #// <<< [Watch] >>> 
2528 #//*********************************************************************
2529 function  Watch()
2530 {
2531   local  ExpressionArray__=("$@")
2532   local  Expression__="$@"
2533   local  evaled__
2534
2535   echo  "Watch> ${FUNCNAME[1]}() ${BASH_SOURCE[1]}(${BASH_LINENO[0]})"  >&2
2536   if [ "${#ExpressionArray__[@]}" != "0" ];then
2537
2538     CheckEvalParamIsConflictToLocal_func  "$Expression__"  ExpressionArray__  Expression__  evaled__
2539
2540     evaled__=`eval echo "\"""$Expression__""\""`
2541     if [ "$Expression__" == "$evaled__" ];then
2542       echo  "Watch> \"$Expression__\""  >&2
2543     else
2544       echo  "Watch> \"$Expression__\" == '""$evaled__""'" >&2
2545     fi
2546   fi
2547 }
2548
2549
2550  
2551 #//*********************************************************************
2552 #// <<< [WS] >>> 
2553 #//*********************************************************************
2554 function  WS()
2555 {
2556   local  Name__="$1"
2557   local  keys__
2558   local  value__
2559   local  index__
2560   local  is__
2561
2562   if [ "$g_ExitStatus" == "0" ];then
2563     CheckArgCount_func  1 "$@"
2564
2565     CheckOutParamIsConflictToLocal_func  "$Name__"  Name__  keys__  value__  index__
2566   fi
2567
2568   eval  keys__=( '"${!'"$Name__"'[@]}"' )
2569
2570   is__=0
2571   if [ x"$keys__" == x""  -o  x"$keys__" == x"0" ];then  is__=1 ;fi
2572
2573   if [ "${#keys__[@]}" -le "1"  -a  "$is__" == "1" ];then
2574     eval  value__='"${'"$Name__"'}"'
2575     StringClass.indexOf_method  "$Name__"  "["
2576     if [ "$g_Ret" -ge "0" ];then
2577       echo -n 'Dump "${'"$Name__"}'" == '  >&2
2578     else
2579       echo -n 'Dump "$'"$Name__"'" == '  >&2
2580     fi
2581     echo  "'""$value__""'"  >&2
2582     echo -n "$value__" | hexdump -C  >&2
2583     #// hexdump -C <<< "$value__"  >&2
2584   else
2585     for index__  in "${keys__[@]}" ;do
2586       WS $Name__[$index__]
2587     done ; done_func $?
2588   fi
2589 }
2590
2591
2592  
2593 #//*********************************************************************
2594 #// <<< [Error_func] >>> 
2595 #//*********************************************************************
2596 function  Error_func()
2597 {
2598   ErrClass.getErrStr_method  "$@" ; g_Err_Desc="$g_Ret"
2599   return_func  1
2600 }
2601
2602
2603  
2604 #//*********************************************************************
2605 #// <<< [return_func] >>> 
2606 #//*********************************************************************
2607 function  return_func()
2608 {
2609   if [ "$g_Err_LineNo" == "???" ];then  g_Err_LineNo=${BASH_LINENO[0]}  ;fi
2610   return  "$1"
2611 }
2612
2613
2614  
2615 #//*********************************************************************
2616 #// <<< [CheckPipeStatus_func] >>> 
2617 #//*********************************************************************
2618 function  CheckPipeStatus_func()
2619 {
2620   local  pipe_status="$@"
2621   local  state
2622
2623   for state in ${pipe_status[@]};do
2624     if [ "$state" != "0" ]; then
2625       echo  '${PIPESTATUS[@]} = '"${pipe_status[@]}" >&2
2626       g_Err_LineNo=${BASH_LINENO[0]}
2627       return  "$state"
2628     fi
2629   done ; done_func $?
2630 }
2631
2632
2633  
2634 #//*********************************************************************
2635 #// <<< [CheckArgCount_func] >>> 
2636 #//*********************************************************************
2637 function  CheckArgCount_func()
2638 {
2639   local  RequestArgumentCount="$1"
2640   shift  1
2641   local  Arguments=( "$@" )
2642   local  str
2643
2644   if [ "${#Arguments[@]}" -ne "$RequestArgumentCount" ];then
2645     str="パラメーターの数が合っていません。 指定=${#Arguments[@]}, 要求=$RequestArgumentCount,"
2646     str="$str コマンドライン: ${FUNCNAME[1]} ${Arguments[@]}"
2647     Error_func  "$str"
2648   fi
2649 }
2650
2651
2652  
2653 #//*********************************************************************
2654 #// <<< [CheckMinArgCount_func] >>> 
2655 #//*********************************************************************
2656 function  CheckMinArgCount_func()
2657 {
2658   local  RequestArgumentCount="$1"
2659   shift  1
2660   local  Arguments=( "$@" )
2661   if [ "${#Arguments[@]}" -lt "$RequestArgumentCount" ];then
2662     echo  "パラメーターが少なすぎます。 指定=${#Arguments[@]}, 要求=$RequestArgumentCount"  >&2
2663     echo  "コマンドライン: ${FUNCNAME[1]} ${Arguments[@]}"  >&2
2664     Error_func
2665   fi
2666 }
2667
2668
2669  
2670 #//*********************************************************************
2671 #// <<< [CheckMaxArgCount_func] >>> 
2672 #//*********************************************************************
2673 function  CheckMaxArgCount_func()
2674 {
2675   local  RequestArgumentCount="$1"
2676   shift  1
2677   local  Arguments=( "$@" )
2678   if [ "${#Arguments[@]}" -gt "$RequestArgumentCount" ];then
2679     echo  "パラメーターが多すぎます。 指定=${#Arguments[@]}, 要求=$RequestArgumentCount"  >&2
2680     echo  "コマンドライン: ${FUNCNAME[1]} ${Arguments[@]}"  >&2
2681     Error_func
2682   fi
2683 }
2684
2685
2686  
2687 #//*********************************************************************
2688 #// <<< [CheckOutParamIsConflictToLocal_func] >>> 
2689 #//*********************************************************************
2690 function  CheckOutParamIsConflictToLocal_func()
2691 {
2692   local  OutParamName__="$1"
2693   shift  1
2694   local  LocalNames__=( "$@" )
2695   local  name__
2696   local  a1__
2697
2698   if [ "$OutParamName__" == "" ];then  Error_func  "出力変数名が指定されていません"  ;fi
2699
2700   for name__  in "${LocalNames__[@]}" ;do
2701     if [ "$OutParamName__" == "$name__" ];then
2702       a1__="<ERROR msg=\"ローカル変数名が衝突しています\" "
2703       a1__="${a1__}local=\"$name__\"/>"
2704       Error_func  "$a1__"
2705     fi
2706   done ; done_func $?
2707 }
2708
2709
2710  
2711 #//*********************************************************************
2712 #// <<< [CheckEvalParamIsConflictToLocal_func] >>> 
2713 #//*********************************************************************
2714 function  CheckEvalParamIsConflictToLocal_func()
2715 {
2716   local  EvalExpression="$1"
2717   shift  1
2718   local  LocalNames=( "$@" )
2719   local  name
2720   local  s
2721
2722   for name  in "${LocalNames[@]}" ;do
2723     if [ "${EvalExpression%%$name*}" != "$EvalExpression"  ];then
2724       s="<ERROR msg=\"ローカル変数名が衝突している可能性があります\" "
2725       s="${s}local=\"$name\"/>"
2726       Error_func  "$s"
2727     fi
2728   done ; done_func $?
2729 }
2730
2731
2732  
2733 #//*********************************************************************
2734 #// <<< [Assert_func] >>> 
2735 #//*********************************************************************
2736 function  Assert_func()
2737 {
2738   local  Expression__="$*"
2739   local  is_pass__
2740   local  message__
2741   local  result__
2742
2743   eval  ' if [ '"$Expression__"' ]; then  is_pass__=1  ;else  is_pass__=0 ;fi '
2744
2745   if [ "$is_pass__" == "0" ];then  result__="`eval  echo "$Expression__"`"  ;fi
2746
2747   if false; then  #// ture, if debug
2748     echo  "<Assert_func"  >&2
2749     echo  "Expression__='$Expression__'"  >&2
2750     echo  "result__='$result__'"  >&2
2751     echo  "is_pass__='$is_pass__'/>"  >&2
2752   fi
2753
2754   if [ "$is_pass__" == "0" ];then
2755     message__=`echo -n "<ERROR msg=\"Assert failed\"><Expression><![CDATA[${LF}$Expression__${LF}]]></Expression>"`
2756     if [ "$Expression__" != "$result__" ];then
2757       message__="$message__"`echo -n "<Result><![CDATA[${LF}$result__${LF}]]></Result>"`
2758     fi
2759     message__="$message__</ERROR>"
2760     Error_func  "$message__"
2761   fi
2762 }
2763
2764
2765  
2766 #//*********************************************************************
2767 #// <<< [TryStart_func] >>> 
2768 #// <<< [TryEnd1_func] >>> 
2769 #// <<< [TryEnd2_func] >>> 
2770 #//*********************************************************************
2771 function  TryStart_func()
2772 {
2773   true
2774   g_Err_NestLevel=$(( $g_Err_NestLevel + 1 ))
2775 }
2776
2777 function  TryEnd1_func()
2778 {
2779   break
2780 }
2781
2782 function  TryEnd2_func()
2783 {
2784   if [ "$g_ExitStatus" == "0" ];then
2785     g_ExitStatus=$1
2786   fi
2787   g_Err_NestLevel=$(( $g_Err_NestLevel - 1 ))
2788 }
2789
2790
2791  
2792 #//*********************************************************************
2793 #// <<< [ErrTrap_func] >>> 
2794 #//*********************************************************************
2795
2796 g_ExitStatus=0
2797 g_Ret=""
2798 g_Ret2=""
2799 g_Ret3=""
2800 g_Err_IsDone=0
2801 g_Err_IsOverwrite=0
2802 g_Err_NestLevel=0
2803 g_Err_ErrID=0
2804 g_Err_Desc=""
2805 g_Err_LineNo="???"
2806 g_PipeStatus=""
2807 g_DebugTrapFunc=""
2808
2809 function  ErrTrap_func()
2810 {
2811   local  a1
2812
2813   if [ "$g_Err_IsDone" == "1" ];then
2814     g_Err_IsDone=0
2815   elif [ "$g_Err_IsOverwrite" == "1" ];then
2816     g_Err_IsOverwrite=0
2817   else
2818
2819     g_Err_ErrID=$(( $g_Err_ErrID + 1 ))
2820
2821     if [ "$g_ExitStatus" == "0" ];then
2822       if [ "$g_Err_LineNo" == "???" ];then  g_Err_LineNo=$1  ;fi
2823       if [ "$g_Err_LineNo" == "???" ]; then
2824         a1="${a1}(ヒント)現在の行番号は、${FUNCNAME[1]} 関数の最初で \"EchoOn_func\" を呼ぶと表示されます。${LF}"
2825       fi
2826       a1="${a1}(開発者向けヒント)ステップ実行したいときは、開始するところから \"Step\" 関数を呼び出してください。 "
2827       a1="${a1}下記コールツリーの最も下の関数が、\` \` を使って echo 出力を取得しているときは、取得しないようにすると、更にコール先の関数が表示されます。${LF}"
2828       ErrClass.getCallTree_method  "$g_Err_LineNo"  2  1
2829       a1="$a1$g_Ret"
2830       g_Err_ErrCallStack="$a1"
2831     else
2832       echo  "<ERROR msg=\"エラー処理中に別のエラーが発生しました。\"/>"  >&2
2833       ErrClass.getCallTree_method  "${BASH_LINENO[0]}"  2  1
2834       echo  "$g_Ret"  >&2
2835     fi
2836   fi
2837 }
2838
2839
2840  
2841 #//*********************************************************************
2842 #// <<< [done_func] >>> 
2843 #//*********************************************************************
2844 function  done_func()
2845 {
2846   CheckArgCount_func  1 "$@"
2847   if [ "$1" != "0" ];then  g_Err_IsDone=1  ;fi
2848   return  "$1"  #// if not 0, throw again
2849 }
2850
2851
2852  
2853 #//*********************************************************************
2854 #// <<< [ErrClass.raiseOverwrite_method] >>> 
2855 #//*********************************************************************
2856 function  ErrClass.raiseOverwrite_method()
2857 {
2858   local  message ; ErrClass.getErrStr_method  "$@" ; message="$g_Ret"
2859   local  exit_status="$g_ExitStatus"
2860
2861   if [ "$message" != "" ];then
2862     g_Err_Desc="$message"
2863   fi
2864
2865   if [ "$exit_status" == "0" ];then
2866     exit_status=1
2867   fi
2868
2869   g_Err_IsOverwrite=1
2870   return  $exit_status
2871 }
2872
2873
2874  
2875 #//*********************************************************************
2876 #// <<< [ErrClass.clear_method] >>> 
2877 #//*********************************************************************
2878 function  ErrClass.clear_method()
2879 {
2880   g_ExitStatus=0
2881   g_Err_Desc=""
2882   g_Err_LineNo="???"
2883   g_PipeStatus=""
2884 }
2885
2886
2887  
2888 #//*********************************************************************
2889 #// <<< [ErrClass.getErrStr_method] >>> 
2890 #//*********************************************************************
2891 function  ErrClass.getErrStr_method()
2892 {
2893   local  Message="$@"
2894
2895   if [ "$Message" != "" ]; then
2896     if [ "${Message%%<ERROR *}" == "" ]; then
2897       g_Ret="$Message"
2898     else
2899       StringClass.replace_method  "$Message"  "&"  "&amp;"
2900       StringClass.replace_method  "$g_Ret"  "<"  "&lt;"
2901       StringClass.replace_method  "$g_Ret"  "\""  "&quot;"
2902       g_Ret="<ERROR msg=\"$g_Ret\"/>"
2903     fi
2904   else
2905     g_Ret=""
2906   fi
2907 }
2908
2909
2910  
2911 #//*********************************************************************
2912 #// <<< [ErrClass.getCallTree_method] >>> 
2913 #//*********************************************************************
2914 function  ErrClass.getCallTree_method()
2915 {
2916   local  LineNo="$1"
2917   local  TopIndex="$2"
2918   local  IsAbleLastCut="$3"
2919   local  indent=" "
2920   local  s
2921
2922   s="コールツリー:"
2923   i=$(( ${#FUNCNAME[@]} - 1 ))
2924   s="$s${LF}""(global) ${BASH_SOURCE[$i]}:${BASH_LINENO[$i-1]}"
2925   for(( i=${#FUNCNAME[@]} - 2; i > $TopIndex; i-- ));do
2926     s="$s${LF}${indent}${FUNCNAME[$i]}() ${BASH_SOURCE[$i]}:${BASH_LINENO[$i-1]}"
2927     indent="${indent} "
2928   done ; done_func $?
2929
2930   case  "${FUNCNAME[$TopIndex]}"  in
2931     "Error_func" | "DebugTrap_func" ) ;;
2932     *)  IsAbleLastCut=0 ;;
2933   esac
2934   if [ "$IsAbleLastCut" != "1" ];then
2935     s="$s${LF}${indent}${FUNCNAME[$TopIndex]}() ${BASH_SOURCE[$TopIndex]}:$LineNo"
2936   fi
2937   g_Ret="$s"
2938 }
2939
2940
2941  
2942 #//*********************************************************************
2943 #// <<< [Exit_func] >>> 
2944 #//*********************************************************************
2945 function  Exit_func()
2946 {
2947   g_DebugTrapFunc=""
2948   trap ':' EXIT
2949   exit $ret
2950 }
2951
2952
2953  
2954 #//*********************************************************************
2955 #// <<< [declare_associative_array] >>> 
2956 #//*********************************************************************
2957 if [ "$declare_associative_array" == "" ];then
2958   if [ "${BASH_VERSINFO[0]}" -ge "4" ];then
2959     declare_associative_array="declare -A"
2960   else
2961     declare_associative_array="declare"
2962   fi
2963 fi
2964
2965
2966  
2967 #//*********************************************************************
2968 #// <<< [g_ArrayLength] >>> 
2969 #//*********************************************************************
2970 g_ArrayLength=0
2971
2972
2973  
2974 #//*********************************************************************
2975 #// <<< [g_AssociativeArrayMaxLength] >>> 
2976 #//*********************************************************************
2977 if [ "$g_AssociativeArrayMaxLength" == "" ];then  g_AssociativeArrayMaxLength=100  ;fi
2978
2979 g_AssociativeArrayLength=0
2980 for (( i = 0; i < $g_AssociativeArrayMaxLength; i ++ ));do
2981   $declare_associative_array  g_AssociativeArrays_$i
2982 done
2983
2984
2985  
2986 #//*********************************************************************
2987 #// <<< [LF] >>> 
2988 #// <<< [Tab] >>> 
2989 #//*********************************************************************
2990 LF=`echo_e_func "\nx"`; LF="${LF:0:1}"
2991 Tab=`echo_e_func "\t"`
2992
2993
2994