fp.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 IncRes}
  13. {$ifdef Windows}
  14. {$R fpw32t.rc}
  15. {$R fpw32ico.rc}
  16. {$endif Windows}
  17. {$endif IncRes}
  18. {$I globdir.inc}
  19. (**********************************************************************)
  20. (* CONDITIONAL DEFINES *)
  21. (* - NODEBUG No Debugging support *)
  22. (* - TP Turbo Pascal mode *)
  23. (* - i386 Target is an i386 IDE *)
  24. (**********************************************************************)
  25. uses
  26. {$ifdef Windows}
  27. windows,
  28. {$endif Windows}
  29. {$ifndef NODEBUG}
  30. {$ifdef Windows}
  31. fpcygwin,
  32. {$endif Windows}
  33. {$endif NODEBUG}
  34. {$ifdef IDEHeapTrc}
  35. PPheap,
  36. {$endif IDEHeapTrc}
  37. {$ifdef Use_DBGHEAP}
  38. dbgheap,
  39. {$endif Use_DBGHEAP}
  40. {$ifdef go32v2}
  41. dpmiexcp,
  42. {$endif go32v2}
  43. {$ifdef fpc}
  44. keyboard,video,mouse,
  45. {$endif fpc}
  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;
  72. {$ifdef fpc}
  73. Const
  74. DummyMouseDriver : TMouseDriver = (
  75. useDefaultQueue : true;
  76. InitDriver : nil;
  77. DoneDriver : nil;
  78. DetectMouse : nil;
  79. ShowMouse : nil;
  80. HideMouse : nil;
  81. GetMouseX : nil;
  82. GetMouseY : nil;
  83. GetMouseButtons : nil;
  84. SetMouseXY : nil;
  85. GetMouseEvent : nil;
  86. PollMouseEvent : nil;
  87. PutMouseEvent : nil;
  88. );
  89. {$endif fpc}
  90. {$ifdef useresstrings}
  91. resourcestring
  92. {$else}
  93. const
  94. {$endif}
  95. { caught signals or abnormal exits }
  96. { Debugger messages and status hints }
  97. error_programexitedwitherror = #3'Program generated a RTE %d'#13+
  98. #3'at address $%s.'#13+
  99. #3'Save your sources and restart the IDE.';
  100. error_programexitedwithsignal = #3'Program generated a signal %d.'#13+
  101. #3'Save your sources and restart the IDE.';
  102. continue_despite_error = #3'The IDE generated an internal error'#13+
  103. #3'Do you really want to continue?'#13+
  104. #3'The IDE could be in an unstable state.';
  105. leaving_after_error = #3'The IDE generated an internal error'#13+
  106. #3'and will now be closed.';
  107. {$ifdef DEBUG}
  108. const
  109. CloseImmediately : boolean = false;
  110. var
  111. StartTime : real;
  112. function getrealtime : real;
  113. var
  114. h,m,s,s100 : word;
  115. begin
  116. gettime(h,m,s,s100);
  117. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  118. end;
  119. {$endif DEBUG}
  120. procedure ProcessParams(BeforeINI: boolean);
  121. function IsSwitch(const Param: string): boolean;
  122. begin
  123. IsSwitch:=(Param<>'') and (Param[1]<>DirSep) { <- allow UNIX root-relative paths }
  124. and (Param[1] in ['-','/']); { <- but still accept dos switch char, eg. '/' }
  125. end;
  126. var I: Sw_integer;
  127. Param: string;
  128. begin
  129. for I:=1 to ParamCount do
  130. begin
  131. Param:=System.ParamStr(I);
  132. if IsSwitch(Param) then
  133. begin
  134. Param:=copy(Param,2,255);
  135. if Param<>'' then
  136. if UpcaseStr(copy(Param,1,2))='HM' then
  137. { HeapMonitor }
  138. begin
  139. if (copy(Param,3,1)='+') or (copy(Param,3,1)='') then
  140. StartupOptions:=StartupOptions or soHeapMonitor
  141. else
  142. if (copy(Param,3,1)='-') then
  143. StartupOptions:=StartupOptions and not soHeapMonitor;
  144. end else
  145. {$ifdef go32v2}
  146. if UpcaseStr(Param)='NOLFN' then
  147. begin
  148. LFNSupport:=false;
  149. end else
  150. {$endif go32v2}
  151. if UpcaseStr(Param)='README' then
  152. begin
  153. ShowReadme:=true;
  154. end else
  155. case Upcase(Param[1]) of
  156. 'C' : { custom config file (BP compatiblity) }
  157. if BeforeINI then
  158. begin
  159. if (length(Param)>=1) and (Param[1] in['=',':']) then
  160. Delete(Param,1,1); { eat separator }
  161. IniFileName:=Param;
  162. end;
  163. 'R' : { enter the directory last exited from (BP comp.) }
  164. begin
  165. Param:=copy(Param,2,255);
  166. if (Param='') or (Param='+') then
  167. StartupOptions:=StartupOptions or soReturnToLastDir
  168. else
  169. if (Param='-') then
  170. StartupOptions:=StartupOptions and (not soReturnToLastDir);
  171. end;
  172. 'S' :
  173. if Length(Param)=1 then
  174. begin
  175. UseMouse:=false;
  176. {$ifdef fpc}
  177. DoneMouse;
  178. SetMouseDriver(DummyMouseDriver);
  179. {$endif fpc}
  180. ButtonCount:=0;
  181. end;
  182. {$ifdef fpc}
  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. {$endif fpc}
  191. {$ifdef DEBUG}
  192. 'Z' : UseOldBufStreamMethod:=true;
  193. 'X' : CloseImmediately:=true;
  194. {$endif DEBUG}
  195. end;
  196. end
  197. else
  198. if not BeforeINI then
  199. TryToOpenFile(nil,Param,0,0,{false}true);
  200. end;
  201. end;
  202. Procedure MyStreamError(Var S: TStream); {$ifndef FPC}far;{$endif}
  203. var ErrS: string;
  204. begin
  205. case S.Status of
  206. stGetError : ErrS:='Get of unregistered object type';
  207. stPutError : ErrS:='Put of unregistered object type';
  208. else ErrS:='';
  209. end;
  210. if ErrS<>'' then
  211. begin
  212. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  213. ErrorBox('Stream error: '+#13+ErrS,nil)
  214. else
  215. writeln('Error: ',ErrS);
  216. end;
  217. end;
  218. {$ifdef linux}
  219. procedure detect_debian;
  220. var attr:word;
  221. f:text;
  222. begin
  223. assign(f,'/etc/debian_version');
  224. getfattr(f,attr);
  225. if doserror=0 then
  226. errorbox('Debian system detected!'#13#13+
  227. 'Debian systems use an incompatible gpm'#13+
  228. 'daemon, therefore your system might'#13+
  229. 'suffer from Debian bug 412927. Please'#13+
  230. 'see http://bugs.debian.org/cgi-bin/'#13+
  231. 'bugreport.cgi?bug=412927 for details.',nil);
  232. end;
  233. {$endif}
  234. procedure DelTempFiles;
  235. begin
  236. DeleteFile(FPOutFileName);
  237. DeleteFile(FPErrFileName);
  238. DeleteFile(GDBOutFileName);
  239. DeleteFile(GDBOutPutFileName);
  240. DeleteFile(GREPOutName);
  241. DeleteFile(GREPErrName);
  242. end;
  243. procedure RegisterIDEObjects;
  244. begin
  245. RegisterApp;
  246. RegisterCodeComplete;
  247. RegisterCodeTemplates;
  248. {$ifdef COLORSEL}
  249. RegisterColorSel;
  250. {$endif COLORSEL}
  251. RegisterAsciiTab;
  252. RegisterDialogs;
  253. RegisterWEditor;
  254. RegisterWCEdit;
  255. RegisterFPCalc;
  256. RegisterFPCompile;
  257. RegisterFPTools;
  258. RegisterFPViews;
  259. {$ifndef NODEBUG}
  260. RegisterFPDebugViews;
  261. RegisterFPRegsViews;
  262. {$endif}
  263. RegisterMenus;
  264. RegisterStdDlg;
  265. RegisterSymbols;
  266. RegisterObjects;
  267. RegisterValidate;
  268. RegisterViews;
  269. RegisterWHTMLScan;
  270. RegisterWUtils;
  271. RegisterWViews;
  272. end;
  273. var CanExit : boolean;
  274. SetJmpRes : longint;
  275. StoreExitProc : pointer;
  276. ErrS : String;
  277. P : record
  278. l1 : longint;
  279. s : pstring;
  280. end;
  281. const
  282. ExitIntercepted : boolean = false;
  283. SeenExitCode : longint =0;
  284. SeenErrorAddr : pointer = nil;
  285. UserWantsToGoOn: boolean = false;
  286. procedure InterceptExit;
  287. begin
  288. {$IFDEF HasSignal}
  289. if StopJmpValid then
  290. begin
  291. ExitIntercepted:=true;
  292. SeenExitCode:=ExitCode;
  293. SeenErrorAddr:=ErrorAddr;
  294. LongJmp(StopJmp,1);
  295. end;
  296. {$ENDIF}
  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+' GBD 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. {$ifdef VESA}
  333. InitVESAScreenModes;
  334. {$endif}
  335. InitRedir;
  336. {$ifndef NODEBUG}
  337. InitBreakpoints;
  338. InitWatches;
  339. {$endif}
  340. InitReservedWords;
  341. InitHelpFiles;
  342. InitSwitches;
  343. InitINIFile;
  344. InitUserScreen;
  345. InitTools;
  346. InitTemplates;
  347. InitCodeTemplates;
  348. InitCodeComplete;
  349. { init target information etc. }
  350. InitSystems;
  351. IDEApp.Init;
  352. CheckINIFile;
  353. ReadSwitches(SwitchesPath);
  354. { load all options after init because of open files }
  355. ReadINIFile;
  356. InitDesktopFile;
  357. LoadDesktop;
  358. {Menubar might be changed because of loading INI file.}
  359. IDEapp.reload_menubar;
  360. { Handle Standard Units }
  361. if UseAllUnitsInCodeComplete then
  362. AddAvailableUnitsToCodeComplete(false);
  363. if UseStandardUnitsInCodeComplete and not assigned(UnitsCodeCompleteWords) then
  364. AddStandardUnitsToCodeComplete;
  365. { why are the screen contents parsed at startup? Gabor
  366. to be able to find location of error in last compilation
  367. from command line PM }
  368. ParseUserScreen;
  369. { Update IDE }
  370. IDEApp.Update;
  371. IDEApp.UpdateMode;
  372. IDEApp.UpdateTarget;
  373. ProcessParams(false);
  374. if ShowReadme then
  375. begin
  376. {$ifdef linux}
  377. {Regrettably we do not have a proper solution.}
  378. detect_debian;
  379. {$endif}
  380. PutCommand(Application,evCommand,cmShowReadme,nil);
  381. ShowReadme:=false; { do not show next time }
  382. end;
  383. StoreExitProc:=ExitProc;
  384. ExitProc:=@InterceptExit;
  385. repeat
  386. {$IFDEF HasSignal}
  387. SetJmpRes:=setjmp(StopJmp);
  388. StopJmpValid:=true;
  389. {$ENDIF}
  390. UserWantsToGoOn:=false;
  391. if SetJmpRes=0 then
  392. begin
  393. {$ifdef DEBUG}
  394. if not CloseImmediately then
  395. {$endif DEBUG}
  396. IDEApp.Run;
  397. end
  398. else
  399. begin
  400. if (SetJmpRes=1) and ExitIntercepted then
  401. begin
  402. { If ExitProc=@InterceptExit then
  403. ExitProc:=StoreExitProc;}
  404. Str(SeenExitCode,ErrS);
  405. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  406. begin
  407. P.l1:=SeenExitCode;
  408. ErrS:=hexstr(longint(SeenErrorAddr),8);
  409. P.s:=@ErrS;
  410. if OKCancelBox(error_programexitedwitherror,@P)=cmCancel then
  411. UserWantsToGoOn:=true;
  412. end
  413. else
  414. writeln('Abnormal exit error: ',ErrS);
  415. end
  416. else
  417. begin
  418. Str(SetJmpRes,ErrS);
  419. { Longjmp was called by fpcatch }
  420. if (application<>nil) and (ideapp.displaymode=dmIDE) then
  421. begin
  422. P.l1:=SetJmpRes;
  423. if OKCancelBox(error_programexitedwithsignal,@P)=cmCancel then
  424. UserWantsToGoOn:=true;
  425. end
  426. else
  427. writeln('Signal error: ',ErrS);
  428. end;
  429. if ideapp.displaymode=dmUser then
  430. begin
  431. writeln('Fatal exception occured while in user screen mode. File save message boxes');
  432. writeln('cannot be displayed. We are sorry, but need to terminate now.');
  433. halt(255);
  434. end;
  435. end;
  436. if (AutoSaveOptions and asEditorFiles)=0 then
  437. CanExit:=IDEApp.AskSaveAll
  438. else
  439. CanExit:=IDEApp.SaveAll;
  440. {$IFDEF HasSignal}
  441. StopJmpValid:=false;
  442. {$ENDIF}
  443. if (SetJmpRes<>0) then
  444. begin
  445. if (not CanExit) or UserWantsToGoOn then
  446. begin
  447. if ConfirmBox(continue_despite_error,nil,false)=cmNo then
  448. CanExit:=true
  449. else
  450. CanExit:=false;
  451. end
  452. else
  453. begin
  454. ErrorBox(leaving_after_error,nil);
  455. end;
  456. end;
  457. until CanExit;
  458. If ExitProc=pointer(@InterceptExit) then
  459. ExitProc:=StoreExitProc;
  460. IDEApp.AutoSave;
  461. DoneDesktopFile;
  462. DelTempFiles;
  463. IDEApp.Done;
  464. WriteSwitches(SwitchesPath);
  465. {$IFDEF HasSignal}
  466. DisableCatchSignals;
  467. {$ENDIF}
  468. DoneCodeComplete;
  469. DoneCodeTemplates;
  470. DoneTemplates;
  471. DoneTools;
  472. DoneUserScreen;
  473. DoneSwitches;
  474. DoneHelpFiles;
  475. DoneHelpFilesTypes;
  476. DoneReservedWords;
  477. DoneToolMessages;
  478. DoneBrowserCol;
  479. {$ifndef NODEBUG}
  480. DoneDebugger;
  481. DoneBreakpoints;
  482. DoneWatches;
  483. {$endif}
  484. {$ifdef fpc}
  485. {$ifdef unix}
  486. Video.ClearScreen;
  487. {$endif unix}
  488. { Video.DoneVideo;
  489. Keyboard.DoneKeyboard;}
  490. {$endif fpc}
  491. {$ifdef VESA}
  492. DoneVESAScreenModes;
  493. {$endif}
  494. {$if defined(unix)}
  495. Keyboard.RestoreStartMode;
  496. {$endif defined(unix)}
  497. {$if defined(windows)}
  498. SetConsoleMode(GetStdHandle(cardinal(Std_Input_Handle)),StartupConsoleMode);
  499. {$endif defined(windows)}
  500. StreamError:=nil;
  501. {$ifdef DEBUG}
  502. if CloseImmediately then
  503. writeln('Used time is ',getrealtime-StartTime:0:2);
  504. {$endif DEBUG}
  505. END.