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