tasty revision to load feisty for full use
[feisty_meow.git] / scripts / core / functions.sh
1 #!/bin/bash
2
3 # This defines some general, useful functions.
4
5 # test whether we've been here before or not.
6 skip_all=
7 type function_sentinel &>/dev/null
8 if [ $? -eq 0 ]; then
9   # there was no error, so we can skip the inits.
10   if [ ! -z "$SHELL_DEBUG" ]; then
11     echo "skipping function definitions, because already defined."
12   fi
13   skip_all=yes
14 else
15   skip_all=
16 fi
17
18 if [ -z "$skip_all" ]; then
19
20   if [ ! -z "$SHELL_DEBUG" ]; then
21     echo "feisty meow function definitions beginning now..."
22   fi
23
24   # a handy little method that can be used for date strings.  it was getting
25   # really tiresome how many different ways the script did the date formatting.
26   function date_stringer() {
27     local sep="$1"; shift
28     if [ -z "$sep" ]; then sep='_'; fi
29     date +"%Y$sep%m$sep%d$sep%H%M$sep%S" | tr -d '/\n/'
30   }
31   
32   # makes a directory of the name specified and then tries to change the
33   # current directory to that directory.
34   function mcd() {
35     if [ ! -d "$1" ]; then mkdir -p "$1"; fi
36     cd "$1"
37   }
38
39   function is_array() {
40     [[ "$(declare -p $1)" =~ "declare -a" ]]
41   }
42
43   function is_alias() {
44     alias $1 &>/dev/null
45     return $?
46   }
47
48   # displays the value of a variable in bash friendly format.
49   function var() {
50     HOLDIFS="$IFS"
51     IFS=""
52     while true; do
53       local varname="$1"; shift
54       if [ -z "$varname" ]; then
55         break
56       fi
57
58       if is_alias "$varname"; then
59 #echo found $varname is alias
60         local tmpfile="$(mktemp $TMP/aliasout.XXXXXX)"
61         alias $varname | sed -e 's/.*=//' >$tmpfile
62         echo "alias $varname=$(cat $tmpfile)"
63         \rm $tmpfile
64       elif [ -z "${!varname}" ]; then
65         echo "$varname undefined"
66       else
67         if is_array "$varname"; then
68 #echo found $varname is array var 
69           local temparray
70           eval temparray="(\${$varname[@]})"
71           echo "$varname=(${temparray[@]})"
72 #hmmm: would be nice to print above with elements enclosed in quotes, so that we can properly
73 # see ones that have spaces in them.
74         else
75 #echo found $varname is simple
76           echo "$varname=${!varname}"
77         fi
78       fi
79     done | sort
80     IFS="$HOLDIFS"
81   }
82
83   # sets the variable in parameter 1 to the value in parameter 2, but only if
84   # that variable was undefined.
85   function set_var_if_undefined()
86   {
87     local var_name="$1"; shift
88     local var_value="$1"; shift
89     if [ -z "${!var_name}" ]; then
90       eval export $var_name="$var_value"
91     fi
92   }
93
94   function success_sound()
95   {
96     if [ ! -z "$CLAM_FINISH_SOUND" ]; then
97       bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh "$CLAM_FINISH_SOUND"
98     fi
99   }
100
101   function error_sound()
102   {
103     if [ ! -z "$CLAM_ERROR_SOUND" ]; then
104       bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh "$CLAM_ERROR_SOUND"
105     fi
106   }
107
108   # checks the result of the last command that was run, and if that failed,
109   # then this complains and exits from bash.  the function parameters are
110   # used as the message to print as a complaint.
111   function check_result()
112   {
113     if [ $? -ne 0 ]; then
114       echo -e "failed on: $*"
115       error_sound
116       exit 1
117     fi
118   }
119
120   # locates a process given a search pattern to match in the process list.
121   # supports a single command line flag style parameter of "-u USERNAME";
122   # if the -u flag is found, a username is expected afterwards, and only the
123   # processes of that user are considered.
124   function psfind() {
125     local -a patterns=("${@}")
126 #echo ====
127 #echo patterns list is: "${patterns[@]}"
128 #echo ====
129
130     local user_flag
131     if [ "${patterns[0]}" == "-u" ]; then
132       user_flag="-u ${patterns[1]}" 
133 #echo "found a -u parm and user=${patterns[1]}" 
134       # void the two elements with that user flag so we don't use them as patterns.
135       unset patterns[0] patterns[1]=
136     else
137       # select all users.
138       user_flag="-e"
139     fi
140
141     local PID_DUMP="$(mktemp "$TMP/zz_pidlist.XXXXXX")"
142     local -a PIDS_SOUGHT
143
144     if [ "$OS" == "Windows_NT" ]; then
145       # gets cygwin's (god awful) ps to show windoze processes also.
146       local EXTRA_DOZER_FLAGS="-W"
147       # pattern to use for peeling off the process numbers.
148       local pid_finder_pattern='s/ *\([0-9][0-9]*\) *.*$/\1/p'
149
150     else
151       # flags which clean up the output on unixes, which apparently cygwin
152       # doesn't count as.  their crappy specialized ps doesn't support this.
153       local EXTRA_UNIX_FLAGS="-o pid,args"
154       # pattern to use for peeling off the process numbers.
155       local pid_finder_pattern='s/^[[:space:]]*\([0-9][0-9]*\).*$/\1/p'
156     fi
157
158     /bin/ps $EXTRA_DOZER_FLAGS $EXTRA_UNIX_FLAGS $user_flag | tail -n +2 >$PID_DUMP
159 #echo ====
160 #echo got all this stuff in the pid dump file:
161 #cat $PID_DUMP
162 #echo ====
163
164     # search for the pattern the user wants to find, and just pluck the process
165     # ids out of the results.
166     local i
167     for i in "${patterns[@]}"; do
168       PIDS_SOUGHT+=($(cat $PID_DUMP \
169         | grep -i "$i" \
170         | sed -n -e "$pid_finder_pattern"))
171     done
172 #echo ====
173 #echo pids sought list became:
174 #echo "${PIDS_SOUGHT[@]}"
175 #echo ====
176
177     if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
178       local PIDS_SOUGHT2=$(printf -- '%s\n' ${PIDS_SOUGHT[@]} | sort | uniq)
179       PIDS_SOUGHT=()
180       PIDS_SOUGHT=${PIDS_SOUGHT2[*]}
181       echo ${PIDS_SOUGHT[*]}
182     fi
183     /bin/rm $PID_DUMP
184   }
185   
186   # finds all processes matching the pattern specified and shows their full
187   # process listing (whereas psfind just lists process ids).
188   function psa() {
189     if [ -z "$1" ]; then
190       echo "psa finds processes by pattern, but there was no pattern on the command line."
191       return 1
192     fi
193     local -a patterns=("${@}")
194     p=$(psfind "${patterns[@]}")
195     if [ -z "$p" ]; then
196       # no matches.
197       return 0
198     fi
199
200     if [ "${patterns[0]}" == "-u" ]; then
201       # void the two elements with that user flag so we don't use them as patterns.
202       unset patterns[0] patterns[1]=
203     fi
204
205     echo ""
206     echo "Processes matching ${patterns[@]}..."
207     echo ""
208     if [ -n "$IS_DARWIN" ]; then
209       unset fuzil_sentinel
210       for i in $p; do
211         # only print the header the first time.
212         if [ -z "$fuzil_sentinel" ]; then
213           ps $i -w -u
214         else
215           ps $i -w -u | sed -e '1d'
216         fi
217         fuzil_sentinel=true
218       done
219     else 
220       # cases besides mac os x's darwin.
221       if [ "$OS" == "Windows_NT" ]; then
222         # special case for windows.
223         ps | head -1
224         for curr in $p; do
225           ps -W -p $curr | tail -n +2
226         done
227       else
228         # normal OSes can handle a nice simple query.
229         ps wu $p
230       fi
231     fi
232   }
233   
234   # an unfortunately similarly named function to the above 'ps' as in process
235   # methods, but this 'ps' stands for postscript.  this takes a postscript file
236   # and converts it into pcl3 printer language and then ships it to the printer.
237   # this mostly makes sense for an environment where one's default printer is
238   # pcl.  if the input postscript causes ghostscript to bomb out, there has been
239   # some good success running ps2ps on the input file and using the cleaned
240   # postscript file for printing.
241   function ps2pcl2lpr() {
242     for $i in $*; do
243       gs -sDEVICE=pcl3 -sOutputFile=- -sPAPERSIZE=letter "$i" | lpr -l 
244     done
245   }
246   
247 #  function fix_alsa() {
248 #    sudo /etc/init.d/alsasound restart
249 #  }
250
251   function screen() {
252     save_terminal_title
253 #hmmm: ugly absolute path here.
254     /usr/bin/screen $*
255     restore_terminal_title
256   }
257   
258   # switches from a /X/path form to an X:/ form.  this also processes cygwin paths.
259   function unix_to_dos_path() {
260     # we usually remove dos slashes in favor of forward slashes.
261     local DOSSYHOME
262     if [[ ! "$OS" =~ ^[Ww][iI][nN] ]]; then
263       # fake this value for non-windows (non-cygwin) platforms.
264       DOSSYHOME="$HOME"
265     else
266       # for cygwin, we must replace the /home/X path with an absolute one, since cygwin
267       # insists on the /home form instead of /c/cygwin/home being possible.  this is
268       # super frustrating and nightmarish.
269       DOSSYHOME="$(cygpath -am "$HOME")"
270     fi
271
272     if [ ! -z "$SERIOUS_SLASH_TREATMENT" ]; then
273       # unless this flag is set, in which case we force dos slashes.
274       echo "$1" | sed -e "s?^$HOME?$DOSSYHOME?g" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/' | sed -e 's/\//\\/g'
275     else
276       echo "$1" | sed -e "s?^$HOME?$DOSSYHOME?g" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/'
277     fi
278   }
279   
280   # switches from an X:/ form to a /cygdrive/X/path form.  this is only useful
281   # for the cygwin environment currently.
282   function dos_to_unix_path() {
283     # we always remove dos slashes in favor of forward slashes.
284 #old:    echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/\1\/\2/'
285          echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/cygdrive\/\1\/\2/'
286   }
287
288   # returns a successful value (0) if this system is debian or ubuntu.
289   function debian_like() {
290     # decide if we think this is debian or ubuntu or a variant.
291     DEBIAN_LIKE=$(if [ ! -z "$(grep -i debian /etc/issue)" \
292         -o ! -z "$(grep -i ubuntu /etc/issue)" ]; then echo 1; else echo 0; fi)
293     if [ $DEBIAN_LIKE -eq 1 ]; then
294       # success; this is debianish.
295       return 0
296     else
297       # this seems like some other OS.
298       return 1
299     fi
300   }
301   
302   # su function: makes su perform a login.
303   # for some OSes, this transfers the X authority information to the new login.
304   function su() {
305     if debian_like; then
306       # debian currently requires the full version which imports X authority
307       # information for su.
308   
309       # get the x authority info for our current user.
310       source "$FEISTY_MEOW_SCRIPTS/x_win/get_x_auth.sh"
311   
312       if [ -z "$X_auth_info" ]; then
313         # if there's no authentication info to pass along, we just do a normal su.
314         /bin/su -l $*
315       else
316         # under X, we update the new login's authority info with the previous
317         # user's info.
318         (unset XAUTHORITY; /bin/su -l $* -c "$X_auth_info ; export DISPLAY=$DISPLAY ; bash")
319       fi
320     else
321       # non-debian supposedly doesn't need the extra overhead any more.
322       # or at least suse doesn't, which is the other one we've tested on.
323       /bin/su -l $*
324     fi
325   }
326   
327   # sudo function wraps the normal sudo by ensuring we replace the terminal
328   # label if they're doing an su with the sudo.
329   function sudo() {
330 #    local first_command="$1"
331     save_terminal_title
332     /usr/bin/sudo "$@"
333     restore_terminal_title
334 #    if [ "$first_command" == "su" ]; then
335 #      # yep, they were doing an su, but they're back now.
336 #      label_terminal_with_info
337 #    fi
338   }
339   
340   # trashes the .#blah files that cvs and svn leave behind when finding conflicts.
341   # this kind of assumes you've already checked them for any salient facts.
342   function clean_cvs_junk() {
343     for i in $*; do
344       find $i -follow -type f -iname ".#*" -exec perl $FEISTY_MEOW_SCRIPTS/files/safedel.pl {} ";" 
345     done
346   }
347
348   # overlay for nechung binary so that we can complain less grossly about it when it's missing.
349   function nechung() {
350     local wheres_nechung=$(which nechung 2>/dev/null)
351     if [ -z "$wheres_nechung" ]; then
352       echo "The nechung oracle program cannot be found.  You may want to consider"
353       echo "rebuilding the feisty meow applications with this command:"
354       echo "bash $FEISTY_MEOW_SCRIPTS/generator/produce_feisty_meow.sh"
355     else
356       $wheres_nechung
357     fi
358   }
359   
360   # recreates all the generated files that the feisty meow scripts use.
361   function regenerate() {
362     # do the bootstrapping process again.
363     save_terminal_title
364     echo "regenerating feisty meow script environment."
365     bash $FEISTY_MEOW_SCRIPTS/core/reconfigure_feisty_meow.sh
366     echo
367     # force a full reload by turning off sentinel variables and methods.
368     unset -v CORE_VARIABLES_LOADED FEISTY_MEOW_LOADING_DOCK USER_CUSTOMIZATIONS_LOADED
369     unalias CORE_ALIASES_LOADED &>/dev/null
370     unset -f function_sentinel 
371     # reload feisty meow environment in current shell.
372     source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
373     # run nechung oracle to give user a new fortune.
374     nechung
375     restore_terminal_title
376   }
377
378   # copies a set of custom scripts into the proper location for feisty meow
379   # to merge their functions and aliases with the standard set.
380   function recustomize()
381   {
382     local custom_user="$1"; shift
383     if [ -z "$custom_user" ]; then
384       # use our default example user if there was no name provided.
385       custom_user=fred
386     fi
387
388     save_terminal_title
389
390     if [ ! -d "$FEISTY_MEOW_SCRIPTS/customize/$custom_user" ]; then
391       echo "The customization folder provided for $custom_user should be:"
392       echo "  '$FEISTY_MEOW_SCRIPTS/customize/$custom_user'"
393       echo "but that folder does not exist.  Skipping customization."
394       return 1
395     fi
396     regenerate >/dev/null
397     pushd "$FEISTY_MEOW_LOADING_DOCK/custom" &>/dev/null
398     incongruous_files="$(bash "$FEISTY_MEOW_SCRIPTS/files/list_non_dupes.sh" "$FEISTY_MEOW_SCRIPTS/customize/$custom_user" "$FEISTY_MEOW_LOADING_DOCK/custom")"
399     
400     #echo "the incongruous files list is: $incongruous_files"
401     # disallow a single character result, since we get "*" as result when nothing exists yet.
402     if [ ${#incongruous_files} -ge 2 ]; then
403       echo "cleaning unknown older overrides..."
404       perl "$FEISTY_MEOW_SCRIPTS/files/safedel.pl" $incongruous_files
405       echo
406     fi
407     popd &>/dev/null
408     echo "copying custom overrides for $custom_user"
409     mkdir -p "$FEISTY_MEOW_LOADING_DOCK/custom" 2>/dev/null
410     perl "$FEISTY_MEOW_SCRIPTS/text/cpdiff.pl" "$FEISTY_MEOW_SCRIPTS/customize/$custom_user" "$FEISTY_MEOW_LOADING_DOCK/custom"
411     if [ -d "$FEISTY_MEOW_SCRIPTS/customize/$custom_user/scripts" ]; then
412       echo "copying custom scripts for $custom_user"
413       \cp -R "$FEISTY_MEOW_SCRIPTS/customize/$custom_user/scripts" "$FEISTY_MEOW_LOADING_DOCK/custom/"
414     fi
415     echo
416     regenerate
417
418     restore_terminal_title
419   }
420
421   # generates a random password where the first parameter is the number of characters
422   # in the password (default 20) and the second parameter specifies whether to use
423   # special characters (1) or not (0).
424   # found function at http://legroom.net/2010/05/06/bash-random-password-generator
425   function random_password()
426   {
427     [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
428     cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-32}
429     echo
430   }
431
432   # a wrapper for the which command that finds items on the path.  some OSes
433   # do not provide which, so we want to not be spewing errors when that
434   # happens.
435   function whichable()
436   {
437     to_find="$1"; shift
438     which which &>/dev/null
439     if [ $? -ne 0 ]; then
440       # there is no which command here.  we produce nothing due to this.
441       echo
442     fi
443     echo $(which $to_find)
444   }
445
446 #hmmm: improve this by not adding the link
447 # if already there, or if the drive is not valid.
448   function add_cygwin_drive_mounts() {
449     for i in c d e f g h q z ; do
450       ln -s /cygdrive/$i $i
451     done
452   }
453
454   ############################
455
456   # takes a file to modify, and then it will replace any occurrences of the
457   # pattern provided as the second parameter with the text in the third
458   # parameter.
459   function replace_pattern_in_file()
460   {
461     local file="$1"; shift
462     local pattern="$1"; shift
463     local replacement="$1"; shift
464     if [ -z "$file" -o -z "$pattern" -o -z "$replacement" ]; then
465       echo "replace_pattern_in_file: needs a filename, a pattern to replace, and the"
466       echo "text to replace that pattern with."
467       return 1
468     fi
469     sed -i -e "s%$pattern%$replacement%g" "$file"
470   }
471
472   # similar to replace_pattern_in_file, but also will add the new value
473   # when the old one did not already exist in the file.
474   function replace_if_exists_or_add()
475   {
476     local file="$1"; shift
477     local phrase="$1"; shift
478     local replacement="$1"; shift
479     if [ -z "$file" -o ! -f "$file" -o -z "$phrase" -o -z "$replacement" ]; then
480       echo "replace_if_exists_or_add: needs a filename, a phrase to replace, and the"
481       echo "text to replace that phrase with."
482       return 1
483     fi
484     grep "$phrase" "$file" >/dev/null
485     # replace if the phrase is there, otherwise add it.
486     if [ $? -eq 0 ]; then
487       replace_pattern_in_file "$file" "$phrase" "$replacement"
488     else
489       # this had better be the complete line.
490       echo "$replacement" >>"$file"
491     fi
492   }
493
494   ############################
495
496   # finds a variable (first parameter) in a particular property file
497   # (second parameter).  the expected format for the file is:
498   # varX=valueX
499   function seek_variable()
500   {
501     local find_var="$1"; shift
502     local file="$1"; shift
503     if [ -z "$find_var" -o -z "$file" -o ! -f "$file" ]; then
504       echo -e "seek_variable: needs two parameters, firstly a variable name, and\nsecondly a file where the variable's value will be sought." 1>&2
505       return 1
506     fi
507   
508     while read line; do
509       if [ ${#line} -eq 0 ]; then continue; fi
510       # split the line into the variable name and value.
511       IFS='=' read -a assignment <<< "$line"
512       local var="${assignment[0]}"
513       local value="${assignment[1]}"
514       if [ "${value:0:1}" == '"' ]; then
515         # assume the entry was in quotes and remove them.
516         value="${value:1:$((${#value} - 2))}"
517       fi
518       if [ "$find_var" == "$var" ]; then
519         echo "$value"
520       fi
521     done < "$file"
522   }
523   
524   # finds a variable (first parameter) in a particular XML format file
525   # (second parameter).  the expected format for the file is:
526   # ... name="varX" value="valueX" ...
527   function seek_variable_in_xml()
528   {
529     local find_var="$1"; shift
530     local file="$1"; shift
531     if [ -z "$find_var" -o -z "$file" -o ! -f "$file" ]; then
532       echo "seek_variable_in_xml: needs two parameters, firstly a variable name, and"
533       echo "secondly an XML file where the variable's value will be sought."
534       return 1
535     fi
536   
537     while read line; do
538       if [ ${#line} -eq 0 ]; then continue; fi
539       # process the line to make it more conventional looking.
540       line="$(echo "$line" | sed -e 's/.*name="\([^"]*\)" value="\([^"]*\)"/\1=\2/')"
541       # split the line into the variable name and value.
542       IFS='=' read -a assignment <<< "$line"
543       local var="${assignment[0]}"
544       local value="${assignment[1]}"
545       if [ "${value:0:1}" == '"' ]; then
546         # assume the entry was in quotes and remove them.
547         value="${value:1:$((${#value} - 2))}"
548       fi
549       if [ "$find_var" == "$var" ]; then
550         echo "$value"
551       fi
552     done < "$file"
553   }
554   
555   ############################
556
557   # goes to a particular directory passed as parameter 1, and then removes all
558   # the parameters after that from that directory.
559   function push_whack_pop()
560   {
561     local dir="$1"; shift
562     pushd "$dir" &>/dev/null
563     if [ $? -ne 0 ]; then echo failed to enter dir--quitting.; fi
564     rm -rf $* &>/dev/null
565     if [ $? -ne 0 ]; then echo received a failure code when removing.; fi
566     popd &>/dev/null
567   }
568
569   function spacem()
570   {
571     while [ $# -gt 0 ]; do
572       arg="$1"; shift
573       if [ ! -f "$arg" -a ! -d "$arg" ]; then
574         echo "failure to find a file or directory named '$arg'."
575         continue
576       fi
577
578       # first we will capture the output of the character replacement operation for reporting.
579       # this is done first since some filenames can't be properly renamed in perl (e.g. if they
580       # have pipe characters apparently).
581       intermediate_name="$(bash "$FEISTY_MEOW_SCRIPTS/files/replace_spaces_with_underscores.sh" "$arg")"
582       local saw_intermediate_result=0
583       if [ -z "$intermediate_name" ]; then
584         # make sure we report something, if there are no further name changes.
585         intermediate_name="'$arg'"
586       else 
587         # now zap the first part of the name off (since original name isn't needed).
588         intermediate_name="$(echo $intermediate_name | sed -e 's/.*=> //')"
589         saw_intermediate_result=1
590       fi
591
592       # first we rename the file to be lower case.
593       actual_file="$(echo $intermediate_name | sed -e "s/'\([^']*\)'/\1/")"
594       final_name="$(perl $FEISTY_MEOW_SCRIPTS/files/renlower.pl "$actual_file")"
595       local saw_final_result=0
596       if [ -z "$final_name" ]; then
597         final_name="$intermediate_name"
598       else
599         final_name="$(echo $final_name | sed -e 's/.*=> //')"
600         saw_final_result=1
601       fi
602 #echo intermed=$saw_intermediate_result 
603 #echo final=$saw_final_result 
604
605       if [[ $saw_intermediate_result != 0 || $saw_final_result != 0 ]]; then
606         # printout the combined operation results.
607         echo "'$arg' => $final_name"
608       fi
609     done
610   }
611
612   ##############
613
614 # new breed of definer functions goes here.  still in progress.
615
616   # defines an alias and remembers that this is a new or modified definition.
617   # if the feisty meow codebase is unloaded, then so are all the aliases that
618   # were defined.
619   function define_yeti_alias()
620   {
621 # if alias exists already, save old value for restore,
622 # otherwise save null value for restore,
623 # have to handle unaliasing if there was no prior value of one
624 # we newly defined.
625 # add alias name to a list of feisty defined aliases.
626
627 #hmmm: first implem, just do the alias and get that working...
628 alias "${@}"
629
630
631 return 0
632   }
633
634   # defines a variable within the feisty meow environment and remembers that
635   # this is a new or modified definition.  if the feisty meow codebase is
636   # unloaded, then so are all the variables that were defined.
637   # this function always exports the variables it defines.
638 #  function define_yeti_variable()
639 #  {
640 ## if variable exists already, save old value for restore,
641 ## otherwise save null value for restore,
642 ## have to handle unsetting if there was no prior value of one
643 ## we newly defined.
644 ## add variable name to a list of feisty defined variables.
645 #
646 ##hmmm: first implem just sets it up and exports the variable.
647 ##  i.e., this method always exports.
648 #export "${@}" 
649 #
650 #
651 #return 0
652 #  }
653
654   ##############
655
656 #hmmm: this points to an extended functions file being needed; not all of these are core.
657
658   # displays documentation in "md" formatted files.
659   function show_md()
660   {
661     local file="$1"; shift
662     pandoc "$file" | lynx -stdin
663   }
664
665   ##############
666
667   # just shows a separator line for an 80 column console, or uses the first
668   # parameter as the number of columns to expect.
669   function separator()
670   {
671     count=$1; shift
672     if [ -z "$count" ]; then
673       count=79
674     fi
675     echo
676     local i
677     for ((i=0; i < $count - 1; i++)); do
678       echo -n "="
679     done
680     echo
681     echo
682   }
683   # alias for separator.
684   function sep()
685   {
686     separator $*
687   }
688
689   ##############
690
691   function function_sentinel()
692   {
693     return 0; 
694   }
695   
696   if [ ! -z "$SHELL_DEBUG" ]; then echo "feisty meow function definitions done."; fi
697
698   ##############
699
700   # test code for set_var_if_undefined.
701   run_test=0
702   if [ $run_test != 0 ]; then
703     echo running tests on set_var_if_undefined.
704     flagrant=petunia
705     set_var_if_undefined flagrant forknordle
706     check_result "testing if defined variable would be whacked"
707     if [ $flagrant != petunia ]; then
708       echo set_var_if_undefined failed to leave the test variable alone
709       exit 1
710     fi
711     unset bobblehead_stomper
712     set_var_if_undefined bobblehead_stomper endurance
713     if [ $bobblehead_stomper != endurance ]; then
714       echo set_var_if_undefined failed to set a variable that was not defined yet
715       exit 1
716     fi
717   fi
718
719 fi
720