~ubuntu-branches/debian/stretch/alpine/stretch

« back to all changes in this revision

Viewing changes to pico/osdep/read.c

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if !defined(lint) && !defined(DOS)
 
2
static char rcsid[] = "$Id: read.c 308 2006-12-08 20:14:42Z hubert@u.washington.edu $";
 
3
#endif
 
4
 
 
5
/*
 
6
 * ========================================================================
 
7
 * Copyright 2006 University of Washington
 
8
 *
 
9
 * Licensed under the Apache License, Version 2.0 (the "License");
 
10
 * you may not use this file except in compliance with the License.
 
11
 * You may obtain a copy of the License at
 
12
 *
 
13
 *     http://www.apache.org/licenses/LICENSE-2.0
 
14
 *
 
15
 * ========================================================================
 
16
 */
 
17
 
 
18
/*
 
19
 * Keyboard input test and read functions
 
20
 */
 
21
 
 
22
#include <system.h>             /* os-dep defs/includes */
 
23
#include <general.h>            /* generally useful definitions */
 
24
 
 
25
#include "../keydefs.h"
 
26
 
 
27
#ifndef _WINDOWS
 
28
#include "raw.h"
 
29
#endif /* !_WINDOWS */
 
30
 
 
31
#include "read.h"
 
32
 
 
33
 
 
34
static time_t _time_of_last_input;
 
35
 
 
36
time_t
 
37
time_of_last_input(void)
 
38
{
 
39
    if(_time_of_last_input == 0)
 
40
      _time_of_last_input = time((time_t *) 0);
 
41
 
 
42
    return(_time_of_last_input);
 
43
}
 
44
 
 
45
#ifndef _WINDOWS
 
46
#if     HAVE_SELECT
 
47
 
 
48
#ifdef  HAVE_SYS_SELECT_H
 
49
# include <sys/select.h>
 
50
#endif
 
51
 
 
52
 
 
53
/*
 
54
 *  Check whether or not a character is ready to be read, or time out.
 
55
 *  This version uses a select call.
 
56
 *
 
57
 *   Args: time_out -- number of seconds before it will time out.
 
58
 *
 
59
 * Result: NO_OP_IDLE:    timed out before any chars ready, time_out > 25
 
60
 *         NO_OP_COMMAND: timed out before any chars ready, time_out <= 25
 
61
 *         READ_INTR:     read was interrupted
 
62
 *         READY_TO_READ: input is available
 
63
 *         BAIL_OUT:      reading input failed, time to bail gracefully
 
64
 *         PANIC_NOW:     system call error, die now
 
65
 */
 
66
UCS
 
67
input_ready(int time_out)
 
68
{
 
69
     struct timeval tmo;
 
70
     fd_set         readfds, errfds;
 
71
     int            res;
 
72
 
 
73
     fflush(stdout);
 
74
 
 
75
     if(time_out > 0){
 
76
         /* Check to see if there are bytes to read with a timeout */
 
77
         FD_ZERO(&readfds);
 
78
         FD_ZERO(&errfds);
 
79
         FD_SET(STDIN_FD, &readfds);
 
80
         FD_SET(STDIN_FD, &errfds);
 
81
         tmo.tv_sec  = time_out;
 
82
         tmo.tv_usec = 0; 
 
83
         res = select(STDIN_FD+1, &readfds, 0, &errfds, &tmo);
 
84
         if(res < 0){
 
85
             if(errno == EINTR || errno == EAGAIN)
 
86
               return(READ_INTR);
 
87
 
 
88
             return(BAIL_OUT);
 
89
         }
 
90
 
 
91
         if(res == 0){ /* the select timed out */
 
92
             if(getppid() == 1){
 
93
                 /* Parent is init! */
 
94
                 return(BAIL_OUT);
 
95
             }
 
96
               
 
97
             /*
 
98
              * "15" is the minimum allowed mail check interval.
 
99
              * Anything less, and we're being told to cycle thru
 
100
              * the command loop because some task is pending...
 
101
              */
 
102
             return(time_out < IDLE_TIMEOUT ? NO_OP_COMMAND : NO_OP_IDLE);
 
103
         }
 
104
     }
 
105
 
 
106
     _time_of_last_input = time((time_t *) 0);
 
107
     return(READY_TO_READ);
 
108
}
 
