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.
29 # define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
30 # endif /* defined O_ACCMODE */
32 static void mydbm_load_file (FILE *, List *, char *);
34 /* Returns NULL on error in which case errno has been set to indicate
35 the error. Can also call error() itself. */
38 mydbm_open (char *file, int flags, int mode)
43 fp = CVS_FOPEN (file, (flags & O_ACCMODE) != O_RDONLY
44 ? FOPEN_BINARY_READWRITE : FOPEN_BINARY_READ);
45 if (fp == NULL && !(existence_error (errno) && (flags & O_CREAT)))
48 db = xmalloc (sizeof (*db));
49 db->dbm_list = getlist ();
51 db->name = xstrdup (file);
55 mydbm_load_file (fp, db->dbm_list, file);
57 error (0, errno, "cannot close %s",
58 primary_root_inverse_translate (file));
66 write_item (Node *node, void *data)
69 fputs (node->key, fp);
71 fputs (node->data, fp);
84 fp = CVS_FOPEN (db->name, FOPEN_BINARY_WRITE);
86 error (1, errno, "cannot write %s", db->name);
87 walklist (db->dbm_list, write_item, fp);
89 error (0, errno, "cannot close %s", db->name);
92 dellist (&db->dbm_list);
99 mydbm_fetch (DBM *db, datum key)
105 /* make sure it's null-terminated */
106 s = xmalloc (key.dsize + 1);
107 (void) strncpy (s, key.dptr, key.dsize);
110 p = findnode (db->dbm_list, s);
114 val.dsize = strlen (p->data);
128 mydbm_firstkey (DBM *db)
133 head = db->dbm_list->list;
138 key.dsize = strlen (p->key);
145 db->dbm_next = p->next;
152 mydbm_nextkey (DBM *db)
157 head = db->dbm_list->list;
162 key.dsize = strlen (p->key);
169 db->dbm_next = p->next;
175 /* Note: only updates the in-memory copy, which is written out at
176 mydbm_close time. Note: Also differs from DBM in that on duplication,
177 it gives a warning, rather than either DBM_INSERT or DBM_REPLACE
180 mydbm_store (DBM *db, datum key, datum value, int flags)
185 node->type = NDBMNODE;
187 node->key = xmalloc (key.dsize + 1);
189 strncat (node->key, key.dptr, key.dsize);
191 node->data = xmalloc (value.dsize + 1);
192 *(char *)node->data = '\0';
193 strncat (node->data, value.dptr, value.dsize);
196 if (addnode (db->dbm_list, node) == -1)
198 error (0, 0, "attempt to insert duplicate key `%s'", node->key);
210 * filename Used in error messages.
213 mydbm_load_file (FILE *fp, List *list, char *filename)
218 size_t value_allocated;
225 value = xmalloc (value_allocated);
229 while ((line_length = getdelim (&line, &line_size, '\012', fp)) >= 0)
232 if (line_length > 0 && line[line_length - 1] == '\012')
234 /* Strip the newline. */
236 line[line_length] = '\0';
238 if (line_length > 0 && line[line_length - 1] == '\015')
240 /* If the file (e.g. modules) was written on an NT box, it will
241 contain CRLF at the ends of lines. Strip them (we can't do
242 this by opening the file in text mode because we might be
245 line[line_length] = '\0';
249 * Add the line to the value, at the end if this is a continuation
250 * line; otherwise at the beginning, but only after any trailing
251 * backslash is removed.
257 * See if the line we read is a continuation line, and strip the
261 cp = &line[line_length - 1];
274 expand_string (&value,
276 strlen (value) + line_length + 5);
277 strcat (value, line);
280 continue; /* comment line */
282 while (*vp && isspace ((unsigned char) *vp))
285 continue; /* empty line */
288 * If this was not a continuation line, add the entry to the database
292 Node *p = getnode ();
296 while (*vp && !isspace ((unsigned char) *vp))
299 *vp++ = '\0'; /* NULL terminate the key */
301 p->key = xstrdup (kp);
302 while (*vp && isspace ((unsigned char) *vp))
303 vp++; /* skip whitespace to value */
308 "warning: NULL value for key `%s' at line %d of `%s'",
310 primary_root_inverse_translate (filename));
314 p->data = xstrdup (vp);
315 if (addnode (list, p) == -1)
319 "duplicate key found for `%s' at line %d of `%s'",
321 primary_root_inverse_translate (filename));
326 if (line_length < 0 && !feof (fp))
327 error (0, errno, "cannot read file `%s' in mydbm_load_file",
328 primary_root_inverse_translate (filename));