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 # locates a process given a search pattern to match in the process list.
38 local PID_DUMP="$(mktemp "$TMP/zz_pidlist.XXXXXX")"
41 if [ "$OS" == "Windows_NT" ]; then
42 # needs to be a windows format filename for 'type' to work.
43 if [ ! -d c:/tmp ]; then
46 # windows7 magical mystery tour lets us create a file c:\\tmp_pids.txt, but then it's not really there
47 # in the root of drive c: when we look for it later. hoping to fix that problem by using a subdir, which
48 # also might be magical thinking from windows perspective.
49 tmppid=c:\\tmp\\pids.txt
50 # we have abandoned all hope of relying on ps on windows. instead
51 # we use wmic to get full command lines for processes.
52 # this does not exist on windows home edition. we are hosed if that's
53 # what they insist on testing on.
54 wmic /locale:ms_409 PROCESS get processid,commandline </dev/null >"$tmppid"
56 if [ ! -z "$(uname -a | grep "^MING" )" ]; then
59 # we 'type' the file to get rid of the unicode result from wmic.
60 cmd $flag type "$tmppid" >$PID_DUMP
62 local CR='
\r' # embedded carriage return.
63 local appropriate_pattern="s/^.* *\([0-9][0-9]*\)[ $CR]*\$/\1/p"
64 for i in "${patterns[@]}"; do
65 PIDS_SOUGHT+=$(cat $PID_DUMP \
67 | sed -n -e "$appropriate_pattern")
68 if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
69 # we want to bail as soon as we get matches, because on the same
70 # platform, the same set of patterns should work to find all
71 # occurrences of the genesis java.
76 /bin/ps $extra_flags wuax >$PID_DUMP
77 # pattern to use for peeling off the process numbers.
78 local appropriate_pattern='s/^[-a-zA-Z_0-9][-a-zA-Z_0-9]* *\([0-9][0-9]*\).*$/\1/p'
79 # remove the first line of the file, search for the pattern the
80 # user wants to find, and just pluck the process ids out of the
82 for i in "${patterns[@]}"; do
83 PIDS_SOUGHT=$(cat $PID_DUMP \
86 | sed -n -e "$appropriate_pattern")
87 if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
88 # we want to bail as soon as we get matches, because on the same
89 # platform, the same set of patterns should work to find all
90 # occurrences of the genesis java.
95 if [ ! -z "$PIDS_SOUGHT" ]; then echo "$PIDS_SOUGHT"; fi
99 # finds all processes matching the pattern specified and shows their full
100 # process listing (whereas psfind just lists process ids).
103 echo "psa finds processes by pattern, but there was no pattern on the command line."
112 echo "Processes containing \"$1\"..."
114 if [ -n "$IS_DARWIN" ]; then
117 # only print the header the first time.
118 if [ -z "$fuzil_sentinel" ]; then
121 ps $i -w -u | sed -e '1d'
126 # cases besides mac os x's darwin.
128 if [ "$OS" = "Windows_NT" ]; then
129 # special case for windows.
133 ps $extra_flags | grep "$curr"
136 # normal OSes can handle a nice simple query.
142 # an unfortunately similarly named function to the above 'ps' as in process
143 # methods, but this 'ps' stands for postscript. this takes a postscript file
144 # and converts it into pcl3 printer language and then ships it to the printer.
145 # this mostly makes sense for an environment where one's default printer is
146 # pcl. if the input postscript causes ghostscript to bomb out, there has been
147 # some good success running ps2ps on the input file and using the cleaned
148 # postscript file for printing.
149 function ps2pcl2lpr() {
151 gs -sDEVICE=pcl3 -sOutputFile=- -sPAPERSIZE=letter "$i" | lpr -l
155 function fix_alsa() {
156 sudo /etc/init.d/alsasound restart
159 # switches from a /X/path form to an X:/ form. this also processes cygwin paths.
160 function unix_to_dos_path() {
161 # we usually remove dos slashes in favor of forward slashes.
162 if [ ! -z "$SERIOUS_SLASH_TREATMENT" ]; then
163 # unless this flag is set, in which case we force dos slashes.
164 echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/' | sed -e 's/\//\\/g'
166 echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/'
170 # switches from an X:/ form to an /X/path form.
171 function dos_to_unix_path() {
172 # we always remove dos slashes in favor of forward slashes.
173 echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/\1\/\2/'
176 # returns a successful value (0) if this system is debian or ubuntu.
177 function debian_like() {
178 # decide if we think this is debian or ubuntu or a variant.
179 DEBIAN_LIKE=$(if [ ! -z "$(grep -i debian /etc/issue)" \
180 -o ! -z "$(grep -i ubuntu /etc/issue)" ]; then echo 1; else echo 0; fi)
181 if [ $DEBIAN_LIKE -eq 1 ]; then
182 # success; this is debianish.
185 # this seems like some other OS.
190 # su function: makes su perform a login.
191 # for some OSes, this transfers the X authority information to the new login.
194 # debian currently requires the full version which imports X authority
195 # information for su.
197 # get the x authority info for our current user.
198 source $FEISTY_MEOW_SCRIPTS/x_win/get_x_auth.sh
200 if [ -z "$X_auth_info" ]; then
201 # if there's no authentication info to pass along, we just do a normal su.
204 # under X, we update the new login's authority info with the previous
206 (unset XAUTHORITY; /bin/su -l $* -c "$X_auth_info ; export DISPLAY=$DISPLAY ; bash")
209 # non-debian supposedly doesn't need the extra overhead any more.
210 # or at least suse doesn't, which is the other one we've tested on.
214 # relabel the console after returning.
215 bash $FEISTY_MEOW_SCRIPTS/tty/label_terminal_with_infos.sh
218 # sudo function wraps the normal sudo by ensuring we replace the terminal
219 # label if they're doing an su with the sudo.
221 local first_command="$1"
223 if [ "$first_command" == "su" ]; then
224 # yep, they were doing an su, but they're back now.
225 bash $FEISTY_MEOW_SCRIPTS/tty/label_terminal_with_infos.sh
229 # trashes the .#blah files that cvs and svn leave behind when finding conflicts.
230 # this kind of assumes you've already checked them for any salient facts.
231 function clean_cvs_junk() {
233 find $i -follow -type f -iname ".#*" -exec perl $FEISTY_MEOW_SCRIPTS/files/safedel.pl {} ";"
237 # overlay for nechung binary so that we can complain less grossly about it when it's missing.
239 local wheres_nechung=$(which nechung 2>/dev/null)
240 if [ -z "$wheres_nechung" ]; then
241 echo "The nechung oracle program cannot be found. You may want to consider"
242 echo "rebuilding the feisty meow applications with this command:"
243 echo " bash $FEISTY_MEOW_SCRIPTS/generator/bootstrap_build.sh"
249 # recreates all the generated files that the feisty meow scripts use.
250 function regenerate() {
251 bash $FEISTY_MEOW_SCRIPTS/core/bootstrap_shells.sh
256 # generates a random password where the first parameter is the number of characters
257 # in the password (default 20) and the second parameter specifies whether to use
258 # special characters (1) or not (0).
259 # found function at http://legroom.net/2010/05/06/bash-random-password-generator
260 function random_password()
262 [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
263 cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-32}
267 function function_sentinel() { return 0; }
269 if [ ! -z "$SHELL_DEBUG" ]; then echo function definitions end....; fi