blob: 86b2931f03f764c4b89450cb14e911ee2b1d0747 (
plain) (
tree)
|
|
<?php
class ScriptBuilderBash extends ScriptBuilderBase
{
public function output($string)
{
echo $string;
}
public function bootstrapLive() { return false; }
public function getMenu(IPxeMenu $menu, bool $bootstrap)
{
return $this->menuToScript($menu);
}
public function getBootEntry($entry)
{
if (!$entry) {
return "echo 'Invalid boot entry id'\nread -n1 -r _\n";
}
return $entry->toScript($this);
}
public function getMenuEntry($entry, $honorPassword = true)
{
if ($entry === null)
return "echo 'Invalid menu entry id - press any key to continue'\nread -n1 -r _\n";
return $entry->getBootEntryScript($this);
}
public function getSpecial($special)
{
return ''; // We can't really do localboot here I guess
}
public function menuToScript($menu)
{
$output = "declare -A items_name items_gap hotkey_item\ndeclare menu_default menu_timeout menu_title\n";
foreach ($menu->items as $entry) {
$id = $entry->menuentryid;
if ($entry->bootEntry === null || (!empty($this->platform) && !$entry->bootEntry->supportsMode($this->platform)))
continue;
if (!$entry->hidden) {
$output .= 'items_name[' . $id . ']=' . $this->bashString($entry->title) . "\n";
if ($entry->gap) {
$output .= 'items_gap[' . $id . "]=1\n";
}
}
if ($entry->hotkey !== false) {
$output .= 'hotkey_item[' . $entry->hotkey . ']=' . $id . "\n";
}
if ($id == $menu->defaultEntryId) {
$output .= "menu_default={$id}\n";
}
}
return $output . "menu_timeout=" . $menu->timeoutMs
. "\nmenu_title=" . $this->bashString($menu->title) . "\n";
}
public function execDataToScript($agnostic, $bios, $efi) : string
{
if ($agnostic !== null)
return $this->execDataToScriptInternal($agnostic);
if ($bios !== null && $this->platform === BootEntry::BIOS)
return $this->execDataToScriptInternal($bios);
if ($efi !== null && $this->platform === BootEntry::EFI)
return $this->execDataToScriptInternal($efi);
return $this->execDataToScriptInternal($bios ?? $efi ?? new ExecData());
}
private function execDataToScriptInternal(ExecData $entry) : string
{
$entry->sanitize();
$script = "declare -a initrd\ndeclare kernel kcl\n";
if (!empty($entry->initRd)) {
foreach ($entry->initRd as $initrd) {
if (empty($initrd))
continue;
$script .= 'initrd+=( ' . $this->bashString($initrd) . " )\n";
}
}
$script .= 'kernel=' . $this->bashString($entry->executable) . "\n";
$script .= 'kcl="' . str_replace('"', '"\\""', $entry->commandLine) . "\"\n"; // Allow expansion
return $script;
}
private function bashString($string)
{
if (strpos($string, "'") === false) {
return "'$string'";
}
return "'" . str_replace("'", "'\\''", $string) . "'";
}
}
|