/usr/share/ada/adainclude/gnatcoll/gnatcoll-scripts-shell.adb is in libgnatcoll1.6-dev 1.6gpl2014-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ------------------------------------------------------------------------------
-- G N A T C O L L --
-- --
-- Copyright (C) 2003-2014, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- --
-- --
-- --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Containers.Indefinite_Vectors;
with Ada.Exceptions; use Ada.Exceptions;
with Ada.IO_Exceptions; use Ada.IO_Exceptions;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Unchecked_Deallocation;
with System.Address_Image;
with System; use System;
with GNAT.Debug_Utilities; use GNAT.Debug_Utilities;
with GNATCOLL.Mmap; use GNATCOLL.Mmap;
with GNAT.OS_Lib; use GNAT.OS_Lib;
with GNATCOLL.Scripts; use GNATCOLL.Scripts;
with GNATCOLL.Scripts.Impl; use GNATCOLL.Scripts.Impl;
with GNATCOLL.Traces; use GNATCOLL.Traces;
with GNATCOLL.Utils; use GNATCOLL.Utils;
package body GNATCOLL.Scripts.Shell is
Me : constant Trace_Handle := Create ("SHELL_SCRIPT", Off);
Me_Log : constant Trace_Handle := Create ("SCRIPTS.LOG", Off);
Cst_Prefix : constant String := "@cst@";
-- Prefix used to store the name of constants in class instances
use Instances_List, Command_Hash;
procedure Free_Internal_Data (Script : access Shell_Scripting_Record'Class);
-- Free the internal memory used to store the results of previous commands
-- and class instances.
----------
-- Misc --
----------
function Name_From_Instance
(Instance : access Class_Instance_Record'Class) return String;
-- Return the string to display to report the instance in the shell
function Instance_From_Name
(Script : access Shell_Scripting_Record'Class;
Name : String) return Class_Instance;
-- Opposite of Name_From_Instance
function Instance_From_Address
(Script : access Shell_Scripting_Record'Class;
Add : System.Address) return Class_Instance;
-- Return an instance from its address
function Execute_GPS_Shell_Command
(Script : access Shell_Scripting_Record'Class;
Command : String;
Errors : access Boolean) return String;
-- Execute a command in the GPS shell and returns its result.
-- Command might be a series of commands, separated by semicolons or
-- newlines. The return value is the result of the last command.
-- If Errors is set to True on exit, then the return value is an error msg
function Execute_GPS_Shell_Command
(Script : access Shell_Scripting_Record'Class;
CL : Arg_List;
Errors : access Boolean) return String;
-- Execute a command in the GPS shell and returns its result.
-- Command must be a single command (no semicolon-separated list).
procedure Module_Command_Handler
(Data : in out Callback_Data'Class;
Command : String);
-- Handles functions specific to the shell language
------------------------
-- Internals Nth_Arg --
------------------------
function Nth_Arg
(Data : Shell_Callback_Data;
N : Positive;
Success : access Boolean) return String;
function Nth_Arg
(Data : Shell_Callback_Data;
N : Positive;
Success : access Boolean) return Unbounded_String;
function Nth_Arg
(Data : Shell_Callback_Data;
N : Positive;
Success : access Boolean) return Subprogram_Type;
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive; Class : Class_Type;
Allow_Null : Boolean; Success : access Boolean) return Class_Instance;
-- These functions are called by the overriden Nth_Arg functions. They try
-- to return the parameter at the location N. If no parameter is found,
-- Success is false, true otherwise. It's the responsibility of the
-- enclosing Nth_Arg to either raise a No_Such_Parameter exception or to
-- return a default value.
--------------------
-- Block_Commands --
--------------------
procedure Block_Commands
(Script : access Shell_Scripting_Record; Block : Boolean) is
begin
Script.Blocked := Block;
end Block_Commands;
------------------------
-- Name_From_Instance --
------------------------
function Name_From_Instance
(Instance : access Class_Instance_Record'Class) return String is
begin
return '<' & Get_Name (Shell_Class_Instance (Instance).Class)
& "_0x" & System.Address_Image (Instance.all'Address)
& '>';
end Name_From_Instance;
------------------------
-- Instance_From_Name --
------------------------
function Instance_From_Name
(Script : access Shell_Scripting_Record'Class;
Name : String) return Class_Instance
is
Index : Natural := Name'First;
begin
if Name = "null" then
return No_Class_Instance;
end if;
while Index <= Name'Last - 3
and then Name (Index .. Index + 2) /= "_0x"
loop
Index := Index + 1;
end loop;
return Instance_From_Address
(Script, Value ("16#" & Name (Index + 3 .. Name'Last - 1) & "#"));
exception
when others =>
-- Invalid instance
return No_Class_Instance;
end Instance_From_Name;
---------------------------
-- Instance_From_Address --
---------------------------
function Instance_From_Address
(Script : access Shell_Scripting_Record'Class;
Add : System.Address) return Class_Instance
is
L : Instances_List.Cursor := First (Script.Instances);
begin
while Has_Element (L) loop
if Get_CIR (Element (L)).all'Address = Add then
return Element (L);
end if;
Next (L);
end loop;
return No_Class_Instance;
end Instance_From_Address;
-----------------
-- Is_Subclass --
-----------------
function Is_Subclass
(Instance : access Shell_Class_Instance_Record;
Base : String) return Boolean
is
pragma Unreferenced (Instance, Base);
begin
-- ??? Not checked
return True;
end Is_Subclass;
------------------
-- Set_Property --
------------------
overriding procedure Set_Property
(Instance : access Shell_Class_Instance_Record;
Name : String; Value : Integer) is
begin
-- We can only retrieve string constants later on, so convert here
Set_Data
(Instance, Cst_Prefix & Name, Create_Property (Image (Value, 0)));
end Set_Property;
overriding procedure Set_Property
(Instance : access Shell_Class_Instance_Record;
Name : String; Value : Float) is
begin
-- We can only retrieve string constants later on, so convert here
Set_Data
(Instance, Cst_Prefix & Name, Create_Property (Float'Image (Value)));
end Set_Property;
overriding procedure Set_Property
(Instance : access Shell_Class_Instance_Record;
Name : String; Value : Boolean) is
begin
Set_Data
(Instance, Cst_Prefix & Name, Create_Property (Boolean'Image (Value)));
end Set_Property;
overriding procedure Set_Property
(Instance : access Shell_Class_Instance_Record;
Name : String; Value : String) is
begin
Set_Data (Instance, Cst_Prefix & Name, Create_Property (Value));
end Set_Property;
---------------------
-- Name_Parameters --
---------------------
procedure Name_Parameters
(Data : in out Shell_Callback_Data; Names : Cst_Argument_List)
is
pragma Unreferenced (Data, Names);
begin
null;
end Name_Parameters;
----------------------------
-- Module_Command_Handler --
----------------------------
procedure Module_Command_Handler
(Data : in out Callback_Data'Class;
Command : String)
is
begin
if Command = "load" then
declare
Filename : constant String := Nth_Arg (Data, 1);
File : Mapped_File;
Errors : aliased Boolean;
begin
File := Open_Read (Filename);
Read (File);
declare
Ignored : constant String :=
Execute_GPS_Shell_Command
(Shell_Scripting (Get_Script (Data)),
String (GNATCOLL.Mmap.Data (File)(1 .. Last (File))),
Errors'Access);
pragma Unreferenced (Ignored);
begin
null;
end;
exception
when Name_Error =>
Set_Error_Msg
(Data, "File not found: """ & Filename & '"');
end;
elsif Command = "echo" or else Command = "echo_error" then
declare
Result : Unbounded_String;
begin
for A in 1 .. Number_Of_Arguments (Data) loop
Append (Result, String'(Nth_Arg (Data, A)));
if A /= Number_Of_Arguments (Data) then
Append (Result, ' ');
end if;
end loop;
if Command = "echo" then
Insert_Text
(Get_Script (Data),
Txt => To_String (Result) & ASCII.LF);
else
Insert_Error
(Get_Script (Data),
Txt => To_String (Result) & ASCII.LF);
end if;
end;
elsif Command = "clear_cache" then
Free_Internal_Data (Shell_Scripting (Get_Script (Data)));
end if;
end Module_Command_Handler;
----------------
-- Initialize --
----------------
procedure Initialize
(Data : in out Shell_Callback_Data'Class;
Script : access Shell_Scripting_Record'Class) is
begin
Data.Script := Shell_Scripting (Script);
Data.Return_Value := null;
Data.Return_Dict := null;
Data.Return_As_List := False;
Data.Return_As_Error := False;
end Initialize;
------------------------------
-- Register_Shell_Scripting --
------------------------------
procedure Register_Shell_Scripting
(Repo : Scripts_Repository;
Script : Shell_Scripting := null)
is
S : Shell_Scripting;
begin
if Script /= null then
S := Script;
else
S := new Shell_Scripting_Record;
end if;
S.Repo := Repo;
Register_Scripting_Language (Repo, S);
Register_Command
(Repo, "load",
Minimum_Args => 1,
Maximum_Args => 1,
Handler => Module_Command_Handler'Access,
Language => Shell_Name);
Register_Command
(Repo, "echo",
Minimum_Args => 0,
Maximum_Args => Natural'Last,
Handler => Module_Command_Handler'Access,
Language => Shell_Name);
Register_Command
(Repo, "echo_error",
Minimum_Args => 0,
Maximum_Args => Natural'Last,
Handler => Module_Command_Handler'Access,
Language => Shell_Name);
Register_Command
(Repo, "clear_cache",
Handler => Module_Command_Handler'Access,
Language => Shell_Name);
end Register_Shell_Scripting;
-------------------
-- List_Commands --
-------------------
procedure List_Commands
(Script : access Shell_Scripting_Record'Class;
Console : Virtual_Console := null)
is
package Command_List is
new Ada.Containers.Indefinite_Vectors (Positive, String);
package Ascending is new Command_List.Generic_Sorting ("<");
V : Command_List.Vector;
begin
-- Put all commands into V
declare
C : Command_Hash.Cursor := Script.Commands_List.First;
begin
while Has_Element (C) loop
V.Append (Element (C).Command.all);
Next (C);
end loop;
end;
-- Sort commands
Ascending.Sort (V);
-- Output them
declare
C : Command_List.Cursor := V.First;
begin
while Command_List.Has_Element (C) loop
Insert_Text
(Script, Console, Command_List.Element (C) & ASCII.LF);
Command_List.Next (C);
end loop;
end;
end List_Commands;
-----------------------
-- Register_Property --
-----------------------
overriding procedure Register_Property
(Script : access Shell_Scripting_Record;
Prop : Property_Descr_Access)
is
pragma Unreferenced (Script, Prop);
begin
-- All the work is done in Execute_Command
null;
end Register_Property;
----------------------
-- Register_Command --
----------------------
overriding procedure Register_Command
(Script : access Shell_Scripting_Record;
Command : Command_Descr_Access)
is
Cmd : GNAT.Strings.String_Access;
Info_C : Command_Hash.Cursor;
Info : Command_Information_Access;
begin
if Command.Command = "" then
return;
end if;
if Command.Class /= No_Class then
if Command.Command = Constructor_Method then
Cmd := new String'(Get_Name (Command.Class));
elsif Command.Command = Destructor_Method then
Cmd := new String'(Get_Name (Command.Class) & ".__delete");
else
Cmd := new String'
(Get_Name (Command.Class) & "." & Command.Command);
-- First parameter is always the instance
end if;
else
Cmd := new String'(Command.Command);
end if;
Info_C := Find (Script.Commands_List, Cmd.all);
-- Check that the command is not already registered
if Has_Element (Info_C) then
raise Program_Error with "Command already registered " & Cmd.all;
else
Info := new Command_Information'
(Command => Cmd,
Cmd => Command);
Include (Script.Commands_List, Cmd.all, Info);
end if;
end Register_Command;
--------------------
-- Register_Class --
--------------------
procedure Register_Class
(Script : access Shell_Scripting_Record;
Name : String;
Base : Class_Type := No_Class)
is
pragma Unreferenced (Script, Name, Base);
begin
-- Classes not supported in the shell module
null;
end Register_Class;
--------------------
-- Display_Prompt --
--------------------
overriding procedure Display_Prompt
(Script : access Shell_Scripting_Record;
Console : Virtual_Console := null) is
begin
Insert_Prompt (Script, Console, Script.Prompt.all);
end Display_Prompt;
----------------
-- Get_Prompt --
----------------
overriding function Get_Prompt
(Script : access Shell_Scripting_Record) return String is
begin
return Script.Prompt.all;
end Get_Prompt;
--------------
-- Complete --
--------------
procedure Complete
(Script : access Shell_Scripting_Record;
Input : String;
Completions : out String_Lists.List)
is
Current : Command_Hash.Cursor;
Info : Command_Information_Access;
begin
Completions := String_Lists.Empty_List;
Current := First (Script.Commands_List);
while Has_Element (Current) loop
Info := Element (Current);
declare
S : constant String := Info.Command.all;
begin
if S'Length >= Input'Length
and then S (S'First .. S'First + Input'Length - 1) = Input
then
String_Lists.Append (Completions, S);
end if;
end;
Next (Current);
end loop;
String_Lists_Sort.Sort (Completions);
end Complete;
---------------------
-- Execute_Command --
---------------------
procedure Execute_Command
(Script : access Shell_Scripting_Record;
CL : Arg_List;
Console : Virtual_Console := null;
Hide_Output : Boolean := False;
Show_Command : Boolean := True;
Errors : out Boolean)
is
pragma Unreferenced (Show_Command);
Old_Console : constant Virtual_Console := Script.Console;
Err : aliased Boolean;
begin
if Console /= null then
Script.Console := Console;
end if;
declare
S : constant String :=
Execute_GPS_Shell_Command
(Script, CL, Err'Unchecked_Access);
begin
Errors := Err;
if S /= "" then
Insert_Text (Script, Console, S & ASCII.LF);
end if;
Script.Console := Old_Console;
-- Do not display the prompt in the shell console if we did not
-- output to it
if not Hide_Output
and then (Console = null or else Console = Old_Console)
then
Display_Prompt (Script, Script.Console);
end if;
end;
end Execute_Command;
-------------------------------
-- Execute_Command_With_Args --
-------------------------------
function Execute_Command_With_Args
(Script : access Shell_Scripting_Record;
CL : Arg_List) return String
is
Errors : aliased Boolean;
begin
return Execute_GPS_Shell_Command
(Script, CL, Errors'Unchecked_Access);
end Execute_Command_With_Args;
------------------
-- Execute_File --
------------------
procedure Execute_File
(Script : access Shell_Scripting_Record;
Filename : String;
Console : Virtual_Console := null;
Hide_Output : Boolean := False;
Show_Command : Boolean := True;
Errors : out Boolean)
is
Old_Console : constant Virtual_Console := Script.Console;
Err : aliased Boolean;
CL : Arg_List;
begin
if Console /= null then
Script.Console := Console;
end if;
CL := Create ("load");
Append_Argument (CL, Filename, One_Arg);
if Show_Command then
Insert_Text (Script, Console, To_Display_String (CL));
end if;
declare
S : constant String := Execute_GPS_Shell_Command
(Script, CL, Err'Unchecked_Access);
begin
Errors := Err;
if S /= "" and then not Hide_Output then
Insert_Text (Script, Console, S & ASCII.LF);
end if;
Script.Console := Old_Console;
if not Hide_Output then
Display_Prompt (Script, Script.Console);
end if;
end;
end Execute_File;
--------------
-- Get_Name --
--------------
function Get_Name (Script : access Shell_Scripting_Record) return String is
pragma Unreferenced (Script);
begin
return Shell_Name;
end Get_Name;
----------
-- Free --
----------
procedure Free (Com : in out Command_Information_Access) is
procedure Unchecked_Free is new Ada.Unchecked_Deallocation
(Command_Information, Command_Information_Access);
begin
Free (Com.Command);
Unchecked_Free (Com);
end Free;
------------------------
-- Free_Internal_Data --
------------------------
procedure Free_Internal_Data
(Script : access Shell_Scripting_Record'Class) is
begin
for R in Script.Returns'Range loop
Free (Script.Returns (R));
end loop;
Script.Instances.Clear;
end Free_Internal_Data;
-------------
-- Destroy --
-------------
procedure Destroy (Script : access Shell_Scripting_Record) is
C : Command_Hash.Cursor;
Com : Command_Information_Access;
begin
Free_Internal_Data (Script);
Free (Script.Prompt);
Free (Script.Returns);
C := First (Script.Commands_List);
while Has_Element (C) loop
Com := Element (C);
Free (Com);
Next (C);
end loop;
Script.Finalized := True;
end Destroy;
----------------
-- Set_Prompt --
----------------
procedure Set_Prompt
(Script : access Shell_Scripting_Record'Class;
Prompt : String) is
begin
Free (Script.Prompt);
Script.Prompt := new String'(Prompt);
end Set_Prompt;
---------------------
-- Execute_Command --
---------------------
function Execute_Command
(Script : access Shell_Scripting_Record;
CL : Arg_List;
Console : Virtual_Console := null;
Hide_Output : Boolean := False;
Show_Command : Boolean := True;
Errors : access Boolean) return String
is
pragma Unreferenced (Show_Command);
Err : aliased Boolean;
Old_Console : constant Virtual_Console := Script.Console;
begin
if Console /= null then
Script.Console := Console;
end if;
declare
Result : constant String := Execute_GPS_Shell_Command
(Script, CL, Err'Unchecked_Access);
begin
Errors.all := Err;
if Result /= "" and then not Hide_Output then
Insert_Text (Script, Console, Result & ASCII.LF);
end if;
Script.Console := Old_Console;
if not Hide_Output then
Display_Prompt (Script, Script.Console);
end if;
return Result;
end;
end Execute_Command;
---------------------
-- Execute_Command --
---------------------
function Execute_Command
(Script : access Shell_Scripting_Record;
CL : Arg_List;
Console : Virtual_Console := null;
Hide_Output : Boolean := False;
Errors : access Boolean) return Boolean
is
Old_Console : constant Virtual_Console := Script.Console;
Err : aliased Boolean;
begin
if Console /= null then
Script.Console := Console;
end if;
declare
Result : constant String := Trim
(Execute_GPS_Shell_Command (Script, CL, Err'Unchecked_Access),
Ada.Strings.Both);
begin
Errors.all := Err;
if not Hide_Output then
Insert_Text (Script, Console, Result & ASCII.LF);
end if;
Script.Console := Old_Console;
if not Hide_Output then
Display_Prompt (Script, Script.Console);
end if;
return Result = "1" or else To_Lower (Result) = "true";
end;
end Execute_Command;
-------------------------------
-- Execute_GPS_Shell_Command --
-------------------------------
function Execute_GPS_Shell_Command
(Script : access Shell_Scripting_Record'Class;
CL : Arg_List;
Errors : access Boolean) return String
is
Data_C : Command_Hash.Cursor;
Data : Command_Information_Access;
Instance : Class_Instance;
Min, Max : Natural;
Found : Boolean;
Count : Natural;
Command : constant String := Get_Command (CL);
begin
Errors.all := False;
if Script.Finalized then
return "";
end if;
if Command = "" then
return "";
end if;
if Active (Me) then
Trace (Me, "Executing " & To_Display_String (CL)
& " blocked=" & Script.Blocked'Img);
end if;
if Script.Blocked then
Errors.all := True;
return "A command is already executing";
end if;
if Active (Me_Log) then
Trace (Me_Log,
"Executing " & To_Display_String (CL, Max_Arg_Length => 100));
end if;
-- Special case: access to instance constants
if Command (Command'First) = '@' then
Min := 1;
Max := 2;
Found := True;
else
Data_C := Find (Script.Commands_List, Command);
Found := Has_Element (Data_C);
if Found then
Data := Element (Data_C);
Min := Data.Cmd.Minimum_Args;
Max := Data.Cmd.Maximum_Args;
if Data.Cmd.Class /= No_Class
and then not Data.Cmd.Static_Method
and then Data.Cmd.Command /= Constructor_Method
and then Data.Cmd.Command /= Destructor_Method
then
Min := Min + 1;
if Max /= Natural'Last then
Max := Max + 1;
end if;
end if;
end if;
end if;
if Found then
if Min <= Args_Length (CL) and then Args_Length (CL) <= Max then
Count := Args_Length (CL);
if Data /= null and then Data.Cmd.Command = Constructor_Method then
Count := Count + 1;
end if;
declare
Callback : Shell_Callback_Data'Class :=
Shell_Callback_Data'Class (Create (Script, Count));
-- The call above allocates Callback.Args, no need to do that
-- below
begin
Callback.Script := Shell_Scripting (Script);
Callback.CL := Create ("");
if Data /= null
and then Data.Cmd.Command = Constructor_Method
then
Instance := New_Instance (Callback.Script, Data.Cmd.Class);
Append_Argument
(Callback.CL,
Name_From_Instance (Get_CIR (Instance)),
One_Arg);
end if;
for A in 1 .. Args_Length (CL) loop
declare
Args_A : constant String := Nth_Arg (CL, A);
begin
if Args_A'Length > 0
and then Args_A (Args_A'First) = '%'
then
declare
Num : Integer;
begin
Num := Integer'Value
(Args_A (Args_A'First + 1 .. Args_A'Last));
Append_Argument
(Callback.CL,
Script.Returns
(Num + Script.Returns'First - 1).all,
One_Arg);
exception
when Constraint_Error =>
Append_Argument (Callback.CL, Args_A, One_Arg);
end;
else
Append_Argument (Callback.CL, Args_A, One_Arg);
end if;
end;
end loop;
if Data = null then
-- Accessing a field
Instance := Nth_Arg (Callback, 1, Any_Class);
-- To match python, we first check for simple properties
declare
Prop : constant Instance_Property :=
Get_Data
(Instance,
Cst_Prefix
& Command
(Command'First + 1 .. Command'Last));
P : Property_Descr_Access;
begin
if Prop /= null then
Trace (Me, "A simple property");
if Number_Of_Arguments (Callback) = 2 then
Errors.all := True;
return "Property is read-only: "
& Command (Command'First + 1 .. Command'Last);
end if;
Set_Return_Value (Callback, As_String (Prop.all));
else
Trace (Me, "A setter/getter property args="
& Number_Of_Arguments (Callback)'Img);
-- Does this correspond to a setter/getter property ?
P := Script.Repo.Properties;
while P /= null loop
exit when P.Class =
Shell_Class_Instance (Get_CIR (Instance)).Class
and then P.Name =
Command (Command'First + 1 .. Command'Last);
P := P.Next;
end loop;
if P = null then
Errors.all := True;
return "Command not recognized: " & Command;
end if;
if Number_Of_Arguments (Callback) = 1 then
if P.Getter = null then
Trace (Me, "Property is read-only");
Errors.all := True;
return "Property is write-only: "
& Command (Command'First + 1 .. Command'Last);
end if;
P.Getter (Callback, P.Name);
-- Already set the return value, nothing else to do
else
if P.Setter = null then
Errors.all := True;
return "Property is read-only: "
& Command (Command'First + 1 .. Command'Last);
end if;
P.Setter (Callback, P.Name);
-- Already set the value, nothing else to do
end if;
end if;
end;
else
Data.Cmd.Handler (Callback, Data.Cmd.Command);
if Callback.Return_As_Error then
Errors.all := True;
Free (Callback.Return_Dict);
declare
R : constant String := Callback.Return_Value.all;
begin
Free (Callback.Return_Value);
return R;
end;
end if;
if Data.Cmd.Command = Constructor_Method then
Set_Return_Value (Callback, Instance);
end if;
if Callback.Return_Dict /= null then
Free (Callback.Return_Value);
Callback.Return_Value := Callback.Return_Dict;
Callback.Return_Dict := null;
end if;
end if;
-- Save the return value for the future
Free (Script.Returns (Script.Returns'Last));
Script.Returns
(Script.Returns'First + 1 .. Script.Returns'Last) :=
Script.Returns
(Script.Returns'First .. Script.Returns'Last - 1);
if Callback.Return_Value = null then
Script.Returns (Script.Returns'First) := new String'("");
else
Script.Returns (Script.Returns'First) :=
Callback.Return_Value;
end if;
if Callback.Return_Value = null then
return "";
else
-- Do not free Callback.Return_Value, it is stored in the
-- list of previous commands
return Callback.Return_Value.all;
end if;
end;
else
Errors.all := True;
return "Incorrect number of arguments for " & Command;
end if;
end if;
Errors.all := True;
return "Command not recognized: " & Command;
exception
when Invalid_Parameter =>
Errors.all := True;
return "Invalid parameter for " & Command;
when E : others =>
Errors.all := True;
return Exception_Information (E);
end Execute_GPS_Shell_Command;
-------------------------------
-- Execute_GPS_Shell_Command --
-------------------------------
function Execute_GPS_Shell_Command
(Script : access Shell_Scripting_Record'Class;
Command : String;
Errors : access Boolean) return String
is
CL : Arg_List;
First, Last : Integer;
Quoted : Boolean;
Triple_Quoted : Boolean;
begin
Errors.all := False;
if Command /= "" then
First := Command'First;
while First <= Command'Last loop
while First <= Command'Last
and then (Command (First) = ' '
or else Command (First) = ASCII.HT)
loop
First := First + 1;
end loop;
if First > Command'Last then
exit;
end if;
Last := First;
Quoted := False;
Triple_Quoted := False;
-- Search until the beginning of the next command (separated by
-- semicolon or newline).
while Last <= Command'Last loop
exit when not Quoted
and then not Triple_Quoted
and then (Command (Last) = ';'
or else Command (Last) = ASCII.LF);
if Command (Last) = '"' then
if Last <= Command'Last - 2
and then Command (Last + 1) = '"'
and then Command (Last + 2) = '"'
then
Triple_Quoted := not Triple_Quoted;
Last := Last + 2;
elsif not Triple_Quoted then
Quoted := not Quoted;
end if;
elsif Command (Last) = '\'
and then Last < Command'Last
then
Last := Last + 1;
end if;
Last := Last + 1;
end loop;
if Last - 1 >= First then
CL := Parse_String (Command (First .. Last - 1),
Command_Line_Treatment (Script));
if CL = Empty_Command_Line then
Errors.all := True;
return "Couldn't parse argument string for "
& Command (First .. Last - 1);
else
declare
R : constant String := Execute_GPS_Shell_Command
(Script,
CL => CL,
Errors => Errors);
begin
if Last > Command'Last then
return R;
end if;
end;
end if;
end if;
First := Last + 1;
end loop;
end if;
return "";
end Execute_GPS_Shell_Command;
----------------
-- Get_Script --
----------------
function Get_Script
(Data : Shell_Callback_Data) return Scripting_Language is
begin
return Scripting_Language (Data.Script);
end Get_Script;
--------------------
-- Get_Repository --
--------------------
function Get_Repository
(Script : access Shell_Scripting_Record) return Scripts_Repository is
begin
return Script.Repo;
end Get_Repository;
--------------------
-- Current_Script --
--------------------
function Current_Script
(Script : access Shell_Scripting_Record) return String
is
pragma Unreferenced (Script);
begin
return "<shell script>";
end Current_Script;
-------------------------
-- Number_Of_Arguments --
-------------------------
function Number_Of_Arguments (Data : Shell_Callback_Data) return Natural is
begin
return Args_Length (Data.CL);
end Number_Of_Arguments;
----------
-- Free --
----------
procedure Free (Data : in out Shell_Callback_Data) is
pragma Unreferenced (Data);
begin
null;
end Free;
-----------
-- Clone --
-----------
function Clone (Data : Shell_Callback_Data) return Callback_Data'Class is
New_CL : Arg_List := Create (Get_Command (Data.CL));
begin
for A in 1 .. Args_Length (Data.CL) loop
Append_Argument (New_CL, Nth_Arg (Data.CL, A), One_Arg);
end loop;
return Shell_Callback_Data'
(Callback_Data with
CL => New_CL,
Script => Data.Script,
Return_Value => null,
Return_Dict => null,
Return_As_List => False,
Return_As_Error => False);
end Clone;
------------
-- Create --
------------
function Create
(Script : access Shell_Scripting_Record;
Arguments_Count : Natural) return Callback_Data'Class
is
Data : constant Shell_Callback_Data :=
(Callback_Data with
Script => Shell_Scripting (Script),
CL => Empty_Command_Line,
Return_Value => null,
Return_Dict => null,
Return_As_List => False,
Return_As_Error => False);
pragma Unreferenced (Arguments_Count);
begin
return Data;
end Create;
-----------------
-- Set_Nth_Arg --
-----------------
procedure Set_Nth_Arg
(Data : in out Shell_Callback_Data; N : Positive; Value : Subprogram_Type)
is
begin
Set_Nth_Arg
(Data.CL, N,
Shell_Subprogram_Record (Value.all).Command.all);
end Set_Nth_Arg;
-----------------
-- Set_Nth_Arg --
-----------------
procedure Set_Nth_Arg
(Data : in out Shell_Callback_Data; N : Positive; Value : String) is
begin
Set_Nth_Arg (Data.CL, N, Value);
end Set_Nth_Arg;
-----------------
-- Set_Nth_Arg --
-----------------
procedure Set_Nth_Arg
(Data : in out Shell_Callback_Data; N : Positive; Value : Integer) is
begin
Set_Nth_Arg (Data.CL, N, Integer'Image (Value));
end Set_Nth_Arg;
-----------------
-- Set_Nth_Arg --
-----------------
procedure Set_Nth_Arg
(Data : in out Shell_Callback_Data; N : Positive; Value : Float) is
begin
Set_Nth_Arg (Data.CL, N, Float'Image (Value));
end Set_Nth_Arg;
-----------------
-- Set_Nth_Arg --
-----------------
procedure Set_Nth_Arg
(Data : in out Shell_Callback_Data; N : Positive; Value : Boolean) is
begin
Set_Nth_Arg (Data.CL, N, Boolean'Image (Value));
end Set_Nth_Arg;
-----------------
-- Set_Nth_Arg --
-----------------
procedure Set_Nth_Arg
(Data : in out Shell_Callback_Data;
N : Positive; Value : Class_Instance) is
begin
Set_Nth_Arg (Data.CL, N, Name_From_Instance (Get_CIR (Value)));
end Set_Nth_Arg;
-----------------
-- Set_Nth_Arg --
-----------------
overriding procedure Set_Nth_Arg
(Data : in out Shell_Callback_Data;
N : Positive; Value : List_Instance) is
begin
Set_Nth_Arg
(Data.CL, N, '(' & Get_Command (Shell_Callback_Data (Value).CL) & ')');
end Set_Nth_Arg;
--------------
-- New_List --
--------------
overriding function New_List
(Script : access Shell_Scripting_Record;
Class : Class_Type := No_Class)
return List_Instance'Class
is
pragma Unreferenced (Class);
List : Shell_Callback_Data;
begin
List.Script := Shell_Scripting (Script);
List.CL := Empty_Command_Line;
return List;
end New_List;
-------------
-- Nth_Arg --
-------------
overriding function Nth_Arg
(Data : Shell_Callback_Data; N : Positive)
return List_Instance'Class
is
List : Shell_Callback_Data;
begin
List.Script := Data.Script;
if N > Args_Length (Data.CL) then
List.CL := Empty_Command_Line; -- An empty list
else
List.CL := Parse_String (Nth_Arg (Data.CL, N), Separate_Args);
end if;
return List;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive; Success : access Boolean)
return String
is
begin
if N > Args_Length (Data.CL) then
Success.all := False;
return "";
else
Success.all := True;
return Nth_Arg (Data.CL, N);
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data;
N : Positive;
Success : access Boolean) return Unbounded_String is
begin
if N > Args_Length (Data.CL) then
Success.all := False;
return Null_Unbounded_String;
else
Success.all := True;
return Nth_Arg (Data.CL, N);
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data;
N : Positive;
Class : Class_Type;
Allow_Null : Boolean;
Success : access Boolean) return Class_Instance
is
Class_Name : constant String := Nth_Arg (Data, N, Success);
Ins : Class_Instance;
begin
if not Success.all then
return No_Class_Instance;
end if;
Ins := Instance_From_Name (Data.Script, Class_Name);
if Ins = No_Class_Instance and then Allow_Null then
return No_Class_Instance;
end if;
if Ins = No_Class_Instance
or else (Class /= Any_Class
and then not Is_Subclass (Ins, Get_Name (Class)))
then
raise Invalid_Parameter;
else
return Ins;
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data;
N : Positive;
Success : access Boolean) return Subprogram_Type
is
Name : constant String := Nth_Arg (Data, N, Success);
begin
if not Success.all then
return null;
else
return new Shell_Subprogram_Record'
(Subprogram_Record with
Script => Get_Script (Data),
Command => new String'(Name));
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive) return Boolean
is
Success : aliased Boolean;
S : constant String := Nth_Arg (Data, N, Success'Access);
begin
if Success then
return Boolean'Value (S);
else
raise No_Such_Parameter;
end if;
exception
when Constraint_Error =>
raise Invalid_Parameter;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive) return Integer
is
Success : aliased Boolean;
S : constant String := Nth_Arg (Data, N, Success'Access);
begin
if Success then
return Integer'Value (S);
else
raise No_Such_Parameter;
end if;
exception
when Constraint_Error =>
raise Invalid_Parameter;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive) return Float
is
Success : aliased Boolean;
S : constant String := Nth_Arg (Data, N, Success'Access);
begin
if Success then
return Float'Value (S);
else
raise No_Such_Parameter;
end if;
exception
when Constraint_Error =>
raise Invalid_Parameter;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive) return String
is
Success : aliased Boolean;
Result : constant String := Nth_Arg (Data, N, Success'Access);
begin
if not Success then
raise No_Such_Parameter;
else
return Result;
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive) return Unbounded_String
is
Success : aliased Boolean;
Result : constant Unbounded_String := Nth_Arg (Data, N, Success'Access);
begin
if not Success then
raise No_Such_Parameter;
else
return Result;
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive) return Subprogram_Type
is
Success : aliased Boolean;
Result : constant Subprogram_Type := Nth_Arg (Data, N, Success'Access);
begin
if not Success then
raise No_Such_Parameter;
else
return Result;
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive; Class : Class_Type;
Allow_Null : Boolean := False) return Class_Instance
is
Success : aliased Boolean;
Result : constant Class_Instance := Nth_Arg
(Data, N, Class, Allow_Null, Success'Access);
begin
if not Success then
raise No_Such_Parameter;
else
return Result;
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive; Default : String)
return String
is
Success : aliased Boolean;
Result : constant String := Nth_Arg (Data, N, Success'Access);
begin
if not Success then
return Default;
else
return Result;
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive; Default : Integer)
return Integer
is
Success : aliased Boolean;
Result : constant String := Nth_Arg (Data, N, Success'Access);
begin
if not Success then
return Default;
else
return Integer'Value (Result);
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive; Default : Float)
return Float
is
Success : aliased Boolean;
Result : constant String := Nth_Arg (Data, N, Success'Access);
begin
if not Success then
return Default;
else
return Float'Value (Result);
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data; N : Positive; Default : Boolean)
return Boolean
is
Success : aliased Boolean;
Result : constant String := Nth_Arg (Data, N, Success'Access);
begin
if not Success then
return Default;
else
return Boolean'Value (Result);
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data;
N : Positive;
Class : Class_Type := Any_Class;
Default : Class_Instance;
Allow_Null : Boolean := False) return Class_Instance
is
Success : aliased Boolean;
Result : constant Class_Instance := Nth_Arg
(Data, N, Class, Allow_Null, Success'Access);
begin
if not Success then
return Default;
else
return Result;
end if;
end Nth_Arg;
-------------
-- Nth_Arg --
-------------
function Nth_Arg
(Data : Shell_Callback_Data;
N : Positive;
Default : Subprogram_Type) return Subprogram_Type
is
Success : aliased Boolean;
Result : constant Subprogram_Type := Nth_Arg (Data, N, Success'Access);
begin
if not Success then
return Default;
else
return Result;
end if;
end Nth_Arg;
-------------------
-- Set_Error_Msg --
-------------------
procedure Set_Error_Msg (Data : in out Shell_Callback_Data; Msg : String) is
begin
Free (Data.Return_Value);
Data.Return_As_Error := True;
Data.Return_Value := new String'(Msg);
end Set_Error_Msg;
------------------------------
-- Set_Return_Value_As_List --
------------------------------
overriding procedure Set_Return_Value_As_List
(Data : in out Shell_Callback_Data;
Size : Natural := 0;
Class : Class_Type := No_Class)
is
pragma Unreferenced (Size, Class);
begin
Data.Return_As_List := True;
end Set_Return_Value_As_List;
--------------------------
-- Set_Return_Value_Key --
--------------------------
procedure Set_Return_Value_Key
(Data : in out Shell_Callback_Data;
Key : String;
Append : Boolean := False)
is
pragma Unreferenced (Append);
Tmp : GNAT.Strings.String_Access;
begin
if Data.Return_Value = null then
if Data.Return_Dict = null then
Data.Return_Dict := new String'(Key & " => ()");
else
Tmp := Data.Return_Dict;
Data.Return_Dict := new String'(Tmp.all & ", " & Key & " => ()");
Free (Tmp);
end if;
else
if Data.Return_Dict = null then
Data.Return_Dict := new String'
(Key & " => (" & Data.Return_Value.all & ')');
else
Tmp := Data.Return_Dict;
Data.Return_Dict := new String'
(Tmp.all & ", " & Key & " => (" & Data.Return_Value.all & ')');
Free (Tmp);
end if;
end if;
Data.Return_As_List := False;
Free (Data.Return_Value);
end Set_Return_Value_Key;
--------------------------
-- Set_Return_Value_Key --
--------------------------
procedure Set_Return_Value_Key
(Data : in out Shell_Callback_Data;
Key : Integer;
Append : Boolean := False) is
begin
Set_Return_Value_Key (Data, Integer'Image (Key), Append);
end Set_Return_Value_Key;
--------------------------
-- Set_Return_Value_Key --
--------------------------
procedure Set_Return_Value_Key
(Data : in out Shell_Callback_Data;
Key : Class_Instance;
Append : Boolean := False) is
begin
Set_Return_Value_Key (Data, Name_From_Instance (Get_CIR (Key)), Append);
end Set_Return_Value_Key;
----------------------
-- Set_Return_Value --
----------------------
procedure Set_Return_Value
(Data : in out Shell_Callback_Data; Value : Integer) is
begin
if not Data.Return_As_List then
Free (Data.Return_Value);
end if;
Set_Return_Value (Data, Integer'Image (Value));
end Set_Return_Value;
----------------------
-- Set_Return_Value --
----------------------
procedure Set_Return_Value
(Data : in out Shell_Callback_Data; Value : Float) is
begin
if not Data.Return_As_List then
Free (Data.Return_Value);
end if;
Set_Return_Value (Data, Float'Image (Value));
end Set_Return_Value;
----------------------
-- Set_Return_Value --
----------------------
procedure Set_Return_Value
(Data : in out Shell_Callback_Data; Value : Boolean) is
begin
if not Data.Return_As_List then
Free (Data.Return_Value);
end if;
Set_Return_Value (Data, Boolean'Image (Value));
end Set_Return_Value;
----------------------
-- Set_Return_Value --
----------------------
procedure Set_Return_Value
(Data : in out Shell_Callback_Data; Value : String)
is
Tmp : GNAT.Strings.String_Access;
begin
if Data.Return_As_List and then Data.Return_Value /= null then
Tmp := Data.Return_Value;
Data.Return_Value := new String (1 .. Tmp'Length + 1 + Value'Length);
Data.Return_Value (1 .. Tmp'Length) := Tmp.all;
Data.Return_Value (Tmp'Length + 1) := ASCII.LF;
Data.Return_Value (Tmp'Length + 2 .. Data.Return_Value'Last) := Value;
Free (Tmp);
else
Free (Data.Return_Value);
Data.Return_Value := new String'(Value);
end if;
end Set_Return_Value;
----------------------
-- Set_Return_Value --
----------------------
procedure Set_Return_Value
(Data : in out Shell_Callback_Data; Value : Class_Instance) is
begin
if Value = No_Class_Instance then
Set_Return_Value (Data, String'("null"));
else
Set_Return_Value (Data, Name_From_Instance (Get_CIR (Value)));
end if;
end Set_Return_Value;
----------------------
-- Set_Return_Value --
----------------------
overriding procedure Set_Return_Value
(Data : in out Shell_Callback_Data; Value : List_Instance) is
begin
Set_Return_Value
(Data, '(' & Get_Command (Shell_Callback_Data (Value).CL) & ')');
end Set_Return_Value;
------------------
-- New_Instance --
------------------
function New_Instance
(Script : access Shell_Scripting_Record;
Class : Class_Type) return Class_Instance
is
Instance : Shell_Class_Instance;
Inst : Class_Instance;
begin
Instance := new Shell_Class_Instance_Record;
Instance.Class := Class;
Inst := From_Instance (Script, Instance);
Instances_List.Prepend (Script.Instances, Inst);
return Inst;
end New_Instance;
----------------
-- Get_Method --
----------------
overriding function Get_Method
(Instance : access Shell_Class_Instance_Record;
Name : String) return Subprogram_Type
is
Inst_Name : constant String := Name_From_Instance (Instance);
begin
return new Shell_Subprogram_Record'
(Script => Instance.Script,
Command => new String'
(Get_Name (Instance.Class) & "." & Name & " " & Inst_Name));
end Get_Method;
--------------------
-- Print_Refcount --
--------------------
function Print_Refcount
(Instance : access Shell_Class_Instance_Record) return String
is
pragma Unreferenced (Instance);
begin
return "";
end Print_Refcount;
---------------------
-- Execute_Command --
---------------------
function Execute_Command
(Script : access Shell_Scripting_Record;
Command : String;
Args : Callback_Data'Class) return Boolean
is
Errors : aliased Boolean;
CL : Arg_List := Create (Command);
begin
for J in 1 .. Args_Length (Shell_Callback_Data (Args).CL) loop
Append_Argument
(CL, Nth_Arg (Shell_Callback_Data (Args).CL, J), One_Arg);
end loop;
declare
Result : constant String := Trim
(Execute_GPS_Shell_Command (Script, CL, Errors'Unchecked_Access),
Ada.Strings.Both);
begin
return Result = "1" or else To_Lower (Result) = "true";
end;
end Execute_Command;
-------------
-- Execute --
-------------
overriding function Execute
(Subprogram : access Shell_Subprogram_Record;
Args : Callback_Data'Class;
Error : not null access Boolean) return Boolean
is
begin
Error.all := False;
return To_Lower
(Execute (Shell_Subprogram (Subprogram), Args)) = "true";
end Execute;
-------------
-- Execute --
-------------
function Execute
(Subprogram : access Shell_Subprogram_Record;
Args : Callback_Data'Class;
Error : not null access Boolean) return String
is
D : constant Shell_Callback_Data := Shell_Callback_Data (Args);
CL : Arg_List;
begin
CL := Create (Subprogram.Command.all);
for Arg in 1 .. Args_Length (D.CL) loop
Append_Argument (CL, Nth_Arg (D.CL, Arg), One_Arg);
end loop;
return Execute_GPS_Shell_Command
(Script => Shell_Scripting (Subprogram.Script),
CL => CL,
Errors => Error);
end Execute;
-------------
-- Execute --
-------------
overriding function Execute
(Subprogram : access Shell_Subprogram_Record;
Args : Callback_Data'Class;
Error : not null access Boolean) return Class_Instance
is
Result : constant String := Execute (Subprogram, Args, Error);
begin
return Instance_From_Name (Shell_Scripting (Subprogram.Script), Result);
end Execute;
-------------
-- Execute --
-------------
overriding function Execute
(Subprogram : access Shell_Subprogram_Record;
Args : Callback_Data'Class;
Error : not null access Boolean) return List_Instance'Class
is
List : Shell_Callback_Data;
begin
List.Script := Shell_Scripting (Subprogram.Script);
List.CL := Parse_String
(Execute (Subprogram, Args, Error), Separate_Args);
return List;
end Execute;
-------------
-- Execute --
-------------
function Execute
(Subprogram : access Shell_Subprogram_Record;
Args : Callback_Data'Class;
Error : not null access Boolean) return GNAT.Strings.String_List
is
pragma Unreferenced (Subprogram, Args);
begin
Error.all := True;
-- ??? We are in asynchronous mode, see Execute for String above
return (1 .. 0 => null);
end Execute;
-------------
-- Execute --
-------------
overriding function Execute
(Subprogram : access Shell_Subprogram_Record;
Args : Callback_Data'Class;
Error : not null access Boolean) return Any_Type
is
pragma Unreferenced (Subprogram, Args);
begin
Error.all := True;
-- Any_Type is not supported for shell scripts
raise Program_Error;
return Empty_Any_Type;
end Execute;
--------------
-- Get_Name --
--------------
function Get_Name
(Subprogram : access Shell_Subprogram_Record) return String is
begin
return "command: " & Subprogram.Command.all;
end Get_Name;
----------
-- Free --
----------
procedure Free (Subprogram : in out Shell_Subprogram_Record) is
begin
Free (Subprogram.Command);
end Free;
----------------
-- Get_Script --
----------------
function Get_Script
(Subprogram : Shell_Subprogram_Record) return Scripting_Language
is
begin
return Subprogram.Script;
end Get_Script;
-----------------
-- Get_Command --
-----------------
function Get_Command
(Subprogram : access Shell_Subprogram_Record) return String is
begin
return Subprogram.Command.all;
end Get_Command;
----------------
-- Initialize --
----------------
procedure Initialize
(Subprogram : in out Shell_Subprogram_Record'Class;
Script : access Scripting_Language_Record'Class;
Command : String) is
begin
Free (Subprogram.Command);
Subprogram.Command := new String'(Command);
Subprogram.Script := Scripting_Language (Script);
end Initialize;
--------------
-- Get_Args --
--------------
function Get_Args
(Data : Shell_Callback_Data) return GNAT.OS_Lib.Argument_List is
begin
return To_List (Data.CL, False);
-- ??? There is a memory leak here. Maybe we can get rid of this
-- subprogram?
end Get_Args;
----------------------------
-- Command_Line_Treatment --
----------------------------
overriding function Command_Line_Treatment
(Script : access Shell_Scripting_Record) return Command_Line_Mode
is
pragma Unreferenced (Script);
begin
return Separate_Args;
end Command_Line_Treatment;
---------------------
-- Execute_Command --
---------------------
overriding procedure Execute_Command
(Args : in out Shell_Callback_Data;
Command : String;
Hide_Output : Boolean := True)
is
pragma Unreferenced (Hide_Output);
Script : constant Shell_Scripting := Shell_Scripting (Get_Script (Args));
Errors : aliased Boolean;
CL : Arg_List := Create (Command);
begin
for J in 1 .. Args_Length (Args.CL) loop
Append_Argument (CL, Nth_Arg (Args.CL, J), One_Arg);
end loop;
declare
Result : constant String := Trim
(Execute_GPS_Shell_Command (Script, CL, Errors'Unchecked_Access),
Ada.Strings.Both);
begin
Free (Args.Return_Value);
Args.Return_Value := new String'(Result);
end;
end Execute_Command;
------------------
-- Return_Value --
------------------
overriding function Return_Value
(Data : Shell_Callback_Data) return String is
begin
if Data.Return_Value = null then
raise Invalid_Parameter with "No return value";
else
return Data.Return_Value.all;
end if;
end Return_Value;
------------------
-- Return_Value --
------------------
overriding function Return_Value
(Data : Shell_Callback_Data) return Integer is
begin
return Integer'Value (Return_Value (Data));
end Return_Value;
------------------
-- Return_Value --
------------------
overriding function Return_Value
(Data : Shell_Callback_Data) return Float is
begin
return Float'Value (Return_Value (Data));
end Return_Value;
------------------
-- Return_Value --
------------------
overriding function Return_Value
(Data : Shell_Callback_Data) return Boolean is
begin
return Boolean'Value (Return_Value (Data));
end Return_Value;
------------------
-- Return_Value --
------------------
overriding function Return_Value
(Data : Shell_Callback_Data) return Class_Instance is
begin
return Instance_From_Name (Data.Script, Return_Value (Data));
end Return_Value;
------------------
-- Return_Value --
------------------
overriding function Return_Value
(Data : Shell_Callback_Data) return List_Instance'Class
is
List : Shell_Callback_Data;
begin
List.Script := Data.Script;
List.CL := Parse_String (Return_Value (Data), Separate_Args);
return List;
end Return_Value;
-------------------
-- Get_User_Data --
-------------------
overriding function Get_User_Data
(Self : not null access Shell_Class_Instance_Record)
return access User_Data_List is
begin
return Self.Props'Access;
end Get_User_Data;
end GNATCOLL.Scripts.Shell;
|