xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/extpar.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: extpar.c,v 1.2 2017/02/14 01:16:49 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	extpar 3
6 /* SUMMARY
7 /*	extract text from parentheses
8 /* SYNOPSIS
9 /*	#include <stringops.h>
10 /*
11 /*	char	*extpar(bp, parens, flags)
12 /*	char	**bp;
13 /*	const char *parens;
14 /*	int	flags;
15 /* DESCRIPTION
16 /*	extpar() extracts text from an input string that is enclosed
17 /*	in the specified parentheses, and updates the buffer pointer
18 /*	to point to that text.
19 /*
20 /*	Arguments:
21 /* .IP bp
22 /*	Pointer to buffer pointer. Both the buffer and the buffer
23 /*	pointer are modified.
24 /* .IP parens
25 /*	One matching pair of parentheses, opening parenthesis first.
26 /* .IP flags
27 /*	EXTPAR_FLAG_NONE, or the bitwise OR of one or more flags:
28 /* .RS
29 /* .IP EXTPAR_FLAG_EXTRACT
30 /*	This flag is intended to instruct expar() callers that
31 /*	expar() should be invoked. It has no effect on expar()
32 /*	itself.
33 /* .IP EXTPAR_FLAG_STRIP
34 /*	Skip whitespace after the opening parenthesis, and trim
35 /*	whitespace before the closing parenthesis.
36 /* .RE
37 /* DIAGNOSTICS
38 /*	panic: the input string does not start with the opening
39 /*	parenthesis.
40 /*
41 /*	In case of error the result value is a dynamically-allocated
42 /*	string with a description of the problem that includes a
43 /*	copy of the offending input.  A non-null result value should
44 /*	be destroyed with myfree(). The following decribes the errors
45 /*	and the state of the buffer and buffer pointer.
46 /* .IP "missing closing parenthesis"
47 /*	The buffer pointer points to text as if a closing parenthesis
48 /*	were present at the end of the input.
49 /* .IP "text after closing parenthesis"
50 /*	The buffer pointer points to text as if the offending text
51 /*	were not present.
52 /* SEE ALSO
53 /*	balpar(3) determine length of string in parentheses
54 /* LICENSE
55 /* .ad
56 /* .fi
57 /*	The Secure Mailer license must be distributed with this software.
58 /* AUTHOR(S)
59 /*	Wietse Venema
60 /*	IBM T.J. Watson Research
61 /*	P.O. Box 704
62 /*	Yorktown Heights, NY 10598, USA
63 /*--*/
64 
65  /*
66   * System library.
67   */
68 #include <sys_defs.h>
69 #include <ctype.h>
70 
71  /*
72   * Utility library.
73   */
74 #include <msg.h>
75 #include <stringops.h>
76 
77 /* extpar - extract text from parentheses */
78 
79 char   *extpar(char **bp, const char *parens, int flags)
80 {
81     char   *cp = *bp;
82     char   *err = 0;
83     size_t  len;
84 
85     if (cp[0] != parens[0])
86 	msg_panic("extpar: no '%c' at start of text: \"%s\"", parens[0], cp);
87     if ((len = balpar(cp, parens)) == 0) {
88 	err = concatenate("missing '", parens + 1, "' in \"",
89 			  cp, "\"", (char *) 0);
90 	cp += 1;
91     } else {
92 	if (cp[len] != 0)
93 	    err = concatenate("syntax error after '", parens + 1, "' in \"",
94 			      cp, "\"", (char *) 0);
95 	cp += 1;
96 	cp[len -= 2] = 0;
97     }
98     if (flags & EXTPAR_FLAG_STRIP) {
99 	trimblanks(cp, len)[0] = 0;
100 	while (ISSPACE(*cp))
101 	    cp++;
102     }
103     *bp = cp;
104     return (err);
105 }
106