From: Bruno Haible Date: Thu, 25 Oct 2001 09:32:36 +0000 (+0000) Subject: New program 'urlget'. X-Git-Tag: v0.11~412 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3b3589efdc60a2709f56e6b67480bb67d39eaa4d;p=thirdparty%2Fgettext.git New program 'urlget'. --- diff --git a/src/ChangeLog b/src/ChangeLog index 61dff26cc..3b6f05a2b 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,14 @@ +2001-10-21 Bruno Haible + + * gnu/gettext/GetURL.java: New file. + * urlget.c: New file. + * Makefile.am (noinst_PROGRAMS): Add urlget. + (urlget_SOURCES): New variable. + (install-exec-local): Also install urlget. + (uninstall-local): Also uninstall urlget. + (gnu/gettext/GetURL.class): New rule. + (gettext.jar): Also include gnu/gettext/GetURL*.class. + 2001-10-08 Bruno Haible * msgl-english.h: New file. diff --git a/src/Makefile.am b/src/Makefile.am index 347ef1b88..ba1930a8c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -23,7 +23,7 @@ bin_PROGRAMS = gettext ngettext \ msgcmp msgfmt msgmerge msgunfmt xgettext \ msgattrib msgcat msgcomm msgconv msgen msgexec msggrep msguniq -noinst_PROGRAMS = hostname +noinst_PROGRAMS = hostname urlget noinst_HEADERS = pos.h message.h po-gram.h po-hash.h po-charset.h po-lex.h \ po.h open-po.h read-po.h str-list.h write-po.h dir-list.h file-list.h \ @@ -81,6 +81,7 @@ msgexec_SOURCES = msgexec.c $(COMMON_SOURCE) msgl-ascii.c write-po.c read-po msggrep_SOURCES = msggrep.c $(COMMON_SOURCE) msgl-ascii.c write-po.c read-po.c msgl-charset.c msguniq_SOURCES = msguniq.c $(COMMON_SOURCE) msgl-ascii.c write-po.c read-po.c msgl-iconv.c msgl-cat.c hostname_SOURCES = hostname.c +urlget_SOURCES = urlget.c # Link dependencies. # po-lex.c and po.c may need -liconv. @@ -121,6 +122,7 @@ DISTCLEANFILES = po-gram-gen2.h user-email install-exec-local: $(mkinstalldirs) $(DESTDIR)$(libdir)/$(PACKAGE) $(INSTALL_PROGRAM_ENV) $(LIBTOOL) --mode=install $(INSTALL_PROGRAM) hostname$(EXEEXT) $(DESTDIR)$(libdir)/$(PACKAGE)/hostname$(EXEEXT) + $(INSTALL_PROGRAM_ENV) $(LIBTOOL) --mode=install $(INSTALL_PROGRAM) urlget$(EXEEXT) $(DESTDIR)$(libdir)/$(PACKAGE)/urlget$(EXEEXT) $(INSTALL_SCRIPT) user-email $(DESTDIR)$(libdir)/$(PACKAGE)/user-email installdirs-local: @@ -128,6 +130,7 @@ installdirs-local: uninstall-local: $(RM) $(DESTDIR)$(libdir)/$(PACKAGE)/hostname$(EXEEXT) + $(RM) $(DESTDIR)$(libdir)/$(PACKAGE)/urlget$(EXEEXT) $(RM) $(DESTDIR)$(libdir)/$(PACKAGE)/user-email @@ -140,8 +143,11 @@ all-java-yes: gettext.jar gnu/gettext/DumpResource.class: $(srcdir)/gnu/gettext/DumpResource.java $(JAVACOMP) -d . $(srcdir)/gnu/gettext/DumpResource.java -gettext.jar: gnu/gettext/DumpResource.class - $(JAR) cf $@ gnu/gettext/DumpResource*.class +gnu/gettext/GetURL.class: $(srcdir)/gnu/gettext/GetURL.java + $(JAVACOMP) -d . $(srcdir)/gnu/gettext/GetURL.java + +gettext.jar: gnu/gettext/DumpResource.class gnu/gettext/GetURL.class + $(JAR) cf $@ gnu/gettext/DumpResource*.class gnu/gettext/GetURL*.class CLEANFILES = gettext.jar gnu/gettext/*.class diff --git a/src/gnu/gettext/GetURL.java b/src/gnu/gettext/GetURL.java new file mode 100644 index 000000000..bd4b27544 --- /dev/null +++ b/src/gnu/gettext/GetURL.java @@ -0,0 +1,85 @@ +/* Fetch an URL's contents. + * Copyright (C) 2001 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 + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package gnu.gettext; + +import java.io.*; +import java.net.*; + +/** + * @author Bruno Haible + */ +public class GetURL { + // Use a separate thread to signal a timeout error if the URL cannot + // be accessed and completely read within a given amount of time. + private static long timeout = 30*1000; // 30 seconds + private boolean done; + private Thread timeoutThread; + public void fetch (String s) { + URL url; + try { + url = new URL(s); + } catch (MalformedURLException e) { + 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() { + public void run () { + try { + sleep(timeout); + if (!done) { + System.err.println(" timed out."); + System.exit(1); + } + } catch (InterruptedException e) { + } + } + }; + timeoutThread.start(); + try { + InputStream istream = new BufferedInputStream(url.openStream()); + OutputStream ostream = new BufferedOutputStream(System.out); + for (;;) { + int b = istream.read(); + if (b < 0) break; + ostream.write(b); + } + ostream.close(); + System.out.flush(); + 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) + System.exit(1); + (new GetURL()).fetch(args[0]); + System.exit(0); + } +} diff --git a/src/urlget.c b/src/urlget.c new file mode 100644 index 000000000..2c2d7779e --- /dev/null +++ b/src/urlget.c @@ -0,0 +1,269 @@ +/* Get the contents of an URL. + Copyright (C) 2001 Free Software Foundation, Inc. + Written by Bruno Haible , 2001. + + 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 + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include "error.h" +#include "progname.h" +#include "basename.h" +#include "full-write.h" +#include "execute.h" +#include "javaexec.h" +#include "system.h" +#include "libgettext.h" + +#define _(str) gettext (str) + +/* On reasonable systems, binary I/O is the default. */ +#ifndef O_BINARY +# define O_BINARY 0 +#endif + +#ifndef STDOUT_FILENO +# define STDOUT_FILENO 1 +#endif + + +/* Only high-level toolkits, written in languages with exception handling, + have an URL datatype and operations to fetch an URL's contents. Such + toolkits are Java (class java.net.URL), Qt (classes QUrl and QUrlOperator). + We use the Java toolkit. + Note that this program doesn't handle redirection pages; programs which + which to process HTML redirection tags need to include a HTML parser, + and only full-fledged browsers like w3m, lynx, links have have both + an URL fetcher (which covers at least the protocols "http", "ftp", "file") + and a HTML parser. */ + + +/* Long options. */ +static const struct option long_options[] = +{ + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } +}; + + +/* Prototypes for local functions. Needed to ensure compiler checking of + function argument counts despite of K&R C function definition syntax. */ +static void usage PARAMS ((int status)) +#if defined __GNUC__ && ((__GNUC__ == 2 && __GNUC_MINOR__ >= 5) || __GNUC__ > 2) + __attribute__ ((noreturn)) +#endif +; +static void cat_file PARAMS ((const char *src_filename)); +static bool execute_it PARAMS ((const char *progname, + const char *prog_path, char **prog_argv, + void *private_data)); +static void fetch PARAMS ((const char *url, const char *file)); + + +int +main (argc, argv) + int argc; + char *argv[]; +{ + int optchar; + bool do_help; + bool do_version; + + /* Set program name for messages. */ + set_program_name (argv[0]); + error_print_progname = maybe_print_progname; + +#ifdef HAVE_SETLOCALE + /* Set locale via LC_ALL. */ + setlocale (LC_ALL, ""); +#endif + + /* Set the text message domain. */ + bindtextdomain (PACKAGE, LOCALEDIR); + textdomain (PACKAGE); + + /* Set default values for variables. */ + do_help = false; + do_version = false; + + /* Parse command line options. */ + while ((optchar = getopt_long (argc, argv, "hV", long_options, NULL)) != EOF) + switch (optchar) + { + case '\0': /* Long option. */ + break; + case 'h': + do_help = true; + break; + case 'V': + do_version = true; + break; + default: + usage (EXIT_FAILURE); + /* NOTREACHED */ + } + + /* Version information requested. */ + if (do_version) + { + printf ("%s (GNU %s) %s\n", basename (program_name), PACKAGE, VERSION); + /* xgettext: no-wrap */ + printf (_("Copyright (C) %s Free Software Foundation, Inc.\n\ +This is free software; see the source for copying conditions. There is NO\n\ +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ +"), + "2001"); + printf (_("Written by %s.\n"), "Bruno Haible"); + exit (EXIT_SUCCESS); + } + + /* Help is requested. */ + if (do_help) + usage (EXIT_SUCCESS); + + /* Test argument count. */ + if (optind + 2 != argc) + error (EXIT_FAILURE, 0, _("expected two arguments")); + + /* Fetch the contents. */ + fetch (argv[optind], argv[optind + 1]); + + exit (EXIT_SUCCESS); +} + +/* Display usage information and exit. */ +static void +usage (status) + int status; +{ + if (status != EXIT_SUCCESS) + fprintf (stderr, _("Try `%s --help' for more information.\n"), + program_name); + else + { + /* xgettext: no-wrap */ + printf (_("\ +Usage: %s [OPTION] URL FILE\n\ +"), program_name); + printf ("\n"); + /* xgettext: no-wrap */ + printf (_("\ +Fetches and outputs the contents of an URL. If the URL cannot be accessed,\n\ +the locally accessible FILE is used instead.\n\ +")); + printf ("\n"); + /* xgettext: no-wrap */ + printf (_("\ +Informative output:\n\ + -h, --help display this help and exit\n\ + -V, --version output version information and exit\n\ +")); + printf ("\n"); + fputs (_("Report bugs to .\n"), + stdout); + } + + exit (status); +} + +/* Copy a file's contents to stdout. */ +static void +cat_file (src_filename) + const char *src_filename; +{ + int src_fd; + char buf[4096]; + const int buf_size = sizeof (buf); + + src_fd = open (src_filename, O_RDONLY | O_BINARY); + if (src_fd < 0) + error (EXIT_FAILURE, errno, _("error while opening \"%s\" for reading"), + src_filename); + + for (;;) + { + ssize_t n_read = read (src_fd, buf, buf_size); + if (n_read < 0) + { +#ifdef EINTR + if (errno == EINTR) + continue; +#endif + error (EXIT_FAILURE, errno, _("error reading \"%s\""), src_filename); + } + if (n_read == 0) + break; + + if (full_write (STDOUT_FILENO, buf, n_read) < 0) + error (EXIT_FAILURE, errno, _("error writing stdout")); + } + + if (close (src_fd) < 0) + error (EXIT_FAILURE, errno, _("error after reading \"%s\""), src_filename); +} + +static bool +execute_it (progname, prog_path, prog_argv, private_data) + const char *progname; + const char *prog_path; + char **prog_argv; + void *private_data; +{ + (void) private_data; + + return execute (progname, prog_path, prog_argv, true, false, false, false) + != 0; +} + +/* Fetch the URL. Upon error, use the FILE as fallback. */ +static void +fetch (url, file) + const char *url; + const char *file; +{ + const char *class_name = "gnu.gettext.GetURL"; + const char *gettextjar; + const char *args[2]; + + /* Make it possible to override the gettext.jar location. This is + necessary for running the testsuite before "make install". */ + gettextjar = getenv ("GETTEXTJAR"); + if (gettextjar == NULL || gettextjar[0] == '\0') + gettextjar = GETTEXTJAR; + + /* Prepare arguments. */ + args[0] = url; + args[1] = NULL; + + /* Fetch the URL's contents. */ + if (execute_java_class (class_name, &gettextjar, 1, true, + args, + false, + execute_it, NULL)) + /* Use the file as fallback. */ + cat_file (file); +}