Start work on ICU based regexp searches
[idzebra-moved-to-github.git] / index / zebraidx.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
4 Zebra 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 Zebra 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 #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
34 #if YAZ_HAVE_ICU
35 #include <yaz/icu.h>
36 #endif
37 #include <yaz/log.h>
38 #include <yaz/options.h>
39 #include <idzebra/version.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 = 0;
63     Res res = res_open(0, 0);
64     Res default_res = res_open(0, 0);
65     
66     int trans_started = 0;
67 #ifndef WIN32
68     char nbuf[100];
69 #endif
70     ZebraService zs = 0;
71     ZebraHandle zh = 0;
72
73 #ifdef WIN32
74 #else
75     sprintf(nbuf, "%.40s(%ld)", *argv, (long) getpid());
76     yaz_log_init_prefix(nbuf);
77 #endif
78     prog = *argv;
79     if (argc < 2)
80     {
81         fprintf(stderr, "%s [options] command <dir> ...\n"
82         "Commands:\n"
83         " update <dir>  Update index with files below <dir>.\n"
84         "               If <dir> is empty filenames are read from stdin.\n"
85         " delete <dir>  Delete index with files below <dir>.\n"
86         " create <db>   Create database <db>\n"
87         " drop <db>     Drop database <db>\n"
88         " commit        Commit changes\n"
89         " clean         Clean shadow files\n"
90         " check:mode    Check register; mode is one of: default, full, quick\n"
91         "Options:\n"
92         " -t <type>     Index files as <type> (grs or text).\n"
93         " -c <config>   Read configuration file <config>.\n"
94         " -g <group>    Index files according to group settings.\n"
95         " -d <database> Records belong to Z39.50 database <database>.\n"
96         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
97         " -n            Don't use shadow system.\n"
98         " -s            Show analysis on stdout, but do no work.\n"
99         " -v <level>    Set logging to <level>.\n"
100         " -l <file>     Write log to <file>.\n"
101         " -L            Don't follow symbolic links.\n"
102         " -f <n>        Display information for the first <n> records.\n"
103         " -V            Show version.\n", *argv
104                  );
105         exit(1);
106     }
107     res_set(default_res, "profilePath", DEFAULT_PROFILE_PATH);
108     res_set(default_res, "modulePath", DEFAULT_MODULE_PATH);
109     while ((ret = options("sVt:c:g:d:m:v:nf:l:L", argv, argc, &arg)) != -2)
110     {
111         if (ret == 0)
112         {
113             if(cmd == 0) /* command */
114             {
115                 if (!zs)
116                 {
117                     const char *config = configName ? configName : "zebra.cfg";
118                     zs = zebra_start_res(config, default_res, res);
119                     if (!zs)
120                     {
121                         yaz_log(YLOG_FATAL, "Cannot read config %s", config);
122                         exit(1);
123                     }   
124                     zh = zebra_open(zs, 0);
125                     zebra_shadow_enable(zh, enable_commit);
126                 }
127
128                 if (database &&
129                     zebra_select_database(zh, database) == ZEBRA_FAIL)
130                 {
131                     yaz_log(YLOG_FATAL, "Could not select database %s "
132                             "errCode=%d",
133                             database, zebra_errCode(zh) );
134                     exit(1);
135                 }
136                 if (!strcmp(arg, "update"))
137                     cmd = 'u';
138                 else if (!strcmp(arg, "update1"))
139                     cmd = 'U';
140                 else if (!strcmp(arg, "update2"))
141                     cmd = 'm';
142                 else if (!strcmp(arg, "dump"))
143                     cmd = 's';
144                 else if (!strcmp(arg, "del") || !strcmp(arg, "delete"))
145                     cmd = 'd';
146                 else if (!strcmp(arg, "adelete"))
147                     cmd = 'a';
148                 else if (!strcmp(arg, "init"))
149                 {
150                     zebra_init(zh);
151                 }
152                 else if (!strcmp(arg, "drop"))
153                 {
154                     cmd = 'D';
155                 }
156                 else if (!strcmp(arg, "create"))
157                 {
158                     cmd = 'C';
159                 }
160                 else if (!strcmp(arg, "commit"))
161                 {
162                     zebra_commit(zh);
163                 }
164                 else if (!strcmp(arg, "clean"))
165                 {
166                     zebra_clean(zh);
167                 }
168                 else if (!strcmp(arg, "stat") || !strcmp(arg, "status"))
169                 {
170                     zebra_register_statistics(zh, 0);
171                 }
172                 else if (!strcmp(arg, "dumpdict"))
173                 {
174                     zebra_register_statistics(zh, 1);
175                 }
176                 else if (!strcmp(arg, "compact"))
177                 {
178                     zebra_compact(zh);
179                 }
180                 else if (!strcmp(arg, "filters"))
181                 {
182                     show_filters(zs);
183                 }
184                 else if (!strncmp(arg, "check", 5))
185                 {
186                     const char *spec = 0;
187                     if (arg[5] == ':')
188                         spec = arg + 6;
189                     else if (arg[5] != '\0')
190                     {
191                         yaz_log(YLOG_FATAL, "missing colon after check");
192                         exit(1);
193                     }
194                     if (zebra_register_check(zh, spec) != ZEBRA_OK)
195                     {
196                         yaz_log(YLOG_WARN, "zebra_register_check failed");
197                     }
198                 }
199                 else
200                 {
201                     yaz_log(YLOG_FATAL, "unknown command: %s", arg);
202                     exit(1);
203                 }
204             }
205             else
206             {
207                 ZEBRA_RES res = ZEBRA_OK;
208                 if (!trans_started)
209                 {
210                     trans_started=1;
211                     if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
212                         exit(1);
213                 }
214                 switch (cmd)
215                 {
216                 case 'u':
217                     res = zebra_repository_index(zh, arg, action_update);
218                     break;
219                 case 'd':
220                     res = zebra_repository_index(zh, arg, action_delete);
221                     break;
222                 case 'a':
223                     res = zebra_repository_index(zh, arg, action_a_delete);
224                     break;
225                 case 's':
226                     res = zebra_repository_show(zh, arg);
227                     nsections = 0;
228                     break;
229                 case 'C':
230                     res = zebra_create_database(zh, arg);
231                     break;
232                 case 'D':
233                     res = zebra_drop_database(zh, arg);
234                     break;
235                 default:
236                     nsections = 0;
237                 }
238                 if (res != ZEBRA_OK)
239                 {
240                     const char *add = zebra_errAdd(zh);
241                     yaz_log(YLOG_FATAL, "Operation failed: %s %s",
242                             zebra_errString(zh), add ? add : "");
243                     
244                     if (trans_started)
245                         if (zebra_end_trans(zh) != ZEBRA_OK)
246                             yaz_log(YLOG_WARN, "zebra_end_trans failed");
247
248
249                     zebra_close(zh);
250                     zebra_stop(zs);
251                     exit(1);
252                 }
253                 log_event_end(NULL, NULL);
254             }
255         }
256         else if (ret == 'V')
257         {
258             char version_str[20];
259             char sys_str[80];
260             zebra_get_version(version_str, sys_str);
261
262             printf("Zebra %s\n", version_str);
263             printf("(C) 1994-2010, Index Data ApS\n");
264             printf("Zebra is free software, covered by the GNU General Public License, and you are\n");
265             printf("welcome to change it and/or distribute copies of it under certain conditions.\n");
266             printf("SHA1 ID: %s\n", sys_str);
267             if (strcmp(version_str, ZEBRAVER))
268                 printf("zebraidx compiled version %s\n", ZEBRAVER);
269 #if YAZ_HAVE_ICU
270             printf("Using ICU\n");
271 #endif
272         }
273         else if (ret == 'v')
274             yaz_log_init_level(yaz_log_mask_str(arg));
275         else if (ret == 'l')
276             yaz_log_init_file(arg);
277         else if (ret == 'm')
278             res_set(res, "memMax", arg);
279         else if (ret == 'd')
280             database = arg;
281         else if (ret == 's')
282             res_set(res, "openRW", "0");
283         else if (ret == 'g')
284             res_set(res, "group", arg);
285         else if (ret == 'f')
286             res_set(res, "fileVerboseLimit", arg);
287         else if (ret == 'c')
288             configName = arg;
289         else if (ret == 't')
290             res_set(res, "recordType", arg);
291         else if (ret == 'n')
292             enable_commit = 0;
293         else if (ret == 'L')
294             res_set(res, "followLinks", "0");
295         else
296             yaz_log(YLOG_WARN, "unknown option '-%s'", arg);
297     } /* while arg */
298
299     if (trans_started)
300         if (zebra_end_trans(zh) != ZEBRA_OK)
301             yaz_log(YLOG_WARN, "zebra_end_trans failed");
302
303     zebra_close(zh);
304     zebra_stop(zs);
305
306     res_close(res);
307     res_close(default_res);
308     exit(0);
309     return 0;
310 }
311
312 /*
313  * Local variables:
314  * c-basic-offset: 4
315  * c-file-style: "Stroustrup"
316  * indent-tabs-mode: nil
317  * End:
318  * vim: shiftwidth=4 tabstop=8 expandtab
319  */
320