File: termstate.c

package info (click to toggle)
vtprint 2.0.2-15
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 488 kB
  • sloc: ansic: 1,587; sed: 733; makefile: 292; sh: 96
file content (140 lines) | stat: -rw-r--r-- 5,031 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**************************************************************************
 **************************************************************************
 **                                                                      **
 ** termstate.c     terminal state functions                             **
 ** ===========                                                          **
 **                                                                      **
 ** Purpose:        Provides functions to simplifify management of some  **
 **                 terminal states.                                     **
 **                                                                      **
 ** Author:         Garrett D'Amore <garrett@sciences.sdsu.edu>          **
 **                                                                      **
 ** Copyright:      1994, Garrett E. D'Amore                             **
 **                                                                      **
 ** NO WARRANTY:    This program is provided entirely without warranty.  **
 **                 The user assumes full responsibility for the use of  **
 **                 this program, and agrees to indemnify the author and **
 **                 the copyright holder from any damage or loss that    **
 **                 may result from the use of or inability to use this  **
 **                 program.  In simple language: YOU USE THIS PROGRAM   **
 **                 AT YOUR OWN RISK!                                    **
 **                                                                      **
 ** Warning:        None.                                                **
 **                                                                      **
 ** Restrictions:   None.                                                **
 **                                                                      **
 ** Algorithm:      None.                                                **
 **                                                                      **
 ** References:     None.                                                **
 **                                                                      **
 ** File formats:   None.                                                **
 **                                                                      **
 ** Rev. History:   June 4, 1994    Garrett D'Amore                      **
 **                 -- Initial coding.                                   **
 **                                                                      **
 ** Notes:          None.                                                **
 **                                                                      **
 **************************************************************************
 **************************************************************************/

/* >>>>>>>>>> Headers <<<<<<<<<< */

#include <errno.h>

#if defined (SGTTY)
#include <sgtty.h>
#elif defined (SYSV_TERMIO)
#include <termio.h>
#else /* POSIX_TERMIOS */
#include <termios.h>
#endif

#include <unistd.h>

#include "termstate.h"

/* >>>>>>>>>> Functions <<<<<<<<<< */

/***************************************
 *
 *  save_termstate  Save terminal state.
 *
 *  Purpose         Saves the terminal state so that we can restore
 *                  it later.  
 *
 *  Parameters      fd:     filedescriptor to save
 *                  state:  pointer to area to save state at
 *
 *  Returns         zero on success,
 *                  non-zero on failure.
 *
 */

int save_termstate (int fd, termstate *state)
{
    if (!isatty (fd)) return (ENOTTY);

#if defined (SGTTY)
    return (gtty (fd, state));
#elif defined (SYSV_TERMIO)
    return (ioctl (fd, TCGETA, state));
#else /* POSIX_TERMIOS */
    return (tcgetattr (fd, state));
#endif 

}

/***************************************
 *
 *  restore_termstate   Restore terminal state.
 *
 *  Purpose         Restores a terminal state saved with
 *                  save_termstate.
 *
 *  Parameters      fd:     file descriptor associated with terminal
 *                  state:  previously saved termstate
 *
 *  Returns         zero on success,
 *                  non-zero on error.
 *
 */

int restore_termstate (int fd, termstate *state)
{
    if (!isatty (fd)) return (ENOTTY);

#if defined (SGTTY)
    return (stty (fd, state));
#elif defined (SYSV_TERMIO)
    return (ioctl (fd, TCSETAW, state)); 
#else /* POSIX_TERMIOS */
    return (tcsetattr (fd, TCSADRAIN, state));
#endif 

}

/***************************************
 *
 *  raw_termstate   Sets a termstate to raw mode.
 *
 *  Purpose         Modifies a termstate structure so that
 *                  it has no ouput postprocessing.
 *
 *  Parameters      state:  terminal state structure to modify
 *
 *  Returns         None.
 *
 */

void raw_termstate (termstate *state)
{

#ifdef SGTTY
    state->sg_flags |= RAW;
#else /* SYSV_TERMIO || POSIX_TERMIOS */
    state->c_oflag |= OPOST;
#endif

    return;
}