forked from php/doc-base
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrep-files.php
More file actions
executable file
·85 lines (71 loc) · 2.66 KB
/
grep-files.php
File metadata and controls
executable file
·85 lines (71 loc) · 2.66 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
#!/usr/bin/php -q
<?php
/*
+----------------------------------------------------------------------+
| Copyright (c) 1997-2023 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net, so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Philip Olson <philip@php.net> |
+----------------------------------------------------------------------+
$Id$
*/
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo "This script requires a non-Windows operating system.\n";
exit;
}
$editor = getenv('EDITOR');
if ($_SERVER['argc'] == 2 &&
in_array($_SERVER['argv'][1], array('--help', '-help', '-h', '-?'))
||
$_SERVER['argc'] < 2) {
echo "\nFind and edit files by grepping for a case insensitive string.\n";
echo "I will open EDITOR if it's set, and the [language directory] is optional.\n\n";
echo "Usage: {$_SERVER['argv'][0]} <string> [langdir]\n";
echo "Example: {$_SERVER['argv'][0]} 'PHP3' en\n";
if ($editor) {
echo "Note: Your EDITOR is set to: {$editor}\n\n";
} else {
echo "Note: You do not have an EDITOR set so I will output a string of files\n\n";
}
exit;
}
$langdir = '';
if (!empty($_SERVER['argv'][2])) {
$dir = trim($_SERVER['argv'][2]);
if (is_dir($dir)) {
$langdir = rtrim($dir, '/') . '/';
} else {
echo "INFO: The desired language directory ({$dir}) does not exist. Skipping...\n";
}
}
$search_str = trim($_SERVER['argv'][1]);
$output = trim(shell_exec("egrep --exclude=\*.svn\* -i -r '{$search_str}' {$langdir}* "));
if (empty($output)) {
echo "INFO: No matches for string '{$search_str}'\n";
exit;
}
$files = array();
foreach (explode("\n", $output) as $line) {
$filename = trim(substr($line, 0, strpos($line, ':')));
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (($ext === 'xml' || $ext === 'ent') && $filename[0] !== '.') {
$files[$filename] = 'open';
}
}
if (empty($files)) {
echo "INFO: No matches for string '{$search_str}'\n";
exit;
}
$files_str = implode(' ', array_keys($files));
if ($editor) {
shell_exec($editor . ' ' . $files_str);
} else {
echo $files_str;
}