1 /* $NetBSD: extpar.c,v 1.4 2022/10/08 16:12:50 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 extpar() callers that
31 /* extpar() 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 /* In case of error the result value is a dynamically-allocated
39 /* string with a description of the problem that includes a
40 /* copy of the offending input. A non-null result value should
41 /* be destroyed with myfree(). The following describes the errors
42 /* and the state of the buffer and buffer pointer.
43 /* .IP "no opening parenthesis at start of text"
44 /* The buffer pointer points to the input text.
45 /* .IP "missing closing parenthesis"
46 /* The buffer pointer points to text as if a closing parenthesis
47 /* were present at the end of the input.
48 /* .IP "text after closing parenthesis"
49 /* The buffer pointer points to text as if the offending text
50 /* were not present.
51 /* SEE ALSO
52 /* balpar(3) determine length of string in parentheses
53 /* LICENSE
54 /* .ad
55 /* .fi
56 /* The Secure Mailer license must be distributed with this software.
57 /* AUTHOR(S)
58 /* Wietse Venema
59 /* IBM T.J. Watson Research
60 /* P.O. Box 704
61 /* Yorktown Heights, NY 10598, USA
62 /*
63 /* Wietse Venema
64 /* Google, Inc.
65 /* 111 8th Avenue
66 /* New York, NY 10011, USA
67 /*--*/
68
69 /*
70 * System library.
71 */
72 #include <sys_defs.h>
73 #include <ctype.h>
74
75 /*
76 * Utility library.
77 */
78 #include <vstring.h>
79 #include <stringops.h>
80
81 /* extpar - extract text from parentheses */
82
extpar(char ** bp,const char * parens,int flags)83 char *extpar(char **bp, const char *parens, int flags)
84 {
85 char *cp = *bp;
86 char *err = 0;
87 size_t len;
88
89 if (cp[0] != parens[0]) {
90 err = vstring_export(vstring_sprintf(vstring_alloc(100),
91 "no '%c' at start of text in \"%s\"", parens[0], cp));
92 len = 0;
93 } else if ((len = balpar(cp, parens)) == 0) {
94 err = concatenate("missing '", parens + 1, "' in \"",
95 cp, "\"", (char *) 0);
96 cp += 1;
97 } else {
98 if (cp[len] != 0)
99 err = concatenate("syntax error after '", parens + 1, "' in \"",
100 cp, "\"", (char *) 0);
101 cp += 1;
102 cp[len -= 2] = 0;
103 }
104 if (flags & EXTPAR_FLAG_STRIP) {
105 trimblanks(cp, len)[0] = 0;
106 while (ISSPACE(*cp))
107 cp++;
108 }
109 *bp = cp;
110 return (err);
111 }
112