home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / util / vim-3.0.lha / Vim / src / term.c < prev    next >
C/C++ Source or Header  |  1994-08-09  |  20KB  |  869 lines

  1. /* vi:ts=4:sw=4
  2.  *
  3.  * VIM - Vi IMproved        by Bram Moolenaar
  4.  *
  5.  * Read the file "credits.txt" for a list of people who contributed.
  6.  * Read the file "uganda.txt" for copying and usage conditions.
  7.  */
  8. /*
  9.  *
  10.  * term.c: functions for controlling the terminal
  11.  *
  12.  * primitive termcap support for Amiga and MSDOS included
  13.  *
  14.  * NOTE: padding and variable substitution is not performed,
  15.  * when compiling without TERMCAP, we use tputs() and tgoto() dummies.
  16.  */
  17.  
  18. #include "vim.h"
  19. #include "globals.h"
  20. #include "param.h"
  21. #include "proto.h"
  22. #ifdef TERMCAP
  23. # ifdef linux
  24. #  include <termcap.h>
  25. #  if 0        /* only required for old versions, it's now in termcap.h */
  26.     typedef int (*outfuntype) (int);
  27. #  endif
  28. #  define TPUTSFUNCAST (outfuntype)
  29. # else
  30. #  define TPUTSFUNCAST
  31. #  ifdef AMIGA
  32. #   include "proto/termlib.pro"
  33. #  endif
  34. # endif
  35. #endif
  36.  
  37. static void parse_builtin_tcap __ARGS((Tcarr *tc, char_u *s));
  38.  
  39. /*
  40.  * Builtin_tcaps must always contain DFLT_TCAP as the first entry!
  41.  * DFLT_TCAP is used, when no terminal is specified with -T option or $TERM.
  42.  * The entries are compact, therefore they normally are included even when
  43.  * TERMCAP is defined.
  44.  * When TERMCAP is defined, the builtin entries can be accessed with
  45.  * "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
  46.  */
  47. static char_u *builtin_tcaps[] =
  48. {
  49. #ifndef NO_BUILTIN_TCAPS
  50.   (char_u *)DFLT_TCAP,        /* almost allways included */
  51. # if !defined(UNIX) && (defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS))
  52.   (char_u *)ANSI_TCAP,        /* default for unix */
  53. # endif
  54. # if !defined(AMIGA) && (defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS))
  55.   (char_u *)AMIGA_TCAP,        /* default for amiga */
  56. # endif
  57. # if !defined(MSDOS) && (defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS))
  58.   (char_u *)PCTERM_TCAP,        /* default for MSdos */
  59. # endif
  60. # if defined(MSDOS) || defined(ALL_BUILTIN_TCAPS)
  61.   (char_u *)PCANSI_TCAP,
  62. # endif
  63. # if !defined(ATARI) && defined(ALL_BUILTIN_TCAPS)
  64.   (char_u *)ATARI_TCAP,        /* default for Atari */
  65. # endif
  66. # if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
  67.   (char_u *)XTERM_TCAP,        /* always included on unix */
  68. # endif
  69. # ifdef ALL_BUILTIN_TCAPS
  70.   (char_u *)VT52_TCAP,
  71. # endif
  72. # if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
  73.   (char_u *)DEBUG_TCAP,        /* always included when debugging */
  74. # endif
  75. #else /* NO_BUILTIN_TCAPS */
  76.   (char_u *)DUMB_TCAP,        /* minimal termcap, used when everything else fails */
  77. #endif /* NO_BUILTIN_TCAPS */
  78.   NULL,
  79. };
  80.  
  81. /*
  82.  * Term_strings contains currently used terminal strings.
  83.  * It is initialized with the default values by parse_builtin_tcap().
  84.  * The values can be changed by setting the parameter with the same name.
  85.  */
  86. Tcarr term_strings;
  87.  
  88. /*
  89.  * Parsing of the builtin termcap entries.
  90.  * The terminal's name is not set, as this is already done in termcapinit().
  91.  * Chop builtin termcaps, string entries are already '\0' terminated.
  92.  * not yet implemented:
  93.  *   boolean entries could be empty strings;
  94.  *   numeric entries would need a flag (e.g. high bit of the skip byte),
  95.  *   so that parse_builtin_tcap can handle them.
  96.  */
  97.     static void
  98. parse_builtin_tcap(tc, s)
  99.     Tcarr *tc;
  100.     char_u *s;
  101. {
  102.     char_u **p = &tc->t_name;
  103.  
  104.     p++;
  105.     for (;;)
  106.     {
  107.         while (*s++)
  108.             ;
  109.         p += *s++;
  110.         if (!*s)
  111.             return;
  112.         *p++ = s;
  113.     }
  114. }
  115.  
  116. #ifdef TERMCAP
  117. # ifndef linux        /* included in <termlib.h> */
  118. #  ifndef AMIGA        /* included in proto/termlib.pro */
  119. int                tgetent();
  120. int                tgetnum();
  121. char            *tgetstr();
  122. int                tgetflag();
  123. int                tputs();
  124. #  endif /* AMIGA */
  125. #  ifndef hpux
  126. extern short    ospeed;
  127. #  endif
  128. # endif /* linux */
  129. # ifndef hpux
  130. char        *UP, *BC, PC;        /* should be extern, but some don't have them */
  131. # endif
  132. #endif /* TERMCAP */
  133.  
  134. #ifdef linux
  135. # define TGETSTR(s, p)    (char_u *)tgetstr((s), (char **)(p))
  136. #else
  137. # define TGETSTR(s, p)    (char_u *)tgetstr((s), (char *)(p))
  138. #endif
  139.  
  140.     void
  141. set_term(term)
  142.     char_u *term;
  143. {
  144.     char_u **p = builtin_tcaps;
  145. #ifdef TERMCAP
  146.     int builtin = 0;
  147. #endif
  148.     int width = 0, height = 0;
  149.  
  150.     if (!STRNCMP(term, "builtin_", (size_t)8))
  151.     {
  152.         term += 8;
  153. #ifdef TERMCAP
  154.         builtin = 1;
  155. #endif
  156.     }
  157. #ifdef TERMCAP
  158.     else
  159.     {
  160.         char_u            *p;
  161.         static char_u    tstrbuf[TBUFSZ];
  162.         char_u            tbuf[TBUFSZ];
  163.         char_u            *tp = tstrbuf;
  164.         int                i;
  165.  
  166.         i = tgetent(tbuf, term);
  167.         if (i == -1)
  168.         {
  169.             EMSG("Cannot open termcap file");
  170.             builtin = 1;
  171.         }
  172.         else if (i == 0)
  173.         {
  174.             EMSG("terminal entry not found");
  175.             builtin = 1;
  176.         }
  177.         else
  178.         {
  179.             clear_termparam();        /* clear old parameters */
  180.         /* output strings */
  181.             T_EL = TGETSTR("ce", &tp);
  182.             T_IL = TGETSTR("al", &tp);
  183.             T_CIL = TGETSTR("AL", &tp);
  184.             T_DL = TGETSTR("dl", &tp);
  185.             T_CDL = TGETSTR("DL", &tp);
  186.             T_CS = TGETSTR("cs", &tp);
  187.             T_ED = TGETSTR("cl", &tp);
  188.             T_CI = TGETSTR("vi", &tp);
  189.             T_CV = TGETSTR("ve", &tp);
  190.             T_CVV = TGETSTR("vs", &tp);
  191.             T_TP = TGETSTR("me", &tp);
  192.             T_TI = TGETSTR("mr", &tp);
  193.             T_TB = TGETSTR("md", &tp);
  194.             T_SE = TGETSTR("se", &tp);
  195.             T_SO = TGETSTR("so", &tp);
  196.             T_CM = TGETSTR("cm", &tp);
  197.             T_SR = TGETSTR("sr", &tp);
  198.             T_CRI = TGETSTR("RI", &tp);
  199.             T_VB = TGETSTR("vb", &tp);
  200.             T_KS = TGETSTR("ks", &tp);
  201.             T_KE = TGETSTR("ke", &tp);
  202.             T_TS = TGETSTR("ti", &tp);
  203.             T_TE = TGETSTR("te", &tp);
  204.  
  205.         /* key codes */
  206.             term_strings.t_ku = TGETSTR("ku", &tp);
  207.             term_strings.t_kd = TGETSTR("kd", &tp);
  208.             term_strings.t_kl = TGETSTR("kl", &tp);
  209.                 /* if cursor-left == backspace, ignore it (televideo 925) */
  210.             if (term_strings.t_kl != NULL && *term_strings.t_kl == Ctrl('H'))
  211.                 term_strings.t_kl = NULL;
  212.             term_strings.t_kr = TGETSTR("kr", &tp);
  213.             /* term_strings.t_sku = TGETSTR("", &tp); termcap code unknown */
  214.             /* term_strings.t_skd = TGETSTR("", &tp); termcap code unknown */
  215. #ifdef ARCHIE
  216.             /* Termcap code made up! */
  217.             term_strings.t_sku = tgetstr("su", &tp);
  218.             term_strings.t_skd = tgetstr("sd", &tp);
  219. #else
  220.             term_strings.t_sku = NULL;
  221.             term_strings.t_skd = NULL;
  222. #endif
  223.             term_strings.t_skl = TGETSTR("#4", &tp);
  224.             term_strings.t_skr = TGETSTR("%i", &tp);
  225.             term_strings.t_f1 = TGETSTR("k1", &tp);
  226.             term_strings.t_f2 = TGETSTR("k2", &tp);
  227.             term_strings.t_f3 = TGETSTR("k3", &tp);
  228.             term_strings.t_f4 = TGETSTR("k4", &tp);
  229.             term_strings.t_f5 = TGETSTR("k5", &tp);
  230.             term_strings.t_f6 = TGETSTR("k6", &tp);
  231.             term_strings.t_f7 = TGETSTR("k7", &tp);
  232.             term_strings.t_f8 = TGETSTR("k8", &tp);
  233.             term_strings.t_f9 = TGETSTR("k9", &tp);
  234.             term_strings.t_f10 = TGETSTR("k;", &tp);
  235.             term_strings.t_sf1 = TGETSTR("F1", &tp);    /* really function keys 11-20 */
  236.             term_strings.t_sf2 = TGETSTR("F2", &tp);
  237.             term_strings.t_sf3 = TGETSTR("F3", &tp);
  238.             term_strings.t_sf4 = TGETSTR("F4", &tp);
  239.             term_strings.t_sf5 = TGETSTR("F5", &tp);
  240.             term_strings.t_sf6 = TGETSTR("F6", &tp);
  241.             term_strings.t_sf7 = TGETSTR("F7", &tp);
  242.             term_strings.t_sf8 = TGETSTR("F8", &tp);
  243.             term_strings.t_sf9 = TGETSTR("F9", &tp);
  244.             term_strings.t_sf10 = TGETSTR("FA", &tp);
  245.             term_strings.t_help = TGETSTR("%1", &tp);
  246.             term_strings.t_undo = TGETSTR("&8", &tp);
  247.  
  248.             height = tgetnum("li");
  249.             width = tgetnum("co");
  250.  
  251.             T_MS = tgetflag("ms") ? (char_u *)"yes" : (char_u *)NULL;
  252.  
  253. # ifndef hpux
  254.             BC = (char *)TGETSTR("bc", &tp);
  255.             UP = (char *)TGETSTR("up", &tp);
  256.             p = TGETSTR("pc", &tp);
  257.             if (p)
  258.                 PC = *p;
  259.             ospeed = 0;
  260. # endif
  261.         }
  262.     }
  263.     if (builtin)
  264. #endif
  265.     {
  266.         while (*p && STRCMP(term, *p))
  267.             p++;
  268.         if (!*p)
  269.         {
  270.             fprintf(stderr, "'%s' not builtin. Available terminals are:\r\n", term);
  271.             for (p = builtin_tcaps; *p; p++)
  272. #ifdef TERMCAP
  273.                 fprintf(stderr, "\tbuiltin_%s\r\n", *p);
  274. #else
  275.                 fprintf(stderr, "\t%s\r\n", *p);
  276. #endif
  277.             if (!starting)        /* when user typed :set term=xxx, quit here */
  278.             {
  279.                 wait_return(TRUE);
  280.                 return;
  281.             }
  282.             sleep(2);
  283.             fprintf(stderr, "defaulting to '%s'\r\n", *builtin_tcaps);
  284.             sleep(2);
  285.             p = builtin_tcaps;
  286.             free(term_strings.t_name);
  287.             term_strings.t_name = strsave(term = *p);
  288.         }
  289.         clear_termparam();        /* clear old parameters */
  290.         parse_builtin_tcap(&term_strings, *p);
  291.     }
  292. /*
  293.  * special: There is no info in the termcap about whether the cursor positioning
  294.  * is relative to the start of the screen or to the start of the scrolling region.
  295.  * We just guess here. Only msdos pcterm is known to do