/[gentoo-src]/rc-scripts/src/awk/cachedepends.awk
Gentoo

Diff of /rc-scripts/src/awk/cachedepends.awk

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.2 Revision 1.19
1# Copyright 1999-2002 Gentoo Technologies, Inc. 1# Copyright 1999-2004 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License, v2 2# Distributed under the terms of the GNU General Public License v2
3# Author: Martin Schlemmer <azarah@gentoo.org>
4# $Header: /var/cvsroot/gentoo-src/rc-scripts/src/awk/cachedepends.awk,v 1.2 2002/11/17 19:33:01 azarah Exp $ 3# $Header: /var/cvsroot/gentoo-src/rc-scripts/src/awk/cachedepends.awk,v 1.19 2004/11/15 16:23:41 vapier Exp $
4
5function print_start() {
6 print "source /sbin/functions.sh" >> TMPCACHE
7 print "" >> TMPCACHE
8 print "need() {" >> TMPCACHE
9 print " echo \"NEED $*\"; return 0" >> TMPCACHE
10 print "}" >> TMPCACHE
11 print "" >> TMPCACHE
12 print "use() {" >> TMPCACHE
13 print " echo \"USE $*\"; return 0" >> TMPCACHE
14 print "}" >> TMPCACHE
15 print "" >> TMPCACHE
16 print "before() {" >> TMPCACHE
17 print " echo \"BEFORE $*\"; return 0" >> TMPCACHE
18 print "}" >> TMPCACHE
19 print "" >> TMPCACHE
20 print "after() {" >> TMPCACHE
21 print " echo \"AFTER $*\"; return 0" >> TMPCACHE
22 print "}" >> TMPCACHE
23 print "" >> TMPCACHE
24 print "provide() {" >> TMPCACHE
25 print " echo \"PROVIDE $*\"; return 0" >> TMPCACHE
26 print "}" >> TMPCACHE
27 print "" >> TMPCACHE
28 print "parallel() {" >> TMPCACHE
29 print " echo \"PARALLEL $*\"; return 0" >> TMPCACHE
30 print "}" >> TMPCACHE
31 print "" >> TMPCACHE
32}
33
34function print_header1(mtime) {
35 print "#*** " MYFILENAME " ***" >> TMPCACHE
36 print "" >> TMPCACHE
37 print "myservice=\"" MYFILENAME "\"" >> TMPCACHE
38 print "myservice=\"${myservice##*/}\"" >> TMPCACHE
39 print "echo \"RCSCRIPT ${myservice}\"" >> TMPCACHE
40 print "" >> TMPCACHE
41 print "echo \"MTIME " mtime "\"" >> TMPCACHE
42 print "" >> TMPCACHE
43}
44
45function print_header2(mtime) {
46 print "(" >> TMPCACHE
47 print " # Get settings for rc-script ..." >> TMPCACHE
48 print "" >> TMPCACHE
49 print " [ -e \"/etc/conf.d/${myservice}\" ] && source \"/etc/conf.d/${myservice}\"" >> TMPCACHE
50 print "" >> TMPCACHE
51 print " [ -e /etc/conf.d/net ] && \\" >> TMPCACHE
52 print " [ \"${myservice%%.*}\" = \"net\" ] && \\" >> TMPCACHE
53 print " [ \"${myservice##*.}\" != \"${myservice}\" ] && source /etc/conf.d/net" >> TMPCACHE
54 print "" >> TMPCACHE
55 print " [ -e /etc/rc.conf ] && source /etc/rc.conf" >> TMPCACHE
56 print "" >> TMPCACHE
57 print " depend() {" >> TMPCACHE
58 print " return 0" >> TMPCACHE
59 print " }" >> TMPCACHE
60 print "" >> TMPCACHE
61}
62
63function print_end() {
64 print "" >> TMPCACHE
65 print " depend" >> TMPCACHE
66 print ")" >> TMPCACHE
67 print "" >> TMPCACHE
68}
5 69
6BEGIN { 70BEGIN {
7 71
8 extension("/lib/rcscripts/filefuncs.so", "dlload") 72 extension("/lib/rcscripts/filefuncs.so", "dlload")
9 73
10 DEPCACHE=SVCDIR "/depcache" 74 # Get our environment variables
75 SVCDIR = ENVIRON["SVCDIR"]
76 if (SVCDIR == "") {
77 eerror("Could not get SVCDIR!")
78 exit 1
79 }
11 80
12 unlink(DEPCACHE) 81 # Since this could be called more than once simultaneously, use a
82 # temporary cache and rename when finished. See bug 47111
83 ("/bin/mktemp "SVCDIR"/depcache.XXXXXXX") | getline TMPCACHE
84 if (TMPCACHE == "") {
85 eerror("Failed to create temporary cache!")
86 exit 1
87 }
88
89 pipe = "ls /etc/init.d/*"
90 while ((pipe | getline tmpstring) > 0)
91 scripts = scripts " " tmpstring
92 close(pipe)
93
94 split(scripts, TMPRCSCRIPTS)
95
96 # Make sure that its a file we are working with,
97 # and do not process scripts, source or backup files.
98 for (x in TMPRCSCRIPTS)
99 if (((isfile(TMPRCSCRIPTS[x])) || (islink(TMPRCSCRIPTS[x]))) &&
100 (TMPRCSCRIPTS[x] !~ /((\.(c|bak))|\~)$/)) {
101
102 RCCOUNT++
103
104 RCSCRIPTS[RCCOUNT] = TMPRCSCRIPTS[x]
105 }
106
107 if (RCCOUNT == 0) {
108 eerror("No scripts to process!")
109 system("rm -f "TMPCACHE)
110 exit 1
111 }
13 112
14 print_start() 113 print_start()
15}
16 114
17function print_start() { 115 for (count = 1;count <= RCCOUNT;count++) {
18 print "need() {" >> (DEPCACHE) 116
19 print " echo \"NEED $*\"; return 0" >> (DEPCACHE) 117 MYFNR = 1
20 print "}" >> (DEPCACHE) 118 MYFILENAME = RCSCRIPTS[count]
21 print "" >> (DEPCACHE) 119 STAT_DATA[1] = 1
22 print "use() {" >> (DEPCACHE)
23 print " echo \"USE $*\"; return 0" >> (DEPCACHE)
24 print "}" >> (DEPCACHE)
25 print "" >> (DEPCACHE)
26 print "before() {" >> (DEPCACHE)
27 print " echo \"BEFORE $*\"; return 0" >> (DEPCACHE)
28 print "}" >> (DEPCACHE)
29 print "" >> (DEPCACHE)
30 print "after() {" >> (DEPCACHE)
31 print " echo \"AFTER $*\"; return 0" >> (DEPCACHE)
32 print "}" >> (DEPCACHE)
33 print "" >> (DEPCACHE)
34 print "provide() {" >> (DEPCACHE)
35 print " echo \"PROVIDE $*\"; return 0" >> (DEPCACHE)
36 print "}" >> (DEPCACHE)
37 print "" >> (DEPCACHE)
38}
39 120
40function print_header() { 121 while (((getline < (RCSCRIPTS[count])) > 0) && (!NEXTFILE)) {
41 print "#*** " FILENAME " ***" >> (DEPCACHE)
42 print "" >> (DEPCACHE)
43 print "myservice=\"" FILENAME "\"" >> (DEPCACHE)
44 print "myservice=\"${myservice##*/}\"" >> (DEPCACHE)
45 print "echo \"RCSCRIPT ${myservice}\"" >> (DEPCACHE)
46 print "" >> (DEPCACHE)
47 print "depend() {" >> (DEPCACHE)
48 print " return 0" >> (DEPCACHE)
49 print "}" >> (DEPCACHE)
50 print "" >> (DEPCACHE)
51}
52 122
53function print_end() {
54 print "" >> (DEPCACHE)
55 print "depend" >> (DEPCACHE)
56 print "" >> (DEPCACHE)
57}
58
59{
60 # If line start with a '#' and is the first line 123 # If line start with a '#' and is the first line
61 if (($0 ~ /^[[:space:]]*#/) && (FNR == 1)) { 124 if (($0 ~ /^[[:space:]]*#/) && (MYFNR == 1)) {
62 125
63 # Remove any spaces and tabs 126 # Remove any spaces and tabs
64 gsub(/[[:space:]]+/, "") 127 gsub(/[[:space:]]+/, "")
65 128
66 if ($0 == "#!/sbin/runscript") { 129 if ($0 == "#!/sbin/runscript") {
130
131 if (RCSCRIPTS[count] ~ /\.sh$/) {
132
133 ewarn(RCSCRIPTS[count] " is invalid (should not end with '.sh')")
134 NEXTFILE = 1
135 continue
136 }
137
138 if (stat(MYFILENAME, STAT_DATA) != 0)
139 ewarn("Could not stat \"" MYFILENAME "\"")
140
67 ISRCSCRIPT = 1 141 ISRCSCRIPT = 1
68 142 print_header1(STAT_DATA["mtime"])
69 print_header()
70 } else 143 } else {
71 nextfile 144
145 NEXTFILE = 1
146 continue
147 }
148 }
72 149
73 # Do not process scripts, source or backup files
74 if (FILENAME ~ /((\.(sh|c|bak))|\~)$/)
75 nextfile
76 }
77
78 # Filter out comments and only process if its a rcscript 150 # Filter out comments and only process if its a rcscript
79 if (($0 !~ /[[:space:]]*#/) && (ISRCSCRIPT == 1 )) { 151 if (($0 !~ /^[[:space:]]*#/) && (ISRCSCRIPT)) {
80 152
81 # If line contain 'depend()', set GOTDEPEND to 1 153 # If line contain 'depend()', set GOTDEPEND to 1
82 if ($0 ~ /depend[[:space:]]*\(\)/) 154 if ($0 ~ /depend[[:space:]]*\(\)/) {
155
83 GOTDEPEND = 1 156 GOTDEPEND = 1
157
158 print_header2()
159 print " # Actual depend() function ..." >> TMPCACHE
160 }
84 161
85 # We have the depend function... 162 # We have the depend function...
86 if (GOTDEPEND == 1) { 163 if (GOTDEPEND) {
87 164
88 # Basic theory is that COUNT will be 0 when we 165 # Basic theory is that COUNT will be 0 when we
89 # have matching '{' and '}' 166 # have matching '{' and '}'
90 COUNT += gsub(/{/, "{") 167 COUNT += gsub(/{/, "{")
91 COUNT -= gsub(/}/, "}") 168 COUNT -= gsub(/}/, "}")
92 169
93 # This is just to verify that we have started with 170 # This is just to verify that we have started with
94 # the body of depend() 171 # the body of depend()
95 SBCOUNT += gsub(/{/, "{") 172 SBCOUNT += gsub(/{/, "{")
173
174 # Make sure depend() contain something, else bash
175 # errors out (empty function).
176 if ((SBCOUNT > 0) && (COUNT == 0))
177 print " \treturn 0" >> TMPCACHE
96 178
97 # Print the depend() function 179 # Print the depend() function
98 print >> (DEPCACHE) 180 print " " $0 >> TMPCACHE
99 181
100 # If COUNT=0, and SBCOUNT>0, it means we have read 182 # If COUNT=0, and SBCOUNT>0, it means we have read
101 # all matching '{' and '}' for depend(), so stop. 183 # all matching '{' and '}' for depend(), so stop.
102 if ((SBCOUNT > 0) && (COUNT == 0)) { 184 if ((SBCOUNT > 0) && (COUNT == 0)) {
103 185
104 GOTDEPEND = 0 186 GOTDEPEND = 0
187 COUNT = 0
188 SBCOUNT = 0
105 ISRCSCRIPT = 0 189 ISRCSCRIPT = 0
106 COUNT = 0
107 SBCOUNT = 0
108 190
109 print_end() 191 print_end()
110 192
111 nextfile 193 NEXTFILE = 1
194 continue
195 }
196 }
112 } 197 }
198
199 MYFNR++
113 } 200 }
114 }
115}
116 201
117END { 202 close(RCSCRIPTS[count])
118 close (DEPCACHE)
119}
120 203
204 NEXTFILE = 0
205
206 }
207
208 system("mv "TMPCACHE" "SVCDIR"/depcache")
209}
210
211
212# vim:ts=4

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.19

  ViewVC Help
Powered by ViewVC 1.1.20