1 /*++
2 /* NAME
3 /*	string_list 3
4 /* SUMMARY
5 /*	match a string against a pattern list
6 /* SYNOPSIS
7 /*	#include <string_list.h>
8 /*
9 /*	STRING_LIST *string_list_init(pname, flags, pattern_list)
10 /*	const char *pname;
11 /*	int	flags;
12 /*	const char *pattern_list;
13 /*
14 /*	int	string_list_match(list, name)
15 /*	STRING_LIST *list;
16 /*	const char *name;
17 /*
18 /*	void string_list_free(list)
19 /*	STRING_LIST *list;
20 /* DESCRIPTION
21 /*	This is a convenience wrapper around the match_list module.
22 /*
23 /*	This module implements tests for list membership of a string.
24 /*
25 /*	Patterns are separated by whitespace and/or commas. A pattern
26 /*	is either a string, a file name (in which case the contents
27 /*	of the file are substituted for the file name) or a type:name
28 /*	lookup table specification.
29 /*
30 /*	A string matches a string list when it appears in the list of
31 /*	string patterns. The matching process is case insensitive.
32 /*	In order to reverse the result, precede a pattern with an
33 /*	exclamation point (!).
34 /*
35 /*	string_list_init() performs initializations. The pname
36 /*	argument specifies error reporting context. The flags 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 last 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 <stdlib.h>
81 #include <unistd.h>
82 #include <msg.h>
83 #include <vstream.h>
84 #include <vstring.h>
85 #include <msg_vstream.h>
86 #include <dict.h>
87 #include <stringops.h>			/* util_utf8_enable */
88 
usage(char * progname)89 static void usage(char *progname)
90 {
91     msg_fatal("usage: %s [-v] patterns string", progname);
92 }
93 
main(int argc,char ** argv)94 int     main(int argc, char **argv)
95 {
96     STRING_LIST *list;
97     char   *string;
98     int     ch;
99 
100     msg_vstream_init(argv[0], VSTREAM_ERR);
101 
102     while ((ch = GETOPT(argc, argv, "v")) > 0) {
103 	switch (ch) {
104 	case 'v':
105 	    msg_verbose++;
106 	    break;
107 	default:
108 	    usage(argv[0]);
109 	}
110     }
111     if (argc != optind + 2)
112 	usage(argv[0]);
113     dict_allow_surrogate = 1;
114     util_utf8_enable = 1;
115     list = string_list_init("command line", MATCH_FLAG_RETURN, argv[optind]);
116     string = argv[optind + 1];
117     vstream_printf("%s: %s\n", string, string_list_match(list, string) ?
118 		   "YES" : list->error == 0 ? "NO" : "ERROR");
119     vstream_fflush(VSTREAM_OUT);
120     string_list_free(list);
121     return (0);
122 }
123 
124 #endif
125