gencalls.sh 2.9 KB

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