"Fossies" - the Fresh Open Source Software Archive

Member "seed7/src/trm_cap.c" (11 Dec 2019, 14913 Bytes) of package /linux/misc/seed7_05_20210223.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "trm_cap.c" see the Fossies "Dox" file reference documentation.

    1 /********************************************************************/
    2 /*                                                                  */
    3 /*  trm_cap.c     Driver for termcap screen access.                 */
    4 /*  Copyright (C) 1989 - 2005  Thomas Mertes                        */
    5 /*                                                                  */
    6 /*  This file is part of the Seed7 Runtime Library.                 */
    7 /*                                                                  */
    8 /*  The Seed7 Runtime Library is free software; you can             */
    9 /*  redistribute it and/or modify it under the terms of the GNU     */
   10 /*  Lesser General Public License as published by the Free Software */
   11 /*  Foundation; either version 2.1 of the License, or (at your      */
   12 /*  option) any later version.                                      */
   13 /*                                                                  */
   14 /*  The Seed7 Runtime Library is distributed in the hope that it    */
   15 /*  will be useful, but WITHOUT ANY WARRANTY; without even the      */
   16 /*  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR */
   17 /*  PURPOSE.  See the GNU Lesser General Public License for more    */
   18 /*  details.                                                        */
   19 /*                                                                  */
   20 /*  You should have received a copy of the GNU Lesser General       */
   21 /*  Public License along with this program; if not, write to the    */
   22 /*  Free Software Foundation, Inc., 51 Franklin Street,             */
   23 /*  Fifth Floor, Boston, MA  02110-1301, USA.                       */
   24 /*                                                                  */
   25 /*  Module: Seed7 Runtime Library                                   */
   26 /*  File: seed7/src/trm_cap.c                                       */
   27 /*  Changes: 1993, 1994  Thomas Mertes                              */
   28 /*  Content: Driver for termcap screen access.                      */
   29 /*                                                                  */
   30 /********************************************************************/
   31 
   32 #include "version.h"
   33 
   34 #ifdef USE_TERMCAP
   35 #include "stdlib.h"
   36 #include "stdio.h"
   37 #include "string.h"
   38 /* #include "curses.h" */
   39 /* #include "term.h" */
   40 
   41 #include "common.h"
   42 #include "os_decls.h"
   43 #include "con_drv.h"
   44 
   45 #undef EXTERN
   46 #define EXTERN
   47 #define DO_INIT
   48 #include "trm_drv.h"
   49 #include "cap_def.h"
   50 
   51 /* #define atexit(x) */
   52 
   53 
   54 
   55 /* Configuration: */
   56 #undef EMULATE_TERMCAP
   57 
   58 
   59 
   60 #ifdef C_PLUS_PLUS
   61 
   62 extern "C" int tgetent (char *, char *);
   63 extern "C" int tgetnum (char *);
   64 extern "C" int tgetflag (char *);
   65 extern "C" char *tgetstr(char *, char **);
   66 extern "C" char *tgoto (char *, int, int);
   67 extern "C" int tputs (char *, int, int (*) (char ch));
   68 
   69 #else
   70 
   71 int tgetent (char *, char *);
   72 int tgetnum (char *);
   73 int tgetflag (char *);
   74 char *tgetstr(char *, char **);
   75 char *tgoto (char *, int, int);
   76 int tputs (char *, int, int (*) (char ch));
   77 
   78 #endif
   79 
   80 
   81 
   82 #ifdef EMULATE_TERMCAP
   83 #define tgetent  my_tgetent
   84 #define tgetnum  my_tgetnum
   85 #define tgetflag my_tgetflag
   86 #define tgetstr  my_tgetstr
   87 
   88 #define CAPBUF_SIZE 2048
   89 
   90 char capabilities[CAPBUF_SIZE + NULL_TERMINATION_LEN];
   91 
   92 
   93 
   94 int my_tgetent (char *capbuf, char *terminal_name)
   95 
   96   {
   97     FILE *term_descr_file;
   98     int len;
   99     int result;
  100 
  101   /* my_tgetent */
  102     if ((term_descr_file = fopen(terminal_name, "r")) != NULL) {
  103       len = fread(capbuf, 1, CAPBUF_SIZE, term_descr_file);
  104       capbuf[len] = '\0';
  105       fclose(term_descr_file);
  106       result = 0;
  107     } else {
  108       capbuf[0] = '\0';
  109       result = -1;
  110     } /* if */
  111     strcpy(capabilities, capbuf);
  112     return result;
  113   } /* my_tgetent */
  114 
  115 
  116 
  117 int my_tgetnum (char *code)
  118 
  119   {
  120     char buffer[33];
  121     char *found;
  122     int result;
  123 
  124   /* my_tgetnum */
  125     result = -1;
  126     buffer[0] = ':';
  127     strcpy(&buffer[1], code);
  128     strcat(buffer, "#");
  129     if ((found = strstr(capabilities, buffer)) != NULL) {
  130       sscanf(found + 4, "%d", &result);
  131     } /* if */
  132 #ifdef TRACE_CAPS
  133     printf("%s#%d\n", code, result);
  134 #endif
  135     return result;
  136   } /* my_tgetnum */
  137 
  138 
  139 
  140 int my_tgetflag (char *code)
  141 
  142   {
  143     char buffer[33];
  144     char *found;
  145     int result;
  146 
  147   /* my_tgetflag */
  148     result = FALSE;
  149     buffer[0] = ':';
  150     strcpy(&buffer[1], code);
  151     strcat(buffer, ":");
  152     if ((found = strstr(capabilities, buffer)) != NULL) {
  153       result = TRUE;
  154     } /* if */
  155 #ifdef TRACE_CAPS
  156     printf("%s:%d\n", code, result);
  157 #endif
  158     return result;
  159   } /* my_tgetflag */
  160 
  161 
  162 
  163 char *my_tgetstr(char *code, char **area)
  164 
  165   {
  166     char buffer[33];
  167     char *found;
  168     char *end;
  169     char *from;
  170     char to_buf[513];
  171     char *to;
  172     char *result;
  173 
  174   /* my_tgetstr */
  175     result = NULL;
  176     buffer[0] = ':';
  177     strcpy(&buffer[1], code);
  178     strcat(buffer, "=");
  179     if ((found = strstr(capabilities, buffer)) != NULL) {
  180       if ((end = strchr(found + 4, ':')) != NULL) {
  181         from = found + 4;
  182         to = to_buf;
  183         while (from != end) {
  184           if (*from == '\\') {
  185             from++;
  186             if (*from == 'E' || *from == 'e') {
  187               *to++ = '\033';
  188             } else if (*from == 'n') {
  189               *to++ = '\n';
  190             } else if (*from == 'l') {
  191               *to++ = '\n';
  192             } else if (*from == 'r') {
  193               *to++ = '\r';
  194             } else if (*from == 't') {
  195               *to++ = '\t';
  196             } else if (*from == 'b') {
  197               *to++ = '\b';
  198             } else if (*from == 'f') {
  199               *to++ = '\f';
  200             } else if (*from == 's') {
  201               *to++ = ' ';
  202             } else if (*from == '0') {
  203               *to++ = '\200';
  204             } else {
  205               *to++ = *from;
  206             } /* if */
  207             from++;
  208           } else if (*from == '^') {
  209             from++;
  210             if (*from >= 'a' && *from <= 'z') {
  211               *to++ = *from - 'a' + 1;
  212             } else if (*from >= 'A' && *from <= 'Z') {
  213               *to++ = *from - 'A' + 1;
  214             } else {
  215               *to++ = *from;
  216             } /* if */
  217             from++;
  218           } else {
  219             *to++ = *from++;
  220           } /* if */
  221         } /* while */
  222         *to = '\0';
  223         if ((result = (char *) malloc(to - to_buf + 1)) != NULL) {
  224           strcpy(result, to_buf);
  225         } /* if */
  226       } /* if */
  227     } /* if */
  228 #ifdef TRACE_CAPS
  229     printf("%s=\"%s\"\n", code, result);
  230 #endif
  231     return result;
  232   } /* my_tgetstr */
  233 #endif
  234 
  235 
  236 
  237 int getcaps (void)
  238 
  239   {
  240     static char capbuf[CAPBUF_SIZE + NULL_TERMINATION_LEN];
  241     int return_code;
  242     char *area;
  243     char *home_dir_path;
  244     char *terminal_name;
  245     char term_descr_file_name[256];
  246     int len;
  247 
  248   /* getcaps */
  249     if (!caps_initialized) {
  250       terminal_name = getenv("TERM");
  251 #ifdef EMULATE_TERMCAP
  252       home_dir_path = getenv("HOME");
  253       if (home_dir_path != NULL) {
  254         strcpy(term_descr_file_name, home_dir_path);
  255         len = strlen(term_descr_file_name);
  256       } else {
  257         len = 0;
  258       } /* if */
  259       if (len > 0 && term_descr_file_name[len - 1] != '/') {
  260         term_descr_file_name[len] = '/';
  261         len++;
  262       } /* if */
  263       strcpy(&term_descr_file_name[len], ".term");
  264       if (terminal_name != NULL) {
  265         strcat(term_descr_file_name, terminal_name);
  266       } /* if */
  267 /*    setupterm(terminal_name, fileno(stdout), &err); */
  268 /* printf(":%s:%s:\n", terminal_name, term_descr_file_name); */
  269       return_code = tgetent(capbuf, term_descr_file_name);
  270 #else
  271       return_code = tgetent(capbuf, terminal_name);
  272 #endif
  273 /*
  274 printf(":%d:\n", return_code);
  275 printf(":%d:\n", strlen(capbuf));
  276 printf("\"%s\"\n", capbuf);
  277 */
  278       area = capbuf;
  279       insert_line =           tgetstr("al", &area);
  280       auto_right_margin =    tgetflag("am");
  281       BC =                    tgetstr("bc", &area);
  282       clr_eos =               tgetstr("cd", &area);
  283       clr_eol =               tgetstr("ce", &area);
  284       clear_screen =          tgetstr("cl", &area);
  285       columns =               tgetnum("co");
  286       cursor_address =        tgetstr("cm", &area);
  287       carriage_return =       tgetstr("cr", &area);
  288       delete_character =      tgetstr("dc", &area);
  289       delete_line =           tgetstr("dl", &area);
  290       enter_delete_mode =     tgetstr("dm", &area);
  291       cursor_down =           tgetstr("do", &area);
  292       exit_delete_mode =      tgetstr("ed", &area);
  293       exit_insert_mode =      tgetstr("ei", &area);
  294       hard_copy =            tgetflag("hc");
  295       insert_character =      tgetstr("ic", &area);
  296       enter_insert_mode =     tgetstr("im", &area);
  297       IP =                    tgetstr("ip", &area);
  298       cursor_left =           tgetstr("le", &area);
  299       lines =                 tgetnum("li");
  300       move_insert_mode =     tgetflag("mi");
  301       cursor_right =          tgetstr("nd", &area);
  302       pad_char =              tgetstr("pc", &area);
  303       cursor_normal =         tgetstr("ve", &area);
  304       exit_standout_mode =    tgetstr("se", &area);
  305       enter_standout_mode =   tgetstr("so", &area);
  306       exit_ca_mode =          tgetstr("te", &area);
  307       enter_ca_mode =         tgetstr("ti", &area);
  308       cursor_up =             tgetstr("up", &area);
  309       cursor_visible =        tgetstr("vs", &area);
  310       cursor_invisible =      tgetstr("vi", &area);
  311       enter_reverse_mode =    tgetstr("mr", &area);
  312       ceol_standout_glitch = tgetflag("xs");
  313       parm_insert_line =      tgetstr("AL", &area);
  314       parm_delete_line =      tgetstr("DL", &area);
  315       keypad_local =          tgetstr("ke", &area);
  316       keypad_xmit =           tgetstr("ks", &area);
  317       key_backspace =         tgetstr("kb", &area);
  318       key_btab =              tgetstr("kB", &area);
  319       key_b2 =                tgetstr("K2", &area);
  320       key_catab =             tgetstr("ka", &area);
  321       key_clear =             tgetstr("kC", &area);
  322       key_ctab =              tgetstr("kt", &area);
  323       key_dc =                tgetstr("kD", &area);
  324       key_dl =                tgetstr("kL", &area);
  325       key_down =              tgetstr("kd", &area);
  326       key_eic =               tgetstr("kM", &area);
  327       key_end =               tgetstr("@7", &area);
  328       key_enter =             tgetstr("@8", &area);
  329       key_eol =               tgetstr("kE", &area);
  330       key_eos =               tgetstr("kS", &area);
  331       key_f0 =                tgetstr("k0", &area);
  332       key_f1 =                tgetstr("k1", &area);
  333       key_f2 =                tgetstr("k2", &area);
  334       key_f3 =                tgetstr("k3", &area);
  335       key_f4 =                tgetstr("k4", &area);
  336       key_f5 =                tgetstr("k5", &area);
  337       key_f6 =                tgetstr("k6", &area);
  338       key_f7 =                tgetstr("k7", &area);
  339       key_f8 =                tgetstr("k8", &area);
  340       key_f9 =                tgetstr("k9", &area);
  341       key_f10 =               tgetstr("k;", &area);
  342       key_f11 =               tgetstr("F1", &area);
  343       key_f12 =               tgetstr("F2", &area);
  344       key_f13 =               tgetstr("F3", &area);
  345       key_f14 =               tgetstr("F4", &area);
  346       key_f15 =               tgetstr("F5", &area);
  347       key_f16 =               tgetstr("F6", &area);
  348       key_f17 =               tgetstr("F7", &area);
  349       key_f18 =               tgetstr("F8", &area);
  350       key_f19 =               tgetstr("F9", &area);
  351       key_f20 =               tgetstr("FA", &area);
  352       key_f21 =               tgetstr("FB", &area);
  353       key_f22 =               tgetstr("FC", &area);
  354       key_f23 =               tgetstr("FD", &area);
  355       key_f24 =               tgetstr("FE", &area);
  356       key_f25 =               tgetstr("FF", &area);
  357       key_f26 =               tgetstr("FG", &area);
  358       key_f27 =               tgetstr("FH", &area);
  359       key_f28 =               tgetstr("FI", &area);
  360       key_f29 =               tgetstr("FJ", &area);
  361       key_f30 =               tgetstr("FK", &area);
  362       key_f30 =               tgetstr("FK", &area);
  363       key_f31 =               tgetstr("FL", &area);
  364       key_f32 =               tgetstr("FM", &area);
  365       key_f33 =               tgetstr("FN", &area);
  366       key_f34 =               tgetstr("FO", &area);
  367       key_f35 =               tgetstr("FP", &area);
  368       key_f36 =               tgetstr("FQ", &area);
  369       key_f37 =               tgetstr("FR", &area);
  370       key_f38 =               tgetstr("FS", &area);
  371       key_f39 =               tgetstr("FT", &area);
  372       key_f40 =               tgetstr("FU", &area);
  373       key_f41 =               tgetstr("FV", &area);
  374       key_f42 =               tgetstr("FW", &area);
  375       key_f43 =               tgetstr("FX", &area);
  376       key_f44 =               tgetstr("FY", &area);
  377       key_f45 =               tgetstr("FZ", &area);
  378       key_f46 =               tgetstr("Fa", &area);
  379       key_f47 =               tgetstr("Fb", &area);
  380       key_f48 =               tgetstr("Fc", &area);
  381       key_f49 =               tgetstr("Fd", &area);
  382       key_f50 =               tgetstr("Fe", &area);
  383       key_f51 =               tgetstr("Ff", &area);
  384       key_f52 =               tgetstr("Fg", &area);
  385       key_f53 =               tgetstr("Fh", &area);
  386       key_f54 =               tgetstr("Fi", &area);
  387       key_f55 =               tgetstr("Fj", &area);
  388       key_f56 =               tgetstr("Fk", &area);
  389       key_f57 =               tgetstr("Fl", &area);
  390       key_f58 =               tgetstr("Fm", &area);
  391       key_f59 =               tgetstr("Fn", &area);
  392       key_f60 =               tgetstr("Fo", &area);
  393       key_f61 =               tgetstr("Fp", &area);
  394       key_f62 =               tgetstr("Fq", &area);
  395       key_f63 =               tgetstr("Fr", &area);
  396       key_find =              tgetstr("@0", &area);
  397       key_home =              tgetstr("kh", &area);
  398       key_ic =                tgetstr("kI", &area);
  399       key_il =                tgetstr("kA", &area);
  400       key_left =              tgetstr("kl", &area);
  401       key_ll =                tgetstr("kH", &area);
  402       key_npage =             tgetstr("kN", &area);
  403       key_ppage =             tgetstr("kP", &area);
  404       key_right =             tgetstr("kr", &area);
  405       key_select=             tgetstr("*6", &area);
  406       key_sf =                tgetstr("kF", &area);
  407       key_sr =                tgetstr("kR", &area);
  408       key_stab =              tgetstr("kT", &area);
  409       key_up =                tgetstr("ku", &area);
  410       if (cursor_address == NULL) {
  411         printf("cursor_address == NULL\n");
  412       } /* if */
  413       caps_initialized = TRUE;
  414     } /* if */
  415 /*  printf("%d <%s>\n", strlen(cursor_address), cursor_address); */
  416     return caps_initialized;
  417   } /* getcaps */
  418 
  419 
  420 
  421 int outch (char ch)
  422 
  423   { /* outch */
  424     return putchar(ch);
  425   } /* outch */
  426 
  427 
  428 
  429 void putcontrol (char *control)
  430 
  431   { /* putcontrol */
  432     if (control != NULL) {
  433       tputs(control, 1, outch);
  434     } /* if */
  435   } /* putcontrol */
  436 
  437 #endif