| 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 "StrArray.h" |
| 27 |
#include "FaxDB.h" |
| 28 |
#include "config.h" |
| 29 |
#include "PageSize.h" |
| 30 |
#include "Sys.h" |
| 31 |
|
| 32 |
#include "NLS.h" |
| 33 |
|
| 34 |
#include <stdarg.h> |
| 35 |
#include <ctype.h> |
| 36 |
#include <pwd.h> |
| 37 |
#include <sys/file.h> |
| 38 |
|
| 39 |
class faxCoverApp { |
| 40 |
private: |
| 41 |
fxStr appName; // for error messages |
| 42 |
fxStr cover; // prototype cover sheet |
| 43 |
fxStr dbName; // .faxdb filename |
| 44 |
FaxDB* db; // fax machine database |
| 45 |
fxStr toName; // to person's name |
| 46 |
fxStr toFaxNumber; // to's fax number |
| 47 |
fxStr toVoiceNumber; // to's voice number |
| 48 |
fxStr toLocation; // to's geographical location |
| 49 |
fxStr toCompany; // to's company/institution |
| 50 |
fxStr fromFaxNumber; // sender's fax number |
| 51 |
fxStr fromVoiceNumber; // sender's voice number |
| 52 |
fxStr fromLocation; // sender's geographical location |
| 53 |
fxStr fromCompany; // sender's company/institution |
| 54 |
fxStr fromMailAddr; // sender's email address |
| 55 |
fxStr regarding; // fax is regarding... |
| 56 |
fxStr comments; // general comments |
| 57 |
fxStr sender; // sender's identity |
| 58 |
fxStr pageCount; // # pages, not counting cover page |
| 59 |
fxStr dateFmt; // strftime format string |
| 60 |
float pageWidth; // page width (mm) |
| 61 |
float pageLength; // page length (mm) |
| 62 |
int maxcomments; // max # of comment lines |
| 63 |
int maxlencomments; // max length of comment lines |
| 64 |
|
| 65 |
static const char* prologue; |
| 66 |
|
| 67 |
fxStr tildeExpand(const fxStr& filename); |
| 68 |
void setupPageSize(const char* name); |
| 69 |
void emitToDefs(const char* to, FaxDBRecord* rec); |
| 70 |
void emitFromDefs(FaxDBRecord* rec); |
| 71 |
void emitCommentDefs(); |
| 72 |
void emitDateDefs(); |
| 73 |
void coverDef(const char* tag, const char* value); |
| 74 |
void makeCoverSheet(); |
| 75 |
void usage(); |
| 76 |
void printError(const char* va_alist ...); |
| 77 |
public: |
| 78 |
faxCoverApp(); |
| 79 |
~faxCoverApp(); |
| 80 |
|
| 81 |
void initialize(int argc, char** argv); |
| 82 |
void open(); |
| 83 |
}; |
| 84 |
|
| 85 |
faxCoverApp::faxCoverApp() |
| 86 |
: cover(FAX_COVER) |
| 87 |
, dbName("~/.faxdb") |
| 88 |
, dateFmt("%a %b %d %Y, %H:%M %Z") |
| 89 |
{ |
| 90 |
db = 0; |
| 91 |
maxcomments = 20; |
| 92 |
maxlencomments = 35; |
| 93 |
} |
| 94 |
|
| 95 |
faxCoverApp::~faxCoverApp() |
| 96 |
{ |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
faxCoverApp::initialize(int argc, char** argv) |
| 101 |
{ |
| 102 |
extern char* optarg; |
| 103 |
int c; |
| 104 |
|
| 105 |
appName = argv[0]; |
| 106 |
u_int l = appName.length(); |
| 107 |
appName = appName.tokenR(l, '/'); |
| 108 |
|
| 109 |
const char* cp = getenv("FAXCOVER"); |
| 110 |
if (cp && *cp) |
| 111 |
cover = cp; |
| 112 |
|
| 113 |
setupPageSize("default"); |
| 114 |
while ((c = Sys::getopt(argc, argv, "C:D:L:M:N:V:X:n:t:f:c:p:l:m:r:s:v:x:z:")) != -1) |
| 115 |
switch (c) { |
| 116 |
case 's': // page size |
| 117 |
setupPageSize(optarg); |
| 118 |
break; |
| 119 |
case 'C': // cover sheet |
| 120 |
cover = optarg; |
| 121 |
break; |
| 122 |
case 'D': // date format string |
| 123 |
dateFmt = optarg; |
| 124 |
break; |
| 125 |
case 'L': // sender's geographic location |
| 126 |
fromLocation = optarg; |
| 127 |
break; |
| 128 |
case 'M': // sender's email address |
| 129 |
fromMailAddr = optarg; |
| 130 |
break; |
| 131 |
case 'N': // sender's fax number |
| 132 |
fromFaxNumber = optarg; |
| 133 |
break; |
| 134 |
case 'V': // sender's voice number |
| 135 |
fromVoiceNumber = optarg; |
| 136 |
break; |
| 137 |
case 'X': // sender's company/institution |
| 138 |
fromCompany = optarg; |
| 139 |
break; |
| 140 |
case 'm': // max # comment lines |
| 141 |
maxcomments = atoi(optarg); |
| 142 |
break; |
| 143 |
case 'z': // max length comment lines |
| 144 |
maxlencomments = atoi(optarg); |
| 145 |
break; |
| 146 |
case 'n': // fax number |
| 147 |
toFaxNumber = optarg; |
| 148 |
break; |
| 149 |
case 'r': // regarding |
| 150 |
regarding = optarg; |
| 151 |
break; |
| 152 |
case 't': // to identity |
| 153 |
toName = optarg; |
| 154 |
break; |
| 155 |
case 'f': // from identity |
| 156 |
sender = optarg; |
| 157 |
break; |
| 158 |
case 'c': // comments |
| 159 |
comments = optarg; |
| 160 |
break; |
| 161 |
case 'p': // page count |
| 162 |
pageCount = optarg; |
| 163 |
break; |
| 164 |
case 'l': // to's location |
| 165 |
toLocation = optarg; |
| 166 |
break; |
| 167 |
case 'v': // to's voice phone number |
| 168 |
toVoiceNumber = optarg; |
| 169 |
break; |
| 170 |
case 'x': // to's company |
| 171 |
toCompany = optarg; |
| 172 |
break; |
| 173 |
case '?': |
| 174 |
usage(); |
| 175 |
} |
| 176 |
if (sender == "" || toFaxNumber == "") |
| 177 |
usage(); |
| 178 |
} |
| 179 |
|
| 180 |
extern void fxFatal(const char* fmt, ...); |
| 181 |
|
| 182 |
void |
| 183 |
faxCoverApp::setupPageSize(const char* name) |
| 184 |
{ |
| 185 |
PageSizeInfo* info = PageSizeInfo::getPageSizeByName(name); |
| 186 |
if (!info) |
| 187 |
fxFatal(_("Unknown page size \"%s\""), name); |
| 188 |
pageWidth = info->width(); |
| 189 |
pageLength = info->height(); |
| 190 |
delete info; |
| 191 |
} |
| 192 |
|
| 193 |
void |
| 194 |
faxCoverApp::open() |
| 195 |
{ |
| 196 |
if (!db) |
| 197 |
db = new FaxDB(tildeExpand(dbName)); |
| 198 |
makeCoverSheet(); |
| 199 |
} |
| 200 |
|
| 201 |
void |
| 202 |
faxCoverApp::usage() |
| 203 |
{ |
| 204 |
fxFatal(_("usage: %s" |
| 205 |
" [-t to]" |
| 206 |
" [-c comments]" |
| 207 |
" [-p #pages]" |
| 208 |
" [-l to-location]" |
| 209 |
" [-m maxcomments]" |
| 210 |
" [-z maxlencomments]" |
| 211 |
" [-r regarding]" |
| 212 |
" [-v to-voice-number]" |
| 213 |
" [-x to-company]" |
| 214 |
" [-C template-file]" |
| 215 |
" [-D date-format]" |
| 216 |
" [-L from-location]" |
| 217 |
" [-M from-mail-address]" |
| 218 |
" [-N from-fax-number]" |
| 219 |
" [-V from-voice-number]" |
| 220 |
" [-X from-company]" |
| 221 |
" [-s pagesize]" |
| 222 |
" -f from" |
| 223 |
" -n fax-number") |
| 224 |
, (const char*) appName); |
| 225 |
} |
| 226 |
|
| 227 |
const char* faxCoverApp::prologue = "\ |
| 228 |
/wordbreak ( ) def\n\ |
| 229 |
/linebreak (\\n) def\n\ |
| 230 |
/doLine {\n\ |
| 231 |
% <line> <width> <height> <x> <y> doLine <width> <height> <x> <y>\n\ |
| 232 |
2 copy moveto 5 -1 roll\n\ |
| 233 |
wordbreak\n\ |
| 234 |
{\n\ |
| 235 |
search {\n\ |
| 236 |
dup stringwidth pop currentpoint pop add 7 index 6 index add gt {\n\ |
| 237 |
6 3 roll 2 index sub 2 copy moveto 6 3 roll\n\ |
| 238 |
} if\n\ |
| 239 |
show wordbreak show\n\ |
| 240 |
}{\n\ |
| 241 |
dup stringwidth pop currentpoint pop add 5 index 4 index add gt {\n\ |
| 242 |
3 1 roll 3 index sub 2 copy moveto 3 -1 roll\n\ |
| 243 |
} if\n\ |
| 244 |
show exit\n\ |
| 245 |
} ifelse\n\ |
| 246 |
} loop\n\ |
| 247 |
2 index sub 2 copy moveto\n\ |
| 248 |
} def\n\ |
| 249 |
/BreakIntoLines {\n\ |
| 250 |
% <width> <height> <x> <y> <text> BreakIntoLines\n\ |
| 251 |
linebreak\n\ |
| 252 |
{\n\ |
| 253 |
search {\n\ |
| 254 |
7 3 roll doLine 6 -2 roll\n\ |
| 255 |
}{\n\ |
| 256 |
5 1 roll doLine exit\n\ |
| 257 |
} ifelse\n\ |
| 258 |
} loop\n\ |
| 259 |
pop pop pop pop\n\ |
| 260 |
} def\n\ |
| 261 |
/BreakIntoCommentX {\n\ |
| 262 |
% <maxlines> <text> BreakIntoCommentX -\n\ |
| 263 |
/cbuf (Comment ) def\n\ |
| 264 |
0 exch\n\ |
| 265 |
linebreak { search { 4 -1 roll 1 add 4 2 roll }{ exch 1 add exit } ifelse } loop\n\ |
| 266 |
dup dup 2 add 1 roll\n\ |
| 267 |
-1 1 { cbuf exch 7 exch 48 add put cbuf cvn exch def } for\n\ |
| 268 |
1 add exch 1 exch { cbuf exch 7 exch 48 add put cbuf cvn () def } for\n\ |
| 269 |
} def\n\ |
| 270 |
/XtoCommentsX {\n\ |
| 271 |
% <X> XtoCommentsX <commentsX>\n\ |
| 272 |
3 string cvs (comments) dup length dup 4 1 roll\n\ |
| 273 |
2 index length add string dup 0 4 -1 roll\n\ |
| 274 |
putinterval dup 4 -2 roll putinterval\n\ |
| 275 |
} def\n\ |
| 276 |
/BreakIntoCommentsX {\n\ |
| 277 |
% <maxlines> <text> BreakIntoCommentsX -\n\ |
| 278 |
exch 1 1 3 2 roll\n\ |
| 279 |
{ XtoCommentsX cvn () def } for\n\ |
| 280 |
dup length string copy 0 1 index 0 4 1 roll\n\ |
| 281 |
{ linebreak 0 get eq {\n\ |
| 282 |
exch dup 0 3 index getinterval 4 -1 roll 1 add dup 5 1 roll\n\ |
| 283 |
XtoCommentsX cvn exch def dup length 2 index sub 1 sub\n\ |
| 284 |
2 index 1 add exch getinterval exch pop 0\n\ |
| 285 |
}{ 1 add } ifelse\n\ |
| 286 |
dup MaxLenComments gt {\n\ |
| 287 |
exch MaxLenComments 1 sub -1 0 {\n\ |
| 288 |
2 copy get wordbreak 0 get eq {\n\ |
| 289 |
mark 4 1 roll\n\ |
| 290 |
{ 2 copy 1 add 1 index length 1 index 1 add sub\n\ |
| 291 |
getinterval 5 -1 roll search { 3 -2 roll pop pop } if\n\ |
| 292 |
length MaxLenComments gt { 4 -1 roll exec\n\ |
| 293 |
}{ false } ifelse\n\ |
| 294 |
}\n\ |
| 295 |
{ true }\n\ |
| 296 |
5 1 roll linebreak 1 index wordbreak 7 3 roll exec\n\ |
| 297 |
counttomark 1 add 4 roll cleartomark { pop exit } if\n\ |
| 298 |
2 copy 1 add 0 exch getinterval 5 -1 roll\n\ |
| 299 |
1 add dup 6 1 roll XtoCommentsX cvn exch def\n\ |
| 300 |
2 copy 1 add 1 index length 1 index sub getinterval\n\ |
| 301 |
3 -1 roll pop 3 -2 roll 1 add sub exch exit\n\ |
| 302 |
} if\n\ |
| 303 |
pop\n\ |
| 304 |
} for\n\ |
| 305 |
exch dup MaxLenComments gt {\n\ |
| 306 |
pop dup 0 MaxLenComments getinterval 3 -1 roll\n\ |
| 307 |
1 add dup 4 1 roll XtoCommentsX cvn exch def\n\ |
| 308 |
dup length MaxLenComments sub\n\ |
| 309 |
MaxLenComments exch getinterval 1\n\ |
| 310 |
} if\n\ |
| 311 |
}if\n\ |
| 312 |
} forall\n\ |
| 313 |
pop exch 1 add XtoCommentsX cvn exch def\n\ |
| 314 |
} def\n\ |
| 315 |
"; |
| 316 |
|
| 317 |
void |
| 318 |
faxCoverApp::makeCoverSheet() |
| 319 |
{ |
| 320 |
int fd; |
| 321 |
if (cover.length() > 0 && cover[0] != '/') { |
| 322 |
fd = Sys::open(tildeExpand("~/" | cover), O_RDONLY); |
| 323 |
if (fd < 0) |
| 324 |
fd = Sys::open(fxStr(FAX_LIBDATA) | "/" | cover, O_RDONLY); |
| 325 |
} else |
| 326 |
fd = Sys::open(cover, O_RDONLY); |
| 327 |
if (fd < 0) { |
| 328 |
printError(_("Could not locate prototype cover sheet \"%s\""), |
| 329 |
(const char*) cover); |
| 330 |
return; |
| 331 |
} |
| 332 |
printf("%%!PS-Adobe-2.0 EPSF-2.0\n"); |
| 333 |
printf("%%%%Creator: faxcover\n"); |
| 334 |
printf("%%%%Title: HylaFAX Cover Sheet\n"); |
| 335 |
time_t t = time(0); |
| 336 |
printf("%%%%CreationDate: %s", ctime(&t)); |
| 337 |
printf("%%%%Origin: 0 0\n"); |
| 338 |
printf("%%%%BoundingBox: 0 0 %.0f %.0f\n", |
| 339 |
(pageWidth/25.4)*72, (pageLength/25.4)*72); |
| 340 |
printf("%%%%Pages: 1 +1\n"); |
| 341 |
printf("%%%%EndComments\n"); |
| 342 |
printf("%%%%BeginProlog\n"); |
| 343 |
printf("%i dict begin\n", maxcomments*2 + 80); |
| 344 |
printf("%s", prologue); |
| 345 |
emitToDefs(toName, toName != "" ? db->find(toName) : (FaxDBRecord*) NULL); |
| 346 |
printf("/pageWidth %.2f def\n", pageWidth); |
| 347 |
printf("/pageLength %.2f def\n", pageLength); |
| 348 |
emitFromDefs(db->find(sender)); |
| 349 |
coverDef("page-count", pageCount); |
| 350 |
emitDateDefs(); |
| 351 |
coverDef("regarding", regarding); |
| 352 |
emitCommentDefs(); |
| 353 |
printf("/MaxComments %i def\n", maxcomments); |
| 354 |
printf("MaxComments comments BreakIntoCommentX\n"); |
| 355 |
printf("/MaxLenComments %i def\n", maxlencomments); |
| 356 |
printf("MaxComments comments BreakIntoCommentsX\n"); |
| 357 |
printf("%%%%EndProlog\n"); |
| 358 |
printf("%%%%Page: \"1\" 1\n"); |
| 359 |
// copy prototype cover page |
| 360 |
char buf[16*1024]; |
| 361 |
int n; |
| 362 |
while ((n = read(fd, buf, sizeof (buf))) > 0) |
| 363 |
fwrite(buf, n, 1, stdout); |
| 364 |
Sys::close(fd); |
| 365 |
printf("\nend\n"); |
| 366 |
} |
| 367 |
|
| 368 |
void |
| 369 |
faxCoverApp::emitToDefs(const char* to, FaxDBRecord* rec) |
| 370 |
{ |
| 371 |
if (rec) { |
| 372 |
to = rec->find(FaxDB::nameKey); |
| 373 |
if (toCompany == "") |
| 374 |
toCompany = rec->find("Company"); |
| 375 |
if (toLocation == "") |
| 376 |
toLocation = rec->find("Location"); |
| 377 |
if (toVoiceNumber == "") { |
| 378 |
toVoiceNumber = rec->find("Voice-Number"); |
| 379 |
if (toVoiceNumber != "") { |
| 380 |
fxStr areaCode(rec->find("Area-Code")); |
| 381 |
if (areaCode != "") |
| 382 |
toVoiceNumber.insert("1-" | areaCode | "-"); |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
coverDef("to", to); |
| 387 |
coverDef("to-company", toCompany); |
| 388 |
coverDef("to-location", toLocation); |
| 389 |
coverDef("to-voice-number", toVoiceNumber); |
| 390 |
coverDef("to-fax-number", toFaxNumber); |
| 391 |
} |
| 392 |
|
| 393 |
void |
| 394 |
faxCoverApp::emitFromDefs(FaxDBRecord* rec) |
| 395 |
{ |
| 396 |
if (rec) { |
| 397 |
fromCompany = rec->find("Company"); |
| 398 |
fromLocation = rec->find("Location"); |
| 399 |
fromFaxNumber = rec->find(FaxDB::numberKey); |
| 400 |
fromVoiceNumber = rec->find("Voice-Number"); |
| 401 |
fromMailAddr = rec->find("Mail-Address"); |
| 402 |
fxStr areaCode(rec->find("Area-Code")); |
| 403 |
if (areaCode != "") { |
| 404 |
if (fromFaxNumber != "") |
| 405 |
fromFaxNumber.insert("1-" | areaCode | "-"); |
| 406 |
if (fromVoiceNumber != "") |
| 407 |
fromVoiceNumber.insert("1-" | areaCode | "-"); |
| 408 |
} |
| 409 |
} |
| 410 |
coverDef("from", sender); |
| 411 |
coverDef("from-fax-number", fromFaxNumber); |
| 412 |
coverDef("from-voice-number",fromVoiceNumber); |
| 413 |
coverDef("from-company", fromCompany); |
| 414 |
coverDef("from-location", fromLocation); |
| 415 |
coverDef("from-mail-address", fromMailAddr); |
| 416 |
} |
| 417 |
|
| 418 |
void |
| 419 |
faxCoverApp::emitCommentDefs() |
| 420 |
{ |
| 421 |
/* |
| 422 |
* Fix up new line characters. |
| 423 |
*/ |
| 424 |
u_int crlf = comments.find(0, "\\n", (u_int) 0); |
| 425 |
while ( crlf < comments.length()) { |
| 426 |
comments.remove(crlf, 2); |
| 427 |
comments.insert('\n', crlf); |
| 428 |
crlf = comments.find(0, "\\n", (u_int) 0); |
| 429 |
} |
| 430 |
coverDef("comments", comments); |
| 431 |
} |
| 432 |
|
| 433 |
void |
| 434 |
faxCoverApp::emitDateDefs() |
| 435 |
{ |
| 436 |
time_t t = time(0); |
| 437 |
char date[128]; |
| 438 |
strftime(date, sizeof (date), dateFmt, localtime(&t)); |
| 439 |
coverDef("todays-date", date); |
| 440 |
} |
| 441 |
|
| 442 |
void |
| 443 |
faxCoverApp::coverDef(const char* tag, const char* value) |
| 444 |
{ |
| 445 |
printf("/%s (", tag); |
| 446 |
for (const char* cp = value; *cp; cp++) { |
| 447 |
if (*cp == '(' || *cp == ')' || *cp == '\\') |
| 448 |
putchar('\\'); |
| 449 |
putchar(*cp); |
| 450 |
} |
| 451 |
printf(") def\n"); |
| 452 |
} |
| 453 |
|
| 454 |
fxStr |
| 455 |
faxCoverApp::tildeExpand(const fxStr& filename) |
| 456 |
{ |
| 457 |
fxStr path(filename); |
| 458 |
if (filename.length() > 1 && filename[0] == '~') { |
| 459 |
path.remove(0); |
| 460 |
struct passwd* pwd = getpwuid(getuid()); |
| 461 |
if (!pwd) { |
| 462 |
fxFatal(_("Can not figure out who you are.")); |
| 463 |
} |
| 464 |
char* cp = pwd->pw_dir; |
| 465 |
path.insert(cp); |
| 466 |
} |
| 467 |
return (path); |
| 468 |
} |
| 469 |
|
| 470 |
void |
| 471 |
faxCoverApp::printError(const char* va_alist ...) |
| 472 |
#define fmt va_alist |
| 473 |
{ |
| 474 |
va_list ap; |
| 475 |
va_start(ap, va_alist); |
| 476 |
fprintf(stderr, "%s: ", (const char*) appName); |
| 477 |
vfprintf(stderr, fmt, ap); |
| 478 |
va_end(ap); |
| 479 |
fprintf(stderr, ".\n"); |
| 480 |
} |
| 481 |
#undef fmt |
| 482 |
|
| 483 |
int |
| 484 |
main(int argc, char** argv) |
| 485 |
{ |
| 486 |
NLS::Setup("hylafax-client"); |
| 487 |
faxCoverApp app; |
| 488 |
app.initialize(argc, argv); |
| 489 |
app.open(); |
| 490 |
return 0; |
| 491 |
} |