cmSystemTools.cxx 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266
  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::GetShortPath(cmd.c_str(), shortCmd))
  1380. {
  1381. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  1382. return false;
  1383. }
  1384. shortCmd += " ";
  1385. shortCmd += args;
  1386. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  1387. // output, retVal, verbose);
  1388. //return WindowsRunCommand(shortCmd.c_str(), dir,
  1389. //output, retVal, verbose);
  1390. return RunCommandViaWin32(shortCmd.c_str(), dir,
  1391. output, retVal, verbose, timeout);
  1392. }
  1393. else
  1394. {
  1395. cmSystemTools::Error("Could not parse command line with quotes ",
  1396. command);
  1397. }
  1398. }
  1399. }
  1400. // if there is only one set of quotes or no quotes then just run the command
  1401. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  1402. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  1403. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  1404. #else
  1405. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  1406. #endif
  1407. }
  1408. /**
  1409. * Find the file the given name. Searches the given path and then
  1410. * the system search path. Returns the full path to the file if it is
  1411. * found. Otherwise, the empty string is returned.
  1412. */
  1413. std::string cmSystemTools::FindFile(const char* name,
  1414. const std::vector<std::string>& userPaths)
  1415. {
  1416. // Add the system search path to our path.
  1417. std::vector<std::string> path = userPaths;
  1418. cmSystemTools::GetPath(path);
  1419. std::string tryPath;
  1420. for(std::vector<std::string>::const_iterator p = path.begin();
  1421. p != path.end(); ++p)
  1422. {
  1423. tryPath = *p;
  1424. tryPath += "/";
  1425. tryPath += name;
  1426. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1427. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1428. {
  1429. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1430. }
  1431. }
  1432. // Couldn't find the file.
  1433. return "";
  1434. }
  1435. /**
  1436. * Find the executable with the given name. Searches the given path and then
  1437. * the system search path. Returns the full path to the executable if it is
  1438. * found. Otherwise, the empty string is returned.
  1439. */
  1440. std::string cmSystemTools::FindProgram(const char* name,
  1441. const std::vector<std::string>& userPaths,
  1442. bool no_system_path)
  1443. {
  1444. // See if the executable exists as written.
  1445. if(cmSystemTools::FileExists(name) &&
  1446. !cmSystemTools::FileIsDirectory(name))
  1447. {
  1448. return cmSystemTools::CollapseFullPath(name);
  1449. }
  1450. std::string tryPath = name;
  1451. tryPath += cmSystemTools::GetExecutableExtension();
  1452. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1453. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1454. {
  1455. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1456. }
  1457. // Add the system search path to our path.
  1458. std::vector<std::string> path = userPaths;
  1459. if (!no_system_path)
  1460. {
  1461. cmSystemTools::GetPath(path);
  1462. }
  1463. for(std::vector<std::string>::const_iterator p = path.begin();
  1464. p != path.end(); ++p)
  1465. {
  1466. tryPath = *p;
  1467. tryPath += "/";
  1468. tryPath += name;
  1469. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1470. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1471. {
  1472. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1473. }
  1474. #ifdef _WIN32
  1475. tryPath += ".com";
  1476. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1477. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1478. {
  1479. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1480. }
  1481. tryPath = *p;
  1482. tryPath += "/";
  1483. tryPath += name;
  1484. #endif
  1485. tryPath += cmSystemTools::GetExecutableExtension();
  1486. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1487. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1488. {
  1489. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1490. }
  1491. }
  1492. // Couldn't find the program.
  1493. return "";
  1494. }
  1495. /**
  1496. * Find the library with the given name. Searches the given path and then
  1497. * the system search path. Returns the full path to the library if it is
  1498. * found. Otherwise, the empty string is returned.
  1499. */
  1500. std::string cmSystemTools::FindLibrary(const char* name,
  1501. const std::vector<std::string>& userPaths,
  1502. const cmMakefile *makefile)
  1503. {
  1504. // See if the executable exists as written.
  1505. if(cmSystemTools::FileExists(name))
  1506. {
  1507. return cmSystemTools::CollapseFullPath(name);
  1508. }
  1509. // Add the system search path to our path.
  1510. std::vector<std::string> path = userPaths;
  1511. cmSystemTools::GetPath(path);
  1512. // Add some lib directories specific to compilers, depending on the
  1513. // current generator, so that library that might have been stored here
  1514. // can be found too.
  1515. // i.e. Microsoft Visual Studio or .Net: path to compiler/../Lib
  1516. // Borland: path to compiler/../Lib
  1517. if (makefile)
  1518. {
  1519. const char* genName = makefile->GetDefinition("CMAKE_GENERATOR");
  1520. if (genName)
  1521. {
  1522. if (!strcmp(genName, "NMake Makefiles") ||
  1523. !strcmp(genName, "Visual Studio 6"))
  1524. {
  1525. const char* compiler = makefile->GetDefinition("CMAKE_CXX_COMPILER");
  1526. if (compiler)
  1527. {
  1528. std::string compiler_path = cmSystemTools::FindProgram(compiler);
  1529. if (compiler_path.size())
  1530. {
  1531. std::string lib_path =
  1532. cmSystemTools::GetFilenamePath(
  1533. cmSystemTools::GetFilenamePath(compiler_path)) + "/Lib";
  1534. path.push_back(lib_path);
  1535. }
  1536. }
  1537. }
  1538. else if (!strcmp(genName, "Visual Studio 7"))
  1539. {
  1540. // It is likely that the compiler won't be in the path for .Net, but
  1541. // we know where devenv is.
  1542. const char* devenv = makefile->GetDefinition("MICROSOFT_DEVENV");
  1543. if (devenv)
  1544. {
  1545. std::string devenv_path = cmSystemTools::FindProgram(devenv);
  1546. if (devenv_path.size())
  1547. {
  1548. std::string vc7_path =
  1549. cmSystemTools::GetFilenamePath(
  1550. cmSystemTools::GetFilenamePath(
  1551. cmSystemTools::GetFilenamePath(devenv_path))) + "/Vc7";
  1552. path.push_back(vc7_path + "/lib");
  1553. path.push_back(vc7_path + "/PlatformSDK/lib");
  1554. }
  1555. }
  1556. }
  1557. else if (!strcmp(genName, "Borland Makefiles"))
  1558. {
  1559. const char* bcb_bin_path = makefile->GetDefinition("BCB_BIN_PATH");
  1560. if (bcb_bin_path)
  1561. {
  1562. std::string lib_path =
  1563. cmSystemTools::GetFilenamePath(bcb_bin_path) + "/Lib";
  1564. path.push_back(lib_path);
  1565. }
  1566. }
  1567. }
  1568. }
  1569. std::string tryPath;
  1570. for(std::vector<std::string>::const_iterator p = path.begin();
  1571. p != path.end(); ++p)
  1572. {
  1573. #if defined(_WIN32) && !defined(__CYGWIN__)
  1574. tryPath = *p;
  1575. tryPath += "/";
  1576. tryPath += name;
  1577. tryPath += ".lib";
  1578. if(cmSystemTools::FileExists(tryPath.c_str()))
  1579. {
  1580. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1581. }
  1582. #else
  1583. tryPath = *p;
  1584. tryPath += "/lib";
  1585. tryPath += name;
  1586. tryPath += ".so";
  1587. if(cmSystemTools::FileExists(tryPath.c_str()))
  1588. {
  1589. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1590. }
  1591. tryPath = *p;
  1592. tryPath += "/lib";
  1593. tryPath += name;
  1594. tryPath += ".a";
  1595. if(cmSystemTools::FileExists(tryPath.c_str()))
  1596. {
  1597. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1598. }
  1599. tryPath = *p;
  1600. tryPath += "/lib";
  1601. tryPath += name;
  1602. tryPath += ".sl";
  1603. if(cmSystemTools::FileExists(tryPath.c_str()))
  1604. {
  1605. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1606. }
  1607. tryPath = *p;
  1608. tryPath += "/lib";
  1609. tryPath += name;
  1610. tryPath += ".dylib";
  1611. if(cmSystemTools::FileExists(tryPath.c_str()))
  1612. {
  1613. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1614. }
  1615. #endif
  1616. }
  1617. // Couldn't find the library.
  1618. return "";
  1619. }
  1620. bool cmSystemTools::FileIsDirectory(const char* name)
  1621. {
  1622. struct stat fs;
  1623. if(stat(name, &fs) == 0)
  1624. {
  1625. #if _WIN32
  1626. return ((fs.st_mode & _S_IFDIR) != 0);
  1627. #else
  1628. return S_ISDIR(fs.st_mode);
  1629. #endif
  1630. }
  1631. else
  1632. {
  1633. return false;
  1634. }
  1635. }
  1636. int cmSystemTools::ChangeDirectory(const char *dir)
  1637. {
  1638. return Chdir(dir);
  1639. }
  1640. std::string cmSystemTools::GetCurrentWorkingDirectory()
  1641. {
  1642. char buf[2048];
  1643. std::string path = Getcwd(buf, 2048);
  1644. return path;
  1645. }
  1646. /**
  1647. * Given the path to a program executable, get the directory part of the path with the
  1648. * file stripped off. If there is no directory part, the empty string is returned.
  1649. */
  1650. std::string cmSystemTools::GetProgramPath(const char* in_name)
  1651. {
  1652. std::string dir, file;
  1653. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1654. return dir;
  1655. }
  1656. /**
  1657. * Given the path to a program executable, get the directory part of the path
  1658. * with the file stripped off. If there is no directory part, the empty
  1659. * string is returned.
  1660. */
  1661. void cmSystemTools::SplitProgramPath(const char* in_name,
  1662. std::string& dir,
  1663. std::string& file)
  1664. {
  1665. dir = in_name;
  1666. file = "";
  1667. cmSystemTools::ConvertToUnixSlashes(dir);
  1668. if(!cmSystemTools::FileIsDirectory(dir.c_str()))
  1669. {
  1670. std::string::size_type slashPos = dir.rfind("/");
  1671. if(slashPos != std::string::npos)
  1672. {
  1673. file = dir.substr(slashPos+1);
  1674. dir = dir.substr(0, slashPos);
  1675. }
  1676. else
  1677. {
  1678. file = dir;
  1679. dir = "";
  1680. }
  1681. }
  1682. if((dir != "") && !cmSystemTools::FileIsDirectory(dir.c_str()))
  1683. {
  1684. std::string oldDir = in_name;
  1685. cmSystemTools::ConvertToUnixSlashes(oldDir);
  1686. cmSystemTools::Error("Error splitting file name off end of path:\n",
  1687. oldDir.c_str(), "\nDirectory not found: ",
  1688. dir.c_str());
  1689. dir = in_name;
  1690. return;
  1691. }
  1692. }
  1693. /**
  1694. * Given a path to a file or directory, convert it to a full path.
  1695. * This collapses away relative paths. The full path is returned.
  1696. */
  1697. std::string cmSystemTools::CollapseFullPath(const char* in_name)
  1698. {
  1699. std::string dir, file;
  1700. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1701. #ifdef _WIN32
  1702. // Ultra-hack warning:
  1703. // This changes to the target directory, saves the working directory,
  1704. // and then changes back to the original working directory.
  1705. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1706. if(dir != "") { Chdir(dir.c_str()); }
  1707. std::string newDir = cmSystemTools::GetCurrentWorkingDirectory();
  1708. Chdir(cwd.c_str());
  1709. cmSystemTools::ConvertToUnixSlashes(newDir);
  1710. std::string newPath = newDir+"/"+file;
  1711. return newPath;
  1712. #else
  1713. # ifdef MAXPATHLEN
  1714. char resolved_name[MAXPATHLEN];
  1715. # else
  1716. # ifdef PATH_MAX
  1717. char resolved_name[PATH_MAX];
  1718. # else
  1719. char resolved_name[5024];
  1720. # endif
  1721. # endif
  1722. if(dir != "")
  1723. {
  1724. realpath(dir.c_str(), resolved_name);
  1725. dir = resolved_name;
  1726. }
  1727. else
  1728. {
  1729. dir = cmSystemTools::GetCurrentWorkingDirectory();
  1730. }
  1731. if(file == "")
  1732. {
  1733. return dir;
  1734. }
  1735. return dir + "/" + file;
  1736. #endif
  1737. }
  1738. /**
  1739. * Return path of a full filename (no trailing slashes).
  1740. * Warning: returned path is converted to Unix slashes format.
  1741. */
  1742. std::string cmSystemTools::GetFilenamePath(const std::string& filename)
  1743. {
  1744. std::string fn = filename;
  1745. cmSystemTools::ConvertToUnixSlashes(fn);
  1746. std::string::size_type slash_pos = fn.rfind("/");
  1747. if(slash_pos != std::string::npos)
  1748. {
  1749. return fn.substr(0, slash_pos);
  1750. }
  1751. else
  1752. {
  1753. return "";
  1754. }
  1755. }
  1756. /**
  1757. * Return file name of a full filename (i.e. file name without path).
  1758. */
  1759. std::string cmSystemTools::GetFilenameName(const std::string& filename)
  1760. {
  1761. std::string fn = filename;
  1762. cmSystemTools::ConvertToUnixSlashes(fn);
  1763. std::string::size_type slash_pos = fn.rfind("/");
  1764. if(slash_pos != std::string::npos)
  1765. {
  1766. return fn.substr(slash_pos + 1);
  1767. }
  1768. else
  1769. {
  1770. return filename;
  1771. }
  1772. }
  1773. /**
  1774. * Return file extension of a full filename (dot included).
  1775. * Warning: this is the longest extension (for example: .tar.gz)
  1776. */
  1777. std::string cmSystemTools::GetFilenameExtension(const std::string& filename)
  1778. {
  1779. std::string name = cmSystemTools::GetFilenameName(filename);
  1780. std::string::size_type dot_pos = name.find(".");
  1781. if(dot_pos != std::string::npos)
  1782. {
  1783. return name.substr(dot_pos);
  1784. }
  1785. else
  1786. {
  1787. return "";
  1788. }
  1789. }
  1790. /**
  1791. * Return file name without extension of a full filename (i.e. without path).
  1792. * Warning: it considers the longest extension (for example: .tar.gz)
  1793. */
  1794. std::string cmSystemTools::GetFilenameWithoutExtension(const std::string& filename)
  1795. {
  1796. std::string name = cmSystemTools::GetFilenameName(filename);
  1797. std::string::size_type dot_pos = name.find(".");
  1798. if(dot_pos != std::string::npos)
  1799. {
  1800. return name.substr(0, dot_pos);
  1801. }
  1802. else
  1803. {
  1804. return name;
  1805. }
  1806. }
  1807. /**
  1808. * Return file name without extension of a full filename (i.e. without path).
  1809. * Warning: it considers the last extension (for example: removes .gz
  1810. * from .tar.gz)
  1811. */
  1812. std::string
  1813. cmSystemTools::GetFilenameWithoutLastExtension(const std::string& filename)
  1814. {
  1815. std::string name = cmSystemTools::GetFilenameName(filename);
  1816. std::string::size_type dot_pos = name.rfind(".");
  1817. if(dot_pos != std::string::npos)
  1818. {
  1819. return name.substr(0, dot_pos);
  1820. }
  1821. else
  1822. {
  1823. return name;
  1824. }
  1825. }
  1826. bool cmSystemTools::FileIsFullPath(const char* in_name)
  1827. {
  1828. std::string name = in_name;
  1829. #if defined(_WIN32)
  1830. // On Windows, the name must be at least two characters long.
  1831. if(name.length() < 2)
  1832. {
  1833. return false;
  1834. }
  1835. if(name[1] == ':')
  1836. {
  1837. return true;
  1838. }
  1839. if(name[0] == '\\')
  1840. {
  1841. return true;
  1842. }
  1843. #else
  1844. // On UNIX, the name must be at least one character long.
  1845. if(name.length() < 1)
  1846. {
  1847. return false;
  1848. }
  1849. #endif
  1850. // On UNIX, the name must begin in a '/'.
  1851. // On Windows, if the name begins in a '/', then it is a full
  1852. // network path.
  1853. if(name[0] == '/')
  1854. {
  1855. return true;
  1856. }
  1857. return false;
  1858. }
  1859. void cmSystemTools::Glob(const char *directory, const char *regexp,
  1860. std::vector<std::string>& files)
  1861. {
  1862. cmDirectory d;
  1863. cmRegularExpression reg(regexp);
  1864. if (d.Load(directory))
  1865. {
  1866. size_t numf;
  1867. unsigned int i;
  1868. numf = d.GetNumberOfFiles();
  1869. for (i = 0; i < numf; i++)
  1870. {
  1871. std::string fname = d.GetFile(i);
  1872. if (reg.find(fname))
  1873. {
  1874. files.push_back(fname);
  1875. }
  1876. }
  1877. }
  1878. }
  1879. void cmSystemTools::GlobDirs(const char *fullPath,
  1880. std::vector<std::string>& files)
  1881. {
  1882. std::string path = fullPath;
  1883. std::string::size_type pos = path.find("/*");
  1884. if(pos == std::string::npos)
  1885. {
  1886. files.push_back(fullPath);
  1887. return;
  1888. }
  1889. std::string startPath = path.substr(0, pos);
  1890. std::string finishPath = path.substr(pos+2);
  1891. cmDirectory d;
  1892. if (d.Load(startPath.c_str()))
  1893. {
  1894. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1895. {
  1896. if((std::string(d.GetFile(i)) != ".")
  1897. && (std::string(d.GetFile(i)) != ".."))
  1898. {
  1899. std::string fname = startPath;
  1900. fname +="/";
  1901. fname += d.GetFile(i);
  1902. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  1903. {
  1904. fname += finishPath;
  1905. cmSystemTools::GlobDirs(fname.c_str(), files);
  1906. }
  1907. }
  1908. }
  1909. }
  1910. }
  1911. void cmSystemTools::ExpandListArguments(std::vector<std::string> const& arguments,
  1912. std::vector<std::string>& newargs)
  1913. {
  1914. std::vector<std::string>::const_iterator i;
  1915. std::string newarg;
  1916. for(i = arguments.begin();i != arguments.end(); ++i)
  1917. {
  1918. // if there are no ; in the name then just copy the current string
  1919. if(i->find(';') == std::string::npos)
  1920. {
  1921. newargs.push_back(*i);
  1922. }
  1923. else
  1924. {
  1925. std::string::size_type start = 0;
  1926. std::string::size_type endpos = 0;
  1927. const std::string::size_type size = i->size();
  1928. // break up ; separated sections of the string into separate strings
  1929. while(endpos != size)
  1930. {
  1931. endpos = i->find(';', start);
  1932. if(endpos == std::string::npos)
  1933. {
  1934. endpos = i->size();
  1935. }
  1936. else
  1937. {
  1938. // skip right over escaped ; ( \; )
  1939. while((endpos != std::string::npos)
  1940. && (endpos > 0)
  1941. && ((*i)[endpos-1] == '\\') )
  1942. {
  1943. endpos = i->find(';', endpos+1);
  1944. }
  1945. if(endpos == std::string::npos)
  1946. {
  1947. endpos = i->size();
  1948. }
  1949. }
  1950. std::string::size_type len = endpos - start;
  1951. if (len > 0)
  1952. {
  1953. // check for a closing ] after the start position
  1954. if(i->find('[', start) == std::string::npos)
  1955. {
  1956. // if there is no [ in the string then keep it
  1957. newarg = i->substr(start, len);
  1958. }
  1959. else
  1960. {
  1961. int opencount = 0;
  1962. int closecount = 0;
  1963. for(std::string::size_type j = start; j < endpos; ++j)
  1964. {
  1965. if(i->at(j) == '[')
  1966. {
  1967. ++opencount;
  1968. }
  1969. else if (i->at(j) == ']')
  1970. {
  1971. ++closecount;
  1972. }
  1973. }
  1974. if(opencount != closecount)
  1975. {
  1976. // skip this one
  1977. endpos = i->find(';', endpos+1);
  1978. if(endpos == std::string::npos)
  1979. {
  1980. endpos = i->size();
  1981. }
  1982. len = endpos - start;
  1983. }
  1984. newarg = i->substr(start, len);
  1985. }
  1986. std::string::size_type pos = newarg.find("\\;");
  1987. if(pos != std::string::npos)
  1988. {
  1989. newarg.erase(pos, 1);
  1990. }
  1991. newargs.push_back(newarg);
  1992. }
  1993. start = endpos+1;
  1994. }
  1995. }
  1996. }
  1997. }
  1998. bool cmSystemTools::GetShortPath(const char* path, std::string& shortPath)
  1999. {
  2000. #if defined(WIN32) && !defined(__CYGWIN__)
  2001. const int size = int(strlen(path)) +1; // size of return
  2002. char *buffer = new char[size]; // create a buffer
  2003. char *tempPath = new char[size]; // create a buffer
  2004. int ret;
  2005. // if the path passed in has quotes around it, first remove the quotes
  2006. if (path[0] == '"' && path[strlen(path)-1] == '"')
  2007. {
  2008. strcpy(tempPath,path+1);
  2009. tempPath[strlen(tempPath)-1] = '\0';
  2010. }
  2011. else
  2012. {
  2013. strcpy(tempPath,path);
  2014. }
  2015. buffer[0] = 0;
  2016. ret = GetShortPathName(tempPath, buffer, size);
  2017. if(buffer[0] == 0 || ret > size)
  2018. {
  2019. if(ret < size)
  2020. {
  2021. LPVOID lpMsgBuf;
  2022. FormatMessage(
  2023. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2024. FORMAT_MESSAGE_FROM_SYSTEM |
  2025. FORMAT_MESSAGE_IGNORE_INSERTS,
  2026. NULL,
  2027. GetLastError(),
  2028. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  2029. (LPTSTR) &lpMsgBuf,
  2030. 0,
  2031. NULL
  2032. );
  2033. cmSystemTools::Error((LPCTSTR)lpMsgBuf);
  2034. LocalFree( lpMsgBuf );
  2035. }
  2036. cmSystemTools::Error("Unable to get a short path: ", path);
  2037. delete [] tempPath;
  2038. return false;
  2039. }
  2040. else
  2041. {
  2042. shortPath = buffer;
  2043. delete [] buffer;
  2044. delete [] tempPath;
  2045. return true;
  2046. }
  2047. #else
  2048. shortPath = path;
  2049. return true;
  2050. #endif
  2051. }
  2052. cmSystemTools::e_FileFormat cmSystemTools::GetFileFormat(const char* cext)
  2053. {
  2054. if ( ! cext || *cext == 0 )
  2055. {
  2056. return cmSystemTools::NO_FILE_FORMAT;
  2057. }
  2058. std::string ext = cmSystemTools::LowerCase(cext);
  2059. if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
  2060. if ( ext == "cxx" || ext == ".cxx" ||
  2061. ext == "cpp" || ext == ".cpp" ||
  2062. ext == "c++" || ext == ".c++" ||
  2063. ext == "cc" || ext == ".cc" ) { return cmSystemTools::CXX_FILE_FORMAT; }
  2064. if ( ext == "java" || ext == ".java" ) { return cmSystemTools::JAVA_FILE_FORMAT; }
  2065. if ( ext == "h" || ext == ".h" ||
  2066. ext == "hpp" || ext == ".hpp" ||
  2067. ext == "h++" || ext == ".h++" ||
  2068. ext == "hxx" || ext == ".hxx" ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  2069. if ( ext == "rc" || ext == ".rc" ) { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  2070. if ( ext == "def" || ext == ".def" ) { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  2071. if ( ext == "lib" || ext == ".lib" ||
  2072. ext == "a" || ext == ".a") { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  2073. if ( ext == "o" || ext == ".o" ||
  2074. ext == "obj" || ext == ".obj") { return cmSystemTools::OBJECT_FILE_FORMAT; }
  2075. #ifdef __APPLE__
  2076. if ( ext == "dylib" || ext == ".dylib" )
  2077. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  2078. if ( ext == "so" || ext == ".so" ||
  2079. ext == "bundle" || ext == ".bundle" )
  2080. { return cmSystemTools::MODULE_FILE_FORMAT; }
  2081. #else // __APPLE__
  2082. if ( ext == "so" || ext == ".so" ||
  2083. ext == "sl" || ext == ".sl" ||
  2084. ext == "dll" || ext == ".dll" )
  2085. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  2086. #endif // __APPLE__
  2087. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  2088. }