fp.pas 16 KB

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