bin/67307: ready to import bootstrap_cmds/decomment from Darwin
Cyrille Lefevre
cyrille.lefevre at laposte.net
Fri May 28 13:10:13 PDT 2004
>Number: 67307
>Category: bin
>Synopsis: ready to import bootstrap_cmds/decomment from Darwin
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: freebsd-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: change-request
>Submitter-Id: current-users
>Arrival-Date: Fri May 28 13:10:04 PDT 2004
>Closed-Date:
>Last-Modified:
>Originator: Cyrille Lefevre
>Release: FreeBSD 5.2-CURRENT i386
>Organization:
ACME
>Environment:
System: FreeBSD gits 5.2-CURRENT FreeBSD 5.2-CURRENT #28: Thu May 13 00:19:50 CEST 2004 root at gits:/disk3/freebsd/current/obj/disk3/freebsd/current/src/sys/CUSTOM i386
>Description:
decomment strip off C/C++ comments from a file.
>How-To-Repeat:
n/a
>Fix:
# This is a shell archive. Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file". Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
# usr.bin/decomment/Makefile
# usr.bin/decomment/decomment.c
#
echo x - usr.bin/decomment/Makefile
sed 's/^X//' >usr.bin/decomment/Makefile << 'END-of-usr.bin/decomment/Makefile'
X# $FreeBSD$
X
XPROG= decomment
XWARNS?= 6
XNO_MAN= yes
X
X.include <bsd.prog.mk>
END-of-usr.bin/decomment/Makefile
echo x - usr.bin/decomment/decomment.c
sed 's/^X//' >usr.bin/decomment/decomment.c << 'END-of-usr.bin/decomment/decomment.c'
X/*
X * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
X *
X * @APPLE_LICENSE_HEADER_START@
X *
X * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
X *
X * This file contains Original Code and/or Modifications of Original Code
X * as defined in and that are subject to the Apple Public Source License
X * Version 2.0 (the 'License'). You may not use this file except in
X * compliance with the License. Please obtain a copy of the License at
X * http://www.opensource.apple.com/apsl/ and read it before using this
X * file.
X *
X * The Original Code and all software distributed under the License are
X * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
X * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
X * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
X * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
X * Please see the License for the specific language governing rights and
X * limitations under the License.
X *
X * @APPLE_LICENSE_HEADER_END@
X */
X/*
X * decomment.c
X *
X * Removes all comments and (optionally) whitespace from an input file.
X * Writes result on stdout.
X */
X/*
X **********************************************************************
X * HISTORY
X * 27-May-04 Cyrille Lefevre at laposte.net
X * Make it compile under FreeBSD. modifications are __FreeBSD__ #ifdef'ed.
X **********************************************************************
X */
X#include <stdio.h>
X#ifdef __FreeBSD__
X#include <ctype.h>
X#include <fcntl.h>
X#include <stdlib.h>
X#include <unistd.h>
X#else
X#include <libc.h>
X#endif
X
X/*
X * State of input scanner.
X */
Xtypedef enum {
X IS_NORMAL,
X IS_SLASH, // encountered opening '/'
X IS_IN_COMMENT, // within / * * / comment
X IS_STAR, // encountered closing '*'
X IS_IN_END_COMMENT // within / / comment
X} input_state_t;
X
Xstatic volatile void usage(char **argv);
X
Xint main(int argc, char **argv)
X{
X int fd;
X char bufchar;
X int bytes_read;
X input_state_t input_state = IS_NORMAL;
X int exit_code = 0;
X int remove_whitespace = 0;
X int arg;
X
X if(argc < 2)
X usage(argv);
X for(arg=2; arg<argc; arg++) {
X switch(argv[arg][0]) {
X case 'r':
X remove_whitespace++;
X break;
X default:
X usage(argv);
X }
X }
X
X fd = open(argv[1], O_RDONLY, 0);
X if(fd <= 0) {
X fprintf(stderr, "Error opening %s\n", argv[1]);
X perror("open");
X exit(1);
X }
X for(;;) {
X bytes_read = read(fd, &bufchar, 1);
X if(bytes_read <= 0) {
X if(bytes_read < 0) {
X fprintf(stderr, "Error reading %s\n", argv[1]);
X perror("read");
X exit_code = 1;
X }
X break;
X }
X
X switch(input_state) {
X
X case IS_NORMAL:
X if(bufchar == '/') {
X /*
X * Might be start of a comment.
X */
X input_state = IS_SLASH;
X }
X else {
X if(!(remove_whitespace && isspace(bufchar))) {
X putchar(bufchar);
X }
X }
X break;
X
X case IS_SLASH:
X switch(bufchar) {
X case '*':
X /*
X * Start of normal comment.
X */
X input_state = IS_IN_COMMENT;
X break;
X
X case '/':
X /*
X * Start of 'to-end-of-line' comment.
X */
X input_state = IS_IN_END_COMMENT;
X break;
X
X default:
X /*
X * Not the start of comment. Emit the '/'
X * we skipped last char in case we were
X * entering a comment this time, then the
X * current char.
X */
X putchar('/');
X if(!(remove_whitespace && isspace(bufchar))) {
X putchar(bufchar);
X }
X input_state = IS_NORMAL;
X break;
X }
X break;
X
X case IS_IN_COMMENT:
X if(bufchar == '*') {
X /*
X * Maybe ending comment...
X */
X input_state = IS_STAR;
X }
X break;
X
X
X case IS_STAR:
X switch(bufchar) {
X case '/':
X /*
X * End of normal comment.
X */
X input_state = IS_NORMAL;
X break;
X
X case '*':
X /*
X * Still could be one char away from end
X * of comment.
X */
X break;
X
X default:
X /*
X * Still inside comment, no end in sight.
X */
X input_state = IS_IN_COMMENT;
X break;
X }
X break;
X
X case IS_IN_END_COMMENT:
X if(bufchar == '\n') {
X /*
X * End of comment. Emit the newline if
X * appropriate.
X */
X if(!remove_whitespace) {
X putchar(bufchar);
X }
X input_state = IS_NORMAL;
X }
X break;
X
X } /* switch input_state */
X } /* main read loop */
X
X /*
X * Done.
X */
X return(exit_code);
X}
X
Xstatic volatile void usage(char **argv)
X{
X printf("usage: %s infile [r(emove whitespace)]\n", argv[0]);
X exit(1);
X}
END-of-usr.bin/decomment/decomment.c
exit
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the freebsd-bugs
mailing list