[fpc-devel] CGI/Command line/GUI all in one

Gilles MARCOU g.marcou at chimie.u-strasbg.fr
Thu Sep 17 11:50:37 CEST 2009


Hello,

I'm trying to build an interface for a program so that it could be used
as a command line, a GUI or a CGI. For the CGI, I use powutils 1.7.1,
FPC 2.2.0 and Lazarus 0.9.24. I mean that the same executable should be
used either as a CGI, a command line or a GUI.

Currently I have kept separated in different unit each user interface
(see the code below). But, at execution, if there is an execution error,
it always open message box. Therefore, even in command line, the
software requires GUI libraries be there (GTK for instance) and it
simply does not work as a CGI.

Do you see a more clever solution? Otherwise, the only solution I see is
to keep all three interfaces in separate executable.

Thanks in advance,
Gilles Marcou

program Predictor;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  //{ you can add units after this },
  Classes, sysutils, UnitRunGUI, UnitRunCMD, UnitRunCGI;

type
    TRuntype=(CGI,CMD,GUI,UNK);

var
   cfg: TStringList;
   rt: TRuntype;
begin
  cfg:=TStringList.Create;
  cfg.LoadFromFile('cfg');
  rt:=UNK;
  if (cfg[0]='CGI') then
     rt:=CGI
  else if (cfg[0]='CMD') then
       rt:=CMD
  else if (cfg[0]='GUI') then
       rt:=GUI;
  FreeAndNil(cfg);
  if (rt=GUI) then
     RunGUI
  else if (rt=CMD) then
       RunCMD
  else if (rt=CGI) then
       RunCGI;
end.
-------
unit UnitRunGUI;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Interfaces, Forms, UnitPredictor;
  
procedure RunGUI;

implementation

procedure RunGUI;
begin
     Application.Initialize;
     Application.CreateForm(TFormPredictor, FormPredictor);
     Application.Run;
end;

end.
--------
unit UnitRunCGI;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, pwinit, pwmain;

procedure RunCGI;

implementation

procedure RunCGI;
begin
     outln('CGI: Not yet supported');
end;

end.
--------
unit UnitRunCMD;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils; 

procedure RunCMD;

implementation

procedure RunCMD;
begin
     writeln('CMD: Not supported yet');
end;

end.
 




More information about the fpc-devel mailing list