Skip to content
Browse files

removed the -d option and the readline workaround, fixed bug with emp…

…ty input condition
  • Loading branch information...
1 parent daa982e commit 88ce296b951c45f93571b4c6fdc581ae3b873062 @micha committed Jul 7, 2009
Showing with 28 additions and 34 deletions.
  1. +0 −6 README.markdown
  2. +28 −28 jsawk
View
6 README.markdown
@@ -63,12 +63,6 @@ Usage
-n
Suppress printing of JSON result set.
- -d <delim>
- Choose a token to indicate end of input. If this token appears on
- a line by itself then reading of JSON input will end. Otherwise,
- 10 blank lines in a row will end input. (Don't look at me, this
- is a spidermonkey issue.) Default delimiter is a single period.
-
-q <query>
Filter JSON through the specified JSONQuery query. If multiple
'-q' options are specified then each query will be performed in
View
56 jsawk
@@ -12,11 +12,12 @@
# licenses of the incorporated software below.
#
-TMP=`mktemp`
+TMP1=`mktemp /tmp/tmp.XXXXXX`
+TMP2=`mktemp /tmp/tmp.XXXXXX`
-trap "rm -f $TMP" SIGINT SIGTERM SIGHUP SIGQUIT
+trap "rm -f $TMP $TMP2" SIGINT SIGTERM SIGHUP SIGQUIT
-cat <<'__END__' > $TMP
+cat <<'__END__' > $TMP1
window = this; // the global object
window.IS = []; // the input set
@@ -905,11 +906,20 @@ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
};
window.alert = p;
window.doJson = function(input) {
- try {
- return eval("("+input+")");
- } catch (e) {
- err("jsawk: JSON parse error: '"+input+"'");
- quit(2);
+ if (typeof input !== "string") {
+ return input;
+ } else {
+ input = input.replace(/\s*$/,"");
+ if (!input.length) {
+ return {};
+ } else {
+ try {
+ return eval("("+input+")");
+ } catch (e) {
+ err("jsawk: JSON parse error: '"+input+"'");
+ quit(2);
+ }
+ }
}
};
window.doCall = function(fun, obj) {
@@ -958,6 +968,8 @@ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
(function(argv) {
argv = Array.prototype.slice.call(argv);
+ var inputLines = argv.shift();
+
var usage = function() {
err("Usage: jsq [-n] [-f jsfile1.js]* [-q jsonquery] [-d delim] "+
"[-b script] \\\n"+
@@ -967,7 +979,6 @@ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
var fun = "";
var noprint = false;
- var end = ".";
var libs = [];
var befores = [];
var afters = [];
@@ -984,10 +995,6 @@ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
case "-n":
noprint = true;
break;
- case "-d":
- if (argv.length < 1) usage();
- end = argv.shift();
- break;
case "-q":
if (argv.length < 1) usage();
queries.push(argv.shift());
@@ -1016,23 +1023,13 @@ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
}
}
- // Spidermonkey can't read input well. This is a workaround
- // from http://whereisandy.com/code/jslint/.
-
- var input="", line="", blankcount="0";
+ var input="";
- while (blankcount < 10){
- line=readline();
-
- if (!line || line.length==0)
- blankcount++;
- else
- blankcount=0;
- if (line==end) break;
+ for (var i=0; i<inputLines; i++) {
+ var line=readline();
input += (line ? line : "");
input += "\n";
}
- input = input.substring(0, input.length-blankcount);
var wrapped;
@@ -1078,14 +1075,17 @@ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
})(arguments);
__END__
-res=$(js $TMP "$@")
+echo "$(cat 2>/dev/null)" > $TMP2
+nlines=$(grep -c '$' $TMP2 2>/dev/null || echo 0)
+
+res=$(cat $TMP2 2>/dev/null | js $TMP1 $nlines "$@")
ret=$?
out=$(echo "$res" |sed '/^OUT: /s/^.....//p;d')
err=$(echo "$res" |sed '/^ERR: /s/^.....//p;d')
[ -n "$err" ] && echo "$err" 1>&2
[ -n "$out" ] && echo "$out"
-rm -f $TMP
+rm -f $TMP1 $TMP2
exit $ret

0 comments on commit 88ce296

Please sign in to comment.
Something went wrong with that request. Please try again.