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
|
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
Module: $RCSfile: cmSystemTools.cxx,v $
Language: C++
Date: $Date: 2006/10/27 20:01:48 $
Version: $Revision: 1.327.2.3 $
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "cmSystemTools.h"
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <cmsys/RegularExpression.hxx>
#include <cmsys/Directory.hxx>
// support for realpath call
#ifndef _WIN32
#include <limits.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/wait.h>
#endif
#if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__))
#include <string.h>
#include <windows.h>
#include <direct.h>
#include <io.h>
#define _unlink unlink
#else
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include <sys/stat.h>
#if defined(CMAKE_BUILD_WITH_CMAKE)
# include <libtar/libtar.h>
# include <memory> // auto_ptr
# include <fcntl.h>
# include <cm_zlib.h>
#endif
#if defined(__sgi) && !defined(__GNUC__)
# pragma set woff 1375 /* base class destructor not virtual */
#endif
#if !defined(HAVE_ENVIRON_NOT_REQUIRE_PROTOTYPE)
// For GetEnvironmentVariables
# if defined(_WIN32)
extern __declspec( dllimport ) char** environ;
# else
extern char** environ;
# endif
#endif
bool cmSystemTools::s_RunCommandHideConsole = false;
bool cmSystemTools::s_DisableRunCommandOutput = false;
bool cmSystemTools::s_ErrorOccured = false;
bool cmSystemTools::s_FatalErrorOccured = false;
bool cmSystemTools::s_DisableMessages = false;
bool cmSystemTools::s_ForceUnixPaths = false;
std::string cmSystemTools::s_Windows9xComspecSubstitute = "command.com";
void cmSystemTools::SetWindows9xComspecSubstitute(const char* str)
{
if ( str )
{
cmSystemTools::s_Windows9xComspecSubstitute = str;
}
}
const char* cmSystemTools::GetWindows9xComspecSubstitute()
{
return cmSystemTools::s_Windows9xComspecSubstitute.c_str();
}
void (*cmSystemTools::s_ErrorCallback)(const char*, const char*,
bool&, void*);
void (*cmSystemTools::s_StdoutCallback)(const char*, int len, void*);
void* cmSystemTools::s_ErrorCallbackClientData = 0;
void* cmSystemTools::s_StdoutCallbackClientData = 0;
// replace replace with with as many times as it shows up in source.
// write the result into source.
#if defined(_WIN32) && !defined(__CYGWIN__)
void cmSystemTools::ExpandRegistryValues(std::string& source)
{
// Regular expression to match anything inside [...] that begins in HKEY.
// Note that there is a special rule for regular expressions to match a
// close square-bracket inside a list delimited by square brackets.
// The "[^]]" part of this expression will match any character except
// a close square-bracket. The ']' character must be the first in the
// list of characters inside the [^...] block of the expression.
cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
// check for black line or comment
while (regEntry.find(source))
{
// the arguments are the second match
std::string key = regEntry.match(1);
std::string val;
if (ReadRegistryValue(key.c_str(), val))
{
std::string reg = "[";
reg += key + "]";
cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
}
else
{
std::string reg = "[";
reg += key + "]";
cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
}
}
}
#else
void cmSystemTools::ExpandRegistryValues(std::string&)
{
}
#endif
std::string cmSystemTools::EscapeQuotes(const char* str)
{
std::string result = "";
for(const char* ch = str; *ch != '\0'; ++ch)
{
if(*ch == '"')
{
result += '\\';
}
result += *ch;
}
return result;
}
std::string cmSystemTools::EscapeSpaces(const char* str)
{
#if defined(_WIN32) && !defined(__CYGWIN__)
bool useDoubleQ = true;
#else
bool useDoubleQ = false;
#endif
if(cmSystemTools::s_ForceUnixPaths)
{
useDoubleQ = false;
}
if(useDoubleQ)
{
std::string result;
// if there are spaces
std::string temp = str;
if (temp.find(" ") != std::string::npos &&
temp.find("\"")==std::string::npos)
{
result = "\"";
result += str;
result += "\"";
return result;
}
return str;
}
else
{
std::string result = "";
for(const char* ch = str; *ch != '\0'; ++ch)
{
if(*ch == ' ')
{
result += '\\';
}
result += *ch;
}
return result;
}
}
std::string cmSystemTools::RemoveEscapes(const char* s)
{
std::string result = "";
for(const char* ch = s; *ch; ++ch)
{
if(*ch == '\\' && *(ch+1) != ';')
{
++ch;
switch (*ch)
{
case '\\': result.insert(result.end(), '\\'); break;
case '"': result.insert(result.end(), '"'); break;
case ' ': result.insert(result.end(), ' '); break;
case 't': result.insert(result.end(), '\t'); break;
case 'n': result.insert(result.end(), '\n'); break;
case 'r': result.insert(result.end(), '\r'); break;
case '#': result.insert(result.end(), '#'); break;
case '(': result.insert(result.end(), '('); break;
case ')': result.insert(result.end(), ')'); break;
case '0': result.insert(result.end(), '\0'); break;
case '\0':
{
cmSystemTools::Error("Trailing backslash in argument:\n", s);
return result;
}
default:
{
std::string chStr(1, *ch);
cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
"\nin argument ", s);
}
}
}
else
{
result.insert(result.end(), *ch);
}
}
return result;
}
void cmSystemTools::Error(const char* m1, const char* m2,
const char* m3, const char* m4)
{
std::string message = "CMake Error: ";
if(m1)
{
message += m1;
}
if(m2)
{
message += m2;
}
if(m3)
{
message += m3;
}
if(m4)
{
message += m4;
}
cmSystemTools::s_ErrorOccured = true;
cmSystemTools::Message(message.c_str(),"Error");
}
void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
{
s_ErrorCallback = f;
s_ErrorCallbackClientData = clientData;
}
void cmSystemTools::SetStdoutCallback(StdoutCallback f, void* clientData)
{
s_StdoutCallback = f;
s_StdoutCallbackClientData = clientData;
}
void cmSystemTools::Stdout(const char* s)
{
if(s_StdoutCallback)
{
(*s_StdoutCallback)(s, strlen(s), s_StdoutCallbackClientData);
}
else
{
std::cout << s;
std::cout.flush();
}
}
void cmSystemTools::Stdout(const char* s, int length)
{
if(s_StdoutCallback)
{
(*s_StdoutCallback)(s, length, s_StdoutCallbackClientData);
}
else
{
std::cout.write(s, length);
std::cout.flush();
}
}
void cmSystemTools::Message(const char* m1, const char *title)
{
if(s_DisableMessages)
{
return;
}
if(s_ErrorCallback)
{
(*s_ErrorCallback)(m1, title, s_DisableMessages,
s_ErrorCallbackClientData);
return;
}
else
{
std::cerr << m1 << std::endl << std::flush;
}
}
void cmSystemTools::ReportLastSystemError(const char* msg)
{
std::string m = msg;
m += ": System Error: ";
m += Superclass::GetLastSystemError();
cmSystemTools::Error(m.c_str());
}
bool cmSystemTools::IsOn(const char* val)
{
if (!val)
{
return false;
}
std::basic_string<char> v = val;
for(std::basic_string<char>::iterator c = v.begin();
c != v.end(); c++)
{
*c = toupper(*c);
}
return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
}
bool cmSystemTools::IsNOTFOUND(const char* val)
{
int len = strlen(val);
const char* notfound = "-NOTFOUND";
const int lenNotFound = 9;
if(len < lenNotFound-1)
{
return false;
}
if(len == lenNotFound-1)
{
return ( strcmp(val, "NOTFOUND") == 0);
}
return ((strncmp((val + (len - lenNotFound)), notfound, lenNotFound) == 0));
}
bool cmSystemTools::IsOff(const char* val)
{
if (!val || strlen(val) == 0)
{
return true;
}
std::basic_string<char> v = val;
for(std::basic_string<char>::iterator c = v.begin();
c != v.end(); c++)
{
*c = toupper(*c);
}
return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
v == "N" || cmSystemTools::IsNOTFOUND(v.c_str()) || v == "IGNORE");
}
std::vector<cmStdString> cmSystemTools::ParseArguments(const char* command)
{
std::vector<cmStdString> args;
std::string arg;
bool win_path = false;
if ( command[0] != '/' && command[1] == ':' && command[2] == '\\' ||
command[0] == '\"' && command[1] != '/' && command[2] == ':'
&& command[3] == '\\' ||
command[0] == '\'' && command[1] != '/' && command[2] == ':'
&& command[3] == '\\' ||
command[0] == '\\' && command[1] == '\\')
{
win_path = true;
}
// Split the command into an argv array.
for(const char* c = command; *c;)
{
// Skip over whitespace.
while(*c == ' ' || *c == '\t')
{
++c;
}
arg = "";
if(*c == '"')
{
// Parse a quoted argument.
++c;
while(*c && *c != '"')
{
arg.append(1, *c);
++c;
}
if(*c)
{
++c;
}
args.push_back(arg);
}
else if(*c == '\'')
{
// Parse a quoted argument.
++c;
while(*c && *c != '\'')
{
arg.append(1, *c);
++c;
}
if(*c)
{
++c;
}
args.push_back(arg);
}
else if(*c)
{
// Parse an unquoted argument.
while(*c && *c != ' ' && *c != '\t')
{
if(*c == '\\' && !win_path)
{
++c;
if(*c)
{
arg.append(1, *c);
++c;
}
}
else
{
arg.append(1, *c);
++c;
}
}
args.push_back(arg);
}
}
return args;
}
bool cmSystemTools::RunSingleCommand(
const char* command,
std::string* output,
int *retVal,
const char* dir,
bool verbose,
double timeout)
{
if(s_DisableRunCommandOutput)
{
verbose = false;
}
std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
if(args.size() < 1)
{
return false;
}
std::vector<const char*> argv;
for(std::vector<cmStdString>::const_iterator a = args.begin();
a != args.end(); ++a)
{
argv.push_back(a->c_str());
}
argv.push_back(0);
if ( output )
{
*output = "";
}
cmsysProcess* cp = cmsysProcess_New();
cmsysProcess_SetCommand(cp, &*argv.begin());
cmsysProcess_SetWorkingDirectory(cp, dir);
if(cmSystemTools::GetRunCommandHideConsole())
{
cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
}
cmsysProcess_SetTimeout(cp, timeout);
cmsysProcess_Execute(cp);
std::vector<char> tempOutput;
char* data;
int length;
if ( output || verbose )
{
while(cmsysProcess_WaitForData(cp, &data, &length, 0))
{
if(output || verbose)
{
// Translate NULL characters in the output into valid text.
// Visual Studio 7 puts these characters in the output of its
// build process.
for(int i=0; i < length; ++i)
{
if(data[i] == '\0')
{
data[i] = ' ';
}
}
}
if ( output )
{
tempOutput.insert(tempOutput.end(), data, data+length);
}
if(verbose)
{
cmSystemTools::Stdout(data, length);
}
}
}
cmsysProcess_WaitForExit(cp, 0);
if ( output && tempOutput.begin() != tempOutput.end())
{
output->append(&*tempOutput.begin(), tempOutput.size());
}
bool result = true;
if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
{
if ( retVal )
{
*retVal = cmsysProcess_GetExitValue(cp);
}
else
{
if ( cmsysProcess_GetExitValue(cp) != 0 )
{
result = false;
}
}
}
else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
{
const char* exception_str = cmsysProcess_GetExceptionString(cp);
if ( verbose )
{
std::cerr << exception_str << std::endl;
}
if ( output )
{
output->append(exception_str, strlen(exception_str));
}
result = false;
}
else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
{
const char* error_str = cmsysProcess_GetErrorString(cp);
if ( verbose )
{
std::cerr << error_str << std::endl;
}
if ( output )
{
output->append(error_str, strlen(error_str));
}
result = false;
}
else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
{
const char* error_str = "Process terminated due to timeout\n";
if ( verbose )
{
std::cerr << error_str << std::endl;
}
if ( output )
{
output->append(error_str, strlen(error_str));
}
result = false;
}
cmsysProcess_Delete(cp);
return result;
}
bool cmSystemTools::RunCommand(const char* command,
std::string& output,
const char* dir,
bool verbose,
int timeout)
{
int dummy;
return cmSystemTools::RunCommand(command, output, dummy,
dir, verbose, timeout);
}
#if defined(WIN32) && !defined(__CYGWIN__)
#include "cmWin32ProcessExecution.h"
// use this for shell commands like echo and dir
bool RunCommandViaWin32(const char* command,
const char* dir,
std::string& output,
int& retVal,
bool verbose,
int timeout)
{
#if defined(__BORLANDC__)
return
cmWin32ProcessExecution::
BorlandRunCommand(command, dir, output,
retVal,
verbose, timeout,
cmSystemTools::GetRunCommandHideConsole());
#else // Visual studio
::SetLastError(ERROR_SUCCESS);
if ( ! command )
{
cmSystemTools::Error("No command specified");
return false;
}
cmWin32ProcessExecution resProc;
if(cmSystemTools::GetRunCommandHideConsole())
{
resProc.SetHideWindows(true);
}
if ( cmSystemTools::GetWindows9xComspecSubstitute() )
{
resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
}
if ( !resProc.StartProcess(command, dir, verbose) )
{
output = resProc.GetOutput();
if(verbose)
{
cmSystemTools::Stdout(output.c_str());
}
return false;
}
resProc.Wait(timeout);
output = resProc.GetOutput();
retVal = resProc.GetExitValue();
return true;
#endif
}
// use this for shell commands like echo and dir
bool RunCommandViaSystem(const char* command,
const char* dir,
std::string& output,
int& retVal,
bool verbose)
{
std::cout << "@@ " << command << std::endl;
std::string commandInDir;
if(dir)
{
commandInDir = "cd ";
commandInDir += cmSystemTools::ConvertToOutputPath(dir);
commandInDir += " && ";
commandInDir += command;
}
else
{
commandInDir = command;
}
command = commandInDir.c_str();
std::string commandToFile = command;
commandToFile += " > ";
std::string tempFile;
tempFile += _tempnam(0, "cmake");
commandToFile += tempFile;
retVal = system(commandToFile.c_str());
std::ifstream fin(tempFile.c_str());
if(!fin)
{
if(verbose)
{
std::string errormsg = "RunCommand produced no output: command: \"";
errormsg += command;
errormsg += "\"";
errormsg += "\nOutput file: ";
errormsg += tempFile;
cmSystemTools::Error(errormsg.c_str());
}
fin.close();
cmSystemTools::RemoveFile(tempFile.c_str());
return false;
}
bool multiLine = false;
std::string line;
while(cmSystemTools::GetLineFromStream(fin, line))
{
output += line;
if(multiLine)
{
output += "\n";
}
multiLine = true;
}
fin.close();
cmSystemTools::RemoveFile(tempFile.c_str());
return true;
}
#else // We have popen
bool RunCommandViaPopen(const char* command,
const char* dir,
std::string& output,
int& retVal,
bool verbose,
int /*timeout*/)
{
// if only popen worked on windows.....
std::string commandInDir;
if(dir)
{
commandInDir = "cd \"";
commandInDir += dir;
commandInDir += "\" && ";
commandInDir += command;
}
else
{
commandInDir = command;
}
commandInDir += " 2>&1";
command = commandInDir.c_str();
const int BUFFER_SIZE = 4096;
char buffer[BUFFER_SIZE];
if(verbose)
{
cmSystemTools::Stdout("running ");
cmSystemTools::Stdout(command);
cmSystemTools::Stdout("\n");
}
fflush(stdout);
fflush(stderr);
FILE* cpipe = popen(command, "r");
if(!cpipe)
{
return false;
}
fgets(buffer, BUFFER_SIZE, cpipe);
while(!feof(cpipe))
{
if(verbose)
{
cmSystemTools::Stdout(buffer);
}
output += buffer;
fgets(buffer, BUFFER_SIZE, cpipe);
}
retVal = pclose(cpipe);
if (WIFEXITED(retVal))
{
retVal = WEXITSTATUS(retVal);
return true;
}
if (WIFSIGNALED(retVal))
{
retVal = WTERMSIG(retVal);
cmOStringStream error;
error << "\nProcess terminated due to ";
switch (retVal)
{
#ifdef SIGKILL
case SIGKILL:
error << "SIGKILL";
break;
#endif
#ifdef SIGFPE
case SIGFPE:
error << "SIGFPE";
break;
#endif
#ifdef SIGBUS
case SIGBUS:
error << "SIGBUS";
break;
#endif
#ifdef SIGSEGV
case SIGSEGV:
error << "SIGSEGV";
break;
#endif
default:
error << "signal " << retVal;
break;
}
output += error.str();
}
return false;
}
#endif // endif WIN32 not CYGWIN
// run a command unix uses popen (easy)
// windows uses system and ShortPath
bool cmSystemTools::RunCommand(const char* command,
std::string& output,
int &retVal,
const char* dir,
bool verbose,
int timeout)
{
if(s_DisableRunCommandOutput)
{
verbose = false;
}
#if defined(WIN32) && !defined(__CYGWIN__)
// if the command does not start with a quote, then
// try to find the program, and if the program can not be
// found use system to run the command as it must be a built in
// shell command like echo or dir
int count = 0;
if(command[0] == '\"')
{
// count the number of quotes
for(const char* s = command; *s != 0; ++s)
{
if(*s == '\"')
{
count++;
if(count > 2)
{
break;
}
}
}
// if there are more than two double quotes use
// GetShortPathName, the cmd.exe program in windows which
// is used by system fails to execute if there are more than
// one set of quotes in the arguments
if(count > 2)
{
cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
if(quoted.find(command))
{
std::string shortCmd;
std::string cmd = quoted.match(1);
std::string args = quoted.match(2);
if(! cmSystemTools::FileExists(cmd.c_str()) )
{
shortCmd = cmd;
}
else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
{
cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
return false;
}
shortCmd += " ";
shortCmd += args;
//return RunCommandViaSystem(shortCmd.c_str(), dir,
// output, retVal, verbose);
//return WindowsRunCommand(shortCmd.c_str(), dir,
//output, retVal, verbose);
return RunCommandViaWin32(shortCmd.c_str(), dir,
output, retVal, verbose, timeout);
}
else
{
cmSystemTools::Error("Could not parse command line with quotes ",
command);
}
}
}
// if there is only one set of quotes or no quotes then just run the command
//return RunCommandViaSystem(command, dir, output, retVal, verbose);
//return WindowsRunCommand(command, dir, output, retVal, verbose);
return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
#else
return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
#endif
}
bool cmSystemTools::DoesFileExistWithExtensions(
const char* name,
const std::vector<std::string>& headerExts)
{
std::string hname;
for( std::vector<std::string>::const_iterator ext = headerExts.begin();
ext != headerExts.end(); ++ext )
{
hname = name;
hname += ".";
hname += *ext;
if(cmSystemTools::FileExists(hname.c_str()))
{
return true;
}
}
return false;
}
bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
{
return Superclass::CopyFileAlways(source, destination);
}
bool cmSystemTools::CopyFileIfDifferent(const char* source,
const char* destination)
{
return Superclass::CopyFileIfDifferent(source, destination);
}
void cmSystemTools::Glob(const char *directory, const char *regexp,
std::vector<std::string>& files)
{
cmsys::Directory d;
cmsys::RegularExpression reg(regexp);
if (d.Load(directory))
{
size_t numf;
unsigned int i;
numf = d.GetNumberOfFiles();
for (i = 0; i < numf; i++)
{
std::string fname = d.GetFile(i);
if (reg.find(fname))
{
files.push_back(fname);
}
}
}
}
void cmSystemTools::GlobDirs(const char *fullPath,
std::vector<std::string>& files)
{
std::string path = fullPath;
std::string::size_type pos = path.find("/*");
if(pos == std::string::npos)
{
files.push_back(fullPath);
return;
}
std::string startPath = path.substr(0, pos);
std::string finishPath = path.substr(pos+2);
cmsys::Directory d;
if (d.Load(startPath.c_str()))
{
for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
{
if((std::string(d.GetFile(i)) != ".")
&& (std::string(d.GetFile(i)) != ".."))
{
std::string fname = startPath;
fname +="/";
fname += d.GetFile(i);
if(cmSystemTools::FileIsDirectory(fname.c_str()))
{
fname += finishPath;
cmSystemTools::GlobDirs(fname.c_str(), files);
}
}
}
}
}
void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
std::vector<std::string>& newargs)
{
std::vector<std::string>::const_iterator i;
for(i = arguments.begin();i != arguments.end(); ++i)
{
cmSystemTools::ExpandListArgument(*i, newargs);
}
}
void cmSystemTools::ExpandListArgument(const std::string& arg,
std::vector<std::string>& newargs,
bool emptyArgs)
{
// If argument is empty, it is an empty list.
if(arg.length() == 0 && !emptyArgs)
{
return;
}
// if there are no ; in the name then just copy the current string
if(arg.find(';') == std::string::npos)
{
newargs.push_back(arg);
return;
}
std::vector<char> newArgVec;
// Break the string at non-escaped semicolons not nested in [].
int squareNesting = 0;
for(const char* c = arg.c_str(); *c; ++c)
{
switch(*c)
{
case '\\':
{
// We only want to allow escaping of semicolons. Other
// escapes should not be processed here.
++c;
if(*c == ';')
{
newArgVec.push_back(*c);
}
else
{
newArgVec.push_back('\\');
if(*c)
{
newArgVec.push_back(*c);
}
}
} break;
case '[':
{
++squareNesting;
newArgVec.push_back(*c);
} break;
case ']':
{
--squareNesting;
newArgVec.push_back(*c);
} break;
case ';':
{
// Break the string here if we are not nested inside square
// brackets.
if(squareNesting == 0)
{
if ( newArgVec.size() || emptyArgs )
{
// Add the last argument if the string is not empty.
newArgVec.push_back(0);
newargs.push_back(&*newArgVec.begin());
newArgVec.clear();
}
}
else
{
newArgVec.push_back(*c);
}
} break;
default:
{
// Just append this character.
newArgVec.push_back(*c);
} break;
}
}
if ( newArgVec.size() || emptyArgs )
{
// Add the last argument if the string is not empty.
newArgVec.push_back(0);
newargs.push_back(&*newArgVec.begin());
}
}
bool cmSystemTools::SimpleGlob(const cmStdString& glob,
std::vector<cmStdString>& files,
int type /* = 0 */)
{
files.clear();
if ( glob[glob.size()-1] != '*' )
{
return false;
}
std::string path = cmSystemTools::GetFilenamePath(glob);
std::string ppath = cmSystemTools::GetFilenameName(glob);
ppath = ppath.substr(0, ppath.size()-1);
if ( path.size() == 0 )
{
path = "/";
}
bool res = false;
cmsys::Directory d;
if (d.Load(path.c_str()))
{
for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
{
if((std::string(d.GetFile(i)) != ".")
&& (std::string(d.GetFile(i)) != ".."))
{
std::string fname = path;
if ( path[path.size()-1] != '/' )
{
fname +="/";
}
fname += d.GetFile(i);
std::string sfname = d.GetFile(i);
if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
{
continue;
}
if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
{
continue;
}
if ( sfname.size() >= ppath.size() &&
sfname.substr(0, ppath.size()) ==
ppath )
{
files.push_back(fname);
res = true;
}
}
}
}
return res;
}
cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
{
if ( ! cext || *cext == 0 )
{
return cmSystemTools::NO_FILE_FORMAT;
}
//std::string ext = cmSystemTools::LowerCase(cext);
std::string ext = cext;
if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
if (
ext == "C" || ext == ".C" ||
ext == "M" || ext == ".M" ||
ext == "c++" || ext == ".c++" ||
ext == "cc" || ext == ".cc" ||
ext == "cpp" || ext == ".cpp" ||
ext == "cxx" || ext == ".cxx" ||
ext == "m" || ext == ".m" ||
ext == "mm" || ext == ".mm"
) { return cmSystemTools::CXX_FILE_FORMAT; }
if (
ext == "f" || ext == ".f" ||
ext == "F" || ext == ".F" ||
ext == "f77" || ext == ".f77" ||
ext == "f90" || ext == ".f90" ||
ext == "for" || ext == ".for" ||
ext == "f95" || ext == ".f95"
) { return cmSystemTools::FORTRAN_FILE_FORMAT; }
if ( ext == "java" || ext == ".java" )
{ return cmSystemTools::JAVA_FILE_FORMAT; }
if (
ext == "H" || ext == ".H" ||
ext == "h" || ext == ".h" ||
ext == "h++" || ext == ".h++" ||
ext == "hm" || ext == ".hm" ||
ext == "hpp" || ext == ".hpp" ||
ext == "hxx" || ext == ".hxx" ||
ext == "in" || ext == ".in" ||
ext == "txx" || ext == ".txx"
) { return cmSystemTools::HEADER_FILE_FORMAT; }
if ( ext == "rc" || ext == ".rc" )
{ return cmSystemTools::RESOURCE_FILE_FORMAT; }
if ( ext == "def" || ext == ".def" )
{ return cmSystemTools::DEFINITION_FILE_FORMAT; }
if ( ext == "lib" || ext == ".lib" ||
ext == "a" || ext == ".a")
{ return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
if ( ext == "o" || ext == ".o" ||
ext == "obj" || ext == ".obj")
{ return cmSystemTools::OBJECT_FILE_FORMAT; }
#ifdef __APPLE__
if ( ext == "dylib" || ext == ".dylib" )
{ return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
if ( ext == "so" || ext == ".so" ||
ext == "bundle" || ext == ".bundle" )
{ return cmSystemTools::MODULE_FILE_FORMAT; }
#else // __APPLE__
if ( ext == "so" || ext == ".so" ||
ext == "sl" || ext == ".sl" ||
ext == "dll" || ext == ".dll" )
{ return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
#endif // __APPLE__
return cmSystemTools::UNKNOWN_FILE_FORMAT;
}
bool cmSystemTools::Split(const char* s, std::vector<cmStdString>& l)
{
std::vector<std::string> temp;
bool res = Superclass::Split(s, temp);
for(std::vector<std::string>::const_iterator i = temp.begin();
i != temp.end(); ++i)
{
l.push_back(*i);
}
return res;
}
std::string cmSystemTools::ConvertToOutputPath(const char* path)
{
#if defined(_WIN32) && !defined(__CYGWIN__)
if(s_ForceUnixPaths)
{
return cmSystemTools::ConvertToUnixOutputPath(path);
}
return cmSystemTools::ConvertToWindowsOutputPath(path);
#else
return cmSystemTools::ConvertToUnixOutputPath(path);
#endif
}
std::string cmSystemTools::ConvertToRunCommandPath(const char* path)
{
#if defined(_WIN32) && !defined(__CYGWIN__)
return cmSystemTools::ConvertToWindowsOutputPath(path);
#else
return cmSystemTools::ConvertToUnixOutputPath(path);
#endif
}
bool cmSystemTools::StringEndsWith(const char* str1, const char* str2)
{
if ( !str1 || !str2 || strlen(str1) < strlen(str2) )
{
return 0;
}
return !strncmp(str1 + (strlen(str1)-strlen(str2)), str2, strlen(str2));
}
#if defined(_WIN32) && !defined(__CYGWIN__)
bool cmSystemTools::CreateSymlink(const char*, const char*)
{
// Should we create a copy here?
return false;
}
#else
bool cmSystemTools::CreateSymlink(const char* origName, const char* newName)
{
return (symlink(origName, newName) >= 0);
}
#endif
// compute the relative path from here to there
std::string cmSystemTools::RelativePath(const char* local, const char* remote)
{
if(!cmSystemTools::FileIsFullPath(local))
{
cmSystemTools::Error("RelativePath must be passed a full path to local: ",
local);
}
if(!cmSystemTools::FileIsFullPath(remote))
{
cmSystemTools::Error
("RelativePath must be passed a full path to remote: ", remote);
}
return cmsys::SystemTools::RelativePath(local, remote);
}
class cmDeletingCharVector : public std::vector<char*>
{
public:
~cmDeletingCharVector()
{
for(std::vector<char*>::iterator i = this->begin();
i != this->end(); ++i)
{
delete []*i;
}
}
};
bool cmSystemTools::PutEnv(const char* value)
{
static cmDeletingCharVector localEnvironment;
char* envVar = new char[strlen(value)+1];
strcpy(envVar, value);
int ret = putenv(envVar);
// save the pointer in the static vector so that it can
// be deleted on exit
localEnvironment.push_back(envVar);
return ret == 0;
}
#ifdef CMAKE_BUILD_WITH_CMAKE
bool cmSystemTools::UnsetEnv(const char* value)
{
#if !defined(HAVE_UNSETENV)
std::string var = value;
var += "=";
return cmSystemTools::PutEnv(var.c_str());
#else
unsetenv(value);
return true;
#endif
}
std::vector<std::string> cmSystemTools::GetEnvironmentVariables()
{
std::vector<std::string> env;
int cc;
for ( cc = 0; environ[cc]; ++ cc )
{
env.push_back(environ[cc]);
}
return env;
}
#endif
void cmSystemTools::EnableVSConsoleOutput()
{
// Visual Studio 8 2005 (devenv.exe or VCExpress.exe) will not
// display output to the console unless this environment variable is
// set. We need it to capture the output of these build tools.
// Note for future work that one could pass "/out \\.\pipe\NAME" to
// either of these executables where NAME is created with
// CreateNamedPipe. This would bypass the internal buffering of the
// output and allow it to be captured on the fly.
#ifdef _WIN32
cmSystemTools::PutEnv("vsconsoleoutput=1");
#endif
}
std::string cmSystemTools::MakeXMLSafe(const char* str)
{
std::vector<char> result;
result.reserve(500);
const char* pos = str;
for ( ;*pos; ++pos)
{
char ch = *pos;
if ( (ch > 126 || ch < 32) && ch != 9 && ch != 10 && ch != 13
&& ch != '\r' )
{
char buffer[33];
sprintf(buffer, "<%d>", static_cast<int>(ch));
//sprintf(buffer, "&#x%0x;", (unsigned int)ch);
result.insert(result.end(), buffer, buffer+strlen(buffer));
}
else
{
const char* const encodedChars[] = {
"&",
"<",
">"
};
switch ( ch )
{
case '&':
result.insert(result.end(), encodedChars[0], encodedChars[0]+5);
break;
case '<':
result.insert(result.end(), encodedChars[1], encodedChars[1]+4);
break;
case '>':
result.insert(result.end(), encodedChars[2], encodedChars[2]+4);
break;
case '\n':
result.push_back('\n');
break;
case '\r': break; // Ignore \r
default:
result.push_back(ch);
}
}
}
if ( result.size() == 0 )
{
return "";
}
return std::string(&*result.begin(), result.size());
}
bool cmSystemTools::IsPathToFramework(const char* path)
{
if(cmSystemTools::FileIsFullPath(path))
{
std::string libname = path;
if(libname.find(".framework") == libname.size()+1-sizeof(".framework"))
{
return true;
}
}
return false;
}
#if defined(CMAKE_BUILD_WITH_CMAKE)
struct cmSystemToolsGZStruct
{
gzFile GZFile;
};
extern "C" {
int cmSystemToolsGZStructOpen(void* call_data, const char *pathname,
int oflags, mode_t mode);
int cmSystemToolsGZStructClose(void* call_data);
ssize_t cmSystemToolsGZStructRead(void* call_data, void* buf, size_t count);
ssize_t cmSystemToolsGZStructWrite(void* call_data, const void* buf,
size_t count);
}
int cmSystemToolsGZStructOpen(void* call_data, const char *pathname,
int oflags, mode_t mode)
{
const char *gzoflags;
int fd;
cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
switch (oflags & O_ACCMODE)
{
case O_WRONLY:
gzoflags = "wb";
break;
case O_RDONLY:
gzoflags = "rb";
break;
default:
case O_RDWR:
errno = EINVAL;
return -1;
}
fd = open(pathname, oflags, mode);
if (fd == -1)
{
return -1;
}
#if !defined(_WIN32) || defined(__CYGWIN__)
if ((oflags & O_CREAT) && fchmod(fd, mode))
{
return -1;
}
#endif
gzf->GZFile = gzdopen(fd, gzoflags);
if (!gzf->GZFile)
{
errno = ENOMEM;
return -1;
}
return fd;
}
int cmSystemToolsGZStructClose(void* call_data)
{
cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
return gzclose(gzf->GZFile);
}
ssize_t cmSystemToolsGZStructRead(void* call_data, void* buf, size_t count)
{
cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
return gzread(gzf->GZFile, buf, count);
}
ssize_t cmSystemToolsGZStructWrite(void* call_data, const void* buf,
size_t count)
{
cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
return gzwrite(gzf->GZFile, (void*)buf, count);
}
#endif
bool cmSystemTools::CreateTar(const char* outFileName,
const std::vector<cmStdString>& files,
bool gzip, bool verbose)
{
#if defined(CMAKE_BUILD_WITH_CMAKE)
TAR *t;
char buf[TAR_MAXPATHLEN];
char pathname[TAR_MAXPATHLEN];
cmSystemToolsGZStruct gzs;
tartype_t gztype = {
(openfunc_t)cmSystemToolsGZStructOpen,
(closefunc_t)cmSystemToolsGZStructClose,
(readfunc_t)cmSystemToolsGZStructRead,
(writefunc_t)cmSystemToolsGZStructWrite,
&gzs
};
// Ok, this libtar is not const safe. for now use auto_ptr hack
char* realName = new char[ strlen(outFileName) + 1 ];
std::auto_ptr<char> realNamePtr(realName);
strcpy(realName, outFileName);
if (tar_open(&t, realName,
(gzip? &gztype : NULL),
O_WRONLY | O_CREAT, 0644,
(verbose?TAR_VERBOSE:0)
| 0) == -1)
{
cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
return false;
}
std::vector<cmStdString>::const_iterator it;
for (it = files.begin(); it != files.end(); ++ it )
{
strncpy(pathname, it->c_str(), sizeof(pathname));
pathname[sizeof(pathname)-1] = 0;
strncpy(buf, pathname, sizeof(buf));
buf[sizeof(buf)-1] = 0;
if (tar_append_tree(t, buf, pathname) != 0)
{
cmOStringStream ostr;
ostr << "Problem with tar_append_tree(\"" << buf << "\", \""
<< pathname << "\"): "
<< strerror(errno);
cmSystemTools::Error(ostr.str().c_str());
tar_close(t);
return false;
}
}
if (tar_append_eof(t) != 0)
{
cmSystemTools::Error("Problem with tar_append_eof(): ", strerror(errno));
tar_close(t);
return false;
}
if (tar_close(t) != 0)
{
cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
return false;
}
return true;
#else
(void)outFileName;
(void)files;
(void)gzip;
(void)verbose;
return false;
#endif
}
bool cmSystemTools::ExtractTar(const char* outFileName,
const std::vector<cmStdString>& files,
bool gzip, bool verbose)
{
(void)files;
#if defined(CMAKE_BUILD_WITH_CMAKE)
TAR *t;
cmSystemToolsGZStruct gzs;
tartype_t gztype = {
cmSystemToolsGZStructOpen,
cmSystemToolsGZStructClose,
cmSystemToolsGZStructRead,
cmSystemToolsGZStructWrite,
&gzs
};
// Ok, this libtar is not const safe. for now use auto_ptr hack
char* realName = new char[ strlen(outFileName) + 1 ];
std::auto_ptr<char> realNamePtr(realName);
strcpy(realName, outFileName);
if (tar_open(&t, realName,
(gzip? &gztype : NULL),
O_RDONLY
#ifdef _WIN32
| O_BINARY
#endif
, 0,
(verbose?TAR_VERBOSE:0)
| 0) == -1)
{
cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
return false;
}
if (tar_extract_all(t, 0) != 0)
{
cmSystemTools::Error("Problem with tar_extract_all(): ", strerror(errno));
return false;
}
if (tar_close(t) != 0)
{
cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
return false;
}
return true;
#else
(void)outFileName;
(void)gzip;
(void)verbose;
return false;
#endif
}
bool cmSystemTools::ListTar(const char* outFileName,
std::vector<cmStdString>& files, bool gzip,
bool verbose)
{
#if defined(CMAKE_BUILD_WITH_CMAKE)
TAR *t;
cmSystemToolsGZStruct gzs;
tartype_t gztype = {
cmSystemToolsGZStructOpen,
cmSystemToolsGZStructClose,
cmSystemToolsGZStructRead,
cmSystemToolsGZStructWrite,
&gzs
};
// Ok, this libtar is not const safe. for now use auto_ptr hack
char* realName = new char[ strlen(outFileName) + 1 ];
std::auto_ptr<char> realNamePtr(realName);
strcpy(realName, outFileName);
if (tar_open(&t, realName,
(gzip? &gztype : NULL),
O_RDONLY
#ifdef _WIN32
| O_BINARY
#endif
, 0,
(verbose?TAR_VERBOSE:0)
| 0) == -1)
{
cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
return false;
}
while ((th_read(t)) == 0)
{
const char* filename = th_get_pathname(t);
files.push_back(filename);
if ( verbose )
{
th_print_long_ls(t);
}
else
{
std::cout << filename << std::endl;
}
#ifdef DEBUG
th_print(t);
#endif
if (TH_ISREG(t) && tar_skip_regfile(t) != 0)
{
cmSystemTools::Error("Problem with tar_skip_regfile(): ",
strerror(errno));
return false;
}
}
if (tar_close(t) != 0)
{
cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
return false;
}
return true;
#else
(void)outFileName;
(void)files;
(void)gzip;
(void)verbose;
return false;
#endif
}
int cmSystemTools::WaitForLine(cmsysProcess* process, std::string& line,
double timeout,
std::vector<char>& out,
std::vector<char>& err)
{
line = "";
std::vector<char>::iterator outiter = out.begin();
std::vector<char>::iterator erriter = err.begin();
while(1)
{
// Check for a newline in stdout.
for(;outiter != out.end(); ++outiter)
{
if((*outiter == '\r') && ((outiter+1) == out.end()))
{
break;
}
else if(*outiter == '\n' || *outiter == '\0')
{
int length = outiter-out.begin();
if(length > 1 && *(outiter-1) == '\r')
{
--length;
}
if(length > 0)
{
line.append(&out[0], length);
}
out.erase(out.begin(), outiter+1);
return cmsysProcess_Pipe_STDOUT;
}
}
// Check for a newline in stderr.
for(;erriter != err.end(); ++erriter)
{
if((*erriter == '\r') && ((erriter+1) == err.end()))
{
break;
}
else if(*erriter == '\n' || *erriter == '\0')
{
int length = erriter-err.begin();
if(length > 1 && *(erriter-1) == '\r')
{
--length;
}
if(length > 0)
{
line.append(&err[0], length);
}
err.erase(err.begin(), erriter+1);
return cmsysProcess_Pipe_STDERR;
}
}
// No newlines found. Wait for more data from the process.
int length;
char* data;
int pipe = cmsysProcess_WaitForData(process, &data, &length, &timeout);
if(pipe == cmsysProcess_Pipe_Timeout)
{
// Timeout has been exceeded.
return pipe;
}
else if(pipe == cmsysProcess_Pipe_STDOUT)
{
// Append to the stdout buffer.
std::vector<char>::size_type size = out.size();
out.insert(out.end(), data, data+length);
outiter = out.begin()+size;
}
else if(pipe == cmsysProcess_Pipe_STDERR)
{
// Append to the stderr buffer.
std::vector<char>::size_type size = err.size();
err.insert(err.end(), data, data+length);
erriter = err.begin()+size;
}
else if(pipe == cmsysProcess_Pipe_None)
{
// Both stdout and stderr pipes have broken. Return leftover data.
if(!out.empty())
{
line.append(&out[0], outiter-out.begin());
out.erase(out.begin(), out.end());
return cmsysProcess_Pipe_STDOUT;
}
else if(!err.empty())
{
line.append(&err[0], erriter-err.begin());
err.erase(err.begin(), err.end());
return cmsysProcess_Pipe_STDERR;
}
else
{
return cmsysProcess_Pipe_None;
}
}
}
}
|