3 # This defines some general, useful functions.
5 # test whether we've been here before or not.
7 function_sentinel &>/dev/null
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."
16 if [ -z "$skip_all" ]; then
18 if [ ! -z "$SHELL_DEBUG" ]; then
19 echo "feisty meow function definitions beginning now..."
22 # a handy little method that can be used for date strings. it was getting
23 # really tiresome how many different ways the script did the date formatting.
24 function date_stringer() {
26 if [ -z "$sep" ]; then sep='_'; fi
27 date +"%Y$sep%m$sep%d$sep%H%M$sep%S" | tr -d '/\n/'
30 # makes a directory of the name specified and then tries to change the
31 # current directory to that directory.
33 if [ ! -d "$1" ]; then mkdir -p "$1"; fi
38 [[ "$(declare -p $1)" =~ "declare -a" ]]
46 # displays the value of a variable in bash friendly format.
51 local varname="$1"; shift
52 if [ -z "$varname" ]; then
56 if is_alias "$varname"; then
57 #echo found $varname is alias
58 local tmpfile="$(mktemp $TMP/aliasout.XXXXXX)"
59 alias $varname | sed -e 's/.*=//' >$tmpfile
60 echo "alias $varname=$(cat $tmpfile)"
62 elif [ -z "${!varname}" ]; then
63 echo "$varname undefined"
65 if is_array "$varname"; then
66 #echo found $varname is array var
68 eval temparray="(\${$varname[@]})"
69 echo "$varname=(${temparray[@]})"
70 #hmmm: would be nice to print above with elements enclosed in quotes, so that we can properly
71 # see ones that have spaces in them.
73 #echo found $varname is simple
74 echo "$varname=${!varname}"
81 function success_sound()
83 if [ ! -z "$CLAM_FINISH_SOUND" ]; then
84 bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh "$CLAM_FINISH_SOUND"
88 function error_sound()
90 if [ ! -z "$CLAM_ERROR_SOUND" ]; then
91 bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh "$CLAM_ERROR_SOUND"
95 # checks the result of the last command that was run, and if that failed,
96 # then this complains and exits from bash. the function parameters are
97 # used as the message to print as a complaint.
98 function check_result()
100 if [ $? -ne 0 ]; then
101 echo -e "failed on: $*"
107 # locates a process given a search pattern to match in the process list.
109 local -a patterns=("${@}")
111 #echo patterns list is: "${patterns[@]}"
115 if [ "${patterns[0]}" == "-u" ]; then
116 user_flag="-u ${patterns[1]}"
117 echo "found a -u parm and user=${patterns[1]}"
118 # void the two elements with that user flag so we don't use them as patterns.
119 unset patterns[0] patterns[1]=
122 local PID_DUMP="$(mktemp "$TMP/zz_pidlist.XXXXXX")"
124 if [ "$OS" == "Windows_NT" ]; then
125 #hmmm: windows isn't implementing the user flag yet!
126 # windows case has some odd gyrations to get the user list.
127 if [ ! -d c:/tmp ]; then
130 # windows7 magical mystery tour lets us create a file c:\\tmp_pids.txt, but then it's not
131 # really there in the root of drive c: when we look for it later. hoping to fix that
132 # problem by using a subdir, which also might be magical thinking from windows perspective.
133 tmppid=c:\\tmp\\pids.txt
134 # we have abandoned all hope of relying on ps on windows. instead we use wmic to get full
135 # command lines for processes.
136 wmic /locale:ms_409 PROCESS get processid,commandline </dev/null >"$tmppid"
138 if [ ! -z "$(uname -a | grep "^MING" )" ]; then
141 # we 'type' the file to get rid of the unicode result from wmic.
142 # needs to be a windows format filename for 'type' to work.
143 cmd $flag type "$tmppid" >$PID_DUMP
145 local appropriate_pattern='s/^.*[[:space:]][[:space:]]*\([0-9][0-9]*\) *\$/\1/p'
147 for i in "${patterns[@]}"; do
148 PIDS_SOUGHT+=($(cat $PID_DUMP \
150 | sed -n -e "$appropriate_pattern"))
153 /bin/ps $user_flag wuax >$PID_DUMP
155 #echo got all this stuff in the pid dump file:
158 # pattern to use for peeling off the process numbers.
159 local appropriate_pattern='s/^[-+a-zA-Z_0-9][-+a-zA-Z_0-9]*[[:space:]][[:space:]]*\([0-9][0-9]*\).*$/\1/p'
160 # remove the first line of the file, search for the pattern the
161 # user wants to find, and just pluck the process ids out of the
164 for i in "${patterns[@]}"; do
166 #echo phase 1: $(cat $PID_DUMP | sed -e '1d' )
167 #echo phase 2: $(cat $PID_DUMP | sed -e '1d' | grep -i "$i" )
168 PIDS_SOUGHT+=($(cat $PID_DUMP \
171 | sed -n -e "$appropriate_pattern"))
174 #echo pids sought list became:
175 #echo "${PIDS_SOUGHT[@]}"
178 if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
179 local PIDS_SOUGHT2=$(printf -- '%s\n' ${PIDS_SOUGHT[@]} | sort | uniq)
181 PIDS_SOUGHT=${PIDS_SOUGHT2[*]}
182 echo ${PIDS_SOUGHT[*]}
187 # finds all processes matching the pattern specified and shows their full
188 # process listing (whereas psfind just lists process ids).
191 echo "psa finds processes by pattern, but there was no pattern on the command line."
200 echo "Processes matching ${@}..."
202 if [ -n "$IS_DARWIN" ]; then
205 # only print the header the first time.
206 if [ -z "$fuzil_sentinel" ]; then
209 ps $i -w -u | sed -e '1d'
214 # cases besides mac os x's darwin.
216 if [ "$OS" = "Windows_NT" ]; then
217 # special case for windows.
223 # normal OSes can handle a nice simple query.
229 # an unfortunately similarly named function to the above 'ps' as in process
230 # methods, but this 'ps' stands for postscript. this takes a postscript file
231 # and converts it into pcl3 printer language and then ships it to the printer.
232 # this mostly makes sense for an environment where one's default printer is
233 # pcl. if the input postscript causes ghostscript to bomb out, there has been
234 # some good success running ps2ps on the input file and using the cleaned
235 # postscript file for printing.
236 function ps2pcl2lpr() {
238 gs -sDEVICE=pcl3 -sOutputFile=- -sPAPERSIZE=letter "$i" | lpr -l
242 function fix_alsa() {
243 sudo /etc/init.d/alsasound restart
246 # switches from a /X/path form to an X:/ form. this also processes cygwin paths.
247 function unix_to_dos_path() {
248 # we usually remove dos slashes in favor of forward slashes.
250 if [[ ! "$OS" =~ ^[Ww][iI][nN] ]]; then
251 # fake this value for non-windows (non-cygwin) platforms.
254 # for cygwin, we must replace the /home/X path with an absolute one, since cygwin
255 # insists on the /home form instead of /c/cygwin/home being possible. this is
256 # super frustrating and nightmarish.
257 DOSSYHOME="$(cygpath -am "$HOME")"
260 if [ ! -z "$SERIOUS_SLASH_TREATMENT" ]; then
261 # unless this flag is set, in which case we force dos slashes.
262 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'
264 echo "$1" | sed -e "s?^$HOME?$DOSSYHOME?g" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/'
268 # switches from an X:/ form to an /X/path form.
269 function dos_to_unix_path() {
270 # we always remove dos slashes in favor of forward slashes.
271 echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/\1\/\2/'
274 # returns a successful value (0) if this system is debian or ubuntu.
275 function debian_like() {
276 # decide if we think this is debian or ubuntu or a variant.
277 DEBIAN_LIKE=$(if [ ! -z "$(grep -i debian /etc/issue)" \
278 -o ! -z "$(grep -i ubuntu /etc/issue)" ]; then echo 1; else echo 0; fi)
279 if [ $DEBIAN_LIKE -eq 1 ]; then
280 # success; this is debianish.
283 # this seems like some other OS.
288 # su function: makes su perform a login.
289 # for some OSes, this transfers the X authority information to the new login.
292 # debian currently requires the full version which imports X authority
293 # information for su.
295 # get the x authority info for our current user.
296 source $FEISTY_MEOW_SCRIPTS/x_win/get_x_auth.sh
298 if [ -z "$X_auth_info" ]; then
299 # if there's no authentication info to pass along, we just do a normal su.
302 # under X, we update the new login's authority info with the previous
304 (unset XAUTHORITY; /bin/su -l $* -c "$X_auth_info ; export DISPLAY=$DISPLAY ; bash")
307 # non-debian supposedly doesn't need the extra overhead any more.
308 # or at least suse doesn't, which is the other one we've tested on.
312 # relabel the console after returning.
313 bash $FEISTY_MEOW_SCRIPTS/tty/label_terminal_with_infos.sh
316 # sudo function wraps the normal sudo by ensuring we replace the terminal
317 # label if they're doing an su with the sudo.
319 local first_command="$1"
321 if [ "$first_command" == "su" ]; then
322 # yep, they were doing an su, but they're back now.
323 bash $FEISTY_MEOW_SCRIPTS/tty/label_terminal_with_infos.sh
327 # trashes the .#blah files that cvs and svn leave behind when finding conflicts.
328 # this kind of assumes you've already checked them for any salient facts.
329 function clean_cvs_junk() {
331 find $i -follow -type f -iname ".#*" -exec perl $FEISTY_MEOW_SCRIPTS/files/safedel.pl {} ";"
335 # overlay for nechung binary so that we can complain less grossly about it when it's missing.
337 local wheres_nechung=$(which nechung 2>/dev/null)
338 if [ -z "$wheres_nechung" ]; then
339 echo "The nechung oracle program cannot be found. You may want to consider"
340 echo "rebuilding the feisty meow applications with this command:"
341 echo "bash $FEISTY_MEOW_SCRIPTS/generator/bootstrap_build.sh"
347 # recreates all the generated files that the feisty meow scripts use.
348 function regenerate() {
349 # do the bootstrapping process again.
350 echo "regenerating feisty meow script environment."
351 bash $FEISTY_MEOW_SCRIPTS/core/bootstrap_shells.sh
353 # force a full reload by turning off sentinel variable and alias.
354 # the nethack one is used by fred's customizations.
355 # interesting note perhaps: found that the NETHACKOPTIONS variable was
356 # not being unset correctly when preceded by an alias. split them up
357 # like they are now due to that bug.
358 unset -v CORE_ALIASES_LOADED FEISTY_MEOW_GENERATED NECHUNG NETHACKOPTIONS
359 unset -f function_sentinel
360 # reload feisty meow environment in current shell.
361 source $FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh
362 # run nechung oracle to give user a new fortune.
366 # generates a random password where the first parameter is the number of characters
367 # in the password (default 20) and the second parameter specifies whether to use
368 # special characters (1) or not (0).
369 # found function at http://legroom.net/2010/05/06/bash-random-password-generator
370 function random_password()
372 [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
373 cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-32}
377 # a wrapper for the which command that finds items on the path. some OSes
378 # do not provide which, so we want to not be spewing errors when that
383 which which &>/dev/null
384 if [ $? -ne 0 ]; then
385 # there is no which command here. we produce nothing due to this.
388 echo $(which $to_find)
391 # copies a set of custom scripts into the proper location for feisty meow
392 # to merge their functions and aliases with the standard set.
393 function recustomize()
396 if [ -z "$user" ]; then
397 # use our default example user if there was no name provided.
400 if [ ! -d "$FEISTY_MEOW_DIR/customizing/$user" ]; then
401 echo "The customization folder provided for $user should be:"
402 echo " '$FEISTY_MEOW_DIR/customizing/$user'"
403 echo "but that folder does not exist. Skipping customization."
406 regenerate >/dev/null
407 pushd "$FEISTY_MEOW_GENERATED/custom" &>/dev/null
408 local incongruous_files="$(bash "$FEISTY_MEOW_SCRIPTS/files/list_non_dupes.sh" "$FEISTY_MEOW_DIR/customizing/$user" "$FEISTY_MEOW_GENERATED/custom")"
409 if [ ${#incongruous_files} -ge 1 ]; then
410 echo "cleaning unknown older overrides..."
411 perl "$FEISTY_MEOW_SCRIPTS/files/safedel.pl" $incongruous_files
415 echo "copying custom overrides for $user"
416 mkdir "$FEISTY_MEOW_GENERATED/custom" 2>/dev/null
417 perl "$FEISTY_MEOW_SCRIPTS/text/cpdiff.pl" "$FEISTY_MEOW_DIR/customizing/$user" "$FEISTY_MEOW_GENERATED/custom"
418 if [ -d "$FEISTY_MEOW_DIR/customizing/$user/scripts" ]; then
419 echo "copying custom scripts for $user"
420 \cp -R "$FEISTY_MEOW_DIR/customizing/$user/scripts" "$FEISTY_MEOW_GENERATED/custom/"
426 #uhhh, this does what now?
427 function add_cygwin_drive_mounts() {
428 for i in c d e f g h q z ; do
429 ln -s /cygdrive/$i $i
433 # takes a file to modify, and then it will replace any occurrences of the
434 # pattern provided as the second parameter with the text in the third
436 function replace_pattern_in_file()
438 local file="$1"; shift
439 local pattern="$1"; shift
440 local replacement="$1"; shift
441 if [ -z "$file" -o -z "$pattern" -o -z "$replacement" ]; then
442 echo "replace_pattern_in_file: needs a filename, a pattern to replace, and the"
443 echo "text to replace that pattern with."
446 sed -i -e "s%$pattern%$replacement%g" "$file"
451 while [ $# -gt 0 ]; do
453 if [ ! -f "$arg" -a ! -d "$arg" ]; then
454 echo "failure to find a file or directory named '$arg'."
457 # first we rename the file to be lower case.
458 perl $FEISTY_MEOW_SCRIPTS/files/renlower.pl "$arg" &>/dev/null
459 # oops, now the name is all lower-case. we need to make the
461 arg2="$(echo "$arg" | tr A-Z a-z)"
462 # we definitely wanted to adjust the case first, rather than doing all
463 # the wacky stuff this script does to the filename... we will capture
464 # the output of the replace operaton for reporting.
465 final_name="$(perl "$FEISTY_MEOW_SCRIPTS/files/replace_spaces_with_underscores.sh" "$arg2")"
466 # now zap the intermediate part of the name off.
467 final_name="$(echo $final_name | sed -e 's/.*=> //')"
468 # printout the combined operation results.
469 echo "'$arg' => $final_name"
475 function function_sentinel() { return 0; }
477 if [ ! -z "$SHELL_DEBUG" ]; then echo "feisty meow function definitions done."; fi