globals.pas 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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. {$ifdef need_path_search}
  895. b : boolean;
  896. {$endif}
  897. begin
  898. {$ifdef delphi}
  899. exepath:=dmisc.getenv('PPC_EXEC_PATH');
  900. {$else delphi}
  901. exepath:=dos.getenv('PPC_EXEC_PATH');
  902. {$endif delphi}
  903. if exepath='' then
  904. fsplit(FixFileName(paramstr(0)),exepath,hs1,hs2);
  905. {$ifdef need_path_search}
  906. if exepath='' then
  907. begin
  908. if pos(source_os.exeext,hs1) <>
  909. (length(hs1) - length(source_os.exeext)+1) then
  910. hs1 := hs1 + source_os.exeext;
  911. {$ifdef delphi}
  912. exepath := findfile(hs1,dmisc.getenv('PATH'),b);
  913. {$else delphi}
  914. exepath := findfile(hs1,dos.getenv('PATH'),b);
  915. {$endif delphi}
  916. end;
  917. {$endif need_path_search}
  918. exepath:=FixPath(exepath,false);
  919. end;
  920. procedure DoneGlobals;
  921. begin
  922. initdefines.done;
  923. if assigned(DLLImageBase) then
  924. StringDispose(DLLImageBase);
  925. RelocSection:=true;
  926. RelocSectionSetExplicitly:=false;
  927. DLLsource:=false;
  928. UseDeffileForExport:=true;
  929. librarysearchpath.Done;
  930. unitsearchpath.Done;
  931. objectsearchpath.Done;
  932. includesearchpath.Done;
  933. end;
  934. procedure InitGlobals;
  935. begin
  936. { set global switches }
  937. do_build:=false;
  938. do_make:=true;
  939. compile_level:=0;
  940. { Output }
  941. OutputFile:='';
  942. OutputExeDir:='';
  943. OutputUnitDir:='';
  944. { Utils directory }
  945. utilsdirectory:='';
  946. { Search Paths }
  947. librarysearchpath.Init;
  948. unitsearchpath.Init;
  949. includesearchpath.Init;
  950. objectsearchpath.Init;
  951. { Def file }
  952. usewindowapi:=false;
  953. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  954. dllversion:='';
  955. { Init values }
  956. initmodeswitches:=fpcmodeswitches;
  957. initlocalswitches:=[cs_check_io];
  958. initmoduleswitches:=[cs_extsyntax,cs_browser];
  959. initglobalswitches:=[cs_check_unit_name,cs_link_static];
  960. {$ifdef i386}
  961. initoptprocessor:=Class386;
  962. initspecificoptprocessor:=Class386;
  963. initpackenum:=4;
  964. {$IFDEF testvarsets}
  965. initsetalloc:=0;
  966. {$ENDIF}
  967. initpackrecords:=packrecord_2;
  968. initoutputformat:=target_asm.id;
  969. initasmmode:=asmmode_i386_att;
  970. {$else not i386}
  971. {$ifdef m68k}
  972. initoptprocessor:=MC68000;
  973. include(initmoduleswitches,cs_fp_emulation);
  974. initpackenum:=4;
  975. {$IFDEF testvarsets}
  976. initsetalloc:=0;
  977. {$ENDIF}
  978. initpackrecords:=packrecord_2;
  979. initoutputformat:=as_m68k_as;
  980. initasmmode:=asmmode_m68k_mot;
  981. {$endif m68k}
  982. {$endif i386}
  983. initdefines.init;
  984. { memory sizes, will be overriden by parameter or default for target
  985. in options or init_parser }
  986. stacksize:=0;
  987. heapsize:=0;
  988. maxheapsize:=0;
  989. { compile state }
  990. in_args:=false;
  991. { must_be_valid:=true; obsolete PM }
  992. not_unit_proc:=true;
  993. apptype:=at_cui;
  994. end;
  995. begin
  996. get_exepath;
  997. {$ifdef EXTDEBUG}
  998. {$ifdef FPC}
  999. EntryMemUsed:=system.HeapSize-MemAvail;
  1000. {$endif FPC}
  1001. {$endif}
  1002. end.
  1003. {
  1004. $Log$
  1005. Revision 1.12 2000-09-24 21:19:50 peter
  1006. * delphi compile fixes
  1007. Revision 1.11 2000/09/24 15:12:40 peter
  1008. * fixed typo
  1009. Revision 1.10 2000/09/24 15:06:16 peter
  1010. * use defines.inc
  1011. Revision 1.9 2000/09/24 10:33:07 peter
  1012. * searching of exe in path also for OS/2
  1013. * fixed searching of exe in path.
  1014. Revision 1.8 2000/09/11 17:00:22 florian
  1015. + first implementation of Netware Module support, thanks to
  1016. Armin Diehl ([email protected]) for providing the patches
  1017. Revision 1.7 2000/08/27 16:11:51 peter
  1018. * moved some util functions from globals,cobjects to cutils
  1019. * splitted files into finput,fmodule
  1020. Revision 1.6 2000/08/12 19:14:58 peter
  1021. * ELF writer works now also with -g
  1022. * ELF writer is default again for linux
  1023. Revision 1.5 2000/08/12 15:30:44 peter
  1024. * IDE patch for stream reading (merged)
  1025. Revision 1.4 2000/08/02 19:49:59 peter
  1026. * first things for default parameters
  1027. Revision 1.3 2000/07/13 12:08:25 michael
  1028. + patched to 1.1.0 with former 1.09patch from peter
  1029. Revision 1.2 2000/07/13 11:32:41 michael
  1030. + removed logs
  1031. }