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