2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: utils.C /main/4 1996/04/21 19:44:10 drk $ */
30 * RESTRICTED CONFIDENTIAL INFORMATION:
32 * The information in this document is subject to special
33 * restrictions in a confidential disclosure agreement between
34 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
35 * document outside HP, IBM, Sun, USL, SCO, or Univel without
36 * Sun's specific written approval. This document and all copies
37 * and derivative works thereof must be returned or destroyed at
40 * Copyright 1994 Sun Microsystems, Inc. All rights reserved.
47 // fn proto for mailx fn
48 static char * dispname(const char *hdr);
50 // error-handling: should do something with minor codes
53 handleError (DtMailEnv &dterror, char *msg)
55 if (dterror.isSet () == DTM_TRUE)
57 fprintf (stderr, "dtmailpr: (%s) %s\n",
58 msg, (const char *)dterror);
59 dterror.logError (DTM_FALSE, "dtmailpr: (%s) %s\n",
60 msg, (const char *)dterror);
72 errorString (enum DmxHeaders hdr)
79 return "(no subject)";
85 return "(unknown date)";
95 formatHeader (DtMailHeaderLine &info, enum DmxHeaderType htype)
97 int i = 0, length = 0;
101 const char *header [DMXNUMHDRS];
103 for (i = 0; i < DMXNUMHDRS; i++)
105 length = info.header_values[i].length ();
108 header [i] = errorString ((enum DmxHeaders)i);
110 header [i] = *((info.header_values[i])[0]);
114 for (i = 0; i < DMXNUMHDRS; i++)
116 buflength += strlen (header [i]);
119 fbuf = new char [buflength + 64];
125 "%-18.18s %-16.16s %4d/%-5s %-.25s",
126 dispname (header [DMXFROM]),
128 atoi (header [DMXCLENGTH]) / 40,
135 "From: %s\nDate: %s\nTo: %s\nSubject: %s\n",
136 dispname (header [DMXFROM]),
146 return (fbuf); //need to free this after using it
149 // stuff grabbed from mailx...it's ugly, but it looks pretty
151 #define NOSTR ((char *) 0) /* Nill string pointer */
152 #define LINESIZE 5120 /* max readable line width */
153 static char *phrase(char *, int , int );
156 * Return a pointer to a dynamic copy of the argument.
158 // changed salloc to malloc
162 register char *cp, *cp2, *top;
164 for (cp = str; *cp; cp++)
166 top = (char *)malloc((unsigned)(cp-str + 1));
169 for (cp = str, cp2 = top; *cp; cp++)
179 return phrase(name, 0, 0);
182 * Return the full name from an RFC-822 header line
183 * or the last two (or one) component of the address.
187 dispname(const char *hdr)
188 // made it a const char * instead of a char *
194 if (((cp = const_cast <char *> (strchr(hdr, '<'))) != 0) && (cp > hdr)) {
196 if ((*hdr == '"') && ((cp = const_cast <char *> (strrchr(++hdr, '"'))) != 0))
199 } else if ((cp = const_cast <char *> (strchr(hdr, '('))) != 0) {
201 if ((cp = const_cast <char *> (strchr(hdr, '+'))) != 0)
203 if ((cp = const_cast <char *> (strrchr(hdr, ')'))) != 0)
207 cp = skin((char *)hdr);
208 if ((cp2 = strrchr(cp, '!')) != 0) {
209 while (cp2 >= cp && *--cp2 != '!');
216 #define equal(a, b) (strcmp(a,b)==0)/* A nice function to string compare */
219 * Skin an arpa net address according to the RFC 822 interpretation
222 // changed salloc to malloc
224 phrase(char *name, int token, int comma)
227 register char *cp, *cp2;
228 char *bufend, *nbufp;
229 int gotlt, lastsp, didq;
235 if (strlen(name) >= (unsigned)LINESIZE)
236 nbufp = (char *)malloc(strlen(name));
242 for (cp = name, cp2 = bufend; (c = *cp++) != 0;) {
246 Start of a comment, ignore it.
249 while ((c = *cp) != 0) {
253 if (*cp == 0) goto outcm;
263 if (nesting <= 0) break;
270 Start a quoted string.
271 Copy it in its entirety.
274 while ((c = *cp) != 0) {
278 if ((c = *cp) == 0) goto outqs;
284 if (gotlt == 0 || gotlt == '<') {
305 if (token && (!comma || c == '\n')) {
335 /* FALLTHROUGH . . . */
338 if (gotlt == 0 || gotlt == '<') {
349 return (token ? --cp : equal(name, nbufp) ? name :
350 nbufp == nbuf ? savestr(nbuf) : nbufp);