fp.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. end else
  157. {$ifdef go32v2}
  158. if UpcaseStr(Param)='NOLFN' then
  159. begin
  160. LFNSupport:=false;
  161. end else
  162. {$endif go32v2}
  163. {$ifdef VESA}
  164. if UpcaseStr(Param)='NOVESA' then
  165. begin
  166. disableVESA:=true;
  167. end else
  168. if UpcaseStr(Param)='VESA' then
  169. begin
  170. (* Force using VESA although it may have been disabled by default *)
  171. disableVESA:=false;
  172. end else
  173. {$endif VESA}
  174. if UpcaseStr(Param)='README' then
  175. begin
  176. ShowReadme:=true;
  177. end else
  178. case Upcase(Param[1]) of
  179. 'C' : { custom config file (BP compatiblity) }
  180. if BeforeINI then
  181. begin
  182. delete(param,1,1); // delete C
  183. if (length(Param)>=1) and (Param[1] in['=',':']) then
  184. Delete(Param,1,1); { eat optional separator }
  185. IniFileName:=Param;
  186. end;
  187. {$ifdef GDBMI}
  188. 'G' : { custom GDB exec file (GDBMI mode only) }
  189. if BeforeINI then
  190. begin
  191. delete(param,1,1); // delete C
  192. if (length(Param)>=1) and (Param[1] in['=',':']) then
  193. Delete(Param,1,1); { eat optional separator }
  194. GDBProgramName:=Param;
  195. end;
  196. {$endif def GDBMI}
  197. 'R' : { enter the directory last exited from (BP comp.) }
  198. begin
  199. Param:=copy(Param,2,255);
  200. if (Param='') or (Param='+') then
  201. StartupOptions:=StartupOptions or soReturnToLastDir
  202. else
  203. if (Param='-') then
  204. StartupOptions:=StartupOptions and (not soReturnToLastDir);
  205. end;
  206. 'S' :
  207. if Length(Param)=1 then
  208. begin
  209. UseMouse:=false;
  210. DoneMouse;
  211. SetMouseDriver(DummyMouseDriver);
  212. ButtonCount:=0;
  213. end;
  214. { 'F' :
  215. if Length(Param)=1 then
  216. NoExtendedFrame:=true;}
  217. {$ifdef Unix}
  218. 'T' : DebuggeeTTY:=Copy(Param,2,High(Param));
  219. {$endif Unix}
  220. { 'M' : TryToMaximizeScreen:=true;}
  221. {$ifdef DEBUG}
  222. 'Z' : UseOldBufStreamMethod:=true;
  223. 'X' : CloseImmediately:=true;
  224. {$endif DEBUG}
  225. end;
  226. end
  227. else
  228. if not BeforeINI then
  229. TryToOpenFileMulti(nil,Param,0,0,{false}true);
  230. end;
  231. end;
  232. Procedure MyStreamError(Var S: TStream);
  233. var ErrS: string;
  234. begin
  235. case S.Status of
  236. stGetError : ErrS:='Get of unregistered object type';
  237. stPutError : ErrS:='Put of unregistered object type';
  238. else ErrS:='';
  239. end;
  240. if ErrS<>'' then
  241. begin
  242. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  243. ErrorBox('Stream error: '+#13+ErrS,nil)
  244. else
  245. writeln('Error: ',ErrS);
  246. end;
  247. end;
  248. procedure DelTempFiles;
  249. begin
  250. DeleteFile(FPOutFileName);
  251. DeleteFile(FPErrFileName);
  252. DeleteFile(GDBOutFileName);
  253. DeleteFile(GDBOutPutFileName);
  254. DeleteFile(GREPOutName);
  255. DeleteFile(GREPErrName);
  256. end;
  257. procedure RegisterIDEObjects;
  258. begin
  259. RegisterApp;
  260. RegisterCodeComplete;
  261. RegisterCodeTemplates;
  262. {$ifdef COLORSEL}
  263. RegisterColorSel;
  264. {$endif COLORSEL}
  265. RegisterAsciiTab;
  266. RegisterDialogs;
  267. RegisterWEditor;
  268. RegisterWCEdit;
  269. RegisterFPCalc;
  270. RegisterFPCompile;
  271. RegisterFPTools;
  272. RegisterFPViews;
  273. {$ifndef NODEBUG}
  274. RegisterFPDebugViews;
  275. RegisterFPRegsViews;
  276. {$endif}
  277. RegisterMenus;
  278. RegisterStdDlg;
  279. RegisterSymbols;
  280. RegisterObjects;
  281. RegisterValidate;
  282. RegisterViews;
  283. RegisterWHTMLScan;
  284. RegisterWUtils;
  285. RegisterWViews;
  286. end;
  287. var CanExit : boolean;
  288. SetJmpRes : longint;
  289. StoreExitProc : pointer;
  290. ErrS : String;
  291. P : record
  292. l1 : longint;
  293. s : pstring;
  294. end;
  295. const
  296. ExitIntercepted : boolean = false;
  297. SeenExitCode : longint =0;
  298. SeenErrorAddr : pointer = nil;
  299. UserWantsToGoOn: boolean = false;
  300. procedure InterceptExit;
  301. begin
  302. {$IFDEF HasSignal}
  303. if StopJmpValid then
  304. begin
  305. ExitIntercepted:=true;
  306. SeenExitCode:=ExitCode;
  307. SeenErrorAddr:=ErrorAddr;
  308. LongJmp(StopJmp,1);
  309. end;
  310. {$ENDIF}
  311. end;
  312. procedure InitCompilerSwitches;
  313. begin
  314. default_settings.globalswitches:=[cs_check_unit_name];
  315. default_settings.moduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  316. default_settings.localswitches:=[cs_typed_const_writable];
  317. end;
  318. {$IFDEF HASAMIGA}
  319. procedure SetAmigaWindowTitle;
  320. begin
  321. { window title first, then screen title, shown when the window is active }
  322. Video.SetWindowTitle(
  323. 'Free Pascal IDE',
  324. 'Free Pascal IDE '+VersionStr+' ['+{$i %date%}+'] - Compiler '+Full_Version_String);
  325. end;
  326. {$ENDIF}
  327. {The square bullet needs an MS-DOS code page. On Unix it is for sure the code
  328. page is not available before video is initialized. (And only in certain
  329. circumstances after that, so, use a plain ascii character as bullet on Unix.)}
  330. {$if defined(unix) or defined(HASAMIGA)}
  331. const bullet='*';
  332. {$else}
  333. const bullet=#254;
  334. {$endif}
  335. var
  336. _GDBVersion: String;
  337. BEGIN
  338. {$IFDEF HasSignal}
  339. EnableCatchSignals;
  340. {$ENDIF}
  341. {$ifdef DEV}
  342. HeapLimit:=4096;
  343. {$endif}
  344. HistorySize:=16384;
  345. { Startup info }
  346. writeln(bullet+' Free Pascal IDE Version '+VersionStr+' ['+{$i %date%}+']');
  347. writeln(bullet+' Compiler Version '+Full_Version_String);
  348. { Process params before printing GDB version because of /G option }
  349. ProcessParams(true);
  350. {$ifndef NODEBUG}
  351. _GDBVersion:=GDBVersion;
  352. while pos(#13#3,_GDBVersion)<>0 do
  353. Delete(_GDBVersion,pos(#13#3,_GDBVersion),2);
  354. writeln(bullet+' GDB Version '+_GDBVersion);
  355. {$ifdef Windows}
  356. {$ifndef USE_MINGW_GDB}
  357. {$ifdef GDBMI}
  358. { No reason to talk about cygwin DLL if we don't use it }
  359. if using_cygwin_gdb then
  360. {$endif GDBMI}
  361. begin
  362. writeln(bullet+' Cygwin "',GetCygwinFullName,'" version ',GetCygwinVersionString);
  363. CheckCygwinVersion;
  364. end;
  365. {$endif}
  366. {$endif Windows}
  367. {$endif NODEBUG}
  368. {$ifdef DEBUG}
  369. StartTime:=getrealtime;
  370. {$endif DEBUG}
  371. InitDirs;
  372. RegisterIDEObjects;
  373. StreamError:=@MyStreamError;
  374. ShowReadme:=ShowReadme or (LocateFile(INIFileName)='');
  375. if LocateFile(INIFileName)<>'' then
  376. writeln(bullet+' Using configuration files from: ',DirOf(LocateFile(INIFileName)));
  377. InitCompilerSwitches;
  378. {$ifdef VESA}
  379. writeln(stderr,'If program stops, try again using -novesa option');
  380. flush(stderr);
  381. InitVESAScreenModes;
  382. {$endif}
  383. InitRedir;
  384. {$ifndef NODEBUG}
  385. InitBreakpoints;
  386. InitWatches;
  387. {$endif}
  388. InitReservedWords;
  389. InitHelpFiles;
  390. InitSwitches;
  391. InitINIFile;
  392. InitUserScreen;
  393. InitTools;
  394. InitTemplates;
  395. InitCodeTemplates;
  396. InitCodeComplete;
  397. { init target information etc. }
  398. InitSystems;
  399. IDEApp.Init;
  400. CheckINIFile;
  401. ReadSwitches(SwitchesPath);
  402. { load all options after init because of open files }
  403. ReadINIFile;
  404. InitDesktopFile;
  405. LoadDesktop;
  406. {Menubar might be changed because of loading INI file.}
  407. IDEapp.reload_menubar;
  408. { Handle Standard Units }
  409. if UseAllUnitsInCodeComplete then
  410. AddAvailableUnitsToCodeComplete(false);
  411. if UseStandardUnitsInCodeComplete and not assigned(UnitsCodeCompleteWords) then
  412. AddStandardUnitsToCodeComplete;
  413. { why are the screen contents parsed at startup? Gabor
  414. to be able to find location of error in last compilation
  415. from command line PM }
  416. ParseUserScreen;
  417. {$IFDEF HASAMIGA}
  418. SetAmigaWindowTitle;
  419. {$ENDIF}
  420. { Update IDE }
  421. IDEApp.Update;
  422. IDEApp.UpdateMode;
  423. IDEApp.UpdateTarget;
  424. ProcessParams(false);
  425. if ShowReadme then
  426. begin
  427. PutCommand(Application,evCommand,cmShowReadme,nil);
  428. ShowReadme:=false; { do not show next time }
  429. end;
  430. StoreExitProc:=ExitProc;
  431. ExitProc:=@InterceptExit;
  432. repeat
  433. {$IFDEF HasSignal}
  434. SetJmpRes:=setjmp(StopJmp);
  435. StopJmpValid:=true;
  436. {$ENDIF}
  437. UserWantsToGoOn:=false;
  438. if SetJmpRes=0 then
  439. begin
  440. {$ifdef DEBUG}
  441. if not CloseImmediately then
  442. {$endif DEBUG}
  443. IDEApp.Run;
  444. end
  445. else
  446. begin
  447. if (SetJmpRes=1) and ExitIntercepted then
  448. begin
  449. { If ExitProc=@InterceptExit then
  450. ExitProc:=StoreExitProc;}
  451. Str(SeenExitCode,ErrS);
  452. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  453. begin
  454. P.l1:=SeenExitCode;
  455. ErrS:=hexstr(PtrUInt(SeenErrorAddr),sizeof(PtrUInt)*2);
  456. P.s:=@ErrS;
  457. if OKCancelBox(error_programexitedwitherror,@P)=cmCancel then
  458. UserWantsToGoOn:=true;
  459. end
  460. else
  461. writeln('Abnormal exit error: ',ErrS);
  462. end
  463. else
  464. begin
  465. Str(SetJmpRes,ErrS);
  466. { Longjmp was called by fpcatch }
  467. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  468. begin
  469. P.l1:=SetJmpRes;
  470. if OKCancelBox(error_programexitedwithsignal,@P)=cmCancel then
  471. UserWantsToGoOn:=true;
  472. end
  473. else
  474. writeln('Signal error: ',ErrS);
  475. end;
  476. if ideapp.displaymode=dmUser then
  477. begin
  478. writeln('Fatal exception occurred while in user screen mode. File save message boxes');
  479. writeln('cannot be displayed. We are sorry, but need to terminate now.');
  480. halt(255);
  481. end;
  482. end;
  483. if (AutoSaveOptions and asEditorFiles)=0 then
  484. CanExit:=IDEApp.AskSaveAll
  485. else
  486. CanExit:=IDEApp.SaveAll;
  487. {$IFDEF HasSignal}
  488. StopJmpValid:=false;
  489. {$ENDIF}
  490. if (SetJmpRes<>0) then
  491. begin
  492. if (not CanExit) or UserWantsToGoOn then
  493. begin
  494. if ConfirmBox(continue_despite_error,nil,false)=cmNo then
  495. CanExit:=true
  496. else
  497. CanExit:=false;
  498. end
  499. else
  500. begin
  501. ErrorBox(leaving_after_error,nil);
  502. end;
  503. end;
  504. until CanExit;
  505. If ExitProc=pointer(@InterceptExit) then
  506. ExitProc:=StoreExitProc;
  507. IDEApp.AutoSave;
  508. DoneDesktopFile;
  509. DelTempFiles;
  510. IDEApp.Done;
  511. WriteSwitches(SwitchesPath);
  512. {$IFDEF HasSignal}
  513. DisableCatchSignals;
  514. {$ENDIF}
  515. DoneCodeComplete;
  516. DoneCodeTemplates;
  517. DoneTemplates;
  518. DoneTools;
  519. DoneUserScreen;
  520. DoneSwitches;
  521. DoneHelpFiles;
  522. DoneHelpFilesTypes;
  523. DoneReservedWords;
  524. DoneToolMessages;
  525. DoneBrowserCol;
  526. {$ifndef NODEBUG}
  527. DoneDebugger;
  528. DoneBreakpoints;
  529. DoneWatches;
  530. {$endif}
  531. {$ifdef unix}
  532. Video.ClearScreen;
  533. {$endif unix}
  534. { Video.DoneVideo;
  535. Keyboard.DoneKeyboard;}
  536. {$ifdef VESA}
  537. DoneVESAScreenModes;
  538. {$endif}
  539. {$if defined(unix)}
  540. Keyboard.RestoreStartMode;
  541. {$endif defined(unix)}
  542. {$if defined(windows)}
  543. SetConsoleMode(GetStdHandle(cardinal(Std_Input_Handle)),StartupConsoleMode);
  544. {$endif defined(windows)}
  545. StreamError:=nil;
  546. {$ifdef DEBUG}
  547. if CloseImmediately then
  548. writeln('Used time is ',getrealtime-StartTime:0:2);
  549. {$endif DEBUG}
  550. END.