60 static Map<String,String> env_ =
null;
69 static class ExternalProgram {
70 String description =
null;
75 ExternalProgram(String description,String envvar,String guess,
77 this.description=description;
85 for (String p : guess.split(File.pathSeparator))
89 for (String p : paths)
90 if ((program=getProgram(p))!=
null)
95 System.getProperty(
"os.name").startsWith(
"Windows")) {
96 program = searchOnWindows();
99 if (warn && program==
null) {
100 System.err.println(
"Could not find program '"+description+
"'");
101 System.err.println(
"Tried at paths");
102 for (String p : paths)
104 System.err.println(
" '"+p+
"'");
105 System.err.println(
"\n\n");
109 private File searchOnWindows() {
110 String programDir1 = System.getenv(
"ProgramFiles(X86)");
111 String programDir2 = System.getenv(
"ProgramFiles");
113 if (programDir1 !=
null) {
114 File programDir =
new File(programDir1);
115 for (File subDir : programDir.listFiles()) {
116 if (subDir.getName().startsWith(
"Graphviz")) {
117 return new File(subDir,
"bin"+File.separator+
"dot.exe");
122 File programDir =
new File(programDir2);
123 for (File subDir : programDir.listFiles()) {
124 if (subDir.getName().startsWith(
"Graphviz")) {
125 return new File(subDir,
"bin"+File.separator+
"dot.exe");
132 File getProgram(String path) {
134 File prog=
new File(path);
142 public boolean isAvailable() {
return program!=
null; }
146 public String getPath() {
148 String message=
"ERROR:\nExternal program '"+description+
"' not found.\n";
150 message+=
"The standard path is '"+guess+
"'.\n";
152 message+=
"Set environment variable '"+envvar+
"' to customize path.";
153 if (
env(envvar)!=
null)
154 message+=
"\n(The current value is '"+envvar+
"="+
env(envvar)+
"'.).";
158 throw new RuntimeException(message);
160 return program.getPath();
164 static final ExternalProgram PDFVIEWER =
165 new ExternalProgram(
"PDF viewer",
"AUD_PDFVIEWER",
"/usr/bin/evince",
false);
166 static final ExternalProgram EMACS =
167 new ExternalProgram(
"emacs editor",
"AUD_EMACS",
"/usr/bin/emacs",
false);
168 static final ExternalProgram EMACSCLIENT =
169 new ExternalProgram(
"emacs-client editor",
170 "AUD_EMACSCLIENT",
"/usr/bin/emacsclient",
false);
176 public static synchronized String
env(String varname) {
178 env_=System.getenv();
179 return env_.get(varname);
185 for (
int i=0;i<level;++i) spaces+=
" ";
194 FileOutputStream f=
new FileOutputStream(file);
195 f.write(text.getBytes());
197 }
catch (IOException e) {
198 System.err.println(
"ERROR: "+e.getMessage());
208 return writeToFile(File.createTempFile(
"aud-",suffix),text);
209 }
catch (IOException e) {
210 System.err.println(
"ERROR: "+e.getMessage());
217 byte[] buffer =
new byte[(int) file.length()];
218 BufferedInputStream f =
null;
220 f =
new BufferedInputStream(
new FileInputStream(file));
222 }
catch (IOException e) {
223 System.err.println(
"ERROR: "+e.getMessage());
226 if (f !=
null)
try { f.close(); }
catch (IOException ignored) { }
228 return new String(buffer);
238 Thread t=
new Thread(
new Runnable()
242 String cmd=Thread.currentThread().getName();
245 new ProcessBuilder(cmd.split(
" ")).start();
246 }
catch(IOException e) {
261 StackTraceElement location=
new Throwable().getStackTrace()[depth+1];
262 String rv=location.getFileName().toString()+
":";
263 rv+=location.getLineNumber()+
" ";
264 rv+=location.getClassName()+
"#"+location.getMethodName();
274 if (EMACS.isAvailable() && EMACSCLIENT.isAvailable()) {
276 EMACSCLIENT.getPath()+
" -a "+EMACS.getPath()+
" -n +"+line+
277 (column>0 ?
":"+column+
" " :
" ")+file;
278 System.err.println(command);
285 StackTraceElement location=
new Throwable().getStackTrace()[depth+1];
286 File file=
new File(location.getFileName().toString());
288 emacsclient(file.getPath(),location.getLineNumber(),0);
290 System.err.println(
"unknown path for source file '"+file+
"'");
Implementation of an array-based vector.
void push_back(T obj)
insert new entry obj at the end [O(1) for sufficient capacity]
System related utilities.
static synchronized String env(String varname)
Get environment variable varname.
static String indent(int level)
get indentation string filled with spaces
static void emacsclient(String file, int line, int column)
open emacs client (Un*x only) (or no action otherwise)
static File writeToTempFile(String text, String suffix)
write text to temporary file
static void execAndDetach(String command)
Execute command in a new process and detach.
static File writeToFile(File file, String text)
write text to file @endiliteral
static String whereAmI()
get code location (like __FILE__,__LINE__
static String readFile(File file)
read entire file and return contents as String
static String whereAmI(int depth)
get code location (like __FILE__,__LINE__
static void viewPDFFile(String filename)
open PDF viewer
static void openCallersSourceInEmacs(int depth)
open emacs whereAmI (Un*x only)
AuD lecture: Data structures, algorithms, examples.