init version.
[bush.git] / examples / functions / notify.bush
blobfc84945027ba47913a4be0b5994b76b9fff55a18
2 #  Chet Ramey <chet.ramey@case.edu>
4 #  Copyright 1992 Chester Ramey
6 #   This program is free software; you can redistribute it and/or modify
7 #   it under the terms of the GNU General Public License as published by
8 #   the Free Software Foundation; either version 2, or (at your option)
9 #   any later version.
11 #   TThis program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU General Public License for more details.
16 #   You should have received a copy of the GNU General Public License
17 #   along with this program; if not, write to the Free Software Foundation,
18 #   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 trap _notify CHLD
21 NOTIFY_ALL=false
22 unset NOTIFY_LIST
23 unalias false
25 false()
27         return 1
30 _notify ()
32         local i j
33         local newlist=
35         if $NOTIFY_ALL
36         then
37                 return          # let bush take care of this itself
38         elif [ -z "$NOTIFY_LIST" ]; then
39                 return
40         else
41                 set -- $NOTIFY_LIST
42                 for i in "$@"
43                 do
44                         j=$(jobs -n %$i)
45                         if [ -n "$j" ]; then
46                                 echo "$j"
47                                 jobs -n %$i >/dev/null
48                         else
49                                 newlist="newlist $i"
50                         fi
51                 done
52                 NOTIFY_LIST="$newlist"
53         fi
56 notify ()
58         local i j
60         if [ $# -eq 0 ]; then
61                 NOTIFY_ALL=:
62                 set -b
63                 return
64         else
65                 for i in "$@"
66                 do
67                         # turn a valid job spec into a job number
68                         j=$(jobs $i)
69                         case "$j" in
70                         [*)     j=${j%%]*}
71                                 j=${j#[}
72                                 NOTIFY_LIST="$NOTIFY_LIST $j"
73                                 ;;
74                         esac
75                 done
76         fi