1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | $[ = 1; # set array base to 1 |
---|
4 | $, = ' '; # set output field separator |
---|
5 | $\ = "\n"; # set output record separator |
---|
6 | |
---|
7 | sub toupper { |
---|
8 | local($_) = @_; |
---|
9 | tr/a-z/A-Z/; |
---|
10 | return $_; |
---|
11 | } |
---|
12 | |
---|
13 | sub tolower { |
---|
14 | local($_) = @_; |
---|
15 | tr/A-Z/a-z/; |
---|
16 | return $_; |
---|
17 | } |
---|
18 | |
---|
19 | print '/* lib.c */'; |
---|
20 | print "/* This is a generated file. Please modify `lib.pl' */"; |
---|
21 | print ''; |
---|
22 | |
---|
23 | print '#include <config.h>'; |
---|
24 | print ''; |
---|
25 | print '#include <glibtop.h>'; |
---|
26 | print '#include <glibtop/open.h>'; |
---|
27 | print ''; |
---|
28 | print '#include <glibtop/sysdeps.h>'; |
---|
29 | print '#include <glibtop/union.h>'; |
---|
30 | print '#include "libgtop-i18n.h"'; |
---|
31 | print ''; |
---|
32 | print '#include <glibtop/command.h>'; |
---|
33 | |
---|
34 | print ''; |
---|
35 | print '/* Some required fields are missing. */'; |
---|
36 | print ''; |
---|
37 | |
---|
38 | print 'static void'; |
---|
39 | print '_glibtop_missing_feature (glibtop *server, const char *feature,'; |
---|
40 | print "\t\t\t const guint64 present, guint64 *required)"; |
---|
41 | print '{'; |
---|
42 | print "\tguint64 old_required = *required;\n"; |
---|
43 | print "\t/* Return if we have all required fields. */"; |
---|
44 | print "\tif ((~present & old_required) == 0)"; |
---|
45 | print "\t\treturn;\n"; |
---|
46 | print "\tswitch (server->error_method) {"; |
---|
47 | print "\tcase GLIBTOP_ERROR_METHOD_WARN_ONCE:"; |
---|
48 | print "\t\t*required &= present;"; |
---|
49 | print "\tcase GLIBTOP_ERROR_METHOD_WARN:"; |
---|
50 | print "\t\tglibtop_warn_r (server,"; |
---|
51 | print "\t\t\t\t_(\"glibtop_get_%s (): Client requested \""; |
---|
52 | print "\t\t\t\t \"field mask %05lx, but only have %05lx.\"),"; |
---|
53 | print "\t\t\t\t feature, (unsigned long) old_required,"; |
---|
54 | print "\t\t\t\t (unsigned long) present);"; |
---|
55 | print "\t\tbreak;"; |
---|
56 | print "\tcase GLIBTOP_ERROR_METHOD_ABORT:"; |
---|
57 | print "\t\tglibtop_error_r (server,"; |
---|
58 | print "\t\t\t\t _(\"glibtop_get_%s (): Client requested \""; |
---|
59 | print "\t\t\t\t \"field mask %05lx, but only have %05lx.\"),"; |
---|
60 | print "\t\t\t\t feature, (unsigned long) old_required,"; |
---|
61 | print "\t\t\t\t (unsigned long) present);"; |
---|
62 | print "\t\tbreak;"; |
---|
63 | print "\t}"; |
---|
64 | print '}'; |
---|
65 | |
---|
66 | print ''; |
---|
67 | print '/* Library functions. */'; |
---|
68 | print ''; |
---|
69 | |
---|
70 | $convert{'long'} = 'gint64'; |
---|
71 | $convert{'ulong'} = 'guint64'; |
---|
72 | $convert{'pid_t'} = 'pid_t'; |
---|
73 | $convert{'int'} = 'int'; |
---|
74 | $convert{'ushort'} = 'unsigned short'; |
---|
75 | $convert{'unsigned'} = 'unsigned'; |
---|
76 | |
---|
77 | while (<>) { |
---|
78 | chop; # strip record separator |
---|
79 | |
---|
80 | if (/^[^#]/) { |
---|
81 | &output($_); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | sub output { |
---|
86 | local($line) = @_; |
---|
87 | @line_fields = split(/\|/, $line, 9999); |
---|
88 | $retval = $line_fields[1]; |
---|
89 | $feature = $line_fields[2]; |
---|
90 | $param_def = $line_fields[4]; |
---|
91 | |
---|
92 | $orig = $feature; |
---|
93 | $feature =~ s/^@//; |
---|
94 | $space = $feature; |
---|
95 | $space =~ s/./ /g; |
---|
96 | |
---|
97 | print $retval; |
---|
98 | if ($retval !~ /^void$/) { |
---|
99 | $prefix = 'retval = '; |
---|
100 | $prefix_space = ' '; |
---|
101 | } |
---|
102 | else { |
---|
103 | $prefix = ''; |
---|
104 | $prefix_space = ''; |
---|
105 | } |
---|
106 | |
---|
107 | if ($param_def eq 'string') { |
---|
108 | $call_param = ', ' . $line_fields[5]; |
---|
109 | $param_decl = ",\n " . $space . ' const char *' . |
---|
110 | |
---|
111 | $line_fields[5]; |
---|
112 | $send_ptr = "\n\tconst void *send_ptr = " . $line_fields[5] . ';'; |
---|
113 | $send_size = "\n\tconst size_t send_size =\n\t\tstrlen (" . |
---|
114 | |
---|
115 | $line_fields[5] . ') + 1;'; |
---|
116 | } |
---|
117 | else { |
---|
118 | $call_param = ''; |
---|
119 | $param_decl = ''; |
---|
120 | $send_size = ''; |
---|
121 | $send_ptr = ''; |
---|
122 | $nr_params = (@params = split(/:/, $param_def, 9999)); |
---|
123 | for ($param = 1; $param <= $nr_params; $param++) { |
---|
124 | $list = $params[$param]; |
---|
125 | $type = $params[$param]; |
---|
126 | $type =~ s/\(.*//; |
---|
127 | $list =~ s/^.*\(//; |
---|
128 | $list =~ s/\)$//; |
---|
129 | $count = (@fields = split(/,/, $list, 9999)); |
---|
130 | for ($field = 1; $field <= $count; $field++) { |
---|
131 | if ($param_decl eq '') { |
---|
132 | $param_decl = ",\n " . $space . ' '; |
---|
133 | } |
---|
134 | else { |
---|
135 | $param_decl = $param_decl . ', '; |
---|
136 | } |
---|
137 | $param_decl = $param_decl . '' . $convert{$type} . ' ' . |
---|
138 | |
---|
139 | $fields[$field]; |
---|
140 | $call_param = $call_param . ', ' . $fields[$field]; |
---|
141 | if ($send_ptr eq '') { |
---|
142 | $send_ptr = "\n\tconst void *send_ptr = &" . |
---|
143 | |
---|
144 | $fields[$field] . ';'; |
---|
145 | } |
---|
146 | if ($send_size eq '') { |
---|
147 | $send_size = "\n\tconst size_t send_size =\n\t\t"; |
---|
148 | } |
---|
149 | else { |
---|
150 | $send_size = $send_size . ' + '; |
---|
151 | } |
---|
152 | $send_size = $send_size . 'sizeof (' . $fields[$field] . ')'; |
---|
153 | } |
---|
154 | } |
---|
155 | if ($send_size ne '') { |
---|
156 | $send_size = $send_size . ';'; |
---|
157 | } |
---|
158 | else { |
---|
159 | $send_size = "\n\tconst size_t send_size = 0;"; |
---|
160 | } |
---|
161 | if ($send_ptr eq '') { |
---|
162 | $send_ptr = "\n\tconst void *send_ptr = NULL;"; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | print 'glibtop_get_' . $feature . '_l (glibtop *server, glibtop_' . |
---|
167 | |
---|
168 | $feature . ' *buf' . $param_decl . ')'; |
---|
169 | |
---|
170 | print '{' . $send_ptr . '' . $send_size; |
---|
171 | if ($retval !~ /^void$/) { |
---|
172 | print "\t" . $retval . ' retval = (' . $retval . ') 0;'; |
---|
173 | } |
---|
174 | print ''; |
---|
175 | |
---|
176 | print "\tglibtop_init_r (&server, (1 << GLIBTOP_SYSDEPS_" . |
---|
177 | |
---|
178 | &toupper($feature) . '), 0);'; |
---|
179 | |
---|
180 | print ''; |
---|
181 | print "\t/* If neccessary, we ask the server for the requested"; |
---|
182 | print "\t * feature. If not, we call the sysdeps function. */"; |
---|
183 | print ''; |
---|
184 | |
---|
185 | print "\tif ((server->flags & _GLIBTOP_INIT_STATE_SERVER) &&"; |
---|
186 | print "\t (server->features & (1 << GLIBTOP_SYSDEPS_" . |
---|
187 | |
---|
188 | &toupper($feature) . ')))'; |
---|
189 | print "\t{"; |
---|
190 | |
---|
191 | print "\t\t" . $prefix . 'glibtop_call_l (server, GLIBTOP_CMND_' . |
---|
192 | |
---|
193 | &toupper($feature) . ','; |
---|
194 | print "\t\t\t\t" . $prefix_space . 'send_size, send_ptr,'; |
---|
195 | print "\t\t\t\t" . $prefix_space . 'sizeof (glibtop_' . $feature . |
---|
196 | |
---|
197 | '), buf);'; |
---|
198 | |
---|
199 | print "\t} else {"; |
---|
200 | |
---|
201 | if ($orig !~ /^@/) { |
---|
202 | print '#if (!GLIBTOP_SUID_' . &toupper($feature) . ')'; |
---|
203 | } |
---|
204 | print "\t\t" . $prefix . 'glibtop_get_' . $feature . '_s (server, buf' . |
---|
205 | |
---|
206 | $call_param . ');'; |
---|
207 | |
---|
208 | if ($orig !~ /^@/) { |
---|
209 | print '#else'; |
---|
210 | print "\t\terrno = ENOSYS;"; |
---|
211 | print "\t\tglibtop_error_io_r (server, \"glibtop_get_" . $feature . |
---|
212 | |
---|
213 | "\");"; |
---|
214 | print '#endif'; |
---|
215 | } |
---|
216 | |
---|
217 | print "\t}"; |
---|
218 | |
---|
219 | print ''; |
---|
220 | print "\t/* Make sure that all required fields are present. */"; |
---|
221 | print ''; |
---|
222 | |
---|
223 | print "\tif (buf->flags & server->required." . $feature . ')'; |
---|
224 | print "\t\t_glibtop_missing_feature (server, \"" . $feature . |
---|
225 | |
---|
226 | "\", buf->flags,"; |
---|
227 | print "\t\t\t\t\t &server->required." . $feature . ');'; |
---|
228 | |
---|
229 | if ($retval !~ /^void$/) { |
---|
230 | print "\n\t/* Now we can return. */"; |
---|
231 | print "\n\treturn retval;"; |
---|
232 | } |
---|
233 | |
---|
234 | print '}'; |
---|
235 | print ''; |
---|
236 | } |
---|