// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
/******************************************************************************
*
* Module Name: osunixxf - UNIX OSL interfaces
*
* Copyright (C) 2000 - 2023, Intel Corp.
*
*****************************************************************************/
/*
* These interfaces are required in order to compile the ASL compiler and the
* various ACPICA tools under Linux or other Unix-like system.
*/
#include <acpi/acpi.h>
#include "accommon.h"
#include "amlcode.h"
#include "acparser.h"
#include "acdebug.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/time.h>
#include <semaphore.h>
#include <pthread.h>
#include <errno.h>
#define _COMPONENT ACPI_OS_SERVICES
ACPI_MODULE_NAME("osunixxf")
/* Upcalls to acpi_exec */
void
ae_table_override(struct acpi_table_header *existing_table,
struct acpi_table_header **new_table);
typedef void *(*PTHREAD_CALLBACK) (void *);
/* Buffer used by acpi_os_vprintf */
#define ACPI_VPRINTF_BUFFER_SIZE 512
#define _ASCII_NEWLINE '\n'
/* Terminal support for acpi_exec only */
#ifdef ACPI_EXEC_APP
#include <termios.h>
struct termios original_term_attributes;
int term_attributes_were_set = 0;
acpi_status acpi_ut_read_line(char *buffer, u32 buffer_length, u32 *bytes_read);
static void os_enter_line_edit_mode(void);
static void os_exit_line_edit_mode(void);
/******************************************************************************
*
* FUNCTION: os_enter_line_edit_mode, os_exit_line_edit_mode
*
* PARAMETERS: None
*
* RETURN: None
*
* DESCRIPTION: Enter/Exit the raw character input mode for the terminal.
*
* Interactive line-editing support for the AML debugger. Used with the
* common/acgetline module.
*
* readline() is not used because of non-portability. It is not available
* on all systems, and if it is, often the package must be manually installed.
*
* Therefore, we use the POSIX tcgetattr/tcsetattr and do the minimal line
* editing that we need in acpi_os_get_line.
*
* If the POSIX tcgetattr/tcsetattr interfaces are unavailable, these
* calls will also work:
* For os_enter_line_edit_mode: system ("stty cbreak -echo")
* For os_exit_line_edit_mode: system ("stty cooked echo")
*
*****************************************************************************/
static void os_enter_line_edit_mode(void)
{
struct termios local_term_attributes;
term_attributes_were_set = 0;
/* STDIN must be a terminal */
if (!isatty(STDIN_FILENO)) {
return;
}
/* Get and keep the original attributes */
if (tcgetattr(STDIN_FILENO, &original_term_attributes)) {
fprintf(stderr, "Could not get terminal attributes!\n");
return;
}
/* Set the new attributes to enable raw character input */
memcpy(&local_term_attributes, &original_term_attributes,
sizeof(struct termios));
local_term_attributes.c_lflag &= ~(ICANON | ECHO);
local_term_attributes.c_cc[VMIN] = 1;
local_term_attributes.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSANOW, &local_term_attributes)) {
fprintf(stderr, "Could not set terminal attributes!\n");
return;
}
term_attributes_were_set = 1;
}
static void os_exit_line_edit_mode(void)
{
if (!term_attributes_were_set) {
return;
}
/* Set terminal attributes back to the original values */
if (tcsetattr(STDIN_FILENO, TCSANOW, &original_term_attributes)) {
fprintf(stderr, "Could not restore terminal attributes!\n");
}