Line data Source code
1 : // Copyright (C) 2011 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.pgm; 16 : 17 : import com.google.gerrit.pgm.util.AbstractProgram; 18 : import com.googlecode.prolog_cafe.exceptions.HaltException; 19 : import com.googlecode.prolog_cafe.lang.BufferingPrologControl; 20 : import com.googlecode.prolog_cafe.lang.Prolog; 21 : import com.googlecode.prolog_cafe.lang.PrologClassLoader; 22 : import com.googlecode.prolog_cafe.lang.SymbolTerm; 23 : import java.io.File; 24 : import java.io.IOException; 25 : import java.util.ArrayList; 26 : import java.util.List; 27 : import org.kohsuke.args4j.Option; 28 : 29 0 : public class PrologShell extends AbstractProgram { 30 0 : @Option(name = "-s", metaVar = "FILE.pl", usage = "file to load") 31 : private List<String> fileName = new ArrayList<>(); 32 : 33 : @Option(name = "-q", usage = "quiet mode without banner") 34 : private boolean quiet; 35 : 36 : @Override 37 : public int run() { 38 0 : if (!quiet) { 39 0 : banner(); 40 : } 41 : 42 0 : BufferingPrologControl pcl = new BufferingPrologControl(); 43 0 : pcl.setPrologClassLoader(new PrologClassLoader(getClass().getClassLoader())); 44 0 : pcl.setEnabled(Prolog.Feature.IO, true); 45 0 : pcl.setEnabled(Prolog.Feature.STATISTICS, true); 46 0 : pcl.configureUserIO(System.in, System.out, System.err); 47 0 : pcl.initialize(Prolog.BUILTIN); 48 : 49 0 : for (String file : fileName) { 50 : String path; 51 : try { 52 0 : path = new File(file).getCanonicalPath(); 53 0 : } catch (IOException e) { 54 0 : path = new File(file).getAbsolutePath(); 55 0 : } 56 0 : pcl.execute(Prolog.BUILTIN, "consult", SymbolTerm.create(path)); 57 0 : System.err.println(); 58 0 : System.err.flush(); 59 0 : } 60 : 61 : try { 62 0 : pcl.execute(Prolog.BUILTIN, "cafeteria"); 63 0 : if (!quiet) { 64 0 : write("% halt\n"); 65 : } 66 0 : return 0; 67 0 : } catch (HaltException halt) { 68 0 : write("% halt(" + halt.getStatus() + ")\n"); 69 0 : return halt.getStatus(); 70 : } 71 : } 72 : 73 : private void banner() { 74 0 : System.err.format( 75 : "Gerrit Code Review %s - Interactive Prolog Shell", 76 0 : com.google.gerrit.common.Version.getVersion()); 77 0 : System.err.println(); 78 0 : System.err.println( 79 : "(type Ctrl-D or \"halt.\" to exit, \"['path/to/file.pl'].\" to load a file)"); 80 0 : System.err.println(); 81 0 : System.err.flush(); 82 0 : } 83 : 84 : private void write(String msg) { 85 0 : System.out.flush(); 86 0 : System.err.flush(); 87 0 : System.out.println(msg); 88 0 : System.out.flush(); 89 0 : } 90 : }