home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / GETCMT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  10KB  |  270 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* getcmt.c - get comments from a C or C++ source file    */
  4. /*
  5.                                Byte_Magic Software
  6.                              9850 Meadowglen Ln. #35
  7.                                Houston, Tx.  77042
  8.                                  (713) 975-9033
  9.  
  10. Author:     Greg Messer
  11. Program:    getcmt.c
  12. Purpose:    Isolate comments from a C or C++ source file. Filter.
  13. Syntax:     getcmt [/L?] [filename]
  14.             (may use redirection for file and/or device I/O)
  15. Release:    Released to the Public Domain by Byte_Magic Software.
  16. Date:       07-11-88 GM...
  17.                 First release.
  18. Revised:    01-13-90 GM...
  19.                 Streamlined logic.
  20.             01-15-90 Bob Stout (RBS)...
  21.                 Fixed unsigned char error as return from getc().
  22.             01-22-90 GM...
  23.                 Fixed bug handling "xx/" (xx = **) at end of comment.
  24.                 Added C++ comment extraction.
  25.                 Added return of count to OS (ERRORLEVEL in MS-DOS).
  26.             01-24-90 RBS
  27.                 Added filename spec option
  28.                 Added /? switch
  29. System:     Compiled with Zortech C V2.06 under MS-DOS 3.20
  30.             for IBM PCs and compatibles.
  31. Rules:      ANSI C comments begin with /x and end with x/. (x = *).
  32.             Comments do not nest and do not appear in string or character
  33.             constants.
  34.             C++ comments begin with double slashes and end at EOL.
  35.             A Microsoft extension to C allows C++ style comments to serve as
  36.             single-line comments in C source.
  37. Comments:   Useful for creating documentation and improving commenting style.
  38.             Input may be from a specified filename or stdin.
  39.             Output is to stdout, so use DOS redirection for output.
  40.             Messages go to stderr so they are not redirectable from the screen.
  41.             Returns ERRORLEVEL = number of comments in source file(s).
  42. Examples:
  43.             Example... Output to screen:
  44.             getcmt < cfile.c
  45.                (displays comments from cfile.c on screen)
  46.             type cfile.c | getcmt
  47.                (same as above but slightly slower)
  48.             getcmt < cfile.c | more
  49.                (same as above, but pauses after each full screen)
  50.             getcmt cfile.c /l | more
  51.                (same as above, but display line numbers)
  52.  
  53.             Example... Output to printer:
  54.             getcmt < cfile.c > prn
  55.                (same as above but prints output on printer)
  56.             type cfile.c | getcmt > prn
  57.                (same as above but slightly slower)
  58.  
  59.             Example... Output to file:
  60.             getcmt < cfile.c > cfile.cmt
  61.                (writes cfile.c comments to cfile.cmt, overwriting existing file)
  62.             getcmt < cfile.c >> cfile.doc
  63.                (writes cfile.c comments to end of cfile.doc (appends))
  64.                        
  65.             getcmt /?
  66.                (displays help screen, returns ERRORLEVEL = 0)
  67.             getcmt /x
  68.                (invalid option - displays help screen, returns ERRORLEVEL = -1)
  69.  
  70.             For complete instructions on using redirection symbols, consult
  71.             the PC-DOS or MS-DOS manual or a general DOS reference book.
  72. */
  73.  
  74. #include <stdlib.h>
  75. #include <stdio.h>
  76. #include <ctype.h>
  77. #include <string.h>
  78.  
  79. #define LOOKS_GREAT 1
  80. #define LESS_FILLING 0
  81.  
  82. int extract_c_cmts(void);
  83. void inside_c_cmt(int);
  84.  
  85. FILE *infile = stdin;                   /* read input from here             */
  86. int  show_nos = 0;                      /* 0 = don't display line numbers   */
  87. int  line_no = 1;                       /* line_no = line number            */
  88.  
  89. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  90.  
  91. main(int argc, char *argv[])                            /* main logic:      */
  92. {
  93.     register int i;
  94.     const char *hype =
  95.         "\nGETCMT v1.1 - GET CoMmenTs\nby Byte_Magic Software\n";
  96.     const char *help =
  97.         "\nUsage: GETCMT [/l?] [filename | <sourcefile.ext] "
  98.         "[>destination file or device]\n"
  99.         "Options:  l - Print line numbers\n"
  100.         "          ? - Help\n"
  101.         "\nFilename optional - Reads source code from stdin "
  102.         "(Ctrl-C to quit before EOF)\n";
  103.     const char *oops =
  104.         "\a*** GETCMT - Can't open input file ";
  105.                                             /* display messages to operator */
  106. #if LOOKS_GREAT
  107.     fputs(hype, stderr);
  108. #elif LESS_FILLING
  109.     i = 0;
  110.     while(hype[i] != '\0')
  111.         putc(hype[i++], stderr);
  112. #endif
  113.  
  114.     if (1 < argc)
  115.     {
  116.         for (i = 1; i < argc; ++i)
  117.         {
  118.             if ('/' == *argv[i])
  119.             {
  120.                 if ('l' == tolower(argv[i][1]))
  121.                     show_nos = 1;
  122.                 else
  123.                 {
  124.                     int ercode;
  125.  
  126.                     ercode = ('?' == argv[i][1]) ? 0 : -1;
  127. #if LOOKS_GREAT
  128.                     fputs(help, stderr);
  129. #elif LESS_FILLING
  130.                     i = 0;
  131.                     while(help[i] != '\0')
  132.                         putc(help[i++], stderr);
  133. #endif
  134.                     if (ercode)             /* output BEL if invalid switch */
  135.                         putc('\a', stderr);
  136.                     return(ercode);
  137.                 }
  138.             }
  139.             else
  140.             {
  141.                 infile = fopen(argv[i], "r");
  142.                 if (!infile)
  143.                 {
  144. #if LOOKS_GREAT
  145.                     fputs(oops, stderr);
  146.                     fputs(argv[i], stderr);
  147. #elif LESS_FILLING
  148.                     char *p = argv[i];
  149.  
  150.                     i = 0;
  151.                     while (oops[i])
  152.                         putc(oops[i], stderr);
  153.                     i = 0;
  154.                     while (*p)
  155.                         putc(*p++, stderr);
  156. #endif
  157.                 }
  158.             }
  159.         }
  160.     }
  161.  
  162.     i = extract_c_cmts();                   /* extract comments in infile   */
  163.     putc('\n', stdout);
  164.  
  165.     return(i);                              /* return number of comments to */
  166.                                             /* OS (ERRORLEVEL in DOS)       */
  167. }
  168.  
  169. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  170.  
  171. int extract_c_cmts()                        /* comment extraction logic:    */
  172. {
  173.     register int chi, cht;              /* chi = char in, cht = char test   */
  174.     int count;                          /* count = comment count            */
  175.  
  176.     count = 0;
  177.     chi = getc(infile);
  178.     while(chi != EOF)                       /* as long as there is input... */
  179.     {
  180.         if(chi == '/')                      /* process comments             */
  181.         {
  182.             cht = getc(infile);
  183.             if(cht == EOF)
  184.                 return(count);
  185.             if(cht == '*' || cht == '/')    /* if start of a comment...     */
  186.             {
  187.                 count++;                    /* count it and                 */
  188.                 inside_c_cmt(cht);          /* output all of the comment    */
  189.             }
  190.             else
  191.                 ungetc(cht, infile);
  192.         }
  193.         if ('\n' == chi)
  194.             line_no += 1;
  195.         chi = getc(infile);                  /* continue scanning input     */
  196.     }
  197.     return(count);
  198. }
  199.  
  200. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  201.  
  202. char *lntoaz(void)                      /* line number to zero-padded ASCII */
  203. {
  204.     int i, num = line_no;
  205.     static char numbuf[] = "0000: ";
  206.  
  207.     if (9999 < num)
  208.         strncpy(numbuf, "0000", 4);
  209.     else for (i = 3; i >= 0; --i)
  210.     {
  211.         numbuf[i] = (char)('0' + num % 10);
  212.         num /= 10;
  213.     }
  214.     return numbuf;
  215. }
  216.  
  217. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  218.  
  219. void inside_c_cmt(int ch)                   /* comment output logic:        */
  220. {                                           /* input ch = either '*' for C  */
  221.                                             /* or '/' for C++               */
  222.  
  223.     register int chi, cht;              /* chi = char in, cht = char test   */
  224. #if LESS_FILLING
  225.     char *p;
  226. #endif
  227.  
  228.     if(ch == '/')                           /* make ch = '\n' if C++        */
  229.         ch = '\n';                          /* note: ch is already 1st char */
  230.                                             /* of end comment if this is C  */
  231.     putc('\n', stdout);
  232.     if (show_nos)
  233.     {
  234. #if LOOKS_GREAT
  235.     fputs(lntoaz(), stdout);
  236. #elif LESS_FILLING
  237.     p = lntoaz();
  238.         while (*p)
  239.         putc(*p++, stdout);
  240. #endif
  241.     }
  242.     chi = getc(infile);
  243.     while(chi != EOF)                       /* as long as there is input... */
  244.     {                                       /* process comments             */
  245.         if(chi == ch)
  246.         {
  247.             if(ch == '\n')                  /* if C++ comment is ended...   */
  248.                 return;                     /* stop outputting              */
  249.             cht = getc(infile);
  250.             if(cht == '/')                  /* if C comment is ended...     */
  251.                 return;                     /* stop outputting              */
  252.             else
  253.             {
  254.                 ungetc(cht, infile);
  255.                 putc(chi, stdout);
  256.             }
  257.         }
  258.         else
  259.             putc(chi, stdout);              /* else comment text, output it */
  260.         if ('\n' == chi)
  261.             line_no += 1;
  262.                                             
  263.         chi = getc(infile);                  /* continue scanning input     */
  264.     }
  265.     return;
  266. }
  267.  
  268. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  269. /* end of getcmt.c  */
  270.