Shell / sed Program To Remove All C and C++ Comments From Program File

in Categories Academic, Script last updated May 6, 2008

Write the shell / sed program which remove all the comments from a simple C program stored in your current directory. You can assume that the c source code contains only syntactically correct comments:
+ start with // and end with a newline

+ starting with /* and ending */ (can be multiline)

+ nesting of comments is not allowed

Make sure that C source is not changed at all.

How do I use this sed script?

$ ./script.sed < input.c $ ./script.sed < input.c > output.c
$ for c in *.c; do script.sed < $c > /tmp/zyzcc.c; /bin/cp -f /tmp/zyzcc.c $c; done

Sample sed code to remove all comments from sed

#! /bin/sed -nf
 
# Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com)
 
# Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org)
# Works its way through the line, copying to hold space the text up to the
# first special character (/, ", ').  The original version went exactly a
# character at a time, hence the greater speed of this one.  But the concept
# and especially the trick of building the line in hold space are entirely
# merit of Brian.
 
:loop
 
# This line is sufficient to remove C++ comments!
/^\/\// s,.*,,
 
/^$/{
  x
  p
  n
  b loop
}
/^"/{
  :double
  /^$/{
    x
    p
    n
    /^"/b break
    b double
  }
 
  H
  x
  s,\n\(.[^\"]*\).*,\1,
  x
  s,.[^\"]*,,
 
  /^"/b break
  /^\\/{
    H
    x
    s,\n\(.\).*,\1,
    x
    s/.//
  }
  b double
}
 
/^'/{
  :single
  /^$/{
    x
    p
    n
    /^'/b break
    b single
  }
  H
  x
  s,\n\(.[^\']*\).*,\1,
  x
  s,.[^\']*,,
 
  /^'/b break
  /^\\/{
    H
    x
    s,\n\(.\).*,\1,
    x
    s/.//
  }
  b single
}
 
/^\/\*/{
  s/.//
  :ccom
  s,^.[^*]*,,
  /^$/ n
  /^\*\//{
    s/..//
    b loop
  }
  b ccom
}
 
:break
H
x
s,\n\(.[^"'/]*\).*,\1,
x
s/.[^"'/]*//
b loop

Please note that this program is copied from public domain.

Shell Script To Read IP Address ( Find Ip Address Script )

in Categories Script last updated April 10, 2008
#!/bin/sh
# Shell script scripts to read ip address
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Get OS name
OS=`uname`
IO="" # store IP
case $OS in
   Linux) IP=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;;
   FreeBSD|OpenBSD) IP=`ifconfig  | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` ;;
   SunOS) IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;;
   *) IP="Unknown";;
esac
echo "$IP"

Shell Script to stop DLink PCI wireless lan card DWL 520 (Debian Linux)

in Categories Script last updated April 10, 2008
#!/bin/sh
# Script to stop DLink PCL wireless lan card DWL 520
# ----------------------------------------------------------------
# Copyright (c) 2006 nixCraft <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# See url for more info:
# https://www.cyberciti.biz/tips/linux-install-and-configure-dlink-dwl-g-520-wireless-lan-pci-card.html
# -------------------------------------------------------------------------
 
# unload driver (DLink 520/rt61)
rmmod rt61
 
#clear firwall
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
 
# append your other stuff below

Shell Script to start DLink PCL wireless lan card 520 / 510

in Categories Script last updated April 10, 2008
#!/bin/sh
# Script to start DLink PCL wireless lan card 520 
# ----------------------------------------------------------------
# Copyright (c) 2006 nixCraft <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# See url for more info:
# https://www.cyberciti.biz/tips/linux-install-and-configure-dlink-dwl-g-520-wireless-lan-pci-card.html
# -------------------------------------------------------------------------
 
# assing values 
# use 'iwlist ra0 scanning' command to get values
SSID=nixcraft
TYPE=Managed
CHANNEL=6
KEY="your-key"
INET=ra0
IWC="/sbin/iwconfig"
 
# ok config ra0
$IWC $INET essid $SSID
$IWC $INET mode $TYPE
$IWC $INET channel $CHANNEL
$IWC $INET key $KEY
 
# firewall 
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
modprobe ip_conntrack
modprobe ip_conntrack_ftp
 
# Setting default filter policy
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
 
# Unlimited access to loop back
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
 
# Allow UDP, DNS and Passive FTP
iptables -A INPUT -i $INET -m state --state ESTABLISHED,RELATED -j ACCEPT
 
#allow bittorent incomming client request :)
iptables -A INPUT -p tcp --destination-port 6881:6882 -j ACCEPT
 
#Uncomment below to allow sshd incoming client request
#iptables -A INPUT -p tcp --destination-port 22 -j ACCEPT
 
# DROP everything and Log it
iptables -A INPUT -j LOG
iptables -A INPUT -j DROP