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

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

  ViewVC Help
Powered by ViewVC 1.1.20