fp.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. {The square bullet needs an MS-DOS code page. On Unix it is for sure the code
  302. page is not available before video is initialized. (And only in certain
  303. circumstances after that, so, use a plain ascii character as bullet on Unix.)}
  304. {$if defined(unix) or defined(amiga) or defined(morphos)}
  305. const bullet='*';
  306. {$else}
  307. const bullet='þ';
  308. {$endif}
  309. BEGIN
  310. {$IFDEF HasSignal}
  311. EnableCatchSignals;
  312. {$ENDIF}
  313. {$ifdef DEV}
  314. HeapLimit:=4096;
  315. {$endif}
  316. HistorySize:=16384;
  317. { Startup info }
  318. writeln(bullet+' Free Pascal IDE Version '+VersionStr+' ['+{$i %date%}+']');
  319. writeln(bullet+' Compiler Version '+Full_Version_String);
  320. {$ifndef NODEBUG}
  321. writeln(bullet+' GDB Version '+GDBVersion);
  322. {$ifdef Windows}
  323. {$ifndef USE_MINGW_GDB}
  324. writeln(bullet+' Cygwin "',GetCygwinFullName,'" version ',GetCygwinVersionString);
  325. CheckCygwinVersion;
  326. {$endif}
  327. {$endif Windows}
  328. {$endif NODEBUG}
  329. ProcessParams(true);
  330. {$ifdef DEBUG}
  331. StartTime:=getrealtime;
  332. {$endif DEBUG}
  333. InitDirs;
  334. RegisterIDEObjects;
  335. StreamError:=@MyStreamError;
  336. ShowReadme:=ShowReadme or (LocateFile(INIFileName)='');
  337. if LocateFile(INIFileName)<>'' then
  338. writeln(bullet+' Using configuration files from: ',DirOf(LocateFile(INIFileName)));
  339. InitCompilerSwitches;
  340. {$ifdef VESA}
  341. writeln(stderr,'If program stops, try again using -novesa option');
  342. flush(stderr);
  343. InitVESAScreenModes;
  344. {$endif}
  345. InitRedir;
  346. {$ifndef NODEBUG}
  347. InitBreakpoints;
  348. InitWatches;
  349. {$endif}
  350. InitReservedWords;
  351. InitHelpFiles;
  352. InitSwitches;
  353. InitINIFile;
  354. InitUserScreen;
  355. InitTools;
  356. InitTemplates;
  357. InitCodeTemplates;
  358. InitCodeComplete;
  359. { init target information etc. }
  360. InitSystems;
  361. IDEApp.Init;
  362. CheckINIFile;
  363. ReadSwitches(SwitchesPath);
  364. { load all options after init because of open files }
  365. ReadINIFile;
  366. InitDesktopFile;
  367. LoadDesktop;
  368. {Menubar might be changed because of loading INI file.}
  369. IDEapp.reload_menubar;
  370. { Handle Standard Units }
  371. if UseAllUnitsInCodeComplete then
  372. AddAvailableUnitsToCodeComplete(false);
  373. if UseStandardUnitsInCodeComplete and not assigned(UnitsCodeCompleteWords) then
  374. AddStandardUnitsToCodeComplete;
  375. { why are the screen contents parsed at startup? Gabor
  376. to be able to find location of error in last compilation
  377. from command line PM }
  378. ParseUserScreen;
  379. { Update IDE }
  380. IDEApp.Update;
  381. IDEApp.UpdateMode;
  382. IDEApp.UpdateTarget;
  383. ProcessParams(false);
  384. if ShowReadme then
  385. begin
  386. PutCommand(Application,evCommand,cmShowReadme,nil);
  387. ShowReadme:=false; { do not show next time }
  388. end;
  389. StoreExitProc:=ExitProc;
  390. ExitProc:=@InterceptExit;
  391. repeat
  392. {$IFDEF HasSignal}
  393. SetJmpRes:=setjmp(StopJmp);
  394. StopJmpValid:=true;
  395. {$ENDIF}
  396. UserWantsToGoOn:=false;
  397. if SetJmpRes=0 then
  398. begin
  399. {$ifdef DEBUG}
  400. if not CloseImmediately then
  401. {$endif DEBUG}
  402. IDEApp.Run;
  403. end
  404. else
  405. begin
  406. if (SetJmpRes=1) and ExitIntercepted then
  407. begin
  408. { If ExitProc=@InterceptExit then
  409. ExitProc:=StoreExitProc;}
  410. Str(SeenExitCode,ErrS);
  411. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  412. begin
  413. P.l1:=SeenExitCode;
  414. ErrS:=hexstr(PtrUInt(SeenErrorAddr),sizeof(PtrUInt)*2);
  415. P.s:=@ErrS;
  416. if OKCancelBox(error_programexitedwitherror,@P)=cmCancel then
  417. UserWantsToGoOn:=true;
  418. end
  419. else
  420. writeln('Abnormal exit error: ',ErrS);
  421. end
  422. else
  423. begin
  424. Str(SetJmpRes,ErrS);
  425. { Longjmp was called by fpcatch }
  426. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  427. begin
  428. P.l1:=SetJmpRes;
  429. if OKCancelBox(error_programexitedwithsignal,@P)=cmCancel then
  430. UserWantsToGoOn:=true;
  431. end
  432. else
  433. writeln('Signal error: ',ErrS);
  434. end;
  435. if ideapp.displaymode=dmUser then
  436. begin
  437. writeln('Fatal exception occured while in user screen mode. File save message boxes');
  438. writeln('cannot be displayed. We are sorry, but need to terminate now.');
  439. halt(255);
  440. end;
  441. end;
  442. if (AutoSaveOptions and asEditorFiles)=0 then
  443. CanExit:=IDEApp.AskSaveAll
  444. else
  445. CanExit:=IDEApp.SaveAll;
  446. {$IFDEF HasSignal}
  447. StopJmpValid:=false;
  448. {$ENDIF}
  449. if (SetJmpRes<>0) then
  450. begin
  451. if (not CanExit) or UserWantsToGoOn then
  452. begin
  453. if ConfirmBox(continue_despite_error,nil,false)=cmNo then
  454. CanExit:=true
  455. else
  456. CanExit:=false;
  457. end
  458. else
  459. begin
  460. ErrorBox(leaving_after_error,nil);
  461. end;
  462. end;
  463. until CanExit;
  464. If ExitProc=pointer(@InterceptExit) then
  465. ExitProc:=StoreExitProc;
  466. IDEApp.AutoSave;
  467. DoneDesktopFile;
  468. DelTempFiles;
  469. IDEApp.Done;
  470. WriteSwitches(SwitchesPath);
  471. {$IFDEF HasSignal}
  472. DisableCatchSignals;
  473. {$ENDIF}
  474. DoneCodeComplete;
  475. DoneCodeTemplates;
  476. DoneTemplates;
  477. DoneTools;
  478. DoneUserScreen;
  479. DoneSwitches;
  480. DoneHelpFiles;
  481. DoneHelpFilesTypes;
  482. DoneReservedWords;
  483. DoneToolMessages;
  484. DoneBrowserCol;
  485. {$ifndef NODEBUG}
  486. DoneDebugger;
  487. DoneBreakpoints;
  488. DoneWatches;
  489. {$endif}
  490. {$ifdef unix}
  491. Video.ClearScreen;
  492. {$endif unix}
  493. { Video.DoneVideo;
  494. Keyboard.DoneKeyboard;}
  495. {$ifdef VESA}
  496. DoneVESAScreenModes;
  497. {$endif}
  498. {$if defined(unix)}
  499. Keyboard.RestoreStartMode;
  500. {$endif defined(unix)}
  501. {$if defined(windows)}
  502. SetConsoleMode(GetStdHandle(cardinal(Std_Input_Handle)),StartupConsoleMode);
  503. {$endif defined(windows)}
  504. StreamError:=nil;
  505. {$ifdef DEBUG}
  506. if CloseImmediately then
  507. writeln('Used time is ',getrealtime-StartTime:0:2);
  508. {$endif DEBUG}
  509. END.