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