1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
|
/* -*- C -*-
* MAIN.C
*
* (c)Copyright 1995 by Tobias Ferber, All Rights Reserved
*
* This file is part of ADOC.
*
* ADOC is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 1 of the License,
* or (at your option) any later version.
*
* ADOC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* $VER: $Id: main.c,v 1.1 2001-07-18 11:05:53 root Exp $ */
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <stdio.h>
#include "adoc.h"
#include "flist.h"
#include "libfun.h"
#include "mactab.h"
#include "version.h"
#include "debug.h"
/*@*/
/* local variables */
static char *whoami;
static FILE *ferr;
/* function to export `ferr' to the scanner */
FILE *get_ferr(void) { return ferr; }
/**/
static void echo(const char *fmt, ...)
{
va_list argp;
va_start(argp,fmt);
fprintf(stderr,"%s: ",whoami);
vfprintf(stderr,(char *)fmt,argp);
fprintf(stderr,"\n");
fflush(stderr);
va_end(argp);
}
/**/
int main(int argc, char *argv[])
{
int err= 0; /* return code */
char *outfile = (char *)0; /* --output-file */
char *errfile = (char *)0; /* --error-file */
char *headerfile = (char *)0; /* --texi-header-file */
char *yank_type = "*"; /* --yank-type */
char *body_env = "smallexample"; /* -B<environment> */
int page_width = 80; /* --page-width */
int tabsize = 8; /* --tab-size */
int tabs_to_spaces = 0; /* --tabs-to-spaces */
int output_type = 1; /* --output-type */
int table_of_contents = 0; /* --table-of-contents */
int sort_entries = 1; /* --preserve-order */
int texi_flags = TEXI_CREATE_HEADER | TEXI_PARSE_REFERENCES | TEXI_ITEMIZE_REFERENCES;
int adoc_flags = ADOC_FORM_FEEDS;
int warn_mask = WARN_NORMAL;
int scanner_flags = 0; /* --indented-comments */
/* --unindent-bodytext */
int minimum_indentation = -1; /* --reindent-bodytext */
/* handles for the macro tables */
int texi_macros = 0;
int body_macros = 0;
#ifdef _DCC /* Dice */
expand_args(argc,argv, &argc,&argv);
#endif /* _DCC */
/* filenames on MS-DOG systems look very ugly: all uppercase and
* backslashes. Perform some cosmetics */
#ifdef __MSDOS__
whoami= "adoc";
#else
whoami= argv[0];
#endif /*__MSDOS__*/
/* set the debugging defaults */
D(bug_init(0,stdout));
/* initialize the default error stream */
ferr= stderr;
if(err == 0)
{
/* prepare the texinfo macro table */
texi_macros= mactab_new( 4+10 );
if(!texi_macros)
err= 1;
}
if(err == 0)
{
/* prepare the body-text macro table */
body_macros= mactab_new( 2 );
if(!body_macros)
err= 2;
}
if(err)
echo("error %d creating macro tables -- not enough memory?", err);
else
/* BEGIN scanning command line arguments */
while( (--argc > 0) && (err <= 0) )
{
char *arg= *++argv;
#ifdef DEBUG
if(argc > 1) { D(bug("examining command line argument `%s' ( `%s', ... ) [%d]", argv[0], argv[1], argc-1)); }
else { D(bug("examining command line argument `%s' ( ) [%d]", argv[0], argc-1)); }
#endif /* DEBUG */
if(*arg=='-')
{
/* remember the original command-line option string */
char *opt= arg;
if(arg[1]=='-')
arg= convert_args(*argv);
switch(*++arg)
{
/*-0*/ case '0':
output_type= 0;
break;
/*-1*/ case '1':
output_type= 1;
break;
/*-2*/ case '2':
output_type= 2;
tabs_to_spaces= 1;
minimum_indentation= 0;
break;
/*-b*/ case 'b':
texi_flags |= TEXI_TABLE_FUNCTIONS;
break;
/*-B*/ case 'B':
if(arg[1]) ++arg;
else arg= (--argc > 0) ? *(++argv) : (char *)0;
if(arg && *arg)
{
body_env= arg;
}
else
{
echo("missing texinfo body text environment after %s option",opt);
err= 1;
}
break;
/*-c*/ case 'c':
err= mactab_add(body_macros, "\\*", "/*", "*\\", "*/", (char *)0);
if(err)
echo("error %d adding comment convertion macros",err);
break;
/*-d*/ case 'd':
#ifdef DEBUG
if(arg[1]) { D(bug_level= atoi( &(arg[1]) )); }
else { D(bug_level= 1); }
#else /* !DEBUG */
echo("not compiled w/ -DDEBUG. No debug information available -- Sorry");
/* no error */
#endif /* DEBUG */
break;
/*-D*/ case 'D':
if(arg[1] && --argc > 0)
{
char *lhs= &arg[1];
char *rhs= *(++argv);
err= mactab_add(texi_macros, lhs, rhs, (char *)0);
if(err)
echo("error adding texinfo macro `%s' = `%s'", lhs, rhs);
}
else
{
echo("missing macro %s after `%s' option",(arg[1] ? "value":"name"),opt);
err= 1;
}
break;
/*-E*/ case 'E':
if(arg[1]) ++arg;
else arg= (--argc > 0) ? *(++argv) : (char *)0;
if(arg && *arg)
{
if(errfile)
{
echo("warning: option `%s' has already been seen", opt);
D(bug("%s \"%s\" superseeds -E \"%s\"", opt, arg, errfile));
}
/*errfile= strcmp(arg,"-") ? arg : (char *)0;*/
errfile= arg;
}
else /* !(arg && *arg) */
{
echo("missing filename after `%s' option", opt);
err= 1;
}
break;
/*-f*/ case 'f':
if(arg[1])
{
while(*++arg) switch(*arg)
{
case 'f':
adoc_flags |= ADOC_FORM_FEEDS;
texi_flags |= TEXI_FUNCTION_NEWPAGE;
break;
default:
echo("unknown paging option: `%s'",opt);
break;
}
}
else /* !arg[1] */
{
adoc_flags &= ~ADOC_FORM_FEEDS;
texi_flags &= ~TEXI_FUNCTION_NEWPAGE;
}
break;
/*-g*/ case 'g':
if(arg[1])
{
while(*++arg) switch(*arg)
{
case 's':
texi_flags |= TEXI_GROUP_SECTIONS;
break;
default:
echo("unknown grouping option: `%s'",opt);
err= 1;
break;
}
}
else texi_flags &= ~TEXI_GROUP_SECTIONS;
break;
/*-H*/ case 'H':
if(arg[1]) ++arg;
else arg= (--argc > 0) ? *(++argv) : (char *)0;
if(arg && *arg)
{
if(headerfile)
{
echo("warning: option `%s' has already been seen", opt);
D(bug("%s \"%s\" superseeds -H \"%s\"", opt, arg, headerfile));
}
headerfile= arg;
}
else /* !(arg && *arg) */
{
echo("missing texinfo header filename after `%s' option", opt);
err= 1;
}
break;
/*-h*/ case 'h':
printf("usage: %s [options] [-o outfile] [@ listfile] [infiles...]\n\n", whoami);
display_args( arg[1] ? atoi(&arg[1]) : 3 );
err= -1; /* negative means exit w/o error */
break;
/*-I*/ case 'I':
table_of_contents= 1;
break;
/*-i*/ case 'i':
yank_type= "i";
break;
/*-j*/ case 'j':
if(arg[1]) ++arg;
else arg= (--argc > 0) ? *(++argv) : (char *)0;
if(arg && *arg)
{
if( (minimum_indentation= atoi(arg)) < 0 )
{
echo("illegal indentation: %d (must be >= 0)", minimum_indentation);
err= 1;
}
}
else /* !(arg && *arg) */
{
echo("missing indentation after `%s' option", opt);
err= 1;
}
break;
/*-l*/ case 'l':
if(arg[1]) ++arg;
else arg= (--argc > 0) ? *(++argv) : (char *)0;
if(arg && *arg)
{
page_width= atoi(arg);
if(page_width < 1)
{
echo("illegal page width: `%s' (must be > 0)", arg);
err= 1;
}
}
else /* !(arg && *arg) */
{
echo("missing page width after `%s' option", opt);
err= 1;
}
break;
/*-M*/ case 'M':
if(arg[1] && --argc > 0)
{
char *lhs= &arg[1];
char *rhs= *(++argv);
err= mactab_add(body_macros, lhs, rhs, (char *)0);
if(err)
echo("error adding body macro `%s' -> `%s'", lhs, rhs);
}
else
{
echo("missing macro %s after `%s' option",(arg[1] ? "value":"name"),opt);
err= 1;
}
break;
/*-n*/ case 'n':
output_type= 0;
break;
/*-o*/ case 'o':
if(arg[1]) ++arg;
else arg= (--argc > 0) ? *(++argv) : (char *)0;
if(arg && *arg)
{
if(outfile)
echo("warning: option `%s' has already been seen", opt);
outfile= arg;
}
else /* !(arg && *arg) */
{
echo("missing filename after `%s' option", opt);
err= 1;
}
break;
/*-p*/ case 'p':
sort_entries= arg[1] ? 1:0;
break;
/*-q*/ case 'q':
break;
/*-T*/ case 'T':
if(arg[1]) ++arg;
else arg= (--argc > 0) ? *(++argv) : (char *)0;
if(arg && *arg)
{
tabs_to_spaces= 1;
tabsize= atoi(arg);
if(tabsize < 1)
{
echo("illegal tab step: `%d' (must be >= 1)", tabsize);
err= 1;
}
}
else /* !(arg && *arg) */
{
echo("missing tab size after `%s' option", opt);
err= 1;
}
break;
/*-t*/ case 't':
tabs_to_spaces= arg[1] ? atoi(&arg[1]) : 1;
break;
/*-U*/ case 'U':
if(arg[1]) ++arg;
else arg= (--argc > 0) ? *(++argv) : (char *)0;
if(arg && *arg)
{
mactab_remove(texi_macros, arg, (char *)0);
mactab_remove(body_macros, arg, (char *)0);
}
else /* !(arg && *arg) */
{
echo("missing macro after `%s' option", opt);
err= 1;
}
break;
/*-u*/ case 'u':
if(arg[1])
{
scanner_flags &= ~SCANNER_UNINDENT_BODYTEXT;
minimum_indentation= -1;
}
else scanner_flags |= SCANNER_UNINDENT_BODYTEXT;
break;
/*-v*/ case 'v':
printf("ADOC Version " VERSION " (compiled " __DATE__ ", " __TIME__ ")\n"
"(c)Copyright 1995 by Tobias Ferber, All Rights Reserved\n" );
err= -1;
break;
/*-W*/ case 'W':
if(arg[1])
{
++arg;
if( isdigit(*arg) )
warn_mask |= atoi(arg);
else switch( strarg(arg, "none", /* 1 */
"arnings", /* 2 */
"keywords", /* 3 */
"absence", /* 4 */
"untitled", /* 5 */
"all", "") ) /* 6 */
{
case 1: warn_mask = WARN_NONE; break;
case 2: warn_mask |= WARN_NORMAL; break;
case 3: warn_mask |= WARN_UNKNOWN_KEYWORDS; break;
case 4: warn_mask |= WARN_MISSING_KEYWORDS; break;
case 5: warn_mask |= WARN_UNTITLED_SECTION; break;
case 6: warn_mask |= WARN_ALL; break;
default:
echo("unknown warning method: `%s'",opt);
err= 1;
break;
}
}
else warn_mask= WARN_NONE;
break;
/*-x*/ case 'x':
if(arg[1])
{
switch( strarg(++arg, "off", /* 1 */
"on", /* 2 */
"itemize", /* 3 */
"", "") ) /* 4 */
{
case 1: texi_flags &= ~TEXI_PARSE_REFERENCES;
texi_flags &= ~TEXI_ITEMIZE_REFERENCES; break;
case 2: texi_flags |= TEXI_PARSE_REFERENCES;
texi_flags &= ~TEXI_ITEMIZE_REFERENCES; break;
case 3: texi_flags |= TEXI_PARSE_REFERENCES;
texi_flags |= TEXI_ITEMIZE_REFERENCES; break;
default:
echo("unknown reference handlig option: `%s'",opt);
err= 1;
break;
}
}
else texi_flags &= ~(TEXI_PARSE_REFERENCES | TEXI_ITEMIZE_REFERENCES);
break;
/*-Y*/ case 'Y':
if(arg[1]) scanner_flags &= ~SCANNER_ALLOW_INDENTED_COMMENTS;
else scanner_flags |= SCANNER_ALLOW_INDENTED_COMMENTS;
break;
/*-y*/ case 'y':
if(arg[1]) ++arg;
else arg= (--argc > 0) ? *(++argv) : (char *)0L;
if(arg && *arg)
yank_type= arg;
else /* !(arg && *arg) */
{
echo("missing comment type string after `%s' option", opt);
err= 1;
}
break;
/*-z*/ case 'z':
texi_flags &= ~TEXI_CREATE_HEADER;
break;
/*-Z*/ case 'Z':
if(arg[1]) texi_flags &= ~TEXI_NO_INDEX;
else texi_flags |= TEXI_NO_INDEX;
break;
/*
* The following options are ignored for compatibility
* with Bill Koester's original version `autodoc' which
* is part of C=ommodore's Native Developer Kit (NDK).
*/
/*-C*/ case 'C':
/*-F*/ case 'F':
/*-s*/ case 's':
/*-a*/ case 'a':
/*-r*/ case 'r':
/*-w*/ case 'w':
echo("warning: option `%s' ignored for compatibility", opt);
break;
/*- */ case '\0':
if( (err= flist_addfile("")) )
echo("out of memory... hmmmmmmmmmpf!");
break;
/*??*/ default:
echo("unrecognized option `%s'", opt);
err= 1;
break;
}
}
else if(*arg=='@')
{
if(arg[1]) ++arg;
else arg= (--argc > 0) ? *(++argv) : (char *)0L;
if(arg && *arg)
{
if( (err= flist_from_file(arg)) )
echo("out of memory... aaarrrrrrgggggghh!");
}
else /* !(arg && *arg) */
{
echo("missing filename after `%s'", *argv);
err= 1;
}
}
else /* *arg != '@' */
{
if(arg && *arg)
{
if( (err= flist_addfile(arg)) )
echo("out of memory... aaaiiiiiieeeeeeeee!");
}
else echo("internal problem parsing command line arguments: arg is empty");
}
}
/* END scanning command line arguments */
D(bug("command line argument parsing done"));
if(err == 0)
{
/* prepare the error stream */
if(errfile && *errfile)
{
D(bug("opening error stream `%s'",errfile));
if( !(ferr= fopen(errfile,"w")) )
{
echo("could not write error messages to `%s'",errfile);
err= __LINE__;
}
}
/*else ferr is initialized to stderr */
/* if no filename is given then read from stdin */
if( !flist_getname() )
flist_addfile("");
/* read the input files (the scanner takes them from the flist queue) */
D(bug("reading autodocs of type `%s'", yank_type));
if(err == 0)
err= read_source(yank_type, warn_mask, scanner_flags);
if(err < 0)
err= -err; /* I/O error */
D(bug("disposing file list"));
flist_dispose();
/*
*/
if( (err == 0) && (minimum_indentation >= 0) )
{
if( (err= funindent(minimum_indentation, tabsize)) )
echo("error %d reworking body text indentation -- not enough memory?",err);
/* funindent() already performed that conversion */
else tabs_to_spaces= 0;
}
if( (err == 0) && (output_type > 0) )
{
FILE *fout;
/* prepare the output file */
if(outfile && *outfile)
{
D(bug("opening output stream `%s'",outfile));
if(!(fout= fopen(outfile,"w")) )
{
echo("could not write to `%s'",outfile);
err= __LINE__;
}
}
else fout= stdout;
if( fout && (err==0) )
{
if(sort_entries)
{
D(bug("sorting entries"));
funsort();
}
switch(output_type)
{
case 1: /* --autodoc */
if(table_of_contents)
{
D(bug("writing table of contents"));
err= gen_autodoc_toc(fout);
}
if(err == 0)
{
D(bug("writing autodocs"));
err= gen_autodoc( fout, page_width, tabs_to_spaces ? tabsize : 0, adoc_flags, mactab(body_macros) );
}
break;
case 2: /* --texinfo */
if(texi_flags & TEXI_CREATE_HEADER)
{
D(bug("creating texinfo header"));
err= gen_texinfo_header( fout, headerfile, mactab(texi_macros) );
}
if(err == 0)
{
D(bug("adding texinfo body macros"));
err= mactab_add( body_macros, "@", "@@",
"{", "@{",
"}", "@}",
/* "...", "@dots{}", */
/* "TeX", "@TeX{}", */
"e.g. ", "e.g.@: ",
"E.g. ", "E.g.@: ",
"i.e. ", "i.e.@: ",
"I.e. ", "I.e.@: ", (char *)0 );
}
if(err == 0)
{
D(bug("creating texinfo output"));
err+= gen_texinfo( fout, tabs_to_spaces ? tabsize : 0, texi_flags, body_env, mactab(body_macros) );
}
if(err)
echo("error creating texinfo output");
break;
default: /* --dry-run */
break;
}
}
if(fout && (fout != stdout))
fclose(fout);
}
D(bug("disposing libfun entries"));
funfree();
}
#ifdef DEBUG
mactab_debug(bug_stream);
#endif
D(bug("disposing macro tables"));
mactab_dispose(body_macros);
mactab_dispose(texi_macros);
/*
*/
if(err > 0)
{
echo("[%s] *** Error %d", (outfile && *outfile) ? outfile : "stdout", err);
fprintf(ferr,"%s terminated abnormally (error %d)\n", whoami, err);
}
D(bug("closing I/O streams"));
if( ferr && (ferr != stderr) && (ferr != stdout) )
fclose(ferr);
D(bug("exiting adoc returning %d (%s)", (err>0) ? 1:0, (err>0) ? "error":"success" ));
D(bug_exit());
#ifdef DEBUG
if(bug_stream && (bug_stream != stdout))
fclose(bug_stream);
#endif /*DEBUG*/
return (err > 0) ? 1:0;
}
|