buffybox/unl0kr/command_line.c

140 lines
4.5 KiB
C
Raw Normal View History

2021-09-23 21:05:36 +02:00
/**
* Copyright 2021 Johannes Marbach
* SPDX-License-Identifier: GPL-3.0-or-later
2021-09-23 21:05:36 +02:00
*/
2024-01-12 10:00:05 +01:00
2021-09-23 21:05:36 +02:00
#include "command_line.h"
#include "unl0kr.h"
2024-03-28 13:44:33 +01:00
#include "../shared/log.h"
2021-09-23 21:05:36 +02:00
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2021-09-23 21:05:36 +02:00
/**
* Static prototypes
*/
/**
* Initialise a command line options struct with default values and exit on failure.
2021-09-23 21:05:36 +02:00
*
* @param opts pointer to the options struct
*/
2021-09-25 11:55:00 +02:00
static void init_opts(ul_cli_opts *opts);
2021-09-23 21:05:36 +02:00
/**
* Output usage instructions.
*/
static void print_usage();
/**
* Static functions
*/
2021-09-25 11:55:00 +02:00
static void init_opts(ul_cli_opts *opts) {
bbx_cli_init_common_opts(&opts->common);
2023-03-12 20:24:28 +00:00
opts->num_config_files = 0;
opts->config_files = NULL;
opts->message = NULL;
opts->newline = true;
2021-09-23 21:05:36 +02:00
}
static void print_usage() {
fprintf(stderr,
/*-------------------------------- 78 CHARS --------------------------------*/
2021-09-23 21:05:36 +02:00
"Usage: unl0kr [OPTION]\n"
"\n"
2022-01-27 20:25:25 +01:00
"Unl0kr values the CRYPTTAB_TRIED variable. Upon completion, the entered\n"
"password is printed to STDOUT. All other output happens on STDERR.\n"
"\n"
2021-09-23 21:05:36 +02:00
"Mandatory arguments to long options are mandatory for short options too.\n"
" -m, --message A message that will be displayed to a user\n"
2023-03-12 20:24:28 +00:00
" -C, --config-override Path to a config override file. Can be supplied\n"
" multiple times. Config files are merged in the\n"
" following order:\n"
2024-09-24 19:09:53 +00:00
" * /usr/share/unl0kr/unl0kr.conf\n"
" * /usr/share/unl0kr/unl0kr.conf.d/* (alphabetically)\n"
2023-03-12 20:24:28 +00:00
" * /etc/unl0kr.conf\n"
" * /etc/unl0kr.conf.d/* (alphabetically)\n"
" * Override files (in supplied order)\n"
" -g, --geometry=NxM[@X,Y] Force a display size of N horizontal times M\n"
" vertical pixels, offset horizontally by X\n"
2022-10-03 14:00:03 +02:00
" pixels and vertically by Y pixels\n"
" -d --dpi=N Override the display's DPI value\n"
" -h, --help Print this message and exit\n"
" -n Do not append a newline character to a password\n"
" -v, --verbose Enable more detailed logging output on STDERR\n"
" -V, --version Print the unl0kr version and exit\n");
/*-------------------------------- 78 CHARS --------------------------------*/
2021-09-23 21:05:36 +02:00
}
/**
* Public functions
*/
2021-09-25 11:45:48 +02:00
void ul_cli_parse_opts(int argc, char *argv[], ul_cli_opts *opts) {
init_opts(opts);
2021-09-23 21:05:36 +02:00
2021-09-25 11:45:48 +02:00
struct option long_opts[] = {
{ "message", required_argument, NULL, 'm' },
{ "config-override", required_argument, NULL, 'C' },
{ "geometry", required_argument, NULL, 'g' },
{ "dpi", required_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
{ "verbose", no_argument, NULL, 'v' },
{ "version", no_argument, NULL, 'V' },
2021-09-23 21:05:36 +02:00
{ NULL, 0, NULL, 0 }
};
int opt, index = 0;
2025-05-09 13:56:44 +02:00
while ((opt = getopt_long(argc, argv, "m:C:g:d:hnvV", long_opts, &index)) != -1) {
2021-09-23 21:05:36 +02:00
switch (opt) {
case 'm':
opts->message = strdup(optarg);
break;
case 'C':
opts->config_files = realloc(opts->config_files, (opts->num_config_files + 1) * sizeof(char *));
if (!opts->config_files) {
2024-03-30 20:52:01 +01:00
bbx_log(BBX_LOG_LEVEL_ERROR, "Could not allocate memory for config file paths");
exit(EXIT_FAILURE);
}
opts->config_files[opts->num_config_files] = optarg;
opts->num_config_files++;
break;
2021-09-23 21:05:36 +02:00
case 'g':
if (bbx_cli_parse_geometry(optarg, &opts->common) != 0) {
exit(EXIT_FAILURE);
2021-09-23 21:05:36 +02:00
}
break;
case 'd':
if (bbx_cli_parse_dpi(optarg, &opts->common) != 0) {
exit(EXIT_FAILURE);
}
break;
2021-09-23 21:05:36 +02:00
case 'h':
print_usage();
exit(EXIT_SUCCESS);
case 'n':
opts->newline = false;
break;
2021-09-23 21:05:36 +02:00
case 'v':
opts->common.verbose = true;
2021-09-23 21:05:36 +02:00
break;
case 'V':
bbx_cli_print_version_and_exit("unl0kr");
break;
2021-09-23 21:05:36 +02:00
default:
print_usage();
exit(EXIT_FAILURE);
}
}
}