source: trunk/third/libgtop/lib/lib.pl @ 20897

Revision 20897, 5.9 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r20896, which included commits to RCS files with non-trunk default branches.
  • Property svn:executable set to *
Line 
1#!/usr/bin/perl
2
3$[ = 1;                 # set array base to 1
4$, = ' ';               # set output field separator
5$\ = "\n";              # set output record separator
6
7sub toupper {
8    local($_) = @_;
9    tr/a-z/A-Z/;
10    return $_;
11}
12
13sub tolower {
14    local($_) = @_;
15    tr/A-Z/a-z/;
16    return $_;
17}
18
19print '/* lib.c */';
20print "/* This is a generated file.  Please modify `lib.pl' */";
21print '';
22
23print '#include <config.h>';
24print '';
25print '#include <glibtop.h>';
26print '#include <glibtop/open.h>';
27print '';
28print '#include <glibtop/sysdeps.h>';
29print '#include <glibtop/union.h>';
30print '#include "libgtop-i18n.h"';
31print '';
32print '#include <glibtop/command.h>';
33
34print '';
35print '/* Some required fields are missing. */';
36print '';
37
38print 'static void';
39print '_glibtop_missing_feature (glibtop *server, const char *feature,';
40print "\t\t\t  const guint64 present, guint64 *required)";
41print '{';
42print "\tguint64 old_required = *required;\n";
43print "\t/* Return if we have all required fields. */";
44print "\tif ((~present & old_required) == 0)";
45print "\t\treturn;\n";
46print "\tswitch (server->error_method) {";
47print "\tcase GLIBTOP_ERROR_METHOD_WARN_ONCE:";
48print "\t\t*required &= present;";
49print "\tcase GLIBTOP_ERROR_METHOD_WARN:";
50print "\t\tglibtop_warn_r (server,";
51print "\t\t\t\t_(\"glibtop_get_%s (): Client requested \"";
52print "\t\t\t\t  \"field mask %05lx, but only have %05lx.\"),";
53print "\t\t\t\t feature, (unsigned long) old_required,";
54print "\t\t\t\t (unsigned long) present);";
55print "\t\tbreak;";
56print "\tcase GLIBTOP_ERROR_METHOD_ABORT:";
57print "\t\tglibtop_error_r (server,";
58print "\t\t\t\t _(\"glibtop_get_%s (): Client requested \"";
59print "\t\t\t\t  \"field mask %05lx, but only have %05lx.\"),";
60print "\t\t\t\t feature, (unsigned long) old_required,";
61print "\t\t\t\t (unsigned long) present);";
62print "\t\tbreak;";
63print "\t}";
64print '}';
65
66print '';
67print '/* Library functions. */';
68print '';
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
77while (<>) {
78    chop;       # strip record separator
79
80    if (/^[^#]/) {
81        &output($_);
82    }
83}
84
85sub 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}
Note: See TracBrowser for help on using the repository browser.