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