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