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. if(!cmSystemTools::FileExists(path))
  175. {
  176. return false;
  177. }
  178. return true;
  179. }
  180. // replace replace with with as many times as it shows up in source.
  181. // write the result into source.
  182. void cmSystemTools::ReplaceString(std::string& source,
  183. const char* replace,
  184. const char* with)
  185. {
  186. std::string::size_type lengthReplace = strlen(replace);
  187. std::string::size_type lengthWith = strlen(with);
  188. std::string rest;
  189. std::string::size_type start = source.find(replace);
  190. while(start != std::string::npos)
  191. {
  192. rest = source.substr(start+lengthReplace);
  193. source = source.substr(0, start);
  194. source += with;
  195. source += rest;
  196. start = source.find(replace, start + lengthWith );
  197. }
  198. }
  199. // Read a registry value.
  200. // Example :
  201. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  202. // => will return the data of the "default" value of the key
  203. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  204. // => will return the data of the "Root" value of the key
  205. #if defined(_WIN32) && !defined(__CYGWIN__)
  206. bool cmSystemTools::ReadRegistryValue(const char *key, std::string &value)
  207. {
  208. std::string primary = key;
  209. std::string second;
  210. std::string valuename;
  211. size_t start = primary.find("\\");
  212. if (start == std::string::npos)
  213. {
  214. return false;
  215. }
  216. size_t valuenamepos = primary.find(";");
  217. if (valuenamepos != std::string::npos)
  218. {
  219. valuename = primary.substr(valuenamepos+1);
  220. }
  221. second = primary.substr(start+1, valuenamepos-start-1);
  222. primary = primary.substr(0, start);
  223. HKEY primaryKey;
  224. if (primary == "HKEY_CURRENT_USER")
  225. {
  226. primaryKey = HKEY_CURRENT_USER;
  227. }
  228. if (primary == "HKEY_CURRENT_CONFIG")
  229. {
  230. primaryKey = HKEY_CURRENT_CONFIG;
  231. }
  232. if (primary == "HKEY_CLASSES_ROOT")
  233. {
  234. primaryKey = HKEY_CLASSES_ROOT;
  235. }
  236. if (primary == "HKEY_LOCAL_MACHINE")
  237. {
  238. primaryKey = HKEY_LOCAL_MACHINE;
  239. }
  240. if (primary == "HKEY_USERS")
  241. {
  242. primaryKey = HKEY_USERS;
  243. }
  244. HKEY hKey;
  245. if(RegOpenKeyEx(primaryKey,
  246. second.c_str(),
  247. 0,
  248. KEY_READ,
  249. &hKey) != ERROR_SUCCESS)
  250. {
  251. return false;
  252. }
  253. else
  254. {
  255. DWORD dwType, dwSize;
  256. dwSize = 1023;
  257. char data[1024];
  258. if(RegQueryValueEx(hKey,
  259. (LPTSTR)valuename.c_str(),
  260. NULL,
  261. &dwType,
  262. (BYTE *)data,
  263. &dwSize) == ERROR_SUCCESS)
  264. {
  265. if (dwType == REG_SZ)
  266. {
  267. value = data;
  268. return true;
  269. }
  270. }
  271. }
  272. return false;
  273. }
  274. #else
  275. bool cmSystemTools::ReadRegistryValue(const char *, std::string &)
  276. {
  277. return false;
  278. }
  279. #endif
  280. // Write a registry value.
  281. // Example :
  282. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  283. // => will set the data of the "default" value of the key
  284. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  285. // => will set the data of the "Root" value of the key
  286. #if defined(_WIN32) && !defined(__CYGWIN__)
  287. bool cmSystemTools::WriteRegistryValue(const char *key, const char *value)
  288. {
  289. std::string primary = key;
  290. std::string second;
  291. std::string valuename;
  292. size_t start = primary.find("\\");
  293. if (start == std::string::npos)
  294. {
  295. return false;
  296. }
  297. size_t valuenamepos = primary.find(";");
  298. if (valuenamepos != std::string::npos)
  299. {
  300. valuename = primary.substr(valuenamepos+1);
  301. }
  302. second = primary.substr(start+1, valuenamepos-start-1);
  303. primary = primary.substr(0, start);
  304. HKEY primaryKey;
  305. if (primary == "HKEY_CURRENT_USER")
  306. {
  307. primaryKey = HKEY_CURRENT_USER;
  308. }
  309. if (primary == "HKEY_CURRENT_CONFIG")
  310. {
  311. primaryKey = HKEY_CURRENT_CONFIG;
  312. }
  313. if (primary == "HKEY_CLASSES_ROOT")
  314. {
  315. primaryKey = HKEY_CLASSES_ROOT;
  316. }
  317. if (primary == "HKEY_LOCAL_MACHINE")
  318. {
  319. primaryKey = HKEY_LOCAL_MACHINE;
  320. }
  321. if (primary == "HKEY_USERS")
  322. {
  323. primaryKey = HKEY_USERS;
  324. }
  325. HKEY hKey;
  326. DWORD dwDummy;
  327. if(RegCreateKeyEx(primaryKey,
  328. second.c_str(),
  329. 0,
  330. "",
  331. REG_OPTION_NON_VOLATILE,
  332. KEY_WRITE,
  333. NULL,
  334. &hKey,
  335. &dwDummy) != ERROR_SUCCESS)
  336. {
  337. return false;
  338. }
  339. if(RegSetValueEx(hKey,
  340. (LPTSTR)valuename.c_str(),
  341. 0,
  342. REG_SZ,
  343. (CONST BYTE *)value,
  344. (DWORD)(strlen(value) + 1)) == ERROR_SUCCESS)
  345. {
  346. return true;
  347. }
  348. return false;
  349. }
  350. #else
  351. bool cmSystemTools::WriteRegistryValue(const char *, const char *)
  352. {
  353. return false;
  354. }
  355. #endif
  356. // Delete a registry value.
  357. // Example :
  358. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  359. // => will delete the data of the "default" value of the key
  360. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  361. // => will delete the data of the "Root" value of the key
  362. #if defined(_WIN32) && !defined(__CYGWIN__)
  363. bool cmSystemTools::DeleteRegistryValue(const char *key)
  364. {
  365. std::string primary = key;
  366. std::string second;
  367. std::string valuename;
  368. size_t start = primary.find("\\");
  369. if (start == std::string::npos)
  370. {
  371. return false;
  372. }
  373. size_t valuenamepos = primary.find(";");
  374. if (valuenamepos != std::string::npos)
  375. {
  376. valuename = primary.substr(valuenamepos+1);
  377. }
  378. second = primary.substr(start+1, valuenamepos-start-1);
  379. primary = primary.substr(0, start);
  380. HKEY primaryKey;
  381. if (primary == "HKEY_CURRENT_USER")
  382. {
  383. primaryKey = HKEY_CURRENT_USER;
  384. }
  385. if (primary == "HKEY_CURRENT_CONFIG")
  386. {
  387. primaryKey = HKEY_CURRENT_CONFIG;
  388. }
  389. if (primary == "HKEY_CLASSES_ROOT")
  390. {
  391. primaryKey = HKEY_CLASSES_ROOT;
  392. }
  393. if (primary == "HKEY_LOCAL_MACHINE")
  394. {
  395. primaryKey = HKEY_LOCAL_MACHINE;
  396. }
  397. if (primary == "HKEY_USERS")
  398. {
  399. primaryKey = HKEY_USERS;
  400. }
  401. HKEY hKey;
  402. if(RegOpenKeyEx(primaryKey,
  403. second.c_str(),
  404. 0,
  405. KEY_WRITE,
  406. &hKey) != ERROR_SUCCESS)
  407. {
  408. return false;
  409. }
  410. else
  411. {
  412. if(RegDeleteValue(hKey,
  413. (LPTSTR)valuename.c_str()) == ERROR_SUCCESS)
  414. {
  415. return true;
  416. }
  417. }
  418. return false;
  419. }
  420. #else
  421. bool cmSystemTools::DeleteRegistryValue(const char *)
  422. {
  423. return false;
  424. }
  425. #endif
  426. // replace replace with with as many times as it shows up in source.
  427. // write the result into source.
  428. #if defined(_WIN32) && !defined(__CYGWIN__)
  429. void cmSystemTools::ExpandRegistryValues(std::string& source)
  430. {
  431. // Regular expression to match anything inside [...] that begins in HKEY.
  432. // Note that there is a special rule for regular expressions to match a
  433. // close square-bracket inside a list delimited by square brackets.
  434. // The "[^]]" part of this expression will match any character except
  435. // a close square-bracket. The ']' character must be the first in the
  436. // list of characters inside the [^...] block of the expression.
  437. cmRegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  438. // check for black line or comment
  439. while (regEntry.find(source))
  440. {
  441. // the arguments are the second match
  442. std::string key = regEntry.match(1);
  443. std::string val;
  444. if (ReadRegistryValue(key.c_str(), val))
  445. {
  446. std::string reg = "[";
  447. reg += key + "]";
  448. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  449. }
  450. else
  451. {
  452. std::string reg = "[";
  453. reg += key + "]";
  454. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  455. }
  456. }
  457. }
  458. #else
  459. void cmSystemTools::ExpandRegistryValues(std::string&)
  460. {
  461. }
  462. #endif
  463. std::string cmSystemTools::EscapeQuotes(const char* str)
  464. {
  465. std::string result = "";
  466. for(const char* ch = str; *ch != '\0'; ++ch)
  467. {
  468. if(*ch == '"')
  469. {
  470. result += '\\';
  471. }
  472. result += *ch;
  473. }
  474. return result;
  475. }
  476. bool cmSystemTools::SameFile(const char* file1, const char* file2)
  477. {
  478. #ifdef _WIN32
  479. HANDLE hFile1, hFile2;
  480. hFile1 = CreateFile( file1,
  481. GENERIC_READ,
  482. FILE_SHARE_READ ,
  483. NULL,
  484. OPEN_EXISTING,
  485. FILE_FLAG_BACKUP_SEMANTICS,
  486. NULL
  487. );
  488. hFile2 = CreateFile( file2,
  489. GENERIC_READ,
  490. FILE_SHARE_READ,
  491. NULL,
  492. OPEN_EXISTING,
  493. FILE_FLAG_BACKUP_SEMANTICS,
  494. NULL
  495. );
  496. if( hFile1 == INVALID_HANDLE_VALUE || hFile2 == INVALID_HANDLE_VALUE)
  497. {
  498. if(hFile1 != INVALID_HANDLE_VALUE)
  499. {
  500. CloseHandle(hFile1);
  501. }
  502. if(hFile2 != INVALID_HANDLE_VALUE)
  503. {
  504. CloseHandle(hFile2);
  505. }
  506. return false;
  507. }
  508. BY_HANDLE_FILE_INFORMATION fiBuf1;
  509. BY_HANDLE_FILE_INFORMATION fiBuf2;
  510. GetFileInformationByHandle( hFile1, &fiBuf1 );
  511. GetFileInformationByHandle( hFile2, &fiBuf2 );
  512. CloseHandle(hFile1);
  513. CloseHandle(hFile2);
  514. return (fiBuf1.nFileIndexHigh == fiBuf2.nFileIndexHigh &&
  515. fiBuf1.nFileIndexLow == fiBuf2.nFileIndexLow);
  516. #else
  517. struct stat fileStat1, fileStat2;
  518. if (stat(file1, &fileStat1) == 0 && stat(file2, &fileStat2) == 0)
  519. {
  520. // see if the files are the same file
  521. // check the device inode and size
  522. if(memcmp(&fileStat2.st_dev, &fileStat1.st_dev, sizeof(fileStat1.st_dev)) == 0 &&
  523. memcmp(&fileStat2.st_ino, &fileStat1.st_ino, sizeof(fileStat1.st_ino)) == 0 &&
  524. fileStat2.st_size == fileStat1.st_size
  525. )
  526. {
  527. return true;
  528. }
  529. }
  530. return false;
  531. #endif
  532. }
  533. // return true if the file exists
  534. bool cmSystemTools::FileExists(const char* filename)
  535. {
  536. struct stat fs;
  537. if (stat(filename, &fs) != 0)
  538. {
  539. return false;
  540. }
  541. else
  542. {
  543. return true;
  544. }
  545. }
  546. // Return a capitalized string (i.e the first letter is uppercased, all other
  547. // are lowercased)
  548. std::string cmSystemTools::Capitalized(const std::string& s)
  549. {
  550. std::string n;
  551. n.resize(s.size());
  552. n[0] = toupper(s[0]);
  553. for (size_t i = 1; i < s.size(); i++)
  554. {
  555. n[i] = tolower(s[i]);
  556. }
  557. return n;
  558. }
  559. // Return a lower case string
  560. std::string cmSystemTools::LowerCase(const std::string& s)
  561. {
  562. std::string n;
  563. n.resize(s.size());
  564. for (size_t i = 0; i < s.size(); i++)
  565. {
  566. n[i] = tolower(s[i]);
  567. }
  568. return n;
  569. }
  570. // Return a lower case string
  571. std::string cmSystemTools::UpperCase(const std::string& s)
  572. {
  573. std::string n;
  574. n.resize(s.size());
  575. for (size_t i = 0; i < s.size(); i++)
  576. {
  577. n[i] = toupper(s[i]);
  578. }
  579. return n;
  580. }
  581. // convert windows slashes to unix slashes
  582. void cmSystemTools::ConvertToUnixSlashes(std::string& path)
  583. {
  584. std::string::size_type pos = 0;
  585. while((pos = path.find('\\', pos)) != std::string::npos)
  586. {
  587. path[pos] = '/';
  588. pos++;
  589. }
  590. // remove any trailing slash
  591. if(path.size() && path[path.size()-1] == '/')
  592. {
  593. path = path.substr(0, path.size()-1);
  594. }
  595. // if there is a tilda ~ then replace it with HOME
  596. if(path.find("~") == 0)
  597. {
  598. if (getenv("HOME"))
  599. {
  600. path = std::string(getenv("HOME")) + path.substr(1);
  601. }
  602. }
  603. // if there is a /tmp_mnt in a path get rid of it!
  604. // stupid sgi's
  605. if(path.find("/tmp_mnt") == 0)
  606. {
  607. path = path.substr(8);
  608. }
  609. }
  610. // change // to /, and escape any spaces in the path
  611. std::string cmSystemTools::ConvertToUnixOutputPath(const char* path)
  612. {
  613. std::string ret = path;
  614. // remove // except at the beginning might be a cygwin drive
  615. std::string::size_type pos = 1;
  616. while((pos = ret.find("//", pos)) != std::string::npos)
  617. {
  618. ret.erase(pos, 1);
  619. }
  620. // now escape spaces if there is a space in the path
  621. if(ret.find(" ") != std::string::npos)
  622. {
  623. std::string result = "";
  624. char lastch = 1;
  625. for(const char* ch = ret.c_str(); *ch != '\0'; ++ch)
  626. {
  627. // if it is already escaped then don't try to escape it again
  628. if(*ch == ' ' && lastch != '\\')
  629. {
  630. result += '\\';
  631. }
  632. result += *ch;
  633. lastch = *ch;
  634. }
  635. ret = result;
  636. }
  637. return ret;
  638. }
  639. std::string cmSystemTools::EscapeSpaces(const char* str)
  640. {
  641. #if defined(_WIN32) && !defined(__CYGWIN__)
  642. std::string result;
  643. // if there are spaces
  644. std::string temp = str;
  645. if (temp.find(" ") != std::string::npos &&
  646. temp.find("\"")==std::string::npos)
  647. {
  648. result = "\"";
  649. result += str;
  650. result += "\"";
  651. return result;
  652. }
  653. return str;
  654. #else
  655. std::string result = "";
  656. for(const char* ch = str; *ch != '\0'; ++ch)
  657. {
  658. if(*ch == ' ')
  659. {
  660. result += '\\';
  661. }
  662. result += *ch;
  663. }
  664. return result;
  665. #endif
  666. }
  667. std::string cmSystemTools::ConvertToOutputPath(const char* path)
  668. {
  669. #if defined(_WIN32) && !defined(__CYGWIN__)
  670. return cmSystemTools::ConvertToWindowsOutputPath(path);
  671. #else
  672. return cmSystemTools::ConvertToUnixOutputPath(path);
  673. #endif
  674. }
  675. // remove double slashes not at the start
  676. std::string cmSystemTools::ConvertToWindowsOutputPath(const char* path)
  677. {
  678. std::string ret = path;
  679. std::string::size_type pos = 0;
  680. // first convert all of the slashes
  681. while((pos = ret.find('/', pos)) != std::string::npos)
  682. {
  683. ret[pos] = '\\';
  684. pos++;
  685. }
  686. // check for really small paths
  687. if(ret.size() < 2)
  688. {
  689. return ret;
  690. }
  691. // now clean up a bit and remove double slashes
  692. // Only if it is not the first position in the path which is a network
  693. // path on windows
  694. pos = 1; // start at position 1
  695. while((pos = ret.find("\\\\", pos)) != std::string::npos)
  696. {
  697. ret.erase(pos, 1);
  698. }
  699. // now double quote the path if it has spaces in it
  700. // and is not already double quoted
  701. if(ret.find(" ") != std::string::npos
  702. && ret[0] != '\"')
  703. {
  704. std::string result;
  705. result = "\"" + ret + "\"";
  706. ret = result;
  707. }
  708. return ret;
  709. }
  710. inline void RemoveComments(char* ptr)
  711. {
  712. while(*ptr)
  713. {
  714. if(*ptr == '#')
  715. {
  716. *ptr = 0;
  717. break;
  718. }
  719. ++ptr;
  720. }
  721. }
  722. bool cmSystemTools::ParseFunction(std::ifstream& fin,
  723. std::string& name,
  724. std::vector<std::string>& arguments,
  725. const char* filename,
  726. bool& parseError)
  727. {
  728. parseError = false;
  729. name = "";
  730. arguments = std::vector<std::string>();
  731. const int BUFFER_SIZE = 4096;
  732. char inbuffer[BUFFER_SIZE];
  733. if(!fin)
  734. {
  735. return false;
  736. }
  737. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  738. {
  739. RemoveComments(inbuffer);
  740. cmRegularExpression blankLine("^[ \t\r]*$");
  741. // cmRegularExpression comment("^[ \t]*#.*$");
  742. cmRegularExpression oneLiner("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)\\)[ \t\r]*$");
  743. cmRegularExpression multiLine("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)$");
  744. cmRegularExpression lastLine("^(.*)\\)[ \t\r]*$");
  745. // check for blank line or comment
  746. if(blankLine.find(inbuffer) )
  747. {
  748. return false;
  749. }
  750. // look for a oneline fun(arg arg2)
  751. else if(oneLiner.find(inbuffer))
  752. {
  753. // the arguments are the second match
  754. std::string args = oneLiner.match(2);
  755. name = oneLiner.match(1);
  756. // break up the arguments
  757. cmSystemTools::GetArguments(args, arguments);
  758. return true;
  759. }
  760. // look for a start of a multiline with no trailing ")" fun(arg arg2
  761. else if(multiLine.find(inbuffer))
  762. {
  763. name = multiLine.match(1);
  764. std::string args = multiLine.match(2);
  765. cmSystemTools::GetArguments(args, arguments);
  766. // Read lines until the closing paren is hit
  767. bool done = false;
  768. while(!done)
  769. {
  770. // read lines until the end paren is found
  771. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  772. {
  773. RemoveComments(inbuffer);
  774. // Check for comment lines and ignore them.
  775. if(blankLine.find(inbuffer))
  776. { continue; }
  777. // Is this the last line?
  778. if(lastLine.find(inbuffer))
  779. {
  780. done = true;
  781. std::string args = lastLine.match(1);
  782. cmSystemTools::GetArguments(args, arguments);
  783. }
  784. else
  785. {
  786. std::string line = inbuffer;
  787. cmSystemTools::GetArguments(line, arguments);
  788. }
  789. }
  790. else
  791. {
  792. parseError = true;
  793. cmSystemTools::Error("Parse error in read function missing end )\nIn File: ",
  794. filename, "\nCurrent line:", inbuffer);
  795. return false;
  796. }
  797. }
  798. return true;
  799. }
  800. else
  801. {
  802. parseError = true;
  803. cmSystemTools::Error("Parse error in read function\nIn file:",
  804. filename, "\nCurrent line:", inbuffer);
  805. return false;
  806. }
  807. }
  808. return false;
  809. }
  810. void cmSystemTools::GetArguments(std::string& line,
  811. std::vector<std::string>& arguments)
  812. {
  813. // Match a normal argument (not quoted, no spaces).
  814. cmRegularExpression normalArgument("[ \t]*(([^ \t\r\\]|[\\].)+)[ \t\r]*");
  815. // Match a quoted argument (surrounded by double quotes, spaces allowed).
  816. cmRegularExpression quotedArgument("[ \t]*(\"([^\"\\]|[\\].)*\")[ \t\r]*");
  817. bool done = false;
  818. while(!done)
  819. {
  820. std::string arg;
  821. std::string::size_type endpos=0;
  822. bool foundQuoted = quotedArgument.find(line.c_str());
  823. bool foundNormal = normalArgument.find(line.c_str());
  824. if(foundQuoted && foundNormal)
  825. {
  826. // Both matches were found. Take the earlier one.
  827. // Favor double-quoted version if there is a tie.
  828. if(normalArgument.start(1) < quotedArgument.start(1))
  829. {
  830. arg = normalArgument.match(1);
  831. endpos = normalArgument.end(1);
  832. }
  833. else
  834. {
  835. arg = quotedArgument.match(1);
  836. endpos = quotedArgument.end(1);
  837. // Strip off the double quotes on the ends.
  838. arg = arg.substr(1, arg.length()-2);
  839. }
  840. }
  841. else if (foundQuoted)
  842. {
  843. arg = quotedArgument.match(1);
  844. endpos = quotedArgument.end(1);
  845. // Strip off the double quotes on the ends.
  846. arg = arg.substr(1, arg.length()-2);
  847. }
  848. else if(foundNormal)
  849. {
  850. arg = normalArgument.match(1);
  851. endpos = normalArgument.end(1);
  852. }
  853. else
  854. {
  855. done = true;
  856. }
  857. if(!done)
  858. {
  859. arguments.push_back(cmSystemTools::RemoveEscapes(arg.c_str()));
  860. line = line.substr(endpos, line.length() - endpos);
  861. }
  862. }
  863. }
  864. std::string cmSystemTools::RemoveEscapes(const char* s)
  865. {
  866. std::string result = "";
  867. for(const char* ch = s; *ch; ++ch)
  868. {
  869. if(*ch == '\\' && *(ch+1) != ';')
  870. {
  871. ++ch;
  872. switch (*ch)
  873. {
  874. case '\\': result.insert(result.end(), '\\'); break;
  875. case '"': result.insert(result.end(), '"'); break;
  876. case ' ': result.insert(result.end(), ' '); break;
  877. case 't': result.insert(result.end(), '\t'); break;
  878. case 'n': result.insert(result.end(), '\n'); break;
  879. case 'r': result.insert(result.end(), '\r'); break;
  880. case '0': result.insert(result.end(), '\0'); break;
  881. case '\0':
  882. {
  883. cmSystemTools::Error("Trailing backslash in argument:\n", s);
  884. return result;
  885. }
  886. default:
  887. {
  888. std::string chStr(1, *ch);
  889. cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
  890. "\nin argument ", s);
  891. }
  892. }
  893. }
  894. else
  895. {
  896. result.insert(result.end(), *ch);
  897. }
  898. }
  899. return result;
  900. }
  901. void cmSystemTools::Error(const char* m1, const char* m2,
  902. const char* m3, const char* m4)
  903. {
  904. std::string message = "CMake Error: ";
  905. if(m1)
  906. {
  907. message += m1;
  908. }
  909. if(m2)
  910. {
  911. message += m2;
  912. }
  913. if(m3)
  914. {
  915. message += m3;
  916. }
  917. if(m4)
  918. {
  919. message += m4;
  920. }
  921. cmSystemTools::s_ErrorOccured = true;
  922. cmSystemTools::Message(message.c_str(),"Error");
  923. }
  924. void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
  925. {
  926. s_ErrorCallback = f;
  927. s_ErrorCallbackClientData = clientData;
  928. }
  929. void cmSystemTools::Message(const char* m1, const char *title)
  930. {
  931. if(s_DisableMessages)
  932. {
  933. return;
  934. }
  935. if(s_ErrorCallback)
  936. {
  937. (*s_ErrorCallback)(m1, title, s_DisableMessages, s_ErrorCallbackClientData);
  938. return;
  939. }
  940. else
  941. {
  942. std::cerr << m1 << std::endl << std::flush;
  943. }
  944. }
  945. bool cmSystemTools::CopyFileIfDifferent(const char* source,
  946. const char* destination)
  947. {
  948. if(cmSystemTools::FilesDiffer(source, destination))
  949. {
  950. cmSystemTools::cmCopyFile(source, destination);
  951. return true;
  952. }
  953. return false;
  954. }
  955. bool cmSystemTools::FilesDiffer(const char* source,
  956. const char* destination)
  957. {
  958. struct stat statSource;
  959. if (stat(source, &statSource) != 0)
  960. {
  961. return true;
  962. }
  963. struct stat statDestination;
  964. if (stat(destination, &statDestination) != 0)
  965. {
  966. return true;
  967. }
  968. if(statSource.st_size != statDestination.st_size)
  969. {
  970. return true;
  971. }
  972. if(statSource.st_size == 0)
  973. {
  974. return false;
  975. }
  976. #if defined(_WIN32) || defined(__CYGWIN__)
  977. std::ifstream finSource(source,
  978. std::ios::binary | std::ios::in);
  979. std::ifstream finDestination(destination,
  980. std::ios::binary | std::ios::in);
  981. #else
  982. std::ifstream finSource(source);
  983. std::ifstream finDestination(destination);
  984. #endif
  985. if(!finSource || !finDestination)
  986. {
  987. return true;
  988. }
  989. char* source_buf = new char[statSource.st_size];
  990. char* dest_buf = new char[statSource.st_size];
  991. finSource.read(source_buf, statSource.st_size);
  992. finDestination.read(dest_buf, statSource.st_size);
  993. if(statSource.st_size != static_cast<long>(finSource.gcount()) ||
  994. statSource.st_size != static_cast<long>(finDestination.gcount()))
  995. {
  996. cmStringStream msg;
  997. msg << "FilesDiffer failed to read files (allocated: "
  998. << statSource.st_size << ", read source: " << finSource.gcount()
  999. << ", read dest: " << finDestination.gcount();
  1000. cmSystemTools::Error(msg.str().c_str());
  1001. delete [] source_buf;
  1002. delete [] dest_buf;
  1003. return false;
  1004. }
  1005. int ret = memcmp((const void*)source_buf,
  1006. (const void*)dest_buf,
  1007. statSource.st_size);
  1008. delete [] dest_buf;
  1009. delete [] source_buf;
  1010. return ret != 0;
  1011. }
  1012. /**
  1013. * Copy a file named by "source" to the file named by "destination".
  1014. */
  1015. void cmSystemTools::cmCopyFile(const char* source,
  1016. const char* destination)
  1017. {
  1018. const int bufferSize = 4096;
  1019. char buffer[bufferSize];
  1020. // If destination is a directory, try to create a file with the same
  1021. // name as the source in that directory.
  1022. std::string new_destination;
  1023. if(cmSystemTools::FileExists(destination) &&
  1024. cmSystemTools::FileIsDirectory(destination))
  1025. {
  1026. new_destination = destination;
  1027. cmSystemTools::ConvertToUnixSlashes(new_destination);
  1028. new_destination += '/';
  1029. std::string source_name = source;
  1030. new_destination += cmSystemTools::GetFilenameName(source_name);
  1031. destination = new_destination.c_str();
  1032. }
  1033. // Create destination directory
  1034. std::string destination_dir = destination;
  1035. destination_dir = cmSystemTools::GetFilenamePath(destination_dir);
  1036. cmSystemTools::MakeDirectory(destination_dir.c_str());
  1037. // Open files
  1038. #if defined(_WIN32) || defined(__CYGWIN__)
  1039. std::ifstream fin(source,
  1040. std::ios::binary | std::ios::in);
  1041. #else
  1042. std::ifstream fin(source);
  1043. #endif
  1044. if(!fin)
  1045. {
  1046. cmSystemTools::Error("CopyFile failed to open input file \"",
  1047. source, "\"");
  1048. return;
  1049. }
  1050. #if defined(_WIN32) || defined(__CYGWIN__)
  1051. std::ofstream fout(destination,
  1052. std::ios::binary | std::ios::out | std::ios::trunc);
  1053. #else
  1054. std::ofstream fout(destination,
  1055. std::ios::out | std::ios::trunc);
  1056. #endif
  1057. if(!fout)
  1058. {
  1059. cmSystemTools::Error("CopyFile failed to open output file \"",
  1060. destination, "\"");
  1061. return;
  1062. }
  1063. // This copy loop is very sensitive on certain platforms with
  1064. // slightly broken stream libraries (like HPUX). Normally, it is
  1065. // incorrect to not check the error condition on the fin.read()
  1066. // before using the data, but the fin.gcount() will be zero if an
  1067. // error occurred. Therefore, the loop should be safe everywhere.
  1068. while(fin)
  1069. {
  1070. fin.read(buffer, bufferSize);
  1071. if(fin.gcount())
  1072. {
  1073. fout.write(buffer, fin.gcount());
  1074. }
  1075. }
  1076. fin.close();
  1077. fout.close();
  1078. // More checks
  1079. struct stat statSource, statDestination;
  1080. if (stat(source, &statSource) != 0 ||
  1081. stat(destination, &statDestination) != 0)
  1082. {
  1083. cmSystemTools::Error("CopyFile failed to copy files!");
  1084. }
  1085. if (statSource.st_size != statDestination.st_size)
  1086. {
  1087. cmStringStream msg;
  1088. msg << "CopyFile failed to copy files (sizes differ, source: "
  1089. << statSource.st_size << " , dest: " << statDestination.st_size;
  1090. cmSystemTools::Error(msg.str().c_str());
  1091. }
  1092. }
  1093. // return true if the file exists
  1094. long int cmSystemTools::ModifiedTime(const char* filename)
  1095. {
  1096. struct stat fs;
  1097. if (stat(filename, &fs) != 0)
  1098. {
  1099. return 0;
  1100. }
  1101. else
  1102. {
  1103. return (long int)fs.st_mtime;
  1104. }
  1105. }
  1106. bool cmSystemTools::RemoveFile(const char* source)
  1107. {
  1108. return unlink(source) != 0 ? false : true;
  1109. }
  1110. bool cmSystemTools::IsOn(const char* val)
  1111. {
  1112. if (!val)
  1113. {
  1114. return false;
  1115. }
  1116. std::basic_string<char> v = val;
  1117. for(std::basic_string<char>::iterator c = v.begin();
  1118. c != v.end(); c++)
  1119. {
  1120. *c = toupper(*c);
  1121. }
  1122. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  1123. }
  1124. bool cmSystemTools::IsOff(const char* val)
  1125. {
  1126. if (!val || strlen(val) == 0)
  1127. {
  1128. return true;
  1129. }
  1130. std::basic_string<char> v = val;
  1131. for(std::basic_string<char>::iterator c = v.begin();
  1132. c != v.end(); c++)
  1133. {
  1134. *c = toupper(*c);
  1135. }
  1136. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  1137. v == "N" || v == "NOTFOUND" || v == "IGNORE");
  1138. }
  1139. bool cmSystemTools::RunCommand(const char* command,
  1140. std::string& output,
  1141. const char* dir,
  1142. bool verbose,
  1143. int timeout)
  1144. {
  1145. int foo;
  1146. return cmSystemTools::RunCommand(command, output, foo, dir, verbose, timeout);
  1147. }
  1148. #if defined(WIN32) && !defined(__CYGWIN__)
  1149. #include "cmWin32ProcessExecution.h"
  1150. // use this for shell commands like echo and dir
  1151. bool RunCommandViaWin32(const char* command,
  1152. const char* dir,
  1153. std::string& output,
  1154. int& retVal,
  1155. bool verbose,
  1156. int timeout)
  1157. {
  1158. #if defined(__BORLANDC__)
  1159. return cmWin32ProcessExecution::BorlandRunCommand(command, dir, output,
  1160. retVal,
  1161. verbose, timeout);
  1162. #else // Visual studio
  1163. ::SetLastError(ERROR_SUCCESS);
  1164. if ( ! command )
  1165. {
  1166. cmSystemTools::Error("No command specified");
  1167. return false;
  1168. }
  1169. //std::cout << "Command: " << command << std::endl;
  1170. if ( dir )
  1171. {
  1172. //std::cout << "Dir: " << dir << std::endl;
  1173. }
  1174. cmWin32ProcessExecution resProc;
  1175. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  1176. {
  1177. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  1178. }
  1179. if ( !resProc.StartProcess(command, dir, verbose) )
  1180. {
  1181. std::cout << "Problem starting command" << std::endl;
  1182. return false;
  1183. }
  1184. resProc.Wait(timeout);
  1185. output = resProc.GetOutput();
  1186. retVal = resProc.GetExitValue();
  1187. return true;
  1188. #endif
  1189. }
  1190. // use this for shell commands like echo and dir
  1191. bool RunCommandViaSystem(const char* command,
  1192. const char* dir,
  1193. std::string& output,
  1194. int& retVal,
  1195. bool verbose)
  1196. {
  1197. std::cout << "@@ " << command << std::endl;
  1198. const int BUFFER_SIZE = 4096;
  1199. char buffer[BUFFER_SIZE];
  1200. std::string commandInDir;
  1201. if(dir)
  1202. {
  1203. commandInDir = "cd ";
  1204. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  1205. commandInDir += " && ";
  1206. commandInDir += command;
  1207. }
  1208. else
  1209. {
  1210. commandInDir = command;
  1211. }
  1212. command = commandInDir.c_str();
  1213. std::string commandToFile = command;
  1214. commandToFile += " > ";
  1215. std::string tempFile;
  1216. tempFile += _tempnam(0, "cmake");
  1217. commandToFile += tempFile;
  1218. retVal = system(commandToFile.c_str());
  1219. std::ifstream fin(tempFile.c_str());
  1220. if(!fin)
  1221. {
  1222. if(verbose)
  1223. {
  1224. std::string errormsg = "RunCommand produced no output: command: \"";
  1225. errormsg += command;
  1226. errormsg += "\"";
  1227. errormsg += "\nOutput file: ";
  1228. errormsg += tempFile;
  1229. cmSystemTools::Error(errormsg.c_str());
  1230. }
  1231. fin.close();
  1232. cmSystemTools::RemoveFile(tempFile.c_str());
  1233. return false;
  1234. }
  1235. bool multiLine = false;
  1236. while(fin)
  1237. {
  1238. fin.getline(buffer, BUFFER_SIZE);
  1239. output += buffer;
  1240. if(multiLine)
  1241. {
  1242. output += "\n";
  1243. }
  1244. multiLine = true;
  1245. }
  1246. fin.close();
  1247. cmSystemTools::RemoveFile(tempFile.c_str());
  1248. return true;
  1249. }
  1250. #else // We have popen
  1251. bool RunCommandViaPopen(const char* command,
  1252. const char* dir,
  1253. std::string& output,
  1254. int& retVal,
  1255. bool verbose,
  1256. int /*timeout*/)
  1257. {
  1258. // if only popen worked on windows.....
  1259. std::string commandInDir;
  1260. if(dir)
  1261. {
  1262. commandInDir = "cd ";
  1263. commandInDir += dir;
  1264. commandInDir += " && ";
  1265. commandInDir += command;
  1266. }
  1267. else
  1268. {
  1269. commandInDir = command;
  1270. }
  1271. commandInDir += " 2>&1";
  1272. command = commandInDir.c_str();
  1273. const int BUFFER_SIZE = 4096;
  1274. char buffer[BUFFER_SIZE];
  1275. if(verbose)
  1276. {
  1277. std::cout << "running " << command << std::endl;
  1278. }
  1279. fflush(stdout);
  1280. fflush(stderr);
  1281. FILE* cpipe = popen(command, "r");
  1282. if(!cpipe)
  1283. {
  1284. return false;
  1285. }
  1286. fgets(buffer, BUFFER_SIZE, cpipe);
  1287. while(!feof(cpipe))
  1288. {
  1289. if(verbose)
  1290. {
  1291. std::cout << buffer << std::flush;
  1292. }
  1293. output += buffer;
  1294. fgets(buffer, BUFFER_SIZE, cpipe);
  1295. }
  1296. retVal = pclose(cpipe);
  1297. if (WIFEXITED(retVal))
  1298. {
  1299. retVal = WEXITSTATUS(retVal);
  1300. return true;
  1301. }
  1302. if (WIFSIGNALED(retVal))
  1303. {
  1304. retVal = WTERMSIG(retVal);
  1305. cmStringStream error;
  1306. error << "\nProcess terminated due to ";
  1307. switch (retVal)
  1308. {
  1309. #ifdef SIGKILL
  1310. case SIGKILL:
  1311. error << "SIGKILL";
  1312. break;
  1313. #endif
  1314. #ifdef SIGFPE
  1315. case SIGFPE:
  1316. error << "SIGFPE";
  1317. break;
  1318. #endif
  1319. #ifdef SIGBUS
  1320. case SIGBUS:
  1321. error << "SIGBUS";
  1322. break;
  1323. #endif
  1324. #ifdef SIGSEGV
  1325. case SIGSEGV:
  1326. error << "SIGSEGV";
  1327. break;
  1328. #endif
  1329. default:
  1330. error << "signal " << retVal;
  1331. break;
  1332. }
  1333. output += error.str();
  1334. }
  1335. return false;
  1336. }
  1337. #endif // endif WIN32 not CYGWIN
  1338. // run a command unix uses popen (easy)
  1339. // windows uses system and ShortPath
  1340. bool cmSystemTools::RunCommand(const char* command,
  1341. std::string& output,
  1342. int &retVal,
  1343. const char* dir,
  1344. bool verbose,
  1345. int timeout)
  1346. {
  1347. if(s_DisableRunCommandOutput)
  1348. {
  1349. verbose = false;
  1350. }
  1351. #if defined(WIN32) && !defined(__CYGWIN__)
  1352. // if the command does not start with a quote, then
  1353. // try to find the program, and if the program can not be
  1354. // found use system to run the command as it must be a built in
  1355. // shell command like echo or dir
  1356. int count = 0;
  1357. if(command[0] == '\"')
  1358. {
  1359. // count the number of quotes
  1360. for(const char* s = command; *s != 0; ++s)
  1361. {
  1362. if(*s == '\"')
  1363. {
  1364. count++;
  1365. if(count > 2)
  1366. {
  1367. break;
  1368. }
  1369. }
  1370. }
  1371. // if there are more than two double quotes use
  1372. // GetShortPathName, the cmd.exe program in windows which
  1373. // is used by system fails to execute if there are more than
  1374. // one set of quotes in the arguments
  1375. if(count > 2)
  1376. {
  1377. cmRegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  1378. if(quoted.find(command))
  1379. {
  1380. std::string shortCmd;
  1381. std::string cmd = quoted.match(1);
  1382. std::string args = quoted.match(2);
  1383. 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. }