2
0

cfileutl.pas 48 KB

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