File: lexholder.c

package info (click to toggle)
freespeech 1.0m-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,640 kB
  • sloc: ansic: 5,315; perl: 202; makefile: 114
file content (271 lines) | stat: -rw-r--r-- 7,451 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* lexholder.c
 *
 * Lexicon database holding utility.
 */

/*
 * Copyright (C) 2010 Igor B. Poretsky <poretsky@mlbox.ru>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <limits.h>

#ifdef FBSD_DATABASE
#include <db.h>
#elif defined(BERKELEYDB)
#include <db_185.h>
#else
#include <ndbm.h>
#endif

#include <fcntl.h>


/* Command line error codes */
#define CL_OK 0
#define CS_CONFLICT 1
#define NO_DB_FILE 2


static const char *usage =
  "Lexical database holding utility.\n\n"

  "This utility is designed for constructing, managing, testing and querying\n"
  "lexical database providing pronunciation info for English words.\n\n"

  "Usage:\t%s [options] <db_path>\n\n"

  "When filling and updating the database, new records are read\n"
  "from the standard input. When extracting data from the database\n"
  "the result is printed to the standard output.\n"
  "This behaviour can be changed by the \"-f\" switch.\n\n"

  "If no options are specified, the program reads it's standard input\n"
  "and stores it's content in the database.\n\n"

  "The recognized options are as follows:\n\n"

  "-h -- Print this help (the only option not requiring the database path)\n"
  "-l -- List database content\n"
  "-s <word> -- Search specified word\n"
  "-d <word> -- Delete record for specified word\n"
  "-f <file> -- Use specified file instead of standard input or output\n"
  "-r -- Replace mode\n"
  "-q -- Don't print messages about ignoring duplicates\n\n";


int main(int argc, char *argv[])
{
  FILE *fp = NULL;
  void *db;
#if defined(FBSD_DATABASE) || defined(BERKELEYDB)
  DBT key, value;
#else
  datum key, value;
#endif
  char line[256];
  char *f = NULL, *d = NULL, *s = NULL;
  int i, n, q = 0, ret = NO_DB_FILE;
#if defined(FBSD_DATABASE) || defined(BERKELEYDB)
  int mode = R_NOOVERWRITE;
#else
  int mode = GDBM_INSERT;
#endif

  /* Parse command line */
  if(argc==1)
    {
      (void)fprintf(stderr, usage, argv[0]);
      return EXIT_FAILURE;
    }
  while((n = getopt(argc,argv,"f:s:d:lqrh")) != -1)
    switch(n)
      {
      case 'f':
        if (strcmp(optarg, "-"))
          f = optarg;
        break;
      case 'd':
        if (d || s) ret = CS_CONFLICT;
        else d = optarg;
        break;
      case 's':
        if (d || s) ret = CS_CONFLICT;
        else s = optarg;
        break;
      case 'l':
        if (d || s) ret = CS_CONFLICT;
        else s = line;
        break;
      case 'r':
#if defined(FBSD_DATABASE) || defined(BERKELEYDB)
        mode = 0;
#else
        mode = GDBM_REPLACE;
#endif
        break;
      case 'q':
        q = 1;
        break;
      case 'h':
        (void)fprintf(stderr, usage, argv[0]);
        return EXIT_SUCCESS;
      default:
        (void)fprintf(stderr, usage, argv[0]);
        return EXIT_FAILURE;
      }
  if (optind && argv[optind])
    ret = CL_OK;
  switch (ret)
    {
    case CS_CONFLICT:
      (void)fprintf(stderr,
                    "Ambiguous options in command line\n");
      return EXIT_FAILURE;
    case NO_DB_FILE:
      (void)fprintf(stderr, "DB file must be specified\n");
      return EXIT_FAILURE;
    default:
      break;
    }

  /* Open file for input or output if necessary */
  if (!d)
    {
      if (f)
        fp = fopen(f, s ? "w" : "r");
      else fp = s ? stdout : stdin;
      if (!fp)
        {
          (void)fprintf(stderr, "Can't open file %s\n", f);
          return EXIT_FAILURE;
        }
    }

  /* Open the database in appropriate mode */
#if defined(FBSD_DATABASE) || defined(BERKELEYDB)
  db = dbopen(argv[optind], s ? O_RDONLY : (O_RDWR | O_CREAT), 0644, DB_HASH, NULL);
#else
  db = dbm_open(argv[optind], s ? O_RDONLY : (O_RDWR | O_CREAT), 0644);
#endif
  if (!db)
    {
      (void)fprintf(stderr, "Cannot open the database %s\n", argv[optind]);
      return EXIT_FAILURE;
    }

  ret = EXIT_SUCCESS;
  if (s == line) /* List database content */
    {
#if defined(FBSD_DATABASE) || defined(BERKELEYDB)
      for (n = 0; !((DB*)db)->seq(db, &key, &value, R_NEXT); n++)
#else
      for (n = 0, key = dbm_firstkey(db); key.dptr; key = dbm_nextkey(db), n++)
#endif
        {
#if defined(FBSD_DATABASE) || defined(BERKELEYDB)
          (void)strncpy(line, key.data, key.size);
          (void)fprintf(fp, "%s %s\n", line, (char *)value.data);
#else
          value = dbm_fetch(db, key);
          (void)strncpy(line, key.dptr, key.dsize);
          (void)fprintf(fp, "%s %s\n", line, (char *)value.dptr);
#endif
        }
      (void)fprintf(stderr, "%i records extracted.\n", n);
    }
  else if (s) /* Lookup database for specified word */
    {
#if defined(FBSD_DATABASE) || defined(BERKELEYDB)
      key.data = s;
      key.size = strlen(s);
      if (((DB*)db)->get(db, &key, &value, 0))
        ret = EXIT_FAILURE;
      else (void)fprintf(fp, "%s\n", (char *)value.data);
#else
      key.dptr = s;
      key.dsize = strlen(s)+1;
      value = dbm_fetch(db, key);
      if (!value.dptr)
        ret = EXIT_FAILURE;
      else (void)fprintf(fp, "%s\n", (char *)value.dptr);
#endif
    }
  else if (d) /* Delete record for specified word */
    {
#if defined(FBSD_DATABASE) || defined(BERKELEYDB)
      key.data = d;
      key.size = strlen(d);
      ret = ((DB*)db)->del(db, &key, 0) ? EXIT_FAILURE : EXIT_SUCCESS;
#else
      key.dptr = d;
      key.dsize = strlen(d)+1;
      dbm_delete(db, key);
#endif
    }
  else /* Fill the database */
    {
      for (i = 0, n = 1; fgets(line, 256, fp); n++)
        {
#if defined(FBSD_DATABASE) || defined(BERKELEYDB)
          key.data = strtok(line," ");
          key.size = strlen(key.data);
          value.data = strtok(NULL,"\n");
          value.size = strlen(value.data) + 1;
          ret = ((DB*)db)->put(db, &key, &value, mode);
#else
          key.dptr = strtok(line," ");
          key.dsize = strlen(key.dptr)+1;
          value.dptr = strtok(NULL,"\n");
          value.dsize = strlen(value.dptr) + 1;
	  ret = dbm_store(db, key, value, mode);
#endif
          if (ret < 0)
            break;
          else if (ret)
            {
              if (!q)
                (void)fprintf(stderr, "%s:%i: warning: Duplicate entry. Ignored.\n",
                              f ? f : "stdin", n);
              i++;
            }
        }
      if (ret < 0)
        {
          (void)fprintf(stderr, "%s:%i: error: Database insertion failure. Exiting now.\n",
                        f ? f : "stdin", n);
          ret = EXIT_FAILURE;
        }
      else
        {
          (void)fprintf(stderr, "%i words processed.\n", --n);
          (void)fprintf(stderr, "%i duplicates ignored.\n", i);
          (void)fprintf(stderr, "%i records put into the database.\n", n - i);
          ret = EXIT_SUCCESS;
        }
    }

  /* All done */
#if defined(FBSD_DATABASE) || defined(BERKELEYDB)
  (void)((DB*)db)->close(db);
#else
  dbm_close(db);
#endif
  (void)fclose(fp);
  return ret;
}