On 11/04/17 09:30, Mark Morgan Lloyd via Lazarus wrote:
Apologies if this is an FAQ. Is there an orthodox way that a Lazarus program can very early on look at how it's been started and decide whether it can usefully fall back to a text mode, e.g. to display help info on stdout rather than a messagebox? I'm tinkering with something (a media test program for SDCards etc.) which I'd like to either run as a conventional console program, or using a GUI. I've previously written stuff where operation was entirely dependant on command-line options, but haven't tried making the decision completely automatically.
In case it helps anybody avoid wasted time: looking carefully at what I've been doing, it turns out that it's not possible to make an LCL-based program running on unix (i.e. with X11 etc.) to fall back cleanly to text mode, such that it could be run on e.g. a serial terminal, by manipulating the main program unit (.lpr or whatever).
The problem is that the main unit will import LCL units which assume the presence of X11 or equivalent, and that they will attempt to initialise themselves before the main block in the .lpr is run. Hence any attempted decision in the .lpr is too late to be of much use.
A program can fairly easily be written such that if it's started from a shell wrapped in a GUI (Konsole, xterm or whatever) it can display help or version text and quit in good order. The same applies if it's run from an SSH session provided that X11 tunnelling is enabled. But it won't run over e.g. a serial line or from the main console on a non-GUI system, and will probably give problems is executed as root using sudo or setuid.
There might be ways of working round this using a special unit imported early (i.e. like cmem etc.), I've not experimented with that approach.
-- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] -- _______________________________________________ Lazarus mailing list [email protected] http://lists.lazarus-ide.org/listinfo/lazarus
