GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
rem_io.c
Go to the documentation of this file.
1 #include <grass/config.h>
2 
3 #ifdef HAVE_SOCKET
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <signal.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <errno.h>
11 
12 #include <grass/gis.h>
13 #include <grass/glocale.h>
14 #include <grass/raster.h>
15 #include <grass/graphics.h>
16 
17 #include "transport.h"
18 #include "open.h"
19 
20 #define BUFFERSIZ 2048
21 
22 int _rfd, _wfd;
23 int _quiet;
24 
25 int sync_driver(char *);
26 
27 static unsigned char outbuf[BUFFERSIZ];
28 static int cursiz;
29 static volatile int no_mon;
30 
31 static RETSIGTYPE dead(int);
32 
33 static int _get(char *buf, int n)
34 {
35  int x;
36 
37  while (n > 0) {
38  x = read(_rfd, buf, n);
39  if (x <= 0) {
40  fprintf(stderr, _("ERROR %s from graphics driver.\n"),
41  x ? "reading" : "eof");
42  exit(1);
43  }
44  n -= x;
45  buf += x;
46  }
47 
48  return 0;
49 }
50 
51 static int flushout(void)
52 {
53  if (cursiz) {
54  write(_wfd, outbuf, (size_t) cursiz);
55  cursiz = 0;
56  }
57 
58  return 0;
59 }
60 
61 int _send_ident(int anint)
62 {
63  unsigned char achar;
64 
65  achar = anint;
66 
67  if ((cursiz + 2) >= BUFFERSIZ)
68  flushout();
69  outbuf[cursiz++] = COMMAND_ESC;
70  outbuf[cursiz++] = achar;
71 
72  return 0;
73 }
74 
75 int _send_char(const unsigned char *achar)
76 {
77  if ((cursiz + 2) >= BUFFERSIZ)
78  flushout();
79  outbuf[cursiz++] = *achar;
80  if (*achar == COMMAND_ESC)
81  outbuf[cursiz++] = 0;
82 
83  return 0;
84 }
85 
86 int _send_char_array(int num, const unsigned char *achar)
87 {
88  while (num-- > 0)
89  _send_char(achar++);
90 
91  return 0;
92 }
93 
94 int _send_int_array(int num, const int *anint)
95 {
96  return _send_char_array(num * sizeof(int), (const unsigned char *)anint);
97 }
98 
99 int _send_float_array(int num, const float *afloat)
100 {
101  return _send_char_array(num * sizeof(float),
102  (const unsigned char *)afloat);
103 }
104 
105 int _send_int(const int *anint)
106 {
107  return _send_char_array(sizeof(int), (const unsigned char *)anint);
108 }
109 
110 int _send_float(const float *afloat)
111 {
112  return _send_char_array(sizeof(float), (const unsigned char *)afloat);
113 }
114 
115 int _send_text(const char *text)
116 {
117  return _send_char_array(1 + strlen(text), (const unsigned char *)text);
118 }
119 
120 int _get_char(char *achar)
121 {
122  flushout();
123  _get(achar, 1);
124 
125  return 0;
126 }
127 
128 int _get_int(int *anint)
129 {
130  flushout();
131  _get((char *)anint, sizeof(int));
132 
133  return 0;
134 }
135 
136 int _get_float(float *afloat)
137 {
138  flushout();
139  _get((char *)afloat, sizeof(float));
140 
141  return 0;
142 }
143 
144 int _get_text(char *buf)
145 {
146  char *b;
147 
148  b = buf;
149  do
150  _get_char(b);
151  while (*b++ != 0);
152 
153  return 0;
154 }
155 
156 char *_get_text_2(void)
157 {
158  static char *buf;
159  static int len;
160  int i;
161 
162  for (i = 0;; i++) {
163  if (i >= len) {
164  len += 1000;
165  buf = G_realloc(buf, len);
166  if (!buf) {
167  fprintf(stderr, _("Unable to allocate memory\n"));
168  exit(1);
169  }
170  }
171  _get_char(&buf[i]);
172  if (!buf[i])
173  break;
174  }
175 
176  return buf;
177 }
178 
179 int REM__open_quiet(void)
180 {
181  _quiet = 1;
182 
183  return 0;
184 }
185 
186 int sync_driver(char *name)
187 {
188 #ifndef __MINGW32__
189  RETSIGTYPE(*sigalarm) (int);
190 #endif
191  int try;
192  int count;
193  unsigned char c;
194 
195  _send_ident(BEGIN);
196  flushout();
197 
198  /*
199  * look for at least BEGIN_SYNC_COUNT zero bytes
200  * then look for COMMAND_ESC
201  *
202  * try twice. first timeout is warning, second is fatal
203  */
204  count = 0;
205 #ifndef __MINGW32__
206  sigalarm = signal(SIGALRM, dead);
207 #endif
208  for (try = 0; try < 2; try++) {
209  no_mon = 0;
210 #ifndef __MINGW32__
211  alarm(try ? 10 : 5);
212 #endif
213  while (no_mon == 0) {
214  if (read(_rfd, &c, (size_t) 1) != 1) {
215  if (no_mon)
216  break; /* from while */
217  fprintf(stderr, _("ERROR - eof from graphics monitor.\n"));
218  exit(-1);
219  }
220  if (c == 0)
221  count++;
222  else if (c == COMMAND_ESC && count >= BEGIN_SYNC_COUNT)
223  break;
224  else
225  count = 0; /* start over */
226  }
227 #ifndef __MINGW32__
228  alarm(0);
229  signal(SIGALRM, sigalarm);
230 #endif
231  if (no_mon == 0)
232  return 1; /* ok! */
233  if (try)
234  break;
235 
236  fprintf(stderr,
237  _("Warning - no response from graphics monitor <%s>.\n"),
238  name);
239  fprintf(stderr, _("Check to see if the mouse is still active.\n"));
240 #ifndef __MINGW32__
241  signal(SIGALRM, dead);
242 #endif
243  }
244  fprintf(stderr, _("ERROR - no response from graphics monitor <%s>.\n"),
245  name);
246  exit(-1);
247 }
248 
249 static RETSIGTYPE dead(int sig)
250 {
251  no_mon = 1;
252 }
253 
254 void _hold_signals(int hold)
255 {
256  static RETSIGTYPE(*sigint) (int);
257 
258 #ifdef SIGQUIT
259  static RETSIGTYPE(*sigquit) (int);
260 #endif
261 
262  if (hold) {
263  sigint = signal(SIGINT, SIG_IGN);
264 #ifdef SIGQUIT
265  sigquit = signal(SIGQUIT, SIG_IGN);
266 #endif
267  }
268  else {
269  signal(SIGINT, sigint);
270 #ifdef SIGQUIT
271  signal(SIGQUIT, sigquit);
272 #endif
273  }
274 }
275 
289 void REM_stabilize(void)
290 {
291  char c;
292 
293  flushout();
294  _send_ident(RESPOND);
295  _get_char(&c);
296 }
297 
298 void REM_kill_driver(void)
299 {
300  char dummy;
301 
302  _send_ident(GRAPH_CLOSE);
303  flushout();
304  read(_rfd, &dummy, 1);
306 }
307 
317 void REM_close_driver(void)
318 {
319  R_stabilize();
320 
321  close(_rfd);
322  close(_wfd);
323  _wfd = _rfd = -1;
324 }
325 
326 void REM_release_driver(void)
327 {
328  close(_rfd);
329  close(_wfd);
330  _wfd = _rfd = -1;
331 }
332 
333 #endif /* HAVE_SOCKET */
float b
Definition: named_colr.c:8
void R_release_driver(void)
Definition: com_io.c:208
int REM__open_quiet(void)
void R_stabilize(void)
Definition: com_io.c:193
string name
Definition: render.py:1314
int count
long num
Definition: g3dcats.c:93
void REM_close_driver(void)
int
Definition: g3dcolor.c:48
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
void REM_kill_driver(void)
#define BEGIN
Definition: lex.yy.c:125
Definition: spawn.c:69
void REM_stabilize(void)
int n
Definition: dataquad.c:291
void REM_release_driver(void)