Added -nocontentid (and -contentid, for symmetry) switch to mhbuild. This allows...
[mmh] / sbr / fmt_addr.c
1
2 /*
3  * fmt_addr.c -- format an address field (from fmt_scan)
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  */
11
12 #include <h/mh.h>
13 #include <h/addrsbr.h>
14 #include <h/fmt_scan.h>
15 #include <h/utils.h>
16
17 static char *buf;               /* our current working buffer  */
18 static char *bufend;            /* end of working buffer       */
19 static char *last_dst;          /* buf ptr at end of last call */
20 static unsigned int bufsiz;     /* current size of buf         */
21
22 #define BUFINCR 512             /* how much to expand buf when if fills */
23
24 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
25
26 /* check if there's enough room in buf for str.  add more mem if needed */
27 #define CHECKMEM(str) \
28             if ((len = strlen (str)) >= bufend - dst) {\
29                 int i = dst - buf;\
30                 int n = last_dst - buf;\
31                 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
32                 buf = mh_xrealloc (buf, bufsiz);\
33                 dst = buf + i;\
34                 last_dst = buf + n;\
35                 bufend = buf + bufsiz;\
36             }
37
38
39 /* fmt_scan will call this routine if the user includes the function
40  * "(formataddr {component})" in a format string.  "orig" is the
41  * original contents of the string register.  "str" is the address
42  * string to be formatted and concatenated onto orig.  This routine
43  * returns a pointer to the concatenated address string.
44  *
45  * We try to not do a lot of malloc/copy/free's (which is why we
46  * don't call "getcpy") but still place no upper limit on the
47  * length of the result string.
48  *
49  * This routine is placed in a separate library so it can be
50  * overridden by particular programs (e.g., "replsbr").
51  */
52
53 char *
54 formataddr (char *orig, char *str)
55 {
56     register int len;
57     register int isgroup;
58     register char *dst;
59     register char *cp;
60     register char *sp;
61     register struct mailname *mp = NULL;
62
63     /* if we don't have a buffer yet, get one */
64     if (bufsiz == 0) {
65         buf = mh_xmalloc (BUFINCR);
66         last_dst = buf;         /* XXX */
67         bufsiz = BUFINCR - 6;  /* leave some slop */
68         bufend = buf + bufsiz;
69     }
70     /*
71      * If "orig" points to our buffer we can just pick up where we
72      * left off.  Otherwise we have to copy orig into our buffer.
73      */
74     if (orig == buf)
75         dst = last_dst;
76     else if (!orig || !*orig) {
77         dst = buf;
78         *dst = '\0';
79     } else {
80         dst = last_dst;         /* XXX */
81         CHECKMEM (orig);
82         CPY (orig);
83     }
84
85     /* concatenate all the new addresses onto 'buf' */
86     for (isgroup = 0; (cp = getname (str)); ) {
87         if ((mp = getm (cp, NULL, 0, fmt_norm, NULL)) == NULL)
88             continue;
89
90         if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
91             *dst++ = ';';
92             isgroup = 0;
93         }
94         /* if we get here we're going to add an address */
95         if (dst != buf) {
96             *dst++ = ',';
97             *dst++ = ' ';
98         }
99         if (mp->m_gname) {
100             CHECKMEM (mp->m_gname);
101             CPY (mp->m_gname);
102             isgroup++;
103         }
104         sp = adrformat (mp);
105         CHECKMEM (sp);
106         CPY (sp);
107         mnfree (mp);
108     }
109
110     if (isgroup)
111         *dst++ = ';';
112
113     *dst = '\0';
114     last_dst = dst;
115     return (buf);
116 }