/* Fetch an URL's contents.
- * Copyright (C) 2001 Free Software Foundation, Inc.
+ * Copyright (C) 2001, 2008 Free Software Foundation, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
import java.net.*;
/**
+ * Fetch an URL's contents and emit it to standard output.
+ * Exit code: 0 = success
+ * 1 = failure
+ * 2 = timeout
* @author Bruno Haible
*/
public class GetURL {
System.exit(1);
return;
}
- // We always print something on stderr because the user should know
- // why we are trying to establish an internet connection.
- System.err.print("Retrieving "+s+"...");
- System.err.flush();
done = false;
timeoutThread =
new Thread() {
try {
sleep(timeout);
if (!done) {
- System.err.println(" timed out.");
- System.exit(1);
+ System.exit(2);
}
} catch (InterruptedException e) {
}
istream.close();
} catch (IOException e) {
//e.printStackTrace();
- System.err.println(" failed.");
System.exit(1);
}
done = true;
- System.err.println(" done.");
}
public static void main (String[] args) {
if (args.length != 1)
HTML parsers.] */
+/* Whether to output something on standard error.
+ This is true by default, because the user should know why we are trying to
+ establish an internet connection. Also, users get confused if a program
+ produces no output for more than 10 seconds for no apparent reason. */
+static bool verbose = true;
+
/* Long options. */
static const struct option long_options[] =
{
{ "help", no_argument, NULL, 'h' },
+ { "quiet", no_argument, NULL, 'q' },
+ { "silent", no_argument, NULL, 'q' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
do_version = false;
/* Parse command line options. */
- while ((optchar = getopt_long (argc, argv, "hV", long_options, NULL)) != EOF)
+ while ((optchar = getopt_long (argc, argv, "hqV", long_options, NULL)) != EOF)
switch (optchar)
{
case '\0': /* Long option. */
break;
- case 'h':
+ case 'h': /* --help */
do_help = true;
break;
- case 'V':
+ case 'q': /* --quiet / --silent */
+ verbose = false;
+ break;
+ case 'V': /* --version */
do_version = true;
break;
default:
-h, --help display this help and exit\n"));
printf (_("\
-V, --version output version information and exit\n"));
+ printf (_("\
+ -q, --quiet, --silent suppress progress indicators\n"));
printf ("\n");
/* TRANSLATORS: The placeholder indicates the bug-reporting address
for this package. Please add _another line_ saying
error (EXIT_FAILURE, errno, _("error after reading \"%s\""), src_filename);
}
+/* Exit code of the Java program. */
+static int java_exitcode;
+
static bool
execute_it (const char *progname,
const char *prog_path, char **prog_argv,
{
(void) private_data;
- return execute (progname, prog_path, prog_argv, true, true, false, false,
- true, false, NULL)
- != 0;
+ java_exitcode =
+ execute (progname, prog_path, prog_argv, true, true, false, false, true,
+ false, NULL);
+ /* Exit code 0 means success, 2 means timed out. */
+ return !(java_exitcode == 0 || java_exitcode == 2);
}
/* Fetch the URL. Upon error, use the FILE as fallback. */
static void
fetch (const char *url, const char *file)
{
+ if (verbose)
+ {
+ fprintf (stderr, _("Retrieving %s..."), url);
+ fflush (stderr);
+ }
+
/* First try: using Java. */
{
const char *class_name = "gnu.gettext.GetURL";
args[1] = NULL;
/* Fetch the URL's contents. */
- if (execute_java_class (class_name, &gettextjar, 1, true, gettextjexedir,
- args,
- false, true,
- execute_it, NULL) == 0)
- return;
+ java_exitcode = 127;
+ if (!execute_java_class (class_name, &gettextjar, 1, true, gettextjexedir,
+ args,
+ false, true,
+ execute_it, NULL))
+ {
+ if (verbose)
+ {
+ if (java_exitcode == 0)
+ fprintf (stderr, _(" done.\n"));
+ else if (java_exitcode == 2)
+ fprintf (stderr, _(" timed out.\n"));
+ }
+ return;
+ }
}
/* Second try: using "wget -q -O - url". */
if (exitstatus != 127)
{
if (exitstatus != 0)
- /* Use the file as fallback. */
- cat_file (file);
+ goto failed;
+ if (verbose)
+ fprintf (stderr, _(" done.\n"));
return;
}
}
if (exitstatus != 127)
{
if (exitstatus != 0)
- /* Use the file as fallback. */
- cat_file (file);
+ goto failed;
+ if (verbose)
+ fprintf (stderr, _(" done.\n"));
return;
}
}
if (exitstatus != 127)
{
if (exitstatus != 0)
- /* Use the file as fallback. */
- cat_file (file);
+ goto failed;
+ if (verbose)
+ fprintf (stderr, _(" done.\n"));
return;
}
}
}
+ failed:
+ if (verbose)
+ fprintf (stderr, _(" failed.\n"));
/* Use the file as fallback. */
cat_file (file);
}