Put local variables footer in all c, h files.
[idzebra-moved-to-github.git] / index / main.c
1 /* $Id: main.c,v 1.129 2006-05-10 08:13:22 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 #endif
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #if HAVE_SYS_TIME_H
34 #include <sys/time.h>
35 #endif
36 #include <time.h>
37 #if HAVE_SYS_TIMES_H
38 #include <sys/times.h>
39 #endif
40
41 #include <yaz/log.h>
42 #include <yaz/options.h>
43 #include <idzebra/api.h>
44
45 char *prog;
46
47 static void filter_cb(void *cd, const char *name)
48 {
49     puts (name);
50 }
51
52 static void show_filters(ZebraService zs)
53 {
54     zebra_filter_info(zs, 0, filter_cb);
55 }
56
57 int main (int argc, char **argv)
58 {
59     int ret;
60     int cmd = 0;
61     char *arg;
62     char *configName = 0;
63     int nsections = 0;
64     int enable_commit = 1;
65     char *database = 0;
66     Res res = res_open(0, 0);
67     
68     int trans_started=0;
69 #if HAVE_SYS_TIMES_H
70     struct tms tms1, tms2;
71     double usec;
72 #endif
73 #if HAVE_SYS_TIME_H
74     struct timeval start_time, end_time;
75 #endif
76 #ifndef WIN32
77     char nbuf[100];
78 #endif
79     ZebraService zs = 0;
80     ZebraHandle zh = 0;
81
82     nmem_init ();
83
84 #ifdef WIN32
85 #else
86     sprintf(nbuf, "%.40s(%ld)", *argv, (long) getpid());
87     yaz_log_init_prefix (nbuf);
88 #endif
89 #if HAVE_SYS_TIMES_H
90     times(&tms1);
91 #endif
92 #if HAVE_SYS_TIME_H
93     gettimeofday(&start_time, 0);
94 #endif
95     prog = *argv;
96     if (argc < 2)
97     {
98         fprintf (stderr, "%s [options] command <dir> ...\n"
99         "Commands:\n"
100         " update <dir>  Update index with files below <dir>.\n"
101         "               If <dir> is empty filenames are read from stdin.\n"
102         " delete <dir>  Delete index with files below <dir>.\n"
103         " create <db>   Create database <db>\n"
104         " drop <db>     Drop database <db>\n"
105         " commit        Commit changes\n"
106         " clean         Clean shadow files\n"
107         "Options:\n"
108         " -t <type>     Index files as <type> (grs or text).\n"
109         " -c <config>   Read configuration file <config>.\n"
110         " -g <group>    Index files according to group settings.\n"
111         " -d <database> Records belong to Z39.50 database <database>.\n"
112         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
113         " -n            Don't use shadow system.\n"
114         " -s            Show analysis on stdout, but do no work.\n"
115         " -v <level>    Set logging to <level>.\n"
116         " -l <file>     Write log to <file>.\n"
117         " -L            Don't follow symbolic links.\n"
118         " -f <n>        Display information for the first <n> records.\n"
119         " -V            Show version.\n", *argv
120                  );
121         exit (1);
122     }
123     while ((ret = options("sVt:c:g:d:m:v:nf:l:L", argv, argc, &arg)) != -2)
124     {
125         if (ret == 0)
126         {
127             if(cmd == 0) /* command */
128             {
129                 if (!zs)
130                 {
131                     const char *config = configName ? configName : "zebra.cfg";
132                     zs = zebra_start_res (config, 0, res);
133                     if (!zs)
134                     {
135                         yaz_log (YLOG_FATAL, "Cannot read config %s", config);
136                         exit (1);
137                     }   
138                     zh = zebra_open (zs, 0);
139                     zebra_shadow_enable (zh, enable_commit);
140                 }
141
142                 if (database &&
143                     zebra_select_database (zh, database) == ZEBRA_FAIL)
144                 {
145                     yaz_log(YLOG_FATAL, "Could not select database %s "
146                             "errCode=%d",
147                             database, zebra_errCode(zh) );
148                     exit (1);
149                 }
150                 if (!strcmp (arg, "update"))
151                     cmd = 'u';
152                 else if (!strcmp (arg, "update1"))
153                     cmd = 'U';
154                 else if (!strcmp (arg, "update2"))
155                     cmd = 'm';
156                 else if (!strcmp (arg, "dump"))
157                     cmd = 's';
158                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
159                     cmd = 'd';
160                 else if (!strcmp (arg, "init"))
161                 {
162                     zebra_init (zh);
163                 }
164                 else if (!strcmp(arg, "drop"))
165                 {
166                     cmd = 'D';
167                 }
168                 else if (!strcmp(arg, "create"))
169                 {
170                     cmd = 'C';
171                 }
172                 else if (!strcmp (arg, "commit"))
173                 {
174                     zebra_commit (zh);
175                 }
176                 else if (!strcmp (arg, "clean"))
177                 {
178                     zebra_clean (zh);
179                 }
180                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
181                 {
182                     zebra_register_statistics (zh,0);
183                 }
184                 else if (!strcmp (arg, "dumpdict"))
185                 {
186                     zebra_register_statistics (zh,1);
187                 }
188                 else if (!strcmp (arg, "compact"))
189                 {
190                     zebra_compact (zh);
191                 }
192                 else if (!strcmp (arg, "filters"))
193                 {
194                     show_filters(zs);
195                 }
196                 else
197                 {
198                     yaz_log (YLOG_FATAL, "unknown command: %s", arg);
199                     exit (1);
200                 }
201             }
202             else
203             {
204                 ZEBRA_RES res = ZEBRA_OK;
205                 if (!trans_started)
206                 {
207                     trans_started=1;
208                     if (zebra_begin_trans (zh, 1) != ZEBRA_OK)
209                         exit(1);
210                 }
211                 switch (cmd)
212                 {
213                 case 'u':
214                     res = zebra_repository_update (zh, arg);
215                     break;
216                 case 'd':
217                     res = zebra_repository_delete (zh, arg);
218                     break;
219                 case 's':
220                     res = zebra_repository_show (zh, arg);
221                     nsections = 0;
222                     break;
223                 case 'C':
224                     res = zebra_create_database(zh, arg);
225                     break;
226                 case 'D':
227                     res = zebra_drop_database(zh, arg);
228                     break;
229                 default:
230                     nsections = 0;
231                 }
232                 if (res != ZEBRA_OK)
233                 {
234                     yaz_log(YLOG_WARN, "Operation failed");
235                     exit(1);
236                 }
237                 log_event_end (NULL, NULL);
238             }
239         }
240         else if (ret == 'V')
241         {
242             printf("Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
243             printf(" (C) 1994-2005, Index Data ApS\n");
244 #ifdef WIN32
245 #ifdef _DEBUG
246             printf(" WIN32 Debug\n");
247 #else
248             printf(" WIN32 Release\n");
249 #endif
250 #endif
251 #if HAVE_BZLIB_H
252             printf("Using: libbzip2, (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
253 #endif
254         }
255         else if (ret == 'v')
256             yaz_log_init_level (yaz_log_mask_str(arg));
257         else if (ret == 'l')
258             yaz_log_init_file (arg);
259         else if (ret == 'm')
260             res_set(res, "memMax", arg);
261         else if (ret == 'd')
262             database = arg;
263         else if (ret == 's')
264             res_set(res, "openRW", "0");
265         else if (ret == 'g')
266             res_set(res, "group", arg);
267         else if (ret == 'f')
268             res_set(res, "fileVerboseLimit", arg);
269         else if (ret == 'c')
270             configName = arg;
271         else if (ret == 't')
272             res_set(res, "recordType", arg);
273         else if (ret == 'n')
274             enable_commit = 0;
275         else if (ret == 'L')
276             res_set(res, "followLinks", "0");
277         else
278             yaz_log (YLOG_WARN, "unknown option '-%s'", arg);
279     } /* while arg */
280
281     if (trans_started)
282         if (zebra_end_trans (zh) != ZEBRA_OK)
283             yaz_log (YLOG_WARN, "zebra_end_trans failed");
284
285     zebra_close (zh);
286     zebra_stop (zs);
287 #if HAVE_SYS_TIMES_H
288 #if HAVE_SYS_TIME_H
289     if (trans_started)
290     {
291         gettimeofday(&end_time, 0);
292         usec = (end_time.tv_sec - start_time.tv_sec) * 1000000.0 +
293             end_time.tv_usec - start_time.tv_usec;
294         times(&tms2);
295         yaz_log (YLOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
296                 usec / 1000000,
297                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
298                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
299     }
300 #endif
301 #endif
302     nmem_exit();
303     exit (0);
304     return 0;
305 }
306
307 /*
308  * Local variables:
309  * c-basic-offset: 4
310  * indent-tabs-mode: nil
311  * End:
312  * vim: shiftwidth=4 tabstop=8 expandtab
313  */
314