Include direct.h for chdir on Windows
[metaproxy-moved-to-github.git] / src / metaproxy_prog.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2008 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20
21 #include <yaz/log.h>
22 #include <yaz/options.h>
23 #include <yaz/daemon.h>
24
25 #include <iostream>
26 #include <stdexcept>
27 #include <libxml/xinclude.h>
28
29 #include "filter.hpp"
30 #include "package.hpp"
31 #include "router_flexml.hpp"
32 #include "factory_static.hpp"
33
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #ifdef WIN32
38 #include <direct.h>
39 #endif
40
41 namespace mp = metaproxy_1;
42
43 static void handler(void *data)
44 {
45     mp::RouterFleXML *routerp = (mp::RouterFleXML*) data;
46
47     mp::Package pack;
48     pack.router(*routerp).move();
49 }
50     
51 int main(int argc, char **argv)
52 {
53     try 
54     {
55         const char *fname = 0;
56         int ret;
57         char *arg;
58         unsigned mode = 0;
59         const char *pidfile = 0;
60         const char *uid = 0;
61
62         while ((ret = options("c{config}:Dh{help}l:p:u:V{version}w:X", 
63                               argv, argc, &arg)) != -2)
64         {
65             switch (ret)
66             {
67             case 'c':
68                 fname = arg;
69                 break;
70             case 'D':
71                 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
72                 break;
73             case 'h':
74                 std::cerr << "metaproxy\n"
75                     " -h|--help     help\n"
76                     " -V|--version  version\n"
77                     " -c|--config f config filename\n"
78                     " -D            daemon and keepalive operation\n"
79                     " -l f          log file f\n"
80                     " -p f          pid file f\n"
81                     " -u id         change uid to id\n"
82                     " -w dir        changes working directory to dir\n"
83                     " -X            debug mode (no fork/daemon mode)\n"
84                           << std::endl;
85                 break;
86             case 'l':
87                 yaz_log_init_file(arg);
88                 break;
89             case 'p':
90                 pidfile = arg;
91                 break;
92             case 'u':
93                 uid = arg;
94                 break;
95             case 'V':
96                 std::cout << VERSION "\n";
97                 std::exit(0);
98                 break;
99             case 'w':
100                 if (chdir(arg)) 
101                 {
102                     std::cerr << "chdir " << arg << " failed" << std::endl;
103                     std::exit(1);
104                 }
105             case 'X':
106                 mode = YAZ_DAEMON_DEBUG;
107                 break;
108             case -1:
109                 std::cerr << "bad option: " << arg << std::endl;
110                 std::exit(1);
111             }
112         }
113         if (!fname)
114         {
115             std::cerr << "No configuration given; use -h for help\n";
116             std::exit(1);
117         }
118
119         yaz_log(YLOG_LOG, "Metaproxy " VERSION " started");
120         xmlDocPtr doc = xmlReadFile(fname,
121                                     NULL, 
122                                     XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
123                                     + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
124         
125         if (!doc)
126         {
127             std::cerr << "XML parsing failed\n";
128             std::exit(1);
129         }
130         // and perform Xinclude then
131         if (xmlXIncludeProcess(doc) > 0) {
132             std::cerr << "processing XInclude directive\n";
133         }
134         mp::FactoryStatic factory;
135         mp::RouterFleXML router(doc, factory, false);
136
137         yaz_daemon("metaproxy", mode, handler, &router, pidfile, uid);
138     }
139     catch (std::logic_error &e) {
140         std::cerr << "std::logic error: " << e.what() << "\n";
141         std::exit(1);
142     }
143     catch (std::runtime_error &e) {
144         std::cerr << "std::runtime error: " << e.what() << "\n";
145         std::exit(1);
146     }
147     catch ( ... ) {
148         std::cerr << "Unknown Exception" << std::endl;
149         std::exit(1);
150     }
151     std::exit(0);
152 }
153
154
155 /*
156  * Local variables:
157  * c-basic-offset: 4
158  * indent-tabs-mode: nil
159  * c-file-style: "stroustrup"
160  * End:
161  * vim: shiftwidth=4 tabstop=8 expandtab
162  */