From 3ba33321de0790d60dc473acee093de5a3650480 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sat, 28 Nov 2015 15:47:44 +0200 Subject: [PATCH 1/8] Use macros for exit values --- sway/main.c | 6 +++--- swaybg/main.c | 2 +- swaygrab/main.c | 4 ++-- swaymsg/main.c | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/sway/main.c b/sway/main.c index dd609214b..60a41923c 100644 --- a/sway/main.c +++ b/sway/main.c @@ -90,7 +90,7 @@ int main(int argc, char **argv) { #else fprintf(stdout, "version not detected\n"); #endif - exit(0); + exit(EXIT_SUCCESS); break; case 'V': // verbose verbose = 1; @@ -98,10 +98,10 @@ int main(int argc, char **argv) { case 'p': ; // --get-socketpath if (getenv("SWAYSOCK")) { fprintf(stdout, "%s\n", getenv("SWAYSOCK")); - exit(0); + exit(EXIT_SUCCESS); } else { fprintf(stderr, "sway socket not detected.\n"); - exit(1); + exit(EXIT_FAILURE); } break; } diff --git a/swaybg/main.c b/swaybg/main.c index ec64ddb71..69d718c51 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -29,7 +29,7 @@ void sway_terminate(void) { } list_free(surfaces); registry_teardown(registry); - exit(1); + exit(EXIT_FAILURE); } int main(int argc, const char **argv) { diff --git a/swaygrab/main.c b/swaygrab/main.c index 63cf223f4..e60d154fc 100644 --- a/swaygrab/main.c +++ b/swaygrab/main.c @@ -10,7 +10,7 @@ #include "ipc-client.h" void sway_terminate(void) { - exit(1); + exit(EXIT_FAILURE); } int numlen(int n) { @@ -163,7 +163,7 @@ int main(int argc, char **argv) { #else fprintf(stdout, "version not detected\n"); #endif - exit(0); + exit(EXIT_SUCCESS); break; } } diff --git a/swaymsg/main.c b/swaymsg/main.c index 3a2e1ee75..e629bcc2b 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -12,7 +12,7 @@ #include "log.h" void sway_terminate(void) { - exit(1); + exit(EXIT_FAILURE); } int main(int argc, char **argv) { @@ -52,7 +52,7 @@ int main(int argc, char **argv) { #else fprintf(stdout, "version not detected\n"); #endif - exit(0); + exit(EXIT_SUCCESS); break; } } From e48a16ce5ec0719f9f119ad5fb5891081e5e6dcd Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Thu, 26 Nov 2015 21:01:37 +0200 Subject: [PATCH 2/8] sway: Print usage and exit on unknown options --- sway/main.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/sway/main.c b/sway/main.c index 60a41923c..7b24d4052 100644 --- a/sway/main.c +++ b/sway/main.c @@ -65,6 +65,18 @@ int main(int argc, char **argv) { }; char *config_path = NULL; + + const char* usage = + "Usage: sway [options] [command]\n" + "\n" + " -c, --config Specify a config file.\n" + " -C, --validate Check the validity of the config file, then exit.\n" + " -d, --debug Enables full logging, including debug information.\n" + " -v, --version Show the version number and quit.\n" + " -V, --verbose Enables more verbose logging.\n" + " --get-socketpath Gets the IPC socket path and prints it, then exits.\n" + "\n"; + int c; while (1) { int option_index = 0; @@ -104,6 +116,9 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } break; + default: + fprintf(stderr, "%s", usage); + exit(EXIT_FAILURE); } } From b235ccd212920db7e444716b7a36fcb4a36a3644 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sat, 28 Nov 2015 16:09:14 +0200 Subject: [PATCH 3/8] swaygrab: Print usage and exit on unknown options --- swaygrab/main.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/swaygrab/main.c b/swaygrab/main.c index e60d154fc..90d118e2a 100644 --- a/swaygrab/main.c +++ b/swaygrab/main.c @@ -135,6 +135,15 @@ int main(int argc, char **argv) { {0, 0, 0, 0} }; + const char *usage = + "Usage: swaygrab [options] [file]\n" + "\n" + " -c, --capture Capture video.\n" + " -v, --version Show the version number and quit.\n" + " -s, --socket Use the specified socket.\n" + " -R, --rate Specify framerate (default: 30)\n" + " -r, --raw Write raw rgba data to stdout.\n"; + int c; while (1) { int option_index = 0; @@ -165,6 +174,9 @@ int main(int argc, char **argv) { #endif exit(EXIT_SUCCESS); break; + default: + fprintf(stderr, "%s", usage); + exit(EXIT_FAILURE); } } From 0d55d1a0676704acdab86da231fe16d7e61c4ccf Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sat, 28 Nov 2015 16:09:19 +0200 Subject: [PATCH 4/8] swaymsg: Print usage and exit on unknown options --- swaymsg/main.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/swaymsg/main.c b/swaymsg/main.c index e629bcc2b..2f2e843ae 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -30,6 +30,14 @@ int main(int argc, char **argv) { {0, 0, 0, 0} }; + const char *usage = + "Usage: swaymsg [options] [message]\n" + "\n" + " -q, --quiet Be quiet.\n" + " -v, --version Show the version number and quit.\n" + " -s, --socket Use the specified socket.\n" + " -t, --type Specify the message type.\n"; + int c; while (1) { int option_index = 0; @@ -54,6 +62,9 @@ int main(int argc, char **argv) { #endif exit(EXIT_SUCCESS); break; + default: + fprintf(stderr, "%s", usage); + exit(EXIT_FAILURE); } } From 2f2e1f3fc710982fd8577091c344af19ac736d2f Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Thu, 26 Nov 2015 21:03:21 +0200 Subject: [PATCH 5/8] sway: Add --help option that prints usage --- sway.1.txt | 3 +++ sway/main.c | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/sway.1.txt b/sway.1.txt index c07158415..c80bd9179 100644 --- a/sway.1.txt +++ b/sway.1.txt @@ -17,6 +17,9 @@ Synopsis Options ------- +*-h, --help*:: + Show help message and quit. + *-c, \--config* :: Specifies a config file. diff --git a/sway/main.c b/sway/main.c index 7b24d4052..473a0990a 100644 --- a/sway/main.c +++ b/sway/main.c @@ -55,6 +55,7 @@ int main(int argc, char **argv) { static int verbose = 0, debug = 0, validate = 0; static struct option long_options[] = { + {"help", no_argument, NULL, 'h'}, {"config", required_argument, NULL, 'c'}, {"validate", no_argument, &validate, 1}, {"debug", no_argument, &debug, 1}, @@ -69,6 +70,7 @@ int main(int argc, char **argv) { const char* usage = "Usage: sway [options] [command]\n" "\n" + " -h, --help Show help message and quit.\n" " -c, --config Specify a config file.\n" " -C, --validate Check the validity of the config file, then exit.\n" " -d, --debug Enables full logging, including debug information.\n" @@ -80,13 +82,17 @@ int main(int argc, char **argv) { int c; while (1) { int option_index = 0; - c = getopt_long(argc, argv, "CdvVpc:", long_options, &option_index); + c = getopt_long(argc, argv, "hCdvVpc:", long_options, &option_index); if (c == -1) { break; } switch (c) { case 0: // Flag break; + case 'h': // help + fprintf(stdout, "%s", usage); + exit(EXIT_SUCCESS); + break; case 'c': // config config_path = strdup(optarg); break; From 12c8cf5bf6dd0d0e77ff78260b603cdaa1731a65 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sat, 28 Nov 2015 16:18:54 +0200 Subject: [PATCH 6/8] swaygrab: Add --help option that prints usage --- swaygrab.1.txt | 3 +++ swaygrab/main.c | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/swaygrab.1.txt b/swaygrab.1.txt index 009fbe7e7..cd4b8bd26 100644 --- a/swaygrab.1.txt +++ b/swaygrab.1.txt @@ -20,6 +20,9 @@ ImageMagick convert for processing. Options ------- +*-h, --help*:: + Show help message and quit. + *-c, \--capture*:: Captures multiple frames as video and passes them into ffmpeg. Continues until you send SIGTERM (ctrl+c) to swaygrab. diff --git a/swaygrab/main.c b/swaygrab/main.c index 90d118e2a..ae17155f3 100644 --- a/swaygrab/main.c +++ b/swaygrab/main.c @@ -127,6 +127,7 @@ int main(int argc, char **argv) { init_log(L_INFO); static struct option long_options[] = { + {"help", no_argument, NULL, 'h'}, {"capture", no_argument, &capture, 'c'}, {"version", no_argument, NULL, 'v'}, {"socket", required_argument, NULL, 's'}, @@ -138,6 +139,7 @@ int main(int argc, char **argv) { const char *usage = "Usage: swaygrab [options] [file]\n" "\n" + " -h, --help Show help message and quit.\n" " -c, --capture Capture video.\n" " -v, --version Show the version number and quit.\n" " -s, --socket Use the specified socket.\n" @@ -147,7 +149,7 @@ int main(int argc, char **argv) { int c; while (1) { int option_index = 0; - c = getopt_long(argc, argv, "cvs:r", long_options, &option_index); + c = getopt_long(argc, argv, "hcvs:r", long_options, &option_index); if (c == -1) { break; } From bf97a5ada5ea4f8b45d15d00dc7f21487af8eadc Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sat, 28 Nov 2015 16:18:46 +0200 Subject: [PATCH 7/8] swaymsg: Add --help option that prints usage --- swaymsg.1.txt | 3 +++ swaymsg/main.c | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/swaymsg.1.txt b/swaymsg.1.txt index d832dd171..984780fa0 100644 --- a/swaymsg.1.txt +++ b/swaymsg.1.txt @@ -17,6 +17,9 @@ Synopsis Options ------- +*-h, --help*:: + Show help message and quit. + *-q, \--quiet*:: Sends the IPC message but does not print the response from sway. diff --git a/swaymsg/main.c b/swaymsg/main.c index 2f2e843ae..8f91dc55e 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -23,6 +23,7 @@ int main(int argc, char **argv) { init_log(L_INFO); static struct option long_options[] = { + {"help", no_argument, NULL, 'h'}, {"quiet", no_argument, &quiet, 'q'}, {"version", no_argument, NULL, 'v'}, {"socket", required_argument, NULL, 's'}, @@ -33,6 +34,7 @@ int main(int argc, char **argv) { const char *usage = "Usage: swaymsg [options] [message]\n" "\n" + " -h, --help Show help message and quit.\n" " -q, --quiet Be quiet.\n" " -v, --version Show the version number and quit.\n" " -s, --socket Use the specified socket.\n" @@ -41,7 +43,7 @@ int main(int argc, char **argv) { int c; while (1) { int option_index = 0; - c = getopt_long(argc, argv, "qvs:t:", long_options, &option_index); + c = getopt_long(argc, argv, "hqvs:t:", long_options, &option_index); if (c == -1) { break; } From 923c3245ace71ea0e26a0b12746a699fa499f759 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sat, 28 Nov 2015 16:35:44 +0200 Subject: [PATCH 8/8] Fix option parsing Using 'flag' results in duplicate code paths for short and long options. This broke the -q short option in swaymsg, because there was: {"quiet", no_argument, &quiet, 'q'} Which will set quiet to 'q' and return 0, not 'q'. --- sway/main.c | 8 +++----- swaygrab/main.c | 6 ++---- swaymsg/main.c | 5 +++-- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/sway/main.c b/sway/main.c index 473a0990a..1261a7ced 100644 --- a/sway/main.c +++ b/sway/main.c @@ -57,10 +57,10 @@ int main(int argc, char **argv) { static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"config", required_argument, NULL, 'c'}, - {"validate", no_argument, &validate, 1}, - {"debug", no_argument, &debug, 1}, + {"validate", no_argument, NULL, 'C'}, + {"debug", no_argument, NULL, 'd'}, {"version", no_argument, NULL, 'v'}, - {"verbose", no_argument, &verbose, 1}, + {"verbose", no_argument, NULL, 'V'}, {"get-socketpath", no_argument, NULL, 'p'}, {0, 0, 0, 0} }; @@ -87,8 +87,6 @@ int main(int argc, char **argv) { break; } switch (c) { - case 0: // Flag - break; case 'h': // help fprintf(stdout, "%s", usage); exit(EXIT_SUCCESS); diff --git a/swaygrab/main.c b/swaygrab/main.c index ae17155f3..681a6da42 100644 --- a/swaygrab/main.c +++ b/swaygrab/main.c @@ -128,10 +128,10 @@ int main(int argc, char **argv) { static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, - {"capture", no_argument, &capture, 'c'}, + {"capture", no_argument, NULL, 'c'}, {"version", no_argument, NULL, 'v'}, {"socket", required_argument, NULL, 's'}, - {"raw", no_argument, &raw, 'r'}, + {"raw", no_argument, NULL, 'r'}, {"rate", required_argument, NULL, 'R'}, {0, 0, 0, 0} }; @@ -154,8 +154,6 @@ int main(int argc, char **argv) { break; } switch (c) { - case 0: // Flag - break; case 's': // Socket socket_path = strdup(optarg); break; diff --git a/swaymsg/main.c b/swaymsg/main.c index 8f91dc55e..f8c9e14c2 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -24,7 +24,7 @@ int main(int argc, char **argv) { static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, - {"quiet", no_argument, &quiet, 'q'}, + {"quiet", no_argument, NULL, 'q'}, {"version", no_argument, NULL, 'v'}, {"socket", required_argument, NULL, 's'}, {"type", required_argument, NULL, 't'}, @@ -48,7 +48,8 @@ int main(int argc, char **argv) { break; } switch (c) { - case 0: // Flag + case 'q': // Quiet + quiet = 1; break; case 's': // Socket socket_path = strdup(optarg);