Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / sbr / RCS / formataddr.c,v
1 head     1.5;
2 branch   ;
3 access   ;
4 symbols  ;
5 locks    ; strict;
6 comment  @ * @;
7
8
9 1.5
10 date     90.04.05.15.30.56;  author sources;  state Exp;
11 branches ;
12 next     1.4;
13
14 1.4
15 date     90.04.05.14.44.56;  author sources;  state Exp;
16 branches ;
17 next     1.3;
18
19 1.3
20 date     90.03.12.10.49.11;  author sources;  state Exp;
21 branches ;
22 next     1.2;
23
24 1.2
25 date     90.03.12.10.45.56;  author sources;  state Exp;
26 branches ;
27 next     1.1;
28
29 1.1
30 date     90.01.30.10.44.41;  author sources;  state Exp;
31 branches ;
32 next     ;
33
34
35 desc
36 @@
37
38
39 1.5
40 log
41 @add ID
42 @
43 text
44 @/* formataddr.c - format an address field (from formatsbr) */
45 #ifndef lint
46 static char ident[] = "@@(#)$Id:$";
47 #endif  lint
48
49 #include "../h/mh.h"
50 #include "../h/addrsbr.h"
51 #include "../h/formatsbr.h"
52 #include <ctype.h>
53 #include <stdio.h>
54
55 static char *buf;               /* our current working buffer */
56 static char *bufend;            /* end of working buffer */
57 static char *last_dst;          /* buf ptr at end of last call */
58 static unsigned int bufsiz;     /* current size of buf */
59
60 #define BUFINCR 512             /* how much to expand buf when if fills */
61
62 #define CPY(s) { cp = (s); while (*dst++ = *cp++) ; --dst; }
63
64 /* check if there's enough room in buf for str.  add more mem if needed */
65 #define CHECKMEM(str) \
66             if ((len = strlen (str)) >= bufend - dst) {\
67                 int i = dst - buf;\
68                 int n = last_dst - buf;\
69                 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
70                 buf = realloc (buf, bufsiz);\
71                 dst = buf + i;\
72                 last_dst = buf + n;\
73                 if (! buf)\
74                     adios (NULLCP, "formataddr: couldn't get buffer space");\
75                 bufend = buf + bufsiz;\
76             }
77
78
79 /* fmtscan will call this routine if the user includes the function
80  * "(formataddr {component})" in a format string.  "orig" is the
81  * original contents of the string register.  "str" is the address
82  * string to be formatted and concatenated onto orig.  This routine
83  * returns a pointer to the concatenated address string.
84  *
85  * We try to not do a lot of malloc/copy/free's (which is why we
86  * don't call "getcpy") but still place no upper limit on the
87  * length of the result string.
88  *
89  * This routine is placed in a separate library so it can be
90  * overridden by particular programs (e.g., "replsbr").
91  */
92 char *formataddr (orig, str)
93     char *orig;
94     char *str;
95 {
96     register int  len;
97     register int  isgroup;
98     register char  *dst;
99     register char  *cp;
100     register char  *sp;
101     register struct mailname *mp = NULL;
102
103     /* if we don't have a buffer yet, get one */
104     if (bufsiz == 0) {
105         buf = malloc (BUFINCR);
106         if (! buf)
107             adios (NULLCP, "formataddr: couldn't allocate buffer space");
108         last_dst = buf;         /* XXX */
109         bufsiz = BUFINCR - 6;  /* leave some slop */
110         bufend = buf + bufsiz;
111     }
112     /*
113      * If "orig" points to our buffer we can just pick up where we
114      * left off.  Otherwise we have to copy orig into our buffer.
115      */
116     if (orig == buf)
117         dst = last_dst;
118     else if (!orig || !*orig) {
119         dst = buf;
120         *dst = '\0';
121     } else {
122         dst = last_dst;         /* XXX */
123         CHECKMEM (orig);
124         CPY (orig);
125     }
126
127     /* concatenate all the new addresses onto 'buf' */
128     for (isgroup = 0; cp = getname (str); ) {
129         if ((mp = getm (cp, NULLCP, 0, fmt_norm, NULLCP)) == NULL)
130             continue;
131
132         if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
133             *dst++ = ';';
134             isgroup = 0;
135         }
136         /* if we get here we're going to add an address */
137         if (dst != buf) {
138             *dst++ = ',';
139             *dst++ = ' ';
140         }
141         if (mp->m_gname) {
142             CHECKMEM (mp->m_gname);
143             CPY (mp->m_gname);
144             isgroup++;
145         }
146         sp = adrformat (mp);
147         CHECKMEM (sp);
148         CPY (sp);
149         mnfree (mp);
150     }
151
152     if (isgroup)
153         *dst++ = ';';
154
155     *dst = '\0';
156     last_dst = dst;
157     return (buf);
158 }
159 @
160
161
162 1.4
163 log
164 @add ID
165 @
166 text
167 @d3 1
168 a3 1
169 static char ident[] = "$Id:";
170 @
171
172
173 1.3
174 log
175 @dst fix
176 @
177 text
178 @d2 3
179 @
180
181
182 1.2
183 log
184 @dst fix
185 @
186 text
187 @d59 1
188 a59 1
189         buf = malloc (BUFINCR); 
190 @
191
192
193 1.1
194 log
195 @Initial revision
196 @
197 text
198 @d59 1
199 a59 1
200         buf = malloc (BUFINCR);
201 d62 1
202 d76 1
203 @