fp.pas 14 KB

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