Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/user/testbin/randcall/gencalls.sh
734 views
1
#!/bin/sh
2
#
3
# gencalls.sh - generate calls.c and calls.h
4
#
5
# Usage: gencalls.sh callspecs-file
6
7
if [ "x$1" = x ]; then
8
echo "Usage: $0 callspecs-file"
9
exit 1
10
fi
11
12
awk < $1 '
13
14
BEGIN {
15
type["ptr"] = "void *";
16
type["int"] = "int";
17
type["off"] = "off_t";
18
type["size"] = "size_t";
19
fmt["ptr"] = "%p";
20
fmt["int"] = "%d";
21
fmt["off"] = "%ld";
22
fmt["size"] = "%lu";
23
cast["ptr"] = "";
24
cast["int"] = "";
25
cast["off"] = "(long)";
26
cast["size"] = "(unsigned long)";
27
28
printf "/* Automatically generated file; do not edit */\n";
29
printf "#include <sys/types.h>\n";
30
printf "#include <sys/stat.h>\n";
31
printf "#include <assert.h>\n";
32
printf "#include <unistd.h>\n";
33
printf "#include <stdio.h>\n";
34
printf "#include <stdlib.h>\n";
35
printf "#include <errno.h>\n";
36
printf "#include <err.h>\n";
37
printf "\n";
38
printf "#include \"extern.h\"\n";
39
printf "\n";
40
41
printf "typedef void (*tryfunc)(int dofork);\n";
42
printf "\n";
43
44
n=0;
45
}
46
47
{
48
printf "static\n";
49
printf "void\n";
50
printf "try_%s(int dofork)\n", $2;
51
printf "{\n";
52
for (i=3; i<=NF; i++) {
53
printf "\t%s a%d = rand%s();\n", type[$i], i-3, $i;
54
}
55
printf "\tint result, pid, status;\n";
56
printf "\tchar buf[128];\n";
57
printf "\n";
58
59
printf "\tsnprintf(buf, sizeof(buf), \"%s(", $2;
60
for (i=3; i<=NF; i++) {
61
printf "%s", fmt[$i];
62
if (i<NF) printf ", ";
63
}
64
printf ")\",\n\t\t";
65
for (i=3; i<=NF; i++) {
66
printf "%s(a%d)", cast[$i], i-3;
67
if (i<NF) printf ", ";
68
}
69
printf ");\n";
70
printf"\tprintf(\"%%-47s\", buf);\n";
71
#printf "\tfflush(stdout);\n";
72
printf "\n";
73
74
printf "\tpid = dofork ? fork() : 0;\n";
75
printf "\tif (pid<0) {\n";
76
printf "\t\terr(1, \"fork\");\n";
77
printf "\t}\n";
78
printf "\tif (pid>0) {\n";
79
printf "\t\twaitpid(pid, &status, 0);\n";
80
printf "\t\treturn;\n";
81
printf "\t}\n";
82
printf "\n";
83
84
printf "\tresult = %s(", $2;
85
for (i=3; i<=NF; i++) {
86
printf "a%d", i-3;
87
if (i<NF) printf ", ";
88
}
89
printf ");\n";
90
91
printf "\tprintf(\" result %%d, errno %%d\\n\", result, errno);\n";
92
printf "\tif (dofork) {\n";
93
printf "\t\texit(0);\n";
94
printf "\t}\n";
95
96
printf "}\n";
97
printf "\n";
98
99
asst[$2] = $1;
100
all[n++] = $2;
101
}
102
103
END {
104
for (a=2; a<=5; a++) {
105
printf "static tryfunc funcs%d[] = {\n", a;
106
for (i=0; i<n; i++) {
107
if (asst[all[i]] <= a) {
108
printf "\ttry_%s,\n", all[i];
109
}
110
}
111
printf "\tNULL\n";
112
printf "};\n";
113
printf "\n";
114
}
115
116
printf "static tryfunc *tables[4] = {\n";
117
printf "\tfuncs2,\n";
118
printf "\tfuncs3,\n";
119
printf "\tfuncs4,\n";
120
printf "\tfuncs5,\n";
121
printf "};\n";
122
printf "\n";
123
124
printf "void\n";
125
printf "trycalls(int asst, int dofork, int count)\n"
126
printf "{\n";
127
printf "\ttryfunc *list;\n";
128
printf "\tint i, j;\n";
129
printf "\n";
130
131
printf "\tassert(asst>=2 && asst<=5);\n";
132
printf "\tlist = tables[asst-2];\n";
133
printf "\n";
134
135
printf "\tfor (i=0; i<count; i++) {\n";
136
printf "\t\tfor (j=0; list[j]; j++) {\n";
137
printf "\t\t\t(*list[j])(dofork);\n";
138
printf "\t\t}\n";
139
printf "\t}\n";
140
printf "}\n";
141
printf "\n";
142
}
143
'
144
145