globals.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. {
  2. $Id$
  3. Copyright (C) 1998-2000 by Florian Klaempfl
  4. This unit implements some support functions and global variables
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit globals;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. {$ifdef win32}
  23. windows,
  24. {$endif}
  25. {$ifdef linux}
  26. linux,
  27. {$endif}
  28. {$ifdef Delphi}
  29. sysutils,
  30. dmisc,
  31. {$else}
  32. strings,
  33. dos,
  34. {$endif}
  35. globtype,version,tokens,systems,cutils,cobjects;
  36. const
  37. {$ifdef linux}
  38. DirSep = '/';
  39. {$else}
  40. {$ifdef amiga}
  41. DirSep = '/';
  42. {$else}
  43. DirSep = '\';
  44. {$endif}
  45. {$endif}
  46. {$ifdef Splitheap}
  47. testsplit : boolean = false;
  48. {$endif Splitheap}
  49. delphimodeswitches : tmodeswitches=
  50. [m_delphi,m_tp,m_all,m_class,m_objpas,m_result,m_string_pchar,
  51. m_pointer_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistring,
  52. m_out,m_default_para];
  53. fpcmodeswitches : tmodeswitches=
  54. [m_fpc,m_all,m_string_pchar,m_nested_comment,m_repeat_forward,
  55. m_cvar_support,m_initfinal,m_add_pointer];
  56. objfpcmodeswitches : tmodeswitches=
  57. [m_objfpc,m_fpc,m_all,m_class,m_objpas,m_result,m_string_pchar,m_nested_comment,
  58. m_repeat_forward,m_cvar_support,m_initfinal,m_add_pointer,m_out,m_default_para];
  59. tpmodeswitches : tmodeswitches=
  60. [m_tp7,m_tp,m_all,m_tp_procvar];
  61. gpcmodeswitches : tmodeswitches=
  62. [m_gpc,m_all];
  63. type
  64. TSearchPathList = object(TStringQueue)
  65. procedure AddPath(s:string;addfirst:boolean);
  66. procedure AddList(list:TSearchPathList;addfirst:boolean);
  67. function FindFile(const f : string;var b : boolean) : string;
  68. end;
  69. var
  70. { specified inputfile }
  71. inputdir : dirstr;
  72. inputfile : namestr;
  73. inputextension : extstr;
  74. { specified outputfile with -o parameter }
  75. outputfile : namestr;
  76. { specified with -FE or -FU }
  77. outputexedir : dirstr;
  78. outputunitdir : dirstr;
  79. { things specified with parameters }
  80. paralinkoptions,
  81. paradynamiclinker : string;
  82. parapreprocess : boolean;
  83. { directory where the utils can be found (options -FD) }
  84. utilsdirectory : dirstr;
  85. { some flags for global compiler switches }
  86. do_build,
  87. do_make : boolean;
  88. not_unit_proc : boolean;
  89. { path for searching units, different paths can be seperated by ; }
  90. exepath : dirstr; { Path to ppc }
  91. librarysearchpath,
  92. unitsearchpath,
  93. objectsearchpath,
  94. includesearchpath : TSearchPathList;
  95. { deffile }
  96. usewindowapi : boolean;
  97. description : string;
  98. dllversion : string;
  99. dllmajor,dllminor,dllrevision : word; { revision only for netware }
  100. { current position }
  101. token, { current token being parsed }
  102. idtoken : ttoken; { holds the token if the pattern is a known word }
  103. tokenpos, { last postion of the read token }
  104. aktfilepos : tfileposinfo; { current position }
  105. { type of currently parsed block }
  106. { isn't full implemented (FK) }
  107. block_type : tblock_type;
  108. in_args : boolean; { arguments must be checked especially }
  109. parsing_para_level : longint; { parameter level, used to convert
  110. proc calls to proc loads in firstcalln }
  111. { Must_be_valid : boolean; should the variable already have a value
  112. obsolete replace by set_varstate function }
  113. compile_level : word;
  114. make_ref : boolean;
  115. resolving_forward : boolean; { used to add forward reference as second ref }
  116. use_esp_stackframe : boolean; { to test for call with ESP as stack frame }
  117. inlining_procedure : boolean; { are we inlining a procedure }
  118. { commandline values }
  119. initdefines : tlinkedlist;
  120. initglobalswitches : tglobalswitches;
  121. initmoduleswitches : tmoduleswitches;
  122. initlocalswitches : tlocalswitches;
  123. initmodeswitches : tmodeswitches;
  124. {$IFDEF testvarsets}
  125. Initsetalloc, {0=fixed, 1 =var}
  126. {$ENDIF}
  127. initpackenum : longint;
  128. initpackrecords : tpackrecords;
  129. initoutputformat : tasm;
  130. initoptprocessor,
  131. initspecificoptprocessor : tprocessors;
  132. initasmmode : tasmmode;
  133. { current state values }
  134. aktglobalswitches : tglobalswitches;
  135. aktmoduleswitches : tmoduleswitches;
  136. aktlocalswitches : tlocalswitches;
  137. nextaktlocalswitches : tlocalswitches;
  138. localswitcheschanged : boolean;
  139. aktmodeswitches : tmodeswitches;
  140. {$IFDEF testvarsets}
  141. aktsetalloc,
  142. {$ENDIF}
  143. aktpackenum : longint;
  144. aktmaxfpuregisters: longint;
  145. aktpackrecords : tpackrecords;
  146. aktoutputformat : tasm;
  147. aktoptprocessor,
  148. aktspecificoptprocessor : tprocessors;
  149. aktasmmode : tasmmode;
  150. { Memory sizes }
  151. heapsize,
  152. maxheapsize,
  153. stacksize : longint;
  154. {$Ifdef EXTDEBUG}
  155. total_of_firstpass,
  156. firstpass_several : longint;
  157. {$ifdef FPC}
  158. EntryMemUsed : longint;
  159. {$endif FPC}
  160. { parameter switches }
  161. debugstop,
  162. only_one_pass : boolean;
  163. {$EndIf EXTDEBUG}
  164. { windows application type }
  165. apptype : tapptype;
  166. const
  167. RelocSection : boolean = true;
  168. RelocSectionSetExplicitly : boolean = false;
  169. LinkTypeSetExplicitly : boolean = false;
  170. DLLsource : boolean = false;
  171. DLLImageBase : pstring = nil;
  172. UseDeffileForExport : boolean = true;
  173. ForceDeffileForExport : boolean = false;
  174. { used to set all registers used for each global function
  175. this should dramatically decrease the number of
  176. recompilations needed PM }
  177. simplify_ppu : boolean = false;
  178. { should we allow non static members ? }
  179. allow_only_static : boolean = false;
  180. Inside_asm_statement : boolean = false;
  181. { for error info in pp.pas }
  182. const
  183. parser_current_file : string = '';
  184. procedure abstract;
  185. function bstoslash(const s : string) : string;
  186. function getdatestr:string;
  187. function gettimestr:string;
  188. function filetimestring( t : longint) : string;
  189. procedure DefaultReplacements(var s:string);
  190. function GetCurrentDir:string;
  191. function path_absolute(const s : string) : boolean;
  192. Function PathExists ( F : String) : Boolean;
  193. Function FileExists ( Const F : String) : Boolean;
  194. Function RemoveFile(const f:string):boolean;
  195. Function RemoveDir(d:string):boolean;
  196. Function GetFileTime ( Var F : File) : Longint;
  197. Function GetNamedFileTime ( Const F : String) : Longint;
  198. Function SplitPath(const s:string):string;
  199. Function SplitFileName(const s:string):string;
  200. Function SplitName(const s:string):string;
  201. Function SplitExtension(Const HStr:String):String;
  202. Function AddExtension(Const HStr,ext:String):String;
  203. Function ForceExtension(Const HStr,ext:String):String;
  204. Function FixPath(s:string;allowdot:boolean):string;
  205. function FixFileName(const s:string):string;
  206. procedure SplitBinCmd(const s:string;var bstr,cstr:string);
  207. procedure SynchronizeFileTime(const fn1,fn2:string);
  208. function FindFile(const f : string;path : string;var b : boolean) : string;
  209. function FindExe(bin:string;var found:boolean):string;
  210. function GetShortName(const n:string):string;
  211. Procedure Shell(const command:string);
  212. function GetEnvPChar(const envname:string):pchar;
  213. procedure FreeEnvPChar(p:pchar);
  214. procedure InitGlobals;
  215. procedure DoneGlobals;
  216. implementation
  217. uses
  218. comphook;
  219. procedure abstract;
  220. begin
  221. do_internalerror(255);
  222. end;
  223. function ngraphsearchvalue(const s1,s2 : string) : double;
  224. const
  225. n = 3;
  226. var
  227. equals,i,j : longint;
  228. hs : string;
  229. begin
  230. equals:=0;
  231. { is the string long enough ? }
  232. if min(length(s1),length(s2))-n+1<1 then
  233. begin
  234. ngraphsearchvalue:=0.0;
  235. exit;
  236. end;
  237. for i:=1 to length(s1)-n+1 do
  238. begin
  239. hs:=copy(s1,i,n);
  240. for j:=1 to length(s2)-n+1 do
  241. if hs=copy(s2,j,n) then
  242. inc(equals);
  243. end;
  244. {$ifdef fpc}
  245. ngraphsearchvalue:=equals/double(max(length(s1),length(s2))-n+1);
  246. {$else}
  247. ngraphsearchvalue:=equals/(max(length(s1),length(s2))-n+1);
  248. {$endif}
  249. end;
  250. function bstoslash(const s : string) : string;
  251. {
  252. return string s with all \ changed into /
  253. }
  254. var
  255. i : longint;
  256. begin
  257. for i:=1to length(s) do
  258. if s[i]='\' then
  259. bstoslash[i]:='/'
  260. else
  261. bstoslash[i]:=s[i];
  262. bstoslash[0]:=s[0];
  263. end;
  264. {****************************************************************************
  265. Time Handling
  266. ****************************************************************************}
  267. Function L0(l:longint):string;
  268. {
  269. return the string of value l, if l<10 then insert a zero, so
  270. the string is always at least 2 chars '01','02',etc
  271. }
  272. var
  273. s : string;
  274. begin
  275. Str(l,s);
  276. if l<10 then
  277. s:='0'+s;
  278. L0:=s;
  279. end;
  280. function gettimestr:string;
  281. {
  282. get the current time in a string HH:MM:SS
  283. }
  284. var
  285. hour,min,sec,hsec : word;
  286. begin
  287. {$ifdef delphi}
  288. dmisc.gettime(hour,min,sec,hsec);
  289. {$else delphi}
  290. dos.gettime(hour,min,sec,hsec);
  291. {$endif delphi}
  292. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  293. end;
  294. function getdatestr:string;
  295. {
  296. get the current date in a string YY/MM/DD
  297. }
  298. var
  299. Year,Month,Day,Wday : Word;
  300. begin
  301. {$ifdef delphi}
  302. dmisc.getdate(year,month,day,wday);
  303. {$else}
  304. dos.getdate(year,month,day,wday);
  305. {$endif}
  306. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  307. end;
  308. function filetimestring( t : longint) : string;
  309. {
  310. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  311. }
  312. var
  313. {$ifndef linux}
  314. DT : DateTime;
  315. {$endif}
  316. Year,Month,Day,Hour,Min,Sec : Word;
  317. begin
  318. if t=-1 then
  319. begin
  320. FileTimeString:='Not Found';
  321. exit;
  322. end;
  323. {$ifndef linux}
  324. unpacktime(t,DT);
  325. Year:=dT.year;month:=dt.month;day:=dt.day;
  326. Hour:=dt.hour;min:=dt.min;sec:=dt.sec;
  327. {$else}
  328. EpochToLocal (t,year,month,day,hour,min,sec);
  329. {$endif}
  330. filetimestring:=L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  331. end;
  332. {****************************************************************************
  333. Default Macro Handling
  334. ****************************************************************************}
  335. procedure DefaultReplacements(var s:string);
  336. begin
  337. { Replace some macro's }
  338. Replace(s,'$FPCVER',version_string);
  339. Replace(s,'$VERSION',version_string);
  340. Replace(s,'$FULLVERSION',full_version_string);
  341. Replace(s,'$FPCDATE',date_string);
  342. Replace(s,'$FPCTARGET',target_cpu_string);
  343. Replace(s,'$FPCCPU',target_cpu_string);
  344. Replace(s,'$TARGET',target_path);
  345. Replace(s,'$FPCOS',target_path);
  346. end;
  347. {****************************************************************************
  348. File Handling
  349. ****************************************************************************}
  350. function GetCurrentDir:string;
  351. var
  352. CurrentDir : string;
  353. begin
  354. GetDir(0,CurrentDir);
  355. GetCurrentDir:=FixPath(CurrentDir,false);
  356. end;
  357. function path_absolute(const s : string) : boolean;
  358. {
  359. is path s an absolute path?
  360. }
  361. begin
  362. path_absolute:=false;
  363. {$ifdef linux}
  364. if (length(s)>0) and (s[1]='/') then
  365. path_absolute:=true;
  366. {$else linux}
  367. {$ifdef amiga}
  368. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or (Pos(':',s) = length(s)) then
  369. path_absolute:=true;
  370. {$else}
  371. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or
  372. ((length(s)>2) and (s[2]=':') and ((s[3]='\') or (s[3]='/'))) then
  373. path_absolute:=true;
  374. {$endif amiga}
  375. {$endif linux}
  376. end;
  377. {$ifndef FPC}
  378. Procedure FindClose(var Info : SearchRec);
  379. Begin
  380. End;
  381. {$endif not FPC}
  382. Function FileExists ( Const F : String) : Boolean;
  383. {$ifndef delphi}
  384. Var
  385. Info : SearchRec;
  386. {$endif}
  387. begin
  388. {$ifdef delphi}
  389. FileExists:=sysutils.FileExists(f);
  390. {$else}
  391. findfirst(F,readonly+archive+hidden,info);
  392. FileExists:=(doserror=0);
  393. findclose(Info);
  394. {$endif delphi}
  395. end;
  396. Function PathExists ( F : String) : Boolean;
  397. Var
  398. Info : SearchRec;
  399. begin
  400. if F[Length(f)] in ['/','\'] then
  401. Delete(f,length(f),1);
  402. findfirst(F,readonly+archive+hidden+directory,info);
  403. PathExists:=(doserror=0) and ((info.attr and directory)=directory);
  404. findclose(Info);
  405. end;
  406. Function RemoveFile(const f:string):boolean;
  407. var
  408. g : file;
  409. begin
  410. assign(g,f);
  411. {$I-}
  412. erase(g);
  413. {$I+}
  414. RemoveFile:=(ioresult=0);
  415. end;
  416. Function RemoveDir(d:string):boolean;
  417. begin
  418. if d[length(d)]=DirSep then
  419. Delete(d,length(d),1);
  420. {$I-}
  421. rmdir(d);
  422. {$I+}
  423. RemoveDir:=(ioresult=0);
  424. end;
  425. Function SplitPath(const s:string):string;
  426. var
  427. i : longint;
  428. begin
  429. i:=Length(s);
  430. while (i>0) and not(s[i] in ['/','\']) do
  431. dec(i);
  432. SplitPath:=Copy(s,1,i);
  433. end;
  434. Function SplitFileName(const s:string):string;
  435. var
  436. p : dirstr;
  437. n : namestr;
  438. e : extstr;
  439. begin
  440. FSplit(s,p,n,e);
  441. SplitFileName:=n+e;
  442. end;
  443. Function SplitName(const s:string):string;
  444. var
  445. i,j : longint;
  446. begin
  447. i:=Length(s);
  448. j:=Length(s);
  449. while (i>0) and not(s[i] in ['/','\']) do
  450. dec(i);
  451. while (j>0) and (s[j]<>'.') do
  452. dec(j);
  453. if j<=i then
  454. j:=255;
  455. SplitName:=Copy(s,i+1,j-(i+1));
  456. end;
  457. Function SplitExtension(Const HStr:String):String;
  458. var
  459. j : longint;
  460. begin
  461. j:=length(Hstr);
  462. while (j>0) and (Hstr[j]<>'.') do
  463. begin
  464. if hstr[j]=DirSep then
  465. j:=0
  466. else
  467. dec(j);
  468. end;
  469. if j=0 then
  470. j:=254;
  471. SplitExtension:=Copy(Hstr,j,255);
  472. end;
  473. Function AddExtension(Const HStr,ext:String):String;
  474. begin
  475. if (Ext<>'') and (SplitExtension(HStr)='') then
  476. AddExtension:=Hstr+Ext
  477. else
  478. AddExtension:=Hstr;
  479. end;
  480. Function ForceExtension(Const HStr,ext:String):String;
  481. var
  482. j : longint;
  483. begin
  484. j:=length(Hstr);
  485. while (j>0) and (Hstr[j]<>'.') do
  486. dec(j);
  487. if j=0 then
  488. j:=255;
  489. ForceExtension:=Copy(Hstr,1,j-1)+Ext;
  490. end;
  491. Function FixPath(s:string;allowdot:boolean):string;
  492. var
  493. i : longint;
  494. begin
  495. { Fix separator }
  496. for i:=1 to length(s) do
  497. if s[i] in ['/','\'] then
  498. s[i]:=DirSep;
  499. { Fix ending / }
  500. if (length(s)>0) and (s[length(s)]<>DirSep) and
  501. (s[length(s)]<>':') then
  502. s:=s+DirSep;
  503. { Remove ./ }
  504. if (not allowdot) and (s='.'+DirSep) then
  505. s:='';
  506. { return }
  507. {$ifdef linux}
  508. FixPath:=s;
  509. {$else}
  510. FixPath:=Lower(s);
  511. {$endif}
  512. end;
  513. function FixFileName(const s:string):string;
  514. var
  515. i : longint;
  516. {$ifdef Linux}
  517. NoPath : boolean;
  518. {$endif Linux}
  519. begin
  520. {$ifdef Linux}
  521. NoPath:=true;
  522. {$endif Linux}
  523. for i:=length(s) downto 1 do
  524. begin
  525. case s[i] of
  526. {$ifdef Linux}
  527. '/','\' : begin
  528. FixFileName[i]:='/';
  529. NoPath:=false; {Skip lowercasing path: 'X11'<>'x11' }
  530. end;
  531. 'A'..'Z' : if NoPath then
  532. FixFileName[i]:=char(byte(s[i])+32)
  533. else
  534. FixFileName[i]:=s[i];
  535. {$else}
  536. '/' : FixFileName[i]:='\';
  537. 'A'..'Z' : FixFileName[i]:=char(byte(s[i])+32);
  538. {$endif}
  539. else
  540. FixFileName[i]:=s[i];
  541. end;
  542. end;
  543. FixFileName[0]:=s[0];
  544. end;
  545. procedure SplitBinCmd(const s:string;var bstr,cstr:string);
  546. var
  547. i : longint;
  548. begin
  549. i:=pos(' ',s);
  550. if i>0 then
  551. begin
  552. bstr:=Copy(s,1,i-1);
  553. cstr:=Copy(s,i+1,length(s)-i);
  554. end
  555. else
  556. begin
  557. bstr:='';
  558. cstr:='';
  559. end;
  560. end;
  561. procedure TSearchPathList.AddPath(s:string;addfirst:boolean);
  562. var
  563. j : longint;
  564. hs,hsd,
  565. CurrentDir,
  566. CurrPath : string;
  567. dir : searchrec;
  568. hp : PStringQueueItem;
  569. procedure addcurrpath;
  570. begin
  571. if addfirst then
  572. begin
  573. Delete(currPath);
  574. Insert(currPath);
  575. end
  576. else
  577. begin
  578. { Check if already in path, then we don't add it }
  579. hp:=Find(currPath);
  580. if not assigned(hp) then
  581. Concat(currPath);
  582. end;
  583. end;
  584. begin
  585. if s='' then
  586. exit;
  587. { Support default macro's }
  588. DefaultReplacements(s);
  589. { get current dir }
  590. CurrentDir:=GetCurrentDir;
  591. repeat
  592. { get currpath }
  593. if addfirst then
  594. begin
  595. j:=length(s);
  596. while (j>0) and (s[j]<>';') do
  597. dec(j);
  598. CurrPath:=FixPath(Copy(s,j+1,length(s)-j),false);
  599. if j=0 then
  600. s:=''
  601. else
  602. System.Delete(s,j,length(s)-j+1);
  603. end
  604. else
  605. begin
  606. j:=Pos(';',s);
  607. if j=0 then
  608. j:=255;
  609. CurrPath:=FixPath(Copy(s,1,j-1),false);
  610. System.Delete(s,1,j);
  611. end;
  612. { fix pathname }
  613. if CurrPath='' then
  614. CurrPath:='.'+DirSep
  615. else
  616. begin
  617. CurrPath:=FixPath(FExpand(CurrPath),false);
  618. if (CurrentDir<>'') and (Copy(CurrPath,1,length(CurrentDir))=CurrentDir) then
  619. CurrPath:='.'+DirSep+Copy(CurrPath,length(CurrentDir)+1,255);
  620. end;
  621. { wildcard adding ? }
  622. if pos('*',currpath)>0 then
  623. begin
  624. if currpath[length(currpath)]=dirsep then
  625. hs:=Copy(currpath,1,length(CurrPath)-1)
  626. else
  627. hs:=currpath;
  628. hsd:=SplitPath(hs);
  629. findfirst(hs,directory,dir);
  630. while doserror=0 do
  631. begin
  632. if (dir.name<>'.') and
  633. (dir.name<>'..') and
  634. ((dir.attr and directory)<>0) then
  635. begin
  636. currpath:=hsd+dir.name+dirsep;
  637. hp:=Find(currPath);
  638. if not assigned(hp) then
  639. AddCurrPath;
  640. end;
  641. findnext(dir);
  642. end;
  643. FindClose(dir);
  644. end
  645. else
  646. begin
  647. if PathExists(currpath) then
  648. addcurrpath;
  649. end;
  650. until (s='');
  651. end;
  652. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  653. var
  654. s : string;
  655. hl : TSearchPathList;
  656. hp,hp2 : PStringQueueItem;
  657. begin
  658. if list.empty then
  659. exit;
  660. { create temp and reverse the list }
  661. if addfirst then
  662. begin
  663. hl.Init;
  664. hp:=list.first;
  665. while assigned(hp) do
  666. begin
  667. hl.insert(hp^.data^);
  668. hp:=hp^.next;
  669. end;
  670. while not hl.empty do
  671. begin
  672. s:=hl.Get;
  673. Delete(s);
  674. Insert(s);
  675. end;
  676. hl.done;
  677. end
  678. else
  679. begin
  680. hp:=list.first;
  681. while assigned(hp) do
  682. begin
  683. hp2:=Find(hp^.data^);
  684. { Check if already in path, then we don't add it }
  685. if not assigned(hp2) then
  686. Concat(hp^.data^);
  687. hp:=hp^.next;
  688. end;
  689. end;
  690. end;
  691. function TSearchPathList.FindFile(const f : string;var b : boolean) : string;
  692. Var
  693. p : PStringQueueItem;
  694. begin
  695. FindFile:='';
  696. b:=false;
  697. p:=first;
  698. while assigned(p) do
  699. begin
  700. If FileExists(p^.data^+f) then
  701. begin
  702. FindFile:=p^.data^;
  703. b:=true;
  704. exit;
  705. end;
  706. p:=p^.next;
  707. end;
  708. end;
  709. Function GetFileTime ( Var F : File) : Longint;
  710. Var
  711. {$ifdef linux}
  712. Info : Stat;
  713. {$endif}
  714. L : longint;
  715. begin
  716. {$ifdef linux}
  717. FStat (F,Info);
  718. L:=Info.Mtime;
  719. {$else}
  720. GetFTime(f,l);
  721. {$endif}
  722. GetFileTime:=L;
  723. end;
  724. Function GetNamedFileTime (Const F : String) : Longint;
  725. begin
  726. GetNamedFileTime:=do_getnamedfiletime(F);
  727. end;
  728. {Touch Assembler and object time to ppu time is there is a ppufilename}
  729. procedure SynchronizeFileTime(const fn1,fn2:string);
  730. var
  731. f : file;
  732. l : longint;
  733. begin
  734. Assign(f,fn1);
  735. {$I-}
  736. reset(f,1);
  737. {$I+}
  738. if ioresult=0 then
  739. begin
  740. getftime(f,l);
  741. { just to be sure in case there are rounding errors }
  742. setftime(f,l);
  743. close(f);
  744. assign(f,fn2);
  745. {$I-}
  746. reset(f,1);
  747. {$I+}
  748. if ioresult=0 then
  749. begin
  750. setftime(f,l);
  751. close(f);
  752. end;
  753. end;
  754. end;
  755. function FindFile(const f : string;path : string;var b : boolean) : string;
  756. Var
  757. singlepathstring : string;
  758. i : longint;
  759. begin
  760. {$ifdef linux}
  761. for i:=1 to length(path) do
  762. if path[i]=':' then
  763. path[i]:=';';
  764. {$endif}
  765. b:=false;
  766. FindFile:='';
  767. repeat
  768. i:=pos(';',path);
  769. if i=0 then
  770. i:=256;
  771. singlepathstring:=FixPath(copy(path,1,i-1),false);
  772. delete(path,1,i);
  773. If FileExists (singlepathstring+f) then
  774. begin
  775. FindFile:=singlepathstring;
  776. b:=true;
  777. exit;
  778. end;
  779. until path='';
  780. end;
  781. function FindExe(bin:string;var found:boolean):string;
  782. begin
  783. bin:=FixFileName(bin)+source_os.exeext;
  784. {$ifdef delphi}
  785. FindExe:=FindFile(bin,'.;'+exepath+';'+dmisc.getenv('PATH'),found)+bin;
  786. {$else delphi}
  787. FindExe:=FindFile(bin,'.;'+exepath+';'+dos.getenv('PATH'),found)+bin;
  788. {$endif delphi}
  789. end;
  790. function GetShortName(const n:string):string;
  791. {$ifdef win32}
  792. var
  793. hs,hs2 : string;
  794. i : longint;
  795. {$endif}
  796. {$ifdef go32v2}
  797. var
  798. hs : string;
  799. {$endif}
  800. begin
  801. GetShortName:=n;
  802. {$ifdef win32}
  803. hs:=n+#0;
  804. i:=Windows.GetShortPathName(@hs[1],@hs2[1],high(hs2));
  805. if (i>0) and (i<=high(hs2)) then
  806. begin
  807. hs2[0]:=chr(strlen(@hs2[1]));
  808. GetShortName:=hs2;
  809. end;
  810. {$endif}
  811. {$ifdef go32v2}
  812. hs:=n;
  813. if Dos.GetShortName(hs) then
  814. GetShortName:=hs;
  815. {$endif}
  816. end;
  817. {****************************************************************************
  818. OS Dependent things
  819. ****************************************************************************}
  820. function GetEnvPChar(const envname:string):pchar;
  821. {$ifdef win32}
  822. var
  823. s : string;
  824. i,len : longint;
  825. hp,p,p2 : pchar;
  826. {$endif}
  827. begin
  828. {$ifdef linux}
  829. GetEnvPchar:=Linux.Getenv(envname);
  830. {$define GETENVOK}
  831. {$endif}
  832. {$ifdef win32}
  833. GetEnvPchar:=nil;
  834. p:=GetEnvironmentStrings;
  835. hp:=p;
  836. while hp^<>#0 do
  837. begin
  838. s:=strpas(hp);
  839. i:=pos('=',s);
  840. len:=strlen(hp);
  841. if upper(copy(s,1,i-1))=upper(envname) then
  842. begin
  843. GetMem(p2,len-length(envname));
  844. Move(hp[i],p2^,len-length(envname));
  845. GetEnvPchar:=p2;
  846. break;
  847. end;
  848. { next string entry}
  849. hp:=hp+len+1;
  850. end;
  851. FreeEnvironmentStrings(p);
  852. {$define GETENVOK}
  853. {$endif}
  854. {$ifdef GETENVOK}
  855. {$undef GETENVOK}
  856. {$else}
  857. GetEnvPchar:=StrPNew(Dos.Getenv(envname));
  858. {$endif}
  859. end;
  860. procedure FreeEnvPChar(p:pchar);
  861. begin
  862. {$ifndef linux}
  863. StrDispose(p);
  864. {$endif}
  865. end;
  866. Procedure Shell(const command:string);
  867. { This is already defined in the linux.ppu for linux, need for the *
  868. expansion under linux }
  869. {$ifdef linux}
  870. begin
  871. Linux.Shell(command);
  872. end;
  873. {$else}
  874. var
  875. comspec : string;
  876. begin
  877. comspec:=getenv('COMSPEC');
  878. Exec(comspec,' /C '+command);
  879. end;
  880. {$endif}
  881. {****************************************************************************
  882. Init
  883. ****************************************************************************}
  884. {$ifdef linux}
  885. {$define need_path_search}
  886. {$endif linux}
  887. {$ifdef os2}
  888. {$define need_path_search}
  889. {$endif os2}
  890. procedure get_exepath;
  891. var
  892. hs1 : namestr;
  893. hs2 : extstr;
  894. b : boolean;
  895. begin
  896. {$ifdef delphi}
  897. exepath:=dmisc.getenv('PPC_EXEC_PATH');
  898. {$else delphi}
  899. exepath:=dos.getenv('PPC_EXEC_PATH');
  900. {$endif delphi}
  901. if exepath='' then
  902. fsplit(FixFileName(paramstr(0)),exepath,hs1,hs2);
  903. {$ifdef need_path_search}
  904. if exepath='' then
  905. begin
  906. if pos(source_os.exeext,hs1) <>
  907. (length(hs1) - length(source_os.exeext)+1) then
  908. hs1 := hs1 + source_os.exeext;
  909. {$ifdef delphi}
  910. exepath := findfile(hs1,dmisc.getenv('PATH'),b);
  911. {$else delphi}
  912. exepath := findfile(hs1,dos.getenv('PATH'),b);
  913. {$endif delphi}
  914. end;
  915. {$endif need_path_search}
  916. exepath:=FixPath(exepath,false);
  917. end;
  918. procedure DoneGlobals;
  919. begin
  920. initdefines.done;
  921. if assigned(DLLImageBase) then
  922. StringDispose(DLLImageBase);
  923. RelocSection:=true;
  924. RelocSectionSetExplicitly:=false;
  925. DLLsource:=false;
  926. UseDeffileForExport:=true;
  927. librarysearchpath.Done;
  928. unitsearchpath.Done;
  929. objectsearchpath.Done;
  930. includesearchpath.Done;
  931. end;
  932. procedure InitGlobals;
  933. begin
  934. { set global switches }
  935. do_build:=false;
  936. do_make:=true;
  937. compile_level:=0;
  938. { Output }
  939. OutputFile:='';
  940. OutputExeDir:='';
  941. OutputUnitDir:='';
  942. { Utils directory }
  943. utilsdirectory:='';
  944. { Search Paths }
  945. librarysearchpath.Init;
  946. unitsearchpath.Init;
  947. includesearchpath.Init;
  948. objectsearchpath.Init;
  949. { Def file }
  950. usewindowapi:=false;
  951. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  952. dllversion:='';
  953. { Init values }
  954. initmodeswitches:=fpcmodeswitches;
  955. initlocalswitches:=[cs_check_io];
  956. initmoduleswitches:=[cs_extsyntax,cs_browser];
  957. initglobalswitches:=[cs_check_unit_name,cs_link_static];
  958. {$ifdef i386}
  959. initoptprocessor:=Class386;
  960. initspecificoptprocessor:=Class386;
  961. initpackenum:=4;
  962. {$IFDEF testvarsets}
  963. initsetalloc:=0;
  964. {$ENDIF}
  965. initpackrecords:=packrecord_2;
  966. initoutputformat:=target_asm.id;
  967. initasmmode:=asmmode_i386_att;
  968. {$else not i386}
  969. {$ifdef m68k}
  970. initoptprocessor:=MC68000;
  971. include(initmoduleswitches,cs_fp_emulation);
  972. initpackenum:=4;
  973. {$IFDEF testvarsets}
  974. initsetalloc:=0;
  975. {$ENDIF}
  976. initpackrecords:=packrecord_2;
  977. initoutputformat:=as_m68k_as;
  978. initasmmode:=asmmode_m68k_mot;
  979. {$endif m68k}
  980. {$endif i386}
  981. initdefines.init;
  982. { memory sizes, will be overriden by parameter or default for target
  983. in options or init_parser }
  984. stacksize:=0;
  985. heapsize:=0;
  986. maxheapsize:=0;
  987. { compile state }
  988. in_args:=false;
  989. { must_be_valid:=true; obsolete PM }
  990. not_unit_proc:=true;
  991. apptype:=at_cui;
  992. end;
  993. begin
  994. get_exepath;
  995. {$ifdef EXTDEBUG}
  996. {$ifdef FPC}
  997. EntryMemUsed:=system.HeapSize-MemAvail;
  998. {$endif FPC}
  999. {$endif}
  1000. end.
  1001. {
  1002. $Log$
  1003. Revision 1.11 2000-09-24 15:12:40 peter
  1004. * fixed typo
  1005. Revision 1.10 2000/09/24 15:06:16 peter
  1006. * use defines.inc
  1007. Revision 1.9 2000/09/24 10:33:07 peter
  1008. * searching of exe in path also for OS/2
  1009. * fixed searching of exe in path.
  1010. Revision 1.8 2000/09/11 17:00:22 florian
  1011. + first implementation of Netware Module support, thanks to
  1012. Armin Diehl ([email protected]) for providing the patches
  1013. Revision 1.7 2000/08/27 16:11:51 peter
  1014. * moved some util functions from globals,cobjects to cutils
  1015. * splitted files into finput,fmodule
  1016. Revision 1.6 2000/08/12 19:14:58 peter
  1017. * ELF writer works now also with -g
  1018. * ELF writer is default again for linux
  1019. Revision 1.5 2000/08/12 15:30:44 peter
  1020. * IDE patch for stream reading (merged)
  1021. Revision 1.4 2000/08/02 19:49:59 peter
  1022. * first things for default parameters
  1023. Revision 1.3 2000/07/13 12:08:25 michael
  1024. + patched to 1.1.0 with former 1.09patch from peter
  1025. Revision 1.2 2000/07/13 11:32:41 michael
  1026. + removed logs
  1027. }