Added include stdlib.h a few places to get prototype for atoi/exit/..
[idzebra-moved-to-github.git] / index / main.c
1 /* $Id: main.c,v 1.122 2005-01-16 23:14:57 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #ifdef WIN32
28 #include <io.h>
29 #else
30 #include <unistd.h>
31 #include <sys/time.h>
32 #endif
33 #include <time.h>
34 #if HAVE_SYS_TIMES_H
35 #include <sys/times.h>
36 #endif
37
38 #include <yaz/log.h>
39 #include <yaz/options.h>
40 #include <idzebra/api.h>
41
42 char *prog;
43
44 static void filter_cb(void *cd, const char *name)
45 {
46     puts (name);
47 }
48
49 static void show_filters(ZebraService zs)
50 {
51     zebra_filter_info(zs, 0, filter_cb);
52 }
53
54 int main (int argc, char **argv)
55 {
56     int ret;
57     int cmd = 0;
58     char *arg;
59     char *configName = 0;
60     int nsections = 0;
61     int enable_commit = 1;
62     char *database = "Default";
63     Res res = res_open(0, 0, 0);
64     
65     int trans_started=0;
66 #if HAVE_SYS_TIMES_H
67     struct tms tms1, tms2;
68     struct timeval start_time, end_time;
69     double usec;
70 #endif
71 #ifndef WIN32
72     char nbuf[100];
73 #endif
74     ZebraService zs = 0;
75     ZebraHandle zh = 0;
76
77     nmem_init ();
78
79 #ifdef WIN32
80 #else
81     sprintf(nbuf, "%.40s(%ld)", *argv, (long) getpid());
82     yaz_log_init_prefix (nbuf);
83 #endif
84 #if HAVE_SYS_TIMES_H
85     times(&tms1);
86     gettimeofday(&start_time, 0);
87 #endif
88     prog = *argv;
89     if (argc < 2)
90     {
91         fprintf (stderr, "%s [options] command <dir> ...\n"
92         "Commands:\n"
93         " update <dir>  Update index with files below <dir>.\n"
94         "               If <dir> is empty filenames are read from stdin.\n"
95         " delete <dir>  Delete index with files below <dir>.\n"
96         " create <db>   Create database <db>\n"
97         " drop <db>     Drop database <db>\n"
98         " commit        Commit changes\n"
99         " clean         Clean shadow files\n"
100         "Options:\n"
101         " -t <type>     Index files as <type> (grs or text).\n"
102         " -c <config>   Read configuration file <config>.\n"
103         " -g <group>    Index files according to group settings.\n"
104         " -d <database> Records belong to Z39.50 database <database>.\n"
105         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
106         " -n            Don't use shadow system.\n"
107         " -s            Show analysis on stdout, but do no work.\n"
108         " -v <level>    Set logging to <level>.\n"
109         " -l <file>     Write log to <file>.\n"
110         " -L            Don't follow symbolic links.\n"
111         " -f <n>        Display information for the first <n> records.\n"
112         " -V            Show version.\n", *argv
113                  );
114         exit (1);
115     }
116     while ((ret = options ("sVt:c:g:d:m:v:nf:l:L"
117                            , argv, argc, &arg)) != -2)
118     {
119         if (ret == 0)
120         {
121             if(cmd == 0) /* command */
122             {
123                 if (!zs)
124                 {
125                     const char *config = configName ? configName : "zebra.cfg";
126                     yaz_log (YLOG_LOG, "Zebra version %s %s",
127                           ZEBRAVER, ZEBRADATE);
128                     zs = zebra_start_res (config, 0, res);
129                     if (!zs)
130                     {
131                         yaz_log (YLOG_FATAL, "Cannot read config %s", config);
132                         exit (1);
133                     }   
134                     zh = zebra_open (zs);
135                     zebra_shadow_enable (zh, enable_commit);
136                 }
137
138                 if (zebra_select_database (zh, database))
139                 {
140                     yaz_log(YLOG_FATAL, "Could not select database %s errCode=%d",
141                          database, zebra_errCode(zh) );
142                     exit (1);
143                 }
144                 if (!strcmp (arg, "update"))
145                     cmd = 'u';
146                 else if (!strcmp (arg, "update1"))
147                     cmd = 'U';
148                 else if (!strcmp (arg, "update2"))
149                     cmd = 'm';
150                 else if (!strcmp (arg, "dump"))
151                     cmd = 's';
152                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
153                     cmd = 'd';
154                 else if (!strcmp (arg, "init"))
155                 {
156                     zebra_init (zh);
157                 }
158                 else if (!strcmp(arg, "drop"))
159                 {
160                     cmd = 'D';
161                 }
162                 else if (!strcmp(arg, "create"))
163                 {
164                     cmd = 'C';
165                 }
166                 else if (!strcmp (arg, "commit"))
167                 {
168                     zebra_commit (zh);
169                 }
170                 else if (!strcmp (arg, "clean"))
171                 {
172                     zebra_clean (zh);
173                 }
174                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
175                 {
176                     zebra_register_statistics (zh,0);
177                 }
178                 else if (!strcmp (arg, "dumpdict"))
179                 {
180                     zebra_register_statistics (zh,1);
181                 }
182                 else if (!strcmp (arg, "compact"))
183                 {
184                     zebra_compact (zh);
185                 }
186                 else if (!strcmp (arg, "filters"))
187                 {
188                     show_filters(zs);
189                 }
190                 else
191                 {
192                     yaz_log (YLOG_FATAL, "unknown command: %s", arg);
193                     exit (1);
194                 }
195             }
196             else
197             {
198                 if (!trans_started)
199                 {
200                     trans_started=1;
201                     if (zebra_begin_trans (zh, 1))
202                         exit(1);
203                 }
204                 switch (cmd)
205                 {
206                 case 'u':
207                     zebra_repository_update (zh, arg);
208                     break;
209                 case 'd':
210                     zebra_repository_delete (zh, arg);
211                     break;
212                 case 's':
213                     zebra_repository_show (zh, arg);
214                     nsections = 0;
215                     break;
216                 case 'C':
217                     zebra_create_database(zh, arg);
218                     break;
219                 case 'D':
220                     zebra_drop_database(zh, arg);
221                     break;
222                 default:
223                     nsections = 0;
224                 }
225                 log_event_end (NULL, NULL);
226             }
227         }
228         else if (ret == 'V')
229         {
230             printf("Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
231             printf(" (C) 1994-2004, Index Data ApS\n");
232 #ifdef WIN32
233 #ifdef _DEBUG
234             printf(" WIN32 Debug\n");
235 #else
236             printf(" WIN32 Release\n");
237 #endif
238 #endif
239 #if HAVE_BZLIB_H
240             printf("Using: libbzip2, (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
241 #endif
242         }
243         else if (ret == 'v')
244             yaz_log_init_level (yaz_log_mask_str(arg));
245         else if (ret == 'l')
246             yaz_log_init_file (arg);
247         else if (ret == 'm')
248             res_set(res, "memMax", arg);
249         else if (ret == 'd')
250             database = arg;
251         else if (ret == 's')
252             res_set(res, "openRW", "0");
253         else if (ret == 'g')
254             res_set(res, "group", arg);
255         else if (ret == 'f')
256             res_set(res, "fileVerboseLimit", arg);
257         else if (ret == 'c')
258             configName = arg;
259         else if (ret == 't')
260             res_set(res, "recordType", arg);
261         else if (ret == 'n')
262             enable_commit = 0;
263         else if (ret == 'L')
264             res_set(res, "followLinks", "0");
265         else
266             yaz_log (YLOG_WARN, "unknown option '-%s'", arg);
267     } /* while arg */
268
269     if (trans_started)
270         zebra_end_trans (zh);
271
272     zebra_close (zh);
273     zebra_stop (zs);
274 #if HAVE_SYS_TIMES_H
275     if (trans_started)
276     {
277         gettimeofday(&end_time, 0);
278         usec = (end_time.tv_sec - start_time.tv_sec) * 1000000.0 +
279             end_time.tv_usec - start_time.tv_usec;
280         times(&tms2);
281         yaz_log (YLOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
282                 usec / 1000000,
283                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
284                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
285     }
286 #endif
287     nmem_exit();
288     exit (0);
289     return 0;
290 }
291