Startup options simplified.

This commit is contained in:
2025-10-01 03:52:01 -04:00
parent 92b9b057dd
commit df115abfbd
2 changed files with 42 additions and 148 deletions

View File

@ -21,7 +21,11 @@ flenser_LDADD = \
$(top_builddir)/dw/libDw-core.a \
$(top_builddir)/lout/liblout.a \
@LIBJPEG_LIBS@ @LIBPNG_LIBS@ @LIBWEBP_LIBS@ @LIBFLTK_LIBS@ @LIBZ_LIBS@ \
@LIBICONV_LIBS@ @LIBPTHREAD_LIBS@ @LIBX11_LIBS@ @LIBSSL_LIBS@
@LIBICONV_LIBS@ @LIBPTHREAD_LIBS@ @LIBX11_LIBS@ @LIBSSL_LIBS@ \
-L $(top_builddir)/../Alepha/ -lalepha
flenser_LDFLAGS = -Wl,-rpath=$(top_builddir)/../Alepha
flenser_SOURCES = \
flenser.cc \

View File

@ -68,49 +68,14 @@
#include "dw/textblock.hh"
#include "dw/table.hh"
#include <Alepha/ProgramOptions.h>
static volatile sig_atomic_t sig_reload = 0;
/**
* Command line options structure
*/
typedef enum {
DILLO_CLI_NONE = 0,
DILLO_CLI_XID = 1 << 0,
DILLO_CLI_FULLWINDOW = 1 << 1,
DILLO_CLI_HELP = 1 << 2,
DILLO_CLI_VERSION = 1 << 3,
DILLO_CLI_LOCAL = 1 << 4,
DILLO_CLI_GEOMETRY = 1 << 5,
DILLO_CLI_ERROR = 1 << 15
} OptID;
typedef struct {
const char *shortopt;
const char *longopt;
int opt_argc; /* positive: mandatory, negative: optional */
OptID id;
const char *help;
} CLI_options;
static const CLI_options Options[] = {
{"-f", "--fullwindow", 0, DILLO_CLI_FULLWINDOW,
" -f, --fullwindow Start in full window mode: hide address bar,\n"
" navigation buttons, menu, and status bar."},
{"-g", "-geometry", 1, DILLO_CLI_GEOMETRY,
" -g, -geometry GEO Set initial window position where GEO is\n"
" WxH[{+-}X{+-}Y]"},
{"-h", "--help", 0, DILLO_CLI_HELP,
" -h, --help Display this help text and exit."},
{"-l", "--local", 0, DILLO_CLI_LOCAL,
" -l, --local Don't load images or stylesheets, or follow\n"
" redirections, for these FILEs or URLs."},
{"-v", "--version", 0, DILLO_CLI_VERSION,
" -v, --version Display version info and exit."},
{"-x", "--xid", 1, DILLO_CLI_XID,
" -x, --xid XID Open first Flenser window in an existing\n"
" window whose window ID is XID."},
{NULL, NULL, 0, DILLO_CLI_NONE, NULL}
};
// TODO: Migrate some of the command line options here?
/*
@ -183,81 +148,14 @@ static void est_sigchld(void)
/**
* Print help text generated from the options structure
*/
static void printHelp(const char *cmdname, const CLI_options *options)
static void printHelp(const char *cmdname)
{
printf("Usage: %s [OPTION]... [--] [URL|FILE]...\n"
"Options:\n", cmdname);
while (options && options->help) {
printf("%s\n", options->help);
options++;
}
printf(" URL URL to browse.\n"
" FILE Local FILE to view.\n"
"\n");
}
/**
* Return the maximum number of option arguments
*/
static int numOptions(const CLI_options *options)
{
int i, max;
for (i = 0, max = 0; options[i].shortopt; i++)
if (abs(options[i].opt_argc) > max)
max = abs(options[i].opt_argc);
return max;
}
/**
* Get next command line option.
*/
static OptID getCmdOption(const CLI_options *options, int argc, char **argv,
char **opt_argv, int *idx)
{
typedef enum { O_SEARCH, O_FOUND, O_NOTFOUND, O_DONE } State;
OptID opt_id = DILLO_CLI_NONE;
int i = 0;
State state = O_SEARCH;
if (*idx >= argc) {
state = O_DONE;
} else {
state = O_NOTFOUND;
for (i = 0; options[i].shortopt; i++) {
if (strcmp(options[i].shortopt, argv[*idx]) == 0 ||
strcmp(options[i].longopt, argv[*idx]) == 0) {
state = O_FOUND;
++*idx;
break;
}
}
}
if (state == O_FOUND) {
int n_arg = options[i].opt_argc;
opt_id = options[i].id;
/* Find the required/optional arguments of the option */
for (i = 0; *idx < argc && i < abs(n_arg) && argv[*idx][0] != '-'; i++)
opt_argv[i] = argv[(*idx)++];
opt_argv[i] = NULL;
/* Optional arguments have opt_argc < 0 */
if (i < n_arg) {
fprintf(stderr, "Option %s requires %d argument%s\n",
argv[*idx-i-1], n_arg, (n_arg == 1) ? "" : "s");
opt_id = DILLO_CLI_ERROR;
}
}
if (state == O_NOTFOUND) {
if (strcmp(argv[*idx], "--") == 0)
(*idx)++;
else if (argv[*idx][0] == '-') {
fprintf(stderr, "Command line option \"%s\" not recognized.\n",
argv[*idx]);
opt_id = DILLO_CLI_ERROR;
}
}
return opt_id;
(void) printHelp;
}
/**
@ -400,14 +298,10 @@ static std::unique_ptr< DilloUrl > makeStartUrl(char *str, bool local)
*/
int main(int argc, char **argv)
{
uint_t opt_id;
uint_t options_got = 0;
uint32_t xid = 0;
int idx = 1;
int xpos = PREFS_GEOMETRY_DEFAULT_XPOS, ypos = PREFS_GEOMETRY_DEFAULT_YPOS,
width = PREFS_GEOMETRY_DEFAULT_WIDTH,
height = PREFS_GEOMETRY_DEFAULT_HEIGHT;
char **opt_argv;
FILE *fp;
srand((uint_t)(time(0) ^ getpid()));
@ -417,43 +311,39 @@ int main(int argc, char **argv)
// Establish our custom SIGCHLD handler
est_sigchld();
using namespace Alepha::literals::option_literals;
bool fullWindow= false;
--"fullwindow"_option << fullWindow << "Start in full window mode: hide address "
<< "bar, navigation buttons, menu, and status bar.";
std::optional< std::string > geometry;
--"geometry"_option << geometry << "Set initial window position where GEO is "
<< "WxH[{+-}X{+-}Y]";
bool local_opt= false;
--"local"_option << local_opt << "Don't load images or stylesheets, or follow "
<< "redirections, for these FILEs or URLs.";
--"version"_option << []{ a_Version_print_info(); ::exit( EXIT_SUCCESS ); }
<< "Display version info and exit.";
std::optional< int > xid;
--"xid"_option << xid << "Open first Flenser window in an existing"
<< "window whose window ID is XID.";
/* Handle command line options */
opt_argv = dNew0(char*, numOptions(Options) + 1);
while ((opt_id = getCmdOption(Options, argc, argv, opt_argv, &idx))) {
options_got |= opt_id;
switch (opt_id) {
case DILLO_CLI_FULLWINDOW:
case DILLO_CLI_LOCAL:
break;
case DILLO_CLI_XID:
const auto params= Alepha::handleOptions( argc, argv );
if( geometry.has_value() )
{
if( !a_Misc_parse_geometry( geometry.value().data(), &xpos, &ypos, &width,
&height ) )
{
char *end;
xid = strtol(opt_argv[0], &end, 0);
if (*end) {
fprintf(stderr, "XID argument \"%s\" not valid.\n",opt_argv[0]);
return 2;
}
break;
}
case DILLO_CLI_GEOMETRY:
if (!a_Misc_parse_geometry(opt_argv[0],&xpos,&ypos,&width,&height)){
fprintf(stderr, "geometry argument \"%s\" not valid. Must be of "
"the form WxH[{+-}X{+-}Y].\n", opt_argv[0]);
return 2;
}
break;
case DILLO_CLI_VERSION:
a_Version_print_info();
return 0;
case DILLO_CLI_HELP:
printHelp(argv[0], Options);
return 0;
default:
printHelp(argv[0], Options);
"the form WxH[{+-}X{+-}Y].\n", geometry.value().c_str());
return 2;
}
}
dFree(opt_argv);
// set the default values for the preferences
a_Prefs_init();
@ -506,9 +396,9 @@ int main(int argc, char **argv)
dw::Textblock::setStretchabilityFactor (prefs.stretchability_factor);
/* command line options override preferences */
if (options_got & DILLO_CLI_FULLWINDOW)
if (fullWindow)
prefs.fullwindow_start = TRUE;
if (options_got & DILLO_CLI_GEOMETRY) {
if (geometry.has_value()) {
prefs.width = width;
prefs.height = height;
prefs.xpos = xpos;
@ -546,7 +436,7 @@ int main(int argc, char **argv)
fl_message_title_default("Flenser: Message");
// Create a new UI/bw pair
BrowserWindow *bw = a_UIcmd_browser_window_new(0, 0, xid, NULL);
BrowserWindow *bw = a_UIcmd_browser_window_new(0, 0, xid.value_or( 0 ), NULL);
/* We need this so that fl_text_extents() in dw/fltkplatform.cc can
* work when FLTK is configured without XFT and Dillo is opening
@ -565,7 +455,7 @@ int main(int argc, char **argv)
}
/* Open URLs/files */
const bool local = options_got & DILLO_CLI_LOCAL;
const bool local = local_opt;
if (idx == argc) {
/* No URLs/files on cmdline. Send startup screen */