summaryrefslogtreecommitdiff
path: root/lib/lib.pl
blob: fe82170f3af37d1ed741a6fc5913e1531fe773cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/perl

$[ = 1;			# set array base to 1
$, = ' ';		# set output field separator
$\ = "\n";		# set output record separator

sub toupper {
    local($_) = @_;
    tr/a-z/A-Z/;
    return $_;
}

sub tolower {
    local($_) = @_;
    tr/A-Z/a-z/;
    return $_;
}

print '/* lib.c */';
print "/* This is a generated file.  Please modify `lib.pl' */";
print '';

print '#include <config.h>';
print '';
print '#include <glibtop.h>';
print '#include <glibtop/error.h>';
print '#include <glibtop/open.h>';
print '';
print '#include <glibtop/sysdeps.h>';
print '#include <glibtop/union.h>';
print '#include <glib/gi18n-lib.h>';
print '';
print '#include <glibtop/command.h>';

print '';
print '/* Some required fields are missing. */';
print '';

print 'static void';
print '_glibtop_missing_feature (glibtop *server, const char *feature,';
print "\t\t\t  const guint64 present, guint64 *required)";
print '{';
print "\tguint64 old_required = *required;\n";
print "\t/* Return if we have all required fields. */";
print "\tif ((~present & old_required) == 0)";
print "\t\treturn;\n";
print "\tswitch (server->error_method) {";
print "\tcase GLIBTOP_ERROR_METHOD_WARN_ONCE:";
print "\t\t*required &= present;";
print "\tcase GLIBTOP_ERROR_METHOD_WARN:";
print "\t\tglibtop_warn_r (server,";
print "\t\t\t\t_(\"glibtop_get_%s (): Client requested \"";
print "\t\t\t\t  \"field mask %05lx, but only have %05lx.\"),";
print "\t\t\t\t feature, (unsigned long) old_required,";
print "\t\t\t\t (unsigned long) present);";
print "\t\tbreak;";
print "\tcase GLIBTOP_ERROR_METHOD_ABORT:";
print "\t\tglibtop_error_r (server,";
print "\t\t\t\t _(\"glibtop_get_%s (): Client requested \"";
print "\t\t\t\t  \"field mask %05lx, but only have %05lx.\"),";
print "\t\t\t\t feature, (unsigned long) old_required,";
print "\t\t\t\t (unsigned long) present);";
print "\t\tbreak;";
print "\t}";
print '}';

print '';
print '/* Library functions. */';
print '';

$convert{'long'} = 'gint64';
$convert{'ulong'} = 'guint64';
$convert{'pid_t'} = 'pid_t';
$convert{'int'} = 'int';
$convert{'ushort'} = 'unsigned short';
$convert{'unsigned'} = 'unsigned';

while (<>) {
    chop;	# strip record separator

    if (/^[^#]/) {
	&output($_);
    }
}

sub output {
    local($line) = @_;
    @line_fields = split(/\|/, $line, 9999);
    $retval = $line_fields[1];
    $feature = $line_fields[2];
    $param_def = $line_fields[4];

    $orig = $feature;
    $feature =~ s/^@//;
    $space = $feature;
    $space =~ s/./ /g;

    print $retval;
    if ($retval !~ /^void$/) {
	$prefix = 'retval = ';
	$prefix_space = '         ';
    }
    else {
	$prefix = '';
	$prefix_space = '';
    }

    if ($param_def eq 'string') {
	$call_param = ', ' . $line_fields[5];
	$param_buf = '';
	$buf_set = '';
	$param_decl = ",\n            " . $space . '    const char *' .

	  $line_fields[5];
	$send_ptr = "\n\tconst void *send_ptr = " . $line_fields[5] . ';';
	$send_size = "\n\tconst size_t send_size =\n\t\tstrlen (" .

	  $line_fields[5] . ') + 1;';
    }
    else {
	$call_param = '';
	$param_decl = '';
	$send_size = '';
	$send_ptr = '';
	$param_buf = '';
	$buf_set = '';
	$nr_params = (@params = split(/:/, $param_def, 9999));
	if ($nr_params) {
	    $param_buf = "\n\tstruct {\n";
	}
	for ($param = 1; $param <= $nr_params; $param++) {
	    $list = $params[$param];
	    $type = $params[$param];
	    $type =~ s/\(.*//;
	    $list =~ s/^.*\(//;
	    $list =~ s/\)$//;
	    $count = (@fields = split(/,/, $list, 9999));

	    if ($count > 0) {
		for ($field = 1; $field <= $count; $field++) {
		    $param_buf .= "\t\t$convert{$type} buf_$fields[$field];\n";
		    $buf_set .= "\tparam_buf.buf_$fields[$field] = $fields[$field];\n";
		}
	    }

	    for ($field = 1; $field <= $count; $field++) {
		if ($param_decl eq '') {
		    $param_decl = ",\n            " . $space . '    ';
		}
		else {
		    $param_decl = $param_decl . ', ';
		}
		$param_decl = $param_decl . '' . $convert{$type} . ' ' .

		  $fields[$field];
		$call_param = $call_param . ', ' . $fields[$field];
		if ($send_ptr eq '') {
		    $send_ptr = "\n\tconst void *send_ptr = &param_buf;";
		}
	    }
	}
	if ($nr_params) {
	    $param_buf .= "\t} param_buf;";
	    $send_size  = "\n\tconst size_t send_size = sizeof param_buf;";
	}
	else {
	    $send_size = "\n\tconst size_t send_size = 0;";
	}
	if ($send_ptr eq '') {
	    $send_ptr = "\n\tconst void *send_ptr = NULL;";
	}
    }

    print 'glibtop_get_' . $feature . '_l (glibtop *server, glibtop_' .

      $feature . ' *buf' . $param_decl . ')';

    print "{" . $param_buf;

    print $send_ptr . '' . $send_size;
    if ($retval !~ /^void$/) {
	print "\t" . $retval . ' retval = (' . $retval . ') 0;';
    }
    print $buf_set;

    print "\tglibtop_init_r (&server, (1 << GLIBTOP_SYSDEPS_" .

      &toupper($feature) . '), 0);';

    print '';
    print "\t/* If neccessary, we ask the server for the requested";
    print "\t * feature. If not, we call the sysdeps function. */";
    print '';

    print "\tif ((server->flags & _GLIBTOP_INIT_STATE_SERVER) &&";
    print "\t    (server->features & (1 << GLIBTOP_SYSDEPS_" .

      &toupper($feature) . ')))';
    print "\t{";

    print "\t\t" . $prefix . 'glibtop_call_l (server, GLIBTOP_CMND_' .

      &toupper($feature) . ',';
    print "\t\t\t\t" . $prefix_space . 'send_size, send_ptr,';
    print "\t\t\t\t" . $prefix_space . 'sizeof (glibtop_' . $feature .

      '), buf);';

    print "\t} else {";

    if ($orig !~ /^@/) {
	print '#if (!GLIBTOP_SUID_' . &toupper($feature) . ')';
    }
    print "\t\t" . $prefix . 'glibtop_get_' . $feature . '_s (server, buf' .

      $call_param . ');';

    if ($orig !~ /^@/) {
	print '#else';
	print "\t\terrno = ENOSYS;";
	print "\t\tglibtop_error_io_r (server, \"glibtop_get_" . $feature .

	  "\");";
	print '#endif';
    }

    print "\t}";

    print '';
    print "\t/* Make sure that all required fields are present. */";
    print '';

    print "\tif (buf->flags & server->required." . $feature . ')';
    print "\t\t_glibtop_missing_feature (server, \"" . $feature .

      "\", buf->flags,";
    print "\t\t\t\t\t  &server->required." . $feature . ');';

    if ($retval !~ /^void$/) {
	print "\n\t/* Now we can return. */";
	print "\n\treturn retval;";
    }

    print '}';
    print '';
}