1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3*27b03b36SApple OSS Distributions *
4*27b03b36SApple OSS Distributions * @APPLE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions *
6*27b03b36SApple OSS Distributions * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7*27b03b36SApple OSS Distributions * Reserved. This file contains Original Code and/or Modifications of
8*27b03b36SApple OSS Distributions * Original Code as defined in and that are subject to the Apple Public
9*27b03b36SApple OSS Distributions * Source License Version 1.0 (the 'License'). You may not use this file
10*27b03b36SApple OSS Distributions * except in compliance with the License. Please obtain a copy of the
11*27b03b36SApple OSS Distributions * License at http://www.apple.com/publicsource and read it before using
12*27b03b36SApple OSS Distributions * this file.
13*27b03b36SApple OSS Distributions *
14*27b03b36SApple OSS Distributions * The Original Code and all software distributed under the License are
15*27b03b36SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16*27b03b36SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17*27b03b36SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18*27b03b36SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19*27b03b36SApple OSS Distributions * License for the specific language governing rights and limitations
20*27b03b36SApple OSS Distributions * under the License."
21*27b03b36SApple OSS Distributions *
22*27b03b36SApple OSS Distributions * @APPLE_LICENSE_HEADER_END@
23*27b03b36SApple OSS Distributions */
24*27b03b36SApple OSS Distributions /*
25*27b03b36SApple OSS Distributions * decomment.c
26*27b03b36SApple OSS Distributions *
27*27b03b36SApple OSS Distributions * Removes all comments and (optionally) whitespace from an input file.
28*27b03b36SApple OSS Distributions * Writes result on stdout.
29*27b03b36SApple OSS Distributions */
30*27b03b36SApple OSS Distributions
31*27b03b36SApple OSS Distributions #include <stdio.h>
32*27b03b36SApple OSS Distributions #include <ctype.h> /* for isspace */
33*27b03b36SApple OSS Distributions #include <libc.h>
34*27b03b36SApple OSS Distributions
35*27b03b36SApple OSS Distributions /*
36*27b03b36SApple OSS Distributions * State of input scanner.
37*27b03b36SApple OSS Distributions */
38*27b03b36SApple OSS Distributions typedef enum {
39*27b03b36SApple OSS Distributions IS_NORMAL,
40*27b03b36SApple OSS Distributions IS_SLASH, // encountered opening '/'
41*27b03b36SApple OSS Distributions IS_IN_COMMENT, // within / * * / comment
42*27b03b36SApple OSS Distributions IS_STAR, // encountered closing '*'
43*27b03b36SApple OSS Distributions IS_IN_END_COMMENT // within / / comment
44*27b03b36SApple OSS Distributions } input_state_t;
45*27b03b36SApple OSS Distributions
46*27b03b36SApple OSS Distributions static void usage(char **argv);
47*27b03b36SApple OSS Distributions
48*27b03b36SApple OSS Distributions int
main(int argc,char ** argv)49*27b03b36SApple OSS Distributions main(int argc, char **argv)
50*27b03b36SApple OSS Distributions {
51*27b03b36SApple OSS Distributions FILE *fp;
52*27b03b36SApple OSS Distributions char bufchar;
53*27b03b36SApple OSS Distributions input_state_t input_state = IS_NORMAL;
54*27b03b36SApple OSS Distributions int exit_code = 0;
55*27b03b36SApple OSS Distributions int remove_whitespace = 0;
56*27b03b36SApple OSS Distributions int arg;
57*27b03b36SApple OSS Distributions
58*27b03b36SApple OSS Distributions if (argc < 2) {
59*27b03b36SApple OSS Distributions usage(argv);
60*27b03b36SApple OSS Distributions }
61*27b03b36SApple OSS Distributions for (arg = 2; arg < argc; arg++) {
62*27b03b36SApple OSS Distributions switch (argv[arg][0]) {
63*27b03b36SApple OSS Distributions case 'r':
64*27b03b36SApple OSS Distributions remove_whitespace++;
65*27b03b36SApple OSS Distributions break;
66*27b03b36SApple OSS Distributions default:
67*27b03b36SApple OSS Distributions usage(argv);
68*27b03b36SApple OSS Distributions }
69*27b03b36SApple OSS Distributions }
70*27b03b36SApple OSS Distributions
71*27b03b36SApple OSS Distributions fp = fopen(argv[1], "r");
72*27b03b36SApple OSS Distributions if (!fp) {
73*27b03b36SApple OSS Distributions fprintf(stderr, "Error opening %s\n", argv[1]);
74*27b03b36SApple OSS Distributions perror("fopen");
75*27b03b36SApple OSS Distributions exit(1);
76*27b03b36SApple OSS Distributions }
77*27b03b36SApple OSS Distributions for (;;) {
78*27b03b36SApple OSS Distributions bufchar = getc_unlocked(fp);
79*27b03b36SApple OSS Distributions if (bufchar == EOF) {
80*27b03b36SApple OSS Distributions break;
81*27b03b36SApple OSS Distributions }
82*27b03b36SApple OSS Distributions
83*27b03b36SApple OSS Distributions switch (input_state) {
84*27b03b36SApple OSS Distributions case IS_NORMAL:
85*27b03b36SApple OSS Distributions if (bufchar == '/') {
86*27b03b36SApple OSS Distributions /*
87*27b03b36SApple OSS Distributions * Might be start of a comment.
88*27b03b36SApple OSS Distributions */
89*27b03b36SApple OSS Distributions input_state = IS_SLASH;
90*27b03b36SApple OSS Distributions } else {
91*27b03b36SApple OSS Distributions if (!(remove_whitespace && isspace(bufchar))) {
92*27b03b36SApple OSS Distributions putchar_unlocked(bufchar);
93*27b03b36SApple OSS Distributions }
94*27b03b36SApple OSS Distributions }
95*27b03b36SApple OSS Distributions break;
96*27b03b36SApple OSS Distributions
97*27b03b36SApple OSS Distributions case IS_SLASH:
98*27b03b36SApple OSS Distributions switch (bufchar) {
99*27b03b36SApple OSS Distributions case '*':
100*27b03b36SApple OSS Distributions /*
101*27b03b36SApple OSS Distributions * Start of normal comment.
102*27b03b36SApple OSS Distributions */
103*27b03b36SApple OSS Distributions input_state = IS_IN_COMMENT;
104*27b03b36SApple OSS Distributions break;
105*27b03b36SApple OSS Distributions
106*27b03b36SApple OSS Distributions case '/':
107*27b03b36SApple OSS Distributions /*
108*27b03b36SApple OSS Distributions * Start of 'to-end-of-line' comment.
109*27b03b36SApple OSS Distributions */
110*27b03b36SApple OSS Distributions input_state = IS_IN_END_COMMENT;
111*27b03b36SApple OSS Distributions break;
112*27b03b36SApple OSS Distributions
113*27b03b36SApple OSS Distributions default:
114*27b03b36SApple OSS Distributions /*
115*27b03b36SApple OSS Distributions * Not the start of comment. Emit the '/'
116*27b03b36SApple OSS Distributions * we skipped last char in case we were
117*27b03b36SApple OSS Distributions * entering a comment this time, then the
118*27b03b36SApple OSS Distributions * current char.
119*27b03b36SApple OSS Distributions */
120*27b03b36SApple OSS Distributions putchar_unlocked('/');
121*27b03b36SApple OSS Distributions if (!(remove_whitespace && isspace(bufchar))) {
122*27b03b36SApple OSS Distributions putchar_unlocked(bufchar);
123*27b03b36SApple OSS Distributions }
124*27b03b36SApple OSS Distributions input_state = IS_NORMAL;
125*27b03b36SApple OSS Distributions break;
126*27b03b36SApple OSS Distributions }
127*27b03b36SApple OSS Distributions break;
128*27b03b36SApple OSS Distributions
129*27b03b36SApple OSS Distributions case IS_IN_COMMENT:
130*27b03b36SApple OSS Distributions if (bufchar == '*') {
131*27b03b36SApple OSS Distributions /*
132*27b03b36SApple OSS Distributions * Maybe ending comment...
133*27b03b36SApple OSS Distributions */
134*27b03b36SApple OSS Distributions input_state = IS_STAR;
135*27b03b36SApple OSS Distributions }
136*27b03b36SApple OSS Distributions break;
137*27b03b36SApple OSS Distributions
138*27b03b36SApple OSS Distributions
139*27b03b36SApple OSS Distributions case IS_STAR:
140*27b03b36SApple OSS Distributions switch (bufchar) {
141*27b03b36SApple OSS Distributions case '/':
142*27b03b36SApple OSS Distributions /*
143*27b03b36SApple OSS Distributions * End of normal comment.
144*27b03b36SApple OSS Distributions */
145*27b03b36SApple OSS Distributions input_state = IS_NORMAL;
146*27b03b36SApple OSS Distributions break;
147*27b03b36SApple OSS Distributions
148*27b03b36SApple OSS Distributions case '*':
149*27b03b36SApple OSS Distributions /*
150*27b03b36SApple OSS Distributions * Still could be one char away from end
151*27b03b36SApple OSS Distributions * of comment.
152*27b03b36SApple OSS Distributions */
153*27b03b36SApple OSS Distributions break;
154*27b03b36SApple OSS Distributions
155*27b03b36SApple OSS Distributions default:
156*27b03b36SApple OSS Distributions /*
157*27b03b36SApple OSS Distributions * Still inside comment, no end in sight.
158*27b03b36SApple OSS Distributions */
159*27b03b36SApple OSS Distributions input_state = IS_IN_COMMENT;
160*27b03b36SApple OSS Distributions break;
161*27b03b36SApple OSS Distributions }
162*27b03b36SApple OSS Distributions break;
163*27b03b36SApple OSS Distributions
164*27b03b36SApple OSS Distributions case IS_IN_END_COMMENT:
165*27b03b36SApple OSS Distributions if (bufchar == '\n') {
166*27b03b36SApple OSS Distributions /*
167*27b03b36SApple OSS Distributions * End of comment. Emit the newline if
168*27b03b36SApple OSS Distributions * appropriate.
169*27b03b36SApple OSS Distributions */
170*27b03b36SApple OSS Distributions if (!remove_whitespace) {
171*27b03b36SApple OSS Distributions putchar_unlocked(bufchar);
172*27b03b36SApple OSS Distributions }
173*27b03b36SApple OSS Distributions input_state = IS_NORMAL;
174*27b03b36SApple OSS Distributions }
175*27b03b36SApple OSS Distributions break;
176*27b03b36SApple OSS Distributions } /* switch input_state */
177*27b03b36SApple OSS Distributions } /* main read loop */
178*27b03b36SApple OSS Distributions
179*27b03b36SApple OSS Distributions /*
180*27b03b36SApple OSS Distributions * Done.
181*27b03b36SApple OSS Distributions */
182*27b03b36SApple OSS Distributions return exit_code;
183*27b03b36SApple OSS Distributions }
184*27b03b36SApple OSS Distributions
185*27b03b36SApple OSS Distributions static void
usage(char ** argv)186*27b03b36SApple OSS Distributions usage(char **argv)
187*27b03b36SApple OSS Distributions {
188*27b03b36SApple OSS Distributions printf("usage: %s infile [r(emove whitespace)]\n", argv[0]);
189*27b03b36SApple OSS Distributions exit(1);
190*27b03b36SApple OSS Distributions }
191