Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2009-09

CGI/Command line/GUI all in one

14 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Gilles MARCOU

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.

Reply by email

Graeme Geldenhuys
to Gilles MARCOU: I'm trying to build an interface for a program so that it could be used as a command…

Gilles MARCOU het geskryf:

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.

I can't see this working - I went through the same problem with the tiOPF project.

If you create a CGI application that pulls in the Forms unit for example. Then try and run that CGI project thourgh a web server that doesn't have X Window System installed, the program will fail with "DISPLAY has not been set" error.

In the case of tiOPF. I was trying to run the console Test Suite on a headless Linux box. I had to first clean up the tiOPF code, to NOT include GUI units in the core tiopf unit. Then only did my console application run correctly on the headless linux box.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email

Joost van der Sluis
to Gilles MARCOU: I'm trying to build an interface for a program so that it could be used as a command…
Gilles MARCOU wrote 12 lines
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.

If you really have a console-only program, you shoudn't use any lcl-units (that pulls in the forms unit) in your program. That'll solve the problem.

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

Or you can select the 'nogui' widgetset in Lazarus. That'll also solve your problem.

Joost

Reply by email

Dariusz Mazur
to Gilles MARCOU: I'm trying to build an interface for a program so that it could be used as a command…

Gilles MARCOU pisze:

Show quoted text 13 lines
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.

You have to change all automatic initialization of visual components and make all linking with GUI libraries dynamic. That i no easy. But you can invoke second executable from this console (distribute 2 file, but always invoke the same)

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

This is not problem to have separate executable, only have one source to both GUI.
I made program which can work as desktop and as web-based (FCGI or standalone web server)
Each form is create dynamical in runtime, source is one, but in compile time:
1 for desktop: clasic units from LCL (or rather VCL, on windows I use Delphi)
2.for web-base different set of units to handle web, but with the same API as VCL

Darek

Show quoted text 107 lines
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. _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel
Darek

Reply by email

Gilles MARCOU

Hi,

thank you for your very fast answers.

Joost, I had an application that worked as GUI (my boss likes GUI), then my colleagues asked me for a command line and my boss asked for a CGI... I resulted in 3 separate projects which is impossible to maintain -I'm not well enough organized. So I wanted to fuse all projects in one application which, according to a config file would run as a CGI, console application or GUI.

So, yes, if the application is console only, there is no problem. But it should, idealy, be running also as GUI. However, I did not understood what is the 'nogui' widgetset and how can I use it? It sounds promising! I think it is in alpha release in Lazarus 0.9.26, that's it?

Michael, the widgetset I'm using is GTK usually. I am using my computer with a local installation of Apache to test my CGI; in that case, the widgetset is obviously compatible. On the webserver, I recompiled from scratch, but it does not work neither. In both cases, I got an "Internal Server Error" message from Apache. In the log of Apache, I found that the CGI ended prematurely because GTK could open display.

I believe Graeme is right. As long as I do not trigger any GUI unit, the CGI works fine. Using the suggestion of Graeme, I ended up with a solution at compile time. I enclose in the main program three user interfaces with $DEFINE and I select the needed one with the option -d<x> of the compiler. Using the same units as in my previous post, I can fuse all my projets compiling the code with option -dGUImode for a GUI application, -dCMDmode for console application, -dCGImode for CGI application -see the code at the end of the post.

Yet it is not as satisfactory as deciding at compile time rather than deciding at run time. I'm open to all suggestions.

Thanks for your help,
Gilles Marcou

program Predictor;
{$mode objfpc}{$H+}
uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  //{ you can add units after this },
  Classes, sysutils,
  {$IFDEF GUImode}UnitRunGUI;{$ENDIF}
  {$IFDEF CMDmode}UnitRunCMD;{$ENDIF}
  {$IFDEF CGImode}UnitRunCGI;{$ENDIF}
begin
  {$IFDEF GUImode}RunGUI;{$ENDIF}
  {$IFDEF CMDmode}RunCMD;{$ENDIF}
  {$IFDEF CGImode}RunCGI;{$ENDIF}
end.

Reply by email

Horacio Jamilis
to Dariusz Mazur: Gilles MARCOU pisze: You have to change all automatic initialization of visual components…
Dariusz Mazur wrote 12 lines
This is not problem to have separate executable, only have one source to both GUI. I made program which can work as desktop and as web-based (FCGI or standalone web server) Each form is create dynamical in runtime, source is one, but in compile time: 1 for desktop: clasic units from LCL (or rather VCL, on windows I use Delphi) 2.for web-base different set of units to handle web, but with the same API as VCL Darek

How do you do this?
I want to do exactly the same... having 2 exe, one for desktop and one for web without doubling the work (from the same sources). I was thinking on using Intraweb (or VCL for the web in delphi)... but, what are you using for the web with FPC?
I want to move to FPC from delphi, but I see no viable alternative to Intraweb in FPC (and Lazarus).
Do you have any sample source I could see?

Thanks

Horacio

Reply by email

Dariusz Mazur
to Horacio Jamilis: How do you do this? I want to do exactly the same... having 2 exe, one for desktop and one

Horacio Jamilis pisze:

Show quoted text 18 lines
Dariusz Mazur escribió: > This is not problem to have separate executable, only have one source > to both GUI. > I made program which can work as desktop and as web-based (FCGI or > standalone web server) > Each form is create dynamical in runtime, source is one, but in > compile time: > 1 for desktop: clasic units from LCL (or rather VCL, on windows I use > Delphi) > 2.for web-base different set of units to handle web, but with the > same API as VCL > > Darek How do you do this? I want to do exactly the same... having 2 exe, one for desktop and one for web without doubling the work (from the same sources). I was thinking on using Intraweb (or VCL for the web in delphi)... but, what are you using for the web with FPC?

