home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume25 / finger / part03 / comdef.c next >
C/C++ Source or Header  |  1992-04-03  |  6KB  |  240 lines

  1. /*
  2.  * comdef.c -- comment defines automagicly
  3.  *    esp for nasty ANSI conforming CPPs!
  4.  *
  5.  * Copyright (C) 1986, 1990  Philip L. Budne
  6.  *
  7.  * This file is part of "Phil's Finger Program".
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 1, or (at your option)
  12.  * any later version.
  13.  *
  14.  */
  15.  
  16. # ifndef lint
  17. static char *rcsid = "$Id: comdef.c,v 3.0 90/07/06 13:10:26 budd Rel $";
  18. # endif /* lint not defined */
  19.  
  20. # include <stdio.h>
  21. # include <ctype.h>
  22.  
  23. /* Should all be runtime options: */
  24. # define USEDEF                /* check for if(n)def for comments */
  25. /*# define DEBUG            /* output debug info */
  26.  
  27. /*
  28.  * the following 4 (PRESERVE_xxx) output "#[s\t]thing" as input if
  29.  * defined.  Otherwise if INDENTATION is defined they will be indented
  30.  * according to depth.  If INDENT_OTHER is defined even includes and
  31.  * defines will be indented (otherwise indented to MIN_INDENT).  if
  32.  * IDENTATION is not defined all output will be indented to
  33.  * MIN_INDENT.
  34.  */
  35.  
  36. /*# define PRESERVE_BAD            /* preserve indentation on failures */
  37. /*# define PRESERVE_IF            /* preserve indentation on ifs */
  38. /*# define PRESERVE_ENDIF        /* preserve indentation on endifs */
  39. /*# define PRESERVE_OTHER        /* preserve indentation on define... */
  40. /*# define INDENTATION            /* indent by depth (but not define..)*/
  41. /*# define INDENT_OTHER            /* indent define/include.. by depth */
  42.  
  43. # ifndef MIN_INDENT
  44. # define MIN_INDENT 1
  45. # endif /* MIN_INDENT not defined */
  46.  
  47. # define REL(i) ((i)+MIN_INDENT)
  48.  
  49. # define EOS '\0'
  50.  
  51. # define MAX 100            /* plenty!! */
  52. char stack[MAX][ 256 ];
  53. int flags[MAX];
  54. int sp = -1;
  55.  
  56. # define FL_NOT 01            /* inverse of "condition" */
  57. # define FL_ELS 02            /* seen else */
  58. # define FL_DEF 04            /* was ifdef or ifndef */
  59.  
  60. main() {
  61.     char line[ 1024 ], saved[ 1024 ];
  62.     register char *cp, *xp, *yp;
  63.     int ln;
  64.  
  65.     ln = -1;
  66.     while( gets( line ) ) {
  67.     cp = line;
  68.     ln++;
  69.  
  70.     while( *cp && isspace( *cp ) )
  71.         cp++;
  72.  
  73.     if( *cp != '#' ) {
  74.         puts( line );
  75.         continue;
  76.     }
  77.     cp++;
  78.  
  79.     while( *cp && isspace( *cp ) )
  80.         cp++;
  81.  
  82.     xp = cp;
  83.     while( isalpha( *cp ) )
  84.         cp++;
  85.  
  86.     strcpy( saved, line );        /* sigh */
  87.     if( *cp != EOS ) {
  88.         if( !isspace( *cp ) ) {
  89.         fprintf( stderr, "%d: bad line: %s\n", ln, line );
  90.         puts( line );
  91.         continue;
  92.         }
  93.         *cp++ = EOS;
  94.         while( isspace( *cp ) )
  95.         cp++;
  96.     }
  97.  
  98. # ifdef DEBUG
  99.     fprintf( stderr, " <<< %s >>>\n", xp );
  100. # endif /* DEBUG defined */
  101.     if( strncmp( xp, "if", 2 ) == 0 || strcmp( xp, "elif" ) == 0 ) {
  102.         if( strcmp( xp, "elif" ) != 0 )
  103.         sp++;
  104. # ifdef PRESERVE_IF
  105.         puts( saved );
  106. # else  /* PRESERVE_IF not defined */
  107.         putit( REL(sp), xp );
  108.         putchar(' ');
  109.         puts( cp );
  110. # endif /* PRESERVE_IF not defined */
  111.  
  112.         if( sp == MAX )
  113.         fprintf( stderr, "%d: Beyond MAX", ln );
  114.         else {
  115.         yp = cp;
  116.         while( *cp != EOS )
  117.             if( *cp == '/' && cp[1] == '*' ) {
  118.             while( isspace( cp[-1] ) && cp >= yp )
  119.                 cp--;
  120.             *cp = EOS;
  121.             break;
  122.             }
  123.             else
  124.             cp++;
  125. # ifdef DEBUG
  126.         fprintf( stderr, "saving: %s\n", yp );
  127. # endif /* DEBUG defined */
  128.         strcpy( stack[sp], yp );
  129.         flags[sp] = 0;
  130.  
  131.         /* check for !exp ???!! */
  132.         if( strcmp( xp, "ifndef" ) == 0 ) {
  133.             flags[sp] |= FL_NOT;
  134. # ifdef USEDEF
  135.             flags[sp] |= FL_DEF;
  136. # endif /* USEDEF defined */
  137.         }
  138. # ifdef USEDEF
  139.         else if( strcmp( xp, "ifdef" ) == 0 )
  140.             flags[sp] |= FL_DEF;
  141. # endif /* USEDEF defined */
  142.         }
  143.     }
  144.     else if( strcmp( xp, "else" ) == 0 ) {
  145.         if( sp < 0 ) {
  146.         fprintf( stderr, "%d: else not under if\n", ln );
  147. # ifdef PRESERVE_BAD
  148.         puts( saved );
  149. # else  /* PRESERVE_BAD not defined */
  150.         putit( REL(sp), "else" );
  151.         putchar('\n');
  152. # endif /* PRESERVE_BAD not defined */
  153.         }
  154.         else {
  155.         flags[sp] ^= FL_NOT;    /* invert not-ness */
  156.         if( flags[sp] & FL_ELS )
  157.             fprintf( stderr, "double else for %s at line %d????\n",
  158.                 stack[ sp ], ln );
  159. # ifdef PRESERVE_BAD
  160.         fputs( line, stdout );
  161. # else  /* PRESERVE_BAD not defined */
  162.         putit( REL(sp), "else" );
  163. # endif /* PRESERVE_BAD not defined */
  164.         /* extra space to match indent on endif */
  165.         putchar( ' ' );
  166.         comment(sp);
  167.         }
  168.     }
  169.     else if( strcmp( xp, "endif" ) == 0 ) {
  170.         if( sp >= MAX || sp < 0 ) {
  171.         if( sp < 0 )
  172.             fprintf( stderr, "%d: Too many endifs\n", ln );
  173. # ifdef PRESERVE_BAD
  174.         puts( saved );
  175. # else  /* PRESERVE_BAD not defined */
  176.         putit( REL(sp), "endif");
  177.         putchar('\n');
  178. # endif /* PRESERVE_BAD not defined */
  179.         } /* bad sp */
  180.         else {
  181. # ifdef PRESERVE_ENDIF
  182.         fputs( line, stdout );
  183. # else  /* PRESERVE_ENDIF not defined */
  184.         putit( REL(sp), "endif" );
  185. # endif /* PRESERVE_ENDIF not defined */
  186.         comment(sp+1);        /* decr in wrong place for indenting */
  187.         } /* ok sp */
  188.         sp--;
  189.     } /* found endif */
  190.     else {                /* found something else */
  191. # ifdef PRESERVE_OTHER
  192.         puts( saved );        /* define, include, or something... */
  193. # else  /* PRESERVE_OTHER not defined */
  194. # ifdef INDENT_OTHER
  195.         putit( REL(sp+1), xp );
  196. # else  /* INDENT_OTHER not defined */
  197.         putit( MIN_INDENT, xp );
  198. # endif /* INDENT_OTHER not defined */
  199.         putchar(' ');
  200.         puts( cp );
  201. # endif /* PRESERVE_OTHER not defined */
  202.     } /* define, include ... */
  203.     } /* while */
  204. } /* main */
  205.  
  206. putit( off, s )
  207.     register off;
  208.     char *s;
  209. {
  210. # ifdef DEBUG
  211.     fprintf( stderr, "level %d off %d putit: %s\n", sp, off, s );
  212. # endif /* DEBUG defined */
  213.  
  214.     putchar('#');
  215.  
  216. # ifdef INDENTATION
  217.     if( off < MIN_INDENT )
  218. # endif /* INDENTATION defined */
  219.     off = MIN_INDENT;
  220.  
  221.     while( off-- > 0 )
  222.     putchar(' ');
  223.  
  224.     fputs( s, stdout );
  225. }
  226.  
  227. comment() {                /* comment elses and endif */
  228.     fputs(" /* ", stdout );
  229.     if( (flags[sp] & FL_DEF) == 0 && (flags[sp] & FL_NOT) )
  230.     fputs("not ", stdout );
  231.  
  232.     fputs( stack[sp], stdout );
  233.     if( flags[sp] & FL_DEF )
  234.     if( flags[sp] & FL_NOT )
  235.         fputs(" not defined", stdout );
  236.     else
  237.         fputs(" defined", stdout );
  238.     puts( " */" );
  239. } /* comment */
  240.