forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readline.php
41 lines (35 loc) · 1.01 KB
/
readline.php
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
<?php
if (php_sapi_name() == "cli") {
$HISTORY_FILE = $_SERVER['HOME'] . "/.mal-history";
function mal_readline($prompt) {
global $HISTORY_FILE;
static $history_loaded = false;
// Load the history file
if (! $history_loaded) {
$history_loaded = true;
if (is_readable($HISTORY_FILE)) {
if ($file = fopen($HISTORY_FILE, "r")) {
while (!feof($file)) {
$line = fgets($file);
if ($line) { readline_add_history($line); }
}
fclose($file);
}
}
}
$line = readline($prompt);
if ($line === false) { return NULL; }
readline_add_history($line);
// Append to the history file
if (is_writable($HISTORY_FILE)) {
if ($file = fopen($HISTORY_FILE, "a")) {
fputs($file, $line . "\n");
fclose($file);
}
}
return $line;
}
} else {
function mal_readline($prompt) {}
}
?>