webserver from synapse, but with several modification (current is much faster)
own framework as I write upper,it contain replacement for units controls, forms, stdctrl of course only this part which I need, but its work

I want to move to FPC from delphi, but I see no viable alternative to Intraweb in FPC (and Lazarus).

Indy don't work properly on linux,
Intraweb is so complicated and very heavy

Do you have any sample source I could see?

http://www.emadar.com/fpc/xwebdemo.zip

its very simple demo with all libraries
but some text are in polish,
fell free to ask, i've try to translate or explain

Darek

Reply by email

Marco van de Voort
to Dariusz Mazur

In our previous episode, Dariusz Mazur said:

> I want to move to FPC from delphi, but I see no viable alternative to > Intraweb in FPC (and Lazarus). Indy don't work properly on linux,

What's the problem?

Reply by email

Dariusz Mazur
to Marco van de Voort

Marco van de Voort pisze:

Show quoted text 12 lines
In our previous episode, Dariusz Mazur said: >> I want to move to FPC from delphi, but I see no viable alternative to >> Intraweb in FPC (and Lazarus). >> > Indy don't work properly on linux, > What's the problem?

webserver was no stable,
maybe something was change, I swich to Synapse two years ago and i think Synapse is faster (I don't make any test, but only about look to source)

Darek

Reply by email

Horacio Jamilis
to Dariusz Mazur: Horacio Jamilis pisze: webserver from synapse, but with several modification (current is…
Dariusz Mazur wrote 41 lines
Horacio Jamilis pisze: > Dariusz Mazur escribió: >> This is not problem to have separate executable, only have one >> source to both GUI. >> I made program which can work as desktop and as web-based (FCGI or >> standalone web server) >> Each form is create dynamical in runtime, source is one, but in >> compile time: >> 1 for desktop: clasic units from LCL (or rather VCL, on windows I >> use Delphi) >> 2.for web-base different set of units to handle web, but with the >> same API as VCL >> >> Darek > How do you do this? > I want to do exactly the same... having 2 exe, one for desktop and > one for web without doubling the work (from the same sources). > I was thinking on using Intraweb (or VCL for the web in delphi)... > but, what are you using for the web with FPC? webserver from synapse, but with several modification (current is much faster) own framework as I write upper,it contain replacement for units controls, forms, stdctrl of course only this part which I need, but its work > I want to move to FPC from delphi, but I see no viable alternative to > Intraweb in FPC (and Lazarus). Indy don't work properly on linux, Intraweb is so complicated and very heavy > Do you have any sample source I could see? > http://www.emadar.com/fpc/xwebdemo.zip its very simple demo with all libraries but some text are in polish, fell free to ask, i've try to translate or explain

I tried to compile the demo project with Delphi 2007 (because I saw a .dpr file)... but it seems that a couple of required files are missing... aka: kom2, kom3 ... and may be others.
I really like to see this working...

Could you fix te problems or may be send me this files? and the command to compile with fpc?
Thanks.

Horacio

Reply by email

Dariusz Mazur
to Horacio Jamilis

Horacio Jamilis pisze:

Show quoted text 51 lines
Dariusz Mazur escribió: > Horacio Jamilis pisze: >> Dariusz Mazur escribió: >>> This is not problem to have separate executable, only have one >>> source to both GUI. >>> I made program which can work as desktop and as web-based (FCGI or >>> standalone web server) >>> Each form is create dynamical in runtime, source is one, but in >>> compile time: >>> 1 for desktop: clasic units from LCL (or rather VCL, on windows I >>> use Delphi) >>> 2.for web-base different set of units to handle web, but with the >>> same API as VCL >>> >>> Darek >> How do you do this? >> I want to do exactly the same... having 2 exe, one for desktop and >> one for web without doubling the work (from the same sources). >> I was thinking on using Intraweb (or VCL for the web in delphi)... >> but, what are you using for the web with FPC? > webserver from synapse, but with several modification (current is > much faster) > own framework as I write upper,it contain replacement for units > controls, forms, stdctrl of course only this part which I need, but > its work > >> I want to move to FPC from delphi, but I see no viable alternative >> to Intraweb in FPC (and Lazarus). > Indy don't work properly on linux, > Intraweb is so complicated and very heavy >> Do you have any sample source I could see? >> > > http://www.emadar.com/fpc/xwebdemo.zip > > its very simple demo with all libraries > but some text are in polish, > fell free to ask, i've try to translate or explain > > > > I tried to compile the demo project with Delphi 2007 (because I saw a .dpr file)... but it seems that a couple of required files are missing... aka: kom2, kom3 ... and may be others. I really like to see this working... Could you fix te problems or may be send me this files? and the command to compile with fpc? Thanks.

I've update xwebdemo,
it contains compiled version too
if any problems, tell me

On FPC all should be OK, maybe except dpr file and synapse (zip does not contain whole)
ex: FPC testmobile.dpr

Darek

Reply by email

Horacio Jamilis
to Dariusz Mazur
Dariusz Mazur wrote 28 lines
>> >> http://www.emadar.com/fpc/xwebdemo.zip >> >> its very simple demo with all libraries >> but some text are in polish, >> fell free to ask, i've try to translate or explain >> >> >> >> > > I tried to compile the demo project with Delphi 2007 (because I saw a > .dpr file)... but it seems that a couple of required files are > missing... aka: kom2, kom3 ... and may be others. > I really like to see this working... > > Could you fix te problems or may be send me this files? > and the command to compile with fpc? > Thanks. I've update xwebdemo, it contains compiled version too if any problems, tell me On FPC all should be OK, maybe except dpr file and synapse (zip does not contain whole) ex: FPC testmobile.dpr

Thanks, I will test it.

Horacio

Reply by email