File: tty.c

package info (click to toggle)
gphoto 0.3.5-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,636 kB
  • ctags: 1,973
  • sloc: ansic: 23,035; sh: 4,851; makefile: 175
file content (188 lines) | stat: -rw-r--r-- 3,360 bytes parent folder | download | duplicates (2)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "config.h"
#include <stdio.h>
#include <string.h>
#if HAVE_FCNTL_H
# include <fcntl.h>
#endif
#include <stdlib.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
#if HAVE_UNISTD_H
# include <sys/types.h>
# include <unistd.h>
#endif
#if HAVE_TERMIOS_H
# include <termios.h>
#else
# if HAVE_TERMIO_H
#  include <termio.h>
# else
#  if HAVE_SYS_IOCTL_H
#   include <sys/ioctl.h>
#  endif
#  include <sgtty.h>
# endif
#endif
#if HAVE_TTOLD_H 
#   include <ttold.h>
#endif

#include "common.h"
#include "tty.h"

#define	TTYTIMEOUT	10

int changespeed(fd, baud)
     int fd;
     int baud;
{
#if HAVE_TERMIOS_H
  /* termios interface */
  struct termios tio;

  if (tcgetattr(fd, &tio) < 0) {
    fprintf(stderr, " Can't get tty attribute.\n");
    close(fd);
    return(-1);
  }
  tio.c_iflag = 0;
  tio.c_oflag = 0;
  tio.c_cflag = CS8 | CREAD | CLOCAL  ; /* 8bit non parity stop 1 */
  tio.c_lflag = 0;
  tio.c_cc[VMIN] = 1;
  tio.c_cc[VTIME] = 5 ;
  cfsetispeed(&tio, baud);
  cfsetospeed(&tio, baud);
  if (tcsetattr(fd, TCSANOW, &tio) < 0) {
    perror("Can't set tty attribute.\n");
    return(-1);
  }
#else
# if HAVE_TERMIO_H
  /* termio interface */
  /*  #  error not implemented yet. */
  fprintf(stderr,"sorry not inplemented yet.\n");
# else
  /* sgtty interface */
  struct sgttyb ttyb;
  if (ioctl(fd, TIOCGETP, &ttyb) < 0) {
    perror("Can't get tty attribute.\n");
    return(-1);
  }
  
  ttyb.sg_ispeed = baud;
  ttyb.sg_ospeed = baud;
  ttyb.sg_flags = 0;		/* 8bit non parity stop 1 */
  if (ioctl(fd, TIOCSETP, &ttyb) < 0) {
    perror("Can't set tty attribute.\n");
    return(-1);
  }

# endif
#endif
  flushtty(fd);
  return(1);
}

int opentty(path)
     char *path;
{
  int fd;
  struct termios tio;

  if ((fd = open(path, O_RDWR | O_NDELAY )) != -1) {
#if HAVE_TERMIOS_H
#else
    if(ioctl(fd, TIOCEXCL, 0) < 0){
      fprintf(stderr, "Can't set tty exclusive mode.\n");
      return(-1);
    }
    if(ioctl(fd, TIOCHPCL, 0) < 0){
      fprintf(stderr, "Can't set tty hold mode.\n");
      return(-1);
    }
#endif

    if(changespeed(fd, DEFAULTBAUD) < 0)
      return(-1); 
  }else{
    fprintf(stderr, "Can't open tty.\n");
    return(-1);
  }

  return(fd);
}

int readtty(fd, p, c)
     int fd;
     unsigned char *p;
     int c;
{
  fd_set readfds;
  int nfds;
  struct timeval tv;
  int i,j;
  unsigned char u;
	
  FD_ZERO(&readfds);
  FD_SET(fd, &readfds);
  tv.tv_sec = TTYTIMEOUT;
  tv.tv_usec = 0;

  for(j = 0 ; j < c ; j++){
    nfds = select( fd +1 , &readfds, NULL, NULL, &tv);
    if(nfds == 0){
      fprintf(stderr,"tty not respond. time up.\n");
      return(0);
    } else {
      if(FD_ISSET(fd, &readfds)){
	if(read(fd, &u, 1) < 0){
	  fprintf(stderr,"tty read fail.\n");
	  return(-1);
	}
	*p = u;
	p++;
      }
    }
  }
  return(j);
}

void flushtty(fd)
     int fd;
{
  u_char c;
  fd_set readfds;
  int nfds;
  struct timeval tv;
  int i,j;
  unsigned char u;
	
  FD_ZERO(&readfds);
  FD_SET(fd, &readfds);
  tv.tv_sec = 0;
  tv.tv_usec = 0;

  while (1) {
    nfds = select(fd +1 , &readfds, NULL, NULL, &tv);
    if(nfds == 0){
      return;
    } else {
      if(FD_ISSET(fd, &readfds)){
	if(read(fd, &c, 1) < 0){
	  fprintf(stderr,"tty read fail.\n");
	  return;
	}
      }
    }
  }
}