Debian Codegraph @

src/options.c

raw symbols SCIP
1 /**
2 * SPDX-FileCopyrightText: 2016-2022 Dr. Tobias Quathamer <[email protected]>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7 #include <stdlib.h>
8 #include <glib.h>
9 #include <glib/gi18n.h>
10 #include <glib/gprintf.h>
11 #include "options.h"
12
13 /**
14 * Global variables to hold the program options.
15 */
16 gchar *option_standard;
17 gchar *option_pathname;
18 gchar *option_namefield;
19 gchar *option_locale;
20 gboolean option_null_separator;
21 gboolean option_flag_output;
22 gboolean option_version;
23
24 // Helper variables, not to be accessed directly.
25 gboolean option_name;
26 gboolean option_officialname;
27 gboolean option_commonname;
28
29 /**
30 * Define the program command line options.
31 */
32 static GOptionEntry entries[] = {
33 { "iso", 'i', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &option_standard,
34 N_
35 ("The ISO standard to use. Possible values: 639-2, 639-3, 639-5, 3166-1, 3166-2, 3166-3, 4217, 15924 (default: 3166-1)"),
36 N_("STANDARD") },
37 { "pathname", 'p', G_OPTION_FLAG_NONE, G_OPTION_ARG_FILENAME, &option_pathname,
38 N_("Use pathname as prefix for the data files (default: /usr/share/iso-codes/json)"),
39 N_("PATHNAME") },
40 { "locale", 'l', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &option_locale,
41 N_("Use this locale for output"),
42 N_("LOCALE") },
43 { "name", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &option_name,
44 N_("Name for the supplied codes (default)"), NULL },
45 { "official_name", 'o', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &option_officialname,
46 N_("Official name for the supplied codes. This may be the same as --name (only applies to ISO 3166-1)"),
47 NULL },
48 { "common_name", 'c', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &option_commonname,
49 N_
50 ("Common name for the supplied codes. This may be the same as --name (only applies to ISO 639-2, 639-3, and 3166-1)"),
51 NULL },
52 { "null", '0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &option_null_separator,
53 N_("Separate entries with a NULL character instead of newline"), NULL },
54 { "flag", 'f', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &option_flag_output,
55 N_("Show country flags with regional indicator symbol letters (only applies to ISO 3166-1)"), NULL },
56 { "version", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &option_version,
57 N_("Show program version and copyright"), NULL },
58 { NULL }
59 };
60
61 /**
62 * Parse the command line arguments.
63 *
64 * @param arguments Array of arguments
65 */
66 gboolean options_parse_command_line(gchar **arguments, GError **error)
67 {
68 int argc = 0;
69 GOptionContext *context;
70
71 // Ensure that there are sensible default options.
72 options_set_default_values();
73
74 context = g_option_context_new(_("[ISO codes]"));
75 g_option_context_add_main_entries(context, entries, GETTEXT_PACKAGE);
76
77 // Count the number of arguments supplied.
78 while (arguments[argc] != NULL) {
79 argc++;
80 }
81 // If the parsing fails, return with error.
82 if (!g_option_context_parse(context, &argc, &arguments, error)) {
83 return FALSE;
84 }
85 // Validate options
86 if (!options_validate(error)) {
87 return FALSE;
88 }
89 return TRUE;
90 }
91
92 /**
93 * Set default values for options.
94 */
95 void options_set_default_values(void)
96 {
97 option_standard = "3166-1";
98 option_pathname = "/usr/share/iso-codes/json";
99 option_namefield = "name";
100 option_locale = "";
101 option_null_separator = FALSE;
102 option_flag_output = FALSE;
103 option_version = FALSE;
104 }
105
106 /**
107 * Validate values of options.
108 */
109 gboolean options_validate(GError **error)
110 {
111 // Handle deprecated standards gracefully
112 if (!g_strcmp0("3166", option_standard)) {
113 // Prevent memory leaks
114 g_free(option_standard);
115 option_standard = g_strdup("3166-1");
116 // Print a small warning
117 g_printerr("isoquery: The standard 3166 is deprecated, please use 3166-1 instead.\n");
118 }
119 if (!g_strcmp0("639", option_standard)) {
120 // Prevent memory leaks
121 g_free(option_standard);
122 option_standard = g_strdup("639-2");
123 // Print a small warning
124 g_printerr("isoquery: The standard 639 is deprecated, please use 639-2 instead.\n");
125 }
126 // Check that the given standard is supported
127 int i = 0;
128 gboolean supported = FALSE;
129 gchar *supported_standards[] = { "639-2", "639-3", "639-5", "3166-1", "3166-2", "3166-3", "4217", "15924", NULL };
130 while (supported_standards[i]) {
131 if (!g_strcmp0(supported_standards[i], option_standard)) {
132 supported = TRUE;
133 break;
134 }
135 i++;
136 }
137 if (!supported) {
138 // TRANSLATORS: The placeholder is a string like "1234-5".
139 g_set_error(error, g_quark_from_string(GETTEXT_PACKAGE), 0, _("ISO standard \"%s\" is not supported."),
140 option_standard);
141 return FALSE;
142 }
143 // Set the value for the name field
144 if (option_officialname) {
145 option_namefield = "official_name";
146 }
147 if (option_commonname) {
148 option_namefield = "common_name";
149 }
150 return TRUE;
151 }
152
153 /**
154 * Construct the filename for JSON data, given the pathname and ISO standard.
155 *
156 * @return gchar * Filename, has to be free'd by the caller.
157 */
158 gchar *options_get_filename(void)
159 {
160 gchar *filename, *complete_path;
161 filename = g_strdup_printf("iso_%s.json", option_standard);
162 complete_path = g_build_filename(option_pathname, filename, NULL);
163 g_free(filename);
164 return complete_path;
165 }
166
167 /**
168 * Show program version and copyright
169 */
170 void options_show_version(void)
171 {
172 // TRANSLATORS: The placeholder is the version identifier.
173 g_printf(_("isoquery %s\n"), VERSION);
174 g_printf(_("Copyright © 2007-2026 Dr. Tobias Quathamer\n"));
175 // TRANSLATORS: Please change the uppercase words as
176 // appropriate for your language.
177 gchar *translation = _("Translation to LANGUAGE Copyright © YEAR YOUR-NAME\n");
178 if (!g_strrstr(translation, "LANGUAGE")) {
179 g_printf("%s", translation);
180 }
181 g_print("\n");
182 g_print("This program is free software: you can redistribute it and/or modify\n");
183 g_print("it under the terms of the GNU General Public License as published by\n");
184 g_print("the Free Software Foundation, either version 3 of the License, or\n");
185 g_print("(at your option) any later version.\n");
186 g_print("\n");
187 g_print("This program is distributed in the hope that it will be useful,\n");
188 g_print("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
189 g_print("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
190 g_print("GNU General Public License for more details.\n");
191 g_print("\n");
192 g_print("You should have received a copy of the GNU General Public License\n");
193 g_print("along with this program. If not, see <http://www.gnu.org/licenses/>.\n");
194 }