1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 02:27:10 +03:00
mal/php/readline.php
Chris McCormick 082158f9ee Allow web exec of a .mal file via symlinked php script.
ln -s  mal.php myscript.php
Then create myscript.mal will get run when the user visits /myscript on a PHP capable webserver.
2016-10-25 10:35:14 +08:00

42 lines
1.0 KiB
PHP

<?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) {}
}
?>