home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / indexmac < prev    next >
Internet Message Format  |  1989-02-03  |  4KB

  1. Path: xanth!mcnc!ncsuvx!gatech!bloom-beacon!husc6!necntc!ncoast!allbery
  2. From: davidsen@crdos1.UUCP (Wm E. Davidsen)
  3. Newsgroups: comp.sources.misc
  4. Subject: v03i011: Creating index listings with [nt]roff
  5. Message-ID: <8805021351.AA03385@crdos1.UUCP>
  6. Date: 2 May 88 17:51:44 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: davidsen@crdos1.UUCP (Wm E. Davidsen)
  9. Lines: 132
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. comp.sources.misc: Volume 3, Issue 11
  13. Submitted-By: "Wm E. Davidsen" <davidsen@crdos1.UUCP>
  14. Archive-Name: indexmac
  15.  
  16.   There was a request for a way to generate an index using ?roff. Here
  17. is the code. At the beginning of the roff input place an
  18.         .so index.mac
  19. to include the index macro. At each place in the text where you want
  20. an index entry, place a line of the form
  21.         .IX "subject"
  22.  
  23.   When running ?roff, save the error output in a file:
  24.     # create the file with the index data
  25.         nroff -mm -Tljc.12 myfile.n >/dev/null 2>myfile.idx
  26.     # sort the data and create the complete document
  27.     sort myfile.idx | indexfmt | cat myfile.n - |
  28.         nroff -man -Tljc.12 > myfile.prn
  29.  
  30.   You will have to use tbl, eqn, etc as needed.  If you can use a
  31. standard format for the index, you can run it off as a separate
  32. document. I actually do something else with named pipes, bit it's not
  33. portable.
  34.  
  35.   You will probably want to hack this a little, but it does work as is.
  36.  
  37. BUGS: if you mess up your input or command options the error messages
  38. are sent to the index file. The index should be created from a known good
  39. input.
  40.  
  41. #!/bin/sh
  42. # shar:    Shell Archiver  (v1.20)
  43. #
  44. #    Run the following text with /bin/sh to create:
  45. #      index.mac
  46. #      indexfmt.c
  47. #
  48. echo "x - extracting index.mac (Text)"
  49. sed 's/^X//' << 'SHAR_EOF' > index.mac &&
  50. X.de IX
  51. X.tm \\$1;\\nP
  52. X..
  53. SHAR_EOF
  54. chmod 0644 index.mac || echo "restore of index.mac fails"
  55. echo "x - extracting indexfmt.c (Text)"
  56. sed 's/^X//' << 'SHAR_EOF' > indexfmt.c &&
  57. X/*
  58. X *  ixformat - format an index file for output
  59. X *
  60. X *  Bill Davidsen, June 1986
  61. X *
  62. X *  This program is a filter which accepts the list of subjects
  63. X *  for an index, and the page on which they occur. It then emits
  64. X *  output in xroff format with all refs to a single subject on
  65. X *  one line.
  66. X */
  67. X
  68. X#include <stdio.h>
  69. X#define MAXLINE 80        /* longest subject */
  70. X#define MAXREFS 10        /* refs to any one subject */
  71. X
  72. Xchar  current[MAXLINE];        /* current subject */
  73. Xint  refs[MAXREFS];        /* current refs */
  74. Xint  nref = 0;            /* # of refs to current subj */
  75. X
  76. Xmain ()
  77. X{
  78. X    char  line[MAXLINE];    /* next subject */
  79. X    int  page;            /* page of current ref */
  80. X
  81. X    while (scanf ("%[^;];%d", line, &page) == 2)
  82. X    { /* see if this is a new subject */
  83. X    while (getchar () != '\n'); /* skip to EOL */
  84. X
  85. X    if (nref == 0)
  86. X    { /* this is the first one in */
  87. X        strcpy (current, line);
  88. X        refs[nref++] = page;
  89. X    /* get the proper footer on it */
  90. X        printf (".PH \"\"\n.PF \"//- index %% -//\"\n.nf\n");
  91. X        continue;
  92. X    }
  93. X
  94. X    if (strcmp (line, current))
  95. X    { /* starting a new subject, output this one */
  96. X        flushref (0);
  97. X
  98. X    /* copy the new subject and page in */
  99. X        strcpy (current, line);
  100. X        refs[0] = page;
  101. X        nref = 1;
  102. X    }
  103. X    else
  104. X    { /* add a reference to this subject */
  105. X        if (nref == MAXREFS)
  106. X        flushref (1);
  107. X        refs[nref++] = page;
  108. X    }
  109. X    }
  110. X
  111. X /* if there are references left, flush them */
  112. X    if (nref)
  113. X    flushref (0);
  114. X    exit (0);
  115. X}
  116. X
  117. XX/*
  118. X *  flushref - output a subject and all current references to it.
  119. X */
  120. X
  121. Xflushref (flag)
  122. X    int  flag;
  123. X{
  124. X    int  ix;            /* loop index */
  125. X
  126. X    printf ("%s  ", current);
  127. X    for (ix = 0; ix < nref;)
  128. X    { /* output a reference, and comma if more */
  129. X    printf ("%d", refs[ix++]);
  130. X    if (ix < nref)
  131. X        putchar (',');
  132. X    }
  133. X    if (flag)
  134. X    putchar (',');
  135. X    putchar ('\n');
  136. X}
  137. SHAR_EOF
  138. chmod 0644 indexfmt.c || echo "restore of indexfmt.c fails"
  139. exit 0
  140.  
  141. bill davidsen        (wedu@ge-crd.ARPA -or- davidsen@crdos1.uucp)
  142.             {uunet | philabs}!steinmetz!crdos1!davidsen
  143. "Stupidity, like virtue, is its own reward" -me
  144.