cfileutl.pas 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. This module provides some basic file/dir handling utils and classes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cfileutl;
  18. {$i fpcdefs.inc}
  19. {$define usedircache}
  20. interface
  21. uses
  22. {$ifdef hasunix}
  23. Baseunix,unix,
  24. {$endif hasunix}
  25. {$ifdef win32}
  26. Windows,
  27. {$endif win32}
  28. {$if defined(go32v2) or defined(watcom)}
  29. Dos,
  30. {$endif}
  31. {$ifdef macos}
  32. macutils,
  33. {$endif macos}
  34. {$IFNDEF USE_FAKE_SYSUTILS}
  35. SysUtils,
  36. {$ELSE}
  37. fksysutl,
  38. {$ENDIF}
  39. GlobType,
  40. CUtils,CClasses,
  41. Systems;
  42. type
  43. TCachedDirectory = class(TFPHashObject)
  44. private
  45. FDirectoryEntries : TFPHashList;
  46. FCached : Boolean;
  47. procedure FreeDirectoryEntries;
  48. function GetItemAttr(const AName: TCmdStr): longint;
  49. function TryUseCache: boolean;
  50. procedure ForceUseCache;
  51. procedure Reload;
  52. public
  53. constructor Create(AList:TFPHashObjectList;const AName:TCmdStr);
  54. destructor destroy;override;
  55. function FileExists(const AName:TCmdStr):boolean;
  56. function FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  57. function DirectoryExists(const AName:TCmdStr):boolean;
  58. property DirectoryEntries:TFPHashList read FDirectoryEntries;
  59. end;
  60. TCachedSearchRec = record
  61. Name : TCmdStr;
  62. Attr : longint;
  63. Pattern : TCmdStr;
  64. CachedDir : TCachedDirectory;
  65. EntryIndex : longint;
  66. end;
  67. PCachedDirectoryEntry = ^TCachedDirectoryEntry;
  68. TCachedDirectoryEntry = record
  69. RealName: TCmdStr;
  70. Attr : longint;
  71. end;
  72. TDirectoryCache = class
  73. private
  74. FDirectories : TFPHashObjectList;
  75. function GetDirectory(const ADir:TCmdStr):TCachedDirectory;
  76. public
  77. constructor Create;
  78. destructor destroy;override;
  79. function FileExists(const AName:TCmdStr):boolean;
  80. function FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  81. function DirectoryExists(const AName:TCmdStr):boolean;
  82. function FindFirst(const APattern:TCmdStr;var Res:TCachedSearchRec):boolean;
  83. function FindNext(var Res:TCachedSearchRec):boolean;
  84. function FindClose(var Res:TCachedSearchRec):boolean;
  85. end;
  86. TSearchPathList = class(TCmdStrList)
  87. procedure AddPath(s:TCmdStr;addfirst:boolean);overload;
  88. procedure AddLibraryPath(const sysroot: TCmdStr; s:TCmdStr;addfirst:boolean);overload;
  89. procedure AddList(list:TSearchPathList;addfirst:boolean);
  90. function FindFile(const f : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  91. end;
  92. function bstoslash(const s : TCmdStr) : TCmdStr;
  93. {Gives the absolute path to the current directory}
  94. function GetCurrentDir:TCmdStr;
  95. {Gives the relative path to the current directory,
  96. with a trailing dir separator. E. g. on unix ./ }
  97. function CurDirRelPath(systeminfo: tsysteminfo): TCmdStr;
  98. function path_absolute(const s : TCmdStr) : boolean;
  99. Function PathExists (const F : TCmdStr;allowcache:boolean) : Boolean;
  100. Function FileExists (const F : TCmdStr;allowcache:boolean) : Boolean;
  101. function FileExistsNonCase(const path,fn:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  102. Function RemoveDir(d:TCmdStr):boolean;
  103. Function FixPath(const s:TCmdStr;allowdot:boolean):TCmdStr;
  104. function FixFileName(const s:TCmdStr):TCmdStr;
  105. function TargetFixPath(s:TCmdStr;allowdot:boolean):TCmdStr;
  106. function TargetFixFileName(const s:TCmdStr):TCmdStr;
  107. procedure SplitBinCmd(const s:TCmdStr;var bstr: TCmdStr;var cstr:TCmdStr);
  108. function FindFile(const f : TCmdStr; const path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  109. { function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;}
  110. function FindFileInExeLocations(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  111. function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  112. function GetShortName(const n:TCmdStr):TCmdStr;
  113. function maybequoted(const s:string):string;
  114. function maybequoted(const s:ansistring):ansistring;
  115. function maybequoted_for_script(const s:ansistring; quote_script: tscripttype):ansistring;
  116. procedure InitFileUtils;
  117. procedure DoneFileUtils;
  118. function UnixRequoteWithDoubleQuotes(const QuotedStr: TCmdStr): TCmdStr;
  119. function RequotedExecuteProcess(const Path: AnsiString; const ComLine: AnsiString; Flags: TExecuteFlags = []): Longint;
  120. function RequotedExecuteProcess(const Path: AnsiString; const ComLine: array of AnsiString; Flags: TExecuteFlags = []): Longint;
  121. function Shell(const command:ansistring): longint;
  122. { hide Sysutils.ExecuteProcess in units using this one after SysUtils}
  123. const
  124. ExecuteProcess = 'Do not use' deprecated 'Use cfileutil.RequotedExecuteProcess instead, ExecuteProcess cannot deal with single quotes as used by Unix command lines';
  125. { * Since native Amiga commands can't handle Unix-style relative paths used by the compiler,
  126. and some GNU tools, Unix2AmigaPath is needed to handle such situations (KB) * }
  127. {$IFDEF HASAMIGA}
  128. { * PATHCONV is implemented in the Amiga/MorphOS system unit * }
  129. {$NOTE TODO Amiga: implement PathConv() in System unit, which works with AnsiString}
  130. function Unix2AmigaPath(path: ShortString): ShortString; external name 'PATHCONV';
  131. {$ELSE}
  132. function Unix2AmigaPath(path: String): String;
  133. {$ENDIF}
  134. {$if FPC_FULLVERSION < 20701}
  135. type
  136. TRawByteSearchRec = TSearchRec;
  137. {$endif}
  138. implementation
  139. uses
  140. Comphook,
  141. Globals;
  142. {$undef AllFilesMaskIsInRTL}
  143. {$if (FPC_VERSION > 2)}
  144. {$define AllFilesMaskIsInRTL}
  145. {$endif FPC_VERSION}
  146. {$if (FPC_VERSION = 2) and (FPC_RELEASE > 2)}
  147. {$define AllFilesMaskIsInRTL}
  148. {$endif}
  149. {$if (FPC_VERSION = 2) and (FPC_RELEASE = 2) and (FPC_PATCH > 0)}
  150. {$define AllFilesMaskIsInRTL}
  151. {$endif}
  152. {$ifndef AllFilesMaskIsInRTL}
  153. {$if defined(go32v2) or defined(watcom)}
  154. const
  155. AllFilesMask = '*.*';
  156. {$else}
  157. const
  158. AllFilesMask = '*';
  159. {$endif not (go32v2 or watcom)}
  160. {$endif not AllFilesMaskIsInRTL}
  161. var
  162. DirCache : TDirectoryCache;
  163. {$IFNDEF HASAMIGA}
  164. { Stub function for Unix2Amiga Path conversion functionality, only available in
  165. Amiga/MorphOS RTL. I'm open for better solutions. (KB) }
  166. function Unix2AmigaPath(path: String): String;
  167. begin
  168. Unix2AmigaPath:=path;
  169. end;
  170. {$ENDIF}
  171. {****************************************************************************
  172. TCachedDirectory
  173. ****************************************************************************}
  174. constructor TCachedDirectory.create(AList:TFPHashObjectList;const AName:TCmdStr);
  175. begin
  176. inherited create(AList,AName);
  177. FDirectoryEntries:=TFPHashList.Create;
  178. FCached:=False;
  179. end;
  180. destructor TCachedDirectory.destroy;
  181. begin
  182. FreeDirectoryEntries;
  183. FDirectoryEntries.Free;
  184. FDirectoryEntries := nil;
  185. inherited destroy;
  186. end;
  187. function TCachedDirectory.TryUseCache:boolean;
  188. begin
  189. Result:=True;
  190. if FCached then
  191. exit;
  192. if not current_settings.disabledircache then
  193. ForceUseCache
  194. else
  195. Result:=False;
  196. end;
  197. procedure TCachedDirectory.ForceUseCache;
  198. begin
  199. if not FCached then
  200. begin
  201. FCached:=True;
  202. Reload;
  203. end;
  204. end;
  205. procedure TCachedDirectory.FreeDirectoryEntries;
  206. var
  207. i: Integer;
  208. begin
  209. if not(tf_files_case_aware in source_info.flags) then
  210. exit;
  211. for i := 0 to DirectoryEntries.Count-1 do
  212. dispose(PCachedDirectoryEntry(DirectoryEntries[i]));
  213. end;
  214. function TCachedDirectory.GetItemAttr(const AName: TCmdStr): longint;
  215. var
  216. entry: PCachedDirectoryEntry;
  217. begin
  218. if not(tf_files_case_sensitive in source_info.flags) then
  219. if (tf_files_case_aware in source_info.flags) then
  220. begin
  221. entry:=PCachedDirectoryEntry(DirectoryEntries.Find(Lower(AName)));
  222. if assigned(entry) then
  223. Result:=entry^.Attr
  224. else
  225. Result:=0;
  226. end
  227. else
  228. Result:=PtrUInt(DirectoryEntries.Find(Lower(AName)))
  229. else
  230. Result:=PtrUInt(DirectoryEntries.Find(AName));
  231. end;
  232. procedure TCachedDirectory.Reload;
  233. var
  234. dir : TRawByteSearchRec;
  235. entry : PCachedDirectoryEntry;
  236. begin
  237. FreeDirectoryEntries;
  238. DirectoryEntries.Clear;
  239. if findfirst(IncludeTrailingPathDelimiter(Name)+AllFilesMask,faAnyFile or faDirectory,dir) = 0 then
  240. begin
  241. repeat
  242. if ((dir.attr and faDirectory)<>faDirectory) or
  243. ((dir.Name<>'.') and
  244. (dir.Name<>'..')) then
  245. begin
  246. { Force Archive bit so the attribute always has a value. This is needed
  247. to be able to see the difference in the directoryentries lookup if a file
  248. exists or not }
  249. Dir.Attr:=Dir.Attr or faArchive;
  250. if not(tf_files_case_sensitive in source_info.flags) then
  251. if (tf_files_case_aware in source_info.flags) then
  252. begin
  253. new(entry);
  254. entry^.RealName:=Dir.Name;
  255. entry^.Attr:=Dir.Attr;
  256. DirectoryEntries.Add(Lower(Dir.Name),entry)
  257. end
  258. else
  259. DirectoryEntries.Add(Lower(Dir.Name),Pointer(Ptrint(Dir.Attr)))
  260. else
  261. DirectoryEntries.Add(Dir.Name,Pointer(Ptrint(Dir.Attr)));
  262. end;
  263. until findnext(dir) <> 0;
  264. findclose(dir);
  265. end;
  266. end;
  267. function TCachedDirectory.FileExists(const AName:TCmdStr):boolean;
  268. var
  269. Attr : Longint;
  270. begin
  271. if not TryUseCache then
  272. begin
  273. { prepend directory name again }
  274. result:=cfileutl.FileExists(Name+AName,false);
  275. exit;
  276. end;
  277. Attr:=GetItemAttr(AName);
  278. if Attr<>0 then
  279. Result:=((Attr and faDirectory)=0)
  280. else
  281. Result:=false;
  282. end;
  283. function TCachedDirectory.FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  284. var
  285. entry : PCachedDirectoryEntry;
  286. begin
  287. if (tf_files_case_aware in source_info.flags) then
  288. begin
  289. if not TryUseCache then
  290. begin
  291. Result:=FileExistsNonCase(path,fn,false,FoundName);
  292. exit;
  293. end;
  294. entry:=PCachedDirectoryEntry(DirectoryEntries.Find(Lower(ExtractFileName(fn))));
  295. if assigned(entry) and
  296. (entry^.Attr<>0) and
  297. ((entry^.Attr and faDirectory) = 0) then
  298. begin
  299. FoundName:=ExtractFilePath(path+fn)+entry^.RealName;
  300. Result:=true
  301. end
  302. else
  303. Result:=false;
  304. end
  305. else
  306. { should not be called in this case, use plain FileExists }
  307. Result:=False;
  308. end;
  309. function TCachedDirectory.DirectoryExists(const AName:TCmdStr):boolean;
  310. var
  311. Attr : Longint;
  312. begin
  313. if not TryUseCache then
  314. begin
  315. Result:=PathExists(Name+AName,false);
  316. exit;
  317. end;
  318. Attr:=GetItemAttr(AName);
  319. if Attr<>0 then
  320. Result:=((Attr and faDirectory)=faDirectory)
  321. else
  322. Result:=false;
  323. end;
  324. {****************************************************************************
  325. TDirectoryCache
  326. ****************************************************************************}
  327. constructor TDirectoryCache.create;
  328. begin
  329. inherited create;
  330. FDirectories:=TFPHashObjectList.Create(true);
  331. end;
  332. destructor TDirectoryCache.destroy;
  333. begin
  334. FDirectories.Free;
  335. FDirectories := nil;
  336. inherited destroy;
  337. end;
  338. function TDirectoryCache.GetDirectory(const ADir:TCmdStr):TCachedDirectory;
  339. var
  340. CachedDir : TCachedDirectory;
  341. DirName : TCmdStr;
  342. begin
  343. if ADir='' then
  344. DirName:='.'+source_info.DirSep
  345. else
  346. DirName:=ADir;
  347. CachedDir:=TCachedDirectory(FDirectories.Find(DirName));
  348. if not assigned(CachedDir) then
  349. CachedDir:=TCachedDirectory.Create(FDirectories,DirName);
  350. Result:=CachedDir;
  351. end;
  352. function TDirectoryCache.FileExists(const AName:TCmdStr):boolean;
  353. var
  354. CachedDir : TCachedDirectory;
  355. begin
  356. Result:=false;
  357. CachedDir:=GetDirectory(ExtractFilePath(AName));
  358. if assigned(CachedDir) then
  359. Result:=CachedDir.FileExists(ExtractFileName(AName));
  360. end;
  361. function TDirectoryCache.FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  362. var
  363. CachedDir : TCachedDirectory;
  364. begin
  365. Result:=false;
  366. CachedDir:=GetDirectory(ExtractFilePath(path+fn));
  367. if assigned(CachedDir) then
  368. Result:=CachedDir.FileExistsCaseAware(path,fn,FoundName);
  369. end;
  370. function TDirectoryCache.DirectoryExists(const AName:TCmdStr):boolean;
  371. var
  372. CachedDir : TCachedDirectory;
  373. begin
  374. Result:=false;
  375. CachedDir:=GetDirectory(ExtractFilePath(AName));
  376. if assigned(CachedDir) then
  377. Result:=CachedDir.DirectoryExists(ExtractFileName(AName));
  378. end;
  379. function TDirectoryCache.FindFirst(const APattern:TCmdStr;var Res:TCachedSearchRec):boolean;
  380. begin
  381. Res.Pattern:=ExtractFileName(APattern);
  382. Res.CachedDir:=GetDirectory(ExtractFilePath(APattern));
  383. Res.CachedDir.ForceUseCache;
  384. Res.EntryIndex:=0;
  385. if assigned(Res.CachedDir) then
  386. Result:=FindNext(Res)
  387. else
  388. Result:=false;
  389. end;
  390. function TDirectoryCache.FindNext(var Res:TCachedSearchRec):boolean;
  391. var
  392. entry: PCachedDirectoryEntry;
  393. begin
  394. if Res.EntryIndex<Res.CachedDir.DirectoryEntries.Count then
  395. begin
  396. if (tf_files_case_aware in source_info.flags) then
  397. begin
  398. entry:=Res.CachedDir.DirectoryEntries[Res.EntryIndex];
  399. Res.Name:=entry^.RealName;
  400. Res.Attr:=entry^.Attr;
  401. end
  402. else
  403. begin
  404. Res.Name:=Res.CachedDir.DirectoryEntries.NameOfIndex(Res.EntryIndex);
  405. Res.Attr:=PtrUInt(Res.CachedDir.DirectoryEntries[Res.EntryIndex]);
  406. end;
  407. inc(Res.EntryIndex);
  408. Result:=true;
  409. end
  410. else
  411. Result:=false;
  412. end;
  413. function TDirectoryCache.FindClose(var Res:TCachedSearchRec):boolean;
  414. begin
  415. { nothing todo }
  416. result:=true;
  417. end;
  418. {****************************************************************************
  419. Utils
  420. ****************************************************************************}
  421. function bstoslash(const s : TCmdStr) : TCmdStr;
  422. {
  423. return TCmdStr s with all \ changed into /
  424. }
  425. var
  426. i : sizeint;
  427. begin
  428. bstoslash:=s;
  429. for i:=1 to length(s) do
  430. if s[i]='\' then
  431. bstoslash[i]:='/';
  432. end;
  433. {Gives the absolute path to the current directory}
  434. var
  435. CachedCurrentDir : TCmdStr;
  436. function GetCurrentDir:TCmdStr;
  437. begin
  438. if CachedCurrentDir='' then
  439. begin
  440. GetDir(0,CachedCurrentDir);
  441. CachedCurrentDir:=FixPath(CachedCurrentDir,false);
  442. end;
  443. result:=CachedCurrentDir;
  444. end;
  445. {Gives the relative path to the current directory,
  446. with a trailing dir separator. E. g. on unix ./ }
  447. function CurDirRelPath(systeminfo: tsysteminfo): TCmdStr;
  448. begin
  449. if systeminfo.system <> system_powerpc_macosclassic then
  450. CurDirRelPath:= '.'+systeminfo.DirSep
  451. else
  452. CurDirRelPath:= ':'
  453. end;
  454. function path_absolute(const s : TCmdStr) : boolean;
  455. {
  456. is path s an absolute path?
  457. }
  458. begin
  459. result:=false;
  460. {$if defined(unix)}
  461. if (length(s)>0) and (s[1] in AllowDirectorySeparators) then
  462. result:=true;
  463. {$elseif defined(hasamiga)}
  464. (* An Amiga path is absolute, if it has a volume/device name in it (contains ":"),
  465. otherwise it's always a relative path, no matter if it starts with a directory
  466. separator or not. (KB) *)
  467. if (length(s)>0) and (Pos(':',s) <> 0) then
  468. result:=true;
  469. {$elseif defined(macos)}
  470. if IsMacFullPath(s) then
  471. result:=true;
  472. {$elseif defined(netware)}
  473. if (Pos (DriveSeparator, S) <> 0) or
  474. ((Length (S) > 0) and (S [1] in AllowDirectorySeparators)) then
  475. result:=true;
  476. {$elseif defined(win32) or defined(win64) or defined(go32v2) or defined(os2) or defined(watcom)}
  477. if ((length(s)>0) and (s[1] in AllowDirectorySeparators)) or
  478. (* The following check for non-empty AllowDriveSeparators assumes that all
  479. other platforms supporting drives and not handled as exceptions above
  480. should work with DOS-like paths, i.e. use absolute paths with one letter
  481. for drive followed by path separator *)
  482. ((length(s)>2) and (s[2] in AllowDriveSeparators) and (s[3] in AllowDirectorySeparators)) then
  483. result:=true;
  484. {$else}
  485. if ((length(s)>0) and (s[1] in AllowDirectorySeparators)) or
  486. (* The following check for non-empty AllowDriveSeparators assumes that all
  487. other platforms supporting drives and not handled as exceptions above
  488. should work with DOS-like paths, i.e. use absolute paths with one letter
  489. for drive followed by path separator *)
  490. ((AllowDriveSeparators <> []) and (length(s)>2) and (s[2] in AllowDriveSeparators) and (s[3] in AllowDirectorySeparators)) then
  491. result:=true;
  492. {$endif unix}
  493. end;
  494. Function FileExists ( Const F : TCmdStr;allowcache:boolean) : Boolean;
  495. begin
  496. {$ifdef usedircache}
  497. if allowcache then
  498. Result:=DirCache.FileExists(F)
  499. else
  500. {$endif usedircache}
  501. Result:=SysUtils.FileExists(F);
  502. if do_checkverbosity(V_Tried) then
  503. begin
  504. if Result then
  505. do_comment(V_Tried,'Searching file '+F+'... found')
  506. else
  507. do_comment(V_Tried,'Searching file '+F+'... not found');
  508. end;
  509. end;
  510. function FileExistsNonCase(const path,fn:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  511. var
  512. fn2 : TCmdStr;
  513. begin
  514. result:=false;
  515. if tf_files_case_sensitive in source_info.flags then
  516. begin
  517. {
  518. Search order for case sensitive systems:
  519. 1. NormalCase
  520. 2. lowercase
  521. 3. UPPERCASE
  522. }
  523. FoundFile:=path+fn;
  524. If (ftNone in AllowedFilenameTransFormations) and FileExists(FoundFile,allowcache) then
  525. begin
  526. result:=true;
  527. exit;
  528. end;
  529. if (ftLowerCase in AllowedFilenameTransFormations) then
  530. begin
  531. fn2:=Lower(fn);
  532. if (fn2<>fn) then
  533. begin
  534. FoundFile:=path+fn2;
  535. If FileExists(FoundFile,allowcache) then
  536. begin
  537. result:=true;
  538. exit;
  539. end;
  540. end;
  541. end;
  542. if (ftUpperCase in AllowedFilenameTransFormations) then
  543. begin
  544. fn2:=Upper(fn);
  545. if (fn2<>fn) then
  546. begin
  547. FoundFile:=path+fn2;
  548. If FileExists(FoundFile,allowcache) then
  549. begin
  550. result:=true;
  551. exit;
  552. end;
  553. end;
  554. end;
  555. end
  556. else
  557. if tf_files_case_aware in source_info.flags then
  558. begin
  559. {
  560. Search order for case aware systems:
  561. 1. NormalCase
  562. }
  563. {$ifdef usedircache}
  564. if allowcache then
  565. begin
  566. result:=DirCache.FileExistsCaseAware(path,fn,fn2);
  567. if result then
  568. begin
  569. FoundFile:=fn2;
  570. exit;
  571. end;
  572. end
  573. else
  574. {$endif usedircache}
  575. begin
  576. FoundFile:=path+fn;
  577. If FileExists(FoundFile,allowcache) then
  578. begin
  579. { don't know the real name in this case }
  580. result:=true;
  581. exit;
  582. end;
  583. end;
  584. end
  585. else
  586. begin
  587. { None case sensitive only lowercase }
  588. FoundFile:=path+Lower(fn);
  589. If FileExists(FoundFile,allowcache) then
  590. begin
  591. result:=true;
  592. exit;
  593. end;
  594. end;
  595. { Set foundfile to something useful }
  596. FoundFile:=fn;
  597. end;
  598. Function PathExists (const F : TCmdStr;allowcache:boolean) : Boolean;
  599. Var
  600. i: longint;
  601. hs : TCmdStr;
  602. begin
  603. if F = '' then
  604. begin
  605. result := true;
  606. exit;
  607. end;
  608. hs := ExpandFileName(F);
  609. I := Pos (DriveSeparator, hs);
  610. if (hs [Length (hs)] = DirectorySeparator) and
  611. (((I = 0) and (Length (hs) > 1)) or (I <> Length (hs) - 1)) then
  612. Delete (hs, Length (hs), 1);
  613. {$ifdef usedircache}
  614. if allowcache then
  615. Result:=DirCache.DirectoryExists(hs)
  616. else
  617. {$endif usedircache}
  618. Result:=SysUtils.DirectoryExists(hs);
  619. end;
  620. Function RemoveDir(d:TCmdStr):boolean;
  621. begin
  622. if d[length(d)]=source_info.DirSep then
  623. Delete(d,length(d),1);
  624. {$push}{$I-}
  625. rmdir(d);
  626. {$pop}
  627. RemoveDir:=(ioresult=0);
  628. end;
  629. Function _FixPath(const s:TCmdStr;allowdot:boolean;const info:tsysteminfo;const drivesep:string):TCmdStr;
  630. var
  631. i, L : sizeint;
  632. begin
  633. Result := s;
  634. L := Length(Result);
  635. if L=0 then
  636. exit;
  637. { Fix separator }
  638. for i:=1 to L do
  639. if (Result[i] in ['/','\']) and (Result[i]<>info.DirSep) then
  640. Result[i]:=info.DirSep;
  641. { Remove . or ./ }
  642. if (not allowdot) and (Result[1]='.') and ((L=1) or (L=2) and (Result[2]=info.DirSep)) then
  643. exit('');
  644. { Fix ending / }
  645. if (Result[L]<>info.DirSep) and (Result[L]<>drivesep) then
  646. Result:=Result+info.DirSep;
  647. { return }
  648. if not ((tf_files_case_aware in info.flags) or
  649. (tf_files_case_sensitive in info.flags)) then
  650. Result := lower(Result);
  651. end;
  652. Function FixPath(const s:TCmdStr;allowdot:boolean):TCmdStr;
  653. begin
  654. Result:=_FixPath(s,allowdot,source_info,DriveSeparator);
  655. end;
  656. {Actually the version in macutils.pp could be used,
  657. but that would not work for crosscompiling, so this is a slightly modified
  658. version of it.}
  659. function TranslatePathToMac (const path: TCmdStr; mpw: Boolean): TCmdStr;
  660. function GetVolumeIdentifier: TCmdStr;
  661. begin
  662. GetVolumeIdentifier := '{Boot}'
  663. (*
  664. if mpw then
  665. GetVolumeIdentifier := '{Boot}'
  666. else
  667. GetVolumeIdentifier := macosBootVolumeName;
  668. *)
  669. end;
  670. var
  671. slashPos, oldpos, newpos, oldlen, maxpos: Longint;
  672. begin
  673. oldpos := 1;
  674. slashPos := Pos('/', path);
  675. TranslatePathToMac:='';
  676. if (slashPos <> 0) then {its a unix path}
  677. begin
  678. if slashPos = 1 then
  679. begin {its a full path}
  680. oldpos := 2;
  681. TranslatePathToMac := GetVolumeIdentifier;
  682. end
  683. else {its a partial path}
  684. TranslatePathToMac := ':';
  685. end
  686. else
  687. begin
  688. slashPos := Pos('\', path);
  689. if (slashPos <> 0) then {its a dos path}
  690. begin
  691. if slashPos = 1 then
  692. begin {its a full path, without drive letter}
  693. oldpos := 2;
  694. TranslatePathToMac := GetVolumeIdentifier;
  695. end
  696. else if (Length(path) >= 2) and (path[2] = ':') then {its a full path, with drive letter}
  697. begin
  698. oldpos := 4;
  699. TranslatePathToMac := GetVolumeIdentifier;
  700. end
  701. else {its a partial path}
  702. TranslatePathToMac := ':';
  703. end;
  704. end;
  705. if (slashPos <> 0) then {its a unix or dos path}
  706. begin
  707. {Translate "/../" to "::" , "/./" to ":" and "/" to ":" }
  708. newpos := Length(TranslatePathToMac);
  709. oldlen := Length(path);
  710. SetLength(TranslatePathToMac, newpos + oldlen); {It will be no longer than what is already}
  711. {prepended plus length of path.}
  712. maxpos := Length(TranslatePathToMac); {Get real maxpos, can be short if String is ShortString}
  713. {There is never a slash in the beginning, because either it was an absolute path, and then the}
  714. {drive and slash was removed, or it was a relative path without a preceding slash.}
  715. while oldpos <= oldlen do
  716. begin
  717. {Check if special dirs, ./ or ../ }
  718. if path[oldPos] = '.' then
  719. if (oldpos + 1 <= oldlen) and (path[oldPos + 1] = '.') then
  720. begin
  721. if (oldpos + 2 > oldlen) or (path[oldPos + 2] in ['/', '\']) then
  722. begin
  723. {It is "../" or ".." translates to ":" }
  724. if newPos = maxPos then
  725. begin {Shouldn't actually happen, but..}
  726. Exit('');
  727. end;
  728. newPos := newPos + 1;
  729. TranslatePathToMac[newPos] := ':';
  730. oldPos := oldPos + 3;
  731. continue; {Start over again}
  732. end;
  733. end
  734. else if (oldpos + 1 > oldlen) or (path[oldPos + 1] in ['/', '\']) then
  735. begin
  736. {It is "./" or "." ignor it }
  737. oldPos := oldPos + 2;
  738. continue; {Start over again}
  739. end;
  740. {Collect file or dir name}
  741. while (oldpos <= oldlen) and not (path[oldPos] in ['/', '\']) do
  742. begin
  743. if newPos = maxPos then
  744. begin {Shouldn't actually happen, but..}
  745. Exit('');
  746. end;
  747. newPos := newPos + 1;
  748. TranslatePathToMac[newPos] := path[oldPos];
  749. oldPos := oldPos + 1;
  750. end;
  751. {When we come here there is either a slash or we are at the end.}
  752. if (oldpos <= oldlen) then
  753. begin
  754. if newPos = maxPos then
  755. begin {Shouldn't actually happen, but..}
  756. Exit('');
  757. end;
  758. newPos := newPos + 1;
  759. TranslatePathToMac[newPos] := ':';
  760. oldPos := oldPos + 1;
  761. end;
  762. end;
  763. SetLength(TranslatePathToMac, newpos);
  764. end
  765. else if (path = '.') then
  766. TranslatePathToMac := ':'
  767. else if (path = '..') then
  768. TranslatePathToMac := '::'
  769. else
  770. TranslatePathToMac := path; {its a mac path}
  771. end;
  772. function _FixFileName(const s:TCmdStr; const info:tsysteminfo):TCmdStr;
  773. var
  774. i : sizeint;
  775. begin
  776. if info.system = system_powerpc_macosclassic then
  777. Result:=TranslatePathToMac(s, true)
  778. else
  779. begin
  780. if (tf_files_case_aware in info.flags) or
  781. (tf_files_case_sensitive in info.flags) then
  782. Result:=s
  783. else
  784. Result:=Lower(s);
  785. for i:=1 to length(s) do
  786. case s[i] of
  787. '/','\' :
  788. if s[i]<>info.dirsep then
  789. Result[i]:=info.dirsep;
  790. end;
  791. end;
  792. end;
  793. function FixFileName(const s:TCmdStr):TCmdStr;
  794. begin
  795. result:=_FixFileName(s,source_info);
  796. end;
  797. Function TargetFixPath(s:TCmdStr;allowdot:boolean):TCmdStr;
  798. begin
  799. result:=_FixPath(s,allowdot,target_info,':');
  800. end;
  801. function TargetFixFileName(const s:TCmdStr):TCmdStr;
  802. begin
  803. result:=_FixFileName(s,target_info);
  804. end;
  805. procedure SplitBinCmd(const s:TCmdStr;var bstr:TCmdStr;var cstr:TCmdStr);
  806. var
  807. i : longint;
  808. begin
  809. i:=pos(' ',s);
  810. if i>0 then
  811. begin
  812. bstr:=Copy(s,1,i-1);
  813. cstr:=Copy(s,i+1,length(s)-i);
  814. end
  815. else
  816. begin
  817. bstr:=s;
  818. cstr:='';
  819. end;
  820. end;
  821. procedure TSearchPathList.AddPath(s:TCmdStr;addfirst:boolean);
  822. begin
  823. AddLibraryPath('',s,AddFirst);
  824. end;
  825. procedure TSearchPathList.AddLibraryPath(const sysroot: TCmdStr; s:TCmdStr;addfirst:boolean);
  826. var
  827. staridx,
  828. i,j : longint;
  829. prefix,
  830. suffix,
  831. CurrentDir,
  832. currPath : TCmdStr;
  833. subdirfound : boolean;
  834. {$ifdef usedircache}
  835. dir : TCachedSearchRec;
  836. {$else usedircache}
  837. dir : TSearchRec;
  838. {$endif usedircache}
  839. hp : TCmdStrListItem;
  840. procedure WarnNonExistingPath(const path : TCmdStr);
  841. begin
  842. if do_checkverbosity(V_Tried) then
  843. do_comment(V_Tried,'Path "'+path+'" not found');
  844. end;
  845. procedure AddCurrPath;
  846. begin
  847. if addfirst then
  848. begin
  849. Remove(currPath);
  850. Insert(currPath);
  851. end
  852. else
  853. begin
  854. { Check if already in path, then we don't add it }
  855. hp:=Find(currPath);
  856. if not assigned(hp) then
  857. Concat(currPath);
  858. end;
  859. end;
  860. begin
  861. if s='' then
  862. exit;
  863. { Support default macro's }
  864. DefaultReplacements(s);
  865. {$warnings off}
  866. if PathSeparator <> ';' then
  867. for i:=1 to length(s) do
  868. if s[i]=PathSeparator then
  869. s[i]:=';';
  870. {$warnings on}
  871. { get current dir }
  872. CurrentDir:=GetCurrentDir;
  873. repeat
  874. { get currpath }
  875. if addfirst then
  876. begin
  877. j:=length(s);
  878. while (j>0) and (s[j]<>';') do
  879. dec(j);
  880. currPath:= TrimSpace(Copy(s,j+1,length(s)-j));
  881. if j=0 then
  882. s:=''
  883. else
  884. System.Delete(s,j,length(s)-j+1);
  885. end
  886. else
  887. begin
  888. j:=Pos(';',s);
  889. if j=0 then
  890. j:=length(s)+1;
  891. currPath:= TrimSpace(Copy(s,1,j-1));
  892. System.Delete(s,1,j);
  893. end;
  894. { fix pathname }
  895. DePascalQuote(currPath);
  896. { GNU LD convention: if library search path starts with '=', it's relative to the
  897. sysroot; otherwise, interpret it as a regular path }
  898. if (length(currPath) >0) and (currPath[1]='=') then
  899. currPath:=sysroot+FixPath(copy(currPath,2,length(currPath)-1),false);
  900. if currPath='' then
  901. currPath:= CurDirRelPath(source_info)
  902. else
  903. begin
  904. currPath:=FixPath(ExpandFileName(currpath),false);
  905. if (CurrentDir<>'') and (Copy(currPath,1,length(CurrentDir))=CurrentDir) then
  906. begin
  907. {$ifdef hasamiga}
  908. currPath:= CurrentDir+Copy(currPath,length(CurrentDir)+1,length(currPath));
  909. {$else}
  910. currPath:= CurDirRelPath(source_info)+Copy(currPath,length(CurrentDir)+1,length(currPath));
  911. {$endif}
  912. end;
  913. end;
  914. { wildcard adding ? }
  915. staridx:=pos('*',currpath);
  916. if staridx>0 then
  917. begin
  918. prefix:=ExtractFilePath(Copy(currpath,1,staridx));
  919. suffix:=Copy(currpath,staridx+1,length(currpath));
  920. subdirfound:=false;
  921. {$ifdef usedircache}
  922. if DirCache.FindFirst(Prefix+AllFilesMask,dir) then
  923. begin
  924. repeat
  925. if (dir.attr and faDirectory)<>0 then
  926. begin
  927. subdirfound:=true;
  928. currpath:=prefix+dir.name+suffix;
  929. if (suffix='') or PathExists(currpath,true) then
  930. begin
  931. hp:=Find(currPath);
  932. if not assigned(hp) then
  933. AddCurrPath;
  934. end;
  935. end;
  936. until not DirCache.FindNext(dir);
  937. end;
  938. DirCache.FindClose(dir);
  939. {$else usedircache}
  940. if findfirst(prefix+AllFilesMask,faDirectory,dir) = 0 then
  941. begin
  942. repeat
  943. if (dir.name<>'.') and
  944. (dir.name<>'..') and
  945. ((dir.attr and faDirectory)<>0) then
  946. begin
  947. subdirfound:=true;
  948. currpath:=prefix+dir.name+suffix;
  949. if (suffix='') or PathExists(currpath,false) then
  950. begin
  951. hp:=Find(currPath);
  952. if not assigned(hp) then
  953. AddCurrPath;
  954. end;
  955. end;
  956. until findnext(dir) <> 0;
  957. FindClose(dir);
  958. end;
  959. {$endif usedircache}
  960. if not subdirfound then
  961. WarnNonExistingPath(currpath);
  962. end
  963. else
  964. begin
  965. if PathExists(currpath,true) then
  966. AddCurrPath
  967. else
  968. WarnNonExistingPath(currpath);
  969. end;
  970. until (s='');
  971. end;
  972. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  973. var
  974. s : TCmdStr;
  975. hl : TSearchPathList;
  976. hp,hp2 : TCmdStrListItem;
  977. begin
  978. if list.empty then
  979. exit;
  980. { create temp and reverse the list }
  981. if addfirst then
  982. begin
  983. hl:=TSearchPathList.Create;
  984. hp:=TCmdStrListItem(list.first);
  985. while assigned(hp) do
  986. begin
  987. hl.insert(hp.Str);
  988. hp:=TCmdStrListItem(hp.next);
  989. end;
  990. while not hl.empty do
  991. begin
  992. s:=hl.GetFirst;
  993. Remove(s);
  994. Insert(s);
  995. end;
  996. hl.Free;
  997. hl := nil;
  998. end
  999. else
  1000. begin
  1001. hp:=TCmdStrListItem(list.first);
  1002. while assigned(hp) do
  1003. begin
  1004. hp2:=Find(hp.Str);
  1005. { Check if already in path, then we don't add it }
  1006. if not assigned(hp2) then
  1007. Concat(hp.Str);
  1008. hp:=TCmdStrListItem(hp.next);
  1009. end;
  1010. end;
  1011. end;
  1012. function TSearchPathList.FindFile(const f :TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1013. Var
  1014. p : TCmdStrListItem;
  1015. begin
  1016. FindFile:=false;
  1017. p:=TCmdStrListItem(first);
  1018. while assigned(p) do
  1019. begin
  1020. result:=FileExistsNonCase(p.Str,f,allowcache,FoundFile);
  1021. if result then
  1022. exit;
  1023. p:=TCmdStrListItem(p.next);
  1024. end;
  1025. { Return original filename if not found }
  1026. FoundFile:=f;
  1027. end;
  1028. function FindFile(const f : TCmdStr; const path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1029. Var
  1030. StartPos, EndPos, L: LongInt;
  1031. begin
  1032. Result:=False;
  1033. if (path_absolute(f)) then
  1034. begin
  1035. Result:=FileExistsNonCase('',f, allowcache, foundfile);
  1036. if Result then
  1037. Exit;
  1038. end;
  1039. StartPos := 1;
  1040. L := Length(Path);
  1041. repeat
  1042. EndPos := StartPos;
  1043. while (EndPos <= L) and ((Path[EndPos] <> PathSeparator) and (Path[EndPos] <> ';')) do
  1044. Inc(EndPos);
  1045. Result := FileExistsNonCase(FixPath(Copy(Path, StartPos, EndPos-StartPos), False), f, allowcache, FoundFile);
  1046. if Result then
  1047. Exit;
  1048. StartPos := EndPos + 1;
  1049. until StartPos > L;
  1050. FoundFile:=f;
  1051. end;
  1052. {
  1053. function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1054. Var
  1055. singlepathstring : TCmdStr;
  1056. startpc,pc : pchar;
  1057. begin
  1058. FindFilePchar:=false;
  1059. if Assigned (Path) then
  1060. begin
  1061. pc:=path;
  1062. repeat
  1063. startpc:=pc;
  1064. while (pc^<>PathSeparator) and (pc^<>';') and (pc^<>#0) do
  1065. inc(pc);
  1066. SetLength(singlepathstring, pc-startpc);
  1067. move(startpc^,singlepathstring[1],pc-startpc);
  1068. singlepathstring:=FixPath(ExpandFileName(singlepathstring),false);
  1069. result:=FileExistsNonCase(singlepathstring,f,allowcache,FoundFile);
  1070. if result then
  1071. exit;
  1072. if (pc^=#0) then
  1073. break;
  1074. inc(pc);
  1075. until false;
  1076. end;
  1077. foundfile:=f;
  1078. end;
  1079. }
  1080. function FindFileInExeLocations(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1081. var
  1082. Path : TCmdStr;
  1083. found : boolean;
  1084. begin
  1085. found:=FindFile(FixFileName(bin),exepath,allowcache,foundfile);
  1086. if not found then
  1087. begin
  1088. {$ifdef macos}
  1089. Path:=GetEnvironmentVariable('Commands');
  1090. {$else}
  1091. Path:=GetEnvironmentVariable('PATH');
  1092. {$endif}
  1093. found:=FindFile(FixFileName(bin),Path,allowcache,foundfile);
  1094. end;
  1095. FindFileInExeLocations:=found;
  1096. end;
  1097. function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1098. var
  1099. b : TCmdStr;
  1100. begin
  1101. { change extension only on platforms that use an exe extension, otherwise on OpenBSD
  1102. 'ld.bfd' gets converted to 'ld' }
  1103. if source_info.exeext<>'' then
  1104. b:=ChangeFileExt(bin,source_info.exeext)
  1105. else
  1106. b:=bin;
  1107. FindExe:=FindFileInExeLocations(b,allowcache,foundfile);
  1108. end;
  1109. function GetShortName(const n:TCmdStr):TCmdStr;
  1110. {$ifdef win32}
  1111. var
  1112. hs,hs2 : TCmdStr;
  1113. i : longint;
  1114. {$endif}
  1115. {$if defined(go32v2) or defined(watcom)}
  1116. var
  1117. hs : shortstring;
  1118. {$endif}
  1119. begin
  1120. GetShortName:=n;
  1121. {$ifdef win32}
  1122. hs:=n+#0;
  1123. hs2:='';
  1124. { may become longer in case of e.g. ".a" -> "a~1" or so }
  1125. setlength(hs2,length(hs)*2);
  1126. i:=Windows.GetShortPathName(@hs[1],@hs2[1],length(hs)*2);
  1127. if (i>0) and (i<=length(hs)*2) then
  1128. begin
  1129. setlength(hs2,strlen(@hs2[1]));
  1130. GetShortName:=hs2;
  1131. end;
  1132. {$endif}
  1133. {$if defined(go32v2) or defined(watcom)}
  1134. hs:=n;
  1135. if Dos.GetShortName(hs) then
  1136. GetShortName:=hs;
  1137. {$endif}
  1138. end;
  1139. function maybequoted(const s:string):string;
  1140. const
  1141. FORBIDDEN_CHARS_DOS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1142. '{', '}', '''', '`', '~'];
  1143. FORBIDDEN_CHARS_OTHER = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1144. '{', '}', '''', ':', '\', '`', '~'];
  1145. var
  1146. forbidden_chars: set of char;
  1147. i : integer;
  1148. quote_script: tscripttype;
  1149. quote_char: ansichar;
  1150. quoted : boolean;
  1151. begin
  1152. if not(cs_link_on_target in current_settings.globalswitches) then
  1153. quote_script:=source_info.script
  1154. else
  1155. quote_script:=target_info.script;
  1156. if quote_script=script_dos then
  1157. forbidden_chars:=FORBIDDEN_CHARS_DOS
  1158. else
  1159. begin
  1160. forbidden_chars:=FORBIDDEN_CHARS_OTHER;
  1161. if quote_script=script_unix then
  1162. include(forbidden_chars,'"');
  1163. end;
  1164. if quote_script=script_unix then
  1165. quote_char:=''''
  1166. else
  1167. quote_char:='"';
  1168. quoted:=false;
  1169. result:=quote_char;
  1170. for i:=1 to length(s) do
  1171. begin
  1172. if s[i]=quote_char then
  1173. begin
  1174. quoted:=true;
  1175. result:=result+'\'+quote_char;
  1176. end
  1177. else case s[i] of
  1178. '\':
  1179. begin
  1180. if quote_script=script_unix then
  1181. begin
  1182. result:=result+'\\';
  1183. quoted:=true
  1184. end
  1185. else
  1186. result:=result+'\';
  1187. end;
  1188. ' ',
  1189. #128..#255 :
  1190. begin
  1191. quoted:=true;
  1192. result:=result+s[i];
  1193. end;
  1194. else begin
  1195. if s[i] in forbidden_chars then
  1196. quoted:=True;
  1197. result:=result+s[i];
  1198. end;
  1199. end;
  1200. end;
  1201. if quoted then
  1202. result:=result+quote_char
  1203. else
  1204. result:=s;
  1205. end;
  1206. function maybequoted_for_script(const s:ansistring; quote_script: tscripttype):ansistring;
  1207. const
  1208. FORBIDDEN_CHARS_DOS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1209. '{', '}', '''', '`', '~'];
  1210. FORBIDDEN_CHARS_OTHER = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1211. '{', '}', '''', ':', '\', '`', '~'];
  1212. var
  1213. forbidden_chars: set of char;
  1214. i : integer;
  1215. quote_char: ansichar;
  1216. quoted : boolean;
  1217. begin
  1218. if quote_script=script_dos then
  1219. forbidden_chars:=FORBIDDEN_CHARS_DOS
  1220. else
  1221. begin
  1222. forbidden_chars:=FORBIDDEN_CHARS_OTHER;
  1223. if quote_script=script_unix then
  1224. include(forbidden_chars,'"');
  1225. end;
  1226. if quote_script=script_unix then
  1227. quote_char:=''''
  1228. else
  1229. quote_char:='"';
  1230. quoted:=false;
  1231. result:=quote_char;
  1232. for i:=1 to length(s) do
  1233. begin
  1234. if s[i]=quote_char then
  1235. begin
  1236. quoted:=true;
  1237. result:=result+'\'+quote_char;
  1238. end
  1239. else case s[i] of
  1240. '\':
  1241. begin
  1242. if quote_script=script_unix then
  1243. begin
  1244. result:=result+'\\';
  1245. quoted:=true
  1246. end
  1247. else
  1248. result:=result+'\';
  1249. end;
  1250. ' ',
  1251. #128..#255 :
  1252. begin
  1253. quoted:=true;
  1254. result:=result+s[i];
  1255. end;
  1256. else begin
  1257. if s[i] in forbidden_chars then
  1258. quoted:=True;
  1259. result:=result+s[i];
  1260. end;
  1261. end;
  1262. end;
  1263. if quoted then
  1264. result:=result+quote_char
  1265. else
  1266. result:=s;
  1267. end;
  1268. function maybequoted(const s:ansistring):ansistring;
  1269. var
  1270. quote_script: tscripttype;
  1271. begin
  1272. if not(cs_link_on_target in current_settings.globalswitches) then
  1273. quote_script:=source_info.script
  1274. else
  1275. quote_script:=target_info.script;
  1276. result:=maybequoted_for_script(s,quote_script);
  1277. end;
  1278. { requotes a string that was quoted for Unix for passing to ExecuteProcess,
  1279. because it only supports Windows-style quoting; this routine assumes that
  1280. everything that has to be quoted for Windows, was also quoted (but
  1281. differently for Unix) -- which is the case }
  1282. function UnixRequoteWithDoubleQuotes(const QuotedStr: TCmdStr): TCmdStr;
  1283. var
  1284. i: longint;
  1285. temp: TCmdStr;
  1286. inquotes: boolean;
  1287. begin
  1288. if QuotedStr='' then
  1289. begin
  1290. result:='';
  1291. exit;
  1292. end;
  1293. inquotes:=false;
  1294. result:='';
  1295. i:=1;
  1296. temp:='';
  1297. while i<=length(QuotedStr) do
  1298. begin
  1299. case QuotedStr[i] of
  1300. '''':
  1301. begin
  1302. if not(inquotes) then
  1303. begin
  1304. inquotes:=true;
  1305. temp:=''
  1306. end
  1307. else
  1308. begin
  1309. { requote for Windows }
  1310. result:=result+maybequoted_for_script(temp,script_dos);
  1311. inquotes:=false;
  1312. end;
  1313. end;
  1314. '\':
  1315. begin
  1316. if inquotes then
  1317. temp:=temp+QuotedStr[i+1]
  1318. else
  1319. result:=result+QuotedStr[i+1];
  1320. inc(i);
  1321. end;
  1322. else
  1323. begin
  1324. if inquotes then
  1325. temp:=temp+QuotedStr[i]
  1326. else
  1327. result:=result+QuotedStr[i];
  1328. end;
  1329. end;
  1330. inc(i);
  1331. end;
  1332. end;
  1333. function RequotedExecuteProcess(const Path: AnsiString; const ComLine: AnsiString; Flags: TExecuteFlags): Longint;
  1334. var
  1335. quote_script: tscripttype;
  1336. begin
  1337. if do_checkverbosity(V_Executable) then
  1338. do_comment(V_Executable,'Executing "'+Path+'" with command line "'+
  1339. ComLine+'"');
  1340. if (cs_link_on_target in current_settings.globalswitches) then
  1341. quote_script:=target_info.script
  1342. else
  1343. quote_script:=source_info.script;
  1344. if quote_script=script_unix then
  1345. result:=sysutils.ExecuteProcess(Path,UnixRequoteWithDoubleQuotes(ComLine),Flags)
  1346. else
  1347. result:=sysutils.ExecuteProcess(Path,ComLine,Flags)
  1348. end;
  1349. function RequotedExecuteProcess(const Path: AnsiString; const ComLine: array of AnsiString; Flags: TExecuteFlags): Longint;
  1350. var
  1351. i : longint;
  1352. st : string;
  1353. begin
  1354. if do_checkverbosity(V_Executable) then
  1355. begin
  1356. if high(ComLine)=0 then
  1357. st:=''
  1358. else
  1359. st:=ComLine[1];
  1360. for i:=2 to high(ComLine) do
  1361. st:=st+' '+ComLine[i];
  1362. do_comment(V_Executable,'Executing "'+Path+'" with command line "'+
  1363. st+'"');
  1364. end;
  1365. result:=sysutils.ExecuteProcess(Path,ComLine,Flags);
  1366. end;
  1367. function Shell(const command:ansistring): longint;
  1368. { This is already defined in the linux.ppu for linux, need for the *
  1369. expansion under linux }
  1370. {$ifdef hasunix}
  1371. begin
  1372. do_comment(V_Executable,'Executing "'+Command+'" with fpSystem call');
  1373. result := Unix.fpsystem(command);
  1374. end;
  1375. {$else hasunix}
  1376. {$ifdef hasamiga}
  1377. begin
  1378. do_comment(V_Executable,'Executing "'+Command+'" using RequotedExecuteProcess');
  1379. result := RequotedExecuteProcess('',command);
  1380. end;
  1381. {$else hasamiga}
  1382. var
  1383. comspec : string;
  1384. begin
  1385. comspec:=GetEnvironmentVariable('COMSPEC');
  1386. do_comment(V_Executable,'Executing "'+Command+'" using comspec "'
  1387. +ComSpec+'"');
  1388. result := RequotedExecuteProcess(comspec,' /C '+command);
  1389. end;
  1390. {$endif hasamiga}
  1391. {$endif hasunix}
  1392. {****************************************************************************
  1393. Init / Done
  1394. ****************************************************************************}
  1395. procedure InitFileUtils;
  1396. begin
  1397. CachedCurrentDir:='';
  1398. DirCache:=TDirectoryCache.Create;
  1399. end;
  1400. procedure DoneFileUtils;
  1401. begin
  1402. DirCache.Free;
  1403. DirCache := nil;
  1404. end;
  1405. end.