Postfix3.3.1
argv_splitq.c
[詳解]
1 /*++
2 /* NAME
3 /* argv_splitq 3
4 /* SUMMARY
5 /* string array utilities
6 /* SYNOPSIS
7 /* #include <argv.h>
8 /*
9 /* ARGV *argv_splitq(string, delim, parens)
10 /* const char *string;
11 /* const char *delim;
12 /* const char *parens;
13 /*
14 /* ARGV *argv_splitq_count(string, delim, parens, count)
15 /* const char *string;
16 /* const char *delim;
17 /* const char *parens;
18 /* ssize_t count;
19 /*
20 /* ARGV *argv_splitq_append(argv, string, delim, parens)
21 /* ARGV *argv;
22 /* const char *string;
23 /* const char *delim;
24 /* const char *parens;
25 /* DESCRIPTION
26 /* argv_splitq() breaks up \fIstring\fR into tokens according
27 /* to the delimiters specified in \fIdelim\fR, while avoiding
28 /* splitting text between matching parentheses. The result is
29 /* a null-terminated string array.
30 /*
31 /* argv_splitq_count() is like argv_splitq() but stops splitting
32 /* input after at most \fIcount\fR -1 times and leaves the
33 /* remainder, if any, in the last array element. It is an error
34 /* to specify a count < 1.
35 /*
36 /* argv_splitq_append() performs the same operation as argv_splitq(),
37 /* but appends the result to an existing string array.
38 /* SEE ALSO
39 /* mystrtokq(), safe string splitter.
40 /* DIAGNOSTICS
41 /* Fatal errors: memory allocation problem.
42 /* LICENSE
43 /* .ad
44 /* .fi
45 /* The Secure Mailer license must be distributed with this software.
46 /* AUTHOR(S)
47 /* Wietse Venema
48 /* IBM T.J. Watson Research
49 /* P.O. Box 704
50 /* Yorktown Heights, NY 10598, USA
51 /*--*/
52 
53 /* System libraries. */
54 
55 #include <sys_defs.h>
56 #include <string.h>
57 
58 /* Application-specific. */
59 
60 #include "mymalloc.h"
61 #include "stringops.h"
62 #include "argv.h"
63 #include "msg.h"
64 
65 /* argv_splitq - split string into token array */
66 
67 ARGV *argv_splitq(const char *string, const char *delim, const char *parens)
68 {
69  ARGV *argvp = argv_alloc(1);
70  char *saved_string = mystrdup(string);
71  char *bp = saved_string;
72  char *arg;
73 
74  while ((arg = mystrtokq(&bp, delim, parens)) != 0)
75  argv_add(argvp, arg, (char *) 0);
76  argv_terminate(argvp);
77  myfree(saved_string);
78  return (argvp);
79 }
80 
81 /* argv_splitq_count - split string into token array */
82 
83 ARGV *argv_splitq_count(const char *string, const char *delim,
84  const char *parens, ssize_t count)
85 {
86  ARGV *argvp = argv_alloc(1);
87  char *saved_string = mystrdup(string);
88  char *bp = saved_string;
89  char *arg;
90 
91  if (count < 1)
92  msg_panic("argv_splitq_count: bad count: %ld", (long) count);
93  while (count-- > 1 && (arg = mystrtokq(&bp, delim, parens)) != 0)
94  argv_add(argvp, arg, (char *) 0);
95  if (*bp)
96  bp += strspn(bp, delim);
97  if (*bp)
98  argv_add(argvp, bp, (char *) 0);
99  argv_terminate(argvp);
100  myfree(saved_string);
101  return (argvp);
102 }
103 
104 /* argv_splitq_append - split string into token array, append to array */
105 
106 ARGV *argv_splitq_append(ARGV *argvp, const char *string, const char *delim,
107  const char *parens)
108 {
109  char *saved_string = mystrdup(string);
110  char *bp = saved_string;
111  char *arg;
112 
113  while ((arg = mystrtokq(&bp, delim, parens)) != 0)
114  argv_add(argvp, arg, (char *) 0);
115  argv_terminate(argvp);
116  myfree(saved_string);
117  return (argvp);
118 }
void myfree(void *ptr)
Definition: mymalloc.c:207
char * mystrdup(const char *str)
Definition: mymalloc.c:225
Definition: argv.h:17
NORETURN msg_panic(const char *fmt,...)
Definition: msg.c:295
ARGV * argv_splitq_count(const char *string, const char *delim, const char *parens, ssize_t count)
Definition: argv_splitq.c:83
void argv_add(ARGV *argvp,...)
Definition: argv.c:197
char * mystrtokq(char **src, const char *sep, const char *parens)
Definition: mystrtok.c:80
ARGV * argv_alloc(ssize_t len)
Definition: argv.c:149
ARGV * argv_splitq_append(ARGV *argvp, const char *string, const char *delim, const char *parens)
Definition: argv_splitq.c:106
ARGV * argv_splitq(const char *string, const char *delim, const char *parens)
Definition: argv_splitq.c:67
void argv_terminate(ARGV *argvp)
Definition: argv.c:242