]> diplodocus.org Git - nmh/blob - sbr/context_replace.c
Bring these changes over from the branch.
[nmh] / sbr / context_replace.c
1
2 /*
3 * context_replace.c -- add/replace an entry in the context/profile list
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13
14
15 void
16 context_replace (char *key, char *value)
17 {
18 register struct node *np;
19
20 /* sanity check - check that context has been read */
21 if (defpath == NULL)
22 adios (NULL, "oops, context hasn't been read yet");
23
24 /*
25 * If list is emtpy, allocate head of profile/context list.
26 */
27 if (!m_defs) {
28 if (!(m_defs = (struct node *) malloc (sizeof(*np))))
29 adios (NULL, "unable to allocate profile storage");
30
31 np = m_defs;
32 np->n_name = getcpy (key);
33 np->n_field = getcpy (value);
34 np->n_context = 1;
35 np->n_next = NULL;
36 ctxflags |= CTXMOD;
37 return;
38 }
39
40 /*
41 * Search list of context/profile entries for
42 * this key, and replace its value if found.
43 */
44 for (np = m_defs;; np = np->n_next) {
45 if (!strcasecmp (np->n_name, key)) {
46 if (strcmp (value, np->n_field)) {
47 if (!np->n_context)
48 admonish (NULL, "bug: context_replace(key=\"%s\",value=\"%s\")", key, value);
49 if (np->n_field)
50 free (np->n_field);
51 np->n_field = getcpy (value);
52 ctxflags |= CTXMOD;
53 }
54 return;
55 }
56 if (!np->n_next)
57 break;
58 }
59
60 /*
61 * Else add this new entry at the end
62 */
63 np->n_next = (struct node *) malloc (sizeof(*np));
64 if (!np->n_next)
65 adios (NULL, "unable to allocate profile storage");
66
67 np = np->n_next;
68 np->n_name = getcpy (key);
69 np->n_field = getcpy (value);
70 np->n_context = 1;
71 np->n_next = NULL;
72 ctxflags |= CTXMOD;
73 }