Start
[exim.git] / src / src / lookups / lf_quote.c
1 /* $Cambridge: exim/src/src/lookups/lf_quote.c,v 1.1 2004/10/07 13:10:01 ph10 Exp $ */
2
3 /*************************************************
4 *     Exim - an Internet mail transport agent    *
5 *************************************************/
6
7 /* Copyright (c) University of Cambridge 1995 - 2004 */
8 /* See the file NOTICE for conditions of use and distribution. */
9
10
11 #include "../exim.h"
12 #include "lf_functions.h"
13
14
15 /*************************************************
16 *   Add string to result, quoting if necessary   *
17 *************************************************/
18
19 /* This function is called by some lookups that create name=value result
20 strings, to handle the quoting of the data. It adds "name=" to the result,
21 followed by appropriately quoted data, followed by a single space.
22
23 Arguments:
24   name           the field name
25   value          the data value
26   vlength        the data length
27   result         the result pointer
28   asize          points to the size variable
29   aoffset        points to the offset variable
30
31 Returns:         the result pointer (possibly updated)
32 */
33
34 uschar *
35 lf_quote(uschar *name, uschar *value, int vlength, uschar *result, int *asize,
36   int *aoffset)
37 {
38 result = string_append(result, asize, aoffset, 2, name, US"=");
39
40 /* NULL is handled as an empty string */
41
42 if (value == NULL) value = US"";
43
44 /* Quote the value if it is empty, contains white space, or starts with a quote
45 character. */
46
47 if (value[0] == 0 || Ustrpbrk(value, " \t\n\r") != NULL || value[0] == '\"')
48   {
49   int j;
50   result = string_cat(result, asize, aoffset, US"\"", 1);
51   for (j = 0; j < vlength; j++)
52     {
53     if (value[j] == '\"' || value[j] == '\\')
54       result = string_cat(result, asize, aoffset, US"\\", 1);
55     result = string_cat(result, asize, aoffset, US value+j, 1);
56     }
57   result = string_cat(result, asize, aoffset, US"\"", 1);
58   }
59 else
60   {
61   result = string_cat(result, asize, aoffset, US value, vlength);
62   }
63
64 return string_cat(result, asize, aoffset, US" ", 1);
65 }
66
67 /* End of lf_quote.c */