Update NEWS for v0.9.2
[quvi-tool.git] / src / opts.c
blob186258be93cfa0c7995c1b7b995372c44f9e328b
1 /* quvi
2 * Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of quvi <http://quvi.sourceforge.net/>.
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <stdlib.h>
24 #include <glib/gi18n.h>
26 #include "lopts.h"
27 #include "lutil.h"
28 #include "opts.h"
30 const GOptionEntry option_entries[] =
32 /* core */
34 "check-mode-offline", 'o', 0, G_OPTION_ARG_NONE,
35 &opts.core.check_mode_offline, NULL, NULL
38 "print-format", 'p', 0, G_OPTION_ARG_STRING, &opts.core.print_format,
39 NULL, NULL
42 "print-subtitles", 'B', 0, G_OPTION_ARG_NONE, &opts.core.print_subtitles,
43 NULL, NULL
46 "print-streams", 'S', 0, G_OPTION_ARG_NONE, &opts.core.print_streams,
47 NULL, NULL
50 "subtitle-export-format", 'L', 0, G_OPTION_ARG_STRING,
51 &opts.core.subtitle_export_format, NULL, NULL
54 "subtitle-language", 'l', 0, G_OPTION_ARG_STRING,
55 &opts.core.subtitle_language, NULL, NULL
58 "stream", 's', 0, G_OPTION_ARG_STRING, &opts.core.stream,
59 NULL, NULL
62 "verbosity", 'b', 0, G_OPTION_ARG_STRING, &opts.core.verbosity,
63 NULL, NULL
65 /* dump */
67 "query-metainfo", 'q', 0, G_OPTION_ARG_NONE, &opts.dump.query_metainfo,
68 NULL, NULL
70 /* exec */
72 "exec-enable-stderr", 'E', 0, G_OPTION_ARG_NONE, &opts.exec.enable_stderr,
73 NULL, NULL
76 "exec-enable-stdout", 'O', 0, G_OPTION_ARG_NONE, &opts.exec.enable_stdout,
77 NULL, NULL
80 "exec-dump-argv", 'A', 0, G_OPTION_ARG_NONE, &opts.exec.dump_argv,
81 NULL, NULL
84 "exec", 'e', 0, G_OPTION_ARG_STRING_ARRAY, &opts.exec.external,
85 NULL, NULL
87 /* get */
89 "output-regex", 'g', 0, G_OPTION_ARG_STRING_ARRAY, &opts.get.output_regex,
90 NULL, NULL
93 "output-file", 'f', 0, G_OPTION_ARG_STRING, &opts.get.output_file,
94 NULL, NULL
97 "output-name", 'n', 0, G_OPTION_ARG_STRING, &opts.get.output_name,
98 NULL, NULL
101 "output-dir", 'i', 0, G_OPTION_ARG_STRING, &opts.get.output_dir,
102 NULL, NULL
105 "overwrite", 'w', 0, G_OPTION_ARG_NONE, &opts.get.overwrite,
106 NULL, NULL
109 "skip-transfer", 'k', 0, G_OPTION_ARG_NONE, &opts.get.skip_transfer,
110 NULL, NULL
113 "resume-from", 'r', 0, G_OPTION_ARG_DOUBLE, &opts.get.resume_from,
114 NULL, NULL
117 "throttle", 't', 0, G_OPTION_ARG_INT, &opts.get.throttle,
118 NULL, NULL
120 /* http */
122 "user-agent", 'u', 0, G_OPTION_ARG_STRING, &opts.http.user_agent,
123 NULL, NULL
125 /* remaining */
127 G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &opts.rargs,
128 NULL, NULL
130 {NULL, 0, 0, 0, NULL, NULL, NULL}
133 static const gchar *dumpformat_possible_values[] =
135 "enum",
136 "rfc2483", /* playlist specific */
137 #ifdef HAVE_JSON_GLIB
138 "json",
139 #endif
140 #ifdef HAVE_LIBXML
141 "xml",
142 #endif
143 NULL
146 static gint cb_chk_str(const gchar *fpath, const gchar *opt_name,
147 const gchar *opt_val, const gchar **ok_strv)
149 gchar *inv_val;
150 if (lopts_chk_str(opt_val, ok_strv, &inv_val) != EXIT_SUCCESS)
151 return (lopts_invalid_value(opt_name, fpath, inv_val, ok_strv));
152 return (EXIT_SUCCESS);
155 #ifdef _1
156 static gint cb_chk_strv(const gchar *fpath, const gchar *opt_name,
157 const gchar **opt_val, const gchar **ok_strv)
159 gchar *inv_val;
160 if (lopts_chk_strv(opt_val, ok_strv, &inv_val) != EXIT_SUCCESS)
161 return (lopts_invalid_value(opt_name, fpath, inv_val, ok_strv));
162 return (EXIT_SUCCESS);
164 #endif
166 static gint cb_chk_re(const gchar *fpath, const gchar *opt_name,
167 const gchar **opt_val)
169 gchar *inv_val;
170 if (lopts_chk_re(opt_val, &inv_val) != EXIT_SUCCESS)
171 return (lopts_invalid_value(opt_name, fpath, inv_val, NULL));
172 return (EXIT_SUCCESS);
175 static gint cb_chk_throttle(const gchar *fpath,
176 const gchar *opt_name,
177 const gint opt_val)
179 gint r = EXIT_SUCCESS;
180 if (opt_val <0)
182 gchar *s = g_strdup_printf("%d", opt_val);
183 lopts_invalid_value(opt_name, fpath, s, NULL);
184 r = EXIT_FAILURE;
185 g_free(s);
187 return (r);
190 static const gchar g_core[] = "core";
191 static const gchar g_dump[] = "dump";
192 static const gchar g_exec[] = "exec";
193 static const gchar g_http[] = "http";
194 static const gchar g_get[] = "get";
196 void cb_parse_keyfile_values(GKeyFile *kf, const gchar *fpath)
198 /* core */
200 lopts_keyfile_get_bool(kf, fpath, g_core,
201 "check-mode-offline", &opts.core.check_mode_offline);
203 lopts_keyfile_get_str(kf, cb_chk_str, fpath, g_core,
204 dumpformat_possible_values,
205 "print-format", &opts.core.print_format);
207 lopts_keyfile_get_str(kf, NULL, fpath, g_core, NULL,
208 "subtitle-export-format",
209 &opts.core.subtitle_export_format);
211 lopts_keyfile_get_str(kf, NULL, fpath, g_core, NULL,
212 "subtitle-language", &opts.core.subtitle_language);
214 lopts_keyfile_get_str(kf, NULL, fpath, g_core, NULL,
215 "stream", &opts.core.stream);
217 lopts_keyfile_get_str(kf, cb_chk_str, fpath, g_core,
218 lutil_verbosity_possible_values,
219 "verbosity", &opts.core.verbosity);
221 /* dump */
223 lopts_keyfile_get_bool(kf, fpath, g_dump,
224 "query-metainfo", &opts.dump.query_metainfo);
226 /* exec */
228 lopts_keyfile_get_bool(kf, fpath, g_exec,
229 "dump-argv", &opts.exec.dump_argv);
231 lopts_keyfile_get_bool(kf, fpath, g_exec,
232 "enable-stderr", &opts.exec.enable_stderr);
234 lopts_keyfile_get_bool(kf, fpath, g_exec,
235 "enable-stdout", &opts.exec.enable_stdout);
237 lopts_keyfile_get_strv(kf, NULL, fpath, g_exec, NULL,
238 "external", &opts.exec.external);
240 /* get */
242 lopts_keyfile_get_str(kf, NULL, fpath, g_get, NULL,
243 "output-dir", &opts.get.output_dir);
245 lopts_keyfile_get_str(kf, NULL, fpath, g_get, NULL,
246 "output-name", &opts.get.output_name);
248 lopts_keyfile_get_re(kf, cb_chk_re, fpath, g_get, NULL,
249 "output-regex", &opts.get.output_regex);
251 lopts_keyfile_get_double(kf, NULL, fpath, g_get,
252 "resume-from", &opts.get.resume_from);
254 lopts_keyfile_get_bool(kf, fpath, g_get,
255 "skip-transfer", &opts.get.skip_transfer);
257 lopts_keyfile_get_int(kf, cb_chk_throttle, fpath, g_get,
258 "throttle", &opts.get.throttle);
260 /* http */
262 lopts_keyfile_get_str(kf, NULL, fpath, g_http, NULL,
263 "user-agent", &opts.http.user_agent);
266 #define _chk_r\
267 do {\
268 if (r != EXIT_SUCCESS)\
269 return (r);\
270 } while (0)
272 gint cb_cmdline_validate_values()
274 gint r;
276 /* output */
278 r = cb_chk_str(NULL, "print-format", opts.core.print_format,
279 dumpformat_possible_values);
280 _chk_r;
282 r = cb_chk_str(NULL, "verbosity", opts.core.verbosity,
283 lutil_verbosity_possible_values);
284 _chk_r;
286 r = cb_chk_re(NULL, "output-regex", (const gchar**) opts.get.output_regex);
287 _chk_r;
289 /* get */
291 r = cb_chk_throttle(NULL, "throttle", opts.get.throttle);
292 _chk_r;
294 return (EXIT_SUCCESS);
297 #undef _chk_r
299 void cb_set_post_parse_defaults()
301 /* core */
303 if (opts.core.subtitle_export_format == NULL)
304 opts.core.subtitle_export_format = g_strdup("srt");
306 if (opts.core.verbosity == NULL)
307 opts.core.verbosity = g_strdup("verbose");
309 /* get */
311 if (opts.get.output_regex == NULL)
313 static gchar *sv[] =
315 "%t:/\\w|\\s/",
316 "%t:s/\\s\\s+/ /",
317 "%t:s/^\\s+//",
318 "%t:s/\\s+$//",
319 NULL
321 opts.get.output_regex = g_strdupv(sv);
324 if (opts.get.output_name == NULL)
325 opts.get.output_name = g_strdup("%t.%e");
327 /* http */
329 if (opts.http.user_agent == NULL)
330 opts.http.user_agent = g_strdup("Mozilla/5.0");
333 gchar *cb_get_config_fpath()
335 const gchar *s = g_getenv("QUVI_CONFIG");
336 if (s != NULL && strlen(s) >0)
337 return (g_strdup(s));
338 return (NULL);
341 /* vim: set ts=2 sw=2 tw=72 expandtab: */