1 package io.jawk.jrt;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.io.Closeable;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.nio.charset.StandardCharsets;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Objects;
35
36 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class StreamInputSource implements InputSource, Closeable {
60
61 private final InputStream defaultInput;
62 private final VariableManager vm;
63 private final JRT jrt;
64
65
66 private Map<Object, Object> arglistMap;
67 private int arglistIdx;
68 private int arglistMaxKey;
69 private boolean hasFilenames;
70
71
72 private PartitioningReader partitioningReader;
73 private boolean currentReaderIsDefaultInput;
74 private boolean currentFromFilenameList;
75 private String currentRecord;
76 private boolean currentReaderExhausted;
77
78
79 private String currentFileOpenError;
80 private boolean currentPresentedToLoop;
81 private int lastArgumentIndex;
82
83
84
85
86
87
88
89
90
91
92
93 @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Fail-fast argument validation; no security-sensitive state to protect from finalizer attacks")
94 public StreamInputSource(InputStream defaultInput, VariableManager vm, JRT jrt) {
95 this.defaultInput = Objects.requireNonNull(defaultInput, "defaultInput");
96 this.vm = Objects.requireNonNull(vm, "vm");
97 this.jrt = Objects.requireNonNull(jrt, "jrt");
98 }
99
100
101
102
103
104
105
106
107
108
109 InputStream getDefaultInput() {
110 return defaultInput;
111 }
112
113
114 @Override
115 public boolean nextRecord() throws IOException {
116 initializeArgList();
117
118 while (true) {
119 if (partitioningReader == null || currentReaderExhausted) {
120 if (!prepareNextReader()) {
121 return false;
122 }
123 currentReaderExhausted = false;
124 }
125
126 String nextRecord = partitioningReader.readRecord();
127 if (nextRecord != null) {
128 currentRecord = nextRecord;
129 currentFromFilenameList = partitioningReader.fromFilenameList();
130 return true;
131 }
132 if (!partitioningReader.fromFilenameList()) {
133 return false;
134 }
135 currentReaderExhausted = true;
136 }
137 }
138
139
140 @Override
141 public String getRecordText() {
142 return currentRecord;
143 }
144
145
146
147
148
149
150
151 @Override
152 public List<String> getFields() {
153 return null;
154 }
155
156
157 @Override
158 public boolean isFromFilenameList() {
159 return currentFromFilenameList;
160 }
161
162
163
164
165
166
167
168 public void setRecordSeparator(String rs) {
169 if (partitioningReader != null) {
170 partitioningReader.setRecordSeparator(rs);
171 }
172 }
173
174
175
176
177
178
179
180 PartitioningReader getPartitioningReader() {
181 return partitioningReader;
182 }
183
184
185
186
187
188
189
190
191 private void initializeArgList() {
192 if (arglistMap != null) {
193 return;
194 }
195 arglistMap = toArgvMap(vm.getARGV());
196 arglistMaxKey = computeMaxArgvKey();
197 arglistIdx = 1;
198 hasFilenames = detectFilenames();
199 }
200
201 private Map<Object, Object> toArgvMap(Object argv) {
202 if (!(argv instanceof Map)) {
203 throw new IllegalArgumentException("ARGV must be a Map.");
204 }
205 @SuppressWarnings("unchecked")
206 Map<Object, Object> argvMap = (Map<Object, Object>) argv;
207 return argvMap;
208 }
209
210
211
212
213
214
215 private int computeMaxArgvKey() {
216 int max = 0;
217 for (Object key : arglistMap.keySet()) {
218 int idx = (int) JRT.toLong(key);
219 if (idx > max) {
220 max = idx;
221 }
222 }
223 return max;
224 }
225
226
227
228
229
230
231
232 private boolean detectFilenames() {
233 int traversalArgCount = getTraversalArgCount();
234 boolean found = false;
235 for (int i = 1; i < traversalArgCount && !found; i++) {
236 Object argValue = getArgvValue(i);
237 if (argValue == MISSING_ARGV_VALUE) {
238 continue;
239 }
240 String arg = jrt.toAwkString(argValue);
241 if (arg.isEmpty() || arg.indexOf('=') > 0) {
242 continue;
243 }
244 found = true;
245 }
246 return found;
247 }
248
249
250
251
252
253
254 private int getArgCount() {
255 double raw = JRT.toDouble(vm.getARGC());
256 if (raw <= 0) {
257 return 0;
258 }
259 if (raw > Integer.MAX_VALUE) {
260 return Integer.MAX_VALUE;
261 }
262 return (int) raw;
263 }
264
265
266
267
268
269
270
271
272 private int getTraversalArgCount() {
273 int argCount = getArgCount();
274 if (argCount <= 0) {
275 return 0;
276 }
277 return Math.min(argCount, arglistMaxKey + 1);
278 }
279
280
281
282
283
284
285
286
287 private String nextArgument() {
288 int traversalArgCount = getTraversalArgCount();
289 while (arglistIdx < traversalArgCount) {
290 int idx = arglistIdx++;
291 Object argValue = getArgvValue(idx);
292 if (argValue == MISSING_ARGV_VALUE) {
293 continue;
294 }
295 String arg = jrt.toAwkString(argValue);
296 if (!arg.isEmpty()) {
297 lastArgumentIndex = idx;
298 return arg;
299 }
300 }
301 return null;
302 }
303
304 private static final Object MISSING_ARGV_VALUE = new Object();
305
306 private Object getArgvValue(int index) {
307 Long longIndex = Long.valueOf(index);
308 if (arglistMap instanceof AssocArray) {
309 return JRT.containsAwkKey(arglistMap, longIndex) ?
310 JRT.getAssocArrayValue(arglistMap, longIndex) :
311 MISSING_ARGV_VALUE;
312 }
313 if (arglistMap.containsKey(longIndex)) {
314 return arglistMap.get(longIndex);
315 }
316 Integer intIndex = Integer.valueOf(index);
317 if (arglistMap.containsKey(intIndex)) {
318 return arglistMap.get(intIndex);
319 }
320 for (Map.Entry<Object, Object> entry : arglistMap.entrySet()) {
321 Object key = entry.getKey();
322 if (!(key instanceof Number)) {
323 continue;
324 }
325 double numericKey = ((Number) key).doubleValue();
326 if (JRT.isActuallyLong(numericKey) && ((long) Math.rint(numericKey)) == index) {
327 return entry.getValue();
328 }
329 }
330 return MISSING_ARGV_VALUE;
331 }
332
333
334
335
336
337
338
339
340
341
342 private boolean prepareNextReader() throws IOException {
343 boolean ready = false;
344 arglistMaxKey = computeMaxArgvKey();
345 hasFilenames = detectFilenames();
346 while (!ready) {
347 String arg = nextArgument();
348 if (arg == null) {
349
350 hasFilenames = detectFilenames();
351 if (partitioningReader == null && !hasFilenames) {
352 partitioningReader = new PartitioningReader(
353 new InputStreamReader(defaultInput, StandardCharsets.UTF_8),
354 jrt.getRSString());
355 currentReaderIsDefaultInput = true;
356 jrt.setFILENAMEViaJrt(jrt.toInputScalar(""));
357
358
359 jrt.setERRNO("");
360 return true;
361 }
362 closeCurrentReaderIfFileStream();
363 return false;
364 }
365 if (arg.indexOf('=') > 0) {
366 setFilelistVariable(arg);
367
368 arglistMaxKey = computeMaxArgvKey();
369 hasFilenames = detectFilenames();
370 if (partitioningReader == null && !hasFilenames) {
371 partitioningReader = new PartitioningReader(
372 new InputStreamReader(defaultInput, StandardCharsets.UTF_8),
373 jrt.getRSString());
374 currentReaderIsDefaultInput = true;
375 jrt.setFILENAMEViaJrt(jrt.toInputScalar(""));
376
377
378 jrt.setERRNO("");
379 return true;
380 }
381 } else {
382 closeCurrentReaderIfFileStream();
383 partitioningReader = openFileListReader(arg);
384 jrt.setFILENAMEViaJrt(jrt.toInputScalar(arg));
385 jrt.setFNR(0L);
386 jrt.setARGIND(Long.valueOf(lastArgumentIndex));
387
388
389 jrt.setERRNO("");
390 ready = true;
391 }
392 }
393 return true;
394 }
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414 public boolean advanceToNextFile() throws IOException {
415 initializeArgList();
416
417
418
419
420 if (!currentPresentedToLoop
421 && partitioningReader != null
422 && !currentReaderExhausted
423 && currentFileOpenError == null) {
424 currentPresentedToLoop = true;
425 return true;
426 }
427
428 currentFileOpenError = null;
429 arglistMaxKey = computeMaxArgvKey();
430 hasFilenames = detectFilenames();
431 while (true) {
432 String arg = nextArgument();
433 if (arg == null) {
434
435 hasFilenames = detectFilenames();
436 if (partitioningReader == null && !hasFilenames) {
437 return presentDefaultInput();
438 }
439 closeCurrentReaderIfFileStream();
440 return false;
441 }
442 if (arg.indexOf('=') > 0) {
443 setFilelistVariable(arg);
444
445 arglistMaxKey = computeMaxArgvKey();
446 hasFilenames = detectFilenames();
447 if (partitioningReader == null && !hasFilenames) {
448 return presentDefaultInput();
449 }
450 } else {
451 closeCurrentReaderIfFileStream();
452 partitioningReader = null;
453 currentReaderExhausted = false;
454 currentPresentedToLoop = true;
455 jrt.setFILENAMEViaJrt(jrt.toInputScalar(arg));
456 beginFileState(lastArgumentIndex);
457 currentFileOpenError = openCurrentFile(arg);
458 if (currentFileOpenError != null) {
459 jrt.setERRNO(currentFileOpenError);
460 }
461 return true;
462 }
463 }
464 }
465
466
467
468
469
470
471
472
473
474
475 public boolean nextRecordInCurrentFile() throws IOException {
476 if (partitioningReader == null || currentReaderExhausted || currentFileOpenError != null) {
477 return false;
478 }
479 String nextRecord = partitioningReader.readRecord();
480 if (nextRecord == null) {
481 currentReaderExhausted = true;
482 return false;
483 }
484 currentRecord = nextRecord;
485 currentFromFilenameList = partitioningReader.fromFilenameList();
486 return true;
487 }
488
489
490
491
492
493
494
495
496 public String getCurrentFileOpenError() {
497 return currentFileOpenError;
498 }
499
500
501
502
503
504
505
506 private boolean presentDefaultInput() {
507 partitioningReader = new PartitioningReader(
508 new InputStreamReader(defaultInput, StandardCharsets.UTF_8),
509 jrt.getRSString());
510 currentReaderIsDefaultInput = true;
511 currentPresentedToLoop = true;
512 jrt.setFILENAMEViaJrt(jrt.toInputScalar(""));
513 beginFileState(0);
514 return true;
515 }
516
517
518
519
520
521
522
523
524
525 private void beginFileState(int argvIndex) {
526 jrt.setFNR(0L);
527 jrt.setERRNO("");
528 jrt.setARGIND(Long.valueOf(argvIndex));
529 jrt.setInputLine("");
530 }
531
532
533
534
535
536
537
538
539 private String openCurrentFile(String arg) {
540
541
542
543
544
545 if ("-".equals(arg) || JRT.isNullDeviceName(arg)) {
546 try {
547 partitioningReader = openFileListReader(arg);
548 return null;
549 } catch (IOException e) {
550 return e.getMessage();
551 }
552 }
553 File file = new File(arg);
554 if (file.isDirectory()) {
555 return "Is a directory";
556 }
557 if (!file.exists()) {
558 return "No such file or directory";
559 }
560 try {
561 partitioningReader = openFileListReader(arg);
562 return null;
563 } catch (IOException e) {
564 return JRT.describeIoReason(e);
565 }
566 }
567
568
569
570
571
572
573
574
575
576
577
578
579 private PartitioningReader openFileListReader(String arg) throws IOException {
580 boolean isDefaultInput = "-".equals(arg);
581
582
583
584 InputStream stream = isDefaultInput ? defaultInput : new FileInputStream(JRT.toPlatformFileName(arg));
585 PartitioningReader reader = new PartitioningReader(
586 new InputStreamReader(stream, StandardCharsets.UTF_8),
587 jrt.getRSString(),
588 true);
589 currentReaderIsDefaultInput = isDefaultInput;
590 return reader;
591 }
592
593
594
595
596
597
598 private void closeCurrentReaderIfFileStream() {
599 if (partitioningReader != null && partitioningReader.fromFilenameList() && !currentReaderIsDefaultInput) {
600 try {
601 partitioningReader.close();
602 } catch (IOException ignored) {
603
604 }
605 }
606 }
607
608
609
610
611
612
613
614
615
616
617 @Override
618 public void close() throws IOException {
619 closeCurrentReaderIfFileStream();
620 }
621
622
623
624
625
626
627
628 private void setFilelistVariable(String nameValue) {
629 int eqIdx = nameValue.indexOf('=');
630 if (eqIdx == 0) {
631 throw new IllegalArgumentException(
632 "Must have a non-blank variable name in a name=value variable assignment argument.");
633 }
634 String name = nameValue.substring(0, eqIdx);
635 if (name.startsWith("awk::")) {
636 name = name.substring("awk::".length());
637 }
638 String value = nameValue.substring(eqIdx + 1);
639 vm.assignVariable(name, jrt.toInputScalar(value));
640 }
641 }