View Javadoc
1   package io.jawk.jrt;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * Jawk
6    * ჻჻჻჻჻჻
7    * Copyright (C) 2006 - 2026 MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
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   * An {@link InputSource} that reads records from an {@link InputStream},
40   * traversing the {@code ARGV} array to open filenames and apply
41   * {@code name=value} variable assignments exactly like the classic AWK
42   * command-line flow.
43   * <p>
44   * When no filename arguments are present in {@code ARGV}, records are read
45   * from the supplied default {@link InputStream} (usually {@code System.in}).
46   * This class is the default {@link InputSource} used internally by the
47   * runtime when no custom source has been configured via
48   * {@code AwkSettings#setInputSource(...)}.
49   * </p>
50   * <p>
51   * API note: this type is public to allow runtime wiring between packages, but
52   * it is considered an internal implementation detail. Embedding applications
53   * should implement {@link InputSource} directly rather than depend on this
54   * class, whose behavior may change in future releases.
55   * </p>
56   *
57   * @see InputSource
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  	// ARGV traversal state
66  	private Map<Object, Object> arglistMap;
67  	private int arglistIdx;
68  	private int arglistMaxKey;
69  	private boolean hasFilenames;
70  
71  	// Current reader and record
72  	private PartitioningReader partitioningReader;
73  	private boolean currentReaderIsDefaultInput;
74  	private boolean currentFromFilenameList;
75  	private String currentRecord;
76  	private boolean currentReaderExhausted;
77  
78  	// Per-file stepping state (BEGINFILE/ENDFILE and nextfile support)
79  	private String currentFileOpenError;
80  	private boolean currentPresentedToLoop;
81  	private int lastArgumentIndex;
82  
83  	/**
84  	 * Creates a stream-backed input source.
85  	 *
86  	 * @param defaultInput the fallback input stream used when {@code ARGV}
87  	 *        contains no filename arguments (typically {@code System.in})
88  	 * @param vm the variable manager providing access to {@code ARGV} and
89  	 *        {@code ARGC}
90  	 * @param jrt the JRT instance used for string conversion and special
91  	 *        variable updates
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 	 * Returns the stream this source falls back to when {@code ARGV} holds no
102 	 * filename. It is what the run treats as its standard input, both for the
103 	 * conventional {@code -} filename and for the gawk special filename
104 	 * {@code /dev/stdin}, which the runtime binds to it when this source becomes
105 	 * the active one.
106 	 *
107 	 * @return the default input stream
108 	 */
109 	InputStream getDefaultInput() {
110 		return defaultInput;
111 	}
112 
113 	/** {@inheritDoc} */
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 	/** {@inheritDoc} */
140 	@Override
141 	public String getRecordText() {
142 		return currentRecord;
143 	}
144 
145 	/**
146 	 * Always returns {@code null} so that the runtime splits {@code $0} using
147 	 * the current field separator (FS).
148 	 *
149 	 * @return {@code null}
150 	 */
151 	@Override
152 	public List<String> getFields() {
153 		return null;
154 	}
155 
156 	/** {@inheritDoc} */
157 	@Override
158 	public boolean isFromFilenameList() {
159 		return currentFromFilenameList;
160 	}
161 
162 	/**
163 	 * Propagates a record-separator change to the active
164 	 * {@link PartitioningReader}.
165 	 *
166 	 * @param rs the new record separator value
167 	 */
168 	public void setRecordSeparator(String rs) {
169 		if (partitioningReader != null) {
170 			partitioningReader.setRecordSeparator(rs);
171 		}
172 	}
173 
174 	/**
175 	 * Returns the underlying {@link PartitioningReader} currently in use, or
176 	 * {@code null} if no reader has been opened yet.
177 	 *
178 	 * @return the active reader, or {@code null}
179 	 */
180 	PartitioningReader getPartitioningReader() {
181 		return partitioningReader;
182 	}
183 
184 	// ------------------------------------------------------------------
185 	// ARGV traversal logic (moved from JRT)
186 	// ------------------------------------------------------------------
187 
188 	/**
189 	 * Initialize internal state for traversing {@code ARGV}.
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 	 * Compute the highest numeric key present in the current {@code arglistMap}.
212 	 *
213 	 * @return the maximum integer key, or {@code 0} when the array is empty
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 	 * Determine whether {@code ARGV} contains any filename entries (arguments
228 	 * without an equals sign).
229 	 *
230 	 * @return {@code true} if at least one filename was found
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 	 * Retrieve the number of command-line arguments supplied to the script.
251 	 *
252 	 * @return {@code ARGC} converted to an {@code int}
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 	 * Return the effective upper bound for ARGV traversal, capped by the
267 	 * highest known ARGV key so that absurdly large ARGC values do not
268 	 * cause unbounded iteration over missing entries.
269 	 *
270 	 * @return the capped traversal count
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 	 * Obtain the next valid argument from {@code ARGV}, skipping
282 	 * uninitialized or empty entries.
283 	 *
284 	 * @return the next argument as an AWK string, or {@code null} if none
285 	 *         remain
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 	 * Prepare the {@link PartitioningReader} for the next input source. This
335 	 * may be a filename, a variable assignment, or standard input if no
336 	 * filenames remain.
337 	 *
338 	 * @return {@code true} if a reader was prepared, {@code false} if no more
339 	 *         input is available
340 	 * @throws IOException if an I/O error occurs while opening a file
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 				// ARGC/ARGV may have changed while evaluating assignments.
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 					// gawk clears ERRNO whenever the main input advances
358 					// successfully
359 					jrt.setERRNO("");
360 					return true;
361 				}
362 				closeCurrentReaderIfFileStream();
363 				return false;
364 			}
365 			if (arg.indexOf('=') > 0) {
366 				setFilelistVariable(arg);
367 				// Recompute bounds so ARGC changes are reflected immediately.
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 					// gawk clears ERRNO whenever the main input advances
377 					// successfully
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 				// gawk clears ERRNO whenever the main input advances
388 				// successfully
389 				jrt.setERRNO("");
390 				ready = true;
391 			}
392 		}
393 		return true;
394 	}
395 
396 	/**
397 	 * Advance to the next input file for the per-file main input loop used
398 	 * when BEGINFILE/ENDFILE rules or {@code nextfile} are present. Variable
399 	 * assignment arguments are applied along the way, exactly like
400 	 * {@link #nextRecord()} does when it crosses a file boundary.
401 	 * <p>
402 	 * On success, FILENAME, FNR, ARGIND, and ERRNO are updated and {@code $0}
403 	 * is cleared, so the BEGINFILE rules observe the new file before any
404 	 * record is read. A file that cannot be opened is still reported as
405 	 * available: ERRNO carries the error description and
406 	 * {@link #getCurrentFileOpenError()} returns it until the next advance,
407 	 * enabling gawk's non-fatal BEGINFILE error handling.
408 	 * </p>
409 	 *
410 	 * @return {@code true} when a new input file (or the initial stdin
411 	 *         stream) is current; {@code false} when input is exhausted
412 	 * @throws IOException if an I/O error occurs while traversing ARGV
413 	 */
414 	public boolean advanceToNextFile() throws IOException {
415 		initializeArgList();
416 
417 		// Adopt a reader already opened by a non-redirected getline that ran
418 		// before the per-file loop (e.g. in a BEGIN rule): it is the current
419 		// input file, already positioned after the records getline consumed.
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 				// ARGC/ARGV may have changed while evaluating assignments.
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 				// Recompute bounds so ARGC changes are reflected immediately.
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 	 * Reads the next record of the current input file only, never advancing
468 	 * to the next input file. Used by the per-file main input loop so that
469 	 * ENDFILE rules can run at each file boundary.
470 	 *
471 	 * @return {@code true} when a record is available; {@code false} at the
472 	 *         end of the current input file
473 	 * @throws IOException if an I/O error occurs
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 	 * Returns the error description recorded when the current input file
491 	 * could not be opened by {@link #advanceToNextFile()}, or {@code null}
492 	 * when the current input is readable.
493 	 *
494 	 * @return the pending open error, or {@code null}
495 	 */
496 	public String getCurrentFileOpenError() {
497 		return currentFileOpenError;
498 	}
499 
500 	/**
501 	 * Presents the default input stream (usually stdin) as the current and
502 	 * only input "file" for the per-file main input loop.
503 	 *
504 	 * @return always {@code true}
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 	 * Resets the per-file special variables observed by BEGINFILE rules: FNR
519 	 * and {@code $0} are cleared, ERRNO is emptied, and ARGIND designates the
520 	 * ARGV entry being processed.
521 	 *
522 	 * @param argvIndex the ARGV index of the new current file, or {@code 0}
523 	 *        for the default input stream
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 	 * Attempts to open the given filename as the current input file.
534 	 *
535 	 * @param arg the filename to open
536 	 * @return {@code null} on success, or a gawk-style error description when
537 	 *         the file cannot be opened for reading
538 	 */
539 	private String openCurrentFile(String arg) {
540 		// The default input stream and the null device are not files to stat:
541 		// Windows reports its null device as nonexistent, under either spelling,
542 		// so the checks below would turn an operand that reads as an empty file
543 		// into an open error — while the plain main input loop, which opens the
544 		// operand without stat'ing it first, reads it happily.
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 	 * Opens a reader for the given {@code ARGV} filename entry. The
570 	 * conventional {@code -} filename designates the default input stream
571 	 * (usually stdin), as required by POSIX; any other name is opened as a
572 	 * regular file, under the name the platform knows it by, so that
573 	 * {@code /dev/null} is the null device on Windows too.
574 	 *
575 	 * @param arg the filename from the {@code ARGV} file list
576 	 * @return a reader presenting the argument as a file-list input
577 	 * @throws IOException if the file cannot be opened for reading
578 	 */
579 	private PartitioningReader openFileListReader(String arg) throws IOException {
580 		boolean isDefaultInput = "-".equals(arg);
581 		// Open the stream before publishing the flag: if the open fails, the
582 		// still-current reader must keep its own classification, so that
583 		// cleanup does not close the caller-provided default input stream.
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 	 * Closes the current {@link PartitioningReader} if it wraps a file stream
595 	 * (not {@code defaultInput}). This prevents file-descriptor leaks when
596 	 * traversing multiple ARGV files.
597 	 */
598 	private void closeCurrentReaderIfFileStream() {
599 		if (partitioningReader != null && partitioningReader.fromFilenameList() && !currentReaderIsDefaultInput) {
600 			try {
601 				partitioningReader.close();
602 			} catch (IOException ignored) {
603 				// Best-effort close; the file is no longer needed.
604 			}
605 		}
606 	}
607 
608 	/**
609 	 * Releases any open file-backed reader held by this source.
610 	 * <p>
611 	 * This method is idempotent and safe to call multiple times. It does
612 	 * <em>not</em> close the default input stream ({@code System.in}).
613 	 * </p>
614 	 *
615 	 * @throws IOException never thrown; signature required by {@link Closeable}
616 	 */
617 	@Override
618 	public void close() throws IOException {
619 		closeCurrentReaderIfFileStream();
620 	}
621 
622 	/**
623 	 * Parse a {@code name=value} argument from the command line and assign it
624 	 * to the corresponding AWK variable.
625 	 *
626 	 * @param nameValue argument in the form {@code name=value}
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 }