1 /*************************************************
2 * xfpt - Simple ASCII->Docbook processor *
3 *************************************************/
5 /* Copyright (c) University of Cambridge, 2006 */
6 /* Written by Philip Hazel. */
8 /* This module contains a number of miscellaneous small utility functions. */
15 /*************************************************
17 *************************************************/
19 /* This removes all white space, including newlines, at the end of a string.
26 misc_detrail(uschar *p)
28 uschar *q = p + Ustrlen(p);
29 while (q > p && isspace(q[-1])) q--;
34 /*************************************************
36 *************************************************/
38 /* The program dies if the memory is not available.
40 Argument: size required
47 void *yield = malloc(size);
48 if (yield == NULL) error(1, size); /* Fatal error */
53 /*************************************************
54 * Copy a string into malloc memory *
55 *************************************************/
62 Returns: pointer to the copied string
66 misc_copystring(uschar *p, int length)
68 uschar *yield = misc_malloc(length + 1);
69 memcpy(yield, p, length);
77 /*************************************************
78 * Read string in quotes *
79 *************************************************/
81 /* Enter pointing to the opening quote, either single or double. Use
82 quote-doubling to include the quote. The string is copied into a given buffer
86 p points to the opening quote
87 lptr if non-NULL, where to return number of characters consumed,
89 buffer NULL => get heap memory, else pointer to buffer
90 blength size of buffer
92 Returns: pointer to the copied string
96 misc_readstring(uschar *p, int *lptr, uschar *buffer, int blength)
102 for (pp = p + 1;; pp++)
105 if (*pp == term) { if (pp[1] != term) break; pp++; }
108 length = pp - p; /* stringlength, over-estimate if any doubled */
109 if (lptr != NULL) *lptr = length + 1;
113 yield = pp = misc_malloc(length + 1);
117 if (length + 1 > blength) error(20, length + 1, blength); /* Hard */
124 if (*p == term) { if (p[1] != term) break; p++; }
135 /*************************************************
136 * Read a possibly quoted item *
137 *************************************************/
139 /* If the item is not in quotes, it is terminated by one of a list of
140 terminators, or alternatively, by white space. The number of characters
141 consumed includes any trailing spaces, but not a terminator character.
144 p pointer to the first significant character in the input
145 term if non-NULL, contains the possible terminators
146 lptr if non-NULL, where to return the number of characters consumed
147 buffer NULL => get heap memory, else pointer to buffer
148 blength size of buffer
150 Returns: pointer to the string, in heap memory
154 misc_readitem(uschar *p, uschar *term, int *lptr, uschar *buffer, int blength)
159 if (*p == '\"' || *p == '\'')
161 yield = misc_readstring(p, &length, buffer, blength);
169 while (*p != 0 && !isspace(*p)) p++;
171 while (Ustrchr(term, *p) == NULL) p++; /* NB zero will match */
176 yield = misc_malloc(length + 1);
180 if (length + 1 > blength) error(20, length + 1, blength); /* Hard */
183 memcpy(yield, pp, length);
193 if (lptr != NULL) *lptr = length;