fp.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. if (length(Param)>=1) and (Param[1] in['=',':']) then
  162. Delete(Param,1,1); { eat separator }
  163. IniFileName:=Param;
  164. end;
  165. 'R' : { enter the directory last exited from (BP comp.) }
  166. begin
  167. Param:=copy(Param,2,255);
  168. if (Param='') or (Param='+') then
  169. StartupOptions:=StartupOptions or soReturnToLastDir
  170. else
  171. if (Param='-') then
  172. StartupOptions:=StartupOptions and (not soReturnToLastDir);
  173. end;
  174. 'S' :
  175. if Length(Param)=1 then
  176. begin
  177. UseMouse:=false;
  178. DoneMouse;
  179. SetMouseDriver(DummyMouseDriver);
  180. ButtonCount:=0;
  181. end;
  182. { 'F' :
  183. if Length(Param)=1 then
  184. NoExtendedFrame:=true;}
  185. {$ifdef Unix}
  186. 'T' : DebuggeeTTY:=Copy(Param,2,High(Param));
  187. {$endif Unix}
  188. { 'M' : TryToMaximizeScreen:=true;}
  189. {$ifdef DEBUG}
  190. 'Z' : UseOldBufStreamMethod:=true;
  191. 'X' : CloseImmediately:=true;
  192. {$endif DEBUG}
  193. end;
  194. end
  195. else
  196. if not BeforeINI then
  197. TryToOpenFileMulti(nil,Param,0,0,{false}true);
  198. end;
  199. end;
  200. Procedure MyStreamError(Var S: TStream);
  201. var ErrS: string;
  202. begin
  203. case S.Status of
  204. stGetError : ErrS:='Get of unregistered object type';
  205. stPutError : ErrS:='Put of unregistered object type';
  206. else ErrS:='';
  207. end;
  208. if ErrS<>'' then
  209. begin
  210. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  211. ErrorBox('Stream error: '+#13+ErrS,nil)
  212. else
  213. writeln('Error: ',ErrS);
  214. end;
  215. end;
  216. {$ifdef linux}
  217. procedure detect_debian;
  218. var attr:word;
  219. f:text;
  220. begin
  221. assign(f,'/etc/debian_version');
  222. getfattr(f,attr);
  223. if doserror=0 then
  224. errorbox('Debian system detected!'#13#13+
  225. 'Debian systems use an incompatible gpm'#13+
  226. 'daemon, therefore your system might'#13+
  227. 'suffer from Debian bug 412927. Please'#13+
  228. 'see http://bugs.debian.org/cgi-bin/'#13+
  229. 'bugreport.cgi?bug=412927 for details.',nil);
  230. end;
  231. {$endif}
  232. procedure DelTempFiles;
  233. begin
  234. DeleteFile(FPOutFileName);
  235. DeleteFile(FPErrFileName);
  236. DeleteFile(GDBOutFileName);
  237. DeleteFile(GDBOutPutFileName);
  238. DeleteFile(GREPOutName);
  239. DeleteFile(GREPErrName);
  240. end;
  241. procedure RegisterIDEObjects;
  242. begin
  243. RegisterApp;
  244. RegisterCodeComplete;
  245. RegisterCodeTemplates;
  246. {$ifdef COLORSEL}
  247. RegisterColorSel;
  248. {$endif COLORSEL}
  249. RegisterAsciiTab;
  250. RegisterDialogs;
  251. RegisterWEditor;
  252. RegisterWCEdit;
  253. RegisterFPCalc;
  254. RegisterFPCompile;
  255. RegisterFPTools;
  256. RegisterFPViews;
  257. {$ifndef NODEBUG}
  258. RegisterFPDebugViews;
  259. RegisterFPRegsViews;
  260. {$endif}
  261. RegisterMenus;
  262. RegisterStdDlg;
  263. RegisterSymbols;
  264. RegisterObjects;
  265. RegisterValidate;
  266. RegisterViews;
  267. RegisterWHTMLScan;
  268. RegisterWUtils;
  269. RegisterWViews;
  270. end;
  271. var CanExit : boolean;
  272. SetJmpRes : longint;
  273. StoreExitProc : pointer;
  274. ErrS : String;
  275. P : record
  276. l1 : longint;
  277. s : pstring;
  278. end;
  279. const
  280. ExitIntercepted : boolean = false;
  281. SeenExitCode : longint =0;
  282. SeenErrorAddr : pointer = nil;
  283. UserWantsToGoOn: boolean = false;
  284. procedure InterceptExit;
  285. begin
  286. {$IFDEF HasSignal}
  287. if StopJmpValid then
  288. begin
  289. ExitIntercepted:=true;
  290. SeenExitCode:=ExitCode;
  291. SeenErrorAddr:=ErrorAddr;
  292. LongJmp(StopJmp,1);
  293. end;
  294. {$ENDIF}
  295. end;
  296. procedure InitCompilerSwitches;
  297. begin
  298. default_settings.globalswitches:=[cs_check_unit_name];
  299. default_settings.moduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  300. default_settings.localswitches:=[cs_typed_const_writable];
  301. end;
  302. {The square bullet needs an MS-DOS code page. On Unix it is for sure the code
  303. page is not available before video is initialized. (And only in certain
  304. circumstances after that, so, use a plain ascii character as bullet on Unix.)}
  305. {$if defined(unix) or defined(amiga) or defined(morphos)}
  306. const bullet='*';
  307. {$else}
  308. const bullet='þ';
  309. {$endif}
  310. BEGIN
  311. {$IFDEF HasSignal}
  312. EnableCatchSignals;
  313. {$ENDIF}
  314. {$ifdef DEV}
  315. HeapLimit:=4096;
  316. {$endif}
  317. HistorySize:=16384;
  318. { Startup info }
  319. writeln(bullet+' Free Pascal IDE Version '+VersionStr+' ['+{$i %date%}+']');
  320. writeln(bullet+' Compiler Version '+Full_Version_String);
  321. {$ifndef NODEBUG}
  322. writeln(bullet+' GDB Version '+GDBVersion);
  323. {$ifdef Windows}
  324. {$ifndef USE_MINGW_GDB}
  325. writeln(bullet+' Cygwin "',GetCygwinFullName,'" version ',GetCygwinVersionString);
  326. CheckCygwinVersion;
  327. {$endif}
  328. {$endif Windows}
  329. {$endif NODEBUG}
  330. ProcessParams(true);
  331. {$ifdef DEBUG}
  332. StartTime:=getrealtime;
  333. {$endif DEBUG}
  334. InitDirs;
  335. RegisterIDEObjects;
  336. StreamError:=@MyStreamError;
  337. ShowReadme:=ShowReadme or (LocateFile(INIFileName)='');
  338. if LocateFile(INIFileName)<>'' then
  339. writeln(bullet+' Using configuration files from: ',DirOf(LocateFile(INIFileName)));
  340. InitCompilerSwitches;
  341. {$ifdef VESA}
  342. InitVESAScreenModes;
  343. {$endif}
  344. InitRedir;
  345. {$ifndef NODEBUG}
  346. InitBreakpoints;
  347. InitWatches;
  348. {$endif}
  349. InitReservedWords;
  350. InitHelpFiles;
  351. InitSwitches;
  352. InitINIFile;
  353. InitUserScreen;
  354. InitTools;
  355. InitTemplates;
  356. InitCodeTemplates;
  357. InitCodeComplete;
  358. { init target information etc. }
  359. InitSystems;
  360. IDEApp.Init;
  361. CheckINIFile;
  362. ReadSwitches(SwitchesPath);
  363. { load all options after init because of open files }
  364. ReadINIFile;
  365. InitDesktopFile;
  366. LoadDesktop;
  367. {Menubar might be changed because of loading INI file.}
  368. IDEapp.reload_menubar;
  369. { Handle Standard Units }
  370. if UseAllUnitsInCodeComplete then
  371. AddAvailableUnitsToCodeComplete(false);
  372. if UseStandardUnitsInCodeComplete and not assigned(UnitsCodeCompleteWords) then
  373. AddStandardUnitsToCodeComplete;
  374. { why are the screen contents parsed at startup? Gabor
  375. to be able to find location of error in last compilation
  376. from command line PM }
  377. ParseUserScreen;
  378. { Update IDE }
  379. IDEApp.Update;
  380. IDEApp.UpdateMode;
  381. IDEApp.UpdateTarget;
  382. ProcessParams(false);
  383. if ShowReadme then
  384. begin
  385. {$ifdef linux}
  386. {Regrettably we do not have a proper solution.}
  387. detect_debian;
  388. {$endif}
  389. PutCommand(Application,evCommand,cmShowReadme,nil);
  390. ShowReadme:=false; { do not show next time }
  391. end;
  392. StoreExitProc:=ExitProc;
  393. ExitProc:=@InterceptExit;
  394. repeat
  395. {$IFDEF HasSignal}
  396. SetJmpRes:=setjmp(StopJmp);
  397. StopJmpValid:=true;
  398. {$ENDIF}
  399. UserWantsToGoOn:=false;
  400. if SetJmpRes=0 then
  401. begin
  402. {$ifdef DEBUG}
  403. if not CloseImmediately then
  404. {$endif DEBUG}
  405. IDEApp.Run;
  406. end
  407. else
  408. begin
  409. if (SetJmpRes=1) and ExitIntercepted then
  410. begin
  411. { If ExitProc=@InterceptExit then
  412. ExitProc:=StoreExitProc;}
  413. Str(SeenExitCode,ErrS);
  414. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  415. begin
  416. P.l1:=SeenExitCode;
  417. ErrS:=hexstr(PtrUInt(SeenErrorAddr),sizeof(PtrUInt)*2);
  418. P.s:=@ErrS;
  419. if OKCancelBox(error_programexitedwitherror,@P)=cmCancel then
  420. UserWantsToGoOn:=true;
  421. end
  422. else
  423. writeln('Abnormal exit error: ',ErrS);
  424. end
  425. else
  426. begin
  427. Str(SetJmpRes,ErrS);
  428. { Longjmp was called by fpcatch }
  429. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  430. begin
  431. P.l1:=SetJmpRes;
  432. if OKCancelBox(error_programexitedwithsignal,@P)=cmCancel then
  433. UserWantsToGoOn:=true;
  434. end
  435. else
  436. writeln('Signal error: ',ErrS);
  437. end;
  438. if ideapp.displaymode=dmUser then
  439. begin
  440. writeln('Fatal exception occured while in user screen mode. File save message boxes');
  441. writeln('cannot be displayed. We are sorry, but need to terminate now.');
  442. halt(255);
  443. end;
  444. end;
  445. if (AutoSaveOptions and asEditorFiles)=0 then
  446. CanExit:=IDEApp.AskSaveAll
  447. else
  448. CanExit:=IDEApp.SaveAll;
  449. {$IFDEF HasSignal}
  450. StopJmpValid:=false;
  451. {$ENDIF}
  452. if (SetJmpRes<>0) then
  453. begin
  454. if (not CanExit) or UserWantsToGoOn then
  455. begin
  456. if ConfirmBox(continue_despite_error,nil,false)=cmNo then
  457. CanExit:=true
  458. else
  459. CanExit:=false;
  460. end
  461. else
  462. begin
  463. ErrorBox(leaving_after_error,nil);
  464. end;
  465. end;
  466. until CanExit;
  467. If ExitProc=pointer(@InterceptExit) then
  468. ExitProc:=StoreExitProc;
  469. IDEApp.AutoSave;
  470. DoneDesktopFile;
  471. DelTempFiles;
  472. IDEApp.Done;
  473. WriteSwitches(SwitchesPath);
  474. {$IFDEF HasSignal}
  475. DisableCatchSignals;
  476. {$ENDIF}
  477. DoneCodeComplete;
  478. DoneCodeTemplates;
  479. DoneTemplates;
  480. DoneTools;
  481. DoneUserScreen;
  482. DoneSwitches;
  483. DoneHelpFiles;
  484. DoneHelpFilesTypes;
  485. DoneReservedWords;
  486. DoneToolMessages;
  487. DoneBrowserCol;
  488. {$ifndef NODEBUG}
  489. DoneDebugger;
  490. DoneBreakpoints;
  491. DoneWatches;
  492. {$endif}
  493. {$ifdef unix}
  494. Video.ClearScreen;
  495. {$endif unix}
  496. { Video.DoneVideo;
  497. Keyboard.DoneKeyboard;}
  498. {$ifdef VESA}
  499. DoneVESAScreenModes;
  500. {$endif}
  501. {$if defined(unix)}
  502. Keyboard.RestoreStartMode;
  503. {$endif defined(unix)}
  504. {$if defined(windows)}
  505. SetConsoleMode(GetStdHandle(cardinal(Std_Input_Handle)),StartupConsoleMode);
  506. {$endif defined(windows)}
  507. StreamError:=nil;
  508. {$ifdef DEBUG}
  509. if CloseImmediately then
  510. writeln('Used time is ',getrealtime-StartTime:0:2);
  511. {$endif DEBUG}
  512. END.