]> diplodocus.org Git - nmh/blob - sbr/context_replace.c
d4926deca372b007dca64aae38c2a9ec0b3379d4
[nmh] / sbr / context_replace.c
1 /*
2 * context_replace.c -- add/replace an entry in the context/profile list
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11
12
13 void
14 context_replace (char *key, char *value)
15 {
16 struct node *np;
17
18 /*
19 * If list is empty, allocate head of profile/context list.
20 */
21 if (!m_defs) {
22 NEW(np);
23 m_defs = np;
24 np->n_name = getcpy (key);
25 np->n_field = getcpy (value);
26 np->n_context = 1;
27 np->n_next = NULL;
28 ctxflags |= CTXMOD;
29 return;
30 }
31
32 /*
33 * Search list of context/profile entries for
34 * this key, and replace its value if found.
35 */
36 for (np = m_defs;; np = np->n_next) {
37 if (!strcasecmp (np->n_name ? np->n_name : "", key ? key : "")) {
38 if (strcmp (value, np->n_field)) {
39 if (!np->n_context)
40 inform("bug: context_replace(key=\"%s\",value=\"%s\"), continuing...", key, value);
41 mh_xfree(np->n_field);
42 np->n_field = mh_xstrdup(value);
43 ctxflags |= CTXMOD;
44 }
45 return;
46 }
47 if (!np->n_next)
48 break;
49 }
50
51 /*
52 * Else add this new entry at the end
53 */
54 NEW(np->n_next);
55 np = np->n_next;
56 np->n_name = getcpy (key);
57 np->n_field = getcpy (value);
58 np->n_context = 1;
59 np->n_next = NULL;
60 ctxflags |= CTXMOD;
61 }