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/>.
25 #include <glib-object.h>
26 #include <glib/gi18n.h>
32 const gchar
*reserved_chars
= "!*'();:@&=+$,/?#[]";
39 static void _setup_gettext()
41 setlocale(LC_ALL
, "");
42 bindtextdomain(GETTEXT_PACKAGE
, LOCALEDIR
);
43 bind_textdomain_codeset(GETTEXT_PACKAGE
, "UTF-8");
44 textdomain(GETTEXT_PACKAGE
);
47 static gint
_exec_man_man(const gchar
*page
)
53 cmdl
= g_strdup_printf("man %s", page
);
57 if (g_spawn_command_line_sync(cmdl
, NULL
, NULL
, NULL
, &e
) == FALSE
)
59 g_printerr(_("error: while executing: `%s': %s\n"), cmdl
, e
->message
);
68 static gint
_cmd_version(gint
, gchar
**);
69 static gint
_cmd_help(gint
, gchar
**);
71 typedef gint (*cmd_cb
)(gint
, gchar
**);
80 static const struct builtin_cmd_s builtin_cmds
[] =
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
},
87 {"--version", "", _cmd_version
},
88 {"-v", "", _cmd_version
},
90 {"--help", "", _cmd_help
},
91 {"help", "", _cmd_help
},
92 {"-h", "", _cmd_help
},
97 typedef struct builtin_cmd_s
*builtin_cmd_t
;
99 static gint
_show_man_page(const gchar
*quvi_cmd
)
104 p
= g_strdup_printf("quvi-%s", quvi_cmd
);
105 r
= _exec_man_man(p
);
111 static gint
_cmd_help(gint argc
, gchar
**argv
)
113 builtin_cmd_t p
= (builtin_cmd_t
) builtin_cmds
;
117 _show_man_page(argv
[1]);
118 return (EXIT_SUCCESS
);
121 g_print(_("Usage: quvi [--version] [--help] COMMAND [ARGS]\n\n"));
122 g_print(_("quvi commands are:\n"));
124 while (p
->cmd
!= NULL
)
126 if (g_strcmp0(p
->cmd
, "help") != 0
127 && g_str_has_prefix(p
->cmd
, "-") == FALSE
)
129 g_print(" %-6s %s\n", p
->cmd
, p
->descr
);
134 g_print(_("\nSee 'quvi help COMMAND' for more information on a "
135 "specific command.\n"));
137 return (EXIT_SUCCESS
);
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>";
149 static gint
_cmd_version(gint argc
, gchar
**argv
)
151 g_print("quvi %s\n built on %s for %s\n"
153 " configuration: %s\n"
154 "libquvi %s\n built on %s for %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
);
171 static gint
_run_internal_cmd(gint argc
, gchar
**argv
)
173 builtin_cmd_t p
= (builtin_cmd_t
) builtin_cmds
;
174 while (p
->cmd
!= NULL
)
176 if (g_strcmp0(p
->cmd
, cmd
) == 0)
177 return (p
->cb(argc
, argv
));
180 g_printerr(_("error: `%s' is not a quvi command. See 'quvi help'.\n"), cmd
);
181 return (EXIT_FAILURE
);
184 static void _opts_free()
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
);
196 g_strfreev(opts
.exec
.external
);
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
);
207 g_free(opts
.http
.user_agent
);
211 g_strfreev(opts
.rargs
);
213 memset(&opts
, 0, sizeof(struct opts_s
));
216 static gint
_cleanup()
226 return (exit_status
);
229 gint
main(gint argc
, gchar
**argv
)
231 exit_status
= EXIT_SUCCESS
;
235 cmd
= g_strdup(argv
[1]);
237 cmd
= g_strdup("help");
243 #if !GLIB_CHECK_VERSION(2,35,0)
247 exit_status
= _run_internal_cmd(argc
, argv
);
251 /* vim: set ts=2 sw=2 tw=72 expandtab: */