Add '3rd-party/xfpt/' from commit '24eaa721effcf2f56d1da62344ee27ac9721d3ec'
[exim.git] / 3rd-party / xfpt / src / misc.c
1 /*************************************************
2 *     xfpt - Simple ASCII->Docbook processor     *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge, 2006 */
6 /* Written by Philip Hazel. */
7
8 /* This module contains a number of miscellaneous small utility functions. */
9
10
11 #include "xfpt.h"
12
13
14
15 /*************************************************
16 *              Detrail a line                    *
17 *************************************************/
18
19 /* This removes all white space, including newlines, at the end of a string.
20
21 Argument:   the string
22 Returns:    nothing
23 */
24
25 void
26 misc_detrail(uschar *p)
27 {
28 uschar *q = p + Ustrlen(p);
29 while (q > p && isspace(q[-1])) q--;
30 *q = 0;
31 }
32
33
34 /*************************************************
35 *               Malloc with check                *
36 *************************************************/
37
38 /* The program dies if the memory is not available.
39
40 Argument:    size required
41 Returns:     pointer
42 */
43
44 void *
45 misc_malloc(int size)
46 {
47 void *yield = malloc(size);
48 if (yield == NULL) error(1, size);   /* Fatal error */
49 return yield;
50 }
51
52
53 /*************************************************
54 *        Copy a string into malloc memory        *
55 *************************************************/
56
57 /*
58 Arguments:
59   p            pointer to start
60   length       length
61
62 Returns:       pointer to the copied string
63 */
64
65 uschar *
66 misc_copystring(uschar *p, int length)
67 {
68 uschar *yield = misc_malloc(length + 1);
69 memcpy(yield, p, length);
70 yield[length] = 0;
71 return yield;
72 }
73
74
75
76
77 /*************************************************
78 *              Read string in quotes             *
79 *************************************************/
80
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
83 or to heap memory.
84
85 Arguments:
86   p           points to the opening quote
87   lptr        if non-NULL, where to return number of characters consumed,
88                 including the quotes
89   buffer      NULL => get heap memory, else pointer to buffer
90   blength     size of buffer
91
92 Returns:      pointer to the copied string
93 */
94
95 uschar *
96 misc_readstring(uschar *p, int *lptr, uschar *buffer, int blength)
97 {
98 int term = *p;
99 int length;
100 uschar *pp, *yield;
101
102 for (pp = p + 1;; pp++)
103   {
104   if (*pp == 0) break;
105   if (*pp == term) { if (pp[1] != term) break; pp++; }
106   }
107
108 length = pp - p;   /* stringlength, over-estimate if any doubled */
109 if (lptr != NULL) *lptr = length + 1;
110
111 if (buffer == NULL)
112   {
113   yield = pp = misc_malloc(length + 1);
114   }
115 else
116   {
117   if (length + 1 > blength) error(20, length + 1, blength);  /* Hard */
118   yield = pp = buffer;
119   }
120
121 for (++p;; p++)
122   {
123   if (*p == 0) break;
124   if (*p == term) { if (p[1] != term) break; p++; }
125   *pp++ = *p;
126   }
127
128 *pp = 0;
129
130 return yield;
131 }
132
133
134
135 /*************************************************
136 *        Read a possibly quoted item             *
137 *************************************************/
138
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.
142
143 Arguments:
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
149
150 Returns:      pointer to the string, in heap memory
151 */
152
153 uschar *
154 misc_readitem(uschar *p, uschar *term, int *lptr, uschar *buffer, int blength)
155 {
156 uschar *yield;
157 int length;
158
159 if (*p == '\"' || *p == '\'')
160   {
161   yield = misc_readstring(p, &length, buffer, blength);
162   p += length;
163   }
164
165 else
166   {
167   uschar *pp = p;
168   if (term == NULL)
169     while (*p != 0 && !isspace(*p)) p++;
170   else
171     while (Ustrchr(term, *p) == NULL) p++;   /* NB zero will match */
172
173   length = p - pp;
174   if (buffer == NULL)
175     {
176     yield = misc_malloc(length + 1);
177     }
178   else
179     {
180     if (length + 1 > blength) error(20, length + 1, blength);  /* Hard */
181     yield = buffer;
182     }
183   memcpy(yield, pp, length);
184   yield[length] = 0;
185   }
186
187 while (isspace(*p))
188   {
189   p++;
190   length++;
191   }
192
193 if (lptr != NULL) *lptr = length;
194 return yield;
195 }
196
197
198 /* End of misc.c */