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 functions.sh because already defined.
16 if [ -z "$skip_all" ]; then
17 if [ ! -z "$SHELL_DEBUG" ]; then
18 echo function definitions begin...
21 # a handy little method that can be used for date strings. it was getting
22 # really tiresome how many different ways the script did the date formatting.
23 function date_stringer() {
25 if [ -z "$sep" ]; then sep='_'; fi
26 date +"%Y$sep%m$sep%d$sep%H%M$sep%S" | tr -d '/\n/'
29 # makes a directory of the name specified and then tries to change the
30 # current directory to that directory.
32 if [ ! -d "$1" ]; then mkdir -p "$1"; fi
36 # displays the value of a variable in bash friendly format.
39 local varname="$1"; shift
40 if [ -z "$varname" ]; then
43 if [ -z "${!varname}" ]; then
44 echo "$varname undefined"
46 echo "$varname=${!varname}"
51 function success_sound()
53 if [ ! -z "$CLAM_FINISH_SOUND" ]; then
54 bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh "$CLAM_FINISH_SOUND"
58 function error_sound()
60 if [ ! -z "$CLAM_ERROR_SOUND" ]; then
61 bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh "$CLAM_ERROR_SOUND"
65 # checks the result of the last command that was run, and if it failed,
66 # then this complains and exits from bash. the function parameters are
67 # used as the message to print as a complaint.
68 function check_result()
71 echo -e "failed on: $*"
77 # locates a process given a search pattern to match in the process list.
79 local PID_DUMP="$(mktemp "$TMP/zz_pidlist.XXXXXX")"
82 if [ "$OS" == "Windows_NT" ]; then
83 # needs to be a windows format filename for 'type' to work.
84 if [ ! -d c:/tmp ]; then
87 # windows7 magical mystery tour lets us create a file c:\\tmp_pids.txt, but then it's not really there
88 # in the root of drive c: when we look for it later. hoping to fix that problem by using a subdir, which
89 # also might be magical thinking from windows perspective.
90 tmppid=c:\\tmp\\pids.txt
91 # we have abandoned all hope of relying on ps on windows. instead
92 # we use wmic to get full command lines for processes.
93 # this does not exist on windows home edition. we are hosed if that's
94 # what they insist on testing on.
95 wmic /locale:ms_409 PROCESS get processid,commandline </dev/null >"$tmppid"
97 if [ ! -z "$(uname -a | grep "^MING" )" ]; then
100 # we 'type' the file to get rid of the unicode result from wmic.
101 cmd $flag type "$tmppid" >$PID_DUMP
103 local CR='
\r' # embedded carriage return.
104 local appropriate_pattern="s/^.* *\([0-9][0-9]*\)[ $CR]*\$/\1/p"
105 for i in "${patterns[@]}"; do
106 PIDS_SOUGHT+=$(cat $PID_DUMP \
108 | sed -n -e "$appropriate_pattern")
109 if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
110 # we want to bail as soon as we get matches, because on the same
111 # platform, the same set of patterns should work to find all
112 # occurrences of the genesis java.
117 /bin/ps $extra_flags wuax >$PID_DUMP
118 # pattern to use for peeling off the process numbers.
119 local appropriate_pattern='s/^[-a-zA-Z_0-9][-a-zA-Z_0-9]* *\([0-9][0-9]*\).*$/\1/p'
120 # remove the first line of the file, search for the pattern the
121 # user wants to find, and just pluck the process ids out of the
123 for i in "${patterns[@]}"; do
124 PIDS_SOUGHT=$(cat $PID_DUMP \
127 | sed -n -e "$appropriate_pattern")
128 if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
129 # we want to bail as soon as we get matches, because on the same
130 # platform, the same set of patterns should work to find all
131 # occurrences of the genesis java.
136 if [ ! -z "$PIDS_SOUGHT" ]; then echo "$PIDS_SOUGHT"; fi
140 # finds all processes matching the pattern specified and shows their full
141 # process listing (whereas psfind just lists process ids).
144 echo "psa finds processes by pattern, but there was no pattern on the command line."
153 echo "Processes containing \"$1\"..."
155 if [ -n "$IS_DARWIN" ]; then
158 # only print the header the first time.
159 if [ -z "$fuzil_sentinel" ]; then
162 ps $i -w -u | sed -e '1d'
167 # cases besides mac os x's darwin.
169 if [ "$OS" = "Windows_NT" ]; then
170 # special case for windows.
174 ps $extra_flags | grep "$curr"
177 # normal OSes can handle a nice simple query.
183 # an unfortunately similarly named function to the above 'ps' as in process
184 # methods, but this 'ps' stands for postscript. this takes a postscript file
185 # and converts it into pcl3 printer language and then ships it to the printer.
186 # this mostly makes sense for an environment where one's default printer is
187 # pcl. if the input postscript causes ghostscript to bomb out, there has been
188 # some good success running ps2ps on the input file and using the cleaned
189 # postscript file for printing.
190 function ps2pcl2lpr() {
192 gs -sDEVICE=pcl3 -sOutputFile=- -sPAPERSIZE=letter "$i" | lpr -l
196 function fix_alsa() {
197 sudo /etc/init.d/alsasound restart
200 # switches from a /X/path form to an X:/ form. this also processes cygwin paths.
201 function unix_to_dos_path() {
202 # we usually remove dos slashes in favor of forward slashes.
203 if [ ! -z "$SERIOUS_SLASH_TREATMENT" ]; then
204 # unless this flag is set, in which case we force dos slashes.
205 echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/' | sed -e 's/\//\\/g'
207 echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/'
211 # switches from an X:/ form to an /X/path form.
212 function dos_to_unix_path() {
213 # we always remove dos slashes in favor of forward slashes.
214 echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/\1\/\2/'
217 # returns a successful value (0) if this system is debian or ubuntu.
218 function debian_like() {
219 # decide if we think this is debian or ubuntu or a variant.
220 DEBIAN_LIKE=$(if [ ! -z "$(grep -i debian /etc/issue)" \
221 -o ! -z "$(grep -i ubuntu /etc/issue)" ]; then echo 1; else echo 0; fi)
222 if [ $DEBIAN_LIKE -eq 1 ]; then
223 # success; this is debianish.
226 # this seems like some other OS.
231 # su function: makes su perform a login.
232 # for some OSes, this transfers the X authority information to the new login.
235 # debian currently requires the full version which imports X authority
236 # information for su.
238 # get the x authority info for our current user.
239 source $FEISTY_MEOW_SCRIPTS/x_win/get_x_auth.sh
241 if [ -z "$X_auth_info" ]; then
242 # if there's no authentication info to pass along, we just do a normal su.
245 # under X, we update the new login's authority info with the previous
247 (unset XAUTHORITY; /bin/su -l $* -c "$X_auth_info ; export DISPLAY=$DISPLAY ; bash")
250 # non-debian supposedly doesn't need the extra overhead any more.
251 # or at least suse doesn't, which is the other one we've tested on.
255 # relabel the console after returning.
256 bash $FEISTY_MEOW_SCRIPTS/tty/label_terminal_with_infos.sh
259 # sudo function wraps the normal sudo by ensuring we replace the terminal
260 # label if they're doing an su with the sudo.
262 local first_command="$1"
264 if [ "$first_command" == "su" ]; then
265 # yep, they were doing an su, but they're back now.
266 bash $FEISTY_MEOW_SCRIPTS/tty/label_terminal_with_infos.sh
270 # trashes the .#blah files that cvs and svn leave behind when finding conflicts.
271 # this kind of assumes you've already checked them for any salient facts.
272 function clean_cvs_junk() {
274 find $i -follow -type f -iname ".#*" -exec perl $FEISTY_MEOW_SCRIPTS/files/safedel.pl {} ";"
278 # overlay for nechung binary so that we can complain less grossly about it when it's missing.
280 local wheres_nechung=$(which nechung 2>/dev/null)
281 if [ -z "$wheres_nechung" ]; then
282 echo "The nechung oracle program cannot be found. You may want to consider"
283 echo "rebuilding the feisty meow applications with this command:"
284 echo "bash $FEISTY_MEOW_SCRIPTS/generator/bootstrap_build.sh"
290 # recreates all the generated files that the feisty meow scripts use.
291 function regenerate() {
292 bash $FEISTY_MEOW_SCRIPTS/core/bootstrap_shells.sh
297 # generates a random password where the first parameter is the number of characters
298 # in the password (default 20) and the second parameter specifies whether to use
299 # special characters (1) or not (0).
300 # found function at http://legroom.net/2010/05/06/bash-random-password-generator
301 function random_password()
303 [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
304 cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-32}
308 # a wrapper for the which command that finds items on the path. some OSes
309 # do not provide which, so we want to not be spewing errors when that
314 which which &>/dev/null
315 if [ $? -ne 0 ]; then
316 # there is no which command here. we produce nothing due to this.
319 echo $(which $to_find)
322 # copies a set of custom scripts into the proper location for feisty meow
323 # to merge their functions and aliases with the standard set.
324 function recustomize()
327 if [ -z "$user" ]; then
328 # use our default example user if there was no name provided.
331 if [ ! -d "$FEISTY_MEOW_DIR/customizing/$user" ]; then
332 echo "The customization folder provided for $user should be:"
333 echo " '$FEISTY_MEOW_DIR/customizing/$user'"
334 echo "but that folder does not exist. Skipping customization."
337 regenerate >/dev/null
338 pushd "$FEISTY_MEOW_GENERATED/custom" &>/dev/null
339 local incongruous_files="$(bash "$FEISTY_MEOW_SCRIPTS/files/list_non_dupes.sh" "$FEISTY_MEOW_DIR/customizing/$user" "$FEISTY_MEOW_GENERATED/custom")"
340 if [ ${#incongruous_files} -ge 1 ]; then
341 echo "cleaning unknown older overrides..."
342 perl "$FEISTY_MEOW_SCRIPTS/files/safedel.pl" $incongruous_files
346 echo "copying custom overrides for $user"
347 mkdir "$FEISTY_MEOW_GENERATED/custom" 2>/dev/null
348 perl "$FEISTY_MEOW_SCRIPTS/text/cpdiff.pl" "$FEISTY_MEOW_DIR/customizing/$user" "$FEISTY_MEOW_GENERATED/custom"
352 function add_cygwin_drive_mounts() {
353 for i in c d e f g h q z ; do
354 ln -s /cygdrive/$i $i
359 # takes a file to modify, and then it will replace any occurrences of the
360 # pattern provided as the second parameter with the text in the third
362 function replace_pattern_in_file()
364 local file="$1"; shift
365 local pattern="$1"; shift
366 local replacement="$1"; shift
367 if [ -z "$file" -o -z "$pattern" -o -z "$replacement" ]; then
368 echo "replace_pattern_in_file: needs a filename, a pattern to replace, and the"
369 echo "text to replace that pattern with."
372 sed -i -e "s%$pattern%$replacement%g" "$file"
377 function function_sentinel() { return 0; }
379 if [ ! -z "$SHELL_DEBUG" ]; then echo function definitions end....; fi