xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/dict_static.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: dict_static.c,v 1.2 2017/02/14 01:16:49 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	dict_static 3
6 /* SUMMARY
7 /*	dictionary manager interface to static variables
8 /* SYNOPSIS
9 /*	#include <dict_static.h>
10 /*
11 /*	DICT	*dict_static_open(name, name, dict_flags)
12 /*	const char *name;
13 /*	int	dummy;
14 /*	int	dict_flags;
15 /* DESCRIPTION
16 /*	dict_static_open() implements a dummy dictionary that returns
17 /*	as lookup result the dictionary name, regardless of the lookup
18 /*	key value.
19 /* SEE ALSO
20 /*	dict(3) generic dictionary manager
21 /* LICENSE
22 /* .ad
23 /* .fi
24 /*	The Secure Mailer license must be distributed with this software.
25 /* AUTHOR(S)
26 /*	jeffm
27 /*	ghostgun.com
28 /*--*/
29 
30 /* System library. */
31 
32 #include "sys_defs.h"
33 #include <stdio.h>			/* sprintf() prototype */
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 
38 /* Utility library. */
39 
40 #include "mymalloc.h"
41 #include "msg.h"
42 #include "stringops.h"
43 #include "dict.h"
44 #include "dict_static.h"
45 
46 /* dict_static_lookup - access static value*/
47 
48 static const char *dict_static_lookup(DICT *dict, const char *unused_name)
49 {
50     DICT_ERR_VAL_RETURN(dict, DICT_ERR_NONE, dict->name);
51 }
52 
53 /* dict_static_close - close static dictionary */
54 
55 static void dict_static_close(DICT *dict)
56 {
57     dict_free(dict);
58 }
59 
60 /* dict_static_open - make association with static variable */
61 
62 DICT   *dict_static_open(const char *name, int open_flags, int dict_flags)
63 {
64     DICT   *dict;
65     char   *err = 0;
66     char   *cp, *saved_name = 0;
67 
68     /*
69      * Let the optimizer worry about eliminating redundant code.
70      */
71 #define DICT_STATIC_OPEN_RETURN(d) do { \
72         DICT *__d = (d); \
73         if (saved_name != 0) \
74             myfree(saved_name); \
75         if (err != 0) \
76             myfree(err); \
77         return (__d); \
78     } while (0)
79 
80     /*
81      * Optionally strip surrounding braces and whitespace.
82      */
83     if (name[0] == CHARS_BRACE[0]) {
84 	saved_name = cp = mystrdup(name);
85 	if ((err = extpar(&cp, CHARS_BRACE, EXTPAR_FLAG_STRIP)) != 0)
86 	    DICT_STATIC_OPEN_RETURN(dict_surrogate(DICT_TYPE_STATIC, name,
87 						   open_flags, dict_flags,
88 						   "bad %s:name syntax: %s",
89 						   DICT_TYPE_STATIC, err));
90 	name = cp;
91     }
92 
93     /*
94      * Bundle up the request.
95      */
96     dict = dict_alloc(DICT_TYPE_STATIC, name, sizeof(*dict));
97     dict->lookup = dict_static_lookup;
98     dict->close = dict_static_close;
99     dict->flags = dict_flags | DICT_FLAG_FIXED;
100     dict->owner.status = DICT_OWNER_TRUSTED;
101     DICT_STATIC_OPEN_RETURN(DICT_DEBUG (dict));
102 }
103