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