shellwords.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Generated by CoffeeScript 1.3.3
  2. (function() {
  3. var scan;
  4. scan = function(string, pattern, callback) {
  5. var match, result;
  6. result = "";
  7. while (string.length > 0) {
  8. match = string.match(pattern);
  9. if (match) {
  10. result += string.slice(0, match.index);
  11. result += callback(match);
  12. string = string.slice(match.index + match[0].length);
  13. } else {
  14. result += string;
  15. string = "";
  16. }
  17. }
  18. return result;
  19. };
  20. exports.split = function(line) {
  21. var field, words;
  22. if (line == null) {
  23. line = "";
  24. }
  25. words = [];
  26. field = "";
  27. scan(line, /\s*(?:([^\s\\\'\"]+)|'((?:[^\'\\]|\\.)*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|$)?/, function(match) {
  28. var dq, escape, garbage, raw, seperator, sq, word;
  29. raw = match[0], word = match[1], sq = match[2], dq = match[3], escape = match[4], garbage = match[5], seperator = match[6];
  30. if (garbage != null) {
  31. throw new Error("Unmatched quote");
  32. }
  33. field += word || (sq || dq || escape).replace(/\\(?=.)/, "");
  34. if (seperator != null) {
  35. words.push(field);
  36. return field = "";
  37. }
  38. });
  39. if (field) {
  40. words.push(field);
  41. }
  42. return words;
  43. };
  44. exports.escape = function(str) {
  45. if (str == null) {
  46. str = "";
  47. }
  48. if (str == null) {
  49. return "''";
  50. }
  51. return str.replace(/([^A-Za-z0-9_\-.,:\/@\n])/g, "\\$1").replace(/\n/g, "'\n'");
  52. };
  53. }).call(this);