fp.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. {
  2. This file is part of the Free Pascal Integrated Development Environment
  3. Copyright (c) 1998-2000 by Berczi Gabor
  4. Main program of the IDE
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program FP;
  12. {$ifdef Windows}
  13. { some windows versions, namely at least XP x64 don't like if the IDE stack
  14. is too big }
  15. {$maxstacksize 3000000}
  16. {$ifdef IncRes}
  17. {$R fpw32t.rc}
  18. {$R fpw32ico.rc}
  19. {$endif IncRes}
  20. {$endif Windows}
  21. {$I globdir.inc}
  22. (**********************************************************************)
  23. (* CONDITIONAL DEFINES *)
  24. (* - NODEBUG No Debugging support *)
  25. (* - i386 Target is an i386 IDE *)
  26. (**********************************************************************)
  27. uses
  28. {$ifdef Windows}
  29. windows,
  30. {$endif Windows}
  31. {$ifndef NODEBUG}
  32. {$ifdef Windows}
  33. {$ifdef USE_MINGW_GDB}
  34. fpmingw,
  35. {$else}
  36. fpcygwin,
  37. {$endif}
  38. {$endif Windows}
  39. {$endif NODEBUG}
  40. {$ifdef IDEHeapTrc}
  41. PPheap,
  42. {$endif IDEHeapTrc}
  43. {$ifdef Use_DBGHEAP}
  44. dbgheap,
  45. {$endif Use_DBGHEAP}
  46. {$ifdef go32v2}
  47. dpmiexcp,
  48. {$endif go32v2}
  49. {$ifdef VESA}
  50. vesa,
  51. {$endif VESA}
  52. keyboard,video,mouse,
  53. {$ifdef HasSignal}
  54. fpcatch,
  55. {$endif HasSignal}
  56. Dos,Objects,
  57. BrowCol,Version,
  58. {$ifndef NODEBUG}
  59. {$ifdef GDBMI}
  60. gdbmiint,
  61. {$else GDBMI}
  62. gdbint,
  63. {$endif GDBMI}
  64. {$endif NODEBUG}
  65. FVConsts,
  66. Drivers,Views,App,Dialogs,HistList,
  67. Menus,StdDlg,Validate,
  68. WEditor,WCEdit,
  69. {$ifdef COLORSEL}
  70. ColorSel,
  71. {$endif COLORSEL}
  72. ASCIITab,
  73. WUtils,WViews,WHTMLScn,WHelp,
  74. FPIDE,FPCalc,FPCompil,
  75. FPIni,FPViews,FPConst,FPVars,FPUtils,FPHelp,FPSwitch,FPUsrScr,
  76. FPTools,
  77. {$ifndef NODEBUG}
  78. FPDebug,FPRegs,
  79. {$ifdef GDBMI}
  80. gdbmiproc,
  81. {$endif GDBMI}
  82. {$endif}
  83. FPTemplt,FPRedir,FPDesk,
  84. FPCodTmp,FPCodCmp,
  85. systems,globtype,globals;
  86. Const
  87. DummyMouseDriver : TMouseDriver = (
  88. useDefaultQueue : true;
  89. InitDriver : nil;
  90. DoneDriver : nil;
  91. DetectMouse : nil;
  92. ShowMouse : nil;
  93. HideMouse : nil;
  94. GetMouseX : nil;
  95. GetMouseY : nil;
  96. GetMouseButtons : nil;
  97. SetMouseXY : nil;
  98. GetMouseEvent : nil;
  99. PollMouseEvent : nil;
  100. PutMouseEvent : nil;
  101. );
  102. {$ifdef useresstrings}
  103. resourcestring
  104. {$else}
  105. const
  106. {$endif}
  107. { caught signals or abnormal exits }
  108. { Debugger messages and status hints }
  109. error_programexitedwitherror = #3'Program generated a RTE %d'#13+
  110. #3'at address $%s.'#13+
  111. #3'Save your sources and restart the IDE.';
  112. error_programexitedwithsignal = #3'Program generated a signal %d.'#13+
  113. #3'Save your sources and restart the IDE.';
  114. continue_despite_error = #3'The IDE generated an internal error'#13+
  115. #3'Do you really want to continue?'#13+
  116. #3'The IDE could be in an unstable state.';
  117. leaving_after_error = #3'The IDE generated an internal error'#13+
  118. #3'and will now be closed.';
  119. {$ifdef DEBUG}
  120. const
  121. CloseImmediately : boolean = false;
  122. var
  123. StartTime : real;
  124. function getrealtime : real;
  125. var
  126. h,m,s,s100 : word;
  127. begin
  128. gettime(h,m,s,s100);
  129. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  130. end;
  131. {$endif DEBUG}
  132. procedure ProcessParams(BeforeINI: boolean);
  133. function IsSwitch(const Param: string): boolean;
  134. begin
  135. IsSwitch:=(Param<>'') and (Param[1]<>DirSep) { <- allow UNIX root-relative paths }
  136. and (Param[1] in ['-','/']); { <- but still accept dos switch AnsiChar, eg. '/' }
  137. end;
  138. var I: Sw_integer;
  139. Param: string;
  140. begin
  141. for I:=1 to ParamCount do
  142. begin
  143. Param:=System.ParamStr(I);
  144. if IsSwitch(Param) then
  145. begin
  146. Param:=copy(Param,2,255);
  147. if Param<>'' then
  148. if UpcaseStr(copy(Param,1,2))='HM' then
  149. { HeapMonitor }
  150. begin
  151. if (copy(Param,3,1)='+') or (copy(Param,3,1)='') then
  152. StartupOptions:=StartupOptions or soHeapMonitor
  153. else
  154. if (copy(Param,3,1)='-') then
  155. StartupOptions:=StartupOptions and not soHeapMonitor;
  156. OverrideHeapMonitor:=true;
  157. end else
  158. {$ifdef go32v2}
  159. if (UpcaseStr(Param)='NOLFN') or (UpcaseStr(Param)='N') then
  160. begin
  161. LFNSupport:=false;
  162. end else
  163. {$endif go32v2}
  164. {$ifdef VESA}
  165. if UpcaseStr(Param)='NOVESA' then
  166. begin
  167. disableVESA:=true;
  168. end else
  169. if UpcaseStr(Param)='VESA' then
  170. begin
  171. (* Force using VESA although it may have been disabled by default *)
  172. disableVESA:=false;
  173. end else
  174. {$endif VESA}
  175. if UpcaseStr(Param)='README' then
  176. begin
  177. ShowReadme:=true;
  178. end else
  179. case Upcase(Param[1]) of
  180. 'C' : { custom config file (BP compatiblity) }
  181. if BeforeINI then
  182. begin
  183. delete(param,1,1); // delete C
  184. if (length(Param)>=1) and (Param[1] in['=',':']) then
  185. Delete(Param,1,1); { eat optional separator }
  186. IniFileName:=MakeFileNameExt(Param,IniExt);
  187. DesktopFileName:=MakeFileNameExt(Param,DesktopExt);
  188. SwitchesFileName:=MakeFileNameExt(Param,SwitchesExt);
  189. DirInfoFileName:=MakeFileNameExt(Param,DirInfoExt);
  190. end;
  191. {$ifdef GDBMI}
  192. 'G' : { custom GDB exec file (GDBMI mode only) }
  193. if BeforeINI then
  194. begin
  195. delete(param,1,1); // delete C
  196. if (length(Param)>=1) and (Param[1] in['=',':']) then
  197. Delete(Param,1,1); { eat optional separator }
  198. GDBProgramName:=Param;
  199. end;
  200. {$endif def GDBMI}
  201. 'R' : { enter the directory last exited from (BP comp.) }
  202. begin
  203. Param:=copy(Param,2,255);
  204. if (Param='') or (Param='+') then
  205. StartupOptions:=StartupOptions or soReturnToLastDir
  206. else
  207. if (Param='-') then
  208. StartupOptions:=StartupOptions and (not soReturnToLastDir);
  209. OverrideLastDirOption:=true;
  210. end;
  211. 'S' :
  212. if Length(Param)=1 then
  213. begin
  214. UseMouse:=false;
  215. DoneMouse;
  216. SetMouseDriver(DummyMouseDriver);
  217. ButtonCount:=0;
  218. end;
  219. { 'F' :
  220. if Length(Param)=1 then
  221. NoExtendedFrame:=true;}
  222. {$ifdef Unix}
  223. 'T' : DebuggeeTTY:=Copy(Param,2,High(Param));
  224. {$endif Unix}
  225. { 'M' : TryToMaximizeScreen:=true;}
  226. {$ifdef DEBUG}
  227. 'Z' : UseOldBufStreamMethod:=true;
  228. 'X' : CloseImmediately:=true;
  229. {$endif DEBUG}
  230. end;
  231. end
  232. else
  233. if not BeforeINI then
  234. TryToOpenFileMulti(nil,Param,0,0,{false}true);
  235. end;
  236. end;
  237. Procedure MyStreamError(Var S: TStream);
  238. var ErrS: string;
  239. begin
  240. case S.Status of
  241. stGetError : ErrS:='Get of unregistered object type';
  242. stPutError : ErrS:='Put of unregistered object type';
  243. else ErrS:='';
  244. end;
  245. if ErrS<>'' then
  246. begin
  247. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  248. ErrorBox('Stream error: '+#13+ErrS,nil)
  249. else
  250. writeln('Error: ',ErrS);
  251. end;
  252. end;
  253. procedure DelTempFiles;
  254. begin
  255. DeleteFile(FPOutFileName);
  256. DeleteFile(FPErrFileName);
  257. DeleteFile(GDBOutFileName);
  258. DeleteFile(GDBOutPutFileName);
  259. DeleteFile(GREPOutName);
  260. DeleteFile(GREPErrName);
  261. end;
  262. procedure RegisterIDEObjects;
  263. begin
  264. RegisterApp;
  265. RegisterCodeComplete;
  266. RegisterCodeTemplates;
  267. {$ifdef COLORSEL}
  268. RegisterColorSel;
  269. {$endif COLORSEL}
  270. RegisterAsciiTab;
  271. RegisterDialogs;
  272. RegisterWEditor;
  273. RegisterWCEdit;
  274. RegisterFPCalc;
  275. RegisterFPCompile;
  276. RegisterFPTools;
  277. RegisterFPViews;
  278. {$ifndef NODEBUG}
  279. RegisterFPDebugViews;
  280. RegisterFPRegsViews;
  281. {$endif}
  282. RegisterMenus;
  283. RegisterStdDlg;
  284. RegisterSymbols;
  285. RegisterObjects;
  286. RegisterValidate;
  287. RegisterViews;
  288. RegisterWHTMLScan;
  289. RegisterWUtils;
  290. RegisterWViews;
  291. end;
  292. var CanExit : boolean;
  293. SetJmpRes : longint;
  294. StoreExitProc : pointer;
  295. ErrS : String;
  296. P : record
  297. l1 : longint;
  298. s : pstring;
  299. end;
  300. const
  301. ExitIntercepted : boolean = false;
  302. SeenExitCode : longint =0;
  303. SeenErrorAddr : pointer = nil;
  304. UserWantsToGoOn: boolean = false;
  305. procedure InterceptExit;
  306. begin
  307. {$IFDEF HasSignal}
  308. if StopJmpValid then
  309. begin
  310. ExitIntercepted:=true;
  311. SeenExitCode:=ExitCode;
  312. SeenErrorAddr:=ErrorAddr;
  313. LongJmp(StopJmp,1);
  314. end;
  315. {$ENDIF}
  316. end;
  317. procedure InitCompilerSwitches;
  318. begin
  319. default_settings.globalswitches:=[cs_check_unit_name];
  320. default_settings.moduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  321. default_settings.localswitches:=[cs_typed_const_writable];
  322. end;
  323. {$IFDEF HASAMIGA}
  324. procedure SetAmigaWindowTitle;
  325. begin
  326. { window title first, then screen title, shown when the window is active }
  327. Video.SetWindowTitle(
  328. 'Free Pascal IDE',
  329. 'Free Pascal IDE '+VersionStr+' ['+{$i %date%}+'] - Compiler '+Full_Version_String);
  330. end;
  331. {$ENDIF}
  332. {The square bullet needs an MS-DOS code page. On Unix it is for sure the code
  333. page is not available before video is initialized. (And only in certain
  334. circumstances after that, so, use a plain ascii character as bullet on Unix.)}
  335. {$if defined(unix) or defined(HASAMIGA)}
  336. const bullet='*';
  337. {$else}
  338. const bullet=#254;
  339. {$endif}
  340. { For DOS in program active current directory is actual system current }
  341. { directory. Have to save and restore on exit. }
  342. {$ifdef go32v2}
  343. var
  344. DirectoryInvokeFpFrom : String;
  345. ChDirRes : Word;
  346. {$endif}
  347. var
  348. _GDBVersion: String;
  349. BEGIN
  350. {$ifdef go32v2}
  351. GetDir(0,DirectoryInvokeFpFrom); {save for restore on exit}
  352. {$endif}
  353. {$IFDEF HasSignal}
  354. EnableCatchSignals;
  355. {$ENDIF}
  356. {$ifdef DEV}
  357. HeapLimit:=4096;
  358. {$endif}
  359. HistorySize:=16384;
  360. { Startup info }
  361. writeln(bullet+' Free Pascal IDE Version '+VersionStr+' ['+{$i %date%}+']');
  362. writeln(bullet+' Compiler Version '+Full_Version_String);
  363. { Process params before printing GDB version because of /G option }
  364. ProcessParams(true);
  365. {$ifndef NODEBUG}
  366. _GDBVersion:=GDBVersion;
  367. while pos(#13#3,_GDBVersion)<>0 do
  368. Delete(_GDBVersion,pos(#13#3,_GDBVersion),2);
  369. writeln(bullet+' GDB Version '+_GDBVersion);
  370. {$ifdef Windows}
  371. {$ifndef USE_MINGW_GDB}
  372. {$ifdef GDBMI}
  373. { No reason to talk about cygwin DLL if we don't use it }
  374. if using_cygwin_gdb then
  375. {$endif GDBMI}
  376. begin
  377. writeln(bullet+' Cygwin "',GetCygwinFullName,'" version ',GetCygwinVersionString);
  378. CheckCygwinVersion;
  379. end;
  380. {$endif}
  381. {$endif Windows}
  382. {$endif NODEBUG}
  383. {$ifdef DEBUG}
  384. StartTime:=getrealtime;
  385. {$endif DEBUG}
  386. InitDirs;
  387. RegisterIDEObjects;
  388. StreamError:=@MyStreamError;
  389. ShowReadme:=ShowReadme or (LocateFile(INIFileName)='');
  390. if LocateFile(INIFileName)<>'' then
  391. writeln(bullet+' Using configuration files from: ',DirOf(LocateFile(INIFileName)));
  392. InitCompilerSwitches;
  393. {$ifdef VESA}
  394. writeln(stderr,'If program stops, try again using -novesa option');
  395. flush(stderr);
  396. InitVESAScreenModes;
  397. if RegisteredVesaVideoModeCount > 0 then
  398. SetVESAVideoDriver;
  399. {$endif}
  400. InitRedir;
  401. {$ifndef NODEBUG}
  402. InitBreakpoints;
  403. InitWatches;
  404. {$endif}
  405. InitReservedWords;
  406. InitHelpFiles;
  407. InitSwitches;
  408. InitINIFile;
  409. InitUserScreen;
  410. InitTools;
  411. InitTemplates;
  412. InitCodeTemplates;
  413. InitCodeComplete;
  414. { init target information etc. }
  415. InitSystems;
  416. IDEApp.Init;
  417. CheckINIFile;
  418. ReadSwitches(SwitchesPath);
  419. { load all options after init because of open files }
  420. ReadINIFile;
  421. InitDesktopFile;
  422. LoadDesktop;
  423. {Adjust to current directory, might be changed by LoadDesktop.}
  424. IDEapp.CurDirChanged;
  425. {Menubar might be changed because of loading INI file.}
  426. IDEapp.reload_menubar;
  427. { Handle Standard Units }
  428. if UseAllUnitsInCodeComplete then
  429. AddAvailableUnitsToCodeComplete(false);
  430. if UseStandardUnitsInCodeComplete and not assigned(UnitsCodeCompleteWords) then
  431. AddStandardUnitsToCodeComplete;
  432. { why are the screen contents parsed at startup? Gabor
  433. to be able to find location of error in last compilation
  434. from command line PM }
  435. ParseUserScreen;
  436. {$IFDEF HASAMIGA}
  437. SetAmigaWindowTitle;
  438. {$ENDIF}
  439. { Update IDE }
  440. IDEApp.Update;
  441. IDEApp.UpdateMode;
  442. IDEApp.UpdateTarget;
  443. IDEApp.UpdateClockAndHeap;
  444. ProcessParams(false);
  445. if ShowReadme then
  446. begin
  447. PutCommand(Application,evCommand,cmShowReadme,nil);
  448. ShowReadme:=false; { do not show next time }
  449. end;
  450. StoreExitProc:=ExitProc;
  451. ExitProc:=@InterceptExit;
  452. repeat
  453. {$IFDEF HasSignal}
  454. SetJmpRes:=setjmp(StopJmp);
  455. StopJmpValid:=true;
  456. {$ENDIF}
  457. UserWantsToGoOn:=false;
  458. if SetJmpRes=0 then
  459. begin
  460. {$ifdef DEBUG}
  461. if not CloseImmediately then
  462. {$endif DEBUG}
  463. IDEApp.Run;
  464. end
  465. else
  466. begin
  467. if (SetJmpRes=1) and ExitIntercepted then
  468. begin
  469. { If ExitProc=@InterceptExit then
  470. ExitProc:=StoreExitProc;}
  471. Str(SeenExitCode,ErrS);
  472. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  473. begin
  474. P.l1:=SeenExitCode;
  475. ErrS:=hexstr(PtrUInt(SeenErrorAddr),sizeof(PtrUInt)*2);
  476. P.s:=@ErrS;
  477. if OKCancelBox(error_programexitedwitherror,@P)=cmCancel then
  478. UserWantsToGoOn:=true;
  479. end
  480. else
  481. writeln('Abnormal exit error: ',ErrS);
  482. end
  483. else
  484. begin
  485. Str(SetJmpRes,ErrS);
  486. { Longjmp was called by fpcatch }
  487. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  488. begin
  489. P.l1:=SetJmpRes;
  490. if OKCancelBox(error_programexitedwithsignal,@P)=cmCancel then
  491. UserWantsToGoOn:=true;
  492. end
  493. else
  494. writeln('Signal error: ',ErrS);
  495. end;
  496. if ideapp.displaymode=dmUser then
  497. begin
  498. writeln('Fatal exception occurred while in user screen mode. File save message boxes');
  499. writeln('cannot be displayed. We are sorry, but need to terminate now.');
  500. halt(255);
  501. end;
  502. end;
  503. if (AutoSaveOptions and asEditorFiles)=0 then
  504. CanExit:=IDEApp.AskSaveAll
  505. else
  506. CanExit:=IDEApp.SaveAll;
  507. {$IFDEF HasSignal}
  508. StopJmpValid:=false;
  509. {$ENDIF}
  510. if (SetJmpRes<>0) then
  511. begin
  512. if (not CanExit) or UserWantsToGoOn then
  513. begin
  514. if ConfirmBox(continue_despite_error,nil,false)=cmNo then
  515. CanExit:=true
  516. else
  517. CanExit:=false;
  518. end
  519. else
  520. begin
  521. ErrorBox(leaving_after_error,nil);
  522. end;
  523. end;
  524. until CanExit;
  525. If ExitProc=pointer(@InterceptExit) then
  526. ExitProc:=StoreExitProc;
  527. IDEApp.AutoSave;
  528. DoneDesktopFile;
  529. DelTempFiles;
  530. IDEApp.Done;
  531. WriteSwitches(SwitchesPath);
  532. {$IFDEF HasSignal}
  533. DisableCatchSignals;
  534. {$ENDIF}
  535. DoneCodeComplete;
  536. DoneCodeTemplates;
  537. DoneTemplates;
  538. DoneTools;
  539. DoneUserScreen;
  540. DoneSwitches;
  541. DoneHelpFiles;
  542. DoneHelpFilesTypes;
  543. DoneReservedWords;
  544. DoneToolMessages;
  545. DoneBrowserCol;
  546. {$ifndef NODEBUG}
  547. DoneDebugger;
  548. DoneBreakpoints;
  549. DoneWatches;
  550. {$endif}
  551. {$ifdef unix}
  552. Video.ClearScreen;
  553. {$endif unix}
  554. { Video.DoneVideo;
  555. Keyboard.DoneKeyboard;}
  556. {$ifdef VESA}
  557. DoneVESAScreenModes;
  558. {$endif}
  559. {$if defined(unix)}
  560. Keyboard.RestoreStartMode;
  561. {$endif defined(unix)}
  562. {$if defined(windows)}
  563. SetConsoleMode(GetStdHandle(cardinal(Std_Input_Handle)),StartupConsoleMode);
  564. {$endif defined(windows)}
  565. {$ifdef go32v2}
  566. {$push}
  567. {$i-}
  568. ChDir(DirectoryInvokeFpFrom); {restore active directory we invoke fp from }
  569. ChDirRes:=IOResult;
  570. if (ChDirRes<>0) then
  571. writeln('Failed to restore start up directory ',DirectoryInvokeFpFrom,' error=',ChDirRes);
  572. {$pop}
  573. {$endif}
  574. StreamError:=nil;
  575. {$ifdef DEBUG}
  576. if CloseImmediately then
  577. writeln('Used time is ',getrealtime-StartTime:0:2);
  578. {$endif DEBUG}
  579. END.