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.
28 # define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
29 # endif /* defined O_ACCMODE */
31 static void mydbm_load_file PROTO ((FILE *, List *, char *));
33 /* Returns NULL on error in which case errno has been set to indicate
34 the error. Can also call error() itself. */
37 mydbm_open (file, flags, mode)
45 fp = CVS_FOPEN (file, (flags & O_ACCMODE) != O_RDONLY ?
46 FOPEN_BINARY_READWRITE : FOPEN_BINARY_READ);
47 if (fp == NULL && !(existence_error (errno) && (flags & O_CREAT)))
50 db = (DBM *) xmalloc (sizeof (*db));
51 db->dbm_list = getlist ();
53 db->name = xstrdup (file);
57 mydbm_load_file (fp, db->dbm_list, file);
59 error (0, errno, "cannot close %s", file);
64 static int write_item PROTO ((Node *, void *));
67 write_item (node, data)
71 FILE *fp = (FILE *)data;
72 fputs (node->key, fp);
74 fputs (node->data, fp);
86 fp = CVS_FOPEN (db->name, FOPEN_BINARY_WRITE);
88 error (1, errno, "cannot write %s", db->name);
89 walklist (db->dbm_list, write_item, (void *)fp);
91 error (0, errno, "cannot close %s", db->name);
94 dellist (&db->dbm_list);
107 /* make sure it's null-terminated */
108 s = xmalloc (key.dsize + 1);
109 (void) strncpy (s, key.dptr, key.dsize);
112 p = findnode (db->dbm_list, s);
116 val.dsize = strlen (p->data);
120 val.dptr = (char *) NULL;
134 head = db->dbm_list->list;
139 key.dsize = strlen (p->key);
143 key.dptr = (char *) NULL;
146 db->dbm_next = p->next;
157 head = db->dbm_list->list;
162 key.dsize = strlen (p->key);
166 key.dptr = (char *) NULL;
169 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 (db, key, value, flags)
187 node->type = NDBMNODE;
189 node->key = xmalloc (key.dsize + 1);
191 strncat (node->key, key.dptr, key.dsize);
193 node->data = xmalloc (value.dsize + 1);
194 *(char *)node->data = '\0';
195 strncat (node->data, value.dptr, value.dsize);
198 if (addnode (db->dbm_list, node) == -1)
200 error (0, 0, "attempt to insert duplicate key `%s'", node->key);
208 mydbm_load_file (fp, list, filename)
211 char *filename; /* Used in error messages. */
216 size_t value_allocated;
223 value = xmalloc (value_allocated);
227 while ((line_length =
228 getstr (&line, &line_size, fp, '\012', 0, GETLINE_NO_LIMIT)) >= 0)
231 if (line_length > 0 && line[line_length - 1] == '\012')
233 /* Strip the newline. */
235 line[line_length] = '\0';
237 if (line_length > 0 && line[line_length - 1] == '\015')
239 /* If the file (e.g. modules) was written on an NT box, it will
240 contain CRLF at the ends of lines. Strip them (we can't do
241 this by opening the file in text mode because we might be
244 line[line_length] = '\0';
248 * Add the line to the value, at the end if this is a continuation
249 * line; otherwise at the beginning, but only after any trailing
250 * backslash is removed.
256 * See if the line we read is a continuation line, and strip the
260 cp = &line[line_length - 1];
273 expand_string (&value,
275 strlen (value) + line_length + 5);
276 strcat (value, line);
279 continue; /* comment line */
281 while (*vp && isspace ((unsigned char) *vp))
284 continue; /* empty line */
287 * If this was not a continuation line, add the entry to the database
291 Node *p = getnode ();
295 while (*vp && !isspace ((unsigned char) *vp))
298 *vp++ = '\0'; /* NULL terminate the key */
300 p->key = xstrdup (kp);
301 while (*vp && isspace ((unsigned char) *vp))
302 vp++; /* skip whitespace to value */
307 "warning: NULL value for key `%s' at line %d of `%s'",
308 p->key, line_num, filename);
312 p->data = xstrdup (vp);
313 if (addnode (list, p) == -1)
317 "duplicate key found for `%s' at line %d of `%s'",
318 p->key, line_num, filename);
323 if (line_length < 0 && !feof (fp))
324 /* FIXME: should give the name of the file. */
325 error (0, errno, "cannot read file in mydbm_load_file");