2 * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
4 * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
7 * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
8 * Portions Copyright (C) 1989-1992, Brian Berliner
10 * You may distribute under the terms of the GNU General Public License as
11 * specified in the README file that comes with the CVS source distribution.
13 * A simple ndbm-emulator for CVS. It parses a text file of the format:
17 * at dbm_open time, and loads the entire file into memory. As such, it is
18 * probably only good for fairly small modules files. Ours is about 30K in
19 * size, and this code works fine.
27 # define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
28 # endif /* defined O_ACCMODE */
30 static void mydbm_load_file (FILE *, List *, char *);
32 /* Returns NULL on error in which case errno has been set to indicate
33 the error. Can also call error() itself. */
36 mydbm_open (char *file, int flags, int mode)
41 fp = CVS_FOPEN (file, (flags & O_ACCMODE) != O_RDONLY
42 ? FOPEN_BINARY_READWRITE : FOPEN_BINARY_READ);
43 if (fp == NULL && !(existence_error (errno) && (flags & O_CREAT)))
46 db = xmalloc (sizeof (*db));
47 db->dbm_list = getlist ();
49 db->name = xstrdup (file);
53 mydbm_load_file (fp, db->dbm_list, file);
55 error (0, errno, "cannot close %s",
56 primary_root_inverse_translate (file));
64 write_item (Node *node, void *data)
67 fputs (node->key, fp);
69 fputs (node->data, fp);
82 fp = CVS_FOPEN (db->name, FOPEN_BINARY_WRITE);
84 error (1, errno, "cannot write %s", db->name);
85 walklist (db->dbm_list, write_item, fp);
87 error (0, errno, "cannot close %s", db->name);
90 dellist (&db->dbm_list);
97 mydbm_fetch (DBM *db, datum key)
103 /* make sure it's null-terminated */
104 s = xmalloc (key.dsize + 1);
105 (void) strncpy (s, key.dptr, key.dsize);
108 p = findnode (db->dbm_list, s);
112 val.dsize = strlen (p->data);
126 mydbm_firstkey (DBM *db)
131 head = db->dbm_list->list;
136 key.dsize = strlen (p->key);
143 db->dbm_next = p->next;
150 mydbm_nextkey (DBM *db)
155 head = db->dbm_list->list;
160 key.dsize = strlen (p->key);
167 db->dbm_next = p->next;
173 /* Note: only updates the in-memory copy, which is written out at
174 mydbm_close time. Note: Also differs from DBM in that on duplication,
175 it gives a warning, rather than either DBM_INSERT or DBM_REPLACE
178 mydbm_store (DBM *db, datum key, datum value, int flags)
183 node->type = NDBMNODE;
185 node->key = xmalloc (key.dsize + 1);
187 strncat (node->key, key.dptr, key.dsize);
189 node->data = xmalloc (value.dsize + 1);
190 *(char *)node->data = '\0';
191 strncat (node->data, value.dptr, value.dsize);
194 if (addnode (db->dbm_list, node) == -1)
196 error (0, 0, "attempt to insert duplicate key `%s'", node->key);
208 * filename Used in error messages.
211 mydbm_load_file (FILE *fp, List *list, char *filename)
216 size_t value_allocated;
223 value = xmalloc (value_allocated);
227 while ((line_length = getdelim (&line, &line_size, '\012', fp)) >= 0)
230 if (line_length > 0 && line[line_length - 1] == '\012')
232 /* Strip the newline. */
234 line[line_length] = '\0';
236 if (line_length > 0 && line[line_length - 1] == '\015')
238 /* If the file (e.g. modules) was written on an NT box, it will
239 contain CRLF at the ends of lines. Strip them (we can't do
240 this by opening the file in text mode because we might be
243 line[line_length] = '\0';
247 * Add the line to the value, at the end if this is a continuation
248 * line; otherwise at the beginning, but only after any trailing
249 * backslash is removed.
255 * See if the line we read is a continuation line, and strip the
259 cp = &line[line_length - 1];
272 expand_string (&value,
274 strlen (value) + line_length + 5);
275 strcat (value, line);
278 continue; /* comment line */
280 while (*vp && isspace ((unsigned char) *vp))
283 continue; /* empty line */
286 * If this was not a continuation line, add the entry to the database
290 Node *p = getnode ();
294 while (*vp && !isspace ((unsigned char) *vp))
297 *vp++ = '\0'; /* NULL terminate the key */
299 p->key = xstrdup (kp);
300 while (*vp && isspace ((unsigned char) *vp))
301 vp++; /* skip whitespace to value */
306 "warning: NULL value for key `%s' at line %d of `%s'",
308 primary_root_inverse_translate (filename));
312 p->data = xstrdup (vp);
313 if (addnode (list, p) == -1)
317 "duplicate key found for `%s' at line %d of `%s'",
319 primary_root_inverse_translate (filename));
324 if (line_length < 0 && !feof (fp))
325 error (0, errno, "cannot read file `%s' in mydbm_load_file",
326 primary_root_inverse_translate (filename));