xref: /netbsd-src/external/ibm-public/postfix/dist/src/global/string_list.c (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
1 /*	$NetBSD: string_list.c,v 1.1.1.2 2013/01/02 18:59:00 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	string_list 3
6 /* SUMMARY
7 /*	match a string against a pattern list
8 /* SYNOPSIS
9 /*	#include <string_list.h>
10 /*
11 /*	STRING_LIST *string_list_init(flags, pattern_list)
12 /*	int	flags;
13 /*	const char *pattern_list;
14 /*
15 /*	int	string_list_match(list, name)
16 /*	STRING_LIST *list;
17 /*	const char *name;
18 /*
19 /*	void string_list_free(list)
20 /*	STRING_LIST *list;
21 /* DESCRIPTION
22 /*	This is a convenience wrapper around the match_list module.
23 /*
24 /*	This module implements tests for list membership of a string.
25 /*
26 /*	Patterns are separated by whitespace and/or commas. A pattern
27 /*	is either a string, a file name (in which case the contents
28 /*	of the file are substituted for the file name) or a type:name
29 /*	lookup table specification.
30 /*
31 /*	A string matches a string list when it appears in the list of
32 /*	string patterns. The matching process is case insensitive.
33 /*	In order to reverse the result, precede a pattern with an
34 /*	exclamation point (!).
35 /*
36 /*	string_list_init() performs initializations. The first argument
37 /*	is a bit-wise OR of zero or more of following:
38 /* .IP MATCH_FLAG_RETURN
39 /*	Request that string_list_match() logs a warning and returns
40 /*	zero with list->error set to a non-zero dictionary error
41 /*	code, instead of raising a fatal error.
42 /* .PP
43 /*	Specify MATCH_FLAG_NONE to request none of the above.
44 /*	The second argument specifies a list of string patterns.
45 /*
46 /*	string_list_match() matches the specified string against the
47 /*	compiled pattern list.
48 /*
49 /*	string_list_free() releases storage allocated by string_list_init().
50 /* DIAGNOSTICS
51 /*	Fatal error: unable to open or read a pattern file or table.
52 /* SEE ALSO
53 /*	match_list(3) generic list matching
54 /*	match_ops(3) match strings by name or by address
55 /* LICENSE
56 /* .ad
57 /* .fi
58 /*	The Secure Mailer license must be distributed with this software.
59 /* AUTHOR(S)
60 /*	Wietse Venema
61 /*	IBM T.J. Watson Research
62 /*	P.O. Box 704
63 /*	Yorktown Heights, NY 10598, USA
64 /*--*/
65 
66 /* System library. */
67 
68 #include <sys_defs.h>
69 
70 /* Utility library. */
71 
72 #include <match_list.h>
73 
74 /* Global library. */
75 
76 #include "string_list.h"
77 
78 #ifdef TEST
79 
80 #include <msg.h>
81 #include <stdlib.h>
82 #include <unistd.h>
83 #include <vstream.h>
84 #include <vstring.h>
85 #include <msg_vstream.h>
86 
87 static void usage(char *progname)
88 {
89     msg_fatal("usage: %s [-v] patterns string", progname);
90 }
91 
92 int     main(int argc, char **argv)
93 {
94     STRING_LIST *list;
95     char   *string;
96     int     ch;
97 
98     msg_vstream_init(argv[0], VSTREAM_ERR);
99 
100     while ((ch = GETOPT(argc, argv, "v")) > 0) {
101 	switch (ch) {
102 	case 'v':
103 	    msg_verbose++;
104 	    break;
105 	default:
106 	    usage(argv[0]);
107 	}
108     }
109     if (argc != optind + 2)
110 	usage(argv[0]);
111     list = string_list_init(MATCH_FLAG_RETURN, argv[optind]);
112     string = argv[optind + 1];
113     vstream_printf("%s: %s\n", string, string_list_match(list, string) ?
114 		   "YES" : list->error == 0 ? "NO" : "ERROR");
115     vstream_fflush(VSTREAM_OUT);
116     string_list_free(list);
117     return (0);
118 }
119 
120 #endif
121