cmSystemTools.cxx 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmSystemTools.h"
  14. #include "errno.h"
  15. #include <stdio.h>
  16. #include <sys/stat.h>
  17. #include "cmRegularExpression.h"
  18. #include <ctype.h>
  19. #include "cmDirectory.h"
  20. #include "cmMakefile.h"
  21. // support for realpath call
  22. #ifndef _WIN32
  23. #include <limits.h>
  24. #include <stdlib.h>
  25. #include <sys/param.h>
  26. #include <sys/wait.h>
  27. #endif
  28. #if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__))
  29. #include <string.h>
  30. #include <windows.h>
  31. #include <direct.h>
  32. #define _unlink unlink
  33. inline int Mkdir(const char* dir)
  34. {
  35. return _mkdir(dir);
  36. }
  37. inline const char* Getcwd(char* buf, unsigned int len)
  38. {
  39. return _getcwd(buf, len);
  40. }
  41. inline int Chdir(const char* dir)
  42. {
  43. #if defined(__BORLANDC__)
  44. return chdir(dir);
  45. #else
  46. return _chdir(dir);
  47. #endif
  48. }
  49. #else
  50. #include <sys/types.h>
  51. #include <fcntl.h>
  52. #include <unistd.h>
  53. inline int Mkdir(const char* dir)
  54. {
  55. return mkdir(dir, 00777);
  56. }
  57. inline const char* Getcwd(char* buf, unsigned int len)
  58. {
  59. return getcwd(buf, len);
  60. }
  61. inline int Chdir(const char* dir)
  62. {
  63. return chdir(dir);
  64. }
  65. #endif
  66. bool cmSystemTools::s_DisableRunCommandOutput = false;
  67. bool cmSystemTools::s_ErrorOccured = false;
  68. bool cmSystemTools::s_DisableMessages = false;
  69. std::string cmSystemTools::s_Windows9xComspecSubstitute = "command.com";
  70. void cmSystemTools::SetWindows9xComspecSubstitute(const char* str)
  71. {
  72. if ( str )
  73. {
  74. cmSystemTools::s_Windows9xComspecSubstitute = str;
  75. }
  76. }
  77. const char* cmSystemTools::GetWindows9xComspecSubstitute()
  78. {
  79. return cmSystemTools::s_Windows9xComspecSubstitute.c_str();
  80. }
  81. void (*cmSystemTools::s_ErrorCallback)(const char*, const char*, bool&, void*);
  82. void* cmSystemTools::s_ErrorCallbackClientData = 0;
  83. // adds the elements of the env variable path to the arg passed in
  84. void cmSystemTools::GetPath(std::vector<std::string>& path)
  85. {
  86. #if defined(_WIN32) && !defined(__CYGWIN__)
  87. const char* pathSep = ";";
  88. #else
  89. const char* pathSep = ":";
  90. #endif
  91. std::string pathEnv = getenv("PATH");
  92. // A hack to make the below algorithm work.
  93. if(pathEnv[pathEnv.length()-1] != ':')
  94. {
  95. pathEnv += pathSep;
  96. }
  97. std::string::size_type start =0;
  98. bool done = false;
  99. while(!done)
  100. {
  101. std::string::size_type endpos = pathEnv.find(pathSep, start);
  102. if(endpos != std::string::npos)
  103. {
  104. path.push_back(pathEnv.substr(start, endpos-start));
  105. start = endpos+1;
  106. }
  107. else
  108. {
  109. done = true;
  110. }
  111. }
  112. for(std::vector<std::string>::iterator i = path.begin();
  113. i != path.end(); ++i)
  114. {
  115. cmSystemTools::ConvertToUnixSlashes(*i);
  116. }
  117. }
  118. const char* cmSystemTools::GetExecutableExtension()
  119. {
  120. #if defined(_WIN32) || defined(__CYGWIN__)
  121. return ".exe";
  122. #else
  123. return "";
  124. #endif
  125. }
  126. bool cmSystemTools::MakeDirectory(const char* path)
  127. {
  128. if(cmSystemTools::FileExists(path))
  129. {
  130. return true;
  131. }
  132. std::string dir = path;
  133. if(dir.size() == 0)
  134. {
  135. return false;
  136. }
  137. cmSystemTools::ConvertToUnixSlashes(dir);
  138. std::string::size_type pos = dir.find(':');
  139. if(pos == std::string::npos)
  140. {
  141. pos = 0;
  142. }
  143. std::string topdir;
  144. while((pos = dir.find('/', pos)) != std::string::npos)
  145. {
  146. topdir = dir.substr(0, pos);
  147. Mkdir(topdir.c_str());
  148. pos++;
  149. }
  150. if(dir[dir.size()-1] == '/')
  151. {
  152. topdir = dir.substr(0, dir.size());
  153. }
  154. else
  155. {
  156. topdir = dir;
  157. }
  158. if(Mkdir(topdir.c_str()) != 0)
  159. {
  160. // There is a bug in the Borland Run time library which makes MKDIR
  161. // return EACCES when it should return EEXISTS
  162. // if it is some other error besides directory exists
  163. // then return false
  164. if( (errno != EEXIST)
  165. #ifdef __BORLANDC__
  166. && (errno != EACCES)
  167. #endif
  168. )
  169. {
  170. cmSystemTools::Error("Faild to create directory:", path);
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. // replace replace with with as many times as it shows up in source.
  177. // write the result into source.
  178. void cmSystemTools::ReplaceString(std::string& source,
  179. const char* replace,
  180. const char* with)
  181. {
  182. std::string::size_type lengthReplace = strlen(replace);
  183. std::string::size_type lengthWith = strlen(with);
  184. std::string rest;
  185. std::string::size_type start = source.find(replace);
  186. while(start != std::string::npos)
  187. {
  188. rest = source.substr(start+lengthReplace);
  189. source = source.substr(0, start);
  190. source += with;
  191. source += rest;
  192. start = source.find(replace, start + lengthWith );
  193. }
  194. }
  195. // Read a registry value.
  196. // Example :
  197. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  198. // => will return the data of the "default" value of the key
  199. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  200. // => will return the data of the "Root" value of the key
  201. #if defined(_WIN32) && !defined(__CYGWIN__)
  202. bool cmSystemTools::ReadRegistryValue(const char *key, std::string &value)
  203. {
  204. std::string primary = key;
  205. std::string second;
  206. std::string valuename;
  207. size_t start = primary.find("\\");
  208. if (start == std::string::npos)
  209. {
  210. return false;
  211. }
  212. size_t valuenamepos = primary.find(";");
  213. if (valuenamepos != std::string::npos)
  214. {
  215. valuename = primary.substr(valuenamepos+1);
  216. }
  217. second = primary.substr(start+1, valuenamepos-start-1);
  218. primary = primary.substr(0, start);
  219. HKEY primaryKey;
  220. if (primary == "HKEY_CURRENT_USER")
  221. {
  222. primaryKey = HKEY_CURRENT_USER;
  223. }
  224. if (primary == "HKEY_CURRENT_CONFIG")
  225. {
  226. primaryKey = HKEY_CURRENT_CONFIG;
  227. }
  228. if (primary == "HKEY_CLASSES_ROOT")
  229. {
  230. primaryKey = HKEY_CLASSES_ROOT;
  231. }
  232. if (primary == "HKEY_LOCAL_MACHINE")
  233. {
  234. primaryKey = HKEY_LOCAL_MACHINE;
  235. }
  236. if (primary == "HKEY_USERS")
  237. {
  238. primaryKey = HKEY_USERS;
  239. }
  240. HKEY hKey;
  241. if(RegOpenKeyEx(primaryKey,
  242. second.c_str(),
  243. 0,
  244. KEY_READ,
  245. &hKey) != ERROR_SUCCESS)
  246. {
  247. return false;
  248. }
  249. else
  250. {
  251. DWORD dwType, dwSize;
  252. dwSize = 1023;
  253. char data[1024];
  254. if(RegQueryValueEx(hKey,
  255. (LPTSTR)valuename.c_str(),
  256. NULL,
  257. &dwType,
  258. (BYTE *)data,
  259. &dwSize) == ERROR_SUCCESS)
  260. {
  261. if (dwType == REG_SZ)
  262. {
  263. value = data;
  264. return true;
  265. }
  266. }
  267. }
  268. return false;
  269. }
  270. #else
  271. bool cmSystemTools::ReadRegistryValue(const char *, std::string &)
  272. {
  273. return false;
  274. }
  275. #endif
  276. // Write a registry value.
  277. // Example :
  278. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  279. // => will set the data of the "default" value of the key
  280. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  281. // => will set the data of the "Root" value of the key
  282. #if defined(_WIN32) && !defined(__CYGWIN__)
  283. bool cmSystemTools::WriteRegistryValue(const char *key, const char *value)
  284. {
  285. std::string primary = key;
  286. std::string second;
  287. std::string valuename;
  288. size_t start = primary.find("\\");
  289. if (start == std::string::npos)
  290. {
  291. return false;
  292. }
  293. size_t valuenamepos = primary.find(";");
  294. if (valuenamepos != std::string::npos)
  295. {
  296. valuename = primary.substr(valuenamepos+1);
  297. }
  298. second = primary.substr(start+1, valuenamepos-start-1);
  299. primary = primary.substr(0, start);
  300. HKEY primaryKey;
  301. if (primary == "HKEY_CURRENT_USER")
  302. {
  303. primaryKey = HKEY_CURRENT_USER;
  304. }
  305. if (primary == "HKEY_CURRENT_CONFIG")
  306. {
  307. primaryKey = HKEY_CURRENT_CONFIG;
  308. }
  309. if (primary == "HKEY_CLASSES_ROOT")
  310. {
  311. primaryKey = HKEY_CLASSES_ROOT;
  312. }
  313. if (primary == "HKEY_LOCAL_MACHINE")
  314. {
  315. primaryKey = HKEY_LOCAL_MACHINE;
  316. }
  317. if (primary == "HKEY_USERS")
  318. {
  319. primaryKey = HKEY_USERS;
  320. }
  321. HKEY hKey;
  322. DWORD dwDummy;
  323. if(RegCreateKeyEx(primaryKey,
  324. second.c_str(),
  325. 0,
  326. "",
  327. REG_OPTION_NON_VOLATILE,
  328. KEY_WRITE,
  329. NULL,
  330. &hKey,
  331. &dwDummy) != ERROR_SUCCESS)
  332. {
  333. return false;
  334. }
  335. if(RegSetValueEx(hKey,
  336. (LPTSTR)valuename.c_str(),
  337. 0,
  338. REG_SZ,
  339. (CONST BYTE *)value,
  340. (DWORD)(strlen(value) + 1)) == ERROR_SUCCESS)
  341. {
  342. return true;
  343. }
  344. return false;
  345. }
  346. #else
  347. bool cmSystemTools::WriteRegistryValue(const char *, const char *)
  348. {
  349. return false;
  350. }
  351. #endif
  352. // Delete a registry value.
  353. // Example :
  354. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  355. // => will delete the data of the "default" value of the key
  356. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  357. // => will delete the data of the "Root" value of the key
  358. #if defined(_WIN32) && !defined(__CYGWIN__)
  359. bool cmSystemTools::DeleteRegistryValue(const char *key)
  360. {
  361. std::string primary = key;
  362. std::string second;
  363. std::string valuename;
  364. size_t start = primary.find("\\");
  365. if (start == std::string::npos)
  366. {
  367. return false;
  368. }
  369. size_t valuenamepos = primary.find(";");
  370. if (valuenamepos != std::string::npos)
  371. {
  372. valuename = primary.substr(valuenamepos+1);
  373. }
  374. second = primary.substr(start+1, valuenamepos-start-1);
  375. primary = primary.substr(0, start);
  376. HKEY primaryKey;
  377. if (primary == "HKEY_CURRENT_USER")
  378. {
  379. primaryKey = HKEY_CURRENT_USER;
  380. }
  381. if (primary == "HKEY_CURRENT_CONFIG")
  382. {
  383. primaryKey = HKEY_CURRENT_CONFIG;
  384. }
  385. if (primary == "HKEY_CLASSES_ROOT")
  386. {
  387. primaryKey = HKEY_CLASSES_ROOT;
  388. }
  389. if (primary == "HKEY_LOCAL_MACHINE")
  390. {
  391. primaryKey = HKEY_LOCAL_MACHINE;
  392. }
  393. if (primary == "HKEY_USERS")
  394. {
  395. primaryKey = HKEY_USERS;
  396. }
  397. HKEY hKey;
  398. if(RegOpenKeyEx(primaryKey,
  399. second.c_str(),
  400. 0,
  401. KEY_WRITE,
  402. &hKey) != ERROR_SUCCESS)
  403. {
  404. return false;
  405. }
  406. else
  407. {
  408. if(RegDeleteValue(hKey,
  409. (LPTSTR)valuename.c_str()) == ERROR_SUCCESS)
  410. {
  411. return true;
  412. }
  413. }
  414. return false;
  415. }
  416. #else
  417. bool cmSystemTools::DeleteRegistryValue(const char *)
  418. {
  419. return false;
  420. }
  421. #endif
  422. // replace replace with with as many times as it shows up in source.
  423. // write the result into source.
  424. #if defined(_WIN32) && !defined(__CYGWIN__)
  425. void cmSystemTools::ExpandRegistryValues(std::string& source)
  426. {
  427. // Regular expression to match anything inside [...] that begins in HKEY.
  428. // Note that there is a special rule for regular expressions to match a
  429. // close square-bracket inside a list delimited by square brackets.
  430. // The "[^]]" part of this expression will match any character except
  431. // a close square-bracket. The ']' character must be the first in the
  432. // list of characters inside the [^...] block of the expression.
  433. cmRegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  434. // check for black line or comment
  435. while (regEntry.find(source))
  436. {
  437. // the arguments are the second match
  438. std::string key = regEntry.match(1);
  439. std::string val;
  440. if (ReadRegistryValue(key.c_str(), val))
  441. {
  442. std::string reg = "[";
  443. reg += key + "]";
  444. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  445. }
  446. else
  447. {
  448. std::string reg = "[";
  449. reg += key + "]";
  450. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  451. }
  452. }
  453. }
  454. #else
  455. void cmSystemTools::ExpandRegistryValues(std::string&)
  456. {
  457. }
  458. #endif
  459. std::string cmSystemTools::EscapeQuotes(const char* str)
  460. {
  461. std::string result = "";
  462. for(const char* ch = str; *ch != '\0'; ++ch)
  463. {
  464. if(*ch == '"')
  465. {
  466. result += '\\';
  467. }
  468. result += *ch;
  469. }
  470. return result;
  471. }
  472. bool cmSystemTools::SameFile(const char* file1, const char* file2)
  473. {
  474. #ifdef _WIN32
  475. HANDLE hFile1, hFile2;
  476. hFile1 = CreateFile( file1,
  477. GENERIC_READ,
  478. FILE_SHARE_READ ,
  479. NULL,
  480. OPEN_EXISTING,
  481. FILE_FLAG_BACKUP_SEMANTICS,
  482. NULL
  483. );
  484. hFile2 = CreateFile( file2,
  485. GENERIC_READ,
  486. FILE_SHARE_READ,
  487. NULL,
  488. OPEN_EXISTING,
  489. FILE_FLAG_BACKUP_SEMANTICS,
  490. NULL
  491. );
  492. if( hFile1 == INVALID_HANDLE_VALUE || hFile2 == INVALID_HANDLE_VALUE)
  493. {
  494. if(hFile1 != INVALID_HANDLE_VALUE)
  495. {
  496. CloseHandle(hFile1);
  497. }
  498. if(hFile2 != INVALID_HANDLE_VALUE)
  499. {
  500. CloseHandle(hFile2);
  501. }
  502. return false;
  503. }
  504. BY_HANDLE_FILE_INFORMATION fiBuf1;
  505. BY_HANDLE_FILE_INFORMATION fiBuf2;
  506. GetFileInformationByHandle( hFile1, &fiBuf1 );
  507. GetFileInformationByHandle( hFile2, &fiBuf2 );
  508. CloseHandle(hFile1);
  509. CloseHandle(hFile2);
  510. return (fiBuf1.nFileIndexHigh == fiBuf2.nFileIndexHigh &&
  511. fiBuf1.nFileIndexLow == fiBuf2.nFileIndexLow);
  512. #else
  513. struct stat fileStat1, fileStat2;
  514. if (stat(file1, &fileStat1) == 0 && stat(file2, &fileStat2) == 0)
  515. {
  516. // see if the files are the same file
  517. // check the device inode and size
  518. if(memcmp(&fileStat2.st_dev, &fileStat1.st_dev, sizeof(fileStat1.st_dev)) == 0 &&
  519. memcmp(&fileStat2.st_ino, &fileStat1.st_ino, sizeof(fileStat1.st_ino)) == 0 &&
  520. fileStat2.st_size == fileStat1.st_size
  521. )
  522. {
  523. return true;
  524. }
  525. }
  526. return false;
  527. #endif
  528. }
  529. // return true if the file exists
  530. bool cmSystemTools::FileExists(const char* filename)
  531. {
  532. struct stat fs;
  533. if (stat(filename, &fs) != 0)
  534. {
  535. return false;
  536. }
  537. else
  538. {
  539. return true;
  540. }
  541. }
  542. // Return a capitalized string (i.e the first letter is uppercased, all other
  543. // are lowercased)
  544. std::string cmSystemTools::Capitalized(const std::string& s)
  545. {
  546. std::string n;
  547. n.resize(s.size());
  548. n[0] = toupper(s[0]);
  549. for (size_t i = 1; i < s.size(); i++)
  550. {
  551. n[i] = tolower(s[i]);
  552. }
  553. return n;
  554. }
  555. // Return a lower case string
  556. std::string cmSystemTools::LowerCase(const std::string& s)
  557. {
  558. std::string n;
  559. n.resize(s.size());
  560. for (size_t i = 0; i < s.size(); i++)
  561. {
  562. n[i] = tolower(s[i]);
  563. }
  564. return n;
  565. }
  566. // Return a lower case string
  567. std::string cmSystemTools::UpperCase(const std::string& s)
  568. {
  569. std::string n;
  570. n.resize(s.size());
  571. for (size_t i = 0; i < s.size(); i++)
  572. {
  573. n[i] = toupper(s[i]);
  574. }
  575. return n;
  576. }
  577. // convert windows slashes to unix slashes
  578. void cmSystemTools::ConvertToUnixSlashes(std::string& path)
  579. {
  580. std::string::size_type pos = 0;
  581. while((pos = path.find('\\', pos)) != std::string::npos)
  582. {
  583. path[pos] = '/';
  584. pos++;
  585. }
  586. // remove any trailing slash
  587. if(path.size() && path[path.size()-1] == '/')
  588. {
  589. path = path.substr(0, path.size()-1);
  590. }
  591. // if there is a tilda ~ then replace it with HOME
  592. if(path.find("~") == 0)
  593. {
  594. if (getenv("HOME"))
  595. {
  596. path = std::string(getenv("HOME")) + path.substr(1);
  597. }
  598. }
  599. // if there is a /tmp_mnt in a path get rid of it!
  600. // stupid sgi's
  601. if(path.find("/tmp_mnt") == 0)
  602. {
  603. path = path.substr(8);
  604. }
  605. }
  606. // change // to /, and escape any spaces in the path
  607. std::string cmSystemTools::ConvertToUnixOutputPath(const char* path)
  608. {
  609. std::string ret = path;
  610. // remove // except at the beginning might be a cygwin drive
  611. std::string::size_type pos = 1;
  612. while((pos = ret.find("//", pos)) != std::string::npos)
  613. {
  614. ret.erase(pos, 1);
  615. }
  616. // now escape spaces if there is a space in the path
  617. if(ret.find(" ") != std::string::npos)
  618. {
  619. std::string result = "";
  620. char lastch = 1;
  621. for(const char* ch = ret.c_str(); *ch != '\0'; ++ch)
  622. {
  623. // if it is already escaped then don't try to escape it again
  624. if(*ch == ' ' && lastch != '\\')
  625. {
  626. result += '\\';
  627. }
  628. result += *ch;
  629. lastch = *ch;
  630. }
  631. ret = result;
  632. }
  633. return ret;
  634. }
  635. std::string cmSystemTools::EscapeSpaces(const char* str)
  636. {
  637. #if defined(_WIN32) && !defined(__CYGWIN__)
  638. std::string result;
  639. // if there are spaces
  640. std::string temp = str;
  641. if (temp.find(" ") != std::string::npos &&
  642. temp.find("\"")==std::string::npos)
  643. {
  644. result = "\"";
  645. result += str;
  646. result += "\"";
  647. return result;
  648. }
  649. return str;
  650. #else
  651. std::string result = "";
  652. for(const char* ch = str; *ch != '\0'; ++ch)
  653. {
  654. if(*ch == ' ')
  655. {
  656. result += '\\';
  657. }
  658. result += *ch;
  659. }
  660. return result;
  661. #endif
  662. }
  663. std::string cmSystemTools::ConvertToOutputPath(const char* path)
  664. {
  665. #if defined(_WIN32) && !defined(__CYGWIN__)
  666. return cmSystemTools::ConvertToWindowsOutputPath(path);
  667. #else
  668. return cmSystemTools::ConvertToUnixOutputPath(path);
  669. #endif
  670. }
  671. // remove double slashes not at the start
  672. std::string cmSystemTools::ConvertToWindowsOutputPath(const char* path)
  673. {
  674. std::string ret = path;
  675. std::string::size_type pos = 0;
  676. // first convert all of the slashes
  677. while((pos = ret.find('/', pos)) != std::string::npos)
  678. {
  679. ret[pos] = '\\';
  680. pos++;
  681. }
  682. // check for really small paths
  683. if(ret.size() < 2)
  684. {
  685. return ret;
  686. }
  687. // now clean up a bit and remove double slashes
  688. // Only if it is not the first position in the path which is a network
  689. // path on windows
  690. pos = 1; // start at position 1
  691. while((pos = ret.find("\\\\", pos)) != std::string::npos)
  692. {
  693. ret.erase(pos, 1);
  694. }
  695. // now double quote the path if it has spaces in it
  696. // and is not already double quoted
  697. if(ret.find(" ") != std::string::npos
  698. && ret[0] != '\"')
  699. {
  700. std::string result;
  701. result = "\"" + ret + "\"";
  702. ret = result;
  703. }
  704. return ret;
  705. }
  706. inline void RemoveComments(char* ptr)
  707. {
  708. while(*ptr)
  709. {
  710. if(*ptr == '#')
  711. {
  712. *ptr = 0;
  713. break;
  714. }
  715. ++ptr;
  716. }
  717. }
  718. bool cmSystemTools::ParseFunction(std::ifstream& fin,
  719. std::string& name,
  720. std::vector<std::string>& arguments,
  721. const char* filename,
  722. bool& parseError)
  723. {
  724. parseError = false;
  725. name = "";
  726. arguments = std::vector<std::string>();
  727. const int BUFFER_SIZE = 4096;
  728. char inbuffer[BUFFER_SIZE];
  729. if(!fin)
  730. {
  731. return false;
  732. }
  733. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  734. {
  735. RemoveComments(inbuffer);
  736. cmRegularExpression blankLine("^[ \t\r]*$");
  737. // cmRegularExpression comment("^[ \t]*#.*$");
  738. cmRegularExpression oneLiner("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)\\)[ \t\r]*$");
  739. cmRegularExpression multiLine("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)$");
  740. cmRegularExpression lastLine("^(.*)\\)[ \t\r]*$");
  741. // check for blank line or comment
  742. if(blankLine.find(inbuffer) )
  743. {
  744. return false;
  745. }
  746. // look for a oneline fun(arg arg2)
  747. else if(oneLiner.find(inbuffer))
  748. {
  749. // the arguments are the second match
  750. std::string args = oneLiner.match(2);
  751. name = oneLiner.match(1);
  752. // break up the arguments
  753. cmSystemTools::GetArguments(args, arguments);
  754. return true;
  755. }
  756. // look for a start of a multiline with no trailing ")" fun(arg arg2
  757. else if(multiLine.find(inbuffer))
  758. {
  759. name = multiLine.match(1);
  760. std::string args = multiLine.match(2);
  761. cmSystemTools::GetArguments(args, arguments);
  762. // Read lines until the closing paren is hit
  763. bool done = false;
  764. while(!done)
  765. {
  766. // read lines until the end paren is found
  767. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  768. {
  769. RemoveComments(inbuffer);
  770. // Check for comment lines and ignore them.
  771. if(blankLine.find(inbuffer))
  772. { continue; }
  773. // Is this the last line?
  774. if(lastLine.find(inbuffer))
  775. {
  776. done = true;
  777. std::string args = lastLine.match(1);
  778. cmSystemTools::GetArguments(args, arguments);
  779. }
  780. else
  781. {
  782. std::string line = inbuffer;
  783. cmSystemTools::GetArguments(line, arguments);
  784. }
  785. }
  786. else
  787. {
  788. parseError = true;
  789. cmSystemTools::Error("Parse error in read function missing end )\nIn File: ",
  790. filename, "\nCurrent line:", inbuffer);
  791. return false;
  792. }
  793. }
  794. return true;
  795. }
  796. else
  797. {
  798. parseError = true;
  799. cmSystemTools::Error("Parse error in read function\nIn file:",
  800. filename, "\nCurrent line:", inbuffer);
  801. return false;
  802. }
  803. }
  804. return false;
  805. }
  806. void cmSystemTools::GetArguments(std::string& line,
  807. std::vector<std::string>& arguments)
  808. {
  809. // Match a normal argument (not quoted, no spaces).
  810. cmRegularExpression normalArgument("[ \t]*(([^ \t\r\\]|[\\].)+)[ \t\r]*");
  811. // Match a quoted argument (surrounded by double quotes, spaces allowed).
  812. cmRegularExpression quotedArgument("[ \t]*(\"([^\"\\]|[\\].)*\")[ \t\r]*");
  813. bool done = false;
  814. while(!done)
  815. {
  816. std::string arg;
  817. std::string::size_type endpos=0;
  818. bool foundQuoted = quotedArgument.find(line.c_str());
  819. bool foundNormal = normalArgument.find(line.c_str());
  820. if(foundQuoted && foundNormal)
  821. {
  822. // Both matches were found. Take the earlier one.
  823. // Favor double-quoted version if there is a tie.
  824. if(normalArgument.start(1) < quotedArgument.start(1))
  825. {
  826. arg = normalArgument.match(1);
  827. endpos = normalArgument.end(1);
  828. }
  829. else
  830. {
  831. arg = quotedArgument.match(1);
  832. endpos = quotedArgument.end(1);
  833. // Strip off the double quotes on the ends.
  834. arg = arg.substr(1, arg.length()-2);
  835. }
  836. }
  837. else if (foundQuoted)
  838. {
  839. arg = quotedArgument.match(1);
  840. endpos = quotedArgument.end(1);
  841. // Strip off the double quotes on the ends.
  842. arg = arg.substr(1, arg.length()-2);
  843. }
  844. else if(foundNormal)
  845. {
  846. arg = normalArgument.match(1);
  847. endpos = normalArgument.end(1);
  848. }
  849. else
  850. {
  851. done = true;
  852. }
  853. if(!done)
  854. {
  855. arguments.push_back(cmSystemTools::RemoveEscapes(arg.c_str()));
  856. line = line.substr(endpos, line.length() - endpos);
  857. }
  858. }
  859. }
  860. std::string cmSystemTools::RemoveEscapes(const char* s)
  861. {
  862. std::string result = "";
  863. for(const char* ch = s; *ch; ++ch)
  864. {
  865. if(*ch == '\\' && *(ch+1) != ';')
  866. {
  867. ++ch;
  868. switch (*ch)
  869. {
  870. case '\\': result.insert(result.end(), '\\'); break;
  871. case '"': result.insert(result.end(), '"'); break;
  872. case ' ': result.insert(result.end(), ' '); break;
  873. case 't': result.insert(result.end(), '\t'); break;
  874. case 'n': result.insert(result.end(), '\n'); break;
  875. case 'r': result.insert(result.end(), '\r'); break;
  876. case '0': result.insert(result.end(), '\0'); break;
  877. case '\0':
  878. {
  879. cmSystemTools::Error("Trailing backslash in argument:\n", s);
  880. return result;
  881. }
  882. default:
  883. {
  884. std::string chStr(1, *ch);
  885. cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
  886. "\nin argument ", s);
  887. }
  888. }
  889. }
  890. else
  891. {
  892. result.insert(result.end(), *ch);
  893. }
  894. }
  895. return result;
  896. }
  897. void cmSystemTools::Error(const char* m1, const char* m2,
  898. const char* m3, const char* m4)
  899. {
  900. std::string message = "CMake Error: ";
  901. if(m1)
  902. {
  903. message += m1;
  904. }
  905. if(m2)
  906. {
  907. message += m2;
  908. }
  909. if(m3)
  910. {
  911. message += m3;
  912. }
  913. if(m4)
  914. {
  915. message += m4;
  916. }
  917. cmSystemTools::s_ErrorOccured = true;
  918. cmSystemTools::Message(message.c_str(),"Error");
  919. }
  920. void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
  921. {
  922. s_ErrorCallback = f;
  923. s_ErrorCallbackClientData = clientData;
  924. }
  925. void cmSystemTools::Message(const char* m1, const char *title)
  926. {
  927. if(s_DisableMessages)
  928. {
  929. return;
  930. }
  931. if(s_ErrorCallback)
  932. {
  933. (*s_ErrorCallback)(m1, title, s_DisableMessages, s_ErrorCallbackClientData);
  934. return;
  935. }
  936. else
  937. {
  938. std::cerr << m1 << std::endl << std::flush;
  939. }
  940. }
  941. bool cmSystemTools::CopyFileIfDifferent(const char* source,
  942. const char* destination)
  943. {
  944. if(cmSystemTools::FilesDiffer(source, destination))
  945. {
  946. cmSystemTools::cmCopyFile(source, destination);
  947. return true;
  948. }
  949. return false;
  950. }
  951. bool cmSystemTools::FilesDiffer(const char* source,
  952. const char* destination)
  953. {
  954. struct stat statSource;
  955. if (stat(source, &statSource) != 0)
  956. {
  957. return true;
  958. }
  959. struct stat statDestination;
  960. if (stat(destination, &statDestination) != 0)
  961. {
  962. return true;
  963. }
  964. if(statSource.st_size != statDestination.st_size)
  965. {
  966. return true;
  967. }
  968. if(statSource.st_size == 0)
  969. {
  970. return false;
  971. }
  972. #if defined(_WIN32) || defined(__CYGWIN__)
  973. std::ifstream finSource(source,
  974. std::ios::binary | std::ios::in);
  975. std::ifstream finDestination(destination,
  976. std::ios::binary | std::ios::in);
  977. #else
  978. std::ifstream finSource(source);
  979. std::ifstream finDestination(destination);
  980. #endif
  981. if(!finSource || !finDestination)
  982. {
  983. return true;
  984. }
  985. char* source_buf = new char[statSource.st_size];
  986. char* dest_buf = new char[statSource.st_size];
  987. finSource.read(source_buf, statSource.st_size);
  988. finDestination.read(dest_buf, statSource.st_size);
  989. if(statSource.st_size != static_cast<long>(finSource.gcount()) ||
  990. statSource.st_size != static_cast<long>(finDestination.gcount()))
  991. {
  992. cmStringStream msg;
  993. msg << "FilesDiffer failed to read files (allocated: "
  994. << statSource.st_size << ", read source: " << finSource.gcount()
  995. << ", read dest: " << finDestination.gcount();
  996. cmSystemTools::Error(msg.str().c_str());
  997. delete [] source_buf;
  998. delete [] dest_buf;
  999. return false;
  1000. }
  1001. int ret = memcmp((const void*)source_buf,
  1002. (const void*)dest_buf,
  1003. statSource.st_size);
  1004. delete [] dest_buf;
  1005. delete [] source_buf;
  1006. return ret != 0;
  1007. }
  1008. /**
  1009. * Copy a file named by "source" to the file named by "destination".
  1010. */
  1011. void cmSystemTools::cmCopyFile(const char* source,
  1012. const char* destination)
  1013. {
  1014. const int bufferSize = 4096;
  1015. char buffer[bufferSize];
  1016. // If destination is a directory, try to create a file with the same
  1017. // name as the source in that directory.
  1018. std::string new_destination;
  1019. if(cmSystemTools::FileExists(destination) &&
  1020. cmSystemTools::FileIsDirectory(destination))
  1021. {
  1022. new_destination = destination;
  1023. cmSystemTools::ConvertToUnixSlashes(new_destination);
  1024. new_destination += '/';
  1025. std::string source_name = source;
  1026. new_destination += cmSystemTools::GetFilenameName(source_name);
  1027. destination = new_destination.c_str();
  1028. }
  1029. // Create destination directory
  1030. std::string destination_dir = destination;
  1031. destination_dir = cmSystemTools::GetFilenamePath(destination_dir);
  1032. cmSystemTools::MakeDirectory(destination_dir.c_str());
  1033. // Open files
  1034. #if defined(_WIN32) || defined(__CYGWIN__)
  1035. std::ifstream fin(source,
  1036. std::ios::binary | std::ios::in);
  1037. #else
  1038. std::ifstream fin(source);
  1039. #endif
  1040. if(!fin)
  1041. {
  1042. cmSystemTools::Error("CopyFile failed to open input file \"",
  1043. source, "\"");
  1044. return;
  1045. }
  1046. #if defined(_WIN32) || defined(__CYGWIN__)
  1047. std::ofstream fout(destination,
  1048. std::ios::binary | std::ios::out | std::ios::trunc);
  1049. #else
  1050. std::ofstream fout(destination,
  1051. std::ios::out | std::ios::trunc);
  1052. #endif
  1053. if(!fout)
  1054. {
  1055. cmSystemTools::Error("CopyFile failed to open output file \"",
  1056. destination, "\"");
  1057. return;
  1058. }
  1059. // This copy loop is very sensitive on certain platforms with
  1060. // slightly broken stream libraries (like HPUX). Normally, it is
  1061. // incorrect to not check the error condition on the fin.read()
  1062. // before using the data, but the fin.gcount() will be zero if an
  1063. // error occurred. Therefore, the loop should be safe everywhere.
  1064. while(fin)
  1065. {
  1066. fin.read(buffer, bufferSize);
  1067. if(fin.gcount())
  1068. {
  1069. fout.write(buffer, fin.gcount());
  1070. }
  1071. }
  1072. fin.close();
  1073. fout.close();
  1074. // More checks
  1075. struct stat statSource, statDestination;
  1076. if (stat(source, &statSource) != 0 ||
  1077. stat(destination, &statDestination) != 0)
  1078. {
  1079. cmSystemTools::Error("CopyFile failed to copy files!");
  1080. }
  1081. if (statSource.st_size != statDestination.st_size)
  1082. {
  1083. cmStringStream msg;
  1084. msg << "CopyFile failed to copy files (sizes differ, source: "
  1085. << statSource.st_size << " , dest: " << statDestination.st_size;
  1086. cmSystemTools::Error(msg.str().c_str());
  1087. }
  1088. }
  1089. // return true if the file exists
  1090. long int cmSystemTools::ModifiedTime(const char* filename)
  1091. {
  1092. struct stat fs;
  1093. if (stat(filename, &fs) != 0)
  1094. {
  1095. return 0;
  1096. }
  1097. else
  1098. {
  1099. return (long int)fs.st_mtime;
  1100. }
  1101. }
  1102. bool cmSystemTools::RemoveFile(const char* source)
  1103. {
  1104. return unlink(source) != 0 ? false : true;
  1105. }
  1106. bool cmSystemTools::IsOn(const char* val)
  1107. {
  1108. if (!val)
  1109. {
  1110. return false;
  1111. }
  1112. std::basic_string<char> v = val;
  1113. for(std::basic_string<char>::iterator c = v.begin();
  1114. c != v.end(); c++)
  1115. {
  1116. *c = toupper(*c);
  1117. }
  1118. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  1119. }
  1120. bool cmSystemTools::IsOff(const char* val)
  1121. {
  1122. if (!val || strlen(val) == 0)
  1123. {
  1124. return true;
  1125. }
  1126. std::basic_string<char> v = val;
  1127. for(std::basic_string<char>::iterator c = v.begin();
  1128. c != v.end(); c++)
  1129. {
  1130. *c = toupper(*c);
  1131. }
  1132. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  1133. v == "N" || v == "NOTFOUND" || v == "IGNORE");
  1134. }
  1135. bool cmSystemTools::RunCommand(const char* command,
  1136. std::string& output,
  1137. const char* dir,
  1138. bool verbose,
  1139. int timeout)
  1140. {
  1141. int foo;
  1142. return cmSystemTools::RunCommand(command, output, foo, dir, verbose, timeout);
  1143. }
  1144. #if defined(WIN32) && !defined(__CYGWIN__)
  1145. #include "cmWin32ProcessExecution.h"
  1146. // use this for shell commands like echo and dir
  1147. bool RunCommandViaWin32(const char* command,
  1148. const char* dir,
  1149. std::string& output,
  1150. int& retVal,
  1151. bool verbose,
  1152. int timeout)
  1153. {
  1154. #if defined(__BORLANDC__)
  1155. return cmWin32ProcessExecution::BorlandRunCommand(command, dir, output,
  1156. retVal,
  1157. verbose, timeout);
  1158. #else // Visual studio
  1159. ::SetLastError(ERROR_SUCCESS);
  1160. if ( ! command )
  1161. {
  1162. cmSystemTools::Error("No command specified");
  1163. return false;
  1164. }
  1165. //std::cout << "Command: " << command << std::endl;
  1166. if ( dir )
  1167. {
  1168. //std::cout << "Dir: " << dir << std::endl;
  1169. }
  1170. cmWin32ProcessExecution resProc;
  1171. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  1172. {
  1173. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  1174. }
  1175. if ( !resProc.StartProcess(command, dir, verbose) )
  1176. {
  1177. std::cout << "Problem starting command" << std::endl;
  1178. return false;
  1179. }
  1180. resProc.Wait(timeout);
  1181. output = resProc.GetOutput();
  1182. retVal = resProc.GetExitValue();
  1183. return true;
  1184. #endif
  1185. }
  1186. // use this for shell commands like echo and dir
  1187. bool RunCommandViaSystem(const char* command,
  1188. const char* dir,
  1189. std::string& output,
  1190. int& retVal,
  1191. bool verbose)
  1192. {
  1193. std::cout << "@@ " << command << std::endl;
  1194. const int BUFFER_SIZE = 4096;
  1195. char buffer[BUFFER_SIZE];
  1196. std::string commandInDir;
  1197. if(dir)
  1198. {
  1199. commandInDir = "cd ";
  1200. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  1201. commandInDir += " && ";
  1202. commandInDir += command;
  1203. }
  1204. else
  1205. {
  1206. commandInDir = command;
  1207. }
  1208. command = commandInDir.c_str();
  1209. std::string commandToFile = command;
  1210. commandToFile += " > ";
  1211. std::string tempFile;
  1212. tempFile += _tempnam(0, "cmake");
  1213. commandToFile += tempFile;
  1214. retVal = system(commandToFile.c_str());
  1215. std::ifstream fin(tempFile.c_str());
  1216. if(!fin)
  1217. {
  1218. if(verbose)
  1219. {
  1220. std::string errormsg = "RunCommand produced no output: command: \"";
  1221. errormsg += command;
  1222. errormsg += "\"";
  1223. errormsg += "\nOutput file: ";
  1224. errormsg += tempFile;
  1225. cmSystemTools::Error(errormsg.c_str());
  1226. }
  1227. fin.close();
  1228. cmSystemTools::RemoveFile(tempFile.c_str());
  1229. return false;
  1230. }
  1231. bool multiLine = false;
  1232. while(fin)
  1233. {
  1234. fin.getline(buffer, BUFFER_SIZE);
  1235. output += buffer;
  1236. if(multiLine)
  1237. {
  1238. output += "\n";
  1239. }
  1240. multiLine = true;
  1241. }
  1242. fin.close();
  1243. cmSystemTools::RemoveFile(tempFile.c_str());
  1244. return true;
  1245. }
  1246. #else // We have popen
  1247. bool RunCommandViaPopen(const char* command,
  1248. const char* dir,
  1249. std::string& output,
  1250. int& retVal,
  1251. bool verbose,
  1252. int /*timeout*/)
  1253. {
  1254. // if only popen worked on windows.....
  1255. std::string commandInDir;
  1256. if(dir)
  1257. {
  1258. commandInDir = "cd ";
  1259. commandInDir += dir;
  1260. commandInDir += " && ";
  1261. commandInDir += command;
  1262. }
  1263. else
  1264. {
  1265. commandInDir = command;
  1266. }
  1267. commandInDir += " 2>&1";
  1268. command = commandInDir.c_str();
  1269. const int BUFFER_SIZE = 4096;
  1270. char buffer[BUFFER_SIZE];
  1271. if(verbose)
  1272. {
  1273. std::cout << "running " << command << std::endl;
  1274. }
  1275. fflush(stdout);
  1276. fflush(stderr);
  1277. FILE* cpipe = popen(command, "r");
  1278. if(!cpipe)
  1279. {
  1280. return false;
  1281. }
  1282. fgets(buffer, BUFFER_SIZE, cpipe);
  1283. while(!feof(cpipe))
  1284. {
  1285. if(verbose)
  1286. {
  1287. std::cout << buffer << std::flush;
  1288. }
  1289. output += buffer;
  1290. fgets(buffer, BUFFER_SIZE, cpipe);
  1291. }
  1292. retVal = pclose(cpipe);
  1293. if (WIFEXITED(retVal))
  1294. {
  1295. retVal = WEXITSTATUS(retVal);
  1296. return true;
  1297. }
  1298. if (WIFSIGNALED(retVal))
  1299. {
  1300. retVal = WTERMSIG(retVal);
  1301. cmStringStream error;
  1302. error << "\nProcess terminated due to ";
  1303. switch (retVal)
  1304. {
  1305. #ifdef SIGKILL
  1306. case SIGKILL:
  1307. error << "SIGKILL";
  1308. break;
  1309. #endif
  1310. #ifdef SIGFPE
  1311. case SIGFPE:
  1312. error << "SIGFPE";
  1313. break;
  1314. #endif
  1315. #ifdef SIGBUS
  1316. case SIGBUS:
  1317. error << "SIGBUS";
  1318. break;
  1319. #endif
  1320. #ifdef SIGSEGV
  1321. case SIGSEGV:
  1322. error << "SIGSEGV";
  1323. break;
  1324. #endif
  1325. default:
  1326. error << "signal " << retVal;
  1327. break;
  1328. }
  1329. output += error.str();
  1330. }
  1331. return false;
  1332. }
  1333. #endif // endif WIN32 not CYGWIN
  1334. // run a command unix uses popen (easy)
  1335. // windows uses system and ShortPath
  1336. bool cmSystemTools::RunCommand(const char* command,
  1337. std::string& output,
  1338. int &retVal,
  1339. const char* dir,
  1340. bool verbose,
  1341. int timeout)
  1342. {
  1343. if(s_DisableRunCommandOutput)
  1344. {
  1345. verbose = false;
  1346. }
  1347. #if defined(WIN32) && !defined(__CYGWIN__)
  1348. // if the command does not start with a quote, then
  1349. // try to find the program, and if the program can not be
  1350. // found use system to run the command as it must be a built in
  1351. // shell command like echo or dir
  1352. int count = 0;
  1353. if(command[0] == '\"')
  1354. {
  1355. // count the number of quotes
  1356. for(const char* s = command; *s != 0; ++s)
  1357. {
  1358. if(*s == '\"')
  1359. {
  1360. count++;
  1361. if(count > 2)
  1362. {
  1363. break;
  1364. }
  1365. }
  1366. }
  1367. // if there are more than two double quotes use
  1368. // GetShortPathName, the cmd.exe program in windows which
  1369. // is used by system fails to execute if there are more than
  1370. // one set of quotes in the arguments
  1371. if(count > 2)
  1372. {
  1373. cmRegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  1374. if(quoted.find(command))
  1375. {
  1376. std::string shortCmd;
  1377. std::string cmd = quoted.match(1);
  1378. std::string args = quoted.match(2);
  1379. if(! cmSystemTools::FileExists(cmd.c_str()) )
  1380. {
  1381. shortCmd = cmd;
  1382. }
  1383. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  1384. {
  1385. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  1386. return false;
  1387. }
  1388. shortCmd += " ";
  1389. shortCmd += args;
  1390. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  1391. // output, retVal, verbose);
  1392. //return WindowsRunCommand(shortCmd.c_str(), dir,
  1393. //output, retVal, verbose);
  1394. return RunCommandViaWin32(shortCmd.c_str(), dir,
  1395. output, retVal, verbose, timeout);
  1396. }
  1397. else
  1398. {
  1399. cmSystemTools::Error("Could not parse command line with quotes ",
  1400. command);
  1401. }
  1402. }
  1403. }
  1404. // if there is only one set of quotes or no quotes then just run the command
  1405. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  1406. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  1407. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  1408. #else
  1409. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  1410. #endif
  1411. }
  1412. /**
  1413. * Find the file the given name. Searches the given path and then
  1414. * the system search path. Returns the full path to the file if it is
  1415. * found. Otherwise, the empty string is returned.
  1416. */
  1417. std::string cmSystemTools::FindFile(const char* name,
  1418. const std::vector<std::string>& userPaths)
  1419. {
  1420. // Add the system search path to our path.
  1421. std::vector<std::string> path = userPaths;
  1422. cmSystemTools::GetPath(path);
  1423. std::string tryPath;
  1424. for(std::vector<std::string>::const_iterator p = path.begin();
  1425. p != path.end(); ++p)
  1426. {
  1427. tryPath = *p;
  1428. tryPath += "/";
  1429. tryPath += name;
  1430. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1431. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1432. {
  1433. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1434. }
  1435. }
  1436. // Couldn't find the file.
  1437. return "";
  1438. }
  1439. /**
  1440. * Find the executable with the given name. Searches the given path and then
  1441. * the system search path. Returns the full path to the executable if it is
  1442. * found. Otherwise, the empty string is returned.
  1443. */
  1444. std::string cmSystemTools::FindProgram(const char* name,
  1445. const std::vector<std::string>& userPaths,
  1446. bool no_system_path)
  1447. {
  1448. // See if the executable exists as written.
  1449. if(cmSystemTools::FileExists(name) &&
  1450. !cmSystemTools::FileIsDirectory(name))
  1451. {
  1452. return cmSystemTools::CollapseFullPath(name);
  1453. }
  1454. std::string tryPath = name;
  1455. tryPath += cmSystemTools::GetExecutableExtension();
  1456. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1457. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1458. {
  1459. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1460. }
  1461. // Add the system search path to our path.
  1462. std::vector<std::string> path = userPaths;
  1463. if (!no_system_path)
  1464. {
  1465. cmSystemTools::GetPath(path);
  1466. }
  1467. for(std::vector<std::string>::const_iterator p = path.begin();
  1468. p != path.end(); ++p)
  1469. {
  1470. tryPath = *p;
  1471. tryPath += "/";
  1472. tryPath += name;
  1473. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1474. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1475. {
  1476. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1477. }
  1478. #ifdef _WIN32
  1479. tryPath += ".com";
  1480. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1481. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1482. {
  1483. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1484. }
  1485. tryPath = *p;
  1486. tryPath += "/";
  1487. tryPath += name;
  1488. #endif
  1489. tryPath += cmSystemTools::GetExecutableExtension();
  1490. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1491. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1492. {
  1493. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1494. }
  1495. }
  1496. // Couldn't find the program.
  1497. return "";
  1498. }
  1499. /**
  1500. * Find the library with the given name. Searches the given path and then
  1501. * the system search path. Returns the full path to the library if it is
  1502. * found. Otherwise, the empty string is returned.
  1503. */
  1504. std::string cmSystemTools::FindLibrary(const char* name,
  1505. const std::vector<std::string>& userPaths,
  1506. const cmMakefile *makefile)
  1507. {
  1508. // See if the executable exists as written.
  1509. if(cmSystemTools::FileExists(name))
  1510. {
  1511. return cmSystemTools::CollapseFullPath(name);
  1512. }
  1513. // Add the system search path to our path.
  1514. std::vector<std::string> path = userPaths;
  1515. cmSystemTools::GetPath(path);
  1516. // Add some lib directories specific to compilers, depending on the
  1517. // current generator, so that library that might have been stored here
  1518. // can be found too.
  1519. // i.e. Microsoft Visual Studio or .Net: path to compiler/../Lib
  1520. // Borland: path to compiler/../Lib
  1521. if (makefile)
  1522. {
  1523. const char* genName = makefile->GetDefinition("CMAKE_GENERATOR");
  1524. if (genName)
  1525. {
  1526. if (!strcmp(genName, "NMake Makefiles") ||
  1527. !strcmp(genName, "Visual Studio 6"))
  1528. {
  1529. const char* compiler = makefile->GetDefinition("CMAKE_CXX_COMPILER");
  1530. if (compiler)
  1531. {
  1532. std::string compiler_path = cmSystemTools::FindProgram(compiler);
  1533. if (compiler_path.size())
  1534. {
  1535. std::string lib_path =
  1536. cmSystemTools::GetFilenamePath(
  1537. cmSystemTools::GetFilenamePath(compiler_path)) + "/Lib";
  1538. path.push_back(lib_path);
  1539. }
  1540. }
  1541. }
  1542. else if (!strcmp(genName, "Visual Studio 7"))
  1543. {
  1544. // It is likely that the compiler won't be in the path for .Net, but
  1545. // we know where devenv is.
  1546. const char* devenv = makefile->GetDefinition("MICROSOFT_DEVENV");
  1547. if (devenv)
  1548. {
  1549. std::string devenv_path = cmSystemTools::FindProgram(devenv);
  1550. if (devenv_path.size())
  1551. {
  1552. std::string vc7_path =
  1553. cmSystemTools::GetFilenamePath(
  1554. cmSystemTools::GetFilenamePath(
  1555. cmSystemTools::GetFilenamePath(devenv_path))) + "/Vc7";
  1556. path.push_back(vc7_path + "/lib");
  1557. path.push_back(vc7_path + "/PlatformSDK/lib");
  1558. }
  1559. }
  1560. }
  1561. else if (!strcmp(genName, "Borland Makefiles"))
  1562. {
  1563. const char* bcb_bin_path = makefile->GetDefinition("BCB_BIN_PATH");
  1564. if (bcb_bin_path)
  1565. {
  1566. std::string lib_path =
  1567. cmSystemTools::GetFilenamePath(bcb_bin_path) + "/Lib";
  1568. path.push_back(lib_path);
  1569. }
  1570. }
  1571. }
  1572. }
  1573. std::string tryPath;
  1574. for(std::vector<std::string>::const_iterator p = path.begin();
  1575. p != path.end(); ++p)
  1576. {
  1577. #if defined(_WIN32) && !defined(__CYGWIN__)
  1578. tryPath = *p;
  1579. tryPath += "/";
  1580. tryPath += name;
  1581. tryPath += ".lib";
  1582. if(cmSystemTools::FileExists(tryPath.c_str()))
  1583. {
  1584. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1585. }
  1586. #else
  1587. tryPath = *p;
  1588. tryPath += "/lib";
  1589. tryPath += name;
  1590. tryPath += ".so";
  1591. if(cmSystemTools::FileExists(tryPath.c_str()))
  1592. {
  1593. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1594. }
  1595. tryPath = *p;
  1596. tryPath += "/lib";
  1597. tryPath += name;
  1598. tryPath += ".a";
  1599. if(cmSystemTools::FileExists(tryPath.c_str()))
  1600. {
  1601. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1602. }
  1603. tryPath = *p;
  1604. tryPath += "/lib";
  1605. tryPath += name;
  1606. tryPath += ".sl";
  1607. if(cmSystemTools::FileExists(tryPath.c_str()))
  1608. {
  1609. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1610. }
  1611. tryPath = *p;
  1612. tryPath += "/lib";
  1613. tryPath += name;
  1614. tryPath += ".dylib";
  1615. if(cmSystemTools::FileExists(tryPath.c_str()))
  1616. {
  1617. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1618. }
  1619. #endif
  1620. }
  1621. // Couldn't find the library.
  1622. return "";
  1623. }
  1624. bool cmSystemTools::FileIsDirectory(const char* name)
  1625. {
  1626. struct stat fs;
  1627. if(stat(name, &fs) == 0)
  1628. {
  1629. #if _WIN32
  1630. return ((fs.st_mode & _S_IFDIR) != 0);
  1631. #else
  1632. return S_ISDIR(fs.st_mode);
  1633. #endif
  1634. }
  1635. else
  1636. {
  1637. return false;
  1638. }
  1639. }
  1640. int cmSystemTools::ChangeDirectory(const char *dir)
  1641. {
  1642. return Chdir(dir);
  1643. }
  1644. std::string cmSystemTools::GetCurrentWorkingDirectory()
  1645. {
  1646. char buf[2048];
  1647. std::string path = Getcwd(buf, 2048);
  1648. return path;
  1649. }
  1650. /**
  1651. * Given the path to a program executable, get the directory part of the path with the
  1652. * file stripped off. If there is no directory part, the empty string is returned.
  1653. */
  1654. std::string cmSystemTools::GetProgramPath(const char* in_name)
  1655. {
  1656. std::string dir, file;
  1657. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1658. return dir;
  1659. }
  1660. /**
  1661. * Given the path to a program executable, get the directory part of the path
  1662. * with the file stripped off. If there is no directory part, the empty
  1663. * string is returned.
  1664. */
  1665. void cmSystemTools::SplitProgramPath(const char* in_name,
  1666. std::string& dir,
  1667. std::string& file)
  1668. {
  1669. dir = in_name;
  1670. file = "";
  1671. cmSystemTools::ConvertToUnixSlashes(dir);
  1672. if(!cmSystemTools::FileIsDirectory(dir.c_str()))
  1673. {
  1674. std::string::size_type slashPos = dir.rfind("/");
  1675. if(slashPos != std::string::npos)
  1676. {
  1677. file = dir.substr(slashPos+1);
  1678. dir = dir.substr(0, slashPos);
  1679. }
  1680. else
  1681. {
  1682. file = dir;
  1683. dir = "";
  1684. }
  1685. }
  1686. if((dir != "") && !cmSystemTools::FileIsDirectory(dir.c_str()))
  1687. {
  1688. std::string oldDir = in_name;
  1689. cmSystemTools::ConvertToUnixSlashes(oldDir);
  1690. cmSystemTools::Error("Error splitting file name off end of path:\n",
  1691. oldDir.c_str(), "\nDirectory not found: ",
  1692. dir.c_str());
  1693. dir = in_name;
  1694. return;
  1695. }
  1696. }
  1697. /**
  1698. * Given a path to a file or directory, convert it to a full path.
  1699. * This collapses away relative paths. The full path is returned.
  1700. */
  1701. std::string cmSystemTools::CollapseFullPath(const char* in_name)
  1702. {
  1703. std::string dir, file;
  1704. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1705. #ifdef _WIN32
  1706. // Ultra-hack warning:
  1707. // This changes to the target directory, saves the working directory,
  1708. // and then changes back to the original working directory.
  1709. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1710. if(dir != "") { Chdir(dir.c_str()); }
  1711. std::string newDir = cmSystemTools::GetCurrentWorkingDirectory();
  1712. Chdir(cwd.c_str());
  1713. cmSystemTools::ConvertToUnixSlashes(newDir);
  1714. std::string newPath = newDir+"/"+file;
  1715. return newPath;
  1716. #else
  1717. # ifdef MAXPATHLEN
  1718. char resolved_name[MAXPATHLEN];
  1719. # else
  1720. # ifdef PATH_MAX
  1721. char resolved_name[PATH_MAX];
  1722. # else
  1723. char resolved_name[5024];
  1724. # endif
  1725. # endif
  1726. if(dir != "")
  1727. {
  1728. realpath(dir.c_str(), resolved_name);
  1729. dir = resolved_name;
  1730. }
  1731. else
  1732. {
  1733. dir = cmSystemTools::GetCurrentWorkingDirectory();
  1734. }
  1735. if(file == "")
  1736. {
  1737. return dir;
  1738. }
  1739. return dir + "/" + file;
  1740. #endif
  1741. }
  1742. /**
  1743. * Return path of a full filename (no trailing slashes).
  1744. * Warning: returned path is converted to Unix slashes format.
  1745. */
  1746. std::string cmSystemTools::GetFilenamePath(const std::string& filename)
  1747. {
  1748. std::string fn = filename;
  1749. cmSystemTools::ConvertToUnixSlashes(fn);
  1750. std::string::size_type slash_pos = fn.rfind("/");
  1751. if(slash_pos != std::string::npos)
  1752. {
  1753. return fn.substr(0, slash_pos);
  1754. }
  1755. else
  1756. {
  1757. return "";
  1758. }
  1759. }
  1760. /**
  1761. * Return file name of a full filename (i.e. file name without path).
  1762. */
  1763. std::string cmSystemTools::GetFilenameName(const std::string& filename)
  1764. {
  1765. std::string fn = filename;
  1766. cmSystemTools::ConvertToUnixSlashes(fn);
  1767. std::string::size_type slash_pos = fn.rfind("/");
  1768. if(slash_pos != std::string::npos)
  1769. {
  1770. return fn.substr(slash_pos + 1);
  1771. }
  1772. else
  1773. {
  1774. return filename;
  1775. }
  1776. }
  1777. /**
  1778. * Return file extension of a full filename (dot included).
  1779. * Warning: this is the longest extension (for example: .tar.gz)
  1780. */
  1781. std::string cmSystemTools::GetFilenameExtension(const std::string& filename)
  1782. {
  1783. std::string name = cmSystemTools::GetFilenameName(filename);
  1784. std::string::size_type dot_pos = name.find(".");
  1785. if(dot_pos != std::string::npos)
  1786. {
  1787. return name.substr(dot_pos);
  1788. }
  1789. else
  1790. {
  1791. return "";
  1792. }
  1793. }
  1794. /**
  1795. * Return file name without extension of a full filename (i.e. without path).
  1796. * Warning: it considers the longest extension (for example: .tar.gz)
  1797. */
  1798. std::string cmSystemTools::GetFilenameWithoutExtension(const std::string& filename)
  1799. {
  1800. std::string name = cmSystemTools::GetFilenameName(filename);
  1801. std::string::size_type dot_pos = name.find(".");
  1802. if(dot_pos != std::string::npos)
  1803. {
  1804. return name.substr(0, dot_pos);
  1805. }
  1806. else
  1807. {
  1808. return name;
  1809. }
  1810. }
  1811. /**
  1812. * Return file name without extension of a full filename (i.e. without path).
  1813. * Warning: it considers the last extension (for example: removes .gz
  1814. * from .tar.gz)
  1815. */
  1816. std::string
  1817. cmSystemTools::GetFilenameWithoutLastExtension(const std::string& filename)
  1818. {
  1819. std::string name = cmSystemTools::GetFilenameName(filename);
  1820. std::string::size_type dot_pos = name.rfind(".");
  1821. if(dot_pos != std::string::npos)
  1822. {
  1823. return name.substr(0, dot_pos);
  1824. }
  1825. else
  1826. {
  1827. return name;
  1828. }
  1829. }
  1830. bool cmSystemTools::FileIsFullPath(const char* in_name)
  1831. {
  1832. std::string name = in_name;
  1833. #if defined(_WIN32)
  1834. // On Windows, the name must be at least two characters long.
  1835. if(name.length() < 2)
  1836. {
  1837. return false;
  1838. }
  1839. if(name[1] == ':')
  1840. {
  1841. return true;
  1842. }
  1843. if(name[0] == '\\')
  1844. {
  1845. return true;
  1846. }
  1847. #else
  1848. // On UNIX, the name must be at least one character long.
  1849. if(name.length() < 1)
  1850. {
  1851. return false;
  1852. }
  1853. #endif
  1854. // On UNIX, the name must begin in a '/'.
  1855. // On Windows, if the name begins in a '/', then it is a full
  1856. // network path.
  1857. if(name[0] == '/')
  1858. {
  1859. return true;
  1860. }
  1861. return false;
  1862. }
  1863. void cmSystemTools::Glob(const char *directory, const char *regexp,
  1864. std::vector<std::string>& files)
  1865. {
  1866. cmDirectory d;
  1867. cmRegularExpression reg(regexp);
  1868. if (d.Load(directory))
  1869. {
  1870. size_t numf;
  1871. unsigned int i;
  1872. numf = d.GetNumberOfFiles();
  1873. for (i = 0; i < numf; i++)
  1874. {
  1875. std::string fname = d.GetFile(i);
  1876. if (reg.find(fname))
  1877. {
  1878. files.push_back(fname);
  1879. }
  1880. }
  1881. }
  1882. }
  1883. void cmSystemTools::GlobDirs(const char *fullPath,
  1884. std::vector<std::string>& files)
  1885. {
  1886. std::string path = fullPath;
  1887. std::string::size_type pos = path.find("/*");
  1888. if(pos == std::string::npos)
  1889. {
  1890. files.push_back(fullPath);
  1891. return;
  1892. }
  1893. std::string startPath = path.substr(0, pos);
  1894. std::string finishPath = path.substr(pos+2);
  1895. cmDirectory d;
  1896. if (d.Load(startPath.c_str()))
  1897. {
  1898. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1899. {
  1900. if((std::string(d.GetFile(i)) != ".")
  1901. && (std::string(d.GetFile(i)) != ".."))
  1902. {
  1903. std::string fname = startPath;
  1904. fname +="/";
  1905. fname += d.GetFile(i);
  1906. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  1907. {
  1908. fname += finishPath;
  1909. cmSystemTools::GlobDirs(fname.c_str(), files);
  1910. }
  1911. }
  1912. }
  1913. }
  1914. }
  1915. void cmSystemTools::ExpandListArguments(std::vector<std::string> const& arguments,
  1916. std::vector<std::string>& newargs)
  1917. {
  1918. std::vector<std::string>::const_iterator i;
  1919. std::string newarg;
  1920. for(i = arguments.begin();i != arguments.end(); ++i)
  1921. {
  1922. // if there are no ; in the name then just copy the current string
  1923. if(i->find(';') == std::string::npos)
  1924. {
  1925. newargs.push_back(*i);
  1926. }
  1927. else
  1928. {
  1929. std::string::size_type start = 0;
  1930. std::string::size_type endpos = 0;
  1931. const std::string::size_type size = i->size();
  1932. // break up ; separated sections of the string into separate strings
  1933. while(endpos != size)
  1934. {
  1935. endpos = i->find(';', start);
  1936. if(endpos == std::string::npos)
  1937. {
  1938. endpos = i->size();
  1939. }
  1940. else
  1941. {
  1942. // skip right over escaped ; ( \; )
  1943. while((endpos != std::string::npos)
  1944. && (endpos > 0)
  1945. && ((*i)[endpos-1] == '\\') )
  1946. {
  1947. endpos = i->find(';', endpos+1);
  1948. }
  1949. if(endpos == std::string::npos)
  1950. {
  1951. endpos = i->size();
  1952. }
  1953. }
  1954. std::string::size_type len = endpos - start;
  1955. if (len > 0)
  1956. {
  1957. // check for a closing ] after the start position
  1958. if(i->find('[', start) == std::string::npos)
  1959. {
  1960. // if there is no [ in the string then keep it
  1961. newarg = i->substr(start, len);
  1962. }
  1963. else
  1964. {
  1965. int opencount = 0;
  1966. int closecount = 0;
  1967. for(std::string::size_type j = start; j < endpos; ++j)
  1968. {
  1969. if(i->at(j) == '[')
  1970. {
  1971. ++opencount;
  1972. }
  1973. else if (i->at(j) == ']')
  1974. {
  1975. ++closecount;
  1976. }
  1977. }
  1978. if(opencount != closecount)
  1979. {
  1980. // skip this one
  1981. endpos = i->find(';', endpos+1);
  1982. if(endpos == std::string::npos)
  1983. {
  1984. endpos = i->size();
  1985. }
  1986. len = endpos - start;
  1987. }
  1988. newarg = i->substr(start, len);
  1989. }
  1990. std::string::size_type pos = newarg.find("\\;");
  1991. if(pos != std::string::npos)
  1992. {
  1993. newarg.erase(pos, 1);
  1994. }
  1995. newargs.push_back(newarg);
  1996. }
  1997. start = endpos+1;
  1998. }
  1999. }
  2000. }
  2001. }
  2002. bool cmSystemTools::GetShortPath(const char* path, std::string& shortPath)
  2003. {
  2004. #if defined(WIN32) && !defined(__CYGWIN__)
  2005. const int size = int(strlen(path)) +1; // size of return
  2006. char *buffer = new char[size]; // create a buffer
  2007. char *tempPath = new char[size]; // create a buffer
  2008. int ret;
  2009. // if the path passed in has quotes around it, first remove the quotes
  2010. if (path[0] == '"' && path[strlen(path)-1] == '"')
  2011. {
  2012. strcpy(tempPath,path+1);
  2013. tempPath[strlen(tempPath)-1] = '\0';
  2014. }
  2015. else
  2016. {
  2017. strcpy(tempPath,path);
  2018. }
  2019. buffer[0] = 0;
  2020. ret = GetShortPathName(tempPath, buffer, size);
  2021. if(buffer[0] == 0 || ret > size)
  2022. {
  2023. if(ret < size)
  2024. {
  2025. LPVOID lpMsgBuf;
  2026. FormatMessage(
  2027. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2028. FORMAT_MESSAGE_FROM_SYSTEM |
  2029. FORMAT_MESSAGE_IGNORE_INSERTS,
  2030. NULL,
  2031. GetLastError(),
  2032. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  2033. (LPTSTR) &lpMsgBuf,
  2034. 0,
  2035. NULL
  2036. );
  2037. cmSystemTools::Error((LPCTSTR)lpMsgBuf);
  2038. LocalFree( lpMsgBuf );
  2039. }
  2040. cmSystemTools::Error("Unable to get a short path: ", path);
  2041. delete [] tempPath;
  2042. return false;
  2043. }
  2044. else
  2045. {
  2046. shortPath = buffer;
  2047. delete [] buffer;
  2048. delete [] tempPath;
  2049. return true;
  2050. }
  2051. #else
  2052. shortPath = path;
  2053. return true;
  2054. #endif
  2055. }
  2056. cmSystemTools::e_FileFormat cmSystemTools::GetFileFormat(const char* cext)
  2057. {
  2058. if ( ! cext || *cext == 0 )
  2059. {
  2060. return cmSystemTools::NO_FILE_FORMAT;
  2061. }
  2062. std::string ext = cmSystemTools::LowerCase(cext);
  2063. if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
  2064. if ( ext == "cxx" || ext == ".cxx" ||
  2065. ext == "cpp" || ext == ".cpp" ||
  2066. ext == "c++" || ext == ".c++" ||
  2067. ext == "cc" || ext == ".cc" ) { return cmSystemTools::CXX_FILE_FORMAT; }
  2068. if ( ext == "java" || ext == ".java" ) { return cmSystemTools::JAVA_FILE_FORMAT; }
  2069. if ( ext == "h" || ext == ".h" ||
  2070. ext == "hpp" || ext == ".hpp" ||
  2071. ext == "h++" || ext == ".h++" ||
  2072. ext == "hxx" || ext == ".hxx" ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  2073. if ( ext == "rc" || ext == ".rc" ) { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  2074. if ( ext == "def" || ext == ".def" ) { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  2075. if ( ext == "lib" || ext == ".lib" ||
  2076. ext == "a" || ext == ".a") { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  2077. if ( ext == "o" || ext == ".o" ||
  2078. ext == "obj" || ext == ".obj") { return cmSystemTools::OBJECT_FILE_FORMAT; }
  2079. #ifdef __APPLE__
  2080. if ( ext == "dylib" || ext == ".dylib" )
  2081. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  2082. if ( ext == "so" || ext == ".so" ||
  2083. ext == "bundle" || ext == ".bundle" )
  2084. { return cmSystemTools::MODULE_FILE_FORMAT; }
  2085. #else // __APPLE__
  2086. if ( ext == "so" || ext == ".so" ||
  2087. ext == "sl" || ext == ".sl" ||
  2088. ext == "dll" || ext == ".dll" )
  2089. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  2090. #endif // __APPLE__
  2091. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  2092. }