Subject: Re: '//' for comments in C code? (archivers/unace)
To: None <tech-pkg@netbsd.org>
From: Erik Osheim <erik@plastic-idolatry.com>
List: tech-pkg
Date: 01/21/2004 18:35:40
--Apple-Mail-2--215823698
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed
>>> 3. add a pre-processing step that uses gnu cpp or awk to strip the
>>> // comments
>>
>> That's exactly what I had had in mind. How could this best be
>> accomplished
>> (using awk, for example)? And how would one add it to the Makefile?
>
> Sed seems simpler:
>
>
> .c.o:
> @(echo ${CC} ${CFLAGS} -c $< && \
> PID=$$$$ && \
> sed -e 's,//.*$$,,' < $< > tmp$${PID}.c && \
> ${CC} ${CFLAGS} -c tmp$${PID}.c && \
> mv tmp$${PID}.o $@ && \
> rm -f tmp$${PID}.c)
>
> I would have used /dev/stdin, but some cc's check that the file ends
> in .c
ADDENDUM: Just finished reading the other emails on this, which came in
while I was writing this. Not sure if this is as useful if it doesn't
do quoting (which I had forgotten also)... I could spiff up the awk
example to do that (although using " and ' as characters in awk is a
bitch).
Just to play devil's advocate: if the .c files mix the two commenting
styles in certain (admittedly unorthodox) ways, you could end up in
trouble. For instance:
/* begin comment
// end comment */
would seriously break something down the road, as would:
/* comment // */
I wrote something in awk that looks like it evaluates the commenting
properly to figure out what stays and what goes. Of course, no
guarantees that it doesn't have bugs, but I did some checking with a
pretty good test file and it did everything ok.
To use this, do: awk -f rem_comments.awk $FILE > temp${FILE}${PID}.c
I'm not as up on make, so I won't try to figure out exactly how that
should look. The script is attached below.
-- Erik
----rem_comments.awk----
$0~/\/\*|\*\/|\/\// {
i=1;
while(i < length) {
mysub = substr($0,i,length-i+1);
if(cmnt==0) {
a = index(mysub,"/*");
m = index(mysub,"//");
if( m && m<a ) {
a=0;
}
if(a) {
cmnt=1;
i = a + 2;
} else if(m) {
$0=substr($0,1,m-1);
break;
} else {
break;
}
} else {
z = index(mysub,"*/");
if( z ) {
cmnt=0;
i = i + ( z - 1 ) + 2;
} else {
break;
}
}
}
}
{
print;
}
------end rem_comments.awk-----
I tested this with this file:
-----test.c------
/* dumb test file for awk script
*
*/
#include <stdlib.h>
int main() {
// all goes
int b = 2;
int a = 2;
/* will this // mess up? */
/* how about // this? */ printf("a\n");
int c;// half stays half goes
printf("x\n");
// all goes
// /* this whole line is gone
printf("not a comment\n");
// */ so is this.
/*
// fake out */
printf("x\n");
return 0;
}
-----end test.c------
--Apple-Mail-2--215823698
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)
iD8DBQFADwzM6jwIlDfhHxgRAp0yAKCyK8tr2/uonktFDKxrIuC5P11lEACeNzOc
i1g2tk211DH05mTbdhKLqFc=
=D60G
-----END PGP SIGNATURE-----
--Apple-Mail-2--215823698--