1 /* $NetBSD: string_list.c,v 1.1.1.1 2009/06/23 10:08:48 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 flags argument 37 /* is ignored; pattern_list specifies a list of string patterns. 38 /* 39 /* string_list_match() matches the specified string against the 40 /* compiled pattern list. 41 /* 42 /* string_list_free() releases storage allocated by string_list_init(). 43 /* DIAGNOSTICS 44 /* Fatal error: unable to open or read a pattern file or table. 45 /* SEE ALSO 46 /* match_list(3) generic list matching 47 /* match_ops(3) match strings by name or by address 48 /* LICENSE 49 /* .ad 50 /* .fi 51 /* The Secure Mailer license must be distributed with this software. 52 /* AUTHOR(S) 53 /* Wietse Venema 54 /* IBM T.J. Watson Research 55 /* P.O. Box 704 56 /* Yorktown Heights, NY 10598, USA 57 /*--*/ 58 59 /* System library. */ 60 61 #include <sys_defs.h> 62 63 /* Utility library. */ 64 65 #include <match_list.h> 66 67 /* Global library. */ 68 69 #include "string_list.h" 70 71 #ifdef TEST 72 73 #include <msg.h> 74 #include <stdlib.h> 75 #include <unistd.h> 76 #include <vstream.h> 77 #include <vstring.h> 78 #include <msg_vstream.h> 79 80 static void usage(char *progname) 81 { 82 msg_fatal("usage: %s [-v] patterns string", progname); 83 } 84 85 int main(int argc, char **argv) 86 { 87 STRING_LIST *list; 88 char *string; 89 int ch; 90 91 msg_vstream_init(argv[0], VSTREAM_ERR); 92 93 while ((ch = GETOPT(argc, argv, "v")) > 0) { 94 switch (ch) { 95 case 'v': 96 msg_verbose++; 97 break; 98 default: 99 usage(argv[0]); 100 } 101 } 102 if (argc != optind + 2) 103 usage(argv[0]); 104 list = string_list_init(MATCH_FLAG_NONE, argv[optind]); 105 string = argv[optind + 1]; 106 vstream_printf("%s: %s\n", string, string_list_match(list, string) ? 107 "YES" : "NO"); 108 vstream_fflush(VSTREAM_OUT); 109 string_list_free(list); 110 return (0); 111 } 112 113 #endif 114