3import java.util.Arrays;
6import java.util.HashMap;
8import java.util.HashSet;
9import java.io.Closeable;
11import java.io.IOException;
12import java.io.EOFException;
13import java.io.PrintStream;
14import java.io.InputStream;
15import java.nio.ByteBuffer;
16import java.nio.file.Files;
17import java.nio.file.OpenOption;
18import java.nio.file.StandardOpenOption;
19import java.nio.channels.Channels;
20import java.nio.channels.SeekableByteChannel;
21import java.nio.channels.ReadableByteChannel;
22import java.util.concurrent.BlockingQueue;
23import java.util.concurrent.LinkedBlockingQueue;
24import java.util.concurrent.Semaphore;
26@SuppressWarnings("unchecked")
27public class ForeignPrimitives {
28 private static final Map<Thread, List<String>> args = new HashMap<>();
29 private static final Semaphore messageMutex = new Semaphore(1);
30 private static final Map<Thread, BlockingQueue<Object>> messages = new HashMap<>();
31 private static final Set<Object> feofFiles = new HashSet<>();
33 public static synchronized void idris_initArgs(Thread t, String [] args) {
34 ForeignPrimitives.args.put(t, Arrays.asList(args));
37 public static synchronized int idris_numArgs(Object thread) {
38 List<String> argsForThread = ForeignPrimitives.args.get((Thread)thread);
39 return (argsForThread == null ? 0 : argsForThread.size());
42 public static synchronized String idris_getArg(Object thread, int num) {
43 List<String> argsForThread = ForeignPrimitives.args.get((Thread)thread);
44 return (argsForThread == null ? null : argsForThread.get(num));
47 public static String getenv(String x) {
48 return System.getenv(x);
51 public static void exit(int exitCode) {
52 System.exit(exitCode);
55 public static void usleep(int microsecs) {
57 Thread.sleep(microsecs / 1000, microsecs % 1000);
58 } catch (InterruptedException ex) {
62 public static void idris_sendMessage(Object from, Object dest, Object message) throws InterruptedException {
63 messageMutex.acquire();
64 BlockingQueue<Object> messagesForTarget = messages.get((Thread)dest);
65 if (messagesForTarget == null) {
66 messagesForTarget = new LinkedBlockingQueue<>();
67 messages.put((Thread)dest, messagesForTarget);
69 messagesForTarget.put(message);
70 messageMutex.release();
73 public static int idris_checkMessage(Object dest) throws InterruptedException {
74 messageMutex.acquire();
75 BlockingQueue<Object> messagesForTarget = messages.get((Thread)dest);
76 messageMutex.release();
77 return (messagesForTarget == null ? 0 : messagesForTarget.size());
80 public static Object idris_recvMessage(Object dest) throws InterruptedException {
81 messageMutex.acquire();
82 BlockingQueue<Object> messagesForTarget = messages.get((Thread)dest);
83 if (messagesForTarget == null) {
84 messagesForTarget = new LinkedBlockingQueue();
85 messages.put((Thread)dest, messagesForTarget);
87 messageMutex.release();
88 return messagesForTarget.take();
91 public static void putStr(String str) {
92 System.out.print(str);
95 public static void putchar(char c) {
99 public static int getchar() {
101 return (char)System.in.read();
102 } catch (IOException ex) {
107 public static SeekableByteChannel fileOpen(String name, String privs) {
109 OpenOption [] options;
112 options = new StandardOpenOption []
113 { StandardOpenOption.READ };
116 options = new StandardOpenOption []
117 { StandardOpenOption.READ,
118 StandardOpenOption.WRITE };
121 options = new StandardOpenOption []
122 { StandardOpenOption.WRITE,
123 StandardOpenOption.CREATE };
126 options = new StandardOpenOption []
127 { StandardOpenOption.READ,
128 StandardOpenOption.WRITE,
129 StandardOpenOption.CREATE };
132 options = new StandardOpenOption []
133 { StandardOpenOption.WRITE,
134 StandardOpenOption.CREATE,
135 StandardOpenOption.APPEND };
138 options = new StandardOpenOption []
139 { StandardOpenOption.READ,
140 StandardOpenOption.WRITE,
141 StandardOpenOption.CREATE,
142 StandardOpenOption.APPEND };
145 options = new StandardOpenOption [] {};
148 return Files.newByteChannel(new File(name).toPath(), options);
149 } catch (IOException ex) {
154 public static synchronized void fileClose(Object file) throws IOException {
155 ((Closeable)file).close();
156 feofFiles.remove(file);
159 public static void fputStr(Object file, String string) throws IOException {
160 if (file instanceof PrintStream) {
161 ((PrintStream)file).print(string);
162 } else if (file instanceof SeekableByteChannel) {
163 ((SeekableByteChannel)file).write(ByteBuffer.wrap(string.getBytes()));
167 public static synchronized String idris_readStr(Object file) throws IOException {
168 ReadableByteChannel channel = file instanceof InputStream ?
169 Channels.newChannel((InputStream)file) :
170 (ReadableByteChannel)file;
171 ByteBuffer buf = ByteBuffer.allocate(1);
172 StringBuilder resultBuilder = new StringBuilder("");
173 String delimiter = System.getProperty("line.separator");
177 read = channel.read(buf);
179 resultBuilder.append(new String(buf.array()));
180 if (resultBuilder.lastIndexOf(delimiter) > -1) {
181 return resultBuilder.toString();
188 return resultBuilder.toString();
191 public static int fileEOF(Object file) {
192 return (feofFiles.contains(file) ? 1 : 0);
195 public static int isNull(Object o) {
196 return (o == null ? 1 : 0);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list