cfileutl.pas 47 KB

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