fp.pas 15 KB

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