fp.pas 14 KB

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