home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / the25.zip / thesrc251.zip / manext.c < prev    next >
C/C++ Source or Header  |  1997-11-04  |  10KB  |  331 lines

  1. /***********************************************************************/
  2. /* MANEXT - Extract manual pages from C source code.                   */
  3. /***********************************************************************/
  4. /*
  5.  * MANEXT - A program to extract manual pages from C source code.
  6.  * Copyright (C) 1991-1997 Mark Hessling
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16.  * General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to:
  20.  *
  21.  *    The Free Software Foundation, Inc.
  22.  *    675 Mass Ave,
  23.  *    Cambridge, MA 02139 USA.
  24.  *
  25.  *
  26.  * If you make modifications to this software that you feel increases
  27.  * it usefulness for the rest of the community, please email the
  28.  * changes, enhancements, bug fixes as well as any and all ideas to me.
  29.  * This software is going to be maintained and enhanced as deemed
  30.  * necessary by the community.
  31.  *
  32.  * Mark Hessling                 Email:             M.Hessling@qut.edu.au
  33.  * PO Box 203                    Phone:                    +617 3802 0800
  34.  * Bellara                       http://www.gu.edu.au/gext/the/markh.html
  35.  * QLD 4507                      **** Maintainer PDCurses & REXX/SQL ****
  36.  * Australia                     ************* Author of THE ************
  37.  */
  38.  
  39. /*
  40. $Id: manext.c 2.1 1995/06/24 16:30:24 MH Rel MH $
  41. */
  42.  
  43. #ifdef HAVE_CONFIG_H
  44. # include <config.h>
  45. #endif
  46.  
  47. #include <stdio.h>
  48.  
  49. #ifdef HAVE_PROTO
  50. void display_info(void);
  51. #else
  52. void display_info();
  53. #endif
  54.  
  55. typedef unsigned char bool;
  56. typedef char CHARTYPE;
  57.  
  58. #define TRUE    1
  59. #define FALSE   0
  60.  
  61. #define MAX_LINE 255
  62.  
  63. #define FORMAT_MANUAL     0
  64. #define FORMAT_QUICK_REF  1
  65.  
  66. #define STATE_IGNORE    0
  67. #define STATE_SYNTAX    1
  68. #define STATE_COMMAND   2
  69. #define STATE_DEFAULT   3
  70. /***********************************************************************/
  71. #ifdef HAVE_PROTO
  72. short strzne(CHARTYPE *str,CHARTYPE ch)
  73. #else
  74. short strzne(str,ch)
  75. CHARTYPE *str;
  76. CHARTYPE ch;
  77. #endif
  78. /***********************************************************************/
  79. {
  80. /*--------------------------- local data ------------------------------*/
  81.  register short len=0;
  82.  register short  i = 0;
  83. /*--------------------------- processing ------------------------------*/
  84.  len = strlen(str);
  85.  for (; i<len && str[i]==ch; i++);
  86.  if (i>=len)
  87.     i = (-1);
  88.  return(i);
  89. }
  90. /***********************************************************************/
  91. #ifdef HAVE_PROTO
  92. short strzrevne(CHARTYPE *str,CHARTYPE ch)
  93. #else
  94. short strzrevne(str,ch)
  95. CHARTYPE *str;
  96. CHARTYPE ch;
  97. #endif
  98. /***********************************************************************/
  99. {
  100. /*--------------------------- local data ------------------------------*/
  101.  register short len=0;
  102. /*--------------------------- processing ------------------------------*/
  103.  len = strlen(str);
  104.  for (--len; len>=0 && str[len]==ch; len--);
  105.  return(len);
  106. }
  107. /***********************************************************************/
  108. #ifdef HAVE_PROTO
  109. bool blank_field(CHARTYPE *field)
  110. #else
  111. bool blank_field(field)
  112. CHARTYPE *field;
  113. #endif
  114. /***********************************************************************/
  115. {
  116. /*--------------------------- local data ------------------------------*/
  117. /*--------------------------- processing ------------------------------*/
  118.  if (strzne(field,' ') == (-1))
  119.     return(TRUE);                /* field is NULL or just contains spaces */
  120.  return(FALSE);
  121. }
  122. /***********************************************************************/
  123. #ifdef HAVE_PROTO
  124. CHARTYPE *strtrunc(CHARTYPE *string)
  125. #else
  126. CHARTYPE *strtrunc(string)
  127. CHARTYPE *string;
  128. #endif
  129. /***********************************************************************/
  130. {
  131. /*--------------------------- local data ------------------------------*/
  132.  register short i=0;
  133.  short pos=0;
  134. /*--------------------------- processing ------------------------------*/
  135.  pos = strzrevne(string,' ');
  136.  if (pos == (-1))
  137.     strcpy(string,"");
  138.  else
  139.     *(string+pos+1) = '\0';
  140.  pos = strzne(string,' ');
  141.  if (pos != (-1))
  142.    {
  143.     for (i=0;*(string+i)!='\0';i++)
  144.        *(string+i) = *(string+i+pos);
  145.     *(string+i) = '\0';
  146.    }
  147.  return(string);
  148. }
  149. /***********************************************************************/
  150. #ifdef HAVE_PROTO
  151. CHARTYPE *strtrim(CHARTYPE *string,char ch)
  152. #else
  153. CHARTYPE *strtrim(string,ch)
  154. CHARTYPE *string,ch;
  155. #endif
  156. /***********************************************************************/
  157. {
  158. /*--------------------------- local data ------------------------------*/
  159.  register short i=0;
  160.  short pos=0;
  161. /*--------------------------- processing ------------------------------*/
  162.  for (i=0;i<strlen(string);i++)
  163.    {
  164.     if (*(string+i) == ch)
  165.        return(string+i+1);
  166.    }
  167.  return(string);
  168. }
  169. /***********************************************************************/
  170. #ifdef HAVE_PROTO
  171. int main(int argc,char *argv[])
  172. #else
  173. int main(argc,argv)
  174. int argc;
  175. char *argv[];
  176. #endif
  177. /***********************************************************************/
  178. {
  179.  char    s[MAX_LINE + 1];        /* input line */
  180.  char    save_line[MAX_LINE + 1];
  181.  register int     i = 0;
  182.  FILE *fp;
  183.  char c;
  184.  char append=0;
  185.  int format=FORMAT_MANUAL;
  186.  int state=STATE_IGNORE;
  187.  int file_start=1;
  188.  
  189. #ifdef __EMX__
  190.  _wildcard(&argc,&argv);
  191. #endif
  192.  
  193.  if (strcmp(argv[1],"-h") == 0)
  194.    {
  195.     display_info();
  196.     exit(1);
  197.    }
  198.  if (strcmp(argv[1],"-q") == 0) /* generate quick reference */
  199.    {
  200.     format = FORMAT_QUICK_REF;
  201.     file_start = 2;
  202.    }
  203.  for(i=file_start;i<argc;i++)
  204.     {
  205.      if ((fp = fopen(argv[i],"r")) == NULL)
  206.        {
  207.         fprintf(stderr,"\nCould not open %s\n",argv[i]);
  208.         continue;
  209.        }
  210.      while(1)
  211.        {
  212.         if (fgets(s, (int)sizeof(s), fp) == NULL)
  213.           {
  214.            if (ferror(fp) != 0)
  215.              {
  216.               fprintf(stderr, "*** Error reading %s.  Exiting.\n",argv[i]);
  217.               exit(1);
  218.              }
  219.            break;
  220.           }
  221.  
  222.         /* check for manual entry marker at beginning of line */
  223.         if (strncmp(s, "/*man-start*", 12) != 0)
  224.             continue;
  225.  
  226.         state = STATE_IGNORE;
  227.         /* inner loop */
  228.         for (;;)
  229.            {
  230.             /* read next line of manual entry */
  231.             if (fgets(s, (int)sizeof(s), fp) == NULL)
  232.               {
  233.                if (ferror(fp) != 0)
  234.                  {
  235.                   fprintf(stderr, "*** Error reading %s.  Exiting.\n",argv[i]);
  236.                   exit(1);
  237.                  }
  238.                 break;
  239.               }
  240.             /* check for end of entry marker */
  241.             if (strncmp(s, "**man-end", 9) == 0)
  242.                break;
  243.             switch(format)
  244.               {
  245.                case FORMAT_MANUAL:
  246.                     printf("     %s",s);
  247.                     break;
  248.                case FORMAT_QUICK_REF:
  249.                     s[strlen(s)-1] = '\0';
  250.                     switch(state)
  251.                       {
  252.                        case STATE_IGNORE:
  253.                             if (strncmp(s, "COMMAND", 7) == 0)
  254.                               {
  255.                                state = STATE_COMMAND;
  256.                                break;
  257.                               }
  258.                             if (strncmp(s, "SYNTAX", 6) == 0)
  259.                               {
  260.                                state = STATE_SYNTAX;
  261.                                break;
  262.                               }
  263.                             if (strncmp(s, "DEFAULT", 7) == 0)
  264.                               {
  265.                                state = STATE_DEFAULT;
  266.                                break;
  267.                               }
  268.                             break;
  269.                        case STATE_COMMAND:
  270.                             strcpy(save_line,s);
  271.                             state = STATE_IGNORE;
  272.                             break;
  273.                        case STATE_DEFAULT:
  274.                             if (blank_field(s))
  275.                               {
  276.                                state = STATE_IGNORE;
  277.                                break;
  278.                               }
  279.                             printf("       Default: %s\n",strtrunc(s));
  280.                             break;
  281.                        case STATE_SYNTAX:
  282.                             if (blank_field(s))
  283.                               {
  284.                                printf("       %s\n",strtrunc(strtrim(save_line,'-')));
  285.                                state = STATE_IGNORE;
  286.                                break;
  287.                               }
  288.                             printf(" %s\n",strtrunc(s));
  289.                             break;
  290.                        default:
  291.                             break;
  292.                       }
  293.                     break;
  294.                default:
  295.                     break;
  296.               }
  297.             }
  298.         if (format == FORMAT_MANUAL)
  299.            printf("\n\n\n     --------------------------------------------------------------------------\n");
  300.  
  301.         /* check if end of file */
  302.         if (feof(fp) != 0)
  303.             break;
  304.        }
  305.      fclose(fp);
  306.     }
  307.  if (format == FORMAT_MANUAL)
  308.     printf("\n\n\n\n\n");
  309.  return(0);
  310. }
  311. /***********************************************************************/
  312. #ifdef HAVE_PROTO
  313. void display_info(void)
  314. #else
  315. void display_info()
  316. #endif
  317. /***********************************************************************/
  318. {
  319. /*--------------------------- local data ------------------------------*/
  320. /*--------------------------- processing ------------------------------*/
  321.  
  322.  fprintf(stderr,"\nMANEXT 1.00 Copyright (C) 1991-1997 Mark Hessling\n");
  323.  fprintf(stderr,"All rights reserved.\n");
  324.  fprintf(stderr,"MANEXT is distributed under the terms of the GNU\n");
  325.  fprintf(stderr,"General Public License and comes with NO WARRANTY.\n");
  326.  fprintf(stderr,"See the file COPYING for details.\n");
  327.  fprintf(stderr,"\nUsage: MANEXT sourcefile [...]\n\n");
  328.  fflush(stderr);
  329.  return;
  330. }
  331.