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