home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / unixlib36d / test / c / tcaptest < prev    next >
Encoding:
Text File  |  1994-03-08  |  3.2 KB  |  104 lines

  1. /* tcaptest.c (c) Copyright 1990 H.Rogers */
  2.  
  3. /* This program tests the termcap(3) library routines. */
  4. /* For full information on termcap, read the appropriate entries
  5.  * in sections 3 and 5 of the UNIX Programmer's Manual. */
  6.  
  7. #include <stdio.h>        /* standard I/O and UNIX */
  8. #include <stdlib.h>        /* standard library */
  9.  
  10. #include "termcap.h"        /* screen handling functions */
  11.  
  12. #ifndef EMULATE
  13. #include "termio.h"        /* terminal I/O */
  14.  
  15. extern int ioctl (int, int, void *);
  16. #endif
  17.  
  18. short ospeed;            /* terminal speed */
  19. char PC, *BC, *UP;        /* terminal capabilities */
  20. static char *CL, *CM, *SO, *SE;
  21. static int LI, CO;
  22.  
  23. static char tent[1024];        /* terminal entry buffer */
  24. static char tbuf[512];        /* terminal capabilities buffer */
  25. static char *tbufp;        /* index pointer for '' */
  26.  
  27. int out (char);            /* single character output function */
  28.  
  29. int
  30. main ()
  31. {
  32.   int i, j;            /* general purpose */
  33.   int fd;            /* file descriptor of tty */
  34. #ifndef EMULATE
  35.   struct termio t[1];        /* terminal I/O control structure */
  36. #endif
  37.  
  38.   if (tgetent (tent, getenv ("TERM")) < 1)    /* get terminal entry */
  39.     {
  40.       fprintf (stderr, "tcaptest: tgetent returned < 1 (have you set TERM ?)\n");
  41.       exit (1);
  42.     }
  43.  
  44.   tbufp = tbuf;            /* initialise index pointer */
  45.  
  46.   if (tgetflag ("pc"))        /* get 'pc' capability */
  47.     PC = *tgetstr ("pc", &tbufp);    /* PC is a library variable */
  48.   else
  49.     PC = '\000';        /* defaults to '\000' */
  50.   if (tgetflag ("bs"))        /* get 'bs' capability */
  51.     BC = "\010";        /* 'bs' capability => BC = "\010" */
  52.   else
  53.     BC = tgetstr ("bc", &tbufp);    /* get 'bc' capability since no 'bs' */
  54.   UP = tgetstr ("up", &tbufp);    /* get 'up' - move cursor up */
  55.   CL = tgetstr ("cl", &tbufp);    /* get 'cl' - clear screen */
  56.   CM = tgetstr ("cm", &tbufp);    /* get 'cm' - cursor motion */
  57.   SO = tgetstr ("so", &tbufp);    /* get 'so' - start 'standout' mode */
  58.   SE = tgetstr ("se", &tbufp);    /* get 'se' - end 'standout' mode */
  59.  
  60.   LI = tgetnum ("li");        /* get 'li' - number of lines */
  61.   CO = tgetnum ("co");        /* get 'co' - number of columns */
  62.  
  63. /* now check all capabilities were present */
  64.  
  65.   if ((!BC) || (!UP) || (!CL) || (!CM) || (!SO) || (!SE))
  66.     {
  67.       fputs ("tcaptest: terminal must have bc, up, cl, cm, so, and se\n", stderr);
  68.       exit (1);
  69.     }
  70.  
  71. #ifndef EMULATE
  72.   fd = fileno (stdout);        /* get terminal file descriptor */
  73.   ioctl (fd, TCGETA, t);    /* get terminal I/O control */
  74.   ospeed = t->c_cflag & CBAUD;    /* set ospeed to baud rate */
  75.   t->c_oflag &= (~XTABS);    /* turn off XTABS for termcap */
  76.   ioctl (fd, TCSETA, t);    /* set terminal I/O control */
  77. #endif
  78.  
  79.   tputs (CL, 0, out);        /* clear screen */
  80.   for (i = 0; i < LI - 2; i++)
  81.     {
  82.       j = (i << 1);
  83.       if (j > CO - 6)
  84.     j = CO - 6;
  85.       tputs (tgoto (CM, j, i), 0, out);        /* goto ((i << 1), i) */
  86.       fputs ("Hi..", stdout);    /* print "Hi.." */
  87.       tputs (SO, 0, out);    /* start 'standout' mode */
  88.       j = CO - 6 - (i << 1);
  89.       if (j < 0)
  90.     j = 0;
  91.       tputs (tgoto (CM, j, i), 0, out);        /* goto (74 - (i << 1), i) */
  92.       fputs ("Hi..", stdout);    /* print "Hi.." */
  93.       tputs (SE, 0, out);    /* end 'standout' mode */
  94.     }
  95.   printf ("\ncolumns: %d\tlines: %d\n", CO, LI);
  96.   return 0;
  97. }
  98.  
  99. int
  100. out (char c)            /* write character to terminal */
  101. {
  102.   return (putchar (c));        /* use putchar() */
  103. }
  104.