| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
#include <ctype.h> |
| 38 |
#include "Man.h" |
| 39 |
|
| 40 |
///////////////////////////////////////////////////////////////////////////// |
| 41 |
// |
| 42 |
// Class: Writer |
| 43 |
// |
| 44 |
///////////////////////////////////////////////////////////////////////////// |
| 45 |
|
| 46 |
FILE *Writer::fp = NULL; |
| 47 |
char *Writer::fullName = NULL; |
| 48 |
char *Writer::sourceFileName = NULL; |
| 49 |
SbBool Writer::inSynopsis = FALSE; |
| 50 |
Language Writer::language = C_PLUS_PLUS; |
| 51 |
SbBool Writer::forBook = FALSE; |
| 52 |
float Writer::baseIndent = 0.0; |
| 53 |
int Writer::numTabChars = -1; |
| 54 |
SbBool Writer::indentedForTab = FALSE; |
| 55 |
|
| 56 |
SbBool |
| 57 |
Writer::open(const char *dirName, const char *fileName) |
| 58 |
{ |
| 59 |
SbString name = dirName; |
| 60 |
|
| 61 |
name += "/"; |
| 62 |
name += fileName; |
| 63 |
|
| 64 |
fullName = strdup(name.getString()); |
| 65 |
|
| 66 |
fp = fopen(fullName, "w"); |
| 67 |
|
| 68 |
if (fp == NULL) { |
| 69 |
fprintf(stderr, "Couldn't open file \"%s\" for writing\n", fullName); |
| 70 |
return FALSE; |
| 71 |
} |
| 72 |
|
| 73 |
return TRUE; |
| 74 |
} |
| 75 |
|
| 76 |
void |
| 77 |
Writer::writePreface(const SbName &className) |
| 78 |
{ |
| 79 |
// These must be the first 2 lines in man pages. Tbl is necessary |
| 80 |
// for the nodekit part tables. |
| 81 |
write("'\\\"! tbl | mmdoc\n" |
| 82 |
"'\\\"macro stdmacro\n"); |
| 83 |
|
| 84 |
// |
| 85 |
// Write strings/macros that are used within man page |
| 86 |
// |
| 87 |
|
| 88 |
// Define code fonts. "Cr" is Roman code font, "Cb" is bold code |
| 89 |
// font. Use different stuff for nroff/troff. |
| 90 |
write(".ie n \\{\\\n" |
| 91 |
". ds Cr \\fB\n" |
| 92 |
". ds Cb \\fB\n" |
| 93 |
".\\}\n" |
| 94 |
".el \\{\\\n" |
| 95 |
". ds Cr \\f7\n" |
| 96 |
". ds Cb \\f8\n" |
| 97 |
".\\}\n"); |
| 98 |
|
| 99 |
// Write man page header |
| 100 |
write(".TH "); |
| 101 |
write(className); |
| 102 |
write("(3IV)"); |
| 103 |
write("\n"); |
| 104 |
} |
| 105 |
|
| 106 |
void |
| 107 |
Writer::startSection(const char *name) |
| 108 |
{ |
| 109 |
write(".SH "); |
| 110 |
write(name); |
| 111 |
write("\n"); |
| 112 |
|
| 113 |
baseIndent = 0.0; |
| 114 |
} |
| 115 |
|
| 116 |
void |
| 117 |
Writer::setFont(Font newFont) |
| 118 |
{ |
| 119 |
switch (newFont) { |
| 120 |
|
| 121 |
case TEXT_ROMAN: |
| 122 |
write("\\f1"); |
| 123 |
break; |
| 124 |
case TEXT_ITALIC: |
| 125 |
write("\\f2"); |
| 126 |
break; |
| 127 |
case TEXT_BOLD: |
| 128 |
write("\\f3"); |
| 129 |
break; |
| 130 |
|
| 131 |
case CODE_ROMAN: |
| 132 |
if (isForBook()) |
| 133 |
write("\\f1"); |
| 134 |
else |
| 135 |
write("\\*(Cr"); |
| 136 |
break; |
| 137 |
case CODE_BOLD: |
| 138 |
if (isForBook()) |
| 139 |
write("\\f3"); |
| 140 |
else |
| 141 |
write("\\*(Cb"); |
| 142 |
break; |
| 143 |
|
| 144 |
case FIXED_ROMAN: |
| 145 |
write("\\*(Cr"); |
| 146 |
break; |
| 147 |
case FIXED_BOLD: |
| 148 |
write("\\*(Cb"); |
| 149 |
break; |
| 150 |
|
| 151 |
case PREVIOUS: |
| 152 |
write("\\fP"); |
| 153 |
break; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
void |
| 158 |
Writer::changeSize(float diff) |
| 159 |
{ |
| 160 |
char buf[32]; |
| 161 |
|
| 162 |
sprintf(buf, "%g", diff); |
| 163 |
if (diff > 0) |
| 164 |
write(".ps +"); |
| 165 |
else |
| 166 |
write(".ps "); |
| 167 |
write(buf); |
| 168 |
write("\n"); |
| 169 |
} |
| 170 |
|
| 171 |
void |
| 172 |
Writer::setBaseIndent(float indInInches) |
| 173 |
{ |
| 174 |
char buf[32]; |
| 175 |
|
| 176 |
baseIndent = indInInches; |
| 177 |
|
| 178 |
// Indentation value = .5i + given value |
| 179 |
sprintf(buf, ".in %gi\n", baseIndent + 0.5); |
| 180 |
write(buf); |
| 181 |
} |
| 182 |
|
| 183 |
void |
| 184 |
Writer::setIndent(int numChars, int firstLineChars) |
| 185 |
{ |
| 186 |
char buf[32]; |
| 187 |
|
| 188 |
// Indentation value = .5i + given value |
| 189 |
sprintf(buf, ".in %dn+.5i\n", numChars); |
| 190 |
write(buf); |
| 191 |
sprintf(buf, ".ti %dn+.5i\n", firstLineChars); |
| 192 |
write(buf); |
| 193 |
} |
| 194 |
|
| 195 |
void |
| 196 |
Writer::setTab(int _numTabChars) |
| 197 |
{ |
| 198 |
numTabChars = _numTabChars; |
| 199 |
|
| 200 |
if (numTabChars >= 0) { |
| 201 |
char buf[32]; |
| 202 |
|
| 203 |
// Set tab to number of characters (tabs are relative to current |
| 204 |
// indentation, so we don't have to add it in) |
| 205 |
if (isForBook()) |
| 206 |
sprintf(buf, "%gm", (float) numTabChars * .6); |
| 207 |
else |
| 208 |
sprintf(buf, "%dm", numTabChars); |
| 209 |
write(".ta "); |
| 210 |
write(buf); |
| 211 |
write("\n"); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
void |
| 216 |
Writer::indentForTab() |
| 217 |
{ |
| 218 |
char buf[32]; |
| 219 |
|
| 220 |
// Set indentation to base indent + number of characters + .5 |
| 221 |
// inches (for indent offset) + .5 inches (for extra indentation) |
| 222 |
sprintf(buf, "%gi+%dn", baseIndent + 1.0, numTabChars); |
| 223 |
write(".in "); |
| 224 |
write(buf); |
| 225 |
write("\n"); |
| 226 |
|
| 227 |
// Set indentation on first line to be base indent |
| 228 |
sprintf(buf, "%gi", baseIndent + 0.5); |
| 229 |
write(".ti "); |
| 230 |
write(buf); |
| 231 |
write("\n"); |
| 232 |
|
| 233 |
// Reset tab stop, so it gets current indentation |
| 234 |
setTab(numTabChars); |
| 235 |
|
| 236 |
// Save the pre-tab stuff in a string so we can test its |
| 237 |
// length when we output the tab |
| 238 |
write(".ds Pt "); |
| 239 |
|
| 240 |
indentedForTab = TRUE; |
| 241 |
} |
| 242 |
|
| 243 |
void |
| 244 |
Writer::tab() |
| 245 |
{ |
| 246 |
if (indentedForTab) { |
| 247 |
char buf[132]; |
| 248 |
|
| 249 |
// |
| 250 |
// NOTE: Do not try to clean any of this up. Troff is very |
| 251 |
// unpredictable, and this is the only permutation I could |
| 252 |
// find that didn't insert extra spaces or misalign tabs |
| 253 |
// anywhere. |
| 254 |
// |
| 255 |
|
| 256 |
// End the string containing the pre-tab stuff |
| 257 |
write("\n"); |
| 258 |
|
| 259 |
// If the pre-tab stuff is wider than the tab space, set the |
| 260 |
// indentation for the next line to the base indentation. |
| 261 |
// Added .ne's to prevent bad page breaks, added \\ after |
| 262 |
// .ie and .el to eliminate extra linebreaks -- AE |
| 263 |
sprintf(buf, |
| 264 |
".ie \\w'\\*(Pt'>=%dn \\{\\\n" |
| 265 |
".ne 3\n" |
| 266 |
"\\*(Pt\n" |
| 267 |
".ti %gi\n" |
| 268 |
"\t\\c\\\n" |
| 269 |
"\\}\n" |
| 270 |
".el\\{\\\n" |
| 271 |
".ne 2\n" |
| 272 |
"\\*(Pt\t\\c\\\n" |
| 273 |
"\\}\n", |
| 274 |
numTabChars, baseIndent + .5); |
| 275 |
write(buf); |
| 276 |
|
| 277 |
indentedForTab = FALSE; |
| 278 |
} |
| 279 |
|
| 280 |
else |
| 281 |
write("\t"); |
| 282 |
} |
| 283 |
|
| 284 |
void |
| 285 |
Writer::setFill(SbBool flag) |
| 286 |
{ |
| 287 |
if (flag) |
| 288 |
write(".fi\n"); |
| 289 |
else |
| 290 |
write(".nf\n"); |
| 291 |
} |
| 292 |
|
| 293 |
void |
| 294 |
Writer::breakLine() |
| 295 |
{ |
| 296 |
write(".br\n"); |
| 297 |
} |
| 298 |
|
| 299 |
void |
| 300 |
Writer::space(float amt) |
| 301 |
{ |
| 302 |
if (amt == 1.0) |
| 303 |
write(".sp\n"); |
| 304 |
else { |
| 305 |
char buf[32]; |
| 306 |
sprintf(buf, ".sp %g\n", amt); |
| 307 |
write(buf); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
void |
| 312 |
Writer::write(const char *string) |
| 313 |
{ |
| 314 |
fprintf(fp, "%s", string); |
| 315 |
} |
| 316 |
|
| 317 |
void |
| 318 |
Writer::write(const SbName &name) |
| 319 |
{ |
| 320 |
fprintf(fp, "%s", name.getString()); |
| 321 |
} |
| 322 |
|
| 323 |
void |
| 324 |
Writer::write(const SbString &string) |
| 325 |
{ |
| 326 |
fprintf(fp, "%s", string.getString()); |
| 327 |
} |
| 328 |
|
| 329 |
void |
| 330 |
Writer::writeBracketed(const SbString &string) |
| 331 |
{ |
| 332 |
const char *s = string.getString(); |
| 333 |
|
| 334 |
// Remove leading spaces |
| 335 |
while (isspace(*s)) |
| 336 |
s++; |
| 337 |
|
| 338 |
writeStuff(s, TRUE, '\0'); |
| 339 |
|
| 340 |
// Write out a newline |
| 341 |
putc('\n', fp); |
| 342 |
} |
| 343 |
|
| 344 |
void |
| 345 |
Writer::writeDescription(const SbString &string) |
| 346 |
{ |
| 347 |
breakLine(); |
| 348 |
setBaseIndent(0.5); |
| 349 |
setFont(Writer::TEXT_ROMAN); |
| 350 |
writeBracketed(string); |
| 351 |
space(); |
| 352 |
setBaseIndent(0.0); |
| 353 |
} |
| 354 |
|
| 355 |
const char * |
| 356 |
Writer::writeStuff(const char *string, SbBool compressSpace, int blockChar) |
| 357 |
{ |
| 358 |
const char *s = string; |
| 359 |
SbBool inSpace = FALSE; |
| 360 |
int curChar = 0; |
| 361 |
|
| 362 |
// Have to check for special character sequences |
| 363 |
while (*s != '\0') { |
| 364 |
|
| 365 |
// Check for special characters |
| 366 |
while (*s == '\\') { |
| 367 |
|
| 368 |
// Check for end of block |
| 369 |
if (blockChar != '\0' && *(s+1) == '.') |
| 370 |
return endBlock(s, blockChar); |
| 371 |
|
| 372 |
// Check for beginning of new block |
| 373 |
else |
| 374 |
s = startBlock(s, compressSpace); |
| 375 |
|
| 376 |
inSpace = FALSE; |
| 377 |
} |
| 378 |
|
| 379 |
// Treat spaces specially |
| 380 |
if (compressSpace) { |
| 381 |
if (isspace(*s)) { |
| 382 |
if (! inSpace) { |
| 383 |
// Insert a newline every once in a while just to |
| 384 |
// keep troff happy - it has some problems with |
| 385 |
// really long lines |
| 386 |
if (curChar > 200 && *(s+1) != '\0' && *(s+1) != '\\') { |
| 387 |
putc('\n', fp); |
| 388 |
// XXX This is ugly, but keeps us from getting bit |
| 389 |
// by an ugly bug, whereby we wrap at a significant |
| 390 |
// character like ' or . thereby causing troff to |
| 391 |
// eat text. Should just test for all significant |
| 392 |
// characters above, but I can't remember them all offhand. |
| 393 |
// The non-printing character (\&) wards off these evil |
| 394 |
// control characters. |
| 395 |
// AE |
| 396 |
write("\\&"); |
| 397 |
curChar = 0; |
| 398 |
} |
| 399 |
else |
| 400 |
putc(' ', fp); |
| 401 |
inSpace = TRUE; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
else { |
| 406 |
inSpace = FALSE; |
| 407 |
if (*s != '\0') |
| 408 |
putc(*s, fp); |
| 409 |
} |
| 410 |
curChar++; |
| 411 |
} |
| 412 |
else |
| 413 |
if (*s != '\0') |
| 414 |
putc(*s, fp); |
| 415 |
|
| 416 |
if (*s != '\0') |
| 417 |
s++; |
| 418 |
} |
| 419 |
|
| 420 |
return s; |
| 421 |
} |
| 422 |
|
| 423 |
const char * |
| 424 |
Writer::startBlock(const char *string, SbBool compressSpace) |
| 425 |
{ |
| 426 |
// We know that *s is a backslash, and that *(s+1) is not a . |
| 427 |
const char *s = string + 1; |
| 428 |
|
| 429 |
switch (*s) { |
| 430 |
|
| 431 |
case 'a': // Argument |
| 432 |
setFont(isForBook() ? TEXT_ITALIC : CODE_ROMAN); |
| 433 |
s = writeStuff(s + 1, compressSpace, 'a'); |
| 434 |
break; |
| 435 |
|
| 436 |
case 'b': // Indented, unfilled block |
| 437 |
write("\n"); |
| 438 |
setFill(FALSE); |
| 439 |
setBaseIndent(baseIndent + .5); |
| 440 |
s = writeStuff(s + 1, FALSE, 'b'); |
| 441 |
break; |
| 442 |
|
| 443 |
case 'c': // Class name |
| 444 |
setFont(CODE_BOLD); |
| 445 |
s = writeStuff(s + 1, compressSpace, 'c'); |
| 446 |
break; |
| 447 |
|
| 448 |
case 'e': // Emphasis |
| 449 |
setFont(TEXT_ITALIC); |
| 450 |
s = writeStuff(s + 1, compressSpace, 'e'); |
| 451 |
break; |
| 452 |
|
| 453 |
case 'k': // Other kind of kode |
| 454 |
setFont(CODE_ROMAN); |
| 455 |
s = writeStuff(s + 1, compressSpace, 'k'); |
| 456 |
break; |
| 457 |
|
| 458 |
case 'm': // Method name |
| 459 |
setFont(CODE_BOLD); |
| 460 |
s = writeStuff(s + 1, compressSpace, 'm'); |
| 461 |
break; |
| 462 |
|
| 463 |
case 'p': // Begin new paragraph (Not ended by \.) |
| 464 |
write("\n"); |
| 465 |
space(); |
| 466 |
s++; |
| 467 |
// Skip over white space at the start of the next paragraph |
| 468 |
while (isspace(*s)) |
| 469 |
s++; |
| 470 |
break; |
| 471 |
|
| 472 |
case 'v': // Variable name |
| 473 |
setFont(CODE_BOLD); |
| 474 |
s = writeStuff(s + 1, compressSpace, 'v'); |
| 475 |
break; |
| 476 |
|
| 477 |
case 'x': // External man page or method reference |
| 478 |
setFont(TEXT_BOLD); |
| 479 |
s = writeStuff(s + 1, compressSpace, 'x'); |
| 480 |
break; |
| 481 |
|
| 482 |
case '+': // C++ only |
| 483 |
if (language != C_PLUS_PLUS) |
| 484 |
s = skipBlock(s + 1); |
| 485 |
else |
| 486 |
s = writeStuff(s + 1, compressSpace, '+'); |
| 487 |
break; |
| 488 |
|
| 489 |
case '-': // C only |
| 490 |
if (language != C) |
| 491 |
s = skipBlock(s + 1); |
| 492 |
else |
| 493 |
s = writeStuff(s + 1, compressSpace, '-'); |
| 494 |
break; |
| 495 |
|
| 496 |
default: |
| 497 |
// Write out the special characters |
| 498 |
putc(*string, fp); |
| 499 |
putc(*(string + 1), fp); |
| 500 |
s++; |
| 501 |
break; |
| 502 |
} |
| 503 |
|
| 504 |
return s; |
| 505 |
} |
| 506 |
|
| 507 |
const char * |
| 508 |
Writer::endBlock(const char *string, int blockChar) |
| 509 |
{ |
| 510 |
// We know that *s is a backslash, and that *(s+1) is a . |
| 511 |
const char *s = string + 2; |
| 512 |
|
| 513 |
switch (blockChar) { |
| 514 |
case 'b': // Indented, unfilled block |
| 515 |
write("\n"); |
| 516 |
setFill(TRUE); |
| 517 |
setBaseIndent(baseIndent - .5); |
| 518 |
// Skip over extra spaces |
| 519 |
while (isspace(*s)) |
| 520 |
s++; |
| 521 |
break; |
| 522 |
|
| 523 |
case 'a': // Any other font |
| 524 |
case 'c': |
| 525 |
case 'e': |
| 526 |
case 'k': |
| 527 |
case 'm': |
| 528 |
case 'v': |
| 529 |
case 'x': |
| 530 |
setFont(TEXT_ROMAN); |
| 531 |
break; |
| 532 |
|
| 533 |
case '+': // C++ only |
| 534 |
case '-': // C++ only |
| 535 |
// Nothing to do |
| 536 |
break; |
| 537 |
|
| 538 |
default: |
| 539 |
fprintf(stderr, "Internal error - ending block of type '%c'\n", |
| 540 |
blockChar); |
| 541 |
break; |
| 542 |
} |
| 543 |
|
| 544 |
return s; |
| 545 |
} |
| 546 |
|
| 547 |
const char * |
| 548 |
Writer::skipBlock(const char *string) |
| 549 |
{ |
| 550 |
// Skip to end of block, including blocks nested in this one |
| 551 |
const char *s = string; |
| 552 |
|
| 553 |
while (TRUE) { |
| 554 |
while (*s != '\0' && *s != '\\') |
| 555 |
s++; |
| 556 |
|
| 557 |
if (*s == '\\') { |
| 558 |
s++; |
| 559 |
if (*s == '.') |
| 560 |
return s + 1; |
| 561 |
// Skip character after backslash since there's no ending delimiter |
| 562 |
else if (*s == 'p') |
| 563 |
s++; |
| 564 |
else |
| 565 |
// if (*s == 'b' || *s == 'c' || *s == '+' || *s == '-') |
| 566 |
s = skipBlock(s + 1); |
| 567 |
// else |
| 568 |
// s++; // Skip character after backslash |
| 569 |
} |
| 570 |
else { |
| 571 |
fprintf(stderr, "\nERROR - %s reach end of file ! (block not terminated)\n", |
| 572 |
sourceFileName); |
| 573 |
fprintf(stderr, "problem at: %s\n", string); |
| 574 |
return s; |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
void |
| 580 |
Writer::finish() |
| 581 |
{ |
| 582 |
fflush(fp); |
| 583 |
} |