Serenity Operating System
at portability 155 lines 4.1 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/String.h> 28#include <AK/HashMap.h> 29#include <assert.h> 30#include <stdio.h> 31#include <string.h> 32#include <termcap.h> 33 34//#define TERMCAP_DEBUG 35 36extern "C" { 37 38char PC; 39char* UP; 40char* BC; 41 42int tgetent(char* bp, const char* name) 43{ 44 (void)bp; 45 (void)name; 46#ifdef TERMCAP_DEBUG 47 fprintf(stderr, "tgetent: bp=%p, name='%s'\n", bp, name); 48#endif 49 PC = '\0'; 50 BC = const_cast<char*>("\033[D"); 51 UP = const_cast<char*>("\033[A"); 52 return 1; 53} 54 55static HashMap<String, const char*>* caps = nullptr; 56 57void ensure_caps() 58{ 59 if (caps) 60 return; 61 caps = new HashMap<String, const char*>; 62 caps->set("DC", "\033[%p1%dP"); 63 caps->set("IC", "\033[%p1%d@"); 64 caps->set("ce", "\033[K"); 65 caps->set("cl", "\033[H\033[J"); 66 caps->set("cr", "\015"); 67 caps->set("dc", "\033[P"); 68 caps->set("ei", ""); 69 caps->set("ic", ""); 70 caps->set("im", ""); 71 caps->set("kd", "\033[B"); 72 caps->set("kl", "\033[D"); 73 caps->set("kr", "\033[C"); 74 caps->set("ku", "\033[A"); 75 caps->set("ks", ""); 76 caps->set("ke", ""); 77 caps->set("le", "\033[D"); 78 caps->set("mm", ""); 79 caps->set("mo", ""); 80 caps->set("pc", ""); 81 caps->set("up", "\033[A"); 82 caps->set("vb", ""); 83 caps->set("am", ""); 84 caps->set("@7", ""); 85 caps->set("kH", ""); 86 caps->set("kI", "\033[L"); 87 caps->set("kh", "\033[H"); 88 caps->set("vs", ""); 89 caps->set("ve", ""); 90 caps->set("E3", ""); 91 caps->set("kD", ""); 92 caps->set("nd", "\033[C"); 93 94 caps->set("co", "80"); 95 caps->set("li", "25"); 96} 97 98char* tgetstr(const char* id, char** area) 99{ 100 ensure_caps(); 101#ifdef TERMCAP_DEBUG 102 fprintf(stderr, "tgetstr: id='%s'\n", id); 103#endif 104 auto it = caps->find(id); 105 if (it != caps->end()) { 106 char* ret = *area; 107 const char* val = (*it).value; 108 strcpy(*area, val); 109 *area += strlen(val) + 1; 110 return ret; 111 } 112 fprintf(stderr, "tgetstr: missing cap id='%s'\n", id); 113 return nullptr; 114} 115 116int tgetflag(const char* id) 117{ 118 (void)id; 119#ifdef TERMCAP_DEBUG 120 fprintf(stderr, "tgetflag: '%s'\n", id); 121#endif 122 auto it = caps->find(id); 123 if (it != caps->end()) 124 return 1; 125 return 0; 126} 127 128int tgetnum(const char* id) 129{ 130#ifdef TERMCAP_DEBUG 131 fprintf(stderr, "tgetnum: '%s'\n", id); 132#endif 133 auto it = caps->find(id); 134 if (it != caps->end()) 135 return atoi((*it).value); 136 ASSERT_NOT_REACHED(); 137} 138 139char* tgoto(const char* cap, int col, int row) 140{ 141 (void)cap; 142 (void)col; 143 (void)row; 144 ASSERT_NOT_REACHED(); 145} 146 147int tputs(const char* str, int affcnt, int (*putc)(int)) 148{ 149 (void)affcnt; 150 size_t len = strlen(str); 151 for (size_t i = 0; i < len; ++i) 152 putc(str[i]); 153 return 0; 154} 155}