home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / tput / tput.c < prev   
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.5 KB  |  116 lines

  1. #ifndef LINT
  2. static char rcsid[] = "$Header: tput.c,v 1.2 86/08/22 13:39:27 badri Exp $" ;
  3. #endif LINT
  4. /*
  5.  * Copyright (C) $Date: 86/08/22 13:39:27 $
  6.  * by $Author: badri $
  7.  * University of Rochester,
  8.  * Department of Electrical Engineering.
  9.  *
  10.  * CoNtEnTs   This file contains a program to emulate the system V
  11.  * CoNtEnTs   version of tput.
  12.  *
  13.  * $Locker: badri $
  14.  * $Source: /u/users/badri/usr/src/local/tput/RCS/tput.c,v $
  15.  * $Revision: 1.2 $
  16.  *
  17.  * History of this release:
  18.  * $Log:    tput.c,v $
  19.  * Revision 1.2  86/08/22  13:39:27  badri
  20.  * 1. Corrected a bug that would cause %d to fail after %%.
  21.  * 2. Included XTABS handling.
  22.  * 3. General cleanup of code.
  23.  * 
  24.  * Revision 1.1  86/08/21  19:23:33  badri
  25.  * Initial revision
  26.  * 
  27.  */
  28. #include <sgtty.h>
  29.  
  30. #ifndef XTABS
  31. #define XTABS 0006000
  32. #endif  XTABS
  33.  
  34. #define LARGEBUF 1024
  35. #define SMALLBUF   64
  36. #define OUTPUT      1
  37. #define SUCCESS     0
  38. #define FAILURE     1
  39. #define AFFLINES    1
  40.  
  41. int errno;
  42.  
  43. main(argc,argv)
  44. int argc;
  45. char *argv[];
  46. {
  47.     char bp[LARGEBUF], *getenv(), *tgetstr(), *tgoto(),
  48.          buf[SMALLBUF], *area, *ptr;
  49.     int outc();
  50.     struct sgttyb ttyprm;
  51.     short ttyflg;
  52.  
  53.     if (argc < 2) exit(FAILURE);
  54.  
  55.     switch (tgetent(bp,getenv("TERM")))
  56.     {
  57.         case -1:
  58.             exit(FAILURE);
  59.  
  60.         case  0:
  61.             exit(FAILURE);
  62.  
  63.         case  1:
  64.             break;
  65.     }
  66.     area = buf;
  67.     if (*(ptr=tgetstr(argv[1],&area)) == '\0') exit(FAILURE);
  68.  
  69.     /*
  70.      * Examine if cursor movement specified. This is done
  71.      * by looking for % followed by any but %. Since %%
  72.      * is a single %, we have to make sure that %% followed
  73.      * by any but % is not interpreted as a format.
  74.      * If cursor movement is specified then tgoto needs
  75.      * to be invoked. Else put as is.
  76.      */
  77.     ptr = buf;
  78.     while (*ptr != '\0')
  79.     {
  80.         if (*(ptr++) != '%') continue;
  81.         if (*ptr != '\0' && *(ptr++) != '%')
  82.         {
  83.             /* This string is a cm string. */
  84.             if (argc != 4) exit(FAILURE);
  85.  
  86.             if (*(ptr=tgoto(buf,atoi(argv[2]),atoi(argv[3]))) == 'O'
  87.                 && *(ptr+1) == 'O' && *(ptr+2) == 'P'
  88.                 && *(ptr+3) == 'S') exit(FAILURE);
  89.  
  90.             /* Turn off XTABS, but save old flags first. */
  91.             if (gtty(OUTPUT,&ttyprm) < 0) exit(errno);
  92.             ttyflg = ttyprm.sg_flags;
  93.  
  94.             ttyprm.sg_flags &= ~XTABS;
  95.             if (stty(OUTPUT,&ttyprm) < 0) exit(errno);
  96.  
  97.             tputs(ptr,AFFLINES,outc);
  98.  
  99.             /* Restore old flags. */
  100.             ttyprm.sg_flags = ttyflg;
  101.             if (stty(OUTPUT,&ttyprm) < 0) exit(errno);
  102.             exit(SUCCESS);
  103.         }
  104.     exit(FAILURE);
  105.     }
  106.     tputs(buf,AFFLINES,outc);
  107.     exit(errno);
  108. }
  109.  
  110. outc(c)
  111. char c;
  112. {
  113.     if (write(OUTPUT,&c,sizeof(char)) < 0) exit(errno);
  114.     return(SUCCESS);
  115. }
  116.