109
 
 
110
 
 
111
#elif   HAVE_POLL
 
112
 
 
113
 
 
114
#ifdef  HAVE_STROPTS_H
 
115
# include <stropts.h>
 
116
#endif
 
117
 
 
118
#ifdef  HAVE_SYS_POLL_H
 
119
# include <poll.h>
 
120
#endif
 
121
 
 
122
/*
 
123
 *  Check whether or not a character is ready to be read, or time out.
 
124
 *  This version uses a poll call.
 
125
 *
 
126
 *   Args: time_out -- number of seconds before it will time out.
 
127
 *
 
128
 * Result: NO_OP_IDLE:    timed out before any chars ready, time_out > 25
 
129
 *         NO_OP_COMMAND: timed out before any chars ready, time_out <= 25
 
130
 *         READ_INTR:     read was interrupted
 
131
 *         READY_TO_READ: input is available
 
132
 *         BAIL_OUT:      reading input failed, time to bail gracefully
 
133
 *         PANIC_NOW:     system call error, die now
 
134
 */
 
135
UCS
 
136
input_ready(int time_out)
 
137
{
 
138
     struct pollfd pollfd;
 
139
     int           res;
 
140
 
 
141
     fflush(stdout);
 
142
 
 
143
     if(time_out > 0){
 
144
         /* Check to see if there are bytes to read with a timeout */
 
145
         pollfd.fd = STDIN_FD;
 
146
         pollfd.events = POLLIN;
 
147
         res = poll (&pollfd, 1, time_out * 1000);
 
148
         if(res >= 0){                          /* status bits OK? */
 
149
             if(pollfd.revents & (POLLERR | POLLNVAL))
 
150
               res = -1;                        /* bad news, exit below! */
 
151
             else if(pollfd.revents & POLLHUP)
 
152
               return(BAIL_OUT);
 
153
         }
 
154
 
 
155
         if(res < 0){
 
156
             if(errno == EINTR || errno == EAGAIN)
 
157
               return(READ_INTR);
 
158
 
 
159
             return(PANIC_NOW);
 
160
         }
 
161
 
 
162
         if(res == 0){ /* the select timed out */
 
163
             if(getppid() == 1){
 
164
                 /* Parent is init! */
 
165
                 return(BAIL_OUT);
 
166
             }
 
167
 
 
168
             /*
 
169
              * "15" is the minimum allowed mail check interval.
 
170
              * Anything less, and we're being told to cycle thru
 
171
              * the command loop because some task is pending...
 
172
              */
 
173
             return(time_out < IDLE_TIMEOUT ? NO_OP_COMMAND : NO_OP_IDLE);
 
174
         }
 
175
     }
 
176
 
 
177
     _time_of_last_input = time((time_t *) 0);
 
178
     return(READY_TO_READ);
 
179
}
 
180
 
 
181
#endif /* HAVE_POLL */
 
182
 
 
183
 
 
184
/*
 
185
 * Read one character from STDIN.
 
186
 *
 
187
 * Result:           -- the single character read
 
188
 *         READ_INTR -- read was interrupted
 
189
 *         BAIL_OUT  -- read error of some sort
 
190
 */
 
191
int
 
192
read_one_char(void)
 
193
{
 
194
     int            res;
 
195
     unsigned char  c;
 
196
 
 
197
     res = read(STDIN_FD, &c, 1);
 
198
 
 
199
     if(res <= 0){
 
200
         /*
 
201
          * Error reading from terminal!
 
202
          * The only acceptable failure is being interrupted.  If so,
 
203
          * return a value indicating such...
 
204
          */
 
205
         if(res < 0 && errno == EINTR)
 
206
           return(READ_INTR);
 
207
         else
 
208
           return(BAIL_OUT);
 
209
     }
 
210
 
 
211
     return((int)c);
 
212
}
 
213
 
 
214
#else /* _WINDOWS */
 
215
int
 
216
set_time_of_last_input(void)
 
217
{
 
218
    _time_of_last_input = time(0L);
 
219
    return(0);
 
220
}
 
221
#endif /* _WINDOWS */