I'm looking into Openssl's source code to find out how the programmers made it possible to run different applications based on command line arguments. For instance: I can run openssl speed
, which has its own options, I can run openssl s_server
which has its own options as well, and so on. It all works like a charm on many operating systems. Besides, Openssl have something which I call 'command line suggestions' where it suggests available options when I press the tabulator key.
I'm looking into source code but have any clue how to implement something similar in my application. Any ideas?
Those are called subcommands in OpenSSL. They include
s_cient
,s_server
,digest
,enc
,dec
,x509
,speed
, etc.OpenSSL provides a a
main
for theopenssl
command and all the subcommands. Themain
in the subcommand is wrapped in a macro so that if not buildingopenssl
command, then the subcommand can become its own stand-alone program.Here's what the use of the macro look like. All of the subcommands do this:
OpenSSL knows when its including all the subcommands with the
openssl
command, and it definesMONOLITH
to switch "off" themains
in the subcommands.And from
apps.h
:If you build the subcommand stand-alone, then you will also need to build
apps.c
and link toapps.o
because its common to them.If I recall correctly,
PROG
unrolls to something likeprog_xxx
whenMONOLITH
is defined using preprocessor magic, wherexxx
is the subcommand in question. So they just look like a bunch of functions:prog_s_cient
,prog_s_server
,prog_digest
, etc.