1 /* quvi
2 * Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
3 *
4 * This file is part of quvi <http://quvi.sourceforge.net/>.
5 *
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.
10 *
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.
15 *
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/>.
19 */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <locale.h>
25 #include <glib-object.h>
26 #include <glib/gi18n.h>
27 #include <quvi.h>
28
29 #include "opts.h"
30 #include "cmd.h"
31
32 const gchar *reserved_chars = "!*'();:@&=+$,/?#[]";
33
34 struct opts_s opts;
35 gint exit_status;
36 gchar *argv0;
37 gchar *cmd;
38
_setup_gettext()39 static void _setup_gettext()
40 {
41 setlocale(LC_ALL, "");
42 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
43 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
44 textdomain(GETTEXT_PACKAGE);
45 }
46
_exec_man_man(const gchar * page)47 static gint _exec_man_man(const gchar *page)
48 {
49 gchar *cmdl;
50 GError *e;
51 gint r;
52
53 cmdl = g_strdup_printf("man %s", page);
54 r = EXIT_SUCCESS;
55 e = NULL;
56
57 if (g_spawn_command_line_sync(cmdl, NULL, NULL, NULL, &e) == FALSE)
58 {
59 g_printerr(_("error: while executing: `%s': %s\n"), cmdl, e->message);
60 r = EXIT_FAILURE;
61 g_error_free(e);
62 }
63
64 g_free(cmdl);
65 return (r);
66 }
67
68 static gint _cmd_version(gint, gchar**);
69 static gint _cmd_help(gint, gchar**);
70
71 typedef gint (*cmd_cb)(gint, gchar**);
72
73 struct builtin_cmd_s
74 {
75 const gchar *cmd;
76 const gchar *descr;
77 const cmd_cb cb;
78 };
79
80 static const struct builtin_cmd_s builtin_cmds[] =
81 {
82 {"dump", N_("Query and print the property values"), cmd_dump},
83 {"get", N_("Save media stream to a file"), cmd_get},
84 {"info", N_("Inspect the configuration and the script properties"), cmd_info},
85 {"scan", N_("Scan and print the found embedded media URLs"), cmd_scan},
86 /* version */
87 {"--version", "", _cmd_version},
88 {"-v", "", _cmd_version},
89 /* help */
90 {"--help", "", _cmd_help},
91 {"help", "", _cmd_help},
92 {"-h", "", _cmd_help},
93 /* eol */
94 {NULL, NULL}
95 };
96
97 typedef struct builtin_cmd_s *builtin_cmd_t;
98
_show_man_page(const gchar * quvi_cmd)99 static gint _show_man_page(const gchar *quvi_cmd)
100 {
101 gchar *p;
102 gint r;
103
104 p = g_strdup_printf("quvi-%s", quvi_cmd);
105 r = _exec_man_man(p);
106 g_free(p);
107
108 return (r);
109 }
110
_cmd_help(gint argc,gchar ** argv)111 static gint _cmd_help(gint argc, gchar **argv)
112 {
113 builtin_cmd_t p = (builtin_cmd_t) builtin_cmds;
114
115 if (argc >1)
116 {
117 _show_man_page(argv[1]);
118 return (EXIT_SUCCESS);
119 }
120
121 g_print(_("Usage: quvi [--version] [--help] COMMAND [ARGS]\n\n"));
122 g_print(_("quvi commands are:\n"));
123
124 while (p->cmd != NULL)
125 {
126 if (g_strcmp0(p->cmd, "help") != 0
127 && g_str_has_prefix(p->cmd, "-") == FALSE)
128 {
129 g_print(" %-6s %s\n", p->cmd, p->descr);
130 }
131 ++p;
132 }
133
134 g_print(_("\nSee 'quvi help COMMAND' for more information on a "
135 "specific command.\n"));
136
137 return (EXIT_SUCCESS);
138 }
139
140 static const gchar copyr[] =
141 "Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>\n"
142 "quvi comes with ABSOLUTELY NO WARRANTY. You may redistribute copies of\n"
143 "quvi under the terms of the GNU Affero General Public License version 3\n"
144 "or later. For more information, see "
145 "<http://www.gnu.org/licenses/agpl.html>.\n\n"
146 "To contact the developers, please mail to "
147 "<quvi-devel@lists.sourceforge.net>";
148
_cmd_version(gint argc,gchar ** argv)149 static gint _cmd_version(gint argc, gchar **argv)
150 {
151 g_print("quvi %s\n built on %s for %s\n"
152 " with %s, %s\n"
153 " configuration: %s\n"
154 "libquvi %s\n built on %s for %s\n"
155 " with %s\n"
156 " configuration: %s\n"
157 "libquvi-scripts %s\n"
158 " configuration: %s\n",
159 VN, BUILD_TIME, CANONICAL_TARGET, CC, CFLAGS, BUILD_OPTS,
160 quvi_version(QUVI_VERSION),
161 quvi_version(QUVI_VERSION_BUILD_TIME),
162 quvi_version(QUVI_VERSION_BUILD_TARGET),
163 quvi_version(QUVI_VERSION_BUILD_CC_CFLAGS),
164 quvi_version(QUVI_VERSION_CONFIGURATION),
165 quvi_version(QUVI_VERSION_SCRIPTS),
166 quvi_version(QUVI_VERSION_SCRIPTS_CONFIGURATION));
167 g_printerr("\n%s\n", copyr);
168 return (EXIT_SUCCESS);
169 }
170
_run_internal_cmd(gint argc,gchar ** argv)171 static gint _run_internal_cmd(gint argc, gchar **argv)
172 {
173 builtin_cmd_t p = (builtin_cmd_t) builtin_cmds;
174 while (p->cmd != NULL)
175 {
176 if (g_strcmp0(p->cmd, cmd) == 0)
177 return (p->cb(argc, argv));
178 ++p;
179 }
180 g_printerr(_("error: `%s' is not a quvi command. See 'quvi help'.\n"), cmd);
181 return (EXIT_FAILURE);
182 }
183
_opts_free()184 static void _opts_free()
185 {
186 /* core */
187
188 g_free(opts.core.subtitle_export_format);
189 g_free(opts.core.subtitle_language);
190 g_free(opts.core.print_format);
191 g_free(opts.core.verbosity);
192 g_free(opts.core.stream);
193
194 /* exec */
195
196 g_strfreev(opts.exec.external);
197
198 /* get */
199
200 g_strfreev(opts.get.output_regex);
201 g_free(opts.get.output_name);
202 g_free(opts.get.output_file);
203 g_free(opts.get.output_dir);
204
205 /* http */
206
207 g_free(opts.http.user_agent);
208
209 /* other */
210
211 g_strfreev(opts.rargs);
212
213 memset(&opts, 0, sizeof(struct opts_s));
214 }
215
_cleanup()216 static gint _cleanup()
217 {
218 _opts_free();
219
220 g_free(argv0);
221 argv0 = NULL;
222
223 g_free(cmd);
224 cmd = NULL;
225
226 return (exit_status);
227 }
228
main(gint argc,gchar ** argv)229 gint main(gint argc, gchar **argv)
230 {
231 exit_status = EXIT_SUCCESS;
232 argv0 = NULL;
233
234 /* quvi COMMAND. */
235 cmd = g_strdup(argv[1]);
236 if (cmd == NULL)
237 cmd = g_strdup("help");
238
239 argc--;
240 argv++;
241
242 _setup_gettext();
243 #if !GLIB_CHECK_VERSION(2,35,0)
244 g_type_init();
245 #endif
246
247 exit_status = _run_internal_cmd(argc, argv);
248 return (_cleanup());
249 }
250
251 /* vim: set ts=2 sw=2 tw=72 expandtab: */
252