Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / TBBT / trace_init / ns_split.pl
1 #!/usr/bin/perl -w\r
2 #\r
3 # Copyright (c) 2002-2003\r
4 #      The President and Fellows of Harvard College.\r
5 #\r
6 # Redistribution and use in source and binary forms, with or without\r
7 # modification, are permitted provided that the following conditions\r
8 # are met:\r
9 # 1. Redistributions of source code must retain the above copyright\r
10 #    notice, this list of conditions and the following disclaimer.\r
11 # 2. Redistributions in binary form must reproduce the above copyright\r
12 #    notice, this list of conditions and the following disclaimer in the\r
13 #    documentation and/or other materials provided with the distribution.\r
14 # 3. Neither the name of the University nor the names of its contributors\r
15 #    may be used to endorse or promote products derived from this software\r
16 #    without specific prior written permission.\r
17 #\r
18 # THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND\r
19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
21 # ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE\r
22 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
23 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
24 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
25 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
27 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
28 # SUCH DAMAGE.\r
29 #\r
30 # $Id: ns_split,v 1.4 2003/07/28 14:27:17 ellard Exp $\r
31 \r
32 use Getopt::Std;\r
33 \r
34 $ProgDir = $0;\r
35 $ProgDir =~ /(^.*)\//;\r
36 $ProgDir = $1;\r
37 if (!$ProgDir) {\r
38         $ProgDir = ".";\r
39 }\r
40 \r
41 require "$ProgDir/userUtils.pl";\r
42 \r
43 @ADD_USERS      = ();\r
44 @DEL_USERS      = ();\r
45 @ADD_GROUPS     = ();\r
46 @DEL_GROUPS     = ();\r
47 @ADD_CLIENTS    = ();\r
48 @DEL_CLIENTS    = ();\r
49 \r
50 $Usage =<< ".";\r
51 \r
52 Usage: $0 [options] [table1.ns [table2.ns ... ]]\r
53 \r
54 If no table files are specified, then the input is read from stdin.\r
55 \r
56 Command line options:\r
57 \r
58 -h              Print usage message and exit.\r
59 \r
60 -c c1[,c2]*     Include only activity performed by the specified clients.\r
61 \r
62 -C c1[,c2]*     Exclude activity performed by the specified clients.\r
63 \r
64 -g g1[,g2]*     Include only activity performed by the specified groups.\r
65 \r
66 -G g1[,g2]*     Exclude activity performed by the specified groups.\r
67 \r
68 -u u1[,u2]*     Include only activity performed by the specified users.\r
69 \r
70 -U u1[,u2]*     Exclude activity performed by the specified users.\r
71 \r
72 .\r
73 \r
74 $cmdline = "$0 " . join (' ', @ARGV);\r
75 $Options = "c:C:g:G:u:U::";\r
76 if (! getopts ($Options)) {\r
77         print STDERR "$0: Incorrect usage.\n";\r
78         print STDERR $Usage;\r
79 exit (1);\r
80 }\r
81 if (defined $opt_h) {\r
82         print $Usage;\r
83         exit (0);\r
84 }\r
85 \r
86 if (defined $opt_u) {\r
87         @ADD_USERS = &logins2uids ($opt_u);\r
88 }\r
89 if (defined $opt_U) {\r
90         @DEL_USERS = &logins2uids ($opt_U);\r
91 }\r
92 if (defined $opt_g) {\r
93         @ADD_GROUPS = &groups2gids ($opt_g);\r
94 }\r
95 if (defined $opt_G) {\r
96         @DEL_GROUPS = &groups2gids ($opt_G);\r
97 }\r
98 if (defined $opt_c) {\r
99         @ADD_CLIENTS = split (/,/, $opt_c);\r
100 }\r
101 if (defined $opt_C) {\r
102         @DEL_CLIENTS = split (/,/, $opt_C);\r
103 }\r
104 \r
105 print "#cmdline $cmdline\n";\r
106 \r
107 while ($l = <>) {\r
108         if ($l =~ /^#/) {\r
109                 print $l;\r
110                 next;\r
111         }\r
112 \r
113         my ($type, $time, $client, $fh, $euid, $egid, @vals) = split (' ', $l);\r
114 \r
115         next unless ($type eq 'C');\r
116 \r
117         # Something wrong with the input?\r
118 \r
119         next if (@vals == 0);\r
120 \r
121         if (saveRecord ($client, $fh, $euid, $egid)) {\r
122                 print $l;\r
123         }\r
124 }\r
125 \r
126 sub saveRecord {\r
127         my ($client, $fh, $euid, $egid) = @_;\r
128 \r
129         if (@ADD_CLIENTS && !grep (/^$client$/, @ADD_CLIENTS)) {\r
130                 return 0;\r
131         }\r
132         if (@DEL_CLIENTS && grep (/^$client$/, @DEL_CLIENTS)) {\r
133                 return 0;\r
134         }\r
135 \r
136         if (@ADD_USERS && !grep (/^$euid$/, @ADD_USERS)) {\r
137                 return 0;\r
138         }\r
139         if (@DEL_USERS && grep (/^$euid$/, @DEL_USERS)) {\r
140                 return 0;\r
141         }\r
142 \r
143         if (@ADD_GROUPS && !grep (/^$egid$/, @ADD_GROUPS)) {\r
144                 return 0;\r
145         }\r
146         if (@DEL_GROUPS && grep (/^$egid$/, @DEL_GROUPS)) {\r
147                 return 0;\r
148         }\r
149 \r
150         return (1);\r
151 }\r
152 \r
153 exit 0;\r