/*- * Copyright (c) 2004 Jacques A. Vidrine * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace vuxml; static int realmain(int ac, char *av[]); static void usage(); int main(int ac, char *av[]) { try { return realmain(ac, av); } catch (except &e) { LOG << e.what() << '\n'; return 1; } catch (std::exception &e) { LOG << "Exception: " << e.what() << '\n'; return 100; } catch (...) { LOG << "Exception.\n"; return 101; } return 0; } static int realmain(int ac, char *av[]) { int ch; bool do_all = false; std::string output_format("text"); std::string from_file; std::string directory; opterr = 0; while ((ch = getopt(ac, av, "ad:f:t:")) != -1) switch (ch) { case 'a': do_all = true; break; case 'd': directory = optarg; break; case 't': output_format = optarg; break; case 'f': from_file = optarg; break; case 'h': case 'H': case '?': default: usage(); return 1; } ac -= optind; av += optind; if (ac < 1) { usage(); return 1; } std::vector args(&av[1], &av[ac]); if (from_file.size() > 0) { args.resize(0); std::string arg; std::istream *in; std::ifstream infile; if (from_file == "-" || from_file == "/dev/stdin") in = &std::cin; else { in = &infile; infile.open(from_file.c_str(), std::ios::in); int sverrno = errno; if (infile.fail()) { LOG << "Failed to open file `" << from_file << "' for reading.\n"; LOG << "Possible reason: " << strerror(sverrno) << '\n'; return 1; } } while (*in >> arg) args.push_back(arg); } std::ifstream db; db.open(av[0], std::ios::in|std::ios::binary); int sverrno = errno; if (db.fail()) { LOG << "Failed to open file `" << av[0] << "' for reading.\n"; LOG << "Possible reason: " << strerror(sverrno) << '\n'; return 1; } std::auto_ptr writer; if (output_format == "text") writer.reset(new TextWriter(std::cout)); else if (output_format == "vuxml") writer.reset(new VuXMLWriter(std::cout)); else if (output_format == "xhtml") writer.reset(new XHTMLWriter(std::cout, 1)); else if (output_format == "xhtml-files") writer.reset(new XHTMLFilesWriter(directory)); else { LOG << "Unknown output format `" << output_format << "'\n"; usage(); return 1; } std::auto_ptr matcher; if (do_all) matcher.reset(new AllMatcher(*writer)); else matcher.reset(new VersionMatcher(args, *writer)); matcher->start(); BasicHandler handler(*matcher); Parser parser; parser.setHandler(handler); parser.parseStream(db); matcher->end(); if (parser.fail()) { LOG << "Parsing failed @ line " << parser.getLine() << ":\n" << parser.getErrorString() << '\n'; return 1; } return 0; } static void usage() { LOG << "VuXML Query Tool (" << PACKAGE_NAME << ") " << PACKAGE_VERSION << "\n" << "Report bugs to " << PACKAGE_BUGREPORT << "\n\n" << "Usage: " << getprogname() << " [options] {filename} {package1} {package2} ... {packageN}\n" << "Options:\n" << " -a Output *all* entries.\n" << " -d {dir} Write output files into specified directory.\n" << " -f {file} Read package names from specified file.\n" << " -h Display this usage summary.\n" << " -t {format} Choose output format.\n" << "Formats: text, vuxml, xhtml, xhtml-files\n"; }