diff --git a/corpus/commands.txt b/corpus/commands.txt index b9e2c36..7de882f 100644 --- a/corpus/commands.txt +++ b/corpus/commands.txt @@ -7,7 +7,7 @@ whoami --- (program - (command (command_name))) + (command (command_name (word)))) =============================== Commands with arguments @@ -19,8 +19,8 @@ git diff --word-diff=color -- file1.txt file2.txt --- (program - (command (command_name) (word)) - (command (command_name) (word) (word) (word) (word) (word))) + (command (command_name (word)) (word)) + (command (command_name (word)) (word) (word) (word) (word) (word))) =============================== Commands with quoted arguments @@ -32,10 +32,10 @@ echo 'hi' --- (program - (command (command_name) (string - (command_substitution (command (command_name))) - (command_substitution (command (command_name))))) - (command (command_name) (raw_string))) + (command (command_name (word)) (string + (command_substitution (command (command_name (word)))) + (command_substitution (command (command_name (word)))))) + (command (command_name (word)) (raw_string))) =============================== Quoted command names @@ -46,7 +46,9 @@ Quoted command names --- (program - (command (string (simple_expansion (variable_name)) (simple_expansion (variable_name))) (word))) + (command + (command_name (string (simple_expansion (variable_name)) (simple_expansion (variable_name)))) + (word))) =============================== Commands with numeric arguments @@ -57,7 +59,7 @@ exit 1 --- (program - (command (command_name) (word))) + (command (command_name (word)) (word))) =================================== Commands with environment variables @@ -71,11 +73,11 @@ VAR1=a VAR2="ok" git diff --word-diff=color (program (command (environment_variable_assignment (variable_name) (word)) - (command_name)) + (command_name (word))) (command (environment_variable_assignment (variable_name) (word)) (environment_variable_assignment (variable_name) (string)) - (command_name) + (command_name (word)) (word) (word))) @@ -90,7 +92,7 @@ VAR2= echo (program (environment_variable_assignment (variable_name)) - (command (environment_variable_assignment (variable_name)) (command_name))) + (command (environment_variable_assignment (variable_name)) (command_name (word)))) =================================== Pipelines @@ -103,11 +105,11 @@ cat foo | grep -v bar (program (pipeline - (command (command_name)) - (command (command_name))) + (command (command_name (word))) + (command (command_name (word)))) (pipeline - (command (command_name) (word)) - (command (command_name) (word) (word)))) + (command (command_name (word)) (word)) + (command (command_name (word)) (word) (word)))) =================================== Lists @@ -121,13 +123,13 @@ a | b && c && d; d e f || e g (list (list (pipeline - (command (command_name)) - (command (command_name))) - (command (command_name))) - (command (command_name))) + (command (command_name (word))) + (command (command_name (word)))) + (command (command_name (word)))) + (command (command_name (word)))) (list - (command (command_name) (word) (word)) - (command (command_name) (word)))) + (command (command_name (word)) (word) (word)) + (command (command_name (word)) (word)))) =============================== File redirects @@ -141,16 +143,16 @@ cat a b > /dev/null (program (command - (command_name) + (command_name (word)) (file_redirect (word))) (command - (command_name) + (command_name (word)) (word) (word) (file_redirect (word))) (command (file_redirect (file_descriptor) (word)) - (command_name))) + (command_name (word)))) =============================== Heredoc redirects @@ -168,10 +170,10 @@ JS (program (command - (command_name) + (command_name (word)) (heredoc_redirect (heredoc))) (command - (command_name) + (command_name (word)) (word) (heredoc_redirect (heredoc)))) @@ -189,8 +191,8 @@ exit (program (command - (command_name) + (command_name (word)) (heredoc_redirect (heredoc (simple_expansion (variable_name)) (expansion (variable_name))))) - (command (command_name))) + (command (command_name (word)))) diff --git a/corpus/control_flow.txt b/corpus/control_flow.txt index a892889..94c2cd7 100644 --- a/corpus/control_flow.txt +++ b/corpus/control_flow.txt @@ -11,16 +11,16 @@ done (program (while_statement - (command (command_name) (word)) + (command (command_name (word)) (word)) (do_group - (command (command_name) (word)) - (command (command_name) (word))))) + (command (command_name (word)) (word)) + (command (command_name (word)) (word))))) ==================================== For statements ==================================== -for a in $(seq 1 10); do +for a in 1 2 $(seq 5 10); do echo $a done @@ -29,9 +29,11 @@ done (program (for_statement (word) - (command (command_substitution (command (command_name) (word) (word)))) + (word) + (word) + (command_substitution (command (command_name (word)) (word) (word))) (do_group - (command (command_name) (simple_expansion (variable_name)))))) + (command (command_name (word)) (simple_expansion (variable_name)))))) ==================================== If statements @@ -50,16 +52,16 @@ fi (program (if_statement (pipeline - (command (command_name) (word)) - (command (command_name) (word) (word))) - (command (command_name) (word)) + (command (command_name (word)) (word)) + (command (command_name (word)) (word) (word))) + (command (command_name (word)) (word)) (elif_clause (pipeline - (command (command_name) (word)) - (command (command_name) (word) (word))) - (command (command_name) (word))) + (command (command_name (word)) (word)) + (command (command_name (word)) (word) (word))) + (command (command_name (word)) (word))) (else_clause - (command (command_name))))) + (command (command_name (word)))))) ==================================== If statements with conditional expressions @@ -74,10 +76,10 @@ fi (program (if_statement (bracket_command - (string (command_substitution (command (command_name)))) + (string (command_substitution (command (command_name (word))))) (word) (raw_string)) - (command (command_name) (word)))) + (command (command_name (word)) (word)))) ==================================== Case statements @@ -98,9 +100,9 @@ esac (program (case_statement (string) (case_item (word) - (command (command_name) (word))) + (command (command_name (word)) (word))) (case_item (word) - (command (command_name) (word))))) + (command (command_name (word)) (word))))) =============================== Subshells @@ -113,7 +115,7 @@ Subshells --- (program - (subshell (command (command_name) (word)))) + (subshell (command (command_name (word)) (word)))) =============================== Function definitions @@ -130,5 +132,5 @@ function do_something_else() { --- (program - (function_definition (command_name) (compound_statement (command (command_name) (word)))) - (function_definition (command_name) (compound_statement (command (command_name) (word))))) + (function_definition (word) (compound_statement (command (command_name (word)) (word)))) + (function_definition (word) (compound_statement (command (command_name (word)) (word))))) diff --git a/corpus/expressions.txt b/corpus/expressions.txt index 0fc3c9c..c4ec01b 100644 --- a/corpus/expressions.txt +++ b/corpus/expressions.txt @@ -8,8 +8,8 @@ echo a b --- (program - (command (command_name) (word)) - (command (command_name) (word) (word))) + (command (command_name (word)) (word)) + (command (command_name (word)) (word) (word))) ============================= Simple variable expansions @@ -20,7 +20,7 @@ echo $abc --- (program - (command (command_name) (simple_expansion (variable_name)))) + (command (command_name (word)) (simple_expansion (variable_name)))) ============================= Variable expansions @@ -32,8 +32,8 @@ echo ${abc:-def} --- (program - (command (command_name) (expansion (variable_name))) - (command (command_name) (expansion (variable_name) (word)))) + (command (command_name (word)) (expansion (variable_name))) + (command (command_name (word)) (expansion (variable_name) (word)))) =================================== Other variable expansion operators @@ -45,7 +45,7 @@ cat ${BAR} ${ABC=def} ${GHI:?jkl} (program (command - (command_name) + (command_name (word)) (expansion (variable_name)) (expansion (variable_name) (word)) (expansion (variable_name) (word)))) @@ -61,14 +61,14 @@ echo $(echo $(echo hi)) (program (command - (command_name) - (command_substitution (command (command_name) (word)))) + (command_name (word)) + (command_substitution (command (command_name (word)) (word)))) (command - (command_name) + (command_name (word)) (command_substitution (command - (command_name) + (command_name (word)) (command_substitution (command - (command_name) + (command_name (word)) (word))))))) ============================= @@ -82,16 +82,16 @@ echo abc > >(wc -c) (program (command - (command_name) + (command_name (word)) (word) (process_substitution (list - (command (command_name) (word)) - (command (command_name) (word))))) + (command (command_name (word)) (word)) + (command (command_name (word)) (word))))) (command - (command_name) + (command_name (word)) (word) (file_redirect (process_substitution - (command (command_name) (word)))))) + (command (command_name (word)) (word)))))) ============================= Single quoted strings @@ -102,7 +102,7 @@ echo 'a b' 'c d' --- (program - (command (command_name) (raw_string) (raw_string))) + (command (command_name (word)) (raw_string) (raw_string))) ============================= Double quoted strings @@ -114,10 +114,10 @@ echo "a ${b} c" "d $e" --- (program - (command (command_name) + (command (command_name (word)) (string) (string)) - (command (command_name) + (command (command_name (word)) (string (expansion (variable_name))) (string (simple_expansion (variable_name))))) @@ -131,11 +131,12 @@ find "`dirname $file`" -name "$base"'*' (program (command - (command_name) - (string (command_substitution (command (command_name) (simple_expansion (variable_name))))) + (command_name (word)) + (string (command_substitution (command (command_name (word)) (simple_expansion (variable_name))))) (word) - (string (simple_expansion (variable_name))) - (raw_string))) + (concatenation + (string (simple_expansion (variable_name))) + (raw_string)))) ========================================= Arrays and array expansions @@ -147,10 +148,15 @@ b=(1 2 3) echo ${a[@]} echo ${#b[@]} +a[$i]=50 + --- (program (environment_variable_assignment (variable_name) (array)) (environment_variable_assignment (variable_name) (array (word) (word) (word))) - (command (command_name) (expansion (variable_name))) - (command (command_name) (expansion (variable_name)))) + (command (command_name (word)) (expansion (variable_name))) + (command (command_name (word)) (expansion (variable_name))) + (environment_variable_assignment + (subscript (variable_name) (simple_expansion (variable_name))) + (word))) diff --git a/corpus/programs.txt b/corpus/programs.txt index d764c1e..781b4a7 100644 --- a/corpus/programs.txt +++ b/corpus/programs.txt @@ -28,7 +28,7 @@ f=g \ (program (command - (command_name) + (command_name (word)) (word) (word)) (command @@ -38,5 +38,5 @@ f=g \ (environment_variable_assignment (variable_name) (word)) - (command_name) + (command_name (word)) (word))) diff --git a/examples/clean-old.sh b/examples/clean-old.sh new file mode 100755 index 0000000..cda80f2 --- /dev/null +++ b/examples/clean-old.sh @@ -0,0 +1,165 @@ +#!/bin/bash + +# look for old 0.x cruft, and get rid of it. +# Should already be sitting in the npm folder. + +# This doesn't have to be quite as cross-platform as install.sh. +# There are some bash-isms, because maintaining *two* +# fully-portable posix/bourne sh scripts is too much for +# one project with a sane maintainer. + +# If readlink isn't available, then this is just too tricky. +# However, greadlink is fine, so Solaris can join the party, too. +readlink="readlink" +which $readlink >/dev/null 2>/dev/null +if [ $? -ne 0 ]; then + readlink="greadlink" + which $readlink >/dev/null 2>/dev/null + if [ $? -ne 0 ]; then + echo "Can't find the readlink or greadlink command. Aborting." + exit 1 + fi +fi + +if [ "x$npm_config_prefix" != "x" ]; then + PREFIXES=$npm_config_prefix +else + node="$NODE" + if [ "x$node" = "x" ]; then + node=`which node` + fi + if [ "x$node" = "x" ]; then + echo "Can't find node to determine prefix. Aborting." + exit 1 + fi + + + PREFIX=`dirname $node` + PREFIX=`dirname $PREFIX` + echo "cleanup prefix=$PREFIX" + PREFIXES=$PREFIX + + altprefix=`"$node" -e process.installPrefix` + if [ "x$altprefix" != "x" ] && [ "x$altprefix" != "x$PREFIX" ]; then + echo "altprefix=$altprefix" + PREFIXES="$PREFIX $altprefix" + fi +fi + +# now prefix is where npm would be rooted by default +# go hunting. + +packages= +for prefix in $PREFIXES; do + packages="$packages + "`ls "$prefix"/lib/node/.npm 2>/dev/null | grep -v .cache` +done + +packages=`echo $packages` + +filelist=() +fid=0 + +for prefix in $PREFIXES; do + # remove any links into the .npm dir, or links to + # version-named shims/symlinks. + for folder in share/man bin lib/node; do + find $prefix/$folder -type l | while read file; do + target=`$readlink $file | grep '/\.npm/'` + if [ "x$target" != "x" ]; then + # found one! + filelist[$fid]="$file" + let 'fid++' + # also remove any symlinks to this file. + base=`basename "$file"` + base=`echo "$base" | awk -F@ '{print $1}'` + if [ "x$base" != "x" ]; then + find "`dirname $file`" -type l -name "$base"'*' \ + | while read l; do + target=`$readlink "$l" | grep "$base"` + if [ "x$target" != "x" ]; then + filelist[$fid]="$1" + let 'fid++' + fi + done + fi + fi + done + + # Scour for shim files. These are relics of 0.2 npm installs. + # note: grep -r is not portable. + find $prefix/$folder -type f \ + | xargs grep -sl '// generated by npm' \ + | while read file; do + filelist[$fid]="$file" + let 'fid++' + done + done + + # now remove the package modules, and the .npm folder itself. + if [ "x$packages" != "x" ]; then + for pkg in $packages; do + filelist[$fid]="$prefix/lib/node/$pkg" + let 'fid++' + for i in $prefix/lib/node/$pkg\@*; do + filelist[$fid]="$i" + let 'fid++' + done + done + fi + + for folder in lib/node/.npm lib/npm share/npm; do + if [ -d $prefix/$folder ]; then + filelist[$fid]="$prefix/$folder" + let 'fid++' + fi + done +done + +# now actually clean, but only if there's anything TO clean +if [ "${#filelist[@]}" -gt 0 ]; then + echo "" + echo "This script will find and eliminate any shims, symbolic" + echo "links, and other cruft that was installed by npm 0.x." + echo "" + + if [ "x$packages" != "x" ]; then + echo "The following packages appear to have been installed with" + echo "an old version of npm, and will be removed forcibly:" + for pkg in $packages; do + echo " $pkg" + done + echo "Make a note of these. You may want to install them" + echo "with npm 1.0 when this process is completed." + echo "" + fi + + OK= + if [ "x$1" = "x-y" ]; then + OK="yes" + fi + + while [ "$OK" != "y" ] && [ "$OK" != "yes" ] && [ "$OK" != "no" ]; do + echo "Is this OK?" + echo " enter 'yes' or 'no'" + echo " or 'show' to see a list of files " + read OK + if [ "x$OK" = "xshow" ] || [ "x$OK" = "xs" ]; then + for i in "${filelist[@]}"; do + echo "$i" + done + fi + done + if [ "$OK" = "no" ]; then + echo "Aborting" + exit 1 + fi + for i in "${filelist[@]}"; do + rm -rf "$i" + done +fi + +echo "" +echo 'All clean!' + +exit 0 diff --git a/examples/doc-build.sh b/examples/doc-build.sh new file mode 100755 index 0000000..6181902 --- /dev/null +++ b/examples/doc-build.sh @@ -0,0 +1,119 @@ +#!/usr/bin/env bash + +if [[ $DEBUG != "" ]]; then + set -x +fi +set -o errexit +set -o pipefail + +if ! [ -x node_modules/.bin/marked-man ]; then + ps=0 + if [ -f .building_marked-man ]; then + pid=$(cat .building_marked-man) + ps=$(ps -p $pid | grep $pid | wc -l) || true + fi + + if [ -f .building_marked-man ] && [ $ps != 0 ]; then + while [ -f .building_marked-man ]; do + sleep 1 + done + else + # a race to see which make process will be the one to install marked-man + echo $$ > .building_marked-man + sleep 1 + if [ $(cat .building_marked-man) == $$ ]; then + make node_modules/.bin/marked-man + rm .building_marked-man + else + while [ -f .building_marked-man ]; do + sleep 1 + done + fi + fi +fi + +if ! [ -x node_modules/.bin/marked ]; then + ps=0 + if [ -f .building_marked ]; then + pid=$(cat .building_marked) + ps=$(ps -p $pid | grep $pid | wc -l) || true + fi + + if [ -f .building_marked ] && [ $ps != 0 ]; then + while [ -f .building_marked ]; do + sleep 1 + done + else + # a race to see which make process will be the one to install marked + echo $$ > .building_marked + sleep 1 + if [ $(cat .building_marked) == $$ ]; then + make node_modules/.bin/marked + rm .building_marked + else + while [ -f .building_marked ]; do + sleep 1 + done + fi + fi +fi + +src=$1 +dest=$2 +name=$(basename ${src%.*}) +date=$(date -u +'%Y-%m-%d %H:%M:%S') +version=$(node cli.js -v) + +mkdir -p $(dirname $dest) + +html_replace_tokens () { + local url=$1 + sed "s|@NAME@|$name|g" \ + | sed "s|@DATE@|$date|g" \ + | sed "s|@URL@|$url|g" \ + | sed "s|@VERSION@|$version|g" \ + | perl -p -e 's/
\3<\/p>/g' \
+ | perl -p -e 's/npm-npm/npm/g' \
+ | perl -p -e 's/([^"-])(npm-)?README(?!\.html)(\(1\))?/\1README<\/a>/g' \
+ | perl -p -e 's/