2 * Copyright (C) 2004 Thomas Vander Stichele <thomas@apestaart.org>
4 * gst-run.c: tool to launch GStreamer tools with correct major/minor
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
34 /* global statics for option parsing */
35 static gboolean _print = FALSE;
36 static gchar *_arg_mm = NULL;
37 static gboolean _arg_list_mm = FALSE;
39 /* popt options table for the wrapper */
40 static GOptionEntry wrapper_options[] = {
41 {"print", 'p', 0, G_OPTION_ARG_NONE, &_print,
42 "print wrapped command line options", NULL},
43 {"gst-mm", 0, 0, G_OPTION_ARG_STRING, &_arg_mm,
44 "Force major/minor version", "VERSION"},
45 {"gst-list-mm", 0, 0, G_OPTION_ARG_NONE, &_arg_list_mm,
46 "List found major/minor versions", NULL},
50 /* print out the major/minor, which is the hash key */
52 hash_print_key (gchar * key, gchar * value)
54 g_print ("%s\n", (gchar *) key);
57 /* return value like strcmp, but compares major/minor numerically */
59 compare_major_minor (const gchar * first, const gchar * second)
61 gchar **firsts, **seconds;
62 gint fmaj, fmin, smaj, smin;
65 firsts = g_strsplit (first, ".", 0);
66 seconds = g_strsplit (second, ".", 0);
68 if (firsts[0] == NULL || firsts[1] == NULL) {
72 if (seconds[0] == NULL || seconds[1] == NULL) {
77 fmaj = atoi (firsts[0]);
78 fmin = atoi (firsts[1]);
79 smaj = atoi (seconds[0]);
80 smin = atoi (seconds[1]);
104 g_strfreev (seconds);
109 find_highest_version (gchar * key, gchar * value, gchar ** highest)
111 if (*highest == NULL) {
112 /* first value, so just set it */
115 if (compare_major_minor (key, *highest) > 0)
119 /* Libtool creates shell scripts named "base" that calls actual binaries as
120 * .libs/lt-base. If we detect this is a libtool script, unmangle so we
121 * find the right binaries */
123 unmangle_libtool (gchar ** dir, gchar ** base)
125 gchar *new_dir, *new_base;
132 /* We assume libtool when base starts with "lt-" and dir ends with ".libs".
133 * On Windows libtool doesn't seem to be adding "lt-" prefix. */
135 if (!g_str_has_prefix (*base, "lt-"))
139 if (!g_str_has_suffix (*dir, ".libs"))
143 new_base = g_strdup (&((*base)[3]));
145 new_base = g_strdup (*base);
147 new_dir = g_path_get_dirname (*dir);
154 /* Returns a directory path that contains the binary given as an argument.
155 * If the binary given contains a path, it gets looked for in that path.
156 * If it doesn't contain a path, it gets looked for in the standard path.
158 * The returned string is newly allocated.
161 get_dir_of_binary (const gchar * binary)
166 base = g_path_get_basename (binary);
167 dir = g_path_get_dirname (binary);
169 /* if putting these two together yields the same as binary,
170 * then we have the right breakup. If not, it's because no path was
171 * specified which caused get_basename to return "." */
172 full = g_build_filename (dir, base, NULL);
176 /* g_build_filename() should be using the last path separator used in the
177 * input according to the docs, but doesn't actually do that, so we have
178 * to fix up the result. */
182 for (tmp = (gchar *) binary + strlen (binary) - 1; tmp >= binary; tmp--) {
183 if (*tmp == '/' || *tmp == '\\') {
184 full[strlen (dir)] = *tmp;
191 if (strcmp (full, binary) != 0) {
192 if (strcmp (dir, ".") != 0) {
193 g_warning ("This should not happen, g_path_get_dirname () has changed.");
200 /* we know no path was specified, so search standard path for binary */
202 full = g_find_program_in_path (base);
204 g_warning ("This should not happen, %s not in standard path.", base);
213 dir = g_path_get_dirname (full);
219 /* Search the given directory for candidate binaries matching the base binary.
220 * Return a GHashTable of major/minor -> directory pairs
223 get_candidates (const gchar * dir, const gchar * base)
226 GError *error = NULL;
229 gchar *suffix, *copy;
232 GPatternSpec *spec, *specexe;
237 GHashTable *candidates = NULL;
239 candidates = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
241 /* compile our pattern specs */
242 pattern = g_strdup_printf ("%s-*.*", base);
243 spec = g_pattern_spec_new (pattern);
245 pattern = g_strdup_printf ("%s-*.*.exe", base);
246 specexe = g_pattern_spec_new (pattern);
249 /* get all dirs from the path and prepend with given dir */
251 path = g_strdup_printf ("%s%c%s",
252 dir, G_SEARCHPATH_SEPARATOR, g_getenv ("PATH"));
254 path = (gchar *) g_getenv ("PATH");
255 dirs = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 0);
259 /* check all of these in reverse order by winding to bottom and going up */
264 while (cur != &dirs[0]) {
266 if (!g_file_test (*cur, G_FILE_TEST_EXISTS) ||
267 !g_file_test (*cur, G_FILE_TEST_IS_DIR)) {
271 gdir = g_dir_open (*cur, 0, &error);
273 g_warning ("Could not open dir %s: %s", *cur, error->message);
274 g_error_free (error);
277 while ((entry = g_dir_read_name (gdir))) {
278 if (g_pattern_match_string (spec, entry)
279 || g_pattern_match_string (specexe, entry)) {
282 /* is it executable ? */
283 full = g_build_filename (*cur, entry, NULL);
284 if (!g_file_test (full, G_FILE_TEST_IS_EXECUTABLE)) {
290 /* strip base and dash from it */
291 suffix = g_strdup (&(entry[strlen (base) + 1]));
292 copy = g_strdup (suffix);
294 /* strip possible .exe from copy */
295 if (g_strrstr (copy, ".exe"))
296 g_strrstr (copy, ".exe")[0] = '\0';
298 /* stricter pattern check: check if it only contains digits or dots */
299 g_strcanon (copy, "0123456789.", 'X');
300 if (strstr (copy, "X")) {
306 g_hash_table_insert (candidates, suffix, g_strdup (*cur));
312 g_pattern_spec_free (spec);
317 main (int argc, char **argv)
319 GHashTable *candidates;
322 gchar *highest = NULL;
323 gchar *binary; /* actual binary we're going to run */
324 gchar *path = NULL; /* and its path */
330 dir = get_dir_of_binary (argv[0]);
331 base = g_path_get_basename (argv[0]);
333 /* parse command line options */
334 desc = g_strdup_printf ("wrapper to call versioned %s", base);
335 ctx = g_option_context_new (desc);
337 g_option_context_set_ignore_unknown_options (ctx, TRUE);
338 g_option_context_add_main_entries (ctx, wrapper_options, GETTEXT_PACKAGE);
339 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
340 g_print ("Error initializing: %s\n", err->message);
343 g_option_context_free (ctx);
345 /* unmangle libtool if necessary */
346 unmangle_libtool (&dir, &base);
349 /* remove .exe suffix, otherwise we'll be looking for gst-blah.exe-*.* */
350 if (strlen (base) > 4 && g_str_has_suffix (base, ".exe")) {
351 base[strlen (base) - 4] = '\0';
355 /* get all candidate binaries */
356 candidates = get_candidates (dir, base);
359 /* only look for 0.10, we don't want 0.11 tools to be picked up accidentally
360 * (and from 0.11 on the unversioned tools are discontinued anyway) */
362 _arg_mm = GST_MAJORMINOR;
365 /* if a version was forced, look it up in the hash table */
366 dir = g_hash_table_lookup (candidates, _arg_mm);
368 g_print ("ERROR: Major/minor %s of tool %s not found.\n", _arg_mm, base);
371 binary = g_strdup_printf ("%s-%s", base, _arg_mm);
375 /* otherwise, just look up the highest version */
377 g_hash_table_foreach (candidates, (GHFunc) find_highest_version,
381 if (highest == NULL) {
382 g_print ("ERROR: No version of tool %s found.\n", base);
385 dir = g_hash_table_lookup (candidates, highest);
386 binary = g_strdup_printf ("%s-%s", base, highest);
391 path = g_build_filename (dir, binary, NULL);
394 /* print out list of major/minors we found if asked for */
395 /* FIXME: do them in order by creating a GList of keys and sort them */
397 g_hash_table_foreach (candidates, (GHFunc) hash_print_key, NULL);
398 g_hash_table_destroy (candidates);
402 /* print out command line if asked for */
407 for (i = 0; i < argc; ++i) {
408 g_print ("%s", argv[i]);
416 if (execv (path, argv) == -1) {
417 g_warning ("Error executing %s: %s (%d)", path, g_strerror (errno), errno);