/* * QEMU readline utility * * Copyright (c) 2003-2004 Fabrice Bellard * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "vl.h" #define TERM_CMD_BUF_SIZE 4095 #define TERM_MAX_CMDS 64 #define NB_COMPLETIONS_MAX 256 #define IS_NORM 0 #define IS_ESC 1 #define IS_CSI 2 #define printf do_not_use_printf static char term_cmd_buf[TERM_CMD_BUF_SIZE + 1]; static int term_cmd_buf_index; static int term_cmd_buf_size; static char term_last_cmd_buf[TERM_CMD_BUF_SIZE + 1]; static int term_last_cmd_buf_index; static int term_last_cmd_buf_size; static int term_esc_state; static int term_esc_param; static char *term_history[TERM_MAX_CMDS]; static int term_hist_entry = -1; static int nb_completions; int completion_index; static char *completions[NB_COMPLETIONS_MAX]; static ReadLineFunc *term_readline_func; static int term_is_password; static char term_prompt[256]; static void *term_readline_opaque; static void term_show_prompt2(void) { term_printf("%s", term_prompt); term_flush(); term_last_cmd_buf_index = 0; term_last_cmd_buf_size = 0; term_esc_state = IS_NORM; } static void term_show_prompt(void) { term_show_prompt2(); term_cmd_buf_index = 0; term_cmd_buf_size = 0; } /* update the displayed command line */ static void term_update(void) { int i, delta, len; if (term_cmd_buf_size != term_last_cmd_buf_size || memcmp(term_cmd_buf, term_last_cmd_buf, term_cmd_buf_size) != 0) { for(i = 0; i < term_last_cmd_buf_index; i++) { term_printf("\033[D"); } term_cmd_buf[term_cmd_buf_size] = '\0'; if (term_is_password) { len = strlen(term_cmd_buf); for(i = 0; i < len; i++) term_printf("*"); } else { term_printf("%s", term_cmd_buf); } term_printf("\033[K"); memcpy(term_last_cmd_buf, term_cmd_buf, term_cmd_buf_size); term_last_cmd_buf_size = term_cmd_buf_size; term_last_cmd_buf_index = term_cmd_buf_size; } if (term_cmd_buf_index != term_last_cmd_buf_index) { delta = term_cmd_buf_index - term_last_cmd_buf_index; if (delta > 0) { for(i = 0;i < delta; i++) { term_printf("\033[C"); } } else { delta = -delta; for(i = 0;i < delta; i++) { term_printf("\033[D"); } } term_last_cmd_buf_index = term_cmd_buf_index; } term_flush(); } static void term_insert_char(int ch) { if (term_cmd_buf_index < TERM_CMD_BUF_SIZE) { memmove(term_cmd_buf + term_cmd_buf_index + 1, term_cmd_buf + term_cmd_buf_index, term_cmd_buf_size - term_cmd_buf_index); term_cmd_buf[term_cmd_buf_index] = ch; term_cmd_buf_size++; term_cmd_buf_index++; } } static void term_backward_char(void) { if (term_cmd_buf_index > 0) { term_cmd_buf_index--; } } static void term_forward_char(void) { if (term_cmd_buf_index < term_cmd_buf_size) { term_cmd_buf_index++; } } static void term_delete_char(void) { if (term_cmd_buf_index < term_cmd_buf_size) { memmo
#import <MetalKit/MetalKit.h>

@interface Renderer : NSObject <MTKViewDelegate>

-(nonnull instancetype)initWithView:(nonnull MTKView *)view;

@end
term_eol(); break; } break; default: break; } term_esc_state = IS_NORM; the_end: break; } term_update(); } void readline_start(const char *prompt, int is_password, ReadLineFunc *readline_func, void *opaque) { pstrcpy(term_prompt, sizeof(term_prompt), prompt); term_readline_func = readline_func; term_readline_opaque = opaque; term_is_password = is_password; term_show_prompt(); } const char *readline_get_history(unsigned int index) { if (index >= TERM_MAX_CMDS) return NULL; return term_history[index]; }