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 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
|
/* -*- C -*-
* GENCODE.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: gencode.c,v 1.3 2002-03-12 12:44:44 joga Exp $ */
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include "libfun.h"
#include "adoc.h"
/* texinfo sepeator for library and function name (used in @node and @xref) */
/* makeinfo has a problem when using "." or ":" or "/" here... */
#define CHAPSEC " "
/* convert tabs in `s' to spaces with a tab step of `ts' */
static void fexpand(FILE *fp, int ts, char *s)
{
int column= 0;
while(*s)
{
switch(*s)
{
case '\n':
fputc(*s,fp);
column= 0;
break;
case '\t':
/*do { fputc(' ',fp); } while( (++column) % ts);*/
do { fputc(' ',fp); ++column; } while(column % ts);
break;
default:
fputc(*s,fp);
++column;
break;
}
++s;
}
}
/*
** AUTODOCS
*/
int gen_autodoc_toc(FILE *fp)
{
char *fun;
fprintf(fp,"TABLE OF CONTENTS\n\n");
for(fun= stepfun(0); fun; fun= stepfun(1))
fprintf(fp,"%s\n",fun);
return 0;
}
int gen_autodoc(FILE *fp, int cols, int tabsize, int flags, char **body_macros)
{
int err= 0;
char *fun, *sec, *text;
for(fun= stepfun(0); fun && (err==0); fun= stepfun(1))
{
if(flags & ADOC_FORM_FEEDS)
fputc('\f',fp);
fprintf(fp,"%s%*s\n\n",fun,(int)(cols-strlen(fun)),fun);
for(sec= stepsec(0); sec && (err==0); sec= stepsec(1))
{
/* indent the section heading with 3 spaces */
if(*sec)
fprintf(fp," %s\n",sec);
if( (text= getsec(sec)) )
{
if(text && *text)
{
char *x= strexpand(text, body_macros);
if(x)
{
if(tabsize > 0)
fexpand(fp,tabsize,x);
else
fputs(x,fp);
free(x);
}
else err= __LINE__;
}
fputc('\n',fp);
}
}
}
return err;
}
/*
** TEXINFO
*/
/* split `fun' into `chapter' and `section' and `subsection'
the string returned by this function has to be disposed via free() by the caller */
static char *chapsec(char *fun, char **chapter, char **section, char **subsection)
{
char *c= strdup(fun);
char *s= (char *)0;
char *ss= (char *)0;
if(c)
{
if( (s= strchr(c,'/')) ) {
*s++= '\0';
if( (ss = strchr(s, '/')) ) {
*ss++= '\0';
}
}
}
*chapter= c;
*section= s;
*subsection = ss;
return c;
}
/*
* The implementation of references in Texinfo are perfectly silly :'(
* Instead of offering one simple reference macro, Texinfo comes up
* with context-sensitive @ref{}, @xref{}, @pref{}, ... and prints
* a `See ...' or `See also ...' right in front of it...
*/
static int see_also(FILE *fp, char *refs, int flags)
{
int err= 0;
char *buf= strdup(refs);
#define isref(c) ( ('A'<=(c) && (c)<='Z') || \
('a'<=(c) && (c)<='z') || \
('0'<=(c) && (c)<='9') || ((c)=='_') || ((c)=='-') || ((c)=='/') || ((c)=='.') )
if(buf)
{
/* make `s' the working pointer in `buf' */
char *s= buf;
/*
* former revisions of ADOC used to indent the parsed references
* by the same amount of white space as the first. Actually we
* do not indent them amymore, so out it goes....
*/
/* indentation string of the first reference */
#if 0
char *indent= (char *)0;
#endif
int num_refs= 0;
while( *s && (err==0) )
{
char *l, *r;
/* move `l' and `r' to the left and right end of a reference in `s' */
for(l=s; *l && !isref(*l); l++) ;
for(r=l; *r && isref(*r); r++) ;
/* terminate the reference string with a '\0' */
if(*r) *r++= '\0';
/* save the indentation of the first reference */
#if 0
if(num_refs == 0)
{
if( (indent= strdup(s)) )
{
char *t= indent;
while(*t==' ' || *t=='\t')
++t;
*t= '\0';
}
else err= __LINE__;
}
#endif
/* move `s' behind the reference */
s= r;
if( *l && (err==0) )
{
/* look for a function `l' and initialize `fun' to it's name */
char *fun= (char *)0;
/*fprintf(stderr,"--> @ref{%s} ?\n",l);*/
if( getfun(l) )
fun= strdup(l);
else if( !strchr(l,'/') )
{
if( islib(l) )
fun= strdup(l);
else
{
/*
* Okay, we tried it the easy way but perhaps this is a reference
* into the library without the library name in front of it.
* Let's try appending `l' to the the current library name...
*/
char *f= getfun( (char *)0 );
if(f)
{
char *x, *xl, *xr, *xss;
if( (x= chapsec(f, &xl, &xr, &xss)) )
{
size_t len= strlen(xl) + 1 + strlen(l) + 1;
char *y= (char *)malloc( len * sizeof(char) );
if(y)
{
sprintf(y,"%s/%s",xl,l);
/*fprintf(stderr,"--> @ref{%s} ?\n",y);*/
if( getfun(y) )
fun= strdup(y);
free(y);
}
else err= __LINE__;
free(x);
}
else err= __LINE__;
}
else /* no current function? */
err= __LINE__;
}
}
if(err == 0)
{
/* print the reference */
if( fun )
{
char *cs, *chapter, *section, *subsection;
if( (cs= chapsec(fun, &chapter, §ion, &subsection)) )
{
if(flags & TEXI_ITEMIZE_REFERENCES)
{
if(num_refs==0)
fprintf(fp,"@itemize\n");
if(subsection) {
fprintf(fp, "@item\n@ref{%s%s%s%s%s}.\n", chapter,CHAPSEC,section,CHAPSEC,subsection);
} else {
if(section) {
fprintf(fp,"@item\n@ref{%s%s%s}.\n",chapter,CHAPSEC,section);
} else {
fprintf(fp,"@item\n@ref{%s}.\n",chapter);
}
}
}
else /* not itemized */
{
if( (num_refs==0) && (flags & TEXI_GROUP_SECTIONS) )
fprintf(fp,"@group\n");
fprintf(fp,"%s",(num_refs==0) ? "@*@xref" : "@ref");
if(subsection) {
fprintf(fp,"{%s%s%s%s%s},\n",chapter,CHAPSEC,section,CHAPSEC,subsection);
} else {
if(section) {
fprintf(fp,"{%s%s%s},\n",chapter,CHAPSEC,section);
} else {
fprintf(fp,"{%s},\n",chapter);
}
}
}
free(cs);
}
else err= __LINE__;
free(fun);
}
else /* !fun */
{
if(flags & TEXI_ITEMIZE_REFERENCES)
{
if(num_refs==0)
fprintf(fp,"@itemize\n");
fprintf(fp,"@item\nSee @file{%s}\n",l);
}
else /* not itemized */
{
if( (num_refs==0) && (flags & TEXI_GROUP_SECTIONS) )
fprintf(fp,"@group\n");
fprintf(fp,"%s{%s},\n",((num_refs==0) ? "@*See @file":"@file"),l);
}
}
/* now at least one reference is printed */
++num_refs;
}
}
}
if(num_refs > 0)
{
if(flags & TEXI_ITEMIZE_REFERENCES)
fprintf(fp,"@end itemize\n");
else /* not itemized */
{
if(flags & TEXI_GROUP_SECTIONS)
fprintf(fp,"@end group\n");
fprintf(fp,"for more information.\n");
}
}
#if 0
if(indent)
free(indent);
#endif
free(buf);
}
else err= __LINE__;
#undef isref
return err;
}
int gen_texinfo_header(FILE *fp, char *fname, char **header_macros)
{
int err= 0;
char *default_header=
"\\input texinfo @c -*-texinfo-*-\n"
"@comment %%**start of header\n"
"@setfilename PROJECT.guide\n"
"@settitle Autodocs for @code{PROJECT}\n"
"@paragraphindent 0\n"
"@iftex\n"
"@afourpaper\n"
"@finalout\n"
"@setchapternewpage on\n" /* odd */
"@end iftex\n"
"@comment %%**end of header\n\n"
"@ifinfo\n"
"@node Top\n"
"@top\n"
"This document describes @code{PROJECT} version REVISION.\n\n"
"@noindent Copyright @copyright{} COPYRIGHT\n"
"@end ifinfo\n\n"
"@titlepage\n"
"@title PROJECT\n"
"@subtitle Documentation taken from source code\n"
"@subtitle Edition EDITION for Version REVISION\n"
"@subtitle @today\n"
"@author AUTHOR\n\n"
"@page\n"
"@vskip 0pt plus 1filll\n"
"Copyright @copyright{} COPYRIGHT\n"
"@end titlepage\n"
"@headings double\n\n";
if(fname && *fname)
{
FILE *fh= fopen(fname,"r");
if(fh)
{
char *header= (char *)0;
size_t header_size= 0;
/* compute the size of the header file */
#ifdef BUGGY_FTELL
do {
(void)fgetc(fh);
if(!feof(fh))
++header_size;
} while(!feof(fh) || ferror(fh))
#else /* ftell() works fine */
if( fseek(fh,0L,2L) >= 0) /* 2 == OFFSET_END */
header_size= ftell(fh);
else
err= __LINE__;
#endif /* BUGGY_FTELL */
if(!ferror(fh) && header_size > 0)
{
if(fseek(fh,-header_size,1L) < 0) /* 1 == OFFSET_CURRENT */
err= __LINE__;
}
else err= __LINE__;
/* load the header */
if(err == 0)
{
if( (header= (char *)malloc( (header_size + 1) * sizeof(char) )) )
{
fread(header, sizeof(char), header_size, fh);
header[header_size]= '\0';
if( ferror(fh) )
{
free(header);
header= (char *)0;
err= __LINE__;
}
}
else err= __LINE__;
}
if(err == 0)
{
char *x= strexpand(header, header_macros);
if(x)
{
fputs(x,fp);
free(x);
}
else /* out of memory */
err= __LINE__;
}
fclose(fh);
}
}
else /* !fname */
{
char *x= strexpand(default_header, header_macros);
if(x)
{
fputs(x,fp);
free(x);
}
else /* out of memory */
err= __LINE__;
}
return err;
}
int gen_texinfo(FILE *fp, int tabsize, int flags, char *body_environment, char **body_macros)
{
int err= 0;
char *fun, *sec;
/* main menu:
create a @menu with an entry for each library.
an additional entry will be created for the function index */
if(err == 0)
{
char *last_chapter = (char *)0; /* last printed chapter */
char *last_section = (char *)0; /* last printed section */
fprintf(fp,"@ifnotinfo\n@node Top\n@end ifnotinfo\n@menu\n");
for(fun= stepfun(0); fun && (err==0); fun= stepfun(1))
{
char *cs, *chapter, *section, *subsection;
if( (cs= chapsec(fun, &chapter, §ion, &subsection)) )
{
/* fprintf(stderr, "chapter = %s\n", chapter ? chapter : "null"); */
/* fprintf(stderr, "last_chapter = %s\n", last_chapter ? last_chapter : "null"); */
/* fprintf(stderr, "section = %s\n", section ? section : "null"); */
/* fprintf(stderr, "subsection = %s\n\n", subsection ? subsection : "null"); */
/* new chapter */
if( !last_chapter || strcmp(last_chapter, chapter) )
{
fprintf(fp,"* %s::\n", chapter);
if(last_chapter)
free(last_chapter);
last_chapter = strdup(chapter);
if(last_section) {
free(last_section);
last_section = 0;
}
}
/* new section */
if ( section && ( !last_section || strcmp(last_section, section) ) ) {
fprintf(fp, "* %s %s::\n", chapter, section);
if(last_section)
free(last_section);
last_section = strdup(section);
}
free(cs);
}
else /* out of memory */
err= __LINE__;
}
if( !(flags & TEXI_NO_INDEX) )
fprintf(fp, "\n"
"* Function Index::\n");
fprintf(fp,"@end menu\n\n");
if(last_section) {
free(last_section);
}
if(last_chapter) {
free(last_chapter);
}
}
/* fprintf(stderr, "done with menu\n\n"); fflush(fp); */
#if 0
/* create one single (flat) menu for functions mapped to
@chapters. This code is obsolete since we introduced
the hierarchical layout with libraries mapped to
@chapters and functions mapped to @sections. */
if(err == 0)
{
fprintf(fp,"@menu\n");
for(fun= stepfun(0); fun && (err==0); fun= stepfun(1))
{
char *cs, *chapter, *section;
if( (cs= chapsec(fun, &chapter, §ion)) )
{
fprintf(fp,"* %s %s::\n", chapter, section);
free(cs);
}
else /* out of memory */
err= __LINE__;
}
if( !(flags & TEXI_NO_INDEX) )
fprintf(fp, "\n"
"* Function Index::\n");
fprintf(fp,"@end menu\n\n");
}
#endif
/* chapters & sections */
if(err == 0)
{
char *last_chapter = (char *)0;
char *last_section = (char *)0;
for(fun= stepfun(0); fun && (err==0); fun= stepfun(1))
{
char *cs, *chapter, *section, *subsection;
if( (cs= chapsec(fun, &chapter, §ion, &subsection)) )
{
int newpage = 0;
char *function = (char *)0;
if(subsection) {
function = subsection;
} else {
if(section) {
function = section;
} else {
function = chapter;
}
}
/* fprintf(stderr, "chapter = %s\n", chapter ? chapter : "null"); */
/* fprintf(stderr, "last_chapter = %s\n", last_chapter ? last_chapter : "null"); */
/* fprintf(stderr, "section = %s\n", section ? section : "null"); */
/* fprintf(stderr, "subsection = %s\n", subsection ? subsection : "null"); */
/* fprintf(stderr, "function = %s\n\n", function); */
if( !last_chapter || strcmp(last_chapter, chapter) )
{
newpage = 1;
/*
* Start a new chapter
*/
fprintf(fp, "\n"
"@node %s\n"
"@chapter %s\n\n"
,chapter
,chapter
);
/* print a sub menu for the current chapter */
/* pushfun();
{
int done;
char *sub_fun;
fprintf(fp,"@menu\n");
for(sub_fun= fun, done= 0; sub_fun && (err==0) && !done; sub_fun= stepfun(1))
{
char *sub_cs, *sub_chapter, *new_section, *new_subsection;
if( (sub_cs= chapsec(sub_fun, &sub_chapter, &new_section, &new_subsection)) )
{
if( (done= strcmp(sub_chapter, chapter)) == 0 )
fprintf(fp,"* %s %s::\n", sub_chapter, new_section);
free(sub_cs);
}
else err= __LINE__;
}
fprintf(fp,"@end menu\n\n");
}
popfun();
*/
if(last_chapter)
free(last_chapter);
last_chapter = strdup(chapter);
if(last_section)
free(last_section);
last_section = 0;
}
if(!newpage) /* we didn't start a new page for the chapter */
{
if(flags & TEXI_FUNCTION_NEWPAGE)
fprintf(fp,"@page\n");
}
if(section && ( !last_section || strcmp(last_section, section) ) )
{
/*
* Start a new section
*/
fprintf(fp, "\n"
"@node %s%s%s\n"
"@section %s\n\n"
,chapter, CHAPSEC, section
,section
);
/* print a sub menu for the current chapter */
/* pushfun();
{
int done;
char *sub_fun;
fprintf(fp,"@menu\n");
for(sub_fun= fun, done= 0; sub_fun && (err==0) && !done; sub_fun= stepfun(1))
{
char *sub_cs, *new_section, *sub_section, *new_subsection;
if( (sub_cs= chapsec(sub_fun, &new_section, &sub_section, &new_subsection)) )
{
if( (done= strcmp(new_section, section)) == 0 )
fprintf(fp,"* %s %s::\n", new_section, new_subsection);
free(sub_cs);
}
else err= __LINE__;
}
fprintf(fp,"@end menu\n\n");
}
popfun();
*/
if(last_section)
free(last_section);
last_section = strdup(section);
}
/*
* function on 3rd hierarchy
*/
if(subsection) {
fprintf(fp, "@node %s%s%s%s%s\n"
"@subsection %s\n\n"
,chapter,CHAPSEC,section,CHAPSEC,subsection
,subsection
);
}
fprintf(fp, "\n"
"@findex %s\n"
"\n"
,function
);
if(flags & TEXI_TABLE_FUNCTIONS)
fprintf(fp,"@table @b\n");
for(sec= stepsec(0); sec && (err == 0); sec= stepsec(1))
{
char *text= getsec(sec);
if(text && *text)
{
if(flags & TEXI_TABLE_FUNCTIONS)
fprintf(fp,"@item %s\n",sec);
else
fprintf(fp,"@b{%s}\n",sec); /* was @strong{} or @code{} */
/*fprintf(fp,"@noindent\n@b{%s}\n",sec);*/
if( (flags & TEXI_PARSE_REFERENCES) && (strcmp(sec,"SEE ALSO") == 0) )
err= see_also(fp, text, flags);
else /* ! "SEE ALSO" */
{
char *x= strexpand(text,body_macros);
if(x)
{
fprintf(fp,"@noindent\n"
"@%s\n",body_environment);
if(flags & TEXI_GROUP_SECTIONS)
fprintf(fp,"@group\n");
if(tabsize > 0)
fexpand(fp,tabsize,x);
else
fputs(x,fp);
if(flags & TEXI_GROUP_SECTIONS)
fprintf(fp,"@end group\n");
fprintf(fp,"@end %s\n"
"@refill\n",body_environment);
free(x);
}
else /* out of memory */
err= __LINE__;
}
}
/* else (!text) -> no error */
fprintf(fp,"\n");
}
if(flags & TEXI_TABLE_FUNCTIONS)
fprintf(fp,"@end table\n");
free(cs);
}
else /* out of memory */
err= __LINE__;
}
if(last_chapter)
free(last_chapter);
if(last_section)
free(last_section);
}
/* index */
if(err == 0)
{
if( !(flags & TEXI_NO_INDEX) )
fprintf(fp, "@node Function Index\n"
"@unnumbered Function Index\n"
"@printindex fn\n"
"\n"
"@page\n"
"@contents\n"
"@bye\n");
}
return err;
}
|