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