clj.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/bin/bash
  2. #
  3. # Launcher script for Clojure programs. These environment variables can be
  4. # used to configure the script:
  5. #
  6. # CLOJURE_HOME
  7. # The root directory where Clojure is installed.
  8. # CLOJURE_JAVA
  9. # The name of the java executable used to run Clojure.
  10. # CLOJURE_JAVA_OPTS
  11. # Additional options to be passed to the java executable.
  12. # CLOJURE_CLASSPATH
  13. # A path to be added to Clojure's classpath.
  14. # CLOJURE_LIBRARY_PATH
  15. # A path to be searched for native code such as DLL's or JNI
  16. # libraries. This gets added to the Java options as
  17. # "-Djava.library.path=$CLOJURE_LIBRARY_PATH".
  18. # CLOJURE_LIB
  19. # This directory, and any jars inside it, will be automatically
  20. # added to Clojure's classpath.
  21. #
  22. # CLOJURE_JLINE
  23. # This should be the path to Jline jar.
  24. # TODO:
  25. # make CLOJURE_LIB a path instead of a single directory
  26. # allow for adding to CLOJURE_LIB from the command line
  27. usage="\
  28. usage: clojure [options] [file1 [file2] ...]
  29. Options:
  30. --help, -h show this message
  31. --java-cmd, -J the Java executable to use
  32. --java-opts, -j add options to be passed on to the JVM
  33. --classpath, -cp add to Clojure's classpath
  34. --library-path, -L add to the path to search for native libraries
  35. --verbose, -v print initialization information
  36. "
  37. ## read ~/.clojurerc for home configuration
  38. [ -e ~/.clojurerc ] && . ~/.clojurerc
  39. ## read ./.clojurerc for project specific configuration
  40. [ -e ./.clojurerc ] && . ./.clojurerc
  41. if [ ! "$CLOJURE_HOME" ]; then
  42. # Find the real path to Clojure's home directory if $0 is a symlink
  43. #program="$0"
  44. #while [ -h "$program" ]; do
  45. # ls=`ls -ld "$program"`
  46. # link=`expr "$ls" : '.*-> \(.*\)$'`
  47. # if expr "$link" : '.*/.*' >/dev/null; then
  48. # program="$link"
  49. # else
  50. # program="`dirname $program`/$link"
  51. # fi
  52. #done
  53. #script_dir=`dirname "$program"`
  54. #relative_clojure_home=`dirname "$script_dir"`
  55. #CLOJURE_HOME=`cd "$relative_clojure_home" && pwd`
  56. . /etc/profile.d/clojure.sh
  57. fi
  58. if [ ! "$CLOJURE_JAVA" ]; then
  59. CLOJURE_JAVA="java";
  60. fi
  61. if [ ! "$CLOJURE_JAVA_OPTS" ]; then
  62. CLOJURE_JAVA_OPTS="-Dpid=$$"; # set the pid for SLIME
  63. fi
  64. if [ ! "$CLOJURE_CLASSPATH" ]; then
  65. CLOJURE_CLASSPATH=".:/usr/share/java/asm-all-4.jar"
  66. fi
  67. ## Add Clojure home jars.
  68. for jar in "$CLOJURE_HOME"/*.jar; do
  69. CLOJURE_CLASSPATH="$CLOJURE_CLASSPATH:$jar"
  70. done
  71. if [ -d "$CLOJURE_LIB" ]; then
  72. CLOJURE_CLASSPATH="$CLOJURE_CLASSPATH:$CLOJURE_LIB"
  73. for jar in "$CLOJURE_LIB"/*.jar; do
  74. CLOJURE_CLASSPATH="$CLOJURE_CLASSPATH:$jar"
  75. done
  76. fi
  77. # this is now the same for both the repl and for scripts
  78. main="clojure.main"
  79. repl=0
  80. verbose=0
  81. while true; do
  82. case $1 in
  83. -h|--help)
  84. echo "$usage"; exit 1;;
  85. -J|--java-cmd)
  86. CLOJURE_JAVA="$2"; shift; shift;;
  87. -j|--java-opts)
  88. CLOJURE_JAVA_OPTS="$CLOJURE_JAVA_OPTS $2"; shift; shift;;
  89. -cp|--classpath)
  90. CLOJURE_CLASSPATH="$CLOJURE_CLASSPATH:$2"; shift; shift;;
  91. -L|--library-path)
  92. if [ "$CLOJURE_LIBRARY_PATH" ]; then
  93. CLOJURE_LIBRARY_PATH="$CLOJURE_LIBRARY_PATH:$2";
  94. else
  95. CLOJURE_LIBRARY_PATH="$2";
  96. fi
  97. shift; shift;;
  98. -v|--verbose)
  99. verbose=1; shift;;
  100. *) break;;
  101. esac
  102. done
  103. [ $verbose -eq 1 ] && echo "$CLOJURE_CLASSPATH"
  104. # If we didn't get any files to load on the commandline, we want to run the
  105. # repl, with command line editing if available.
  106. [ $# -eq 0 ] && repl=1
  107. # If the classpath contains the JLine jar, use the JLine console runner
  108. if expr "$CLOJURE_CLASSPATH" : ".*jline.*\.jar" >/dev/null; then
  109. [ $repl -eq 1 ] && jline="jline.ConsoleRunner"
  110. fi
  111. # Enable rlwrap if present
  112. if [ $repl -eq 1 ] && [ -z $jline ]; then
  113. rlwrap=`type -p rlwrap`
  114. fi
  115. ## Add CLOJURE_LIBRARY_PATH to the Java options if necessary
  116. if [ -n "$CLOJURE_LIBRARY_PATH" ]; then
  117. CLOJURE_JAVA_OPTS="$CLOJURE_JAVA_OPTS -Djava.library.path=$CLOJURE_LIBRARY_PATH"
  118. fi
  119. cmd=`echo $rlwrap "$CLOJURE_JAVA" "$CLOJURE_JAVA_OPTS" -cp "$CLOJURE_CLASSPATH" $jline $main`
  120. [ $verbose -eq 1 ] && echo "$cmd" "$@"
  121. exec `echo $cmd` "$@"