File: pdt_console.c

package info (click to toggle)
swi-prolog 7.2.3%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,180 kB
  • ctags: 45,684
  • sloc: ansic: 330,358; perl: 268,104; sh: 6,795; java: 4,904; makefile: 4,561; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (209 lines) | stat: -rw-r--r-- 5,028 bytes parent folder | download | duplicates (3)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*  $Id$

    Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 2011, VU University Amsterdam

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <string.h>
#include <assert.h>

#define ESC 27		/* = '\e', but that is not supported in VC */

typedef struct pdt_console
{ struct pdt_console   *next;
  void *		input_handle;
  void *		output_handle;
  IOFUNCTIONS	        input_functions;
  IOFUNCTIONS	        output_functions;
  IOFUNCTIONS	       *org_input_functions;
  IOFUNCTIONS	       *org_output_functions;
  int			closed;
} pdt_console;

static pdt_console *consoles = NULL;

static pdt_console *
find_console(void *input_handle, void *output_handle)
{ pdt_console *c;

  for(c=consoles; c; c=c->next)
  { if ( c->input_handle == input_handle ||
	 c->output_handle == output_handle )
      return c;
  }

  if ( (c=malloc(sizeof(*c))) )
  { memset(c, 0, sizeof(*c));
    c->input_handle         = input_handle;
    c->output_handle        = output_handle;
    c->input_functions      = *Suser_input->functions;
    c->output_functions     = *Suser_output->functions;
    c->org_input_functions  = Suser_input->functions;
    c->org_output_functions = Suser_output->functions;
    c->next                 = consoles;
    consoles                = c;
  }

  return c;
}


static Sclose_function
free_console(void *input_handle, void *output_handle)
{ pdt_console **pc;
  pdt_console *c;

  for(pc = &consoles; (c=*pc); pc = &c->next)
  { Sclose_function cf = NULL;

    c = *pc;

    if ( input_handle && c->input_handle == input_handle )
    { cf = c->org_input_functions->close;
      c->input_handle = NULL;
      Suser_input->functions = c->org_input_functions;
    } else if ( output_handle && c->output_handle == output_handle )
    { cf = c->org_output_functions->close;
      c->output_handle = NULL;
      Suser_input->functions = c->org_output_functions;
    }

    if ( cf )
    { if ( !c->input_handle && !c->output_handle )
      { *pc = c->next;
	free(c);
      }

      return cf;
    }
  }

  return NULL;
}


static ssize_t
pdt_read(void *handle, char *buf, size_t size)
{ pdt_console *c = find_console(handle, NULL);
  IOSTREAM *out;

  if ( c &&
       PL_ttymode(Suser_input) == PL_RAWTTY  &&
       (out = Suser_output) )
  { ssize_t rc;
    static char go_single[] = {ESC, 's'};

    if ( (*c->org_output_functions->write)(out->handle, go_single, 2) == 2 )
    { rc = (*c->org_input_functions->read)(handle, buf, 2);
      if ( rc == 2 )
	rc = 1;				/* drop \n */
      return rc;
    }
  }

  return (*c->org_input_functions->read)(handle, buf, size);
}


static ssize_t
pdt_write(void *handle, char *buf, size_t size)
{ pdt_console *c = find_console(NULL, handle);
  char *e = buf+size;
  size_t written = 0;

  while(buf < e)
  { char *em;
    ssize_t rc;

    for(em=buf; *em != ESC && em < e; em++)
      ;
    rc = (*c->org_output_functions->write)(handle, buf, em-buf);
    if ( rc < 0 )
      return rc;
    written += (em-buf);
    if ( rc != (em-buf) )
      return written;
    if ( em != e )
    { static char esc[2] = {ESC,ESC};

      if ( (*c->org_output_functions->write)(handle, esc, 2) != 2 )
	return -1;
      em++;
    }
    buf = em;
  }

  return written;
}


static int
pdt_close_in(void *handle)
{ Sclose_function cf = free_console(handle, NULL);

  if ( cf )
    return (*cf)(handle);

  return -1;
}


static int
pdt_close_out(void *handle)
{ Sclose_function cf = free_console(NULL, handle);

  if ( cf )
    return (*cf)(handle);

  return -1;
}


static foreign_t
pdt_wrap_console()
{ IOSTREAM *in, *out;

  if ( (in=Suser_input) && (out=Suser_output) )
  { pdt_console *c;

    assert(in->functions->read != pdt_read);
    assert(out->functions->write != pdt_write);

    if ( (c=find_console(in->handle, out->handle)) )
    { in->functions = &c->input_functions;
      in->functions->read  = pdt_read;
      in->functions->close = pdt_close_in;
      out->functions = &c->output_functions;
      out->functions->write = pdt_write;
      out->functions->close = pdt_close_out;
    }
  }

  return TRUE;
}


install_t
install_pdt_console()
{ PL_register_foreign("pdt_wrap_console", 0, pdt_wrap_console, 0);
}