Postfix3.3.1
string_list.c
[詳解]
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 
89 static void usage(char *progname)
90 {
91  msg_fatal("usage: %s [-v] patterns string", progname);
92 }
93 
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]);
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");
120  string_list_free(list);
121  return (0);
122 }
123 
124 #endif
int msg_verbose
Definition: msg.c:177
#define MATCH_FLAG_RETURN
Definition: match_list.h:40
#define VSTREAM_OUT
Definition: vstream.h:67
#define STRING_LIST
Definition: string_list.h:22
int main(int argc, char **argv)
Definition: anvil.c:1010
#define string_list_init(o, f, p)
Definition: string_list.h:24
#define string_list_free
Definition: string_list.h:27
#define string_list_match
Definition: string_list.h:26
int dict_allow_surrogate
VSTREAM * vstream_printf(const char *fmt,...)
Definition: vstream.c:1335
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
int vstream_fflush(VSTREAM *stream)
Definition: vstream.c:1257
#define GETOPT(argc, argv, str)
Definition: sys_defs.h:1313
void msg_vstream_init(const char *name, VSTREAM *vp)
Definition: msg_vstream.c:77
int util_utf8_enable
Definition: printable.c:47
#define VSTREAM_ERR
Definition: vstream.h:68