-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp
More file actions
executable file
·150 lines (125 loc) · 3.53 KB
/
Copy pathp
File metadata and controls
executable file
·150 lines (125 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env php
<?php
/**
* p - AWK like PHP wrapper for command line
*
* @copyright 2019 USAMI Kenta
* @author USAMI Kenta
* @license https://github.com/bag2php/p-command/blob/master/LICENSE MPL-2.0
*/
const BAG2_P_CMD_VERSION = '0.1.0';
$options = \getopt('d:f:v:B:E:F::R:', ['help', 'version'], $optind);
$rest_args = \count($argv) > $optind ? \array_slice($argv, $optind) : [];
$OFS = ' ';
$ORS = PHP_EOL;
$FS = '/\s/';
if (isset($options['help']) || isset($options['version'])) {
echo 'p ', BAG2_P_CMD_VERSION, ' - ';
echo 'AWK like PHP wrapper for command line ', \PHP_EOL;
exit(1);
}
if (isset($options['F'])) {
$FS = '/'. \implode('|', \array_map(function ($d) {
return \preg_quote($d, '/');
}, (array)$options['F'])) . '/';
}
foreach ((array)($options['d'] ?? []) as $def) {
if (\strpos($def, '=') === false) {
\ini_set($def, '1');
} else {
[$name, $value] = \explode('=', $def);
\ini_set($name, $value);
}
}
unset($def, $name, $value);
foreach ((array)($options['f'] ?? []) as $file) {
include $file;
}
unset($file);
foreach ((array)($options['v'] ?? []) as $def) {
if (\strpos($def, '=') === false) {
$var = [$def => null];
} else {
[$name, $value] = \explode('=', $def);
$var = [$name => $value];
}
\extract($var);
}
foreach ((array)($options['B'] ?? []) as $code) {
eval($code . ';');
}
unset($code);
${"\0codes\0"} = \array_map(function($code) {
if (\strpos($code, ':') === false) {
return [
'test' => 'return true;',
'code' => "{$code};"
];
}
throw new RuntimeException("Multiple statement NOT Supported yet.");
}, (array)($options['R'] ?? []));
${"\0files\0"} = ['-' => STDIN];
foreach ($rest_args as $a) {
if (\preg_match('/\A(?<test>[^{}]*){(?<code>.+)\}\z/', $a, $matches)) {
$test = \trim($matches['test']);
if ($test === '') {
$test = 'true';
} elseif (\preg_match('@\A/.+/\z@', $test)) {
$test = \sprintf('\\preg_match(\'%s\', $argn)', strtr(
$test,
['\\' => '\\\\', "'" => "\\'"]
));
}
${"\0codes\0"}[] = [
'test' => "return {$test};",
'code' => "return {$matches['code']};",
];
} else {
${"\0files\0"}[$a] = \fopen($a, 'r');
}
}
unset($rest_args, $a, $matches, $test);
${"\0output\0"} = function($value) use ($OFS): string {
if ($value === null) {
return '';
}
if (\is_string($value) || \is_int($value) || \is_float($value)) {
return (string)$value;
}
if (\is_iterable($value)) {
$values = [];
foreach ($value as $v) {
$values[] = (string)$v;
}
return \implode($OFS, $values);
}
return (string)$value;
};
$NR = 1;
foreach (${"\0files\0"} as $FILENAME => $fp) {
$FNR = 1;
while ($line = \fgets($fp)) {
$argi = $NR;
$argn = \rtrim($line, $ORS);
$F0 = ${0} = $argn;
$F = \preg_split($FS, $argn) ?: [];
\array_unshift($F, $F0);
// extract($F, \EXTR_PREFIX_ALL, 'F');
// extract($F, \EXTR_PREFIX_ALL);
$NF = \count($F);
foreach (${"\0codes\0"} as $c) {
if (eval($c['test'])) {
$_ = eval($c['code']);
}
}
if (isset($_)) {
echo ${"\0output\0"}($_), $ORS;
}
$FNR++;
$NR++;
unset($_);
}
}
foreach ((array)($options['E'] ?? []) as $code) {
eval($code . ';');
}