2
0

fp.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 char, 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. BEGIN
  336. {$IFDEF HasSignal}
  337. EnableCatchSignals;
  338. {$ENDIF}
  339. {$ifdef DEV}
  340. HeapLimit:=4096;
  341. {$endif}
  342. HistorySize:=16384;
  343. { Startup info }
  344. writeln(bullet+' Free Pascal IDE Version '+VersionStr+' ['+{$i %date%}+']');
  345. writeln(bullet+' Compiler Version '+Full_Version_String);
  346. { Process params before printing GDB version because of /G option }
  347. ProcessParams(true);
  348. {$ifndef NODEBUG}
  349. writeln(bullet+' GDB Version '+GDBVersion);
  350. {$ifdef Windows}
  351. {$ifndef USE_MINGW_GDB}
  352. {$ifdef GDBMI}
  353. { No reason to talk about cygwin DLL if we don't use it }
  354. if using_cygwin_gdb then
  355. {$endif GDBMI}
  356. begin
  357. writeln(bullet+' Cygwin "',GetCygwinFullName,'" version ',GetCygwinVersionString);
  358. CheckCygwinVersion;
  359. end;
  360. {$endif}
  361. {$endif Windows}
  362. {$endif NODEBUG}
  363. {$ifdef DEBUG}
  364. StartTime:=getrealtime;
  365. {$endif DEBUG}
  366. InitDirs;
  367. RegisterIDEObjects;
  368. StreamError:=@MyStreamError;
  369. ShowReadme:=ShowReadme or (LocateFile(INIFileName)='');
  370. if LocateFile(INIFileName)<>'' then
  371. writeln(bullet+' Using configuration files from: ',DirOf(LocateFile(INIFileName)));
  372. InitCompilerSwitches;
  373. {$ifdef VESA}
  374. writeln(stderr,'If program stops, try again using -novesa option');
  375. flush(stderr);
  376. InitVESAScreenModes;
  377. {$endif}
  378. InitRedir;
  379. {$ifndef NODEBUG}
  380. InitBreakpoints;
  381. InitWatches;
  382. {$endif}
  383. InitReservedWords;
  384. InitHelpFiles;
  385. InitSwitches;
  386. InitINIFile;
  387. InitUserScreen;
  388. InitTools;
  389. InitTemplates;
  390. InitCodeTemplates;
  391. InitCodeComplete;
  392. { init target information etc. }
  393. InitSystems;
  394. IDEApp.Init;
  395. CheckINIFile;
  396. ReadSwitches(SwitchesPath);
  397. { load all options after init because of open files }
  398. ReadINIFile;
  399. InitDesktopFile;
  400. LoadDesktop;
  401. {Menubar might be changed because of loading INI file.}
  402. IDEapp.reload_menubar;
  403. { Handle Standard Units }
  404. if UseAllUnitsInCodeComplete then
  405. AddAvailableUnitsToCodeComplete(false);
  406. if UseStandardUnitsInCodeComplete and not assigned(UnitsCodeCompleteWords) then
  407. AddStandardUnitsToCodeComplete;
  408. { why are the screen contents parsed at startup? Gabor
  409. to be able to find location of error in last compilation
  410. from command line PM }
  411. ParseUserScreen;
  412. {$IFDEF HASAMIGA}
  413. SetAmigaWindowTitle;
  414. {$ENDIF}
  415. { Update IDE }
  416. IDEApp.Update;
  417. IDEApp.UpdateMode;
  418. IDEApp.UpdateTarget;
  419. ProcessParams(false);
  420. if ShowReadme then
  421. begin
  422. PutCommand(Application,evCommand,cmShowReadme,nil);
  423. ShowReadme:=false; { do not show next time }
  424. end;
  425. StoreExitProc:=ExitProc;
  426. ExitProc:=@InterceptExit;
  427. repeat
  428. {$IFDEF HasSignal}
  429. SetJmpRes:=setjmp(StopJmp);
  430. StopJmpValid:=true;
  431. {$ENDIF}
  432. UserWantsToGoOn:=false;
  433. if SetJmpRes=0 then
  434. begin
  435. {$ifdef DEBUG}
  436. if not CloseImmediately then
  437. {$endif DEBUG}
  438. IDEApp.Run;
  439. end
  440. else
  441. begin
  442. if (SetJmpRes=1) and ExitIntercepted then
  443. begin
  444. { If ExitProc=@InterceptExit then
  445. ExitProc:=StoreExitProc;}
  446. Str(SeenExitCode,ErrS);
  447. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  448. begin
  449. P.l1:=SeenExitCode;
  450. ErrS:=hexstr(PtrUInt(SeenErrorAddr),sizeof(PtrUInt)*2);
  451. P.s:=@ErrS;
  452. if OKCancelBox(error_programexitedwitherror,@P)=cmCancel then
  453. UserWantsToGoOn:=true;
  454. end
  455. else
  456. writeln('Abnormal exit error: ',ErrS);
  457. end
  458. else
  459. begin
  460. Str(SetJmpRes,ErrS);
  461. { Longjmp was called by fpcatch }
  462. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  463. begin
  464. P.l1:=SetJmpRes;
  465. if OKCancelBox(error_programexitedwithsignal,@P)=cmCancel then
  466. UserWantsToGoOn:=true;
  467. end
  468. else
  469. writeln('Signal error: ',ErrS);
  470. end;
  471. if ideapp.displaymode=dmUser then
  472. begin
  473. writeln('Fatal exception occurred while in user screen mode. File save message boxes');
  474. writeln('cannot be displayed. We are sorry, but need to terminate now.');
  475. halt(255);
  476. end;
  477. end;
  478. if (AutoSaveOptions and asEditorFiles)=0 then
  479. CanExit:=IDEApp.AskSaveAll
  480. else
  481. CanExit:=IDEApp.SaveAll;
  482. {$IFDEF HasSignal}
  483. StopJmpValid:=false;
  484. {$ENDIF}
  485. if (SetJmpRes<>0) then
  486. begin
  487. if (not CanExit) or UserWantsToGoOn then
  488. begin
  489. if ConfirmBox(continue_despite_error,nil,false)=cmNo then
  490. CanExit:=true
  491. else
  492. CanExit:=false;
  493. end
  494. else
  495. begin
  496. ErrorBox(leaving_after_error,nil);
  497. end;
  498. end;
  499. until CanExit;
  500. If ExitProc=pointer(@InterceptExit) then
  501. ExitProc:=StoreExitProc;
  502. IDEApp.AutoSave;
  503. DoneDesktopFile;
  504. DelTempFiles;
  505. IDEApp.Done;
  506. WriteSwitches(SwitchesPath);
  507. {$IFDEF HasSignal}
  508. DisableCatchSignals;
  509. {$ENDIF}
  510. DoneCodeComplete;
  511. DoneCodeTemplates;
  512. DoneTemplates;
  513. DoneTools;
  514. DoneUserScreen;
  515. DoneSwitches;
  516. DoneHelpFiles;
  517. DoneHelpFilesTypes;
  518. DoneReservedWords;
  519. DoneToolMessages;
  520. DoneBrowserCol;
  521. {$ifndef NODEBUG}
  522. DoneDebugger;
  523. DoneBreakpoints;
  524. DoneWatches;
  525. {$endif}
  526. {$ifdef unix}
  527. Video.ClearScreen;
  528. {$endif unix}
  529. { Video.DoneVideo;
  530. Keyboard.DoneKeyboard;}
  531. {$ifdef VESA}
  532. DoneVESAScreenModes;
  533. {$endif}
  534. {$if defined(unix)}
  535. Keyboard.RestoreStartMode;
  536. {$endif defined(unix)}
  537. {$if defined(windows)}
  538. SetConsoleMode(GetStdHandle(cardinal(Std_Input_Handle)),StartupConsoleMode);
  539. {$endif defined(windows)}
  540. StreamError:=nil;
  541. {$ifdef DEBUG}
  542. if CloseImmediately then
  543. writeln('Used time is ',getrealtime-StartTime:0:2);
  544. {$endif DEBUG}
  545. END.