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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361
|
(* libguestfs
* Copyright (C) 2009-2020 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*)
(* Please read generator/README first. *)
open Printf
open Std_utils
open Types
open Utils
open Pr
open Docstrings
open Optgroups
open Actions
open Structs
open Events
let generate_header = generate_header ~inputs:["generator/c.ml"]
(* Generate C API. *)
type optarg_proto = Dots | VA | Argv
let is_public { visibility = v } = match v with
| VPublic | VPublicNoFish | VStateTest | VDebug -> true
| VBindTest | VInternal -> false
let is_private f = not (is_public f)
let public_functions_sorted =
List.filter is_public (actions |> sort)
let private_functions_sorted =
List.filter is_private (actions |> sort)
(* Generate a C function prototype. *)
let rec generate_prototype ?(extern = true) ?(static = false)
?(semicolon = true)
?(single_line = false) ?(indent = "") ?(newline = false)
?(in_daemon = false)
?(dll_public = false)
?(attribute_noreturn = false)
?(prefix = "") ?(suffix = "")
?handle
?(optarg_proto = Dots)
name (ret, args, optargs) =
pr "%s" indent;
if extern then pr "extern ";
if dll_public then pr "GUESTFS_DLL_PUBLIC ";
if static then pr "static ";
let space = ref false in
(match ret with
| RErr
| RInt _
| RBool _ ->
pr "int";
space := true
| RInt64 _ ->
pr "int64_t";
space := true
| RConstString _ | RConstOptString _ ->
pr "const char *"
| RString _ | RBufferOut _ ->
pr "char *"
| RStringList _ | RHashtable _ ->
pr "char **"
| RStruct (_, typ) ->
if not in_daemon then pr "struct guestfs_%s *" typ
else pr "guestfs_int_%s *" typ
| RStructList (_, typ) ->
if not in_daemon then pr "struct guestfs_%s_list *" typ
else pr "guestfs_int_%s_list *" typ
);
if single_line && !space then pr " ";
if attribute_noreturn then pr "__attribute__((noreturn)) ";
if not single_line then pr "\n%s" indent;
let is_RBufferOut = match ret with RBufferOut _ -> true | _ -> false in
pr "%s%s%s (" prefix name suffix;
let comma = ref false in
(match handle with
| None -> ()
| Some handle -> pr "guestfs_h *%s" handle; comma := true
);
let next () =
if !comma then (
if single_line then pr ", "
else (
let namelen = String.length prefix + String.length name +
String.length suffix + 2 in
pr ",\n%s%s" indent (String.spaces namelen)
)
);
comma := true
in
List.iter (
function
| String ((PlainString|Device|Dev_or_Path|Pathname|Key|GUID|Filename), n)
| OptString n ->
next ();
pr "const char *%s" n
| String ((Mountable|Mountable_or_Path), n) ->
next();
if in_daemon then
pr "const mountable_t *%s" n
else
pr "const char *%s" n
| StringList (_, n) ->
next ();
pr "char *const *%s" n
| Bool n -> next (); pr "int %s" n
| Int n -> next (); pr "int %s" n
| Int64 n -> next (); pr "int64_t %s" n
| String ((FileIn|FileOut), n) ->
if not in_daemon then (next (); pr "const char *%s" n)
| BufferIn n ->
next ();
pr "const char *%s" n;
next ();
pr "size_t %s_size" n
| Pointer (t, n) ->
next ();
pr "void * /* really %s */ %s" t n
) args;
if is_RBufferOut then (next (); pr "size_t *size_r");
if optargs <> [] then (
next ();
match optarg_proto with
| Dots -> pr "..."
| VA -> pr "va_list args"
| Argv -> pr "const struct guestfs_%s_argv *optargs" name
);
(* Was anything output between ()? If not, it's a 'function(void)'. *)
if !comma = false then pr "void";
pr ")";
if semicolon then pr ";";
if newline then pr "\n"
(* Generate C call arguments, eg "(handle, foo, bar)" *)
and generate_c_call_args ?handle ?(implicit_size_ptr = "&size")
?(in_daemon = false)
(ret, args, optargs) =
pr "(";
let comma = ref false in
let next () =
if !comma then pr ", ";
comma := true
in
(match handle with
| None -> ()
| Some handle -> pr "%s" handle; comma := true
);
List.iter (
function
| BufferIn n ->
next ();
pr "%s, %s_size" n n
| String ((Mountable|Mountable_or_Path), n) ->
next ();
(if in_daemon then pr "&%s" else pr "%s") n
| arg ->
next ();
pr "%s" (name_of_argt arg)
) args;
(* For RBufferOut calls, add implicit size pointer parameter. *)
(match ret with
| RBufferOut _ ->
next ();
pr "%s" implicit_size_ptr
| _ -> ()
);
(* For calls with optional arguments, add implicit optargs parameter. *)
if optargs <> [] then (
next ();
pr "optargs"
);
pr ")"
(* Generate the pod documentation for the C API. *)
and generate_actions_pod () =
generate_header PODStyle GPLv2plus;
List.iter (
function
| ({ once_had_no_optargs = false } as f) ->
generate_actions_pod_entry f
| ({ once_had_no_optargs = true } as f) ->
generate_actions_pod_back_compat_entry f;
generate_actions_pod_entry f
) (actions |> documented_functions |> sort)
and generate_actions_pod_entry ({ c_name;
style = ret, args, optargs as style } as f) =
pr "=head2 guestfs_%s\n\n" c_name;
generate_prototype ~extern:false ~indent:" " ~handle:"g"
~prefix:"guestfs_" c_name style;
pr "\n\n";
(match deprecation_notice ~prefix:"guestfs_" f with
| None -> ()
| Some txt -> pr "%s\n\n" txt
);
if optargs <> [] then (
pr "You may supply a list of optional arguments to this call.\n";
pr "Use zero or more of the following pairs of parameters,\n";
pr "and terminate the list with C<-1> on its own.\n";
pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
List.iter (
fun argt ->
let n = name_of_optargt argt in
pr " GUESTFS_%s_%s, " (String.uppercase_ascii c_name)
(String.uppercase_ascii n);
match argt with
| OBool n -> pr "int %s,\n" n
| OInt n -> pr "int %s,\n" n
| OInt64 n -> pr "int64_t %s,\n" n
| OString n -> pr "const char *%s,\n" n
| OStringList n -> pr "char *const *%s,\n" n
) optargs;
pr "\n";
);
pr "%s\n\n" f.longdesc;
let ret, args, optargs = style in
(match ret with
| RErr ->
pr "This function returns 0 on success or -1 on error.\n\n"
| RInt _ ->
pr "On error this function returns -1.\n\n"
| RInt64 _ ->
pr "On error this function returns -1.\n\n"
| RBool _ ->
pr "This function returns a C truth value on success or -1 on error.\n\n"
| RConstString _ ->
pr "This function returns a string, or NULL on error.
The string is owned by the guest handle and must I<not> be freed.\n\n"
| RConstOptString _ ->
pr "This function returns a string which may be NULL.
There is no way to return an error from this function.
The string is owned by the guest handle and must I<not> be freed.\n\n"
| RString _ ->
pr "This function returns a string, or NULL on error.
I<The caller must free the returned string after use>.\n\n"
| RStringList _ ->
pr "This function returns a NULL-terminated array of strings
(like L<environ(3)>), or NULL if there was an error.
I<The caller must free the strings and the array after use>.\n\n"
| RStruct (_, typ) ->
pr "This function returns a C<struct guestfs_%s *>,
or NULL if there was an error.
I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
| RStructList (_, typ) ->
pr "This function returns a C<struct guestfs_%s_list *>,
or NULL if there was an error.
I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
| RHashtable _ ->
pr "This function returns a NULL-terminated array of
strings, or NULL if there was an error.
The array of strings will always have length C<2n+1>, where
C<n> keys and values alternate, followed by the trailing NULL entry.
I<The caller must free the strings and the array after use>.\n\n"
| RBufferOut _ ->
pr "This function returns a buffer, or NULL on error.
The size of the returned buffer is written to C<*size_r>.
I<The caller must free the returned buffer after use>.\n\n"
);
if f.progress then
pr "%s\n\n" progress_message;
if f.protocol_limit_warning then
pr "%s\n\n" protocol_limit_warning;
if List.exists (function String (Key, _) -> true | _ -> false) args then
pr "This function takes a key or passphrase parameter which
could contain sensitive material. Read the section
L</KEYS AND PASSPHRASES> for more information.\n\n";
(match f.optional with
| None -> ()
| Some opt ->
pr "This function depends on the feature C<%s>. See also
L</guestfs_feature_available>.\n\n" opt
);
(match version_added f with
| Some version -> pr "(Added in %s)\n\n" version
| None -> assert false
);
(* Handling of optional argument variants. *)
if optargs <> [] then (
pr "=head2 guestfs_%s_va\n\n" c_name;
generate_prototype ~extern:false ~indent:" " ~handle:"g"
~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
c_name style;
pr "\n\n";
pr "This is the \"va_list variant\" of L</guestfs_%s>.\n\n" c_name;
pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
pr "=head2 guestfs_%s_argv\n\n" c_name;
generate_prototype ~extern:false ~indent:" " ~handle:"g"
~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
c_name style;
pr "\n\n";
pr "This is the \"argv variant\" of L</guestfs_%s>.\n\n" c_name;
pr "See L</CALLS WITH OPTIONAL ARGUMENTS>.\n\n";
)
and generate_actions_pod_back_compat_entry ({ name;
style = ret, args, _ } as f) =
pr "=head2 guestfs_%s\n\n" name;
generate_prototype ~extern:false ~indent:" " ~handle:"g"
~prefix:"guestfs_" name (ret, args, []);
pr "\n\n";
pr "This function is provided for backwards compatibility\n";
pr "with earlier versions of libguestfs. It simply calls\n";
pr "L</guestfs_%s_opts> with no optional arguments.\n" name;
pr "\n";
(match version_added f with
| Some version -> pr "(Added in %s)\n\n\n" version
| None -> assert false
)
and generate_structs_pod () =
generate_header PODStyle GPLv2plus;
(* Structs documentation. *)
List.iter (
fun { s_name = typ; s_cols = cols } ->
pr "=head2 guestfs_%s\n" typ;
pr "\n";
pr " struct guestfs_%s {\n" typ;
List.iter (
function
| name, FChar -> pr " char %s;\n" name
| name, FUInt32 -> pr " uint32_t %s;\n" name
| name, FInt32 -> pr " int32_t %s;\n" name
| name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
| name, FInt64 -> pr " int64_t %s;\n" name
| name, FString -> pr " char *%s;\n" name
| name, FBuffer ->
pr " /* The next two fields describe a byte array. */\n";
pr " uint32_t %s_len;\n" name;
pr " char *%s;\n" name
| name, FUUID ->
pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
pr " char %s[32];\n" name
| name, FOptPercent ->
pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
pr " float %s;\n" name
) cols;
pr " };\n";
pr " \n";
pr " struct guestfs_%s_list {\n" typ;
pr " uint32_t len; /* Number of elements in list. */\n";
pr " struct guestfs_%s *val; /* Elements. */\n" typ;
pr " };\n";
pr "\n";
pr " int guestfs_compare_%s (const struct guestfs_%s *, const struct guestfs_%s *);\n"
typ typ typ;
pr " int guestfs_compare_%s_list (const struct guestfs_%s_list *, const struct guestfs_%s_list *);\n"
typ typ typ;
pr " \n";
pr " struct guestfs_%s *guestfs_copy_%s (const struct guestfs_%s *);\n"
typ typ typ;
pr " struct guestfs_%s_list *guestfs_copy_%s_list (const struct guestfs_%s_list *);\n"
typ typ typ;
pr " \n";
pr " void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
pr " void guestfs_free_%s_list (struct guestfs_%s_list *);\n"
typ typ;
pr "\n"
) structs
and generate_availability_pod () =
generate_header PODStyle GPLv2plus;
(* Availability documentation. *)
pr "=over 4\n";
pr "\n";
List.iter (
fun (group, fns) ->
pr "=item B<%s>\n" group;
pr "\n";
pr "The following functions:\n";
List.iter (pr "L</guestfs_%s>\n") (List.map (fun { name = n } -> n) fns);
pr "\n"
) optgroups;
pr "=back\n";
pr "\n"
(* Generate the guestfs.h file. *)
and generate_guestfs_h () =
generate_header CStyle LGPLv2plus;
pr "\
/* ---------- IMPORTANT NOTE ----------
*
* All API documentation is in the manpage, 'guestfs(3)'.
* To read it, type: man 3 guestfs
* Or read it online here: http://libguestfs.org/guestfs.3.html
*
* Go and read it now! This header file won't make much sense.
*
* For example code using the C API, see 'guestfs-examples(3)'
* available online at: http://libguestfs.org/guestfs-examples.3.html
*
* ------------------------------------
*/
#ifndef GUESTFS_H_
#define GUESTFS_H_
#ifdef __cplusplus
extern \"C\" {
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdarg.h>
#if defined(__GNUC__) && !defined(GUESTFS_GCC_VERSION)
# define GUESTFS_GCC_VERSION \\
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif
/* Define GUESTFS_NO_WARN_DEPRECATED to not warn about deprecated API functions. */
#define GUESTFS_DEPRECATED_NO_REPLACEMENT
#define GUESTFS_DEPRECATED_REPLACED_BY(s)
#ifndef GUESTFS_NO_WARN_DEPRECATED
# if defined(__GNUC__) && GUESTFS_GCC_VERSION >= 40500 /* gcc >= 4.5 */
# undef GUESTFS_DEPRECATED_NO_REPLACEMENT
# undef GUESTFS_DEPRECATED_REPLACED_BY
# define GUESTFS_DEPRECATED_NO_REPLACEMENT __attribute__((__deprecated__))
# define GUESTFS_DEPRECATED_REPLACED_BY(s) __attribute__((__deprecated__(\"change the program to use guestfs_\" s \" instead of this deprecated function\")))
# endif
#endif /* !GUESTFS_NO_WARN_DEPRECATED */
#if defined(__GNUC__) && GUESTFS_GCC_VERSION >= 40000 /* gcc >= 4.0 */
# define GUESTFS_DLL_PUBLIC __attribute__((visibility (\"default\")))
#else
# define GUESTFS_DLL_PUBLIC
#endif
#if defined(__GNUC__) && GUESTFS_GCC_VERSION >= 30100 /* gcc >= 3.1 */
# define GUESTFS_NORETURN __attribute__((noreturn))
#else
# define GUESTFS_NORETURN
#endif
/* The handle. */
#ifndef GUESTFS_TYPEDEF_H
#define GUESTFS_TYPEDEF_H 1
typedef struct guestfs_h guestfs_h;
#endif
/* Connection management. */
extern GUESTFS_DLL_PUBLIC guestfs_h *guestfs_create (void);
#define GUESTFS_HAVE_CREATE_FLAGS 1
extern GUESTFS_DLL_PUBLIC guestfs_h *guestfs_create_flags (unsigned flags, ...);
#define GUESTFS_CREATE_NO_ENVIRONMENT (1 << 0)
#define GUESTFS_CREATE_NO_CLOSE_ON_EXIT (1 << 1)
extern GUESTFS_DLL_PUBLIC void guestfs_close (guestfs_h *g);
/* Error handling. */
extern GUESTFS_DLL_PUBLIC const char *guestfs_last_error (guestfs_h *g);
#define GUESTFS_HAVE_LAST_ERRNO 1
extern GUESTFS_DLL_PUBLIC int guestfs_last_errno (guestfs_h *g);
#ifndef GUESTFS_TYPEDEF_ERROR_HANDLER_CB
#define GUESTFS_TYPEDEF_ERROR_HANDLER_CB 1
typedef void (*guestfs_error_handler_cb) (guestfs_h *g, void *opaque, const char *msg);
#endif
#ifndef GUESTFS_TYPEDEF_ABORT_CB
#define GUESTFS_TYPEDEF_ABORT_CB 1
typedef void (*guestfs_abort_cb) (void) GUESTFS_NORETURN;
#endif
extern GUESTFS_DLL_PUBLIC void guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *opaque);
extern GUESTFS_DLL_PUBLIC guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *g, void **opaque_rtn);
#define GUESTFS_HAVE_PUSH_ERROR_HANDLER 1
extern GUESTFS_DLL_PUBLIC void guestfs_push_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *opaque);
#define GUESTFS_HAVE_POP_ERROR_HANDLER 1
extern GUESTFS_DLL_PUBLIC void guestfs_pop_error_handler (guestfs_h *g);
extern GUESTFS_DLL_PUBLIC void guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb);
extern GUESTFS_DLL_PUBLIC guestfs_abort_cb guestfs_get_out_of_memory_handler (guestfs_h *g);
/* Events. */
";
List.iter (
fun (name, bitmask) ->
pr "#define GUESTFS_EVENT_%-16s 0x%04x\n"
(String.uppercase_ascii name) bitmask
) events;
pr "#define GUESTFS_EVENT_%-16s 0x%04x\n" "ALL" all_events_bitmask;
pr "\n";
pr "\
#ifndef GUESTFS_TYPEDEF_EVENT_CALLBACK
#define GUESTFS_TYPEDEF_EVENT_CALLBACK 1
typedef void (*guestfs_event_callback) (
guestfs_h *g,
void *opaque,
uint64_t event,
int event_handle,
int flags,
const char *buf, size_t buf_len,
const uint64_t *array, size_t array_len);
#endif
#define GUESTFS_HAVE_SET_EVENT_CALLBACK 1
extern GUESTFS_DLL_PUBLIC int guestfs_set_event_callback (guestfs_h *g, guestfs_event_callback cb, uint64_t event_bitmask, int flags, void *opaque);
#define GUESTFS_HAVE_DELETE_EVENT_CALLBACK 1
extern GUESTFS_DLL_PUBLIC void guestfs_delete_event_callback (guestfs_h *g, int event_handle);
#define GUESTFS_HAVE_EVENT_TO_STRING 1
extern GUESTFS_DLL_PUBLIC char *guestfs_event_to_string (uint64_t event);
/* Old-style event handling. */
#ifndef GUESTFS_TYPEDEF_LOG_MESSAGE_CB
#define GUESTFS_TYPEDEF_LOG_MESSAGE_CB 1
typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque, char *buf, int len);
#endif
#ifndef GUESTFS_TYPEDEF_SUBPROCESS_QUIT_CB
#define GUESTFS_TYPEDEF_SUBPROCESS_QUIT_CB 1
typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
#endif
#ifndef GUESTFS_TYPEDEF_LAUNCH_DONE_CB
#define GUESTFS_TYPEDEF_LAUNCH_DONE_CB 1
typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
#endif
#ifndef GUESTFS_TYPEDEF_CLOSE_CB
#define GUESTFS_TYPEDEF_CLOSE_CB 1
typedef void (*guestfs_close_cb) (guestfs_h *g, void *opaque);
#endif
#ifndef GUESTFS_TYPEDEF_PROGRESS_CB
#define GUESTFS_TYPEDEF_PROGRESS_CB 1
typedef void (*guestfs_progress_cb) (guestfs_h *g, void *opaque, int proc_nr, int serial, uint64_t position, uint64_t total);
#endif
#ifndef GUESTFS_NO_DEPRECATED
extern GUESTFS_DLL_PUBLIC void guestfs_set_log_message_callback (guestfs_h *g, guestfs_log_message_cb cb, void *opaque)
GUESTFS_DEPRECATED_REPLACED_BY(\"set_event_callback\");
extern GUESTFS_DLL_PUBLIC void guestfs_set_subprocess_quit_callback (guestfs_h *g, guestfs_subprocess_quit_cb cb, void *opaque)
GUESTFS_DEPRECATED_REPLACED_BY(\"set_event_callback\");
extern GUESTFS_DLL_PUBLIC void guestfs_set_launch_done_callback (guestfs_h *g, guestfs_launch_done_cb cb, void *opaque)
GUESTFS_DEPRECATED_REPLACED_BY(\"set_event_callback\");
#define GUESTFS_HAVE_SET_CLOSE_CALLBACK 1
extern GUESTFS_DLL_PUBLIC void guestfs_set_close_callback (guestfs_h *g, guestfs_close_cb cb, void *opaque)
GUESTFS_DEPRECATED_REPLACED_BY(\"set_event_callback\");
#define GUESTFS_HAVE_SET_PROGRESS_CALLBACK 1
extern GUESTFS_DLL_PUBLIC void guestfs_set_progress_callback (guestfs_h *g, guestfs_progress_cb cb, void *opaque)
GUESTFS_DEPRECATED_REPLACED_BY(\"set_event_callback\");
#endif /* !GUESTFS_NO_DEPRECATED */
/* Private data area. */
#define GUESTFS_HAVE_SET_PRIVATE 1
extern GUESTFS_DLL_PUBLIC void guestfs_set_private (guestfs_h *g, const char *key, void *data);
#define GUESTFS_HAVE_GET_PRIVATE 1
extern GUESTFS_DLL_PUBLIC void *guestfs_get_private (guestfs_h *g, const char *key);
#define GUESTFS_HAVE_FIRST_PRIVATE 1
extern GUESTFS_DLL_PUBLIC void *guestfs_first_private (guestfs_h *g, const char **key_rtn);
#define GUESTFS_HAVE_NEXT_PRIVATE 1
extern GUESTFS_DLL_PUBLIC void *guestfs_next_private (guestfs_h *g, const char **key_rtn);
/* Structures. */
";
(* The structures are carefully written to have exactly the same
* in-memory format as the XDR structures that we use on the wire to
* the daemon. The reason for creating copies of these structures
* here is just so we don't have to export the whole of
* guestfs_protocol.h (which includes much unrelated and
* XDR-dependent stuff that we don't want to be public, or required
* by clients).
*
* To reiterate, we will pass these structures to and from the client
* with a simple assignment or memcpy, so the format must be
* identical to what rpcgen / the RFC defines.
*)
(* Public structures. *)
let generate_all_structs = List.iter (
fun { s_name = typ; s_cols = cols } ->
pr "#define GUESTFS_HAVE_STRUCT_%s 1\n" (String.uppercase_ascii typ);
pr "\n";
pr "struct guestfs_%s {\n" typ;
List.iter (
function
| name, FChar -> pr " char %s;\n" name
| name, FString -> pr " char *%s;\n" name
| name, FBuffer ->
pr " uint32_t %s_len;\n" name;
pr " char *%s;\n" name
| name, FUUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
| name, FUInt32 -> pr " uint32_t %s;\n" name
| name, FInt32 -> pr " int32_t %s;\n" name
| name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
| name, FInt64 -> pr " int64_t %s;\n" name
| name, FOptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
) cols;
pr "};\n";
pr "\n";
pr "struct guestfs_%s_list {\n" typ;
pr " uint32_t len;\n";
pr " struct guestfs_%s *val;\n" typ;
pr "};\n";
pr "\n";
pr "extern GUESTFS_DLL_PUBLIC int guestfs_compare_%s (const struct guestfs_%s *, const struct guestfs_%s *);\n" typ typ typ;
pr "extern GUESTFS_DLL_PUBLIC int guestfs_compare_%s_list (const struct guestfs_%s_list *, const struct guestfs_%s_list *);\n" typ typ typ;
pr "\n";
pr "extern GUESTFS_DLL_PUBLIC struct guestfs_%s *guestfs_copy_%s (const struct guestfs_%s *);\n" typ typ typ;
pr "extern GUESTFS_DLL_PUBLIC struct guestfs_%s_list *guestfs_copy_%s_list (const struct guestfs_%s_list *);\n" typ typ typ;
pr "\n";
pr "extern GUESTFS_DLL_PUBLIC void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
pr "extern GUESTFS_DLL_PUBLIC void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
pr "\n"
) in
generate_all_structs external_structs;
pr "\
/* Actions. */
";
let generate_action_header { name = shortname;
style = ret, args, optargs as style;
deprecated_by } =
pr "#define GUESTFS_HAVE_%s 1\n" (String.uppercase_ascii shortname);
if optargs <> [] then (
List.iteri (
fun i argt ->
let uc_shortname = String.uppercase_ascii shortname in
let n = name_of_optargt argt in
let uc_n = String.uppercase_ascii n in
pr "#define GUESTFS_%s_%s %d\n" uc_shortname uc_n i;
) optargs;
);
let deprecated =
match deprecated_by with
| Not_deprecated -> false
| Replaced_by _ | Deprecated_no_replacement -> true in
if deprecated then
pr "#ifndef GUESTFS_NO_DEPRECATED\n";
generate_prototype ~single_line:true ~semicolon:false ~dll_public:true
~handle:"g" ~prefix:"guestfs_" shortname style;
(match deprecated_by with
| Not_deprecated ->
pr ";\n"
| Replaced_by fn ->
pr "\n GUESTFS_DEPRECATED_REPLACED_BY (%S);\n" fn
| Deprecated_no_replacement ->
pr "\n GUESTFS_DEPRECATED_NO_REPLACEMENT;\n"
);
if deprecated then
pr "#endif /* !GUESTFS_NO_DEPRECATED */\n";
if optargs <> [] then (
generate_prototype ~single_line:true ~newline:true ~handle:"g"
~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
~dll_public:true
shortname style;
pr "\n";
pr "struct guestfs_%s_argv {\n" shortname;
pr " uint64_t bitmask;\n";
List.iteri (
fun i argt ->
let c_type =
match argt with
| OBool n -> "int "
| OInt n -> "int "
| OInt64 n -> "int64_t "
| OString n -> "const char *"
| OStringList n -> "char *const *" in
let uc_shortname = String.uppercase_ascii shortname in
let n = name_of_optargt argt in
let uc_n = String.uppercase_ascii n in
pr "# define GUESTFS_%s_%s_BITMASK (UINT64_C(1)<<%d)\n" uc_shortname uc_n i;
pr " %s%s;\n" c_type n
) optargs;
pr "};\n";
pr "\n";
generate_prototype ~single_line:true ~newline:true ~handle:"g"
~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
~dll_public:true
shortname style;
);
pr "\n"
in
let generate_all_headers = List.iter (
fun ({ name; style = ret, args, _ } as f) ->
(* If once_had_no_optargs is set, then we need to generate a
* <name>_opts variant, plus a backwards-compatible wrapper
* called just <name> with no optargs.
*)
if f.once_had_no_optargs then (
generate_action_header { f with style = ret, args, [] };
generate_action_header { f with name = f.name ^ "_opts" };
)
else
generate_action_header f
) in
generate_all_headers public_functions_sorted;
pr "\
#if GUESTFS_PRIVATE
/* Symbols protected by GUESTFS_PRIVATE are NOT part of the public,
* stable API, and can change at any time! We export them because
* they are used by some of the language bindings.
*/
/* Private functions. */
";
generate_all_headers private_functions_sorted;
pr "\
/* Private structures. */
";
generate_all_structs internal_structs;
pr "\
#endif /* End of GUESTFS_PRIVATE. */
/* Deprecated macros. Use GUESTFS_HAVE_* instead. */
#define LIBGUESTFS_HAVE_CREATE_FLAGS 1
#define LIBGUESTFS_HAVE_LAST_ERRNO 1
#define LIBGUESTFS_HAVE_PUSH_ERROR_HANDLER 1
#define LIBGUESTFS_HAVE_POP_ERROR_HANDLER 1
#define LIBGUESTFS_HAVE_SET_EVENT_CALLBACK 1
#define LIBGUESTFS_HAVE_DELETE_EVENT_CALLBACK 1
#define LIBGUESTFS_HAVE_SET_CLOSE_CALLBACK 1
#define LIBGUESTFS_HAVE_SET_PROGRESS_CALLBACK 1
#define LIBGUESTFS_HAVE_SET_PRIVATE 1
#define LIBGUESTFS_HAVE_GET_PRIVATE 1
#define LIBGUESTFS_HAVE_FIRST_PRIVATE 1
#define LIBGUESTFS_HAVE_NEXT_PRIVATE 1
";
List.iter (
fun { name = shortname } ->
pr "#define LIBGUESTFS_HAVE_%s 1\n" (String.uppercase_ascii shortname);
) public_functions_sorted;
pr "
/* End of deprecated macros. */
#ifdef __cplusplus
}
#endif
#endif /* GUESTFS_H_ */
"
(* Generate the guestfs-internal-actions.h file. *)
and generate_internal_actions_h () =
generate_header CStyle LGPLv2plus;
pr "#ifndef GUESTFS_INTERNAL_ACTIONS_H_\n";
pr "#define GUESTFS_INTERNAL_ACTIONS_H_\n";
pr "\n";
List.iter (
fun { c_name; style } ->
generate_prototype ~single_line:true ~newline:true ~handle:"g"
~prefix:"guestfs_impl_" ~optarg_proto:Argv
c_name style
) (actions |> non_daemon_functions |> sort);
pr "\n";
pr "#endif /* GUESTFS_INTERNAL_ACTIONS_H_ */\n"
(* Generate structs-cleanups.h file. *)
and generate_client_structs_cleanups_h () =
generate_header CStyle LGPLv2plus;
pr "\
/* These CLEANUP_* macros automatically free the struct or struct list
* pointed to by the local variable at the end of the current scope.
*/
#ifndef GUESTFS_STRUCTS_CLEANUPS_H_
#define GUESTFS_STRUCTS_CLEANUPS_H_
#ifdef HAVE_ATTRIBUTE_CLEANUP
";
List.iter (
fun { s_name = name } ->
pr "#define CLEANUP_FREE_%s \\\n" (String.uppercase_ascii name);
pr " __attribute__((cleanup(guestfs_int_cleanup_free_%s)))\n" name;
pr "#define CLEANUP_FREE_%s_LIST \\\n" (String.uppercase_ascii name);
pr " __attribute__((cleanup(guestfs_int_cleanup_free_%s_list)))\n" name
) structs;
pr "#else /* !HAVE_ATTRIBUTE_CLEANUP */\n";
List.iter (
fun { s_name = name } ->
pr "#define CLEANUP_FREE_%s\n" (String.uppercase_ascii name);
pr "#define CLEANUP_FREE_%s_LIST\n" (String.uppercase_ascii name)
) structs;
pr "\
#endif /* !HAVE_ATTRIBUTE_CLEANUP */
/* These functions are used internally by the CLEANUP_* macros.
* Don't call them directly.
*/
";
List.iter (
fun { s_name = name } ->
pr "extern void guestfs_int_cleanup_free_%s (void *ptr);\n"
name;
pr "extern void guestfs_int_cleanup_free_%s_list (void *ptr);\n"
name
) structs;
pr "\n";
pr "#endif /* GUESTFS_STRUCTS_CLEANUPS_H_ */\n"
(* Functions to free structures. *)
and generate_client_structs_free () =
generate_header CStyle LGPLv2plus;
pr "\
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include \"guestfs.h\"
#include \"guestfs-internal.h\"
#include \"guestfs_protocol.h\"
";
pr "/* Structure-freeing functions. These rely on the fact that the\n";
pr " * structure format is identical to the XDR format. See note in\n";
pr " * generator.ml.\n";
pr " */\n";
pr "\n";
List.iter (
fun { s_name = typ } ->
pr "GUESTFS_DLL_PUBLIC void\n";
pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
pr "{\n";
pr " if (x) {\n";
pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
pr " free (x);\n";
pr " }\n";
pr "}\n";
pr "\n";
pr "GUESTFS_DLL_PUBLIC void\n";
pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
pr "{\n";
pr " if (x) {\n";
pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n"
typ;
pr " free (x);\n";
pr " }\n";
pr "}\n";
pr "\n";
) structs
(* Functions to compare structures. *)
and generate_client_structs_compare () =
generate_header CStyle LGPLv2plus;
pr "\
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include \"guestfs.h\"
#include \"guestfs-internal.h\"
";
pr "/* Structure-comparison functions. */\n";
List.iter (
fun { s_name = typ; s_cols = cols } ->
let has_nonnumeric_cols =
let nonnumeric = function
| _,(FString|FUUID|FBuffer) -> true
| _,(FChar|FUInt32|FInt32|FUInt64|FBytes|FInt64|FOptPercent) -> false
in
List.exists nonnumeric cols in
pr "\n";
pr "GUESTFS_DLL_PUBLIC int\n";
pr "guestfs_compare_%s (const struct guestfs_%s *s1, const struct guestfs_%s *s2)\n"
typ typ typ;
pr "{\n";
if has_nonnumeric_cols then (
pr " int r;\n";
pr "\n";
);
List.iter (
function
| name, FString ->
pr " r = strcmp (s1->%s, s2->%s);\n" name name;
pr " if (r != 0) return r;\n"
| name, FBuffer ->
pr " if (s1->%s_len < s2->%s_len) return -1;\n" name name;
pr " else if (s1->%s_len > s2->%s_len) return 1;\n" name name;
pr " else {\n";
pr " r = memcmp (s1->%s, s2->%s, s1->%s_len);\n" name name name;
pr " if (r != 0) return r;\n";
pr " }\n"
| name, FUUID ->
pr " r = memcmp (s1->%s, s2->%s, 32 * sizeof (char));\n" name name;
pr " if (r != 0) return r;\n"
| name, FChar
| name, FUInt32
| name, FInt32
| name, FUInt64
| name, FBytes
| name, FInt64
| name, FOptPercent ->
pr " if (s1->%s < s2->%s) return -1;\n" name name;
pr " else if (s1->%s > s2->%s) return 1;\n" name name
) cols;
pr " return 0;\n";
pr "}\n";
pr "\n";
pr "GUESTFS_DLL_PUBLIC int\n";
pr "guestfs_compare_%s_list (const struct guestfs_%s_list *s1, const struct guestfs_%s_list *s2)\n"
typ typ typ;
pr "{\n";
pr " if (s1->len < s2->len) return -1;\n";
pr " else if (s1->len > s2->len) return 1;\n";
pr " else {\n";
pr " size_t i;\n";
pr " int r;\n";
pr "\n";
pr " for (i = 0; i < s1->len; ++i) {\n";
pr " r = guestfs_compare_%s (&s1->val[i], &s2->val[i]);\n" typ;
pr " if (r != 0) return r;\n";
pr " }\n";
pr " return 0;\n";
pr " }\n";
pr "}\n"
) structs
(* Functions to copy structures. *)
and generate_client_structs_copy () =
generate_header CStyle LGPLv2plus;
pr "\
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include \"guestfs.h\"
#include \"guestfs-internal.h\"
";
pr "/* Structure-copying functions. */\n";
List.iter (
fun { s_name = typ; s_cols = cols } ->
let has_boxed_cols =
let boxed = function
| _,(FString|FBuffer) -> true
| _,(FChar|FUUID|FUInt32|FInt32|FUInt64|FBytes|FInt64|FOptPercent) ->
false
in
List.exists boxed cols in
if has_boxed_cols then (
pr "\n";
pr "static void\n";
pr "free_%s (struct guestfs_%s *s)\n" typ typ;
pr "{\n";
List.iter (
function
| name, FString
| name, FBuffer -> pr " free (s->%s);\n" name
| _, FChar
| _, FUUID
| _, FUInt32
| _, FInt32
| _, FUInt64
| _, FBytes
| _, FInt64
| _, FOptPercent -> ()
) cols;
pr "}\n"
);
pr "\n";
if has_boxed_cols then (
pr "static int\n";
pr "copy_%s (const struct guestfs_%s *inp, struct guestfs_%s *out)\n"
typ typ typ;
pr "{\n";
pr " int err;\n";
pr "\n";
List.iter (
function
| name, FString
| name, FBuffer -> pr " out->%s = NULL;\n" name
| _, FChar
| _, FUUID
| _, FUInt32
| _, FInt32
| _, FUInt64
| _, FBytes
| _, FInt64
| _, FOptPercent -> ()
) cols;
List.iter (
function
| name, FString ->
pr " out->%s = strdup (inp->%s);\n" name name;
pr " if (out->%s == NULL) goto error;\n" name
| name, FBuffer ->
pr " /* This adds NUL-termination, which is not strictly required\n";
pr " * but avoids a common bug in calling code. Note that callers\n";
pr " * should NOT depend on this behaviour intentionally.\n";
pr " */\n";
pr " out->%s_len = inp->%s_len;\n" name name;
pr " out->%s = malloc (out->%s_len + 1);\n" name name;
pr " if (out->%s == NULL) goto error;\n" name;
pr " memcpy (out->%s, inp->%s, out->%s_len);\n" name name name;
pr " out->%s[out->%s_len] = '\\0';\n" name name
| name, FUUID ->
pr " memcpy (out->%s, inp->%s, 32 * sizeof (char));\n" name name;
| name, FChar
| name, FUInt32
| name, FInt32
| name, FUInt64
| name, FBytes
| name, FInt64
| name, FOptPercent ->
pr " out->%s = inp->%s;\n" name name;
) cols;
pr " return 0;\n";
pr "\n";
pr "error: ;\n";
pr " err = errno;\n";
pr " free_%s (out);\n" typ;
pr " errno = err;\n";
pr " return -1;\n";
pr "}\n";
)
else (
(* If a struct has no boxed columns, then we can just do a memcpy. *)
pr "static void\n";
pr "copy_%s (const struct guestfs_%s *inp, struct guestfs_%s *out)\n"
typ typ typ;
pr "{\n";
pr " memcpy (out, inp, sizeof *out);\n";
pr "}\n";
);
pr "\n";
pr "GUESTFS_DLL_PUBLIC struct guestfs_%s *\n" typ;
pr "guestfs_copy_%s (const struct guestfs_%s *inp)\n" typ typ;
pr "{\n";
pr " struct guestfs_%s *ret;\n" typ;
pr "\n";
pr " ret = malloc (sizeof *ret);\n";
pr " if (ret == NULL)\n";
pr " return NULL;\n";
pr "\n";
if has_boxed_cols then (
pr " if (copy_%s (inp, ret) == -1) {\n" typ;
pr " int err;\n";
pr "\n";
pr " err = errno;\n";
pr " free (ret);\n";
pr " errno = err;\n";
pr " return NULL;\n";
pr " }\n"
) else (
pr " copy_%s (inp, ret);\n" typ
);
pr "\n";
pr " return ret;\n";
pr "}\n";
pr "\n";
pr "GUESTFS_DLL_PUBLIC struct guestfs_%s_list *\n" typ;
pr "guestfs_copy_%s_list (const struct guestfs_%s_list *inp)\n" typ typ;
pr "{\n";
pr " int err;\n";
pr " struct guestfs_%s_list *ret;\n" typ;
pr " size_t i = 0;\n";
if has_boxed_cols then
pr " size_t j;\n";
pr "\n";
pr " ret = malloc (sizeof *ret);\n";
pr " if (ret == NULL)\n";
pr " return NULL;\n";
pr "\n";
pr " ret->len = inp->len;\n";
pr " ret->val = malloc (sizeof (struct guestfs_%s) * ret->len);\n" typ;
pr " if (ret->val == NULL)\n";
pr " goto error;\n";
pr "\n";
pr " for (i = 0; i < ret->len; ++i) {\n";
if has_boxed_cols then (
pr " if (copy_%s (&inp->val[i], &ret->val[i]) == -1)\n" typ;
pr " goto error;\n"
) else (
pr " copy_%s (&inp->val[i], &ret->val[i]);\n" typ
);
pr " }\n";
pr "\n";
pr " return ret;\n";
pr "\n";
pr "error: ;\n";
pr " err = errno;\n";
if has_boxed_cols then (
pr " for (j = 0; j < i; ++j)\n";
pr " free_%s (&ret->val[j]);\n" typ
);
pr " free (ret->val);\n";
pr " free (ret);\n";
pr " errno = err;\n";
pr " return NULL;\n";
pr "}\n";
) structs
(* Functions to free structures used by the CLEANUP_* macros. *)
and generate_client_structs_cleanups_c () =
generate_header CStyle LGPLv2plus;
pr "\
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include \"guestfs.h\"
#include \"structs-cleanups.h\"
";
pr "/* Cleanup functions used by CLEANUP_* macros. Do not call\n";
pr " * these functions directly.\n";
pr " */\n";
pr "\n";
List.iter (
fun { s_name = typ } ->
pr "void\n";
pr "guestfs_int_cleanup_free_%s (void *ptr)\n" typ;
pr "{\n";
pr " guestfs_free_%s (* (struct guestfs_%s **) ptr);\n" typ typ;
pr "}\n";
pr "\n";
pr "void\n";
pr "guestfs_int_cleanup_free_%s_list (void *ptr)\n" typ;
pr "{\n";
pr " guestfs_free_%s_list (* (struct guestfs_%s_list **) ptr);\n"
typ typ;
pr "}\n";
pr "\n";
) structs
(* Generate structs-print.c file. *)
and generate_client_structs_print_c () =
generate_header CStyle LGPLv2plus;
pr "\
#include <config.h>
#include <inttypes.h>
#include \"c-ctype.h\"
#include \"guestfs.h\"
#include \"structs-print.h\"
";
let write_structs =
List.iter (
fun { s_name = typ; s_cols = cols } ->
let needs_i =
List.exists (function (_, (FUUID|FBuffer)) -> true | _ -> false) cols in
pr "void\n";
pr "guestfs_int_print_%s_indent (struct guestfs_%s *%s, FILE *dest, const char *linesep, const char *indent)\n"
typ typ typ;
pr "{\n";
if needs_i then (
pr " size_t i;\n";
pr "\n"
);
List.iter (
function
| name, FString ->
pr " fprintf (dest, \"%%s%s: %%s%%s\", indent, %s->%s, linesep);\n"
name typ name
| name, FUUID ->
pr " fprintf (dest, \"%%s%s: \", indent);\n" name;
pr " for (i = 0; i < 32; ++i)\n";
pr " fprintf (dest, \"%%c\", %s->%s[i]);\n" typ name;
pr " fprintf (dest, \"%%s\", linesep);\n"
| name, FBuffer ->
pr " fprintf (dest, \"%%s%s: \", indent);\n" name;
pr " for (i = 0; i < %s->%s_len; ++i)\n" typ name;
pr " if (c_isprint (%s->%s[i]))\n" typ name;
pr " fprintf (dest, \"%%c\", %s->%s[i]);\n" typ name;
pr " else\n";
pr " fprintf (dest, \"\\\\x%%02x\", (unsigned) %s->%s[i]);\n"
typ name;
pr " fprintf (dest, \"%%s\", linesep);\n"
| name, (FUInt64|FBytes) ->
pr " fprintf (dest, \"%%s%s: %%\" PRIu64 \"%%s\", indent, %s->%s, linesep);\n"
name typ name
| name, FInt64 ->
pr " fprintf (dest, \"%%s%s: %%\" PRIi64 \"%%s\", indent, %s->%s, linesep);\n"
name typ name
| name, FUInt32 ->
pr " fprintf (dest, \"%%s%s: %%\" PRIu32 \"%%s\", indent, %s->%s, linesep);\n"
name typ name
| name, FInt32 ->
pr " fprintf (dest, \"%%s%s: %%\" PRIi32 \"%%s\", indent, %s->%s, linesep);\n"
name typ name
| name, FChar ->
pr " fprintf (dest, \"%%s%s: %%c%%s\", indent, %s->%s, linesep);\n"
name typ name
| name, FOptPercent ->
pr " if (%s->%s >= 0)\n" typ name;
pr " fprintf (dest, \"%%s%s: %%g %%%%%%s\", indent, (double) %s->%s, linesep);\n"
name typ name;
pr " else\n";
pr " fprintf (dest, \"%%s%s: %%s\", indent, linesep);\n" name
) cols;
pr "}\n";
pr "\n";
) in
write_structs external_structs;
pr "\
#if GUESTFS_PRIVATE
";
write_structs internal_structs;
pr "\
#endif /* End of GUESTFS_PRIVATE. */
"
(* Generate structs-print.h file. *)
and generate_client_structs_print_h () =
generate_header CStyle LGPLv2plus;
pr "\
#ifndef GUESTFS_INTERNAL_STRUCTS_PRINT_H_
#define GUESTFS_INTERNAL_STRUCTS_PRINT_H_
#include <stdio.h>
";
let write_structs =
List.iter (
fun { s_name = name } ->
pr "extern void guestfs_int_print_%s_indent (struct guestfs_%s *%s, FILE *dest, const char *linesep, const char *indent);\n"
name name name
) in
write_structs external_structs;
pr "\
#if GUESTFS_PRIVATE
";
write_structs internal_structs;
pr "\
#endif /* End of GUESTFS_PRIVATE. */
#endif /* GUESTFS_INTERNAL_STRUCTS_PRINT_H_ */
"
(* Generate the client-side dispatch stubs. *)
and generate_client_actions actions () =
generate_header CStyle LGPLv2plus;
pr "\
#include <config.h>
/* It is safe to call deprecated functions from this file. */
#undef GUESTFS_NO_DEPRECATED
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include \"guestfs.h\"
#include \"guestfs-internal.h\"
#include \"guestfs-internal-actions.h\"
#include \"guestfs_protocol.h\"
#include \"errnostring.h\"
#include \"structs-print.h\"
";
(* Generate code for enter events. *)
let enter_event shortname =
pr " guestfs_int_call_callbacks_message (g, GUESTFS_EVENT_ENTER,\n";
pr " \"%s\", %d);\n"
shortname (String.length shortname)
in
(* Generate code to check String-like parameters are not passed in
* as NULL (returning an error if they are).
*)
let check_null_strings c_name (ret, args, optargs) =
let pr_newline = ref false in
List.iter (
function
(* parameters which should not be NULL *)
| String (_, n)
| BufferIn n
| StringList (_, n)
| Pointer (_, n) ->
pr " if (%s == NULL) {\n" n;
pr " error (g, \"%%s: %%s: parameter cannot be NULL\",\n";
pr " \"%s\", \"%s\");\n" c_name n;
let errcode =
match errcode_of_ret ret with
| `CannotReturnError ->
(* XXX hack *)
if c_name = "internal_test_rconstoptstring" then
`ErrorIsNULL
else
failwithf
"%s: RConstOptString function has invalid parameter '%s'"
c_name n
| (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr_newline := true
(* can be NULL *)
| OptString _
(* not applicable *)
| Bool _
| Int _
| Int64 _ -> ()
) args;
(* For optional arguments. *)
List.iter (
function
| OString n ->
pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK) &&\n"
(String.uppercase_ascii c_name) (String.uppercase_ascii n);
pr " optargs->%s == NULL) {\n" n;
pr " error (g, \"%%s: %%s: optional parameter cannot be NULL\",\n";
pr " \"%s\", \"%s\");\n" c_name n;
let errcode =
match errcode_of_ret ret with
| `CannotReturnError -> assert false
| (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr_newline := true
| OStringList n ->
pr " if ((optargs->bitmask & GUESTFS_%s_%s_BITMASK) &&\n"
(String.uppercase_ascii c_name) (String.uppercase_ascii n);
pr " optargs->%s == NULL) {\n" n;
pr " error (g, \"%%s: %%s: optional list cannot be NULL\",\n";
pr " \"%s\", \"%s\");\n" c_name n;
let errcode =
match errcode_of_ret ret with
| `CannotReturnError -> assert false
| (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr_newline := true
(* not applicable *)
| OBool _ | OInt _ | OInt64 _ -> ()
) optargs;
if !pr_newline then pr "\n";
in
(* Generate code to reject optargs we don't know about. *)
let reject_unknown_optargs c_name = function
| _, _, [] -> ()
| ret, _, optargs ->
let len = List.length optargs in
let mask = Int64.lognot (Int64.pred (Int64.shift_left 1L len)) in
pr " if (optargs->bitmask & UINT64_C(0x%Lx)) {\n" mask;
pr " error (g, \"%%s: unknown option in guestfs_%%s_argv->bitmask (this can happen if a program is compiled against a newer version of libguestfs, then dynamically linked to an older version)\",\n";
pr " \"%s\", \"%s\");\n" c_name c_name;
let errcode =
match errcode_of_ret ret with
| `CannotReturnError -> assert false
| (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr "\n";
in
(* Generate code to check for parameter validation (where supported
* for the type).
*)
let check_args_validity c_name (ret, args, optargs) =
let pr_newline = ref false in
List.iter (
function
| String (GUID, n) ->
pr " if (!guestfs_int_validate_guid (%s)) {\n" n;
pr " error (g, \"%%s: %%s: parameter is not a valid GUID\",\n";
pr " \"%s\", \"%s\");\n" c_name n;
let errcode =
match errcode_of_ret ret with
| `CannotReturnError -> assert false
| (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr_newline := true
| StringList (Filename, n) ->
pr " {\n";
pr " size_t i;\n";
pr " for (i = 0; %s[i] != NULL; ++i) {\n" n;
pr " if (strchr (%s[i], '/') != NULL) {\n" n;
pr " error (g, \"%%s: %%s: '%%s' is not a file name\",\n";
pr " \"%s\", \"%s\", %s[i]);\n" c_name n n;
let errcode =
match errcode_of_ret ret with
| `CannotReturnError -> assert false
| (`ErrorIsMinusOne |`ErrorIsNULL) as e -> e in
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr " }\n";
pr " }\n";
pr_newline := true
(* not applicable *)
| String ((PlainString|Device|Mountable|Pathname|
Dev_or_Path|Mountable_or_Path|
FileIn|FileOut|Key|Filename), _)
| BufferIn _
| StringList _
| Pointer (_, _)
| OptString _
| Bool _
| Int _
| Int64 _ -> ()
) args;
if !pr_newline then pr "\n";
in
(* Generate code to generate guestfish call traces. *)
let trace_call name c_name (ret, args, optargs) =
pr " if (trace_flag) {\n";
let needs_i =
List.exists (function
| StringList _ -> true
| _ -> false) args ||
List.exists (function
| OStringList _ -> true
| _ -> false) optargs in
if needs_i then (
pr " size_t i;\n";
pr "\n"
);
pr " guestfs_int_trace_open (&trace_buffer);\n";
pr " fprintf (trace_buffer.fp, \"%%s\", \"%s\");\n" name;
(* Required arguments. *)
List.iter (
function
| String ((PlainString|Device|Mountable|Pathname|Filename
|Dev_or_Path|Mountable_or_Path|FileIn|FileOut|GUID), n) ->
(* guestfish doesn't support string escaping, so neither do we *)
pr " fprintf (trace_buffer.fp, \" \\\"%%s\\\"\", %s);\n" n
| String (Key, n) ->
(* don't print keys *)
pr " fprintf (trace_buffer.fp, \" \\\"***\\\"\");\n"
| OptString n -> (* string option *)
pr " if (%s)\n" n;
pr " fprintf (trace_buffer.fp, \" \\\"%%s\\\"\", %s);\n" n;
pr " else\n";
pr " fprintf (trace_buffer.fp, \" null\");\n"
| StringList (_, n) ->
pr " fputc (' ', trace_buffer.fp);\n";
pr " fputc ('\"', trace_buffer.fp);\n";
pr " for (i = 0; %s[i]; ++i) {\n" n;
pr " if (i > 0) fputc (' ', trace_buffer.fp);\n";
pr " fputs (%s[i], trace_buffer.fp);\n" n;
pr " }\n";
pr " fputc ('\"', trace_buffer.fp);\n";
| Bool n -> (* boolean *)
pr " fputs (%s ? \" true\" : \" false\", trace_buffer.fp);\n" n
| Int n -> (* int *)
pr " fprintf (trace_buffer.fp, \" %%d\", %s);\n" n
| Int64 n ->
pr " fprintf (trace_buffer.fp, \" %%\" PRIi64, %s);\n" n
| BufferIn n -> (* RHBZ#646822 *)
pr " fputc (' ', trace_buffer.fp);\n";
pr " guestfs_int_print_BufferIn (trace_buffer.fp, %s, %s_size);\n" n n
| Pointer (t, n) ->
pr " fprintf (trace_buffer.fp, \" (%s)%%p\", %s);\n" t n
) args;
(* Optional arguments. *)
List.iter (
fun argt ->
let n = name_of_optargt argt in
pr " if (optargs->bitmask & GUESTFS_%s_%s_BITMASK) {\n"
(String.uppercase_ascii c_name) (String.uppercase_ascii n);
(match argt with
| OString n ->
pr " fprintf (trace_buffer.fp, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s);\n" n n
| OStringList n ->
pr " fprintf (trace_buffer.fp, \" \\\"%%s:\", \"%s\");\n" n;
pr " for (i = 0; optargs->%s[i] != NULL; ++i) {\n" n;
pr " if (i > 0) fputc (' ', trace_buffer.fp);\n";
pr " fputs (optargs->%s[i], trace_buffer.fp);\n" n;
pr " }\n";
pr " fputc ('\\\"', trace_buffer.fp);\n"
| OBool n ->
pr " fprintf (trace_buffer.fp, \" \\\"%%s:%%s\\\"\", \"%s\", optargs->%s ? \"true\" : \"false\");\n" n n
| OInt n ->
pr " fprintf (trace_buffer.fp, \" \\\"%%s:%%d\\\"\", \"%s\", optargs->%s);\n" n n
| OInt64 n ->
pr " fprintf (trace_buffer.fp, \" \\\"%%s:%%\" PRIi64 \"\\\"\", \"%s\", optargs->%s);\n" n n
);
pr " }\n"
) optargs;
pr " guestfs_int_trace_send_line (g, &trace_buffer);\n";
pr " }\n";
pr "\n";
in
let trace_return ?(indent = 2) name (ret, _, _) rv =
let indent = String.spaces indent in
pr "%sif (trace_flag) {\n" indent;
let needs_i =
match ret with
| RStringList _ | RHashtable _ | RStructList _ -> true
| _ -> false in
if needs_i then (
pr "%s size_t i;\n" indent;
pr "\n"
);
pr "%s guestfs_int_trace_open (&trace_buffer);\n" indent;
pr "%s fprintf (trace_buffer.fp, \"%%s = \", \"%s\");\n" indent name;
(match ret with
| RErr | RInt _ | RBool _ ->
pr "%s fprintf (trace_buffer.fp, \"%%d\", %s);\n" indent rv
| RInt64 _ ->
pr "%s fprintf (trace_buffer.fp, \"%%\" PRIi64, %s);\n" indent rv
| RConstString _ | RString _ ->
pr "%s fprintf (trace_buffer.fp, \"\\\"%%s\\\"\", %s);\n" indent rv
| RConstOptString _ ->
pr "%s fprintf (trace_buffer.fp, \"\\\"%%s\\\"\", %s != NULL ? %s : \"NULL\");\n"
indent rv rv
| RBufferOut _ ->
pr "%s guestfs_int_print_BufferOut (trace_buffer.fp, %s, *size_r);\n" indent rv
| RStringList _ | RHashtable _ ->
pr "%s fputs (\"[\", trace_buffer.fp);\n" indent;
pr "%s for (i = 0; %s[i]; ++i) {\n" indent rv;
pr "%s if (i > 0) fputs (\", \", trace_buffer.fp);\n" indent;
pr "%s fputs (\"\\\"\", trace_buffer.fp);\n" indent;
pr "%s fputs (%s[i], trace_buffer.fp);\n" indent rv;
pr "%s fputs (\"\\\"\", trace_buffer.fp);\n" indent;
pr "%s }\n" indent;
pr "%s fputs (\"]\", trace_buffer.fp);\n" indent;
| RStruct (_, typ) ->
pr "%s fprintf (trace_buffer.fp, \"<struct guestfs_%s = \");\n"
indent typ;
pr "%s guestfs_int_print_%s_indent (%s, trace_buffer.fp, \", \", \"\");\n"
indent typ rv;
pr "%s fprintf (trace_buffer.fp, \">\");\n" indent
| RStructList (_, typ) ->
pr "%s fprintf (trace_buffer.fp, \"<struct guestfs_%s_list(%%u)\", %s->len);\n"
indent typ rv;
pr "%s if (%s->len > 0)\n" indent rv;
pr "%s fprintf (trace_buffer.fp, \" = \");\n" indent;
pr "%s for (i = 0; i < %s->len; ++i) {\n" indent rv;
pr "%s if (i != 0)\n" indent;
pr "%s fprintf (trace_buffer.fp, \" \");\n" indent;
pr "%s fprintf (trace_buffer.fp, \"[%%zu]{\", i);\n" indent;
pr "%s guestfs_int_print_%s_indent (&%s->val[i], trace_buffer.fp, \", \", \"\");\n"
indent typ rv;
pr "%s fprintf (trace_buffer.fp, \"}\");\n" indent;
pr "%s }\n" indent;
pr "%s fprintf (trace_buffer.fp, \">\");\n" indent
);
pr "%s guestfs_int_trace_send_line (g, &trace_buffer);\n" indent;
pr "%s}\n" indent;
pr "\n";
in
let trace_return_error ?(indent = 2) name (ret, _, _) errcode =
let indent = String.spaces indent in
pr "%sif (trace_flag)\n" indent;
pr "%s guestfs_int_trace (g, \"%%s = %%s (error)\",\n" indent;
pr "%s \"%s\", \"%s\");\n"
indent name (string_of_errcode errcode)
in
let handle_null_optargs optargs c_name =
if optargs <> [] then (
pr " struct guestfs_%s_argv optargs_null;\n" c_name;
pr " if (!optargs) {\n";
pr " optargs_null.bitmask = 0;\n";
pr " optargs = &optargs_null;\n";
pr " }\n\n";
)
in
(* For non-daemon functions, generate a wrapper around each function. *)
let generate_non_daemon_wrapper { name; c_name;
style = ret, _, optargs as style;
config_only } =
if optargs = [] then
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" ~prefix:"guestfs_"
~dll_public:true
c_name style
else
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv" ~optarg_proto:Argv
~dll_public:true
c_name style;
pr "{\n";
pr " ACQUIRE_LOCK_FOR_CURRENT_SCOPE (g);\n";
handle_null_optargs optargs c_name;
pr " int trace_flag = g->trace;\n";
pr " struct trace_buffer trace_buffer;\n";
(match ret with
| RErr | RInt _ | RBool _ ->
pr " int r;\n"
| RInt64 _ ->
pr " int64_t r;\n"
| RConstString _ ->
pr " const char *r;\n"
| RConstOptString _ ->
pr " const char *r;\n"
| RString _ | RBufferOut _ ->
pr " char *r;\n"
| RStringList _ | RHashtable _ ->
pr " char **r;\n"
| RStruct (_, typ) ->
pr " struct guestfs_%s *r;\n" typ
| RStructList (_, typ) ->
pr " struct guestfs_%s_list *r;\n" typ
);
pr "\n";
if config_only then (
pr " if (g->state != CONFIG) {\n";
pr " error (g, \"%%s: this function can only be called in the config state\",\n";
pr " \"%s\");\n" c_name;
pr " return -1;\n";
pr " }\n";
);
enter_event name;
check_null_strings c_name style;
reject_unknown_optargs c_name style;
check_args_validity c_name style;
trace_call name c_name style;
pr " r = guestfs_impl_%s " c_name;
generate_c_call_args ~handle:"g" ~implicit_size_ptr:"size_r" style;
pr ";\n";
pr "\n";
(match errcode_of_ret ret with
| (`ErrorIsMinusOne | `ErrorIsNULL) as errcode ->
pr " if (r != %s) {\n" (string_of_errcode errcode);
trace_return ~indent:4 name style "r";
pr " } else {\n";
trace_return_error ~indent:4 name style errcode;
pr " }\n";
| `CannotReturnError ->
trace_return name style "r";
);
pr "\n";
pr " return r;\n";
pr "}\n";
pr "\n"
in
List.iter (
function
| { wrapper = true } as f ->
generate_non_daemon_wrapper f
| { wrapper = false } ->
() (* no wrapper *)
) (actions |> non_daemon_functions |> sort);
(* Client-side stubs for each function. *)
let generate_daemon_stub { name; c_name;
style = ret, args, optargs as style } =
let errcode =
match errcode_of_ret ret with
| `CannotReturnError -> assert false
| (`ErrorIsMinusOne | `ErrorIsNULL) as e -> e in
(* Generate the action stub. *)
if optargs = [] then
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" ~prefix:"guestfs_"
~dll_public:true
c_name style
else
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" ~prefix:"guestfs_" ~suffix:"_argv"
~optarg_proto:Argv
~dll_public:true
c_name style;
pr "{\n";
pr " ACQUIRE_LOCK_FOR_CURRENT_SCOPE (g);\n";
handle_null_optargs optargs c_name;
let args_passed_to_daemon =
List.filter (function String ((FileIn| FileOut), _) -> false | _ -> true)
args in
(match args_passed_to_daemon, optargs with
| [], [] -> ()
| _, _ -> pr " struct guestfs_%s_args args;\n" name
);
pr " guestfs_message_header hdr;\n";
pr " guestfs_message_error err;\n";
let has_ret =
match ret with
| RErr -> false
| RConstString _ | RConstOptString _ ->
failwithf "RConstString|RConstOptString cannot be used by daemon functions"
| RInt _ | RInt64 _
| RBool _ | RString _ | RStringList _
| RStruct _ | RStructList _
| RHashtable _ | RBufferOut _ ->
pr " struct guestfs_%s_ret ret;\n" name;
true in
pr " int serial;\n";
pr " int r;\n";
pr " int trace_flag = g->trace;\n";
pr " struct trace_buffer trace_buffer;\n";
(match ret with
| RErr | RInt _ | RBool _ -> pr " int ret_v;\n"
| RInt64 _ -> pr " int64_t ret_v;\n"
| RConstString _ | RConstOptString _ -> pr " const char *ret_v;\n"
| RString _ | RBufferOut _ -> pr " char *ret_v;\n"
| RStringList _ | RHashtable _ -> pr " char **ret_v;\n"
| RStruct (_, typ) -> pr " struct guestfs_%s *ret_v;\n" typ
| RStructList (_, typ) -> pr " struct guestfs_%s_list *ret_v;\n" typ
);
let has_filein =
List.exists (function String (FileIn, _) -> true | _ -> false) args in
if has_filein then (
pr " uint64_t progress_hint = 0;\n";
pr " struct stat progress_stat;\n";
) else
pr " const uint64_t progress_hint = 0;\n";
pr "\n";
enter_event name;
check_null_strings c_name style;
reject_unknown_optargs c_name style;
check_args_validity c_name style;
trace_call name c_name style;
(* Calculate the total size of all FileIn arguments to pass
* as a progress bar hint.
*)
List.iter (
function
| String (FileIn, n) ->
pr " if (stat (%s, &progress_stat) == 0 &&\n" n;
pr " S_ISREG (progress_stat.st_mode))\n";
pr " progress_hint += progress_stat.st_size;\n";
pr "\n";
| _ -> ()
) args;
(* This is a daemon_function so check the appliance is up. *)
pr " if (guestfs_int_check_appliance_up (g, \"%s\") == -1) {\n" name;
trace_return_error ~indent:4 name style errcode;
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr "\n";
(* Send the main header and arguments. *)
if args_passed_to_daemon = [] && optargs = [] then (
pr " serial = guestfs_int_send (g, GUESTFS_PROC_%s, progress_hint, 0,\n"
(String.uppercase_ascii name);
pr " NULL, NULL);\n"
) else (
List.iter (
function
| String (_, n) ->
pr " args.%s = (char *) %s;\n" n n
| OptString n ->
pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
| StringList (_, n) ->
pr " args.%s.%s_val = (char **) %s;\n" n n n;
pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
| Bool n ->
pr " args.%s = %s;\n" n n
| Int n ->
pr " args.%s = %s;\n" n n
| Int64 n ->
pr " args.%s = %s;\n" n n
| BufferIn n ->
pr " /* Just catch grossly large sizes. XDR encoding will make this precise. */\n";
pr " if (%s_size >= GUESTFS_MESSAGE_MAX) {\n" n;
trace_return_error ~indent:4 name style errcode;
pr " error (g, \"%%s: size of input buffer too large\", \"%s\");\n"
name;
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr " args.%s.%s_val = (char *) %s;\n" n n n;
pr " args.%s.%s_len = %s_size;\n" n n n
| Pointer _ -> assert false
) args_passed_to_daemon;
List.iter (
fun argt ->
let n = name_of_optargt argt in
pr " if (optargs->bitmask & GUESTFS_%s_%s_BITMASK) {\n"
(String.uppercase_ascii c_name) (String.uppercase_ascii n);
(match argt with
| OBool n
| OInt n
| OInt64 n ->
pr " args.%s = optargs->%s;\n" n n;
pr " } else {\n";
pr " args.%s = 0;\n" n;
pr " }\n";
| OString n ->
pr " args.%s = (char *) optargs->%s;\n" n n;
pr " } else {\n";
pr " args.%s = (char *) \"\";\n" n;
pr " }\n";
| OStringList n ->
pr " args.%s.%s_val = (char **) optargs->%s;\n" n n n;
pr " for (args.%s.%s_len = 0; optargs->%s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
pr " } else {\n";
pr " args.%s.%s_len = 0;\n" n n;
pr " args.%s.%s_val = NULL;\n" n n;
pr " }\n";
)
) optargs;
pr " serial = guestfs_int_send (g, GUESTFS_PROC_%s,\n"
(String.uppercase_ascii name);
pr " progress_hint, %s,\n"
(if optargs <> [] then "optargs->bitmask" else "0");
pr " (xdrproc_t) xdr_guestfs_%s_args, (char *) &args);\n"
name;
);
pr " if (serial == -1) {\n";
trace_return_error ~indent:4 name style errcode;
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr "\n";
(* Send any additional files (FileIn) requested. *)
let need_read_reply_label = ref false in
List.iter (
function
| String (FileIn, n) ->
pr " r = guestfs_int_send_file (g, %s);\n" n;
pr " if (r == -1) {\n";
trace_return_error ~indent:4 name style errcode;
pr " /* daemon will send an error reply which we discard */\n";
pr " guestfs_int_recv_discard (g, \"%s\");\n" name;
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr " if (r == -2) /* daemon cancelled */\n";
pr " goto read_reply;\n";
need_read_reply_label := true;
pr "\n";
| _ -> ()
) args;
(* Wait for the reply from the remote end. *)
if !need_read_reply_label then pr " read_reply:\n";
pr " memset (&hdr, 0, sizeof hdr);\n";
pr " memset (&err, 0, sizeof err);\n";
if has_ret then pr " memset (&ret, 0, sizeof ret);\n";
pr "\n";
pr " r = guestfs_int_recv (g, \"%s\", &hdr, &err,\n " name;
if not has_ret then
pr "NULL, NULL"
else
pr "(xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret" name;
pr ");\n";
pr " if (r == -1) {\n";
trace_return_error ~indent:4 name style errcode;
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr "\n";
pr " if (guestfs_int_check_reply_header (g, &hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
(String.uppercase_ascii name);
trace_return_error ~indent:4 name style errcode;
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr "\n";
pr " if (hdr.status == GUESTFS_STATUS_ERROR) {\n";
pr " int errnum = 0;\n";
pr "\n";
trace_return_error ~indent:4 name style errcode;
pr " if (err.errno_string[0] != '\\0')\n";
pr " errnum = guestfs_int_string_to_errno (err.errno_string);\n";
pr " if (errnum <= 0)\n";
pr " error (g, \"%%s: %%s\", \"%s\", err.error_message);\n"
name;
pr " else\n";
pr " guestfs_int_error_errno (g, errnum, \"%%s: %%s\", \"%s\",\n"
name;
pr " err.error_message);\n";
pr " free (err.error_message);\n";
pr " free (err.errno_string);\n";
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr "\n";
(* Expecting to receive further files (FileOut)? *)
List.iter (
function
| String (FileOut, n) ->
pr " if (guestfs_int_recv_file (g, %s) == -1) {\n" n;
trace_return_error ~indent:4 name style errcode;
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr "\n";
| _ -> ()
) args;
(match ret with
| RErr ->
pr " ret_v = 0;\n"
| RInt n | RInt64 n | RBool n ->
pr " ret_v = ret.%s;\n" n
| RConstString _ | RConstOptString _ ->
failwithf "RConstString|RConstOptString cannot be used by daemon functions"
| RString (_, n) ->
pr " ret_v = ret.%s; /* caller will free */\n" n
| RStringList (_, n) | RHashtable (_, _, n) ->
pr " /* caller will free this, but we need to add a NULL entry */\n";
pr " ret.%s.%s_val =\n" n n;
pr " safe_realloc (g, ret.%s.%s_val,\n" n n;
pr " sizeof (char *) * (ret.%s.%s_len + 1));\n"
n n;
pr " ret.%s.%s_val[ret.%s.%s_len] = NULL;\n" n n n n;
pr " ret_v = ret.%s.%s_val;\n" n n
| RStruct (n, _) ->
pr " /* caller will free this */\n";
pr " ret_v = safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
| RStructList (n, _) ->
pr " /* caller will free this */\n";
pr " ret_v = safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
| RBufferOut n ->
pr " /* RBufferOut is tricky: If the buffer is zero-length, then\n";
pr " * _val might be NULL here. To make the API saner for\n";
pr " * callers, we turn this case into a unique pointer (using\n";
pr " * malloc(1)).\n";
pr " */\n";
pr " if (ret.%s.%s_len > 0) {\n" n n;
pr " *size_r = ret.%s.%s_len;\n" n n;
pr " ret_v = ret.%s.%s_val; /* caller will free */\n" n n;
pr " } else {\n";
pr " free (ret.%s.%s_val);\n" n n;
pr " char *p = safe_malloc (g, 1);\n";
pr " *size_r = ret.%s.%s_len;\n" n n;
pr " ret_v = p;\n";
pr " }\n";
);
trace_return name style "ret_v";
pr " return ret_v;\n";
pr "}\n\n"
in
List.iter (
fun f ->
generate_daemon_stub f
) (actions |> daemon_functions |> sort)
(* Functions which have optional arguments have two or three
* generated variants.
*)
and generate_client_actions_variants () =
generate_header CStyle LGPLv2plus;
pr "\
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <inttypes.h>
#include \"guestfs.h\"
#include \"guestfs-internal.h\"
#include \"guestfs-internal-actions.h\"
";
let generate_va_variants { name; c_name;
style = ret, args, optargs as style } =
assert (optargs <> []); (* checked by caller *)
(* Get the name of the last regular argument. *)
let last_arg =
match ret with
| RBufferOut _ -> "size_r"
| _ ->
match args with
| [] -> "g"
| args ->
let last = List.hd (List.rev args) in
let name = name_of_argt last in
match last with
| BufferIn n -> name ^ "_size"
| _ -> name
in
let rtype =
match ret with
| RErr | RInt _ | RBool _ -> "int "
| RInt64 _ -> "int64_t "
| RConstString _ | RConstOptString _ -> "const char *"
| RString _ | RBufferOut _ -> "char *"
| RStringList _ | RHashtable _ -> "char **"
| RStruct (_, typ) -> sprintf "struct guestfs_%s *" typ
| RStructList (_, typ) ->
sprintf "struct guestfs_%s_list *" typ in
(* The regular variable args function, just calls the _va variant. *)
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" ~prefix:"guestfs_" c_name style;
pr "{\n";
pr " va_list optargs;\n";
pr "\n";
pr " %sr;\n" rtype;
pr "\n";
pr " va_start (optargs, %s);\n" last_arg;
pr " r = guestfs_%s_va " c_name;
generate_c_call_args ~handle:"g" ~implicit_size_ptr:"size_r" style;
pr ";\n";
pr " va_end (optargs);\n";
pr "\n";
pr " return r;\n";
pr "}\n\n";
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA
c_name style;
pr "{\n";
pr " ACQUIRE_LOCK_FOR_CURRENT_SCOPE (g);\n";
pr " struct guestfs_%s_argv optargs_s;\n" c_name;
pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" c_name;
pr " int i;\n";
pr " uint64_t i_mask;\n";
pr "\n";
pr " optargs_s.bitmask = 0;\n";
pr "\n";
pr " while ((i = va_arg (args, int)) >= 0) {\n";
pr " switch (i) {\n";
List.iter (
fun argt ->
let n = name_of_optargt argt in
pr " case GUESTFS_%s_%s:\n"
(String.uppercase_ascii c_name) (String.uppercase_ascii n);
pr " optargs_s.%s = va_arg (args, " n;
(match argt with
| OBool _ | OInt _ -> pr "int"
| OInt64 _ -> pr "int64_t"
| OString _ -> pr "const char *"
| OStringList _ -> pr "char *const *"
);
pr ");\n";
pr " break;\n";
) optargs;
let errcode =
match errcode_of_ret ret with
| `CannotReturnError -> assert false
| (`ErrorIsMinusOne | `ErrorIsNULL) as e -> e in
pr " default:\n";
pr " error (g, \"%%s: unknown option %%d (this can happen if a program is compiled against a newer version of libguestfs, then dynamically linked to an older version)\",\n";
pr " \"%s\", i);\n" name;
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr "\n";
pr " i_mask = UINT64_C(1) << i;\n";
pr " if (optargs_s.bitmask & i_mask) {\n";
pr " error (g, \"%%s: same optional argument specified more than once\",\n";
pr " \"%s\");\n" name;
pr " return %s;\n" (string_of_errcode errcode);
pr " }\n";
pr " optargs_s.bitmask |= i_mask;\n";
pr " }\n";
pr "\n";
pr " return guestfs_%s_argv " c_name;
generate_c_call_args ~handle:"g" ~implicit_size_ptr:"size_r" style;
pr ";\n";
pr "}\n\n"
and generate_back_compat_wrapper { name;
style = ret, args, _ as style } =
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" ~prefix:"guestfs_"
name (ret, args, []);
pr "{\n";
pr " ACQUIRE_LOCK_FOR_CURRENT_SCOPE (g);\n";
pr " struct guestfs_%s_opts_argv optargs_s = { .bitmask = 0 };\n" name;
pr " struct guestfs_%s_opts_argv *optargs = &optargs_s;\n" name;
pr "\n";
pr " return guestfs_%s_opts_argv " name;
generate_c_call_args ~handle:"g" ~implicit_size_ptr:"size_r" style;
pr ";\n";
pr "}\n";
pr "\n"
in
List.iter (
function
| { style = _, _, [] } -> ()
| ({ style = _, _, (_::_); once_had_no_optargs = false } as f) ->
generate_va_variants f
| ({ style = _, _, (_::_); once_had_no_optargs = true } as f) ->
generate_va_variants f;
generate_back_compat_wrapper f
) (actions |> sort)
(* Code for turning events and event bitmasks into printable strings. *)
and generate_event_string_c () =
generate_header CStyle LGPLv2plus;
(* Longest event name. *)
let longest = List.fold_left (
fun longest (name, _) ->
let len = String.length name in
if len > longest then len else longest
) 0 events in
pr "\
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>
#include \"guestfs.h\"
#include \"guestfs-internal.h\"
GUESTFS_DLL_PUBLIC char *
guestfs_event_to_string (uint64_t event)
{
char *ret;
uint64_t i;
size_t len;
/* Count how long the final string will be. */
len = 1;
for (i = 0; i < %d; ++i) {
if ((event & (UINT64_C(1) << i)) != 0)
len += %d; /* overestimate */
}
/* Allocate the final string and construct it. */
ret = malloc (len);
if (!ret)
return NULL;
len = 0;
" (List.length events) (longest + 1);
(* Sort the events alphabetically so the events are returned sorted. *)
let sorted_event_names = List.sort compare (List.map fst events) in
List.iter (
fun name ->
pr " if ((event & GUESTFS_EVENT_%s) != 0) {\n"
(String.uppercase_ascii name);
pr " strcpy (&ret[len], \"%s,\");\n" name;
pr " len += %d + 1;\n" (String.length name);
pr " }\n";
) sorted_event_names;
pr "
if (len > 0)
ret[len-1] = '\\0'; /* truncates the final trailing comma */
else
ret[0] = '\\0';
return ret;
}
"
(* Generate the linker script which controls the visibility of
* symbols in the public ABI and ensures no other symbols get
* exported accidentally.
*)
and generate_linker_script () =
generate_header HashStyle GPLv2plus;
let globals = [
"guestfs_create";
"guestfs_create_flags";
"guestfs_close";
"guestfs_delete_event_callback";
"guestfs_event_to_string";
"guestfs_first_private";
"guestfs_get_error_handler";
"guestfs_get_out_of_memory_handler";
"guestfs_get_private";
"guestfs_last_errno";
"guestfs_last_error";
"guestfs_next_private";
"guestfs_pop_error_handler";
"guestfs_push_error_handler";
"guestfs_set_close_callback";
"guestfs_set_error_handler";
"guestfs_set_event_callback";
"guestfs_set_launch_done_callback";
"guestfs_set_log_message_callback";
"guestfs_set_out_of_memory_handler";
"guestfs_set_private";
"guestfs_set_progress_callback";
"guestfs_set_subprocess_quit_callback";
] in
let functions =
List.flatten (
List.map (
function
| { c_name; style = _, _, [] } -> ["guestfs_" ^ c_name]
| { c_name; style = _, _, (_::_);
once_had_no_optargs = false } ->
["guestfs_" ^ c_name;
"guestfs_" ^ c_name ^ "_va";
"guestfs_" ^ c_name ^ "_argv"]
| { name; c_name; style = _, _, (_::_);
once_had_no_optargs = true } ->
["guestfs_" ^ name;
"guestfs_" ^ c_name;
"guestfs_" ^ c_name ^ "_va";
"guestfs_" ^ c_name ^ "_argv"]
) actions
) in
let struct_frees =
List.concat (
List.map (fun { s_name = typ } ->
["guestfs_compare_" ^ typ;
"guestfs_compare_" ^ typ ^ "_list";
"guestfs_copy_" ^ typ;
"guestfs_copy_" ^ typ ^ "_list";
"guestfs_free_" ^ typ;
"guestfs_free_" ^ typ ^ "_list"]
) structs
) in
let globals = List.sort compare (globals @
functions @
struct_frees) in
pr "{\n";
pr " global:\n";
List.iter (pr " %s;\n") globals;
pr "\n";
pr " local:\n";
pr " *;\n";
pr "};\n"
and generate_max_proc_nr () =
pr "%d\n" Proc_nr.max_proc_nr
|