diff options
author | Hartmut Goebel <h.goebel@crazy-compilers.com> | 2019-03-03 22:08:53 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-04-03 13:44:54 +0200 |
commit | ac123283d47ff2418111cb194a7aa92e9dfa0cd0 (patch) | |
tree | 890527f63e97ef3c8e718fa86a720ba375b8d7b3 /src/util | |
parent | 88c55363e17beaa1c2d7344b3d4a90e1d639dc67 (diff) | |
download | gnunet-ac123283d47ff2418111cb194a7aa92e9dfa0cd0.tar.gz gnunet-ac123283d47ff2418111cb194a7aa92e9dfa0cd0.zip |
gnunet-qr: Use GNUNET_PROGRAM_run to simplify the code.
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/Makefile.am | 3 | ||||
-rw-r--r-- | src/util/gnunet-qr.c | 115 |
2 files changed, 53 insertions, 65 deletions
diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 548af6305..a5a7ff288 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am | |||
@@ -282,6 +282,9 @@ gnunet_uri_LDADD = \ | |||
282 | gnunet_qr_SOURCES = \ | 282 | gnunet_qr_SOURCES = \ |
283 | gnunet-qr.c \ | 283 | gnunet-qr.c \ |
284 | gnunet-qr-utils.h | 284 | gnunet-qr-utils.h |
285 | gnunet_qr_LDADD = \ | ||
286 | libgnunetutil.la \ | ||
287 | $(GN_LIBINTL) | ||
285 | gnunet_qr_LDFLAGS= $(libzbar_LIBS) | 288 | gnunet_qr_LDFLAGS= $(libzbar_LIBS) |
286 | gnunet_qr_CFLAGS = $(libzbar_CFLAGS) -DBINDIR=\"@bindir@/\" | 289 | gnunet_qr_CFLAGS = $(libzbar_CFLAGS) -DBINDIR=\"@bindir@/\" |
287 | 290 | ||
diff --git a/src/util/gnunet-qr.c b/src/util/gnunet-qr.c index ea0e0fea2..b54f352d5 100644 --- a/src/util/gnunet-qr.c +++ b/src/util/gnunet-qr.c | |||
@@ -21,70 +21,34 @@ | |||
21 | #include <stdio.h> | 21 | #include <stdio.h> |
22 | #include <zbar.h> | 22 | #include <zbar.h> |
23 | #include <stdbool.h> | 23 | #include <stdbool.h> |
24 | #include <getopt.h> | 24 | #include "platform.h" |
25 | #include "gnunet_util_lib.h" | ||
25 | #include "gnunet-qr-utils.h" | 26 | #include "gnunet-qr-utils.h" |
26 | 27 | ||
27 | static const char *usage_note = | ||
28 | "gnunet-qr\n" | ||
29 | "Scan a QR code using a video device and import\n" | ||
30 | "\n" | ||
31 | "Arguments mandatory for long options are also mandatory for short options.\n" | ||
32 | " -c, --config FILENAME use configuration file FILENAME\n" | ||
33 | " -d, --device DEVICE use device DEVICE\n" | ||
34 | " -s, --silent do not show preview windows\n" | ||
35 | " -h, --help print this help\n" | ||
36 | " -v, --verbose be verbose\n" | ||
37 | "Report bugs to gnunet-developers@gnu.org.\n" | ||
38 | "\n" | ||
39 | "GNUnet home page: https://gnunet.org/\n" | ||
40 | "General help using GNU software: https://www.gnu.org/gethelp/\n"; | ||
41 | |||
42 | #define LOG(fmt, ...) if (verbose == true) printf(fmt, ## __VA_ARGS__) | 28 | #define LOG(fmt, ...) if (verbose == true) printf(fmt, ## __VA_ARGS__) |
43 | 29 | ||
44 | int main (int argc, char **argv) | 30 | // Command line options |
31 | // program exit code | ||
32 | static long unsigned int exit_code = 1; | ||
33 | |||
34 | static char* device = "/dev/video0"; | ||
35 | static int verbose = false; | ||
36 | static int silent = false; | ||
37 | |||
38 | /** | ||
39 | * Main function that will be run by the scheduler. | ||
40 | * | ||
41 | * @param cls closure | ||
42 | * @param args remaining command-line arguments | ||
43 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
44 | * @param cfg configuration | ||
45 | */ | ||
46 | static void | ||
47 | run (void *cls, | ||
48 | char *const *args, | ||
49 | const char *cfgfile, | ||
50 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
45 | { | 51 | { |
46 | const char* configuration = NULL; | ||
47 | const char* device = "/dev/video0"; | ||
48 | static bool verbose = false; | ||
49 | static bool silent = false; | ||
50 | |||
51 | static struct option long_options[] = { | ||
52 | {"verbose", no_argument, 0, 'v'}, | ||
53 | {"silent", no_argument, 0, 's'}, | ||
54 | {"help", no_argument, 0, 'h'}, | ||
55 | {"config", required_argument, 0, 'c'}, | ||
56 | {"device", required_argument, 0, 'd'}, | ||
57 | {0, 0, 0, 0} | ||
58 | }; | ||
59 | while (1) { | ||
60 | int opt; | ||
61 | opt = getopt_long (argc, argv, "c:hd:sv", | ||
62 | long_options, NULL); | ||
63 | if (opt == -1) | ||
64 | break; | ||
65 | |||
66 | switch (opt) { | ||
67 | case 'h': | ||
68 | printf(usage_note); | ||
69 | return 0; | ||
70 | case 'c': | ||
71 | configuration = optarg; | ||
72 | break; | ||
73 | case 'd': | ||
74 | device = optarg; | ||
75 | break; | ||
76 | case 's': | ||
77 | silent = true; | ||
78 | break; | ||
79 | case 'v': | ||
80 | verbose = true; | ||
81 | break; | ||
82 | default: | ||
83 | printf(usage_note); | ||
84 | return 1; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | /* create a Processor */ | 52 | /* create a Processor */ |
89 | LOG("Initializing\n"); | 53 | LOG("Initializing\n"); |
90 | zbar_processor_t *proc = zbar_processor_create(1); | 54 | zbar_processor_t *proc = zbar_processor_create(1); |
@@ -119,8 +83,6 @@ int main (int argc, char **argv) | |||
119 | zbar_processor_set_visible(proc, 0); | 83 | zbar_processor_set_visible(proc, 0); |
120 | 84 | ||
121 | // extract results | 85 | // extract results |
122 | int rc = 1; | ||
123 | |||
124 | const zbar_symbol_set_t* symbols = zbar_processor_get_results(proc); | 86 | const zbar_symbol_set_t* symbols = zbar_processor_get_results(proc); |
125 | const zbar_symbol_t* symbol = zbar_symbol_set_first_symbol(symbols); | 87 | const zbar_symbol_t* symbol = zbar_symbol_set_first_symbol(symbols); |
126 | 88 | ||
@@ -132,14 +94,14 @@ int main (int argc, char **argv) | |||
132 | if (configuration == NULL) { | 94 | if (configuration == NULL) { |
133 | char* command_args[] = {"gnunet-uri", data, NULL }; | 95 | char* command_args[] = {"gnunet-uri", data, NULL }; |
134 | LOG("Running `gnunet-uri %s`\n", data); | 96 | LOG("Running `gnunet-uri %s`\n", data); |
135 | rc = fork_and_exec(BINDIR "gnunet-uri", command_args); | 97 | exit_code = fork_and_exec(BINDIR "gnunet-uri", command_args); |
136 | } else { | 98 | } else { |
137 | char* command_args[] = {"gnunet-uri", "-c", configuration, data, NULL }; | 99 | char* command_args[] = {"gnunet-uri", "-c", configuration, data, NULL }; |
138 | LOG("Running `gnunet-uri -c '%s' %s`\n", configuration, data); | 100 | LOG("Running `gnunet-uri -c '%s' %s`\n", configuration, data); |
139 | rc = fork_and_exec(BINDIR "gnunet-uri", command_args); | 101 | exit_code = fork_and_exec(BINDIR "gnunet-uri", command_args); |
140 | }; | 102 | }; |
141 | 103 | ||
142 | if (rc != 0) { | 104 | if (exit_code != 0) { |
143 | printf("Failed to add URI %s\n", data); | 105 | printf("Failed to add URI %s\n", data); |
144 | } else { | 106 | } else { |
145 | printf("Added URI %s\n", data); | 107 | printf("Added URI %s\n", data); |
@@ -148,6 +110,29 @@ int main (int argc, char **argv) | |||
148 | 110 | ||
149 | /* clean up */ | 111 | /* clean up */ |
150 | zbar_processor_destroy(proc); | 112 | zbar_processor_destroy(proc); |
113 | }; | ||
151 | 114 | ||
152 | return(rc); | 115 | |
116 | int | ||
117 | main (int argc, char *const *argv) | ||
118 | { | ||
119 | static int ret; | ||
120 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
121 | GNUNET_GETOPT_option_string ('d', "device", "DEVICE", | ||
122 | gettext_noop ("use video-device DEVICE (default: /dev/video0"), | ||
123 | &device), | ||
124 | GNUNET_GETOPT_option_flag ('\0', "verbose", | ||
125 | gettext_noop ("be verbose"), | ||
126 | &verbose), | ||
127 | GNUNET_GETOPT_option_flag ('s', "silent", | ||
128 | gettext_noop ("do not show preview windows"), | ||
129 | &silent), | ||
130 | GNUNET_GETOPT_OPTION_END | ||
131 | }; | ||
132 | ret = GNUNET_PROGRAM_run (argc, | ||
133 | argv, | ||
134 | "gnunet-qr", | ||
135 | gettext_noop ("Scan a QR code using a video device and import the uri read"), | ||
136 | options, &run, NULL); | ||
137 | return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1; | ||
153 | } | 138 | } |