2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
27 * Removes all comments and (optionally) whitespace from an input file.
28 * Writes result on stdout.
32 #include <ctype.h> /* for isspace */
36 * State of input scanner.
40 IS_SLASH
, // encountered opening '/'
41 IS_IN_COMMENT
, // within / * * / comment
42 IS_STAR
, // encountered closing '*'
43 IS_IN_END_COMMENT
// within / / comment
46 static void usage(char **argv
);
48 int main(int argc
, char **argv
)
52 input_state_t input_state
= IS_NORMAL
;
54 int remove_whitespace
= 0;
59 for(arg
=2; arg
<argc
; arg
++) {
60 switch(argv
[arg
][0]) {
69 fp
= fopen(argv
[1], "r");
71 fprintf(stderr
, "Error opening %s\n", argv
[1]);
76 bufchar
= getc_unlocked(fp
);
85 * Might be start of a comment.
87 input_state
= IS_SLASH
;
90 if(!(remove_whitespace
&& isspace(bufchar
))) {
91 putchar_unlocked(bufchar
);
100 * Start of normal comment.
102 input_state
= IS_IN_COMMENT
;
107 * Start of 'to-end-of-line' comment.
109 input_state
= IS_IN_END_COMMENT
;
114 * Not the start of comment. Emit the '/'
115 * we skipped last char in case we were
116 * entering a comment this time, then the
119 putchar_unlocked('/');
120 if(!(remove_whitespace
&& isspace(bufchar
))) {
121 putchar_unlocked(bufchar
);
123 input_state
= IS_NORMAL
;
131 * Maybe ending comment...
133 input_state
= IS_STAR
;
142 * End of normal comment.
144 input_state
= IS_NORMAL
;
149 * Still could be one char away from end
156 * Still inside comment, no end in sight.
158 input_state
= IS_IN_COMMENT
;
163 case IS_IN_END_COMMENT
:
164 if(bufchar
== '\n') {
166 * End of comment. Emit the newline if
169 if(!remove_whitespace
) {
170 putchar_unlocked(bufchar
);
172 input_state
= IS_NORMAL
;
176 } /* switch input_state */
177 } /* main read loop */
185 static void usage(char **argv
)
187 printf("usage: %s infile [r(emove whitespace)]\n", argv
[0]);