]> diplodocus.org Git - nmh/blob - sbr/context_find.c
sendsbr.c: Move interface to own file.
[nmh] / sbr / context_find.c
1 /* context_find.c -- find 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 "concat.h"
10 #include "context_find.h"
11
12
13 char *
14 context_find (const char *str)
15 {
16 struct node *np;
17
18 str = FENDNULL(str);
19 for (np = m_defs; np; np = np->n_next)
20 if (!strcasecmp(FENDNULL(np->n_name), str))
21 return np->n_field;
22
23 return NULL;
24 }
25
26
27 /*
28 * Helper function to search first, if subtype is non-NULL, for
29 * invoname-string-type/subtype and then, if not yet found,
30 * invoname-string-type. If entry is found but is empty, it is
31 * treated as not found.
32 */
33 char *
34 context_find_by_type (const char *string, const char *type,
35 const char *subtype)
36 {
37 char *value = NULL;
38
39 if (subtype) {
40 char *cp;
41
42 cp = concat (invo_name, "-", string, "-", type, "/", subtype, NULL);
43 if ((value = context_find (cp)) != NULL && *value == '\0') value = NULL;
44 free (cp);
45 }
46
47 if (value == NULL) {
48 char *cp;
49
50 cp = concat (invo_name, "-", string, "-", type, NULL);
51 if ((value = context_find (cp)) != NULL && *value == '\0') value = NULL;
52 free (cp);
53 }
54
55 return value;
56 }
57
58
59 /*
60 * Helper function to search profile an entry with name beginning with prefix.
61 * The search is case insensitive.
62 */
63 int
64 context_find_prefix (const char *prefix)
65 {
66 struct node *np;
67 size_t len;
68
69 len = strlen(prefix);
70 for (np = m_defs; np; np = np->n_next) {
71 if (np->n_name && ! strncasecmp (np->n_name, prefix, len)) {
72 return 1;
73 }
74 }
75
76 return 0;
77 }