Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

jvscode [[at]] fastmail [[dot]] fm

Compile & install cdecl on Mac OS X


# 1. readline

# first compile and install the latest version of readline (into /usr/local/lib)
open http://tiswww.case.edu/php/chet/readline/rltop.html

# let's make sure we use standard system libs & files
sudo mv -i /opt/local /opt/local-off
sudo mv -i /usr/local /usr/local-off

#sudo mv -i /opt/local-off /opt/local
#sudo mv -i /usr/local-off /usr/local


cd ~/Desktop

fwftp   # open ipfw firewall for ftp (cf. http://codesnippets.joyent.com/posts/show/1284)

curl -L -O ftp://ftp.cwru.edu/pub/bash/readline-6.1.tar.gz

fwdef   # restore default ipfw rules

tar -xzf readline-6.1.tar.gz

cd readline-6.1

./configure

make

sudo mv -i /usr/local-off /usr/local

sudo make install

sudo mv -i /opt/local-off /opt/local



find ~/Desktop/readline-6.1 -name "*dylib"
ls -1 /usr/local/lib/*readline*

[[ -d /usr/local ]] && sudo mkdir -p /usr/local/src || echo 'mkdir failed!'

sudo cp ~/Desktop/readline-6.1.tar.gz /usr/local/src

tar -C /usr/local/src -xzf ~/Desktop/readline-6.1.tar.gz

find /usr/local/src/readline-6.1 -name "*compat.c"

otool -L /usr/local/lib/*readline*6*



# 2. cdecl

# compile & install cdecl (into /usr/local/bin; with readline support)

open http://www.cdecl.org

cd ~/Desktop

curl -L -O http://cdecl.org/files/cdecl-blocks-2.5.tar.gz

tar -xzf cdecl-blocks-2.5.tar.gz

cd cdecl-blocks-2.5


# edit Makefile

sed -i "" -E \
   -e 's/^( *CFLAGS *=.+)/\1 -Wall -Ddodebug -Ddoyydebug -DUSE_READLINE/' \
   -e 's|^ *LIBS *=.+|LIBS= -L/usr/local/lib -lreadline -L/usr/lib -lncurses|' \
   -e 's|^ *BINDIR *=.+|BINDIR= /usr/local/bin|' \
   -e 's|^ *MANDIR *=.+|MANDIR= /usr/local/share/man/man1|' \
   -e 's|^ *CATDIR *=.+|CATDIR = /usr/local/share/man/cat1|' \
   Makefile


# edit cdgram.y

cat <<-'EOF' | sed -e 's/^ *//' -e 's/ *$//' | /bin/ed -s cdgram.y
   H
   1i

   %{
   int yylex(void);
   %}

   .
   wq
EOF



# edit cdecl.c

# replace "Function" by "rl_compentry_func_t"
# while (...) -> while ((...))
# add missing parentheses: if ... { ... }

sed -i "" -E \
   -e 's/^( *rl_completion_entry_function *= *\()( *Function *)(\* *\)keyword_completion;)/\1 rl_compentry_func_t \3/' \
   -e 's/while +\(command += +commands\[index\]\) +{/while ((command = commands[index])) {/' \
   -e 's/while +\(option += +options\[index\]\) +{/while ((option = options[index])) {/' \
   -e 's/} +else +while +\(keyword += +keywords\[index\]\) +{/} else while ((keyword = keywords[index])) {/' \
   -e 's/^([[:space:]]+if \(type \&\& \(strcmp\(type, "void"\) == 0\)\))/\1 {/' \
   -e 's/^([[:space:]]+if \(strcmp\(type, "void"\) == 0\))/\1 {/' \
   -e 's/^([[:space:]]+"pointer to void"\);)/\1 }/' \
   cdecl.c



# add some include statements to cdecl.c

cat <<-'EOF' | sed -e 's/^ *//' -e 's/ *$//' | /bin/ed -s cdecl.c
   H
   /^[[:space:]]\{0,\}#[[:space:]]\{0,\}include[[:space:]]\{0,\}<readline\/readline.h>/a

   #include </usr/local/src/readline-6.1/compat.c>
   #include </usr/local/include/readline/readline.h>
   #include </usr/local/include/readline/history.h>
   #include <unistd.h>
   //#include </usr/local/include/readline/rltypedefs.h>
   int dotmpfile_from_string(char *s);

   .
   /^[[:space:]]\{0,\}#[[:space:]]\{0,\}include[[:space:]]\{0,\}<readline\/readline.h>/d
   wq
EOF



# get rid of warning: missing braces around initializer (in cdecl.c)

cat <<-'EOF' | sed -e 's/^ *//' -e 's/ *$//' | /bin/ed -s cdecl.c
   H
   /^[[:space:]]\{0,\}char crosscheck\[9\]\[9\] = {/i

   char crossXYZcheck[9][9] = {
    /*                  L, I, S, C, V, U, S, F, D, */
    /* long */          {_, _, _, _, _, _, _, _, _},
    /* int */           {_, _, _, _, _, _, _, _, _},
    /* short */         {X, _, _, _, _, _, _, _, _},
    /* char */          {X, X, X, _, _, _, _, _, _},
    /* void */          {X, X, X, X, _, _, _, _, _},
    /* unsigned */      {R, _, R, R, X, _, _, _, _},
    /* signed */        {P, P, P, P, X, X, _, _, _},
    /* float */         {A, X, X, X, X, X, X, _, _},
    /* double */        {P, X, X, X, X, X, X, X, _}
   };

   .
   /^[[:space:]]\{0,\}char crosscheck\[9\]\[9\] = {/,+12d
   g/crossXYZcheck/s//crosscheck/
   wq
EOF



# open -e cdecl.c Makefile


make

make test

echo $?

sudo make install



ls -l "$(type -P cdecl)"
otool -L "$(type -P cdecl)"
type -a c++decl cdecl
ls -l $(type -P c++decl cdecl)

man cdecl
cdecl --help

cdecl
explain int (*(*foo)(void ))[3]
declare bar as volatile pointer to array 64 of const int
cast foo into block(int, long long) returning double
explain char ** const * const x
declare x as const pointer to const pointer to pointer to char
[ctrl-c]

Remove duplicates in PATH


# remove duplicates in PATH, but keep the order
# e.g. in ~/.bash_login at the very end

PATH="$(printf "%s" "${PATH}" | /usr/bin/awk -v RS=: -v ORS=: '!($0 in a) {a[$0]; print}')"

# cf. http://chunchung.blogspot.com/2007/11/remove-duplicate-paths-from-path-in.html
#PATH="$(printf "%s" "${PATH}" | /usr/bin/awk -F: '{for(i=1;i<=NF;i++){if(!($i in a)){a[$i];printf s$i;s=":"}}}')"

PATH="${PATH%:}"    # remove trailing colon

export PATH

xmlstarlet example 5


open http://aplawrence.com/rss/index.html

curl -s http://feeds2.feedburner.com/SiteNewsForAplawrenceUnixLinuxAndMacOsXResources | \
    xmlstarlet fo > aplawrence.xml

less aplawrence.xml

xmlstarlet val aplawrence.xml

xmlstarlet el -a aplawrence.xml | less

xmlstarlet el -v aplawrence.xml | less


# def=... is default namespace
xmlstarlet sel \
   -N def="http://purl.org/rss/1.0/" \
   -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \
   -t -m "//rdf:RDF" -v '.' \
   aplawrence.xml | less

xmlstarlet sel \
   -N def="http://purl.org/rss/1.0/" \
   -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \
   -t -m "//rdf:RDF" -v 'def:channel' \
   aplawrence.xml | less

# same
xmlstarlet sel \
   -N def="http://purl.org/rss/1.0/" \
   -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \
   -t -m "//rdf:RDF/def:channel" -n \
   -v '../def:channel' -n \
   aplawrence.xml | less

xmlstarlet sel \
   -N def="http://purl.org/rss/1.0/" \
   -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \
   -t -m "//rdf:RDF/def:channel/def:title" -v '.' \
   aplawrence.xml


xmlstarlet sel \
   -N def="http://purl.org/rss/1.0/" \
   -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \
   -N sy="http://purl.org/rss/1.0/modules/syndication/" \
   -N dc="http://purl.org/dc/elements/1.1/" \
   -t \
   -m "//rdf:RDF/def:channel" -n \
   -v '@rdf:about' -n -n \
   -v 'def:title' -n \
   -v 'def:description' -n \
   -v 'sy:updatePeriod' -n \
   -v 'sy:updateFrequency' -n \
   -v 'sy:updateBase' -n \
   -v 'dc:rights' -n \
   -v 'dc:creator' -n \
   -v 'dc:date' -n \
   -v 'def:image/@rdf:resource' -n -n \
   -v 'def:items/rdf:Seq/rdf:li[1]/@rdf:resource' -n \
   -v 'def:items/rdf:Seq/rdf:li[2]/@rdf:resource' -n \
   -v 'def:items/rdf:Seq/rdf:li[3]/@rdf:resource' -n -n  \
   -m './def:items/rdf:Seq/rdf:li/@rdf:resource' -v '.' -n \
   aplawrence.xml


xmlstarlet sel \
   -N def="http://purl.org/rss/1.0/" \
   -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \
   -N sy="http://purl.org/rss/1.0/modules/syndication/" \
   -N dc="http://purl.org/dc/elements/1.1/" \
   -t \
   -m "//rdf:RDF/def:item" -n \
   -v 'def:title' -n \
   -v '@rdf:about' -n \
   aplawrence.xml



# delete the matching line plus one further line with sed
# cf. http://codesnippets.joyent.com/posts/show/2043

printf "a word\nand yet another word\n" | /usr/bin/sed '/word$/{N;d;}'


xmlstarlet sel \
   -T \
   -N def="http://purl.org/rss/1.0/" \
   -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \
   -N sy="http://purl.org/rss/1.0/modules/syndication/" \
   -N dc="http://purl.org/dc/elements/1.1/" \
   -t \
   -m "//rdf:RDF/def:item" -n \
   -o "Title:  " -v 'def:title' -n \
   -o "URL:  " -v '@rdf:about' -n \
   -v 'def:description' -n \
   aplawrence.xml | \
   sed -E -e 's/^((Title|URL): +.*)/<p>\1<\p>/g' | \
   html2text -nobs -style pretty -width 120 | \
   sed -E \
   -e :a -e '/Advertise_Here$/N; s/\\\n//; ta' \
   -e '/Advertise_Here/d' \
   -e $'s/^(Title: +.*)/\\\n\\\n\033[31m\\1\033[m/g' \
   -e $'s/^(URL: +.*)/\033[34m\\1\033[m\\\n/g' | \
   less -r



#----------------------------------------------------------



curl -s http://feeds2.feedburner.com/Aplawrencecomments | xmlstarlet fo > aplawrence.xml

xmlstarlet el -a aplawrence.xml | less

# best of best II
xmlstarlet sel \
   -T \
   -N def="http://purl.org/rss/1.0/" \
   -N rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" \
   -N sy="http://purl.org/rss/1.0/modules/syndication/" \
   -N dc="http://purl.org/dc/elements/1.1/" \
   -t \
   -m "//rdf:RDF/def:item" -n \
   -o "Title:  " -v 'def:title' -n \
   -o "URL:  " -v 'def:link' -n \
   -v 'def:description' -n \
   aplawrence.xml | \
   sed -E -e 's/^((Title|URL): +.*)/<p>\1<\p>/g' | \
   html2text -nobs -style pretty -width 120 | \
   sed -E \
   -e '/Advertise_Here/{N;d;}' \
   -e $'s/^(Title: +.*)/\\\n\\\n\033[31m\\1\033[m/g' \
   -e $'s/^(URL: +.*)/\033[34m\\1\033[m\\\n/g' | \
   less -r



first lines of aplawrence.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss1full.xsl"?>
<?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" 
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http:/
/webns.net/mvcb/" xmlns="http://purl.org/rss/1.0/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  <channel rdf:about="http://aplawrence.com/rss.rdf">
    <title>Main  News at  A.P.Lawrence</title>
    <link>http://aplawrence.com/</link>
    <description>
Main  feed at aplawrence.com: Thousands of articles, reviews, consultants listings, skills tests, opinion, how-to's for Unix, Linux and Mac OS X, networking, web site maintenance and m
ore.. 
</description>
...

Inspect HTTP traffic with tshark


# tshark is the console version of wireshark
# cf. http://audun.ytterdal.net/2009/03/tshark-to-the-rescue

# port info wireshark
# sudo port install wireshark
# port contents wireshark | grep /opt/local/bin

tshark -i eth0 -n -aduration:60 -zhttp,tree -zhttp_srv,tree \
   -T fields -e http.host -e http.request.uri -e http.request.method \
   -R http -tad 'src host 10.0.0.144 and (dst port 80 or dst port 443)'


tshark -i eth0 -aduration:60 -d tcp.port==3306,mysql -T fields -e mysql.query 'port 3306'

xmlstarlet example 3


# cf. http://thebogles.com/blog/2009/08/xml_grep-cookbook/


# print all events

xmlstarlet sel -t -m '//Events/Event' -n \
   -v 'EventDate' -n \
   -v 'PerformanceName' -n \
   -v "@ID" -n \
   foo.xml


# print all events for a given artist

#xmlstarlet sel -C -t \
xmlstarlet sel -t -n \
  -m "//Events/Event/ArtistIDs" \
   -i "ArtistID[@ID='806533']/@ID" \
   -v '../EventDate' -n \
   -v '../PerformanceName' -n -n \
   foo.xml


# print all events for given artists

xmlstarlet sel -t -n \
   -m "//Events/Event/ArtistIDs" \
   -i "ArtistID[@ID='113468888' or @ID='1134688880']/@ID" \
   -v '../EventDate' -n \
   -v '../PerformanceName' -n -n \
   foo.xml


# print all event IDs on a given date

xmlstarlet sel -t -n \
   -m "//Events/Event" -i "EventDate[text()='2010-12-17']" -n \
   -v "@ID" -n \
   foo.xml


# extract the time and performance name of events on a given date

xmlstarlet sel -t -n \
   -m "//Events/Event" -i "EventDate[text()='2010-12-17']" -n \
   -v "EventDate" -n \
   -v "PerformanceName" -n \
   foo.xml


# match a regular expression

xmlstarlet sel -t -n -m '//Events/Event' -n \
   -o 'PerformanceName:  ' -v 'PerformanceName' -n \
   -o 'EventDate:  ' -v 'EventDate' -n \
   -o 'EventID:  ' -v '@ID' -n \
   -o 'Primary ArtistID:  ' -v "ArtistIDs/ArtistID[@Type='Primary']/@ID" -n \
   -o 'Secondary ArtistID:  ' -v "ArtistIDs/ArtistID[@Type='Secondary']/@ID" -n \
foo.xml | egrep -A 3 -B 1 'EventDate: *2009-07-25'



# print a matching paragraph using sed
# cf. http://sed.sourceforge.net/sed1line.txt
xmlstarlet sel -t -n -m '//Events/Event' -n \
   -o 'PerformanceName:  ' -v 'PerformanceName' -n \
   -o 'EventDate:  ' -v 'EventDate' -n \
   -o 'EventID:  ' -v '@ID' -n \
   -o 'Primary ArtistID:  ' -v "ArtistIDs/ArtistID[@Type='Primary']/@ID" -n \
   -o 'Secondary ArtistID:  ' -v "ArtistIDs/ArtistID[@Type='Secondary']/@ID" -n \
foo.xml | sed -e '/./{H;$!d;}' -e 'x;/20.0-..-../!d;'


# print a matching paragraph using awk
# cf. http://www.pement.org/awk/paragrep.awk.txt
xmlstarlet sel -t -n -m '//Events/Event' -n \
   -o 'PerformanceName:  ' -v 'PerformanceName' -n \
   -o 'EventDate:  ' -v 'EventDate' -n \
   -o 'EventID:  ' -v '@ID' -n \
   -o 'Primary ArtistID:  ' -v "ArtistIDs/ArtistID[@Type='Primary']/@ID" -n \
   -o 'Secondary ArtistID:  ' -v "ArtistIDs/ArtistID[@Type='Secondary']/@ID" -n \
foo.xml | /usr/bin/awk 'BEGIN{RS="" } /20.0-..-../{print "\n"$0"\n"}'
#foo.xml | /usr/bin/awk 'BEGIN{RS=""; ORS="\n\n" } /20..-..-../{print $0}'
#foo.xml | /usr/bin/awk 'BEGIN{RS="" } /20.9-..-../'



foo.xml:

<?xml version="1.0" ?>                                                                                                                     
<Events>                                                                                                                                           
  <Event ID="0E0042D2CF2E9E44i">                                                                                                           
    <ArtistIDs>                                                                                                                           
      <ArtistID ID="806533" Type="Primary"/>                                                                                               
      <ArtistID ID="1134688" Type="Secondary"/>                                                                                           
    </ArtistIDs>
    <EventDate>2009-07-25</EventDate>                                                                                           
    <PerformanceName>Summer Splash 2009</PerformanceName>                                               
  </Event>   
  <Event ID="0E0042D2CF2E9E44ii">                                                                                                           
    <ArtistIDs>                                                                                                                           
      <ArtistID ID="806533" Type="Primary"/>                                                                                               
      <ArtistID ID="113468888" Type="Secondary"/>                                                                                           
    </ArtistIDs>
    <EventDate>2010-12-17</EventDate>                                                                                           
    <PerformanceName>Summer Splash 2010</PerformanceName>                                               
  </Event>  
  <Event ID="0E0042D2CF2E9E44iii">                                                                                                           
    <ArtistIDs>                                                                                                                           
      <ArtistID ID="8065330" Type="Primary"/>                                                                                               
      <ArtistID ID="1134688880" Type="Secondary"/>                                                                                           
    </ArtistIDs>
    <EventDate>2010-12-17</EventDate>                                                                                           
    <PerformanceName>Yet Another Summer Splash 2010</PerformanceName>                                               
  </Event>       
   <!-- etc -->
</Events>


xmlstarlet example 1

# cf. http://discussions.apple.com/thread.jspa?threadID=2294454

cd ~/Desktop

curl --silent 'http://weather.yahooapis.com/forecastrss?p=USVA0023&u=f' > weather-Arlington-VA.xml

xmlstarlet --help         #  cf. port info xmlstarlet

xmlstarlet fo --help

xmlstarlet fo weather-Arlington-VA.xml > weather-Arlington-VA-formatted.xml 
#xmlstarlet fo --nocdata weather-Arlington-VA.xml > weather-Arlington-VA-formatted.xml 

open -e weather-Arlington-VA-formatted.xml

xmlstarlet c14n --help

xmlstarlet c14n --with-comments weather-Arlington-VA-formatted.xml > weather-Arlington-VA-canon.xml; echo $?

open -e weather-Arlington-VA-canon.xml



xmlstarlet val weather-Arlington-VA.xml; echo $?

xmlstarlet el -a weather-Arlington-VA.xml       # display element structure of XML file including attributes

xmlstarlet el -v weather-Arlington-VA.xml       # same, but show attributes and their values


xmlstarlet sel --help

# note: yweather=... could also be any varname=... varname:atmosphere/@humidity ...

xmlstarlet sel -N yweather="http://xml.weather.yahoo.com/ns/rss/1.0" \
-t -m "//rss/channel" -n \
-v 'title' -n \
-o 'humidity: ' -v 'yweather:atmosphere/@humidity' -n \
-o 'visibility: ' -v 'yweather:atmosphere/@visibility' -n \
-o 'pressure: ' -v 'yweather:atmosphere/@pressure' -n \
-o 'rising: ' -v 'yweather:atmosphere/@rising' -n \
weather-Arlington-VA.xml


xmlstarlet sel -N geo="http://www.w3.org/2003/01/geo/wgs84_pos#" \
-N yweather="http://xml.weather.yahoo.com/ns/rss/1.0" \
-t -m "//rss/channel/item" -n \
-o 'title: ' -v 'title' -n \
-o 'pubDate: ' -v "pubDate" -n \
-o 'geo:lat: ' -v "geo:lat" -n \
-o 'geo:long: ' -v "geo:long" -n \
-o 'yweather:condition: ' -v 'yweather:condition/@text' -n \
weather-Arlington-VA.xml


xmlstarlet sel -N yweather="http://xml.weather.yahoo.com/ns/rss/1.0" \
-t -m "//rss/channel/item" -n \
-v 'title' -n \
-v 'yweather:condition/@text' -n \
-v 'yweather:condition/@date' -n \
-v 'yweather:condition/@temp' -n -n \
-v 'yweather:forecast[1]/@day' -n \
-v 'yweather:forecast[1]/@date' -n \
-v 'yweather:forecast[1]/@low' -n \
-v 'yweather:forecast[1]/@high' -n \
-v 'yweather:forecast[1]/@text' -n -n \
-v 'yweather:forecast[2]/@day' -n \
-v 'yweather:forecast[2]/@date' -n \
-v 'yweather:forecast[2]/@low' -n \
-v 'yweather:forecast[2]/@high' -n \
-v 'yweather:forecast[2]/@text' -n \
weather-Arlington-VA.xml


# day today & day tomorrow
date +%a
date -r $(date +%s) +%a
date -r $(( $(date +%s) + 86400 )) +%a

dayToday="$(/bin/date +%a)"
dayTomorrow="$(/bin/date -r $(( $(/bin/date +%s) + 86400 )) +%a)"

xmlstarlet sel -N yweather="http://xml.weather.yahoo.com/ns/rss/1.0" \
-t -m "//rss/channel/item" -n \
-v 'title' -n \
-v 'yweather:condition/@text' -n \
-v 'yweather:condition/@date' -n \
-v 'yweather:condition/@temp' -n -n \
-v "yweather:forecast[@day='${dayToday}']/@day" -n \
-v "yweather:forecast[@day='${dayToday}']/@date" -n \
-v "yweather:forecast[@day='${dayToday}']/@low" -n \
-v "yweather:forecast[@day='${dayToday}']/@high" -n \
-v "yweather:forecast[@day='${dayToday}']/@text" -n -n \
-v "yweather:forecast[@day='${dayTomorrow}']/@day" -n \
-v "yweather:forecast[@day='${dayTomorrow}']/@date" -n \
-v "yweather:forecast[@day='${dayTomorrow}']/@low" -n \
-v "yweather:forecast[@day='${dayTomorrow}']/@high" -n \
-v "yweather:forecast[@day='${dayTomorrow}']/@text" -n \
weather-Arlington-VA.xml



# see http://discussions.apple.com/thread.jspa?threadID=2294454 for a Perl version

/bin/sh

# xmlstarlet version

curl --silent "http://weather.yahooapis.com/forecastrss?p=USVA0023&u=f" | \
xmlstarlet sel -N yweather="http://xml.weather.yahoo.com/ns/rss/1.0" \
-t -m "//rss/channel/item" -n \
-o 'Now | ' \
-v 'yweather:condition/@text' \
-o ',  ' \
-v 'yweather:condition/@temp' \
-o $'\302\260F' \
-n -n \
-o 'Tomorrow | ' \
-v 'yweather:forecast[2]/@text' \
-o ',  ' \
-v 'yweather:forecast[2]/@high' \
-o '|' \
-v 'yweather:forecast[2]/@low' \
-o $'\302\260F' -n \
| sed -E -e $'/Now/s/(.+)/\033[31m\\1\033[m/' -e $'/Tomorrow/s/(.+)/\033[32m\\1\033[m/'



# sed version

curl --silent "http://weather.yahooapis.com/forecastrss?p=USVA0023&u=f" | \
sed -n '/Current Conditions:/{n;{N;N;N;p;};}' | sed -E \
-e 2,3d \
-e 's/<.+$//' \
-e 's/^.+ - +(.+)/Tomorrow  |  \1/' \
-e '1,1s/(.+)/Now  |  \1/' \
-e $'s/([[:digit:]]) *(F( |$))/\\1\302\260\\2/g' \
-e $'s/([[:digit:]]+) +Low: +([[:digit:]]+)/\\1|\\2\302\260F/' \
-e 's/High: //' \
-e 's/\./,/' \
-e $'/Now/s/(.+)/\\\n\033[31m\\1\033[m\\\n/' \
-e $'/Tomorrow/s/(.+)/\033[32m\\1\033[m\\\n/'



weather-Arlington-VA-formatted.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
  <channel>
    <title>Yahoo! Weather - Arlington, VA</title>
    <link>http://us.rd.yahoo.com/dailynews/rss/weather/Arlington__VA/*http://weather.yahoo.com/forecast/USVA0023_f.html</link>
    <description>Yahoo! Weather for Arlington, VA</description>
    <language>en-us</language>
    <lastBuildDate>Thu, 14 Jan 2010 11:52 am EST</lastBuildDate>
    <ttl>60</ttl>
    <yweather:location city="Arlington" region="VA" country="US"/>
    <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
    <yweather:wind chill="43" direction="150" speed="3"/>
    <yweather:atmosphere humidity="40" visibility="10" pressure="30.29" rising="2"/>
    <yweather:astronomy sunrise="7:25 am" sunset="5:09 pm"/>
    <image>
      <title>Yahoo! Weather</title>
      <width>142</width>
      <height>18</height>
      <link>http://weather.yahoo.com</link>
      <url>http://l.yimg.com/a/i/us/nws/th/main_142b.gif</url>
    </image>
    <item>
      <title>Conditions for Arlington, VA at 11:52 am EST</title>
      <geo:lat>38.88</geo:lat>
      <geo:long>-77.1</geo:long>
      <link>http://us.rd.yahoo.com/dailynews/rss/weather/Arlington__VA/*http://weather.yahoo.com/forecast/USVA0023_f.html</link>
      <pubDate>Thu, 14 Jan 2010 11:52 am EST</pubDate>
      <yweather:condition text="Partly Cloudy" code="30" temp="43" date="Thu, 14 Jan 2010 11:52 am EST"/>
      <description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/30.gif"/><br />
<b>Current Conditions:</b><br />
Partly Cloudy, 43 F<BR />
<BR /><b>Forecast:</b><BR />
Thu - Partly Cloudy. High: 45 Low: 33<br />
Fri - Partly Cloudy. High: 50 Low: 35<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Arlington__VA/*http://weather.yahoo.com/forecast/USVA0023_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]></description>
      <yweather:forecast day="Thu" date="14 Jan 2010" low="33" high="45" text="Partly Cloudy" code="30"/>
      <yweather:forecast day="Fri" date="15 Jan 2010" low="35" high="50" text="Partly Cloudy" code="30"/>
      <guid isPermaLink="false">USVA0023_2010_01_14_11_52_EST</guid>
    </item>
  </channel>
</rss>
<!-- api7.weather.re4.yahoo.com uncompressed/chunked Thu Jan 14 09:55:27 PST 2010 -->


Print inode numbers


open http://en.wikipedia.org/wiki/Inode


man find ls stat

help command help pwd type


type -t pwd
type -a pwd


find "$(pwd -P)" -ls
find "$(pwd -P)" -exec stat -f "%i  --  %N" '{}' \;
find "$(pwd -P)" -exec stat -f "%i  --  %N" '{}' +
find "$(pwd -P)" -exec sh -c 'exec stat -f "%i  --  %N" "$@"' _ '{}' +



# cf. LSCOLORS Generator, http://geoff.greer.fm/lscolors/
# man ls
export CLICOLOR_FORCE=1
#export CLICOLOR=1
#export LSCOLORS=ExGxFxDxCxHxHxCbCeEbEb
#export LSCOLORS=GxFxCxDxBxegedabagacad      
#export LSCOLORS=gxfxcxdxbxegedabagacad     # cyan directories
export LSCOLORS="exfxcxdxbxegedabagacad"    # blue directories


cd /dev
#cd /dev/fd
ls -i
ls -1i
ls -1i "$(pwd -P)"/*
command ls -1i "$(pwd -P)"/*

stat -f "%i  --  %N" "$(pwd -P)"/*
stat -f "%i  --  %N  --  %Y" "$(pwd -P)"/*

stat -f "%i - %r - %v  --  %d  --  %N" /dev/*
stat -f "%i - %Sr - %v  --  %Sd  --  %N" /dev/*


# See also:
# - Why you shouldn't parse the output of ls(1), http://mywiki.wooledge.org/ParsingLs
# - Bash Pitfalls, http://mywiki.wooledge.org/BashPitfalls


SQL injection example (SQLite)

/* 

File name - injattack.c 
Purpose - The main file to show how SQLite can be vulnerable to SQL injection attacks 

URL: http://www.squidoo.com/sqlitehammer,
     (see "What is a SQL Injection Attack") 

Copyright 2008 Jay Godse

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. 



Assumes injattack.db already exists with a table : 
CREATE TABLE names (name string, age string); 
insert into names values ('Jay', 33); 
insert into names values ('Kay', 44); 
insert into names values ('Ray', 55); 
CREATE TABLE dummies(dname string, dage string); 
insert into dummies select * from names; 


// ----------------------------------------------- ADDED  BEGIN


/usr/bin/sqlite3 injattack.db  < <( 
/bin/cat <<-'EOF'
     PRAGMA encoding = "UTF-8";
     PRAGMA auto_vacuum = 1;
     CREATE TABLE names (name string, age string); 
     CREATE TABLE dummies(dname string, dage string); 
EOF
)

/usr/bin/sqlite3 injattack.db  < <( 
cat <<-'EOF'
BEGIN;
insert into names (name,age) values ('Jay', 33); 
insert into names (name,age) values ('Kay', 44); 
insert into names (name,age) values ('Ray', 55); 
insert into dummies (dname,dage) select * from names; 
COMMIT;
.q
EOF
)

sqlite3 injattack.db  ".dump"


gcc injattack.c -lsqlite3 -DSQL_INJECTION_PROOF -o inj 
sqlite3 injattack.db ".dump"; ./inj 101 'Jay'; sqlite3 injattack.db ".dump"
sqlite3 injattack.db ".dump"; ./inj 73 "Jay'; drop table dummies;"; sqlite3 injattack.db ".dump" 
printf "\n\n"; sqlite3 injattack.db ".dump"; printf "\n\n"; ./inj 73 "Jay'; drop table dummies;"; printf "\n\n"; sqlite3 injattack.db ".dump"; printf "\n\n"


gcc injattack.c -lsqlite3 -DSQL_INJECTION_VULNERABLE -o inj 
sqlite3 injattack.db ".dump"; ./inj 101 'Jay'; sqlite3 injattack.db ".dump" 
# successful injection attack: the dummies table is gone
sqlite3 injattack.db ".dump"; ./inj 73 "Jay'; drop table dummies;"; sqlite3 injattack.db ".dump"   

See also: 
SQL Statement Object, http://www.sqlite.org/c3ref/stmt.html


// ----------------------------------------------- ADDED  END


Build the injection proof program using: 
gcc injattack.c -lsqlite3 -DSQL_INJECTION_PROOF -o inj 
To dump the database, run the program, and dump the database do this: 
sqlite3 injattack.db ".dump"; ./inj 101 'Jay';sqlite3 injattack.db ".dump" 
and notice that the dummies table is fine. 
 
Now try it with an injection attack: 
sqlite3 injattack.db ".dump"; ./inj 73 "Jay'; drop table dummies;"; sqlite3 injattack.db ".dump" 
and notice that the dummies table is fine. 
 
Build the injection vulnerable program using: 
gcc injattack.c -lsqlite3 -DSQL_INJECTION_VULNERABLE -o inj 
Run the same dump/run/dump script and notice that the dummies table is gone 

*/


#include <stdio.h>  
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sqlite3.h>

/*#define SQL_INJECTION_PROOF */
/* #define SQL_INJECTION_VULNERABLE */

sqlite3 *db; /* global SQLite database handle */

int main (int argc, char **argv) {
int nrows, ncols, rc, i,j;
char *zErr;
char **startupConfig;
char* sqlinsert="update names set age=? where name=?";
char* end_ptr;
long lAge;

if (argc!=3) {
printf("Usage: injattExample \n");
return 0;
}
printf ("argv[1]=%s, argv[2]=%s\n", argv[1], argv[2]);

errno=0;
lAge=strtol(argv[1], &end_ptr, 0); /* extract the age */

if ((ERANGE==errno) || (end_ptr==argv[1])) {
printf("An integer for the age please!\n");
return 0;
};
printf ("The age is %d\n", (int)lAge);

printf("Opening the database to update name/age into it\n");
rc = sqlite3_open("./injattack.db", &db);

#ifdef SQL_INJECTION_PROOF
sqlite3_stmt *insertstmt;
printf ("Injection proof\n");
rc = sqlite3_prepare(db, sqlinsert, strlen(sqlinsert), &insertstmt, NULL);
/* This is where you use the variable binding instead of formatted strings */
sqlite3_bind_text(insertstmt, 2, argv[2], strlen(argv[2]), SQLITE_STATIC);
sqlite3_bind_int(insertstmt, 1, atoi(argv[1]));

sqlite3_step(insertstmt);
sqlite3_finalize(insertstmt);
#endif

#ifdef SQL_INJECTION_VULNERABLE
printf("Injection vulnerable\n");
char sqlvulnerable[256];
sprintf (sqlvulnerable, "update names set age=%s where name='%s'", argv[1], argv[2]);
printf("SQL to execute is: \n\t\t %s\n", sqlvulnerable);
rc = sqlite3_exec(db,sqlvulnerable, NULL,0, &zErr);
#endif

sqlite3_close(db);
return 0;
}

Enable portarchivemode to create binary archive packages by default


#man macports.conf
#nano ~/.macports/macports.conf

sudo nano /opt/local/etc/macports/macports.conf

# Create and use binary archive packages for installation/reinstallation ease
portarchivemode		yes

# Where to store/retrieve ports binary archive files
portarchivepath		/opt/local/var/macports/packages


# See also: 
#
# - http://guide.macports.org/#using.binaries.archives
#
# - http://guide.macports.org/#using.binaries.binary-packages
#
#   "Binary packages are standalone binary installers that are 
#    precompiled; they do not require MacPorts on the target system."

# create a binary archive package
sudo port -d archive tree

# create binary packages 
sudo port clean --all tree
sudo port -d pkg tree
sudo port -d dmg tree
open -a Finder "$(port dir tree)"
find -x "$(port work tree)" -name "*.pkg"
find -x "$(port work tree)" -type f -name "*.dmg"


tree -C ~/Desktop

Search for strings in Project Gutenberg etexts


#!/opt/local/bin/bash

# cat /usr/local/bin/shp

# adapted from: http://code.cutup.org/sh/shkspr


# cf. Compiling GNU sed 4.1.4 on Mac OS X, 
# http://codesnippets.joyent.com/posts/show/1835

sed=/usr/local/bin/gnused              # ... for case-insensitive matching


[[ -f ~/.shakespeare.txt ]] || {

   /usr/bin/curl -L -o ~/.shakespeare.txt http://www.gutenberg.org/dirs/etext94/shaks12.txt

   # delete all carriage returns
   /usr/bin/sed -i "" -e $'s/\r//g'  ~/.shakespeare.txt

} 


[[ "$1" ]] || {
   echo "use: $(/usr/bin/basename $0) <search string>"
   echo "     $(/usr/bin/basename $0) <search 1> | $(/usr/bin/basename $0) <search 2> ..."
   exit 1
} 


if [[ -t 0 ]]; then
#if [[ ! -s '/dev/stdin' ]]; then

   /usr/bin/awk -v f="$@" '
      BEGIN { RS = "" }
      0 < index(toupper($0),toupper(f)) {
     printf "\n\n\033[32mBEGINNING\033[m\n\n" $0 "\n\n\033[32mEND\033[m\n\n"
   }
   ' ~/.shakespeare.txt 2>/dev/null | $sed -r -e "s/(${@})/$(printf '\033[1m\\1\033[m')/i" 2>/dev/null | /usr/bin/less -r 2>/dev/null

else

   /usr/bin/awk -v f="$@" '
      BEGIN { RS = "" }
      0 < index(toupper($0),toupper(f)) {
      printf "\n\n\033[32mBEGINNING\033[m\n\n" $0 "\n\n\033[32mEND\033[m\n\n"
    }  
   ' /dev/stdin 2>/dev/null | $sed -r -e "s/(${@})/$(printf '\033[1m\\1\033[m')/i" 2>/dev/null | /usr/bin/less -r 2>/dev/null

fi


exit 0