| 1 |
/* $Id$ */ |
| 2 |
/* |
| 3 |
* Copyright (c) 1990-1996 Sam Leffler |
| 4 |
* Copyright (c) 1991-1996 Silicon Graphics, Inc. |
| 5 |
* HylaFAX is a trademark of Silicon Graphics |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 8 |
* its documentation for any purpose is hereby granted without fee, provided |
| 9 |
* that (i) the above copyright notices and this permission notice appear in |
| 10 |
* all copies of the software and related documentation, and (ii) the names of |
| 11 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 12 |
* publicity relating to the software without the specific, prior written |
| 13 |
* permission of Sam Leffler and Silicon Graphics. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 16 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 17 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 18 |
* |
| 19 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 20 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 21 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 22 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 23 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 24 |
* OF THIS SOFTWARE. |
| 25 |
*/ |
| 26 |
#include "MsgFmt.h" |
| 27 |
#include "StackBuffer.h" |
| 28 |
#include "TextFormat.h" |
| 29 |
|
| 30 |
#include <ctype.h> |
| 31 |
|
| 32 |
MsgFmt::MsgFmt() |
| 33 |
{ |
| 34 |
} |
| 35 |
|
| 36 |
MsgFmt::MsgFmt(const MsgFmt& other) |
| 37 |
: boldFont(other.boldFont) |
| 38 |
, italicFont(other.italicFont) |
| 39 |
, headToKeep(other.headToKeep) |
| 40 |
#ifdef notdef |
| 41 |
, headMap(other.headMap) |
| 42 |
#endif |
| 43 |
{ |
| 44 |
headerStop = other.headerStop; |
| 45 |
verbose = other.verbose; |
| 46 |
} |
| 47 |
|
| 48 |
MsgFmt::~MsgFmt() |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
const fxStr* |
| 53 |
MsgFmt::findHeader(const fxStr& name) const |
| 54 |
{ |
| 55 |
for (u_int i = 0, n = fields.length(); i < n; i++) |
| 56 |
if (strcasecmp(fields[i], name) == 0) |
| 57 |
return (&headers[i]); |
| 58 |
return (NULL); |
| 59 |
} |
| 60 |
|
| 61 |
fxStr |
| 62 |
MsgFmt::mapHeader(const fxStr& name) |
| 63 |
{ |
| 64 |
for (fxStrDictIter hi(headMap); hi.notDone(); hi++) |
| 65 |
if (strcasecmp(hi.key(), name) == 0) |
| 66 |
return (hi.value()); |
| 67 |
return (name); |
| 68 |
} |
| 69 |
|
| 70 |
bool |
| 71 |
MsgFmt::getLine(FILE* fd, fxStackBuffer& buf) |
| 72 |
{ |
| 73 |
buf.reset(); |
| 74 |
for (;;) { |
| 75 |
int c = getc(fd); |
| 76 |
if (c == EOF) |
| 77 |
return (buf.getLength() > 0); |
| 78 |
c &= 0xff; |
| 79 |
if (c == '\n') |
| 80 |
break; |
| 81 |
buf.put(c); |
| 82 |
} |
| 83 |
return (true); |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
* This function replaces comments with a single white space. |
| 88 |
* Unclosed comments are automatically closed at end of string. |
| 89 |
* Stray closing parentheses are left untouched, as are other invalid chars. |
| 90 |
* Headers which can contain quoted strings should not go through this |
| 91 |
* revision of this function as is doesn't honnor them and could end up doing |
| 92 |
* the wrong thing. |
| 93 |
*/ |
| 94 |
fxStr |
| 95 |
MsgFmt::stripComments(const fxStr& s) |
| 96 |
{ |
| 97 |
fxStr q; |
| 98 |
u_int depth = 0; |
| 99 |
bool wasSpace = true; |
| 100 |
for (u_int i = 0; i < s.length(); i++) { |
| 101 |
switch (s[i]) { |
| 102 |
case '(': |
| 103 |
depth++; |
| 104 |
break; |
| 105 |
case ')': |
| 106 |
if (depth > 0) |
| 107 |
depth--; |
| 108 |
break; |
| 109 |
case '\\': |
| 110 |
if (depth == 0) { |
| 111 |
q.append(s[i++]); // Don't decode them at this time |
| 112 |
q.append(s[i]); |
| 113 |
wasSpace = false; |
| 114 |
} else |
| 115 |
i++; |
| 116 |
break; |
| 117 |
default: |
| 118 |
if (depth == 0) { |
| 119 |
if (!isspace(s[i]) || !wasSpace) { // Trim consecutive spaces |
| 120 |
q.append(s[i]); |
| 121 |
wasSpace = isspace(s[i]); |
| 122 |
} |
| 123 |
} |
| 124 |
break; |
| 125 |
} |
| 126 |
} |
| 127 |
while (q.length() > 0 && isspace(q[q.length()-1])) |
| 128 |
q.remove(q.length()-1, 1); // Trim trailing white space |
| 129 |
return q; |
| 130 |
} |
| 131 |
|
| 132 |
void |
| 133 |
MsgFmt::parseHeaders(FILE* fd, u_int& lineno) |
| 134 |
{ |
| 135 |
fxStackBuffer buf; |
| 136 |
fxStr field; // current header field |
| 137 |
while (getLine(fd, buf)) { |
| 138 |
lineno++; |
| 139 |
if (buf.getLength() == 0) |
| 140 |
break; |
| 141 |
/* |
| 142 |
* Collect field name in a canonical format. |
| 143 |
* If the line begins with whitespace, then |
| 144 |
* it's the continuation of a previous header. |
| 145 |
*/ |
| 146 |
fxStr line(&buf[0], buf.getLength()); |
| 147 |
u_int len = line.length(); |
| 148 |
while (len > 0 && isspace(line[line.length()-1])) { |
| 149 |
line.remove(line.length()-1, 1); // trim trailing whitespace |
| 150 |
len--; |
| 151 |
} |
| 152 |
if (len > 0 && !isspace(line[0])) { |
| 153 |
u_int l = 0; |
| 154 |
field = line.token(l, ':'); |
| 155 |
if (field != "" && l < len) { // record new header |
| 156 |
fields.append(field); |
| 157 |
// skip leading whitespace |
| 158 |
for (; l < len && isspace(line[l]); l++) |
| 159 |
; |
| 160 |
headers.append(line.tail(len-l)); |
| 161 |
if (verbose) |
| 162 |
fprintf(stderr, _("HEADER %s: %s\n") |
| 163 |
, (const char*) fields[fields.length()-1] |
| 164 |
, (const char*) headers[headers.length()-1] |
| 165 |
); |
| 166 |
} |
| 167 |
} else if (field != "") { // append continuation |
| 168 |
headers[headers.length()-1].append(line); |
| 169 |
if (verbose) |
| 170 |
fprintf(stderr, _("+HEADER %s: %s\n") |
| 171 |
, (const char*) field |
| 172 |
, (const char*) line |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
/* |
| 179 |
* Scan envelope for any meta-headers that |
| 180 |
* control how formatting is to be done. |
| 181 |
*/ |
| 182 |
for (u_int i = 0, n = fields.length(); i < n; i++) { |
| 183 |
const fxStr& field = fields[i]; |
| 184 |
if (strncasecmp(field, "x-fax-", 6) == 0) |
| 185 |
setConfigItem(&field[6], headers[i]); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void |
| 190 |
MsgFmt::setupConfig() |
| 191 |
{ |
| 192 |
verbose = false; |
| 193 |
boldFont = "Helvetica-Bold"; |
| 194 |
italicFont = "Helvetica-Oblique"; |
| 195 |
|
| 196 |
headToKeep.resize(0); |
| 197 |
headToKeep.append("To"); |
| 198 |
headToKeep.append("From"); |
| 199 |
headToKeep.append("Subject"); |
| 200 |
headToKeep.append("Cc"); |
| 201 |
headToKeep.append("Date"); |
| 202 |
|
| 203 |
for (fxStrDictIter iter(headMap); iter.notDone(); iter++) |
| 204 |
headMap.remove(iter.key()); |
| 205 |
} |
| 206 |
|
| 207 |
#undef streq |
| 208 |
#define streq(a,b) (strcasecmp(a,b)==0) |
| 209 |
|
| 210 |
bool |
| 211 |
MsgFmt::setConfigItem(const char* tag, const char* value) |
| 212 |
{ |
| 213 |
if (streq(tag, "headers")) { |
| 214 |
char* cp = strcpy(new char[strlen(value) + 1], value); |
| 215 |
char* tp; |
| 216 |
do { |
| 217 |
tp = strchr(cp, ' '); |
| 218 |
if (tp) { |
| 219 |
*tp++ = '\0'; |
| 220 |
} |
| 221 |
if (streq(cp, "clear")) { |
| 222 |
headToKeep.resize(0); |
| 223 |
} else { |
| 224 |
headToKeep.append(cp); |
| 225 |
} |
| 226 |
} while ((cp = tp)); |
| 227 |
delete [] cp; |
| 228 |
} else if (streq(tag, "mapheader")) { |
| 229 |
char* tp = (char *) strchr(value, ' '); |
| 230 |
if (tp) { |
| 231 |
for (*tp++ = '\0'; isspace(*tp); tp++) |
| 232 |
; |
| 233 |
headMap[value] = tp; |
| 234 |
} |
| 235 |
} else if (streq(tag, "boldfont")) { |
| 236 |
boldFont = value; |
| 237 |
} else if (streq(tag, "italicfont")) { |
| 238 |
italicFont = value; |
| 239 |
} else if (streq(tag, "verbose")) { |
| 240 |
verbose = FaxConfig::getBoolean(tag); |
| 241 |
} else |
| 242 |
return (false); |
| 243 |
return (true); |
| 244 |
} |
| 245 |
#undef streq |
| 246 |
|
| 247 |
u_int |
| 248 |
MsgFmt::headerCount(void) |
| 249 |
{ |
| 250 |
u_int nl = 0; |
| 251 |
for (u_int i = 0, n = headToKeep.length(); i < n; i++) |
| 252 |
if (findHeader(headToKeep[i])) |
| 253 |
nl++; // XXX handle wrapped lines |
| 254 |
return (nl); |
| 255 |
} |
| 256 |
|
| 257 |
#ifdef roundup |
| 258 |
#undef roundup |
| 259 |
#endif |
| 260 |
#define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) |
| 261 |
|
| 262 |
bool |
| 263 |
MsgFmt::formatHeaders(TextFormat& fmt) |
| 264 |
{ |
| 265 |
/* |
| 266 |
* Calculate tab stop for headers based on the |
| 267 |
* maximum width of the headers we are to keep. |
| 268 |
*/ |
| 269 |
const TextFont* bold = fmt.getFont("Bold"); |
| 270 |
if (!bold) |
| 271 |
bold = fmt.addFont("Bold", boldFont); |
| 272 |
headerStop = 0; |
| 273 |
u_int i; |
| 274 |
u_int nHead = headToKeep.length(); |
| 275 |
for (i = 0; i < nHead; i++) { |
| 276 |
TextCoord w = bold->strwidth(mapHeader(headToKeep[i])); |
| 277 |
if (w > headerStop) |
| 278 |
headerStop = w; |
| 279 |
} |
| 280 |
headerStop += bold->charwidth(':'); |
| 281 |
TextCoord boldTab = 8 * bold->charwidth(' '); |
| 282 |
headerStop = roundup(headerStop, boldTab); |
| 283 |
|
| 284 |
/* |
| 285 |
* Format headers we want; embolden field name |
| 286 |
* and italicize field value. We wrap long |
| 287 |
* items to the field tab stop calculated above. |
| 288 |
*/ |
| 289 |
u_int nl = 0; |
| 290 |
for (i = 0; i < nHead; i++) { |
| 291 |
const fxStr* value = findHeader(headToKeep[i]); |
| 292 |
if (value) { |
| 293 |
fmt.beginLine(); |
| 294 |
TextCoord hm = bold->show(fmt.getOutputFile(), |
| 295 |
mapHeader(headToKeep[i]) | ":"); |
| 296 |
fmt.hrMove(headerStop - hm); |
| 297 |
showItalic(fmt, *value); |
| 298 |
fmt.endLine(); |
| 299 |
nl++; |
| 300 |
} |
| 301 |
} |
| 302 |
if (nl > 0) { |
| 303 |
/* |
| 304 |
* Insert a blank line between the envelope and the |
| 305 |
* body. Note that we ``know too much here''--we |
| 306 |
* know to insert whitespace below to insure valid |
| 307 |
* PostScript is generated (sigh). |
| 308 |
*/ |
| 309 |
fmt.beginLine(); |
| 310 |
fputc(' ', fmt.getOutputFile()); // XXX whitespace needed |
| 311 |
fmt.endLine(); |
| 312 |
return true; |
| 313 |
} |
| 314 |
return false; |
| 315 |
} |
| 316 |
|
| 317 |
/* |
| 318 |
* Display the string in italic, wrapping to the |
| 319 |
* field header tab stop on any line overflows. |
| 320 |
*/ |
| 321 |
void |
| 322 |
MsgFmt::showItalic(TextFormat& fmt, const char* cp) |
| 323 |
{ |
| 324 |
const TextFont* italic = fmt.getFont("Italic"); |
| 325 |
if (!italic) |
| 326 |
italic = fmt.addFont("Italic", italicFont); |
| 327 |
while (isspace(*cp)) // trim leading whitespace |
| 328 |
cp++; |
| 329 |
TextCoord x = fmt.getXOff(); // current x position on line |
| 330 |
FILE* tf = fmt.getOutputFile(); // output stream |
| 331 |
const char* tp = cp; |
| 332 |
for (; *tp != '\0'; tp++) { |
| 333 |
TextCoord hm = italic->charwidth(*tp); |
| 334 |
if (*tp == '\n' || x+hm > fmt.getRHS()) {// text would overflow line |
| 335 |
italic->show(tf, cp, tp-cp), cp = tp;// flush pending text |
| 336 |
if (!fmt.getLineWrapping()) // truncate line, don't wrap |
| 337 |
return; |
| 338 |
fmt.endLine(); // terminate line |
| 339 |
fmt.beginLine(); // begin another line |
| 340 |
fmt.hrMove(headerStop); // reposition to header stop |
| 341 |
x = fmt.getXOff(); |
| 342 |
if (*tp == '\n') { // remove leading white space |
| 343 |
for (tp++; isspace(*tp); tp++) |
| 344 |
; |
| 345 |
cp = --tp; |
| 346 |
} |
| 347 |
} |
| 348 |
x += hm; |
| 349 |
} |
| 350 |
if (tp > cp) |
| 351 |
italic->show(tf, cp, tp-cp); // flush remainder |
| 352 |
} |