/[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.3 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.3 2002/11/18 09:02:26 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 $
5 4
6function print_start() { 5function print_start() {
6 print "source /sbin/functions.sh" >> TMPCACHE
7 print "" >> TMPCACHE
7 print "need() {" >> (DEPCACHE) 8 print "need() {" >> TMPCACHE
8 print " echo \"NEED $*\"; return 0" >> (DEPCACHE) 9 print " echo \"NEED $*\"; return 0" >> TMPCACHE
9 print "}" >> (DEPCACHE) 10 print "}" >> TMPCACHE
10 print "" >> (DEPCACHE) 11 print "" >> TMPCACHE
11 print "use() {" >> (DEPCACHE) 12 print "use() {" >> TMPCACHE
12 print " echo \"USE $*\"; return 0" >> (DEPCACHE) 13 print " echo \"USE $*\"; return 0" >> TMPCACHE
13 print "}" >> (DEPCACHE) 14 print "}" >> TMPCACHE
14 print "" >> (DEPCACHE) 15 print "" >> TMPCACHE
15 print "before() {" >> (DEPCACHE) 16 print "before() {" >> TMPCACHE
16 print " echo \"BEFORE $*\"; return 0" >> (DEPCACHE) 17 print " echo \"BEFORE $*\"; return 0" >> TMPCACHE
17 print "}" >> (DEPCACHE) 18 print "}" >> TMPCACHE
18 print "" >> (DEPCACHE) 19 print "" >> TMPCACHE
19 print "after() {" >> (DEPCACHE) 20 print "after() {" >> TMPCACHE
20 print " echo \"AFTER $*\"; return 0" >> (DEPCACHE) 21 print " echo \"AFTER $*\"; return 0" >> TMPCACHE
21 print "}" >> (DEPCACHE) 22 print "}" >> TMPCACHE
22 print "" >> (DEPCACHE) 23 print "" >> TMPCACHE
23 print "provide() {" >> (DEPCACHE) 24 print "provide() {" >> TMPCACHE
24 print " echo \"PROVIDE $*\"; return 0" >> (DEPCACHE) 25 print " echo \"PROVIDE $*\"; return 0" >> TMPCACHE
25 print "}" >> (DEPCACHE) 26 print "}" >> TMPCACHE
26 print "" >> (DEPCACHE) 27 print "" >> TMPCACHE
28 print "parallel() {" >> TMPCACHE
29 print " echo \"PARALLEL $*\"; return 0" >> TMPCACHE
30 print "}" >> TMPCACHE
31 print "" >> TMPCACHE
27} 32}
28 33
29function print_header() { 34function print_header1(mtime) {
30 print "#*** " MYFILENAME " ***" >> (DEPCACHE) 35 print "#*** " MYFILENAME " ***" >> TMPCACHE
31 print "" >> (DEPCACHE) 36 print "" >> TMPCACHE
32 print "myservice=\"" MYFILENAME "\"" >> (DEPCACHE) 37 print "myservice=\"" MYFILENAME "\"" >> TMPCACHE
33 print "myservice=\"${myservice##*/}\"" >> (DEPCACHE) 38 print "myservice=\"${myservice##*/}\"" >> TMPCACHE
34 print "echo \"RCSCRIPT ${myservice}\"" >> (DEPCACHE) 39 print "echo \"RCSCRIPT ${myservice}\"" >> TMPCACHE
35 print "" >> (DEPCACHE) 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
36 print "depend() {" >> (DEPCACHE) 57 print " depend() {" >> TMPCACHE
37 print " return 0" >> (DEPCACHE) 58 print " return 0" >> TMPCACHE
38 print "}" >> (DEPCACHE) 59 print " }" >> TMPCACHE
39 print "" >> (DEPCACHE) 60 print "" >> TMPCACHE
40} 61}
41 62
42function print_end() { 63function print_end() {
43 print "" >> (DEPCACHE) 64 print "" >> TMPCACHE
44 print "depend" >> (DEPCACHE) 65 print " depend" >> TMPCACHE
66 print ")" >> TMPCACHE
45 print "" >> (DEPCACHE) 67 print "" >> TMPCACHE
46} 68}
47 69
48BEGIN { 70BEGIN {
49 71
50 extension("/lib/rcscripts/filefuncs.so", "dlload") 72 extension("/lib/rcscripts/filefuncs.so", "dlload")
73
74 # Get our environment variables
75 SVCDIR = ENVIRON["SVCDIR"]
76 if (SVCDIR == "") {
77 eerror("Could not get SVCDIR!")
78 exit 1
79 }
80
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 }
51 88
52 pipe = "ls /etc/init.d/*" 89 pipe = "ls /etc/init.d/*"
53 while ((pipe | getline tmpstring) > 0) 90 while ((pipe | getline tmpstring) > 0)
54 scripts = scripts " " tmpstring 91 scripts = scripts " " tmpstring
55 close(pipe) 92 close(pipe)
57 split(scripts, TMPRCSCRIPTS) 94 split(scripts, TMPRCSCRIPTS)
58 95
59 # Make sure that its a file we are working with, 96 # Make sure that its a file we are working with,
60 # and do not process scripts, source or backup files. 97 # and do not process scripts, source or backup files.
61 for (x in TMPRCSCRIPTS) 98 for (x in TMPRCSCRIPTS)
62 if ((isfile(TMPRCSCRIPTS[x])) && 99 if (((isfile(TMPRCSCRIPTS[x])) || (islink(TMPRCSCRIPTS[x]))) &&
63 (TMPRCSCRIPTS[x] !~ /((\.(sh|c|bak))|\~)$/)) { 100 (TMPRCSCRIPTS[x] !~ /((\.(c|bak))|\~)$/)) {
64 101
65 RCCOUNT++ 102 RCCOUNT++
66 103
67 RCSCRIPTS[RCCOUNT] = TMPRCSCRIPTS[x] 104 RCSCRIPTS[RCCOUNT] = TMPRCSCRIPTS[x]
68 } 105 }
69 106
70 DEPCACHE=SVCDIR "/depcache" 107 if (RCCOUNT == 0) {
71 108 eerror("No scripts to process!")
72 unlink(DEPCACHE) 109 system("rm -f "TMPCACHE)
110 exit 1
111 }
73 112
74 print_start() 113 print_start()
75 114
76 for (count = 1;count <= RCCOUNT;count++) { 115 for (count = 1;count <= RCCOUNT;count++) {
77 116
78 MYFNR = 1 117 MYFNR = 1
79 MYFILENAME = RCSCRIPTS[count] 118 MYFILENAME = RCSCRIPTS[count]
119 STAT_DATA[1] = 1
80 120
81 while (((getline < (RCSCRIPTS[count])) > 0) && (!NEXTFILE)) { 121 while (((getline < (RCSCRIPTS[count])) > 0) && (!NEXTFILE)) {
82 122
83 # If line start with a '#' and is the first line 123 # If line start with a '#' and is the first line
84 if (($0 ~ /^[[:space:]]*#/) && (MYFNR == 1)) { 124 if (($0 ~ /^[[:space:]]*#/) && (MYFNR == 1)) {
85 125
86 # Remove any spaces and tabs 126 # Remove any spaces and tabs
87 gsub(/[[:space:]]+/, "") 127 gsub(/[[:space:]]+/, "")
88 128
89 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 "\"")
90 140
91 ISRCSCRIPT = 1 141 ISRCSCRIPT = 1
92 print_header() 142 print_header1(STAT_DATA["mtime"])
93 } else { 143 } else {
94 144
95 NEXTFILE = 1 145 NEXTFILE = 1
96 continue 146 continue
97 } 147 }
98 } 148 }
99 149
100 # Filter out comments and only process if its a rcscript 150 # Filter out comments and only process if its a rcscript
101 if (($0 !~ /[[:space:]]*#/) && (ISRCSCRIPT == 1 )) { 151 if (($0 !~ /^[[:space:]]*#/) && (ISRCSCRIPT)) {
102 152
103 # If line contain 'depend()', set GOTDEPEND to 1 153 # If line contain 'depend()', set GOTDEPEND to 1
104 if ($0 ~ /depend[[:space:]]*\(\)/) 154 if ($0 ~ /depend[[:space:]]*\(\)/) {
155
105 GOTDEPEND = 1 156 GOTDEPEND = 1
157
158 print_header2()
159 print " # Actual depend() function ..." >> TMPCACHE
160 }
106 161
107 # We have the depend function... 162 # We have the depend function...
108 if (GOTDEPEND == 1) { 163 if (GOTDEPEND) {
109 164
110 # Basic theory is that COUNT will be 0 when we 165 # Basic theory is that COUNT will be 0 when we
111 # have matching '{' and '}' 166 # have matching '{' and '}'
112 COUNT += gsub(/{/, "{") 167 COUNT += gsub(/{/, "{")
113 COUNT -= gsub(/}/, "}") 168 COUNT -= gsub(/}/, "}")
114 169
115 # This is just to verify that we have started with 170 # This is just to verify that we have started with
116 # the body of depend() 171 # the body of depend()
117 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
118 178
119 # Print the depend() function 179 # Print the depend() function
120 print >> (DEPCACHE) 180 print " " $0 >> TMPCACHE
121 181
122 # If COUNT=0, and SBCOUNT>0, it means we have read 182 # If COUNT=0, and SBCOUNT>0, it means we have read
123 # all matching '{' and '}' for depend(), so stop. 183 # all matching '{' and '}' for depend(), so stop.
124 if ((SBCOUNT > 0) && (COUNT == 0)) { 184 if ((SBCOUNT > 0) && (COUNT == 0)) {
125 185
126 GOTDEPEND = 0 186 GOTDEPEND = 0
127 ISRCSCRIPT = 0
128 COUNT = 0 187 COUNT = 0
129 SBCOUNT = 0 188 SBCOUNT = 0
189 ISRCSCRIPT = 0
130 190
131 print_end() 191 print_end()
132 192
133 NEXTFILE = 1 193 NEXTFILE = 1
134 continue 194 continue
135 } 195 }
136 } 196 }
137 } 197 }
143 203
144 NEXTFILE = 0 204 NEXTFILE = 0
145 205
146 } 206 }
147 207
148 close (DEPCACHE) 208 system("mv "TMPCACHE" "SVCDIR"/depcache")
149} 209}
150 210
151 211
152# vim:ts=4 212# vim:ts=4

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

  ViewVC Help
Powered by ViewVC 1.1.20