Init winsock. Incorporate ICU. Pazpar2 runs on Windows.
[pazpar2-moved-to-github.git] / src / pazpar2.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2008 Index Data
3
4 Pazpar2 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 Pazpar2 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
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #ifdef WIN32
24 #include <winsock.h>
25 #endif
26
27 #include <signal.h>
28 #include <assert.h>
29
30 #include "pazpar2.h"
31 #include "database.h"
32 #include "settings.h"
33 #include <yaz/daemon.h>
34
35 void child_handler(void *data)
36 {
37     start_proxy();
38     init_settings();
39
40     if (*global_parameters.settings_path_override)
41         settings_read(global_parameters.settings_path_override);
42     else if (global_parameters.server->settings)
43         settings_read(global_parameters.server->settings);
44     else
45         yaz_log(YLOG_WARN, "No settings-directory specified");
46     global_parameters.odr_in = odr_createmem(ODR_DECODE);
47     global_parameters.odr_out = odr_createmem(ODR_ENCODE);
48
49
50     pazpar2_event_loop();
51
52 }
53
54 static void show_version(void)
55 {
56     char yaz_version_str[80];
57     printf("Pazpar2 " PACKAGE_VERSION "\n");
58
59     yaz_version(yaz_version_str, 0);
60
61     printf("Configuration:");
62 #if HAVE_ICU
63     printf(" icu:?");
64 #endif
65     printf(" yaz:%s", yaz_version_str);
66     printf("\n");
67     exit(0);
68 }            
69
70 #ifdef WIN32
71 static int tcpip_init (void)
72 {
73     WORD requested;
74     WSADATA wd;
75
76     requested = MAKEWORD(1, 1);
77     if (WSAStartup(requested, &wd))
78         return 0;
79     return 1;
80 }
81 #endif
82
83
84 int main(int argc, char **argv)
85 {
86     int daemon = 0;
87     int ret;
88     int log_file_in_use = 0;
89     char *arg;
90     const char *pidfile = 0;
91     const char *uid = 0;
92
93 #ifndef WIN32
94     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
95         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
96 #else
97     tcpip_init();
98 #endif
99
100     yaz_log_init_prefix("pazpar2");
101 #if YAZ_VERSIONL >= 0x03001B
102     yaz_log_xml_errors(0, YLOG_WARN);
103 #endif
104
105
106     while ((ret = options("dDf:h:l:p:t:u:VX", argv, argc, &arg)) != -2)
107     {
108         switch (ret)
109         {
110         case 'd':
111             global_parameters.dump_records = 1;
112             break;
113         case 'D':
114             daemon = 1;
115             break;
116         case 'f':
117             if (!read_config(arg))
118                 exit(1);
119             break;
120         case 'h':
121             strcpy(global_parameters.listener_override, arg);
122             break;
123         case 'l':
124             yaz_log_init_file(arg);
125             log_file_in_use = 1;
126             break;
127         case 'p':
128             pidfile = arg;
129             break;
130         case 't':
131             strcpy(global_parameters.settings_path_override, arg);
132             break;
133         case 'u':
134             uid = arg;
135             break;
136         case 'V':
137             show_version();
138         case 'X':
139             global_parameters.debug_mode = 1;
140             break;
141         default:
142             fprintf(stderr, "Usage: pazpar2\n"
143                     "    -d                      (show internal records)\n"
144                     "    -D                      Daemon mode (background)\n"
145                     "    -f configfile\n"
146                     "    -h [host:]port          (REST protocol listener)\n"
147                     "    -l file                 log to file\n"
148                     "    -p pidfile              PID file\n"
149                     "    -t settings\n"
150                     "    -u uid\n"
151                     "    -V                      show version\n"
152                     "    -X                      debug mode\n"
153                 );
154             exit(1);
155         }
156     }
157
158     yaz_log(YLOG_LOG, "Pazpar2 %s started", VERSION);
159     if (daemon && !log_file_in_use)
160     {
161         yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
162                 "mode");
163         exit(1);
164     }
165     if (!config)
166     {
167         yaz_log(YLOG_FATAL, "Load config with -f");
168         exit(1);
169     }
170     global_parameters.server = config->servers;
171
172     start_http_listener();
173     yaz_daemon("pazpar2",
174                (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
175                (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
176                child_handler, 0 /* child_data */,
177                pidfile, uid);
178     return 0;
179 }
180
181
182 /*
183  * Local variables:
184  * c-basic-offset: 4
185  * indent-tabs-mode: nil
186  * End:
187  * vim: shiftwidth=4 tabstop=8 expandtab
188  */