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.File;
26 import java.io.FileOutputStream;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.PrintStream;
32 import java.nio.charset.StandardCharsets;
33 import java.text.DecimalFormatSymbols;
34 import java.util.AbstractList;
35 import java.util.ArrayList;
36 import java.util.Date;
37 import java.util.Enumeration;
38 import java.util.HashMap;
39 import java.util.IdentityHashMap;
40 import java.util.HashSet;
41 import java.util.IllegalFormatException;
42 import java.util.List;
43 import java.util.Locale;
44 import java.util.Map;
45 import java.util.Objects;
46 import java.util.Set;
47 import java.util.StringTokenizer;
48 import java.util.regex.Matcher;
49 import java.util.regex.Pattern;
50 import io.jawk.Awk;
51 import io.jawk.intermediate.UninitializedObject;
52 import io.jawk.intermediate.UntypedObject;
53 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 public class JRT {
90
91 private static final boolean IS_WINDOWS = System.getProperty("os.name").indexOf("Windows") >= 0;
92
93
94
95
96
97
98
99
100 private static final int DYNAMIC_PATTERN_CACHE_LIMIT = 256;
101
102
103 private static final String DEV_STDIN = "/dev/stdin";
104
105 private static final String DEV_STDOUT = "/dev/stdout";
106
107 private static final String DEV_STDERR = "/dev/stderr";
108
109 private static final String DEV_FD_0 = "/dev/fd/0";
110
111 private static final String DEV_FD_1 = "/dev/fd/1";
112
113 private static final String DEV_FD_2 = "/dev/fd/2";
114
115 private static final String DEV_NULL = "/dev/null";
116
117 private static final String WINDOWS_NULL_DEVICE = "NUL";
118
119 private final VariableManager vm;
120
121 private IoState ioState;
122
123 private AwkSink awkSink;
124
125 private PrintStream error;
126
127 private PrintStream warning = System.err;
128
129
130
131
132
133
134 private InputStream standardInput = System.in;
135
136 private boolean spawnedProcessesInheritStandardInput;
137
138
139
140
141
142 private AwkSink standardErrorSink;
143
144 private Object ignorecase = Long.valueOf(0L);
145
146 private boolean ignoreCase;
147
148
149 private boolean posix;
150
151 private Map<Pattern, Pattern> caseInsensitivePatterns;
152
153 private Map<String, Pattern> dynamicPatterns;
154
155 private Map<String, Pattern> dynamicPatternsIgnoreCase;
156
157 private final StringBuffer replaceResult = new StringBuffer();
158
159 private Object inputLine = null;
160
161 private RecordState recordState;
162
163 private InputSource activeSource;
164 private static final UninitializedObject BLANK = new UninitializedObject();
165
166
167
168
169
170
171 private final List<Object> recordFieldsView = new AbstractList<Object>() {
172 @Override
173 public Object get(int index) {
174 if (index < 0 || index >= size()) {
175 throw new IndexOutOfBoundsException("Field index " + index + ", NF " + size());
176 }
177 return jrtGetInputField(index + 1L);
178 }
179
180 @Override
181 public int size() {
182 return getNF().intValue();
183 }
184 };
185
186 private static final Integer ONE = Integer.valueOf(1);
187 private static final Integer ZERO = Integer.valueOf(0);
188 private static final Integer MINUS_ONE = Integer.valueOf(-1);
189 private String jrtInputString;
190
191
192 private long nr;
193 private long fnr;
194 private int rstart;
195 private int rlength;
196 private Object filename;
197 private Object errno;
198 private Object argind;
199 private boolean syntheticFilePresented;
200 private String fs;
201 private String rs;
202 private String ofs;
203 private String ors;
204 private String convfmt;
205 private String ofmt;
206 private String subsep;
207 private final Locale locale;
208 private final char decimalSeparator;
209
210 private static final class FileOutputState {
211
212 private final AwkSink sink;
213
214 private FileOutputState(AwkSink sinkParam) {
215 this.sink = Objects.requireNonNull(sinkParam, "sink");
216 }
217 }
218
219 private static final class CommandInputState {
220
221 private final Process process;
222 private final PartitioningReader reader;
223 private final Thread errorPump;
224
225 private CommandInputState(Process processParam, PartitioningReader readerParam, Thread errorPumpParam) {
226 this.process = Objects.requireNonNull(processParam, "process");
227 this.reader = Objects.requireNonNull(readerParam, "reader");
228 this.errorPump = errorPumpParam;
229 }
230 }
231
232 private static final class ProcessOutputState {
233
234 private final Process process;
235 private final AwkSink sink;
236 private final PrintStream processOutput;
237 private final Thread stdoutPump;
238 private final Thread stderrPump;
239
240 private ProcessOutputState(Process processParam, AwkSink sinkParam, PrintStream processOutputParam,
241 Thread stdoutPumpParam, Thread stderrPumpParam) {
242 this.process = Objects.requireNonNull(processParam, "process");
243 this.sink = Objects.requireNonNull(sinkParam, "sink");
244 this.processOutput = Objects.requireNonNull(processOutputParam, "processOutput");
245 this.stdoutPump = stdoutPumpParam;
246 this.stderrPump = stderrPumpParam;
247 }
248 }
249
250
251
252
253
254
255 private static final class FlushingAwkSink extends OutputStreamAwkSink {
256
257 private FlushingAwkSink(PrintStream printStream, Locale locale) {
258 super(printStream, locale);
259 }
260
261 @Override
262 public void print(String ofs, String ors, String ofmt, Object... values) {
263 super.print(ofs, ors, ofmt, values);
264 flush();
265 }
266
267 @Override
268 public void printf(String ofs, String ors, String ofmt, String convfmt, String format, Object... values) {
269 super.printf(ofs, ors, ofmt, convfmt, format, values);
270 flush();
271 }
272 }
273
274 private static final class IoState {
275
276 private final Map<String, PartitioningReader> fileReaders = new HashMap<String, PartitioningReader>();
277 private final Map<String, CommandInputState> commandInputs = new HashMap<String, CommandInputState>();
278 private final Map<String, FileOutputState> fileOutputs = new HashMap<String, FileOutputState>();
279 private final Map<String, ProcessOutputState> processOutputs = new HashMap<String, ProcessOutputState>();
280
281
282
283
284
285
286
287 private final Map<String, AwkSink> specialOutputs = new HashMap<String, AwkSink>();
288 }
289
290
291
292
293
294
295
296
297
298 @SuppressFBWarnings(value = {
299 "EI_EXPOSE_REP2",
300 "CT_CONSTRUCTOR_THROW" }, justification = "JRT must hold the provided runtime collaborators for later use;"
301 + " fail-fast argument validation with no security-sensitive state to protect from finalizer attacks")
302 public JRT(VariableManager vm, Locale locale, AwkSink awkSink, PrintStream error) {
303 this.vm = vm;
304 this.locale = locale == null ? Locale.US : locale;
305 this.decimalSeparator = DecimalFormatSymbols.getInstance(this.locale).getDecimalSeparator();
306 this.awkSink = Objects.requireNonNull(awkSink, "awkSink");
307 this.error = error == null ? System.err : error;
308 this.nr = 0L;
309 this.fnr = 0L;
310 this.rstart = 0;
311 this.rlength = 0;
312 this.filename = "";
313 this.fs = Awk.DEFAULT_FS;
314 this.rs = Awk.DEFAULT_RS;
315 this.ofs = Awk.DEFAULT_OFS;
316 this.ors = Awk.DEFAULT_ORS;
317 this.convfmt = Awk.DEFAULT_CONVFMT;
318 this.ofmt = Awk.DEFAULT_OFMT;
319 this.subsep = Awk.DEFAULT_SUBSEP;
320 }
321
322
323
324
325
326
327
328
329 public void setPosix(boolean posix) {
330 this.posix = posix;
331 }
332
333
334
335
336
337
338
339 public void setAwkSink(AwkSink sink) {
340 awkSink = Objects.requireNonNull(sink, "awkSink");
341 }
342
343
344
345
346
347
348
349 public void setErrorStream(PrintStream errorStream) {
350 this.error = Objects.requireNonNull(errorStream, "errorStream");
351 }
352
353
354
355
356
357
358
359
360
361 public void setWarningStream(PrintStream warningStream) {
362 this.warning = Objects.requireNonNull(warningStream, "warningStream");
363
364
365 this.standardErrorSink = null;
366 }
367
368
369
370
371
372
373
374
375
376
377
378 public void bindStandardInput(InputSource inputSource) {
379 this.standardInput = inputSource instanceof StreamInputSource ?
380 ((StreamInputSource) inputSource).getDefaultInput() :
381 System.in;
382 }
383
384
385
386
387
388
389
390 public void printWarning(String message) {
391 warning.println(message);
392 warning.flush();
393 }
394
395
396
397
398
399
400 public AwkSink getAwkSink() {
401 return awkSink;
402 }
403
404
405
406
407
408
409 public Locale getLocale() {
410 return locale;
411 }
412
413 private IoState getIoState() {
414 if (ioState == null) {
415 ioState = new IoState();
416 }
417 return ioState;
418 }
419
420
421
422
423
424
425
426
427 public static boolean isJrtManagedSpecialVariable(String name) {
428 switch (name) {
429 case "FS":
430 case "RS":
431 case "OFS":
432 case "ORS":
433 case "CONVFMT":
434 case "OFMT":
435 case "SUBSEP":
436 case "FILENAME":
437 case "NF":
438 case "NR":
439 case "FNR":
440 case "ARGC":
441 case "IGNORECASE":
442 case "ERRNO":
443 case "ARGIND":
444 return true;
445 default:
446 return false;
447 }
448 }
449
450
451
452
453
454
455
456
457
458 public static boolean isGawkOnlySpecialVariable(String name) {
459 return "ERRNO".equals(name) || "ARGIND".equals(name);
460 }
461
462
463
464
465
466
467
468 public static Map<String, Object> copySpecialVariables(Map<String, Object> variableMap) {
469 Map<String, Object> specialVariables = new HashMap<String, Object>();
470 if (variableMap == null || variableMap.isEmpty()) {
471 return specialVariables;
472 }
473 for (Map.Entry<String, Object> entry : variableMap.entrySet()) {
474 if (isJrtManagedSpecialVariable(entry.getKey())) {
475 specialVariables.put(entry.getKey(), entry.getValue());
476 }
477 }
478 return specialVariables;
479 }
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496 public void prepareForExecution(String defaultFs, String defaultRs) {
497
498 jrtCloseAll();
499
500
501 ioState = null;
502 inputLine = null;
503 recordState = null;
504 activeSource = null;
505 jrtInputString = null;
506 nr = 0L;
507 fnr = 0L;
508 rstart = 0;
509 rlength = 0;
510 filename = "";
511 errno = "";
512 argind = ZERO;
513 syntheticFilePresented = false;
514
515
516 setFS(defaultFs == null ? Awk.DEFAULT_FS : defaultFs);
517 setRS(defaultRs);
518 setOFS(Awk.DEFAULT_OFS);
519 setORS(Awk.DEFAULT_ORS);
520 setCONVFMT(Awk.DEFAULT_CONVFMT);
521 setOFMT(Awk.DEFAULT_OFMT);
522 setSUBSEP(Awk.DEFAULT_SUBSEP);
523 setFILENAMEViaJrt("");
524 setNR(0);
525 setFNR(0);
526 setRSTART(0);
527 setRLENGTH(0);
528 setIGNORECASE(Long.valueOf(0L));
529 }
530
531
532
533
534
535
536
537 public final void assignInitialVariables(Map<String, Object> initialVarMap) {
538 for (Map.Entry<String, Object> var : initialVarMap.entrySet()) {
539 String name = var.getKey();
540 Object value = var.getValue();
541 if (!applySpecialVariable(name, value)) {
542 vm.assignVariable(name, value);
543 }
544 }
545 }
546
547
548
549
550
551
552
553
554
555 public boolean applySpecialVariable(String name, Object value) {
556 switch (name) {
557 case "FS":
558 setFS(value);
559 return true;
560 case "RS":
561 setRS(value);
562 return true;
563 case "OFS":
564 setOFS(value);
565 return true;
566 case "ORS":
567 setORS(value);
568 return true;
569 case "CONVFMT":
570 setCONVFMT(value);
571 return true;
572 case "OFMT":
573 setOFMT(value);
574 return true;
575 case "SUBSEP":
576 setSUBSEP(value);
577 return true;
578 case "FILENAME":
579 setFILENAMEViaJrt(value);
580 return true;
581 case "NF":
582 setNF(value);
583 return true;
584 case "NR":
585 setNR(value);
586 return true;
587 case "FNR":
588 setFNR(value);
589 return true;
590 case "ARGC":
591 setARGC(value);
592 return true;
593 case "IGNORECASE":
594 setIGNORECASE(value);
595 return true;
596 case "ERRNO":
597 setERRNO(value);
598 return true;
599 case "ARGIND":
600 setARGIND(value);
601 return true;
602 default:
603 return false;
604 }
605 }
606
607
608
609
610
611
612
613
614
615
616 public final void applySpecialVariables(Map<String, Object> variableMap) {
617 if (variableMap == null || variableMap.isEmpty()) {
618 return;
619 }
620 for (Map.Entry<String, Object> var : variableMap.entrySet()) {
621
622
623 applySpecialVariable(var.getKey(), var.getValue());
624 }
625 }
626
627
628
629
630
631
632
633
634
635
636 public static void assignEnvironmentVariables(AssocArray aa) {
637 Map<String, String> env = System.getenv();
638 for (Map.Entry<String, String> var : env.entrySet()) {
639 aa.put(var.getKey(), new StrNum(var.getValue()));
640 }
641 }
642
643
644
645
646
647
648
649
650 public static Map<Object, Object> createAwkMap(boolean sortedArrayKeys) {
651 return AssocArray.create(sortedArrayKeys);
652 }
653
654
655
656
657
658
659
660
661
662
663 public static boolean containsAwkKey(Map<Object, Object> map, Object key) {
664 if (map instanceof AssocArray) {
665 return ((AssocArray) map).isIn(key);
666 }
667 return map.containsKey(key);
668 }
669
670
671
672
673
674
675
676
677
678
679
680
681 public static Object getAssocArrayValue(Map<Object, Object> map, Object key) {
682 if (map instanceof AssocArray) {
683 return map.get(key);
684 }
685 Object value = map.get(key);
686 return value != null ? value : BLANK;
687 }
688
689
690
691
692
693
694
695
696
697
698
699
700 public String getAwkStringEntry(Map<Object, Object> map, Object key) {
701 if (!containsAwkKey(map, key)) {
702 return null;
703 }
704 return toAwkString(getAssocArrayValue(map, key));
705 }
706
707
708
709
710
711
712
713
714 public String toAwkString(Object o) {
715 return AwkPrintf.toAwkString(o, this.convfmt, this.locale);
716 }
717
718
719
720
721
722
723
724
725
726
727
728
729 public boolean compare(Object o1, Object o2, int mode) {
730 return compare2(o1, o2, mode, isIgnoreCase(), this.convfmt, this.locale);
731 }
732
733
734
735
736
737
738
739 public static double toDouble(final Object o) {
740 if (o == null) {
741 return 0;
742 }
743
744 if (o instanceof Number) {
745 return ((Number) o).doubleValue();
746 }
747
748 if (o instanceof Character) {
749 return (double) ((Character) o).charValue();
750 }
751
752 if (o instanceof StrNum) {
753 StrNum strNum = (StrNum) o;
754 if (strNum.isNumber()) {
755 return strNum.doubleValue();
756 }
757 }
758
759
760
761 String s = o.toString();
762 int length = s.length();
763 int start = 0;
764 while (start < length && Character.isWhitespace(s.charAt(start))) {
765 start++;
766 }
767
768
769
770 if (start < length) {
771 char sign = s.charAt(start);
772 if ((sign == '+' || sign == '-') && isBlankToEnd(s, start + 4)) {
773 if (s.regionMatches(true, start + 1, "inf", 0, 3)) {
774 return sign == '-' ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
775 }
776 if (s.regionMatches(true, start + 1, "nan", 0, 3)) {
777 return Double.NaN;
778 }
779 }
780 }
781 int end = numericPrefixEnd(s, start, '.');
782 if (end == start) {
783 return 0;
784 }
785
786 return Double.parseDouble(start == 0 && end == length ? s : s.substring(start, end));
787 }
788
789
790
791
792
793
794
795
796 public static boolean isActuallyLong(double d) {
797 double r = Math.rint(d);
798 return Math.abs(d - r) < Math.ulp(d);
799 }
800
801
802 private static final double TWO_POW_63 = 9.223372036854775808e18;
803
804
805
806
807
808
809
810
811
812
813
814
815 public static Object toScalarNumber(double d) {
816 if (isActuallyLong(d)) {
817 double rounded = Math.rint(d);
818 if (rounded >= -TWO_POW_63 && rounded < TWO_POW_63) {
819 return Long.valueOf((long) rounded);
820 }
821 }
822 return Double.valueOf(d);
823 }
824
825
826
827
828
829
830
831
832
833 public static Object truncateToScalar(double d) {
834 if (Double.isNaN(d) || Double.isInfinite(d)) {
835 return Double.valueOf(d);
836 }
837 return toScalarNumber(d < 0 ? Math.ceil(d) : Math.floor(d));
838 }
839
840
841
842
843
844
845
846
847
848
849 private static boolean isExactIntegral(Object o) {
850 return o instanceof Long || o instanceof Integer;
851 }
852
853
854
855
856
857
858
859
860
861
862
863 public static Object add(Object o1, Object o2) {
864 if (isExactIntegral(o1) && isExactIntegral(o2)) {
865 try {
866 return Math.addExact(((Number) o1).longValue(), ((Number) o2).longValue());
867 } catch (ArithmeticException overflow) {
868 return toDouble(o1) + toDouble(o2);
869 }
870 }
871 return toDouble(o1) + toDouble(o2);
872 }
873
874
875
876
877
878
879
880
881
882
883
884 public static Object subtract(Object o1, Object o2) {
885 if (isExactIntegral(o1) && isExactIntegral(o2)) {
886 try {
887 return Math.subtractExact(((Number) o1).longValue(), ((Number) o2).longValue());
888 } catch (ArithmeticException overflow) {
889 return toDouble(o1) - toDouble(o2);
890 }
891 }
892 return toDouble(o1) - toDouble(o2);
893 }
894
895
896
897
898
899
900
901
902
903
904
905 public static Object multiply(Object o1, Object o2) {
906 if (isExactIntegral(o1) && isExactIntegral(o2)) {
907 try {
908 return Math.multiplyExact(((Number) o1).longValue(), ((Number) o2).longValue());
909 } catch (ArithmeticException overflow) {
910 return toDouble(o1) * toDouble(o2);
911 }
912 }
913 return toDouble(o1) * toDouble(o2);
914 }
915
916
917
918
919
920
921
922
923
924
925
926
927 public static Object divide(Object o1, Object o2) {
928 if (isExactIntegral(o1) && isExactIntegral(o2)) {
929 long l1 = ((Number) o1).longValue();
930 long l2 = ((Number) o2).longValue();
931 if (l2 != 0 && l1 % l2 == 0 && (l1 != Long.MIN_VALUE || l2 != -1)) {
932 return l1 / l2;
933 }
934 }
935 return toDouble(o1) / toDouble(o2);
936 }
937
938
939
940
941
942
943
944
945
946
947
948 public static Object mod(Object o1, Object o2) {
949 if (isExactIntegral(o1) && isExactIntegral(o2)) {
950 long l2 = ((Number) o2).longValue();
951 if (l2 != 0) {
952 return ((Number) o1).longValue() % l2;
953 }
954 }
955 return toDouble(o1) % toDouble(o2);
956 }
957
958
959
960
961
962
963
964
965
966 public static Object pow(Object o1, Object o2) {
967 return Math.pow(toDouble(o1), toDouble(o2));
968 }
969
970
971
972
973
974
975
976
977
978
979 public static Object negate(Object o) {
980 if (isExactIntegral(o)) {
981 long l = ((Number) o).longValue();
982 if (l != Long.MIN_VALUE) {
983 return -l;
984 }
985 }
986 return -toDouble(o);
987 }
988
989
990
991
992
993
994
995 public static long toLong(final Object o) {
996 if (o == null) {
997 return 0;
998 }
999
1000 if (o instanceof Number) {
1001 return ((Number) o).longValue();
1002 }
1003
1004 if (o instanceof Character) {
1005 return (long) ((Character) o).charValue();
1006 }
1007
1008
1009
1010
1011
1012
1013
1014 String s = o.toString();
1015 int length = s.length();
1016 int index = 0;
1017 while (index < length && Character.isWhitespace(s.charAt(index))) {
1018 index++;
1019 }
1020 boolean negative = index < length && s.charAt(index) == '-';
1021 if (index < length && (negative || s.charAt(index) == '+')) {
1022 index++;
1023 }
1024 int firstDigit = index;
1025 long value = 0;
1026 while (index < length && isAsciiDigit(s.charAt(index))) {
1027 int digit = s.charAt(index) - '0';
1028 if (value > (Long.MAX_VALUE - digit) / 10) {
1029
1030
1031 break;
1032 }
1033 value = value * 10 + digit;
1034 index++;
1035 }
1036 if (index > firstDigit && !continuesNumber(s, index)) {
1037 return negative ? -value : value;
1038 }
1039 return (long) toDouble(o);
1040 }
1041
1042
1043
1044
1045
1046 private static boolean isBlankToEnd(String value, int index) {
1047 for (int i = index; i < value.length(); i++) {
1048 if (!Character.isWhitespace(value.charAt(i))) {
1049 return false;
1050 }
1051 }
1052 return true;
1053 }
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063 private static boolean continuesNumber(String value, int index) {
1064 if (index >= value.length()) {
1065 return false;
1066 }
1067 char c = value.charAt(index);
1068 if (isAsciiDigit(c) || c == '.') {
1069 return true;
1070 }
1071 if (c != 'e' && c != 'E') {
1072 return false;
1073 }
1074 int afterExponent = index + 1;
1075 if (afterExponent < value.length() && (value.charAt(afterExponent) == '+' || value.charAt(afterExponent) == '-')) {
1076 afterExponent++;
1077 }
1078 return afterExponent < value.length() && isAsciiDigit(value.charAt(afterExponent));
1079 }
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089 public static long parseFieldNumber(Object obj) {
1090 long num = toLong(obj);
1091 if (num < 0) {
1092 throw new AwkRuntimeException("Field $(" + obj.toString() + ") is incorrect.");
1093 }
1094 return num;
1095 }
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112 public static boolean compare2(Object o1, Object o2, int mode) {
1113 return compare2(o1, o2, mode, false);
1114 }
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128 public static boolean compare2(Object o1, Object o2, int mode, boolean ignoreCase) {
1129
1130
1131
1132
1133 return compare2(o1, o2, mode, ignoreCase, null, Locale.US);
1134 }
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157 private static boolean compare2(Object o1, Object o2, int mode, boolean ignoreCase, String convfmt, Locale locale) {
1158 if (o1 instanceof Number && o2 instanceof Number) {
1159 if (isExactIntegral(o1) && isExactIntegral(o2)) {
1160
1161
1162 int comparison = Long.compare(((Number) o1).longValue(), ((Number) o2).longValue());
1163 if (mode == 0) {
1164 return comparison == 0;
1165 }
1166 return mode < 0 ? comparison < 0 : comparison > 0;
1167 }
1168 return compareNumbers(((Number) o1).doubleValue(), ((Number) o2).doubleValue(), mode);
1169 }
1170
1171 if (o1 instanceof UninitializedObject) {
1172 if (isBlankOrZero(o2, convfmt, locale)) {
1173 return mode == 0;
1174 } else {
1175 return mode < 0;
1176 }
1177 }
1178 if (o2 instanceof UninitializedObject) {
1179 if (isBlankOrZero(o1, convfmt, locale)) {
1180 return mode == 0;
1181 } else {
1182 return mode > 0;
1183 }
1184 }
1185
1186 if (isNumericComparisonOperand(o1) && isNumericComparisonOperand(o2)) {
1187 return compareNumbers(getDoubleForComparison(o1), getDoubleForComparison(o2), mode);
1188 }
1189
1190
1191
1192
1193 String o1String = AwkPrintf.toAwkString(o1, convfmt, locale);
1194 String o2String = AwkPrintf.toAwkString(o2, convfmt, locale);
1195
1196 if (mode == 0) {
1197 return ignoreCase ? o1String.equalsIgnoreCase(o2String) : o1String.equals(o2String);
1198 }
1199 int comparison = ignoreCase ? o1String.compareToIgnoreCase(o2String) : o1String.compareTo(o2String);
1200 return mode < 0 ? comparison < 0 : comparison > 0;
1201 }
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212 public int index(String haystack, String needle) {
1213 if (!ignoreCase) {
1214 return haystack.indexOf(needle) + 1;
1215 }
1216 int max = haystack.length() - needle.length();
1217 for (int i = 0; i <= max; i++) {
1218 if (haystack.regionMatches(true, i, needle, 0, needle.length())) {
1219 return i + 1;
1220 }
1221 }
1222 return 0;
1223 }
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236 private static boolean isBlankOrZero(Object value, String convfmt, Locale locale) {
1237 if (value instanceof UninitializedObject) {
1238 return true;
1239 }
1240 if (value instanceof Number) {
1241 return ((Number) value).doubleValue() == 0.0D;
1242 }
1243 if (value instanceof StrNum && ((StrNum) value).isNumber()) {
1244 return ((StrNum) value).doubleValue() == 0.0D;
1245 }
1246 String stringValue = AwkPrintf.toAwkString(value, convfmt, locale);
1247 return "".equals(stringValue) || "0".equals(stringValue);
1248 }
1249
1250 private static boolean isNumericComparisonOperand(Object value) {
1251 return value instanceof Number || value instanceof StrNum && ((StrNum) value).isNumber();
1252 }
1253
1254 private static double getDoubleForComparison(Object value) {
1255 if (value instanceof Number) {
1256 return ((Number) value).doubleValue();
1257 }
1258 return ((StrNum) value).doubleValue();
1259 }
1260
1261 private static boolean compareNumbers(double o1Number, double o2Number, int mode) {
1262 if (mode < 0) {
1263 return o1Number < o2Number;
1264 } else if (mode == 0) {
1265 return o1Number == o2Number;
1266 } else {
1267 return o1Number > o2Number;
1268 }
1269 }
1270
1271
1272
1273
1274
1275
1276
1277 public static Object toJavaScalar(Object value) {
1278 if (value instanceof StrNum) {
1279 return value.toString();
1280 }
1281 if (value instanceof Double || value instanceof Float) {
1282 return toScalarNumber(((Number) value).doubleValue());
1283 }
1284 return value;
1285 }
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296 public boolean isParseableNumber(String value) {
1297 return isParseableNumber(value, decimalSeparator);
1298 }
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312 public static Object untypedToBlank(Object value) {
1313 return value instanceof UntypedObject ? BLANK : value;
1314 }
1315
1316 static boolean isParseableNumber(String value, char decimalSeparator) {
1317 int length = value.length();
1318 int start = 0;
1319 while (start < length && Character.isWhitespace(value.charAt(start))) {
1320 start++;
1321 }
1322 int end = numericPrefixEnd(value, start, decimalSeparator);
1323 return end > start && isBlankToEnd(value, end);
1324 }
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335 static String trimWhitespace(String value) {
1336 int end = value.length();
1337 int start = 0;
1338 while (start < end && Character.isWhitespace(value.charAt(start))) {
1339 start++;
1340 }
1341 while (end > start && Character.isWhitespace(value.charAt(end - 1))) {
1342 end--;
1343 }
1344 return start == 0 && end == value.length() ? value : value.substring(start, end);
1345 }
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367 private static int numericPrefixEnd(String value, int start, char decimalSeparator) {
1368 int length = value.length();
1369 int index = start;
1370
1371 if (index < length && (value.charAt(index) == '+' || value.charAt(index) == '-')) {
1372 index++;
1373 }
1374
1375 boolean digitFound = false;
1376 while (index < length && isAsciiDigit(value.charAt(index))) {
1377 index++;
1378 digitFound = true;
1379 }
1380
1381 if (index < length && value.charAt(index) == decimalSeparator) {
1382 index++;
1383 while (index < length && isAsciiDigit(value.charAt(index))) {
1384 index++;
1385 digitFound = true;
1386 }
1387 }
1388
1389 if (!digitFound) {
1390 return start;
1391 }
1392
1393 if (index < length && (value.charAt(index) == 'e' || value.charAt(index) == 'E')) {
1394 int afterExponent = index + 1;
1395 if (afterExponent < length && (value.charAt(afterExponent) == '+' || value.charAt(afterExponent) == '-')) {
1396 afterExponent++;
1397 }
1398 int exponentDigits = afterExponent;
1399 while (afterExponent < length && isAsciiDigit(value.charAt(afterExponent))) {
1400 afterExponent++;
1401 }
1402
1403
1404 if (afterExponent > exponentDigits) {
1405 index = afterExponent;
1406 }
1407 }
1408
1409 return index;
1410 }
1411
1412 private static boolean isAsciiDigit(char c) {
1413 return c >= '0' && c <= '9';
1414 }
1415
1416 static String normalizeNumberForComparison(String value, char decimalSeparator) {
1417 return decimalSeparator == '.' ? value : value.replace(decimalSeparator, '.');
1418 }
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431 public static Object inc(Object o) {
1432 if (isExactIntegral(o)) {
1433 long l = ((Number) o).longValue();
1434 if (l != Long.MAX_VALUE) {
1435 return l + 1;
1436 }
1437 }
1438 return toDouble(o) + 1;
1439 }
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452 public static Object dec(Object o) {
1453 if (isExactIntegral(o)) {
1454 long l = ((Number) o).longValue();
1455 if (l != Long.MIN_VALUE) {
1456 return l - 1;
1457 }
1458 }
1459 return toDouble(o) - 1;
1460 }
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479 public final boolean toBoolean(Object o) {
1480 boolean val;
1481 if (o instanceof Integer) {
1482 val = ((Integer) o).intValue() != 0;
1483 } else if (o instanceof Long) {
1484 val = ((Long) o).longValue() != 0;
1485 } else if (o instanceof Double) {
1486 val = ((Double) o).doubleValue() != 0;
1487 } else if (o instanceof StrNum) {
1488 StrNum strNum = (StrNum) o;
1489 val = strNum.isNumber() ? strNum.doubleValue() != 0 : strNum.toString().length() > 0;
1490 } else if (o instanceof String) {
1491 val = (o.toString().length() > 0);
1492 } else if (o instanceof UninitializedObject) {
1493 val = false;
1494 } else if (o instanceof Pattern) {
1495
1496 Pattern pattern = caseAwarePattern((Pattern) o);
1497 Object inputField = jrtGetInputField(0);
1498 String s = inputField instanceof UninitializedObject ? "" : inputField.toString();
1499 Matcher matcher = pattern.matcher(s);
1500 val = matcher.find();
1501 } else {
1502 throw new Error("Unknown operand_stack type: " + o.getClass() + " for value " + o);
1503 }
1504 return val;
1505 }
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516 public int split(Object array, Object string) {
1517 return splitWorker(new StringTokenizer(toAwkString(string)), toArrayMap(array));
1518 }
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532 public int split(Object fieldSeparator, Object array, Object string) {
1533 return splitWorker(splitTokenizer(toAwkString(string), fieldSeparator), toArrayMap(array));
1534 }
1535
1536 private static Map<Object, Object> toArrayMap(Object array) {
1537 if (!(array instanceof Map)) {
1538 throw new IllegalArgumentException("split target must be a Map.");
1539 }
1540 @SuppressWarnings("unchecked")
1541 Map<Object, Object> arrayMap = (Map<Object, Object>) array;
1542 return arrayMap;
1543 }
1544
1545 private int splitWorker(Enumeration<Object> e, Map<Object, Object> array) {
1546 int cnt = 0;
1547 array.clear();
1548 while (e.hasMoreElements()) {
1549 Object value = e.nextElement();
1550 array.put(Long.valueOf(++cnt), toInputScalar(value));
1551 }
1552 array.put(0L, Long.valueOf(cnt));
1553 return cnt;
1554 }
1555
1556
1557
1558
1559
1560
1561
1562
1563 public PartitioningReader getPartitioningReader() {
1564 if (activeSource instanceof StreamInputSource) {
1565 return ((StreamInputSource) activeSource).getPartitioningReader();
1566 }
1567 return null;
1568 }
1569
1570
1571
1572
1573
1574
1575
1576
1577 public Object getInputLine() {
1578 if (recordState != null) {
1579 return recordState.getField(0);
1580 }
1581 return inputLine;
1582 }
1583
1584
1585
1586
1587
1588
1589
1590 public Integer getNF() {
1591 if (recordState == null) {
1592 return Integer.valueOf(0);
1593 }
1594 return Integer.valueOf(recordState.getNF());
1595 }
1596
1597
1598
1599
1600
1601
1602 public void setNF(Object nfObject) {
1603 jrtSetNF(nfObject);
1604 }
1605
1606
1607
1608
1609
1610
1611 public Long getNR() {
1612 return Long.valueOf(nr);
1613 }
1614
1615
1616
1617
1618
1619
1620 public void setNR(Object value) {
1621 this.nr = toLong(value);
1622 }
1623
1624
1625
1626
1627
1628
1629 public Long getFNR() {
1630 return Long.valueOf(fnr);
1631 }
1632
1633
1634
1635
1636
1637
1638 public void setFNR(Object value) {
1639 this.fnr = toLong(value);
1640 }
1641
1642
1643
1644
1645
1646
1647 public Object getFSVar() {
1648 return fs;
1649 }
1650
1651
1652
1653
1654
1655
1656 public String getFSString() {
1657 return fs;
1658 }
1659
1660
1661
1662
1663
1664
1665 public void setFS(Object value) {
1666 this.fs = value == null ? "" : value.toString();
1667 }
1668
1669
1670
1671
1672
1673
1674
1675 public void setIGNORECASE(Object value) {
1676 this.ignorecase = value == null ? Long.valueOf(0L) : value;
1677
1678
1679 this.ignoreCase = toBoolean(this.ignorecase);
1680 }
1681
1682
1683
1684
1685
1686
1687 public Object getIGNORECASEVar() {
1688 return ignorecase;
1689 }
1690
1691
1692
1693
1694
1695
1696
1697
1698 public boolean isIgnoreCase() {
1699 return ignoreCase;
1700 }
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710 public int regexpFlags() {
1711 return ignoreCase ? Pattern.CASE_INSENSITIVE : 0;
1712 }
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724 public int replaceFirst(String orig, String repl, String ere) {
1725 return replace(orig, repl, ere, false);
1726 }
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738 public int replaceAll(String orig, String repl, String ere) {
1739 return replace(orig, repl, ere, true);
1740 }
1741
1742 private int replace(String orig, String repl, String ere, boolean global) {
1743 replaceResult.setLength(0);
1744 String preparedReplacement = prepareSubReplacement(repl, posix);
1745 Matcher matcher = dynamicPattern(ere).matcher(orig);
1746 int count = 0;
1747 while (matcher.find()) {
1748 count++;
1749 matcher.appendReplacement(replaceResult, preparedReplacement);
1750 if (!global) {
1751 break;
1752 }
1753 }
1754 matcher.appendTail(replaceResult);
1755 return count;
1756 }
1757
1758
1759
1760
1761
1762
1763
1764 public String getReplaceResult() {
1765 return replaceResult.toString();
1766 }
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777 public boolean matches(String text, Object regexp) {
1778 if (regexp instanceof Pattern) {
1779
1780 return caseAwarePattern((Pattern) regexp).matcher(text).find();
1781 }
1782 return dynamicPattern(toAwkString(regexp)).matcher(text).find();
1783 }
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793 public int matchPosition(String s, String ere) {
1794 Matcher matcher = dynamicPattern(ere).matcher(s);
1795 if (matcher.find()) {
1796 int start = matcher.start() + 1;
1797 setRSTART(start);
1798 setRLENGTH(matcher.end() - matcher.start());
1799 return start;
1800 }
1801 setRSTART(0);
1802 setRLENGTH(-1);
1803 return 0;
1804 }
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817 public Enumeration<Object> splitTokenizer(String input, Object separator) {
1818 if (separator instanceof Pattern) {
1819 return new RegexTokenizer(input, caseAwarePattern((Pattern) separator));
1820 }
1821 String fsString = toAwkString(separator);
1822 if (fsString.equals(" ")) {
1823 return new StringTokenizer(input);
1824 }
1825 if (fsString.isEmpty()) {
1826 return new CharacterTokenizer(input);
1827 }
1828 if (fsString.length() == 1) {
1829 char fsChar = fsString.charAt(0);
1830 if (ignoreCase && Character.isLetter(fsChar)) {
1831
1832
1833 return new RegexTokenizer(input, dynamicPattern(fsString));
1834 }
1835 return new SingleCharacterTokenizer(input, fsChar);
1836 }
1837 return new RegexTokenizer(input, dynamicPattern(fsString));
1838 }
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854 public static String prepareSubReplacement(String awkRepl, boolean posix) {
1855 if (awkRepl == null) {
1856 return "";
1857 }
1858 if (awkRepl.indexOf('\\') == -1 && awkRepl.indexOf('$') == -1 && awkRepl.indexOf('&') == -1) {
1859 return awkRepl;
1860 }
1861 StringBuilder javaRepl = new StringBuilder();
1862 int length = awkRepl.length();
1863 for (int i = 0; i < length; i++) {
1864 char c = awkRepl.charAt(i);
1865 if (c != '\\') {
1866 appendLiteral(javaRepl, c);
1867 continue;
1868 }
1869 char next = i + 1 < length ? awkRepl.charAt(i + 1) : 0;
1870 if (next == '&') {
1871
1872 javaRepl.append('&');
1873 i++;
1874 } else if (posix) {
1875
1876
1877 javaRepl.append("\\\\");
1878 if (next == '\\') {
1879 i++;
1880 }
1881 } else if (awkRepl.startsWith("\\\\\\&", i)) {
1882
1883 javaRepl.append("\\\\&");
1884 i += 3;
1885 } else if (awkRepl.startsWith("\\\\\\\\", i)) {
1886
1887 javaRepl.append("\\\\\\\\");
1888 i += 3;
1889 } else if (awkRepl.startsWith("\\\\&", i)) {
1890
1891 javaRepl.append("\\\\$0");
1892 i += 2;
1893 } else {
1894
1895
1896 javaRepl.append("\\\\");
1897 }
1898 }
1899 return javaRepl.toString();
1900 }
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915 public static String prepareGensubReplacement(String awkRepl, int maxGroup) {
1916 if (awkRepl == null) {
1917 return "";
1918 }
1919 if (awkRepl.indexOf('\\') == -1 && awkRepl.indexOf('$') == -1 && awkRepl.indexOf('&') == -1) {
1920 return awkRepl;
1921 }
1922 StringBuilder javaRepl = new StringBuilder();
1923 int length = awkRepl.length();
1924 for (int i = 0; i < length; i++) {
1925 char c = awkRepl.charAt(i);
1926 if (c != '\\') {
1927 appendLiteral(javaRepl, c);
1928 } else if (i == length - 1) {
1929
1930
1931 javaRepl.append("\\\\");
1932 } else {
1933 c = awkRepl.charAt(++i);
1934 if (c == '&') {
1935 javaRepl.append('&');
1936 } else if (c == '\\') {
1937 javaRepl.append("\\\\");
1938 } else if (Character.isDigit(c)) {
1939 if (c - '0' <= maxGroup) {
1940 javaRepl.append('$').append(c);
1941 }
1942 } else {
1943 appendLiteral(javaRepl, c);
1944 }
1945 }
1946 }
1947 return javaRepl.toString();
1948 }
1949
1950
1951
1952
1953
1954
1955 private static void appendLiteral(StringBuilder javaRepl, char c) {
1956 if (c == '$') {
1957 javaRepl.append("\\$");
1958 } else if (c == '&') {
1959 javaRepl.append("$0");
1960 } else {
1961 javaRepl.append(c);
1962 }
1963 }
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975 public Pattern caseAwarePattern(Pattern pattern) {
1976 if (!ignoreCase || (pattern.flags() & Pattern.CASE_INSENSITIVE) != 0) {
1977 return pattern;
1978 }
1979 if (caseInsensitivePatterns == null) {
1980 caseInsensitivePatterns = new IdentityHashMap<Pattern, Pattern>();
1981 }
1982 return caseInsensitivePatterns
1983 .computeIfAbsent(pattern, base -> Pattern.compile(base.pattern(), base.flags() | Pattern.CASE_INSENSITIVE));
1984 }
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001 public Pattern dynamicPattern(String ere) {
2002 if (dynamicPatterns == null) {
2003 dynamicPatterns = new HashMap<String, Pattern>();
2004 dynamicPatternsIgnoreCase = new HashMap<String, Pattern>();
2005 }
2006 Map<String, Pattern> cache = ignoreCase ? dynamicPatternsIgnoreCase : dynamicPatterns;
2007 Pattern pattern = cache.get(ere);
2008 if (pattern == null) {
2009 if (cache.size() >= DYNAMIC_PATTERN_CACHE_LIMIT) {
2010 cache.clear();
2011 }
2012 pattern = Pattern.compile(ere, regexpFlags());
2013 cache.put(ere, pattern);
2014 }
2015 return pattern;
2016 }
2017
2018
2019
2020
2021
2022
2023 public Object getRSVar() {
2024 return rs;
2025 }
2026
2027
2028
2029
2030
2031
2032 public String getRSString() {
2033 return rs;
2034 }
2035
2036
2037
2038
2039
2040
2041 public void setRS(Object value) {
2042 this.rs = value == null ? "" : value.toString();
2043 applyRS(this.rs);
2044 }
2045
2046
2047
2048
2049
2050
2051 public Object getOFSVar() {
2052 return ofs;
2053 }
2054
2055
2056
2057
2058
2059
2060 public String getOFSString() {
2061 return ofs;
2062 }
2063
2064
2065
2066
2067
2068
2069 public void setOFS(Object value) {
2070 this.ofs = value == null ? "" : value.toString();
2071 }
2072
2073
2074
2075
2076
2077
2078 public Object getORSVar() {
2079 return ors;
2080 }
2081
2082
2083
2084
2085
2086
2087 public String getORSString() {
2088 return ors;
2089 }
2090
2091
2092
2093
2094
2095
2096 public void setORS(Object value) {
2097 this.ors = value == null ? "" : value.toString();
2098 }
2099
2100
2101
2102
2103
2104
2105 public Integer getRSTART() {
2106 return Integer.valueOf(rstart);
2107 }
2108
2109
2110
2111
2112
2113
2114 public void setRSTART(Object value) {
2115 this.rstart = (int) toLong(value);
2116 }
2117
2118
2119
2120
2121
2122
2123 public Integer getRLENGTH() {
2124 return Integer.valueOf(rlength);
2125 }
2126
2127
2128
2129
2130
2131
2132 public void setRLENGTH(Object value) {
2133 this.rlength = (int) toLong(value);
2134 }
2135
2136
2137
2138
2139
2140
2141 public Object getFILENAME() {
2142 return filename == null ? "" : filename;
2143 }
2144
2145
2146
2147
2148
2149
2150 public void setFILENAMEViaJrt(Object name) {
2151 this.filename = normalizeRecordValue(name);
2152 }
2153
2154
2155
2156
2157
2158
2159 public Object getERRNO() {
2160 return errno == null ? "" : errno;
2161 }
2162
2163
2164
2165
2166
2167
2168 public void setERRNO(Object value) {
2169 this.errno = normalizeRecordValue(value);
2170 }
2171
2172
2173
2174
2175
2176
2177 public Object getARGIND() {
2178 return argind == null ? ZERO : argind;
2179 }
2180
2181
2182
2183
2184
2185
2186 public void setARGIND(Object value) {
2187 this.argind = normalizeRecordValue(value);
2188 }
2189
2190
2191
2192
2193
2194
2195 public Object getSUBSEPVar() {
2196 return subsep;
2197 }
2198
2199
2200
2201
2202
2203
2204 public String getSUBSEPString() {
2205 return subsep;
2206 }
2207
2208
2209
2210
2211
2212
2213 public void setSUBSEP(Object value) {
2214 this.subsep = value == null ? "" : value.toString();
2215 }
2216
2217
2218
2219
2220
2221
2222 public Object getCONVFMTVar() {
2223 return convfmt;
2224 }
2225
2226
2227
2228
2229
2230
2231 public String getCONVFMTString() {
2232 return convfmt;
2233 }
2234
2235
2236
2237
2238
2239
2240 public void setCONVFMT(Object value) {
2241 this.convfmt = value == null ? "" : value.toString();
2242 }
2243
2244
2245
2246
2247
2248
2249 public String getOFMTString() {
2250 return ofmt;
2251 }
2252
2253
2254
2255
2256
2257
2258 public void setOFMT(Object value) {
2259 this.ofmt = value == null ? "" : value.toString();
2260 }
2261
2262
2263
2264
2265
2266
2267 public Object getARGCVar() {
2268 return vm.getARGC();
2269 }
2270
2271
2272
2273
2274
2275
2276 public void setARGC(Object value) {
2277 vm.assignVariable("ARGC", value);
2278 }
2279
2280
2281
2282
2283
2284
2285
2286
2287 public void setInputLine(Object inputLineParam) {
2288 Object inputValue = normalizeRecordValue(inputLineParam);
2289 this.inputLine = inputValue;
2290 recordState = new RecordState(inputValue, null);
2291 }
2292
2293
2294
2295
2296
2297
2298
2299 public Object toInputScalar(Object value) {
2300 if (value instanceof String) {
2301 return new StrNum((String) value, decimalSeparator);
2302 }
2303 if (value instanceof StrNum) {
2304 return value;
2305 }
2306 if (value == null || value instanceof UninitializedObject) {
2307 return new StrNum("", decimalSeparator);
2308 }
2309 return new StrNum(value.toString(), decimalSeparator);
2310 }
2311
2312 private static Object normalizeRecordValue(Object value) {
2313 if (value == null || value instanceof UninitializedObject) {
2314 return "";
2315 }
2316 return value;
2317 }
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329 public boolean consumeInput(final InputSource source) throws IOException {
2330 Objects.requireNonNull(source, "source");
2331 activeSource = source;
2332 if (!source.nextRecord()) {
2333 return false;
2334 }
2335
2336 bindConsumedRecord(source);
2337 return true;
2338 }
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357 public boolean consumeCurrentFileInput(final InputSource source) throws IOException {
2358 Objects.requireNonNull(source, "source");
2359 if (!(source instanceof StreamInputSource)) {
2360
2361 return consumeInput(source);
2362 }
2363 StreamInputSource streamSource = (StreamInputSource) source;
2364 throwIfCurrentFileUnopened(streamSource);
2365 activeSource = source;
2366 if (!streamSource.nextRecordInCurrentFile()) {
2367 return false;
2368 }
2369 bindConsumedRecord(source);
2370 return true;
2371 }
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387 public Object consumeCurrentFileInputToTarget(final InputSource source) throws IOException {
2388 Objects.requireNonNull(source, "source");
2389 if (!(source instanceof StreamInputSource)) {
2390
2391 return consumeInputToTarget(source);
2392 }
2393 StreamInputSource streamSource = (StreamInputSource) source;
2394 throwIfCurrentFileUnopened(streamSource);
2395 activeSource = source;
2396 materializeCurrentRecord();
2397 if (!streamSource.nextRecordInCurrentFile()) {
2398 return null;
2399 }
2400
2401 RecordState inputState = new RecordState(source);
2402 this.nr++;
2403 if (countsTowardFNR(source)) {
2404 this.fnr++;
2405 }
2406 return new StrNum(inputState.getRecordText(), decimalSeparator);
2407 }
2408
2409
2410
2411
2412
2413
2414
2415
2416 private void throwIfCurrentFileUnopened(StreamInputSource streamSource) {
2417 String openError = streamSource.getCurrentFileOpenError();
2418 if (openError != null) {
2419 throw new AwkRuntimeException("cannot open file `" + toAwkString(getFILENAME()) + "' for reading: " + openError);
2420 }
2421 }
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437 public boolean advanceToNextFile(final InputSource source) throws IOException {
2438 Objects.requireNonNull(source, "source");
2439 if (source instanceof StreamInputSource) {
2440 return ((StreamInputSource) source).advanceToNextFile();
2441 }
2442
2443 if (syntheticFilePresented) {
2444 return false;
2445 }
2446 syntheticFilePresented = true;
2447 return true;
2448 }
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458 public boolean hasPendingInputFileError(InputSource source) {
2459 return source instanceof StreamInputSource && ((StreamInputSource) source).getCurrentFileOpenError() != null;
2460 }
2461
2462
2463
2464
2465
2466
2467
2468 private void bindConsumedRecord(InputSource source) {
2469 inputLine = null;
2470 recordState = new RecordState(source);
2471
2472 this.nr++;
2473 if (countsTowardFNR(source)) {
2474 this.fnr++;
2475 }
2476 }
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489 private static boolean countsTowardFNR(InputSource source) {
2490 return source instanceof StreamInputSource || source.isFromFilenameList();
2491 }
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504 public Object consumeInputToTarget(final InputSource source) throws IOException {
2505 Objects.requireNonNull(source, "source");
2506 activeSource = source;
2507 materializeCurrentRecord();
2508 if (!source.nextRecord()) {
2509 return null;
2510 }
2511
2512 RecordState inputState = new RecordState(source);
2513 this.nr++;
2514 if (countsTowardFNR(source)) {
2515 this.fnr++;
2516 }
2517 return new StrNum(inputState.getRecordText(), decimalSeparator);
2518 }
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529 public boolean consumeInputForEval(InputSource source) throws IOException {
2530 return consumeInput(source);
2531 }
2532
2533
2534
2535
2536
2537
2538
2539 protected void initializeInputFields(String record, List<String> preFields) {
2540 recordState = new RecordState(toInputScalar(record), preFields);
2541 }
2542
2543
2544
2545
2546
2547 public void jrtParseFields() {
2548 RecordState state = ensureRecordStateForTextMutation();
2549 state.ensureFieldsMaterialized();
2550 }
2551
2552
2553
2554
2555
2556
2557
2558 public boolean hasInputFields() {
2559 return recordState != null;
2560 }
2561
2562
2563
2564
2565
2566
2567
2568
2569 public void jrtSetNF(Object nfObj) {
2570 int nf = (int) toDouble(nfObj);
2571 if (nf < 0) {
2572 nf = 0;
2573 }
2574
2575 RecordState state = ensureRecordStateForFieldMutation();
2576 int currentNF = state.getNF();
2577
2578 if (nf < currentNF) {
2579 for (int i = currentNF; i > nf; i--) {
2580 state.removeField(i - 1);
2581 }
2582 } else if (nf > currentNF) {
2583 for (int i = currentNF + 1; i <= nf; i++) {
2584 state.addField("");
2585 }
2586 }
2587
2588 state.markRecordTextDirty();
2589 }
2590
2591
2592
2593
2594
2595
2596
2597 public Object jrtGetInputField(Object fieldnumObj) {
2598 return jrtGetInputField(parseFieldNumber(fieldnumObj));
2599 }
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609 public Object jrtGetInputField(long fieldnum) {
2610 if (fieldnum < 0 || fieldnum > Integer.MAX_VALUE) {
2611 throw new AwkRuntimeException("Field $(" + Long.valueOf(fieldnum) + ") is incorrect.");
2612 }
2613 if (recordState == null) {
2614 return BLANK;
2615 }
2616 return recordState.getField((int) fieldnum);
2617 }
2618
2619
2620
2621
2622
2623
2624
2625
2626 public String jrtSetInputField(Object valueObj, long fieldNum) {
2627 if (fieldNum > Integer.MAX_VALUE) {
2628 throw new AwkRuntimeException("Field $(" + Long.valueOf(fieldNum) + ") is incorrect.");
2629 }
2630 String value = valueObj == null ? "" : valueObj.toString();
2631 int fieldIndex = (int) fieldNum;
2632 RecordState state = ensureRecordStateForFieldMutation();
2633 if (valueObj instanceof UninitializedObject) {
2634 if (fieldIndex <= state.getNF()) {
2635 state.setField(fieldIndex - 1, "");
2636 }
2637 } else {
2638 while (state.getNF() < fieldIndex) {
2639 state.addField(BLANK);
2640 }
2641 state.setField(fieldIndex - 1, valueObj);
2642 }
2643 state.markRecordTextDirty();
2644 return value;
2645 }
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657 protected void rebuildDollarZeroFromFields() {
2658 if (recordState != null) {
2659 recordState.markRecordTextDirty();
2660 inputLine = recordState.getField(0);
2661 }
2662 }
2663
2664 private void materializeCurrentRecord() {
2665 if (recordState != null) {
2666 recordState.materialize();
2667 }
2668 }
2669
2670 private RecordState ensureRecordStateForTextMutation() {
2671 if (recordState == null) {
2672 recordState = new RecordState(inputLine, null);
2673 }
2674 return recordState;
2675 }
2676
2677 private RecordState ensureRecordStateForFieldMutation() {
2678 RecordState state = ensureRecordStateForTextMutation();
2679 state.ensureFieldsMaterialized();
2680 return state;
2681 }
2682
2683 private List<Object> sanitizeFields(List<String> rawFields) {
2684 List<Object> copy = new ArrayList<Object>(rawFields.size());
2685 for (String field : rawFields) {
2686 String value = field == null ? "" : field;
2687 copy.add(new StrNum(value, decimalSeparator));
2688 }
2689 return copy;
2690 }
2691
2692 private List<Object> splitRecordText(String recordText, String fieldSeparator) {
2693 List<Object> fields = new ArrayList<Object>();
2694 if (recordText == null || recordText.isEmpty()) {
2695 return fields;
2696 }
2697
2698 Enumeration<Object> tokenizer = splitTokenizer(recordText, fieldSeparator);
2699
2700 while (tokenizer.hasMoreElements()) {
2701 fields.add(new StrNum((String) tokenizer.nextElement(), decimalSeparator));
2702 }
2703 return fields;
2704 }
2705
2706 private static String joinFieldsWithLiteralSeparator(List<Object> fields, String separator) {
2707 StringBuilder sb = new StringBuilder();
2708 for (int i = 0; i < fields.size(); i++) {
2709 if (i > 0) {
2710 sb.append(separator);
2711 }
2712 Object field = fields.get(i);
2713 sb.append(field == null ? "" : field.toString());
2714 }
2715 return sb.toString();
2716 }
2717
2718 private String rebuildRecordTextFromFields(List<Object> fields) {
2719
2720
2721
2722 StringBuilder sb = new StringBuilder();
2723 for (int i = 0; i < fields.size(); i++) {
2724 if (i > 0) {
2725 sb.append(ofs);
2726 }
2727 Object field = fields.get(i);
2728 sb.append(field == null ? "" : toAwkString(field));
2729 }
2730 return sb.toString();
2731 }
2732
2733 private final class RecordState {
2734
2735 private final String fieldSeparatorAtRead;
2736 private final InputSource source;
2737 private String recordText;
2738 private Object recordScalar;
2739 private List<Object> fields;
2740 private boolean recordTextAvailable;
2741 private boolean fieldsAvailable;
2742 private boolean recordTextDirty;
2743 private boolean fieldsDirty;
2744 private boolean recordTextLoadedFromSource;
2745 private boolean fieldsLoadedFromSource;
2746
2747 private RecordState(InputSource source) {
2748 this(null, null, source);
2749 }
2750
2751 private RecordState(Object recordValue, List<String> rawFields) {
2752 this(recordValue, rawFields, null);
2753 }
2754
2755 private RecordState(Object recordValue, List<String> rawFields, InputSource source) {
2756 this.fieldSeparatorAtRead = fs;
2757 this.source = source;
2758 if (recordValue != null) {
2759 this.recordScalar = normalizeRecordValue(recordValue);
2760 this.recordText = this.recordScalar.toString();
2761 this.recordTextAvailable = true;
2762 } else if (rawFields == null && source == null) {
2763 this.recordScalar = "";
2764 this.recordText = "";
2765 this.recordTextAvailable = true;
2766 }
2767 if (rawFields != null) {
2768 this.fields = sanitizeFields(rawFields);
2769 this.fieldsAvailable = true;
2770 this.fieldsDirty = false;
2771 } else {
2772 this.fieldsAvailable = false;
2773 this.fieldsDirty = true;
2774 }
2775 this.recordTextDirty = false;
2776 }
2777
2778 private void ensureFieldsMaterialized() {
2779 if (fieldsAvailable && !fieldsDirty) {
2780 return;
2781 }
2782 if (!recordTextDirty) {
2783 loadFieldsFromSource();
2784 if (fieldsAvailable && !fieldsDirty) {
2785 return;
2786 }
2787 }
2788 fields = splitRecordText(getRecordText(), fieldSeparatorAtRead);
2789 fieldsAvailable = true;
2790 fieldsDirty = false;
2791 }
2792
2793 private String getRecordText() {
2794 if (!recordTextAvailable || recordTextDirty) {
2795 if (recordTextDirty) {
2796 recordText = rebuildRecordTextFromFields(fields);
2797 recordScalar = recordText;
2798 } else {
2799 loadRecordTextFromSource();
2800 if (!recordTextAvailable) {
2801 loadFieldsFromSource();
2802 if (!fieldsAvailable) {
2803 throw new IllegalStateException(
2804 "InputSource must provide record text, fields, or both after nextRecord()");
2805 }
2806 recordText = joinFieldsWithLiteralSeparator(fields, fieldSeparatorAtRead);
2807 recordScalar = new StrNum(recordText, decimalSeparator);
2808 }
2809 }
2810 recordTextAvailable = true;
2811 recordTextDirty = false;
2812 }
2813 return recordText;
2814 }
2815
2816 private int getNF() {
2817 ensureFieldsMaterialized();
2818 return fields.size();
2819 }
2820
2821 private Object getField(int fieldIndex) {
2822 if (fieldIndex == 0) {
2823 String value = getRecordText();
2824 if (recordScalar == null) {
2825 recordScalar = value;
2826 }
2827 return recordScalar;
2828 }
2829 ensureFieldsMaterialized();
2830 int zeroBasedIndex = fieldIndex - 1;
2831 if (zeroBasedIndex < 0 || zeroBasedIndex >= fields.size()) {
2832 return BLANK;
2833 }
2834 return fields.get(zeroBasedIndex);
2835 }
2836
2837 private void setField(int zeroBasedIndex, Object value) {
2838 ensureFieldsMaterialized();
2839 fields.set(zeroBasedIndex, normalizeFieldValue(value));
2840 markRecordTextDirty();
2841 }
2842
2843 private void addField(Object value) {
2844 ensureFieldsMaterialized();
2845 fields.add(normalizeFieldValue(value));
2846 markRecordTextDirty();
2847 }
2848
2849 private Object normalizeFieldValue(Object value) {
2850 if (value == null) {
2851 return "";
2852 }
2853 return value;
2854 }
2855
2856 private void removeField(int zeroBasedIndex) {
2857 ensureFieldsMaterialized();
2858 fields.remove(zeroBasedIndex);
2859 markRecordTextDirty();
2860 }
2861
2862 private void markRecordTextDirty() {
2863 recordTextDirty = true;
2864 recordTextAvailable = fieldsAvailable;
2865 recordScalar = null;
2866 }
2867
2868 private void materialize() {
2869 getRecordText();
2870 ensureFieldsMaterialized();
2871 }
2872
2873 private void loadRecordTextFromSource() {
2874 if (source == null || recordTextLoadedFromSource) {
2875 return;
2876 }
2877 recordText = source.getRecordText();
2878 recordTextAvailable = recordText != null;
2879 if (recordTextAvailable) {
2880 recordScalar = new StrNum(recordText, decimalSeparator);
2881 }
2882 recordTextLoadedFromSource = true;
2883 }
2884
2885 private void loadFieldsFromSource() {
2886 if (source == null || fieldsLoadedFromSource) {
2887 return;
2888 }
2889 List<String> rawFields = source.getFields();
2890 fieldsLoadedFromSource = true;
2891 if (rawFields != null) {
2892 fields = sanitizeFields(rawFields);
2893 fieldsAvailable = true;
2894 fieldsDirty = false;
2895 }
2896 }
2897 }
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911 public Integer jrtConsumeFileInputForGetline(String fileNameParam) {
2912 if (fileNameParam.isEmpty()) {
2913 throw new AwkRuntimeException("expression for `<' redirection has null string value");
2914 }
2915 try {
2916 if (jrtConsumeFileInput(fileNameParam)) {
2917 return ONE;
2918 }
2919 jrtInputString = "";
2920 return ZERO;
2921 } catch (IOException ioe) {
2922 jrtInputString = "";
2923 setERRNO(describeOpenFailure(fileNameParam, ioe));
2924 return MINUS_ONE;
2925 }
2926 }
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941 public Integer jrtConsumeCommandInputForGetline(String cmdString) {
2942 if (cmdString.isEmpty()) {
2943 throw new AwkRuntimeException("expression for `|' redirection has null string value");
2944 }
2945 try {
2946 if (jrtConsumeCommandInput(cmdString)) {
2947 return ONE;
2948 }
2949 jrtInputString = "";
2950 return ZERO;
2951 } catch (IOException ioe) {
2952 jrtInputString = "";
2953 setERRNO(describeIoReason(ioe));
2954 return MINUS_ONE;
2955 }
2956 }
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967 private static String describeOpenFailure(String fileNameParam, IOException ioe) {
2968 if (!isStandardInputName(fileNameParam) && !isNullDeviceName(fileNameParam)) {
2969 File file = new File(toPlatformFileName(fileNameParam));
2970 if (file.isDirectory()) {
2971 return "Is a directory";
2972 }
2973 if (!file.exists()) {
2974 return "No such file or directory";
2975 }
2976 }
2977 return describeIoReason(ioe);
2978 }
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989 static String describeIoReason(IOException ioe) {
2990 String message = ioe.getMessage();
2991 if (message == null || message.isEmpty()) {
2992 return "Permission denied";
2993 }
2994 int open = message.lastIndexOf('(');
2995 if (open >= 0 && message.endsWith(")")) {
2996 return message.substring(open + 1, message.length() - 1);
2997 }
2998 return message;
2999 }
3000
3001
3002
3003
3004
3005
3006
3007
3008 public String jrtGetInputString() {
3009 return jrtInputString;
3010 }
3011
3012
3013
3014
3015
3016
3017
3018
3019 public Map<String, PrintStream> getOutputFiles() {
3020 Map<String, PrintStream> outputFiles = new HashMap<String, PrintStream>();
3021 for (Map.Entry<String, FileOutputState> entry : getIoState().fileOutputs.entrySet()) {
3022 outputFiles.put(entry.getKey(), entry.getValue().sink.getPrintStream());
3023 }
3024 return outputFiles;
3025 }
3026
3027
3028
3029
3030
3031
3032
3033
3034 private static boolean isStandardInputName(String fileNameParam) {
3035 return DEV_STDIN.equals(fileNameParam) || DEV_FD_0.equals(fileNameParam);
3036 }
3037
3038
3039
3040
3041
3042
3043
3044
3045 private static boolean isStandardOutputName(String fileNameParam) {
3046 return DEV_STDOUT.equals(fileNameParam) || DEV_FD_1.equals(fileNameParam);
3047 }
3048
3049
3050
3051
3052
3053
3054
3055
3056 private static boolean isStandardErrorName(String fileNameParam) {
3057 return DEV_STDERR.equals(fileNameParam) || DEV_FD_2.equals(fileNameParam);
3058 }
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078 static boolean isNullDeviceName(String fileNameParam) {
3079 return DEV_NULL.equals(fileNameParam) || (IS_WINDOWS && WINDOWS_NULL_DEVICE.equalsIgnoreCase(fileNameParam));
3080 }
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101 static String toPlatformFileName(String fileNameParam) {
3102 return IS_WINDOWS && DEV_NULL.equals(fileNameParam) ? WINDOWS_NULL_DEVICE : fileNameParam;
3103 }
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113 private AwkSink getStandardErrorSink() {
3114 if (standardErrorSink == null) {
3115 standardErrorSink = new FlushingAwkSink(warning, locale);
3116 }
3117 return standardErrorSink;
3118 }
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132 protected AwkSink getFileAwkSink(String fileNameParam, boolean append) {
3133 if (isStandardOutputName(fileNameParam)) {
3134 return openSpecialOutput(fileNameParam, awkSink);
3135 }
3136 if (isStandardErrorName(fileNameParam)) {
3137 return openSpecialOutput(fileNameParam, getStandardErrorSink());
3138 }
3139 return getOrCreateFileOutputState(fileNameParam, append).sink;
3140 }
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150 private AwkSink openSpecialOutput(String fileNameParam, AwkSink sink) {
3151 getIoState().specialOutputs.put(fileNameParam, sink);
3152 return sink;
3153 }
3154
3155
3156
3157
3158
3159
3160
3161 protected AwkSink getPipeAwkSink(String cmd) {
3162 return getOrCreateProcessOutputState(cmd).sink;
3163 }
3164
3165
3166
3167
3168
3169
3170
3171 public void printDefault(Object[] values) throws IOException {
3172 awkSink.print(ofs, ors, ofmt, values);
3173 }
3174
3175
3176
3177
3178
3179
3180
3181
3182 public void printRecordDefault() throws IOException {
3183 awkSink.printRecord(ofs, ors, ofmt, recordFieldsView, jrtGetInputField(0));
3184 }
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194 public void printToFile(String fileNameParam, boolean append, Object[] values) throws IOException {
3195 getFileAwkSink(fileNameParam, append).print(ofs, ors, ofmt, values);
3196 }
3197
3198
3199
3200
3201
3202
3203
3204
3205 public void printToProcess(String cmd, Object[] values) throws IOException {
3206 AwkSink sink = getPipeAwkSink(cmd);
3207 sink.print(ofs, ors, ofmt, values);
3208 sink.flush();
3209 }
3210
3211
3212
3213
3214
3215
3216
3217
3218 public void printfDefault(String format, Object[] values) throws IOException {
3219 awkSink.printf(ofs, ors, ofmt, convfmt, format, values);
3220 }
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231 public String sprintf(String format, Object... values) {
3232 return awkSink.sprintf(convfmt, format, values);
3233 }
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244 public void printfToFile(String fileNameParam, boolean append, String format, Object[] values) throws IOException {
3245 AwkSink sink = getFileAwkSink(fileNameParam, append);
3246 sink.printf(ofs, ors, ofmt, convfmt, format, values);
3247 }
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257 public void printfToProcess(String cmd, String format, Object[] values) throws IOException {
3258 AwkSink sink = getPipeAwkSink(cmd);
3259 sink.printf(ofs, ors, ofmt, convfmt, format, values);
3260 sink.flush();
3261 }
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271 public PrintStream jrtGetPrintStream(String fileNameParam, boolean append) {
3272 return getFileAwkSink(fileNameParam, append).getPrintStream();
3273 }
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299 public boolean jrtConsumeFileInput(String fileNameParam) throws IOException {
3300 Map<String, PartitioningReader> fileReaders = getIoState().fileReaders;
3301 PartitioningReader pr = fileReaders.get(fileNameParam);
3302 if (pr == null) {
3303 InputStream inputStream = isStandardInputName(fileNameParam) ?
3304 standardInput :
3305 new FileInputStream(toPlatformFileName(fileNameParam));
3306 pr = new PartitioningReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8), this.rs);
3307 fileReaders.put(fileNameParam, pr);
3308 }
3309
3310 String recordText = pr.readRecord();
3311 if (recordText == null) {
3312 return false;
3313 }
3314 jrtInputString = recordText;
3315 return true;
3316 }
3317
3318 private static Process spawnProcess(String cmd, boolean inheritStandardInput) throws IOException {
3319 ProcessBuilder pb = IS_WINDOWS
3320
3321 ? new ProcessBuilder("cmd.exe", "/c", cmd)
3322
3323 : new ProcessBuilder("/bin/sh", "-c", cmd);
3324 if (inheritStandardInput) {
3325 pb.redirectInput(ProcessBuilder.Redirect.INHERIT);
3326 }
3327 return pb.start();
3328 }
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349 public void setSpawnedProcessesInheritStandardInput(boolean inherit) {
3350 this.spawnedProcessesInheritStandardInput = inherit;
3351 }
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361 private boolean spawnedProcessInheritsStandardInput() {
3362 return spawnedProcessesInheritStandardInput;
3363 }
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384 public boolean jrtConsumeCommandInput(String cmd) throws IOException {
3385 CommandInputState commandInput = getOrCreateCommandInputState(cmd);
3386 String recordText = commandInput.reader.readRecord();
3387 if (recordText == null) {
3388 return false;
3389 }
3390 jrtInputString = recordText;
3391 return true;
3392 }
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403 public PrintStream jrtSpawnForOutput(String cmd) {
3404 return getPipeAwkSink(cmd).getPrintStream();
3405 }
3406
3407 private FileOutputState getOrCreateFileOutputState(String fileNameParam, boolean append) {
3408 IoState state = getIoState();
3409 FileOutputState outputState = state.fileOutputs.get(fileNameParam);
3410 if (outputState == null) {
3411 outputState = createFileOutputState(fileNameParam, append);
3412 state.fileOutputs.put(fileNameParam, outputState);
3413 }
3414 return outputState;
3415 }
3416
3417 private FileOutputState createFileOutputState(String fileNameParam, boolean append) {
3418 try {
3419 PrintStream printStream = new PrintStream(
3420 new FileOutputStream(toPlatformFileName(fileNameParam), append),
3421 true,
3422 StandardCharsets.UTF_8.name());
3423 return new FileOutputState(new OutputStreamAwkSink(printStream, locale));
3424 } catch (IOException ioe) {
3425 throw new AwkRuntimeException("Cannot open " + fileNameParam + " for writing: " + ioe);
3426 }
3427 }
3428
3429 private CommandInputState getOrCreateCommandInputState(String cmd) throws IOException {
3430 IoState state = getIoState();
3431 CommandInputState commandInput = state.commandInputs.get(cmd);
3432 if (commandInput == null) {
3433 commandInput = createCommandInputState(cmd);
3434 state.commandInputs.put(cmd, commandInput);
3435 }
3436 return commandInput;
3437 }
3438
3439 private CommandInputState createCommandInputState(String cmd) throws IOException {
3440 Process process = null;
3441 Thread errorPump = null;
3442 try {
3443
3444
3445 boolean inheritStandardInput = spawnedProcessInheritsStandardInput();
3446 process = spawnProcess(cmd, inheritStandardInput);
3447 if (!inheritStandardInput) {
3448 process.getOutputStream().close();
3449 }
3450 errorPump = DataPump.dumpAndReturnThread(cmd + " stderr", process.getErrorStream(), error);
3451 PartitioningReader reader = new PartitioningReader(
3452 new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8),
3453 this.rs);
3454 return new CommandInputState(process, reader, errorPump);
3455 } catch (IOException ioe) {
3456 if (process != null) {
3457 process.destroy();
3458 }
3459 joinDataPump(errorPump);
3460 throw ioe;
3461 }
3462 }
3463
3464 private ProcessOutputState getOrCreateProcessOutputState(String cmd) {
3465 IoState state = getIoState();
3466 ProcessOutputState outputState = state.processOutputs.get(cmd);
3467 if (outputState == null) {
3468 outputState = createProcessOutputState(cmd);
3469 state.processOutputs.put(cmd, outputState);
3470 }
3471 return outputState;
3472 }
3473
3474 private ProcessOutputState createProcessOutputState(String cmd) {
3475 Process process = null;
3476 Thread stderrPump = null;
3477 Thread stdoutPump = null;
3478 PrintStream processOutput = null;
3479 try {
3480 processOutput = awkSink.getPrintStream();
3481
3482 process = spawnProcess(cmd, false);
3483 stderrPump = DataPump.dumpAndReturnThread(cmd + " stderr", process.getErrorStream(), error);
3484 stdoutPump = DataPump.dumpAndReturnThread(cmd + " stdout", process.getInputStream(), processOutput);
3485 PrintStream processInput = new PrintStream(process.getOutputStream(), true, StandardCharsets.UTF_8.name());
3486 return new ProcessOutputState(
3487 process,
3488 new OutputStreamAwkSink(processInput, locale),
3489 processOutput,
3490 stdoutPump,
3491 stderrPump);
3492 } catch (IOException ioe) {
3493 if (process != null) {
3494 process.destroy();
3495 }
3496 joinDataPump(stdoutPump);
3497 joinDataPump(stderrPump);
3498 throw new AwkRuntimeException("Can't spawn " + cmd + ": " + ioe);
3499 }
3500 }
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517 public Integer jrtClose(String fileNameParam) {
3518 boolean b1 = jrtCloseFileReader(fileNameParam);
3519 boolean b2 = jrtCloseCommandReader(fileNameParam);
3520 boolean b3 = jrtCloseOutputFile(fileNameParam);
3521 boolean b4 = jrtCloseOutputStream(fileNameParam);
3522 boolean b5 = jrtCloseSpecialOutput(fileNameParam);
3523
3524 return (b1 || b2 || b3 || b4 || b5) ? ZERO : MINUS_ONE;
3525 }
3526
3527
3528
3529
3530
3531
3532 public void jrtCloseAll() {
3533 IoState state = ioState;
3534 if (state == null) {
3535 return;
3536 }
3537 Set<String> set = new HashSet<String>();
3538 for (String s : state.fileReaders.keySet()) {
3539 set.add(s);
3540 }
3541 for (String s : state.commandInputs.keySet()) {
3542 set.add(s);
3543 }
3544 for (String s : state.fileOutputs.keySet()) {
3545 set.add(s);
3546 }
3547 for (String s : state.processOutputs.keySet()) {
3548 set.add(s);
3549 }
3550 for (String s : state.specialOutputs.keySet()) {
3551 set.add(s);
3552 }
3553 for (String s : set) {
3554 jrtClose(s);
3555 }
3556 }
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570 private boolean jrtCloseSpecialOutput(String fileNameParam) {
3571 IoState state = ioState;
3572 if (state == null) {
3573 return false;
3574 }
3575 AwkSink sink = state.specialOutputs.remove(fileNameParam);
3576 if (sink == null) {
3577 return false;
3578 }
3579 try {
3580 sink.flush();
3581 return true;
3582 } catch (IOException ioe) {
3583 setERRNO(ioe.toString());
3584 return false;
3585 }
3586 }
3587
3588 private boolean jrtCloseOutputFile(String fileNameParam) {
3589 IoState state = ioState;
3590 if (state == null) {
3591 return false;
3592 }
3593 FileOutputState outputState = state.fileOutputs.remove(fileNameParam);
3594 if (outputState != null) {
3595 outputState.sink.getPrintStream().close();
3596 }
3597 return outputState != null;
3598 }
3599
3600 private boolean jrtCloseOutputStream(String cmd) {
3601 IoState state = ioState;
3602 if (state == null) {
3603 return false;
3604 }
3605 ProcessOutputState outputState = state.processOutputs.remove(cmd);
3606 if (outputState == null) {
3607 return false;
3608 }
3609 outputState.sink.getPrintStream().close();
3610 try {
3611
3612
3613 outputState.process.waitFor();
3614 outputState.process.exitValue();
3615 } catch (InterruptedException ie) {
3616 Thread.currentThread().interrupt();
3617 outputState.process.destroyForcibly();
3618 throw new AwkRuntimeException("Caught exception while waiting for process exit: " + ie);
3619 } finally {
3620 joinDataPump(outputState.stdoutPump);
3621 joinDataPump(outputState.stderrPump);
3622 outputState.processOutput.flush();
3623 error.flush();
3624 }
3625 return true;
3626 }
3627
3628 private boolean jrtCloseFileReader(String fileNameParam) {
3629 IoState state = ioState;
3630 if (state == null) {
3631 return false;
3632 }
3633 PartitioningReader pr = state.fileReaders.get(fileNameParam);
3634 if (pr == null) {
3635 return false;
3636 }
3637 state.fileReaders.remove(fileNameParam);
3638 if (isStandardInputName(fileNameParam)) {
3639
3640
3641
3642
3643
3644 return true;
3645 }
3646 try {
3647 pr.close();
3648 return true;
3649 } catch (IOException ioe) {
3650 return false;
3651 }
3652 }
3653
3654 private boolean jrtCloseCommandReader(String cmd) {
3655 IoState state = ioState;
3656 if (state == null) {
3657 return false;
3658 }
3659 CommandInputState commandInput = state.commandInputs.remove(cmd);
3660 if (commandInput == null) {
3661 return false;
3662 }
3663 try {
3664 commandInput.reader.close();
3665 try {
3666
3667
3668 commandInput.process.waitFor();
3669 commandInput.process.exitValue();
3670 } catch (InterruptedException ie) {
3671 Thread.currentThread().interrupt();
3672 commandInput.process.destroyForcibly();
3673 throw new AwkRuntimeException("Caught exception while waiting for process exit: " + ie);
3674 }
3675 return true;
3676 } catch (IOException ioe) {
3677 return false;
3678 } finally {
3679 joinDataPump(commandInput.errorPump);
3680 error.flush();
3681 }
3682 }
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698 public Integer jrtSystem(String cmd) {
3699 try {
3700 PrintStream processOutput = awkSink.getPrintStream();
3701
3702
3703 boolean inheritStandardInput = spawnedProcessInheritsStandardInput();
3704 Process p = spawnProcess(cmd, inheritStandardInput);
3705 if (!inheritStandardInput) {
3706 p.getOutputStream().close();
3707 }
3708 Thread errorPump = DataPump.dumpAndReturnThread(cmd + " stderr", p.getErrorStream(), error);
3709 Thread outputPump = DataPump.dumpAndReturnThread(cmd + " stdout", p.getInputStream(), processOutput);
3710 boolean interrupted = false;
3711 int retcode;
3712 while (true) {
3713 try {
3714 retcode = p.waitFor();
3715 break;
3716 } catch (InterruptedException ie) {
3717
3718 interrupted = true;
3719 }
3720 }
3721 joinDataPump(outputPump);
3722 joinDataPump(errorPump);
3723 processOutput.flush();
3724 error.flush();
3725 if (interrupted) {
3726 Thread.currentThread().interrupt();
3727 }
3728 return Integer.valueOf(retcode);
3729 } catch (IOException ioe) {
3730 return MINUS_ONE;
3731 }
3732 }
3733
3734 private static void joinDataPump(Thread pump) {
3735 if (pump == null) {
3736 return;
3737 }
3738 boolean interrupted = false;
3739 while (true) {
3740 try {
3741 pump.join();
3742 break;
3743 } catch (InterruptedException ie) {
3744 interrupted = true;
3745 }
3746 }
3747 if (interrupted) {
3748 Thread.currentThread().interrupt();
3749 }
3750 }
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763 public static String sprintfNoCatch(Locale locale, String fmtArg, Object... arr) throws IllegalFormatException {
3764 return String.format(locale, fmtArg, arr);
3765 }
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776 public static void printfNoCatch(Locale locale, String fmtArg, Object... arr) {
3777 System.out.print(sprintfNoCatch(locale, fmtArg, arr));
3778 }
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790 public static void printfNoCatch(PrintStream ps, Locale locale, String fmtArg, Object... arr) {
3791 ps.print(sprintfNoCatch(locale, fmtArg, arr));
3792 }
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803 public static String substr(Object startposObj, String str) {
3804 int startpos = (int) toDouble(startposObj);
3805 if (startpos <= 0) {
3806 throw new AwkRuntimeException("2nd arg to substr must be a positive integer");
3807 }
3808 if (startpos > str.length()) {
3809 return "";
3810 } else {
3811 return str.substring(startpos - 1);
3812 }
3813 }
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825 public static String substr(Object sizeObj, Object startposObj, String str) {
3826 int startpos = (int) toDouble(startposObj);
3827 if (startpos <= 0) {
3828 throw new AwkRuntimeException("2nd arg to substr must be a positive integer");
3829 }
3830 if (startpos > str.length()) {
3831 return "";
3832 }
3833 int size = (int) toDouble(sizeObj);
3834 if (size < 0) {
3835 throw new AwkRuntimeException("3nd arg to substr must be a non-negative integer");
3836 }
3837 if (startpos + size > str.length()) {
3838 return str.substring(startpos - 1);
3839 } else {
3840 return str.substring(startpos - 1, startpos + size - 1);
3841 }
3842 }
3843
3844
3845
3846
3847
3848
3849
3850
3851 public static int timeSeed() {
3852 long l = new Date().getTime();
3853 long l2 = l % (1000 * 60 * 60 * 24);
3854 int seed = (int) l2;
3855 return seed;
3856 }
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866 public static BSDRandom newRandom(int seed) {
3867 return new BSDRandom(seed);
3868 }
3869
3870
3871
3872
3873
3874
3875
3876
3877 public void applyRS(Object rsObj) {
3878 if (activeSource instanceof StreamInputSource) {
3879 ((StreamInputSource) activeSource).setRecordSeparator(rsObj.toString());
3880 }
3881 }
3882 }