/*- * Copyright (c) 2000 Chris Costello * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include void usage(void); char *find_comment(char *); void usage(void) { fprintf(stderr, "Usage: comments [file ...]\n"); exit(EXIT_FAILURE); } char * find_comment(char *s) { int constant = 0; for (; *s != '\n'; ++s) { if (*s == '"') constant = !constant; if (!constant && *s == '/' && s[1] == '*') return (s); } return (NULL); } int main(int argc, char **argv) { char linebuf[_POSIX2_LINE_MAX]; char *cp, *ep; int comment; if (argc < 2) usage(); for (--argc, ++argv; argc != 0; --argc, ++argv) { if (strcmp(*argv, "-") == 0) *argv = "/dev/stdin"; if (freopen(*argv, "r", stdin) == NULL) { perror(*argv); continue; } comment = 0; while (fgets(linebuf, sizeof linebuf, stdin) != NULL) { if (!comment) { cp = find_comment(linebuf); if (cp != NULL) { comment = 1; cp += 2; if (*cp == '-') cp++; cp += strspn(cp, "\t "); if ((ep = strstr(cp, "*/")) != NULL) { *ep = '\n'; *++ep = '\0'; comment = 0; } if (*cp == '\n') continue; fputs(cp, stdout); } } else { cp = strstr(linebuf, "*/"); if (cp != NULL) { *cp = '\0'; comment = 0; } cp = linebuf + strspn(linebuf, "\t "); if (*cp == '*') { ++cp; cp += strspn(cp, "\t "); } fputs(cp, stdout); } } } return 0; }