cursor movement using termcap

sloane at noscvax.UUCP sloane at noscvax.UUCP
Tue May 1 01:04:40 AEST 1984


In response to:
---------------------------
>I'm trying to incorporate the cursor movement (cm) option from
>termcap.  Can someone describe the proper sequence of calls needed
>to move the cursor to column X, row Y using tgetstr(), tputs(),  
>and tgoto()?  I don't have access to the curses package.
>
---------------------------


These routines allow some screen control using termcap strings as defined
in the TERM variable. They have *NOT* yet been tested, although they
probably work since they were 'borrowed' from another program. Good luck...

Compile these with: cc -o displib displib.c -ltermlib
Use as: #include displib

The usual disclaimers apply...
      
	 Gary K. Sloane
      c/o Naval Ocean Systems Center
      COTD Building 1 Room B205
      San Diego, California 92152

      MILNET: sloane at nosc
      UUCP:   ...{sdcsvax}!noscvax!sloane
      DDD: (619) 225-8401 x391


---------------------------CUT HERE-------------------
#include <stdio.h>
#define bool int

/* terminal capabilities to get from termcap    */
char *AL,       /* insert line                  */
     *CD,       /* clear to end of display      */
     *CE,       /* clear to end of line         */
     *CL,       /* clear display                */
     *CM,       /* cursor motion string         */
     *DL,       /* delete line                  */
     *IS,       /* initialize terminal          */
      PC,       /* pad character                */
     *TE,       /* initialize CM mode            */
     *TI;       /* terminate CM mode            */

/* termcap routines */
char *tgetent(), /* extract termcap entry            */
     *tgetstr(), /* get string value of a capability */
     *tgoto();   /* return cursor addressing string  */

/* getenv routine and vars */
char *getenv(),  /* get environment variables        */
     *term;      /* pointer to $TERM value           */

/* termcap and getenv data buffers */
char tbuf[1024],
     tcapbuf[256];

/* screen parameters */
int  scrlen,      /* screen length                    */
     scrwid,      /* screen width -1                  */
     curline,     /* current line                     */
     curcol;      /* current column                   */

/************************************************************************/
/* routine to get terminal capabilities, validate & initialize terminal */
/************************************************************************/
initscr()
{
char *ap, *pcptr;

/* get terminal type and termcap entry */
term = getenv("TERM");
if(term == 0)
    {
    fprintf(stderr, "** No TERM variable in environment **\n");
    exit(1);
    }
switch(tgetent(tbuf, term))
    {
    case -1:
        {
        fprintf(stderr, "** Cannot open termcap data file **\n");
        exit(2);
        }
    case  0:
        {
        fprintf(stderr, "** %s: Unknown terminal type **\n", term);
        exit(3);
        }
    }

/* make sure it's not a hard-copy terminal */
if(tgetnum("hc") != -1)
    {
    fprintf(stderr, "** You cannot run this program from a hard copy terminal **\n");
    exit(4);
    }

/* get terminal attributes and capabilities */
ap = tcapbuf;
AL = tgetstr("al", &ap);
CD = tgetstr("cd", &ap);
CE = tgetstr("ce", &ap);
CL = tgetstr("cl", &ap);
CM = tgetstr("cm", &ap);
DL = tgetstr("dl", &ap);
IS = tgetstr("is", &ap);
TE = tgetstr("te", &ap);
TI = tgetstr("ti", &ap);
if(pcptr = tgetstr("pc", &ap))
   PC = *pcptr;
scrlen = tgetnum("li");
scrwid = tgetnum("co");

/* make sure terminal has required capabilities */
if( (CM == 0) || (CL == 0) )
    {
    fprintf(stderr, "** Terminal must have clear and cursor movement capabilities **\n");
    exit(5);
    }
if( (scrlen <= 0) || (scrwid <= 0) )
    {
    fprintf(stderr, "** Termcap entry must show screen size **\n");
    exit(6);
    }

/* send initialization string if defined */
if(IS != 0)
    {
    putpad(IS);
    }

/* home cursor and clear screen either way */
home();
clear();

/* normal exit */
exit(0);
}

/**************************************************************************/
/* routines to send control sequences to terminal with appropriate padding */
/**************************************************************************/
outch(c)
char c;
{
putchar(c);
}

putpad(str)
char *str;
{
if(str)
    tputs(str, 0, outch);
}

/*********************************/
/* routine to move cursor home   */
/* uses curxy so HO is not req'd */
/*********************************/
home()
{
gotoxy(0,0);
}

/***************************/
/* routine to clear screen */
/***************************/ 
clear()
{
putpad(CL);
}

/******************************************/
/* routine to move cursor to position x,y */
/******************************************/
gotoxy(lin,col)
int lin, col;
{
char *cmstr = tgoto(CM, col, lin);
/* if special mode for CM, start it */
if(TI != 0)
    putpad(TI);
/* do the CM */
putpad(cmstr);
/* if special mode for CM, end it */
if(TE != 0)
    putpad(TE);
/* set current line and column */
curline = lin;
curcol = col;
}

/***********************************/ 
/* routine to clear to end of line */
/***********************************/
cleol()
{
int i;

/* if terminal has CE, do it */
if (CE != 0)
    putpad(CE);
/* if terminal doesn't have CE, fake it */ 
else
    {
    for(i=curcol; i<scrwid; i++)
        putchar(' ');
    gotoxy(curline, curcol);
    }
}

/**************************************/
/* routine to clear to end of display */
/**************************************/
cleod()
{
int lin, col;

/* if terminal has CD, do it */
if (CD != 0)
    putpad(CD);
/* if it doesn't have CD, fake it */
else
    {
    /* save cursor position */
    lin = curline;
    col = curcol;
    /* clear rest of current line */
    cleol();
    /* clear rest of screen */
    while (curline < scrlen)
        {
        gotoxy(++curline, 0);
        cleol();
        }
    /* return to correct position */
    gotoxy(lin,col);
    }
}



More information about the Comp.unix mailing list