Allow for long options using getopt_long

This commit is contained in:
Marc Di Luzio 2019-05-10 20:37:51 +01:00
parent 99c7d04e69
commit baff9c0363
2 changed files with 58 additions and 39 deletions

View File

@ -56,6 +56,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "gamemode_client.h" #include "gamemode_client.h"
#include "logging.h" #include "logging.h"
#include <getopt.h>
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -64,12 +65,13 @@ POSSIBILITY OF SUCH DAMAGE.
#define USAGE_TEXT \ #define USAGE_TEXT \
"Usage: %s [-d] [-l] [-r] [-t] [-h] [-v]\n\n" \ "Usage: %s [-d] [-l] [-r] [-t] [-h] [-v]\n\n" \
" -d daemonize self after launch\n" \ " -r, --request Request gamemode and pause\n" \
" -l log to syslog\n" \ " -s, --status Query the status of gamemode\n" \
" -r request gamemode and pause\n" \ " -d, --daemonize Daemonize self after launch\n" \
" -t run tests\n" \ " -l, --log-to-syslog Log to syslog\n" \
" -h print this help\n" \ " -r, --test Run tests\n" \
" -v print version\n" \ " -h, --help Print this help\n" \
" -v, --version Print version\n" \
"\n" \ "\n" \
"See man page for more information.\n" "See man page for more information.\n"
@ -102,8 +104,17 @@ int main(int argc, char *argv[])
bool daemon = false; bool daemon = false;
bool use_syslog = false; bool use_syslog = false;
int opt = 0; int opt = 0;
int status;
while ((opt = getopt(argc, argv, "dlsrtvh")) != -1) { /* Options struct for getopt_long */
static struct option long_options[] = {
{ "daemonize", no_argument, 0, 'd' }, { "log-to-syslog", no_argument, 0, 'l' },
{ "request", no_argument, 0, 'r' }, { "test", no_argument, 0, 't' },
{ "status", no_argument, 0, 's' }, { "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' }
};
static const char *short_options = "dls::r::tvh";
while ((opt = getopt_long(argc, argv, short_options, long_options, 0)) != -1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
daemon = true; daemon = true;
@ -111,31 +122,41 @@ int main(int argc, char *argv[])
case 'l': case 'l':
use_syslog = true; use_syslog = true;
break; break;
case 's': {
if ((status = gamemode_query_status()) < 0) { case 's':
switch (gamemode_query_status()) {
case 0:
LOG_MSG("gamemode is inactive\n");
break;
case 1:
LOG_MSG("gamemode is active\n");
break;
case -1:
LOG_ERROR("gamemode status request failed: %s\n", gamemode_error_string()); LOG_ERROR("gamemode status request failed: %s\n", gamemode_error_string());
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else if (status > 0) { default:
LOG_MSG("gamemode is active\n"); LOG_ERROR("gamemode_query_status returned unexpected value 2\n");
} else { exit(EXIT_FAILURE);
LOG_MSG("gamemode is inactive\n");
} }
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break;
}
case 'r': case 'r':
if (gamemode_request_start() < 0) { if (gamemode_request_start() < 0) {
LOG_ERROR("gamemode request failed: %s\n", gamemode_error_string()); LOG_ERROR("gamemode request failed: %s\n", gamemode_error_string());
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if ((status = gamemode_query_status()) == 2) { switch (gamemode_query_status()) {
case 2:
LOG_MSG("gamemode request succeeded and is active\n"); LOG_MSG("gamemode request succeeded and is active\n");
} else if (status == 1) { break;
case 1:
LOG_ERROR("gamemode request succeeded and is active but registration failed\n"); LOG_ERROR("gamemode request succeeded and is active but registration failed\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else { case 0:
LOG_ERROR("gamemode request succeeded but is not active\n"); LOG_ERROR("gamemode request succeeded but is not active\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -153,23 +174,20 @@ int main(int argc, char *argv[])
} }
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break;
case 't': case 't': {
status = game_mode_run_client_tests(); int status = game_mode_run_client_tests();
exit(status); exit(status);
break; }
case 'v': case 'v':
LOG_MSG(VERSION_TEXT); LOG_MSG(VERSION_TEXT);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break;
case 'h': case 'h':
LOG_MSG(USAGE_TEXT, argv[0]); LOG_MSG(USAGE_TEXT, argv[0]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break;
default: default:
fprintf(stderr, USAGE_TEXT, argv[0]); fprintf(stderr, USAGE_TEXT, argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
break;
} }
} }

View File

@ -4,7 +4,7 @@
.SH NAME .SH NAME
gamemoded \- optimises system performance on demand gamemoded \- optimises system performance on demand
.SH SYNOPSIS .SH SYNOPSIS
\fBgamemoded\fR [\fB\-d\fR] [\fB\-l\fR] [\fB\-h\fR] [\fB\-v\fR] \fBgamemoded\fR [OPTIONS...]
.SH DESCRIPTION .SH DESCRIPTION
\fBGameMode\fR is a daemon/lib combo for Linux that allows games to request a set of optimisations be temporarily applied to the host OS. \fBGameMode\fR is a daemon/lib combo for Linux that allows games to request a set of optimisations be temporarily applied to the host OS.
@ -12,26 +12,27 @@ The design has a clear cut abstraction between the host daemon and library (\fBg
\fBGameMode\fR was designed primarily as a stop-gap solution to problems with the Intel and AMD CPU powersave or ondemand governors, but is intended to be expanded beyond just CPU governor states, as there are a wealth of automation tasks one might want to apply. \fBGameMode\fR was designed primarily as a stop-gap solution to problems with the Intel and AMD CPU powersave or ondemand governors, but is intended to be expanded beyond just CPU governor states, as there are a wealth of automation tasks one might want to apply.
.SH OPTIONS .SH OPTIONS
.TP 8 .TP 8
.B \-d .B \-r, \-\-request
Request gamemode and pause
.TP 8
.B \-s, \-\-status
Query the status of gamemode
.TP 8
.B \-d, \-\-daemonize
Run the daemon as a separate process (daemonize it) Run the daemon as a separate process (daemonize it)
.TP 8 .TP 8
.B \-l .B \-l, \-\-log-to-syslog
Log to syslog Log to syslog
.TP 8 .TP 8
.B \-r .B \-h, \-\-help
Request gamemode and wait for any signal
.TP 8
.B \-s
Query the current status of gamemode
.TP 8
.B \-h
Print help text Print help text
.TP 8 .TP 8
.B \-t .B \-t, \-\-test
Run diagnostic tests on the current installation Run diagnostic tests on the current installation
.TP 8 .TP 8
.B \-v .B \-v, \-\-version
Print the version Print the version
.SH USAGE .SH USAGE