fp.pas 14 KB

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