coltoff@burdvax.UUCP (Joel Coltoff) (08/13/84)
Isn't there anyone out there. The other day I was given the task of writing some documentation on a group of programs used here. Since the person we got them from is no longer available for comment and no notes were left around I had to start with the comments in the program. Fortunately this was done quite well. However I wasn't up to editing all the source files just to look at the comments. I remembered that way back in the dark and distant past a program was posted to the net that would check a C program for proper use of comments. With a few quick mods this was changed to be a comment printer. I've left this with the same name the original poster gave it but feel it deserves a better name. It sort of does the opposite of the C pre-processor but that doesn't give me any hints for a humorous name. Maybe it can be call buchwald Since Art Buchwald can look at a mish mash of nonsense and spit out some comments on what might be going on. Your comments and suggestions are welcome. ----------------- TEAR HERE AND RUN THROUGH SH ------------------------ : This is a shar archive. Extract with sh, not csh. echo x - cnest.c cat > cnest.c << '!Weasels!Rip!My!Flesh' /* Title: cnest Purpose: To find and report all nested or unclosed comments in a c source file. Author: Tom Anderson Modifications: Joel Coltoff Usage: cnest <filename1> <filename2> ... History: December 3, 1982 creation date August 14, 1984 comment printing */ #include <stdio.h> #define TRUE 1 #define FALSE 0 main(argc,argv) unsigned int argc ; char *argv[] ; { char c ; char prev_char; unsigned char in_comment; unsigned int line_number; unsigned int file_index; int check; int i,j ; char status; FILE *chk_file; status = 0; file_index = 1; check = 0; /* Process arguments in program call */ for ( i = 1; argv[i][0] == '-'; ++i ) { /* set options */ j= 1; /* skip past '-' */ while (argv[i][j] != NULL) { switch (argv[i][j]) { case 'c' : check ++ ; file_index = 2; argc -- ; break; default : printf("Invalid argument \"-%c\"\n",argv[i][j]); exit(-1); break; } ++j; /* skip to next option */ } } do { line_number = 1; in_comment = FALSE; prev_char = ' '; if (argc == 1) chk_file = stdin; else { if ((chk_file = fopen(argv[file_index],"r")) == (FILE *) NULL) { fprintf(stderr,"%s: Can't access %s",argv[0],argv[file_index]); status = 2; continue; } } while ( ( c = fgetc(chk_file) ) != EOF ) { switch (c) { case '\n': line_number++; if ( in_comment ) putchar( c ); break; case '*': if (prev_char == '/') { if (in_comment && check ) { if (argc >= 2 ) fprintf(stderr, "%s: Line %d: Consecutive comment begins\n", argv[file_index],line_number); else fprintf(stderr, "Line %d: Consecutive comment begins\n", line_number); status = 1; } in_comment = TRUE; printf( "/*" ); } break; case '/': if (prev_char == '*') { if (! in_comment && check ) { if (argc >= 2 ) fprintf(stderr, "%s: Line %d: Comment close without begin\n", argv[file_index],line_number); else fprintf(stderr, "Line %d: Comment close without begin\n", line_number); status = 1; } in_comment = FALSE; printf( "*/\n" ); } break; default: if ( in_comment ) putchar( c ); break; } prev_char = c; } if (in_comment && check ) { if (argc >= 2 ) fprintf(stderr, "%s: Unclosed comment\n", argv[file_index]); else fprintf(stderr, "Unclosed comment\n"); status = 1; } fclose(chk_file); } while (++file_index < argc); exit (status); } !Weasels!Rip!My!Flesh echo x - cnest.1 cat > cnest.1 << '!Weasels!Rip!My!Flesh' .TH CNEST 1B 8/14/84 .UC .SH CNEST cnest \- Print or check comments in a C program .SH SYNOPSIS .B cnest [ -c] [filename] .. .SH DESCRIPTION .I Cnest is program that will print the comments in a C program to the standard output. Optionally it can be used to check that comment delimiters are properly balanced. .SH DIAGNOSTICS Prints error message for three conditions. 1) Starting a second comment without closing the first. 2) Closing a comment without opening one. 3) Not closing a comment. .SH AUTHOR Original program Tom Anderson, modifications by Joel Coltoff .SH BUGS The first diagnostic may not be an error. Should be done with .I lex but I haven't learned that yet. !Weasels!Rip!My!Flesh -------------------- THE END ---------------------- And remember, Mars Needs Women Joel Coltoff {sjuvax,bpa,sdcrdcf}!burdvax!coltoff (215)648-7258 -- Joel Coltoff {presby,bpa,psuvax}!burdvax!coltoff (215)648-7258