GNU PROLOG with UTF8 support
stream_supp.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------*
2  * GNU Prolog *
3  * *
4  * Part : Prolog buit-in predicates *
5  * File : stream_supp.h *
6  * Descr.: stream support - header file *
7  * Author: Daniel Diaz *
8  * *
9  * Copyright (C) 1999-2015 Daniel Diaz *
10  * *
11  * This file is part of GNU Prolog *
12  * *
13  * GNU Prolog is free software: you can redistribute it and/or *
14  * modify it under the terms of either: *
15  * *
16  * - the GNU Lesser General Public License as published by the Free *
17  * Software Foundation; either version 3 of the License, or (at your *
18  * option) any later version. *
19  * *
20  * or *
21  * *
22  * - the GNU General Public License as published by the Free *
23  * Software Foundation; either version 2 of the License, or (at your *
24  * option) any later version. *
25  * *
26  * or both in parallel, as here. *
27  * *
28  * GNU Prolog is distributed in the hope that it will be useful, *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
31  * General Public License for more details. *
32  * *
33  * You should have received copies of the GNU General Public License and *
34  * the GNU Lesser General Public License along with this program. If *
35  * not, see http://www.gnu.org/licenses/. *
36  *-------------------------------------------------------------------------*/
37 
38 
39 #include <stdio.h>
40 
41 
42 
43 /*---------------------------------*
44  * Constants *
45  *---------------------------------*/
46 
47 
48 #define STREAM_PB_SIZE 8 /* push back buffer size */
49 
50 
51 
52 
53 #define STREAM_MODE_READ 0
54 #define STREAM_MODE_WRITE 1
55 #define STREAM_MODE_APPEND 2
56 
57 #define STREAM_EOF_ACTION_ERROR 0
58 #define STREAM_EOF_ACTION_EOF_CODE 1
59 #define STREAM_EOF_ACTION_RESET 2
60 
61 #define STREAM_BUFFERING_NONE 0
62 #define STREAM_BUFFERING_LINE 1
63 #define STREAM_BUFFERING_BLOCK 2
64 
65 
66 #define STREAM_EOF_NOT 0
67 #define STREAM_EOF_AT 1
68 #define STREAM_EOF_PAST 2
69 
70 
71 
72  /* values for Get_Stream_Or_Alias */
73 #define STREAM_CHECK_VALID 0 /* simply a valid stream */
74 #define STREAM_CHECK_EXIST 1 /* valid and exist */
75 #define STREAM_CHECK_INPUT 2 /* valid, exist and mode=input */
76 #define STREAM_CHECK_OUTPUT 3 /* valid, exist and mode=output */
77 
78 
79 
80 
81 #define STREAM_FCT_UNDEFINED ((StmFct) (-1)) /* for optional fct */
82 
83 
84 
85 
86  /* Constant term streams (prop.other) */
87 #define TERM_STREAM_ATOM 1 /* values also used in stream.pl */
88 #define TERM_STREAM_CHARS 2
89 #define TERM_STREAM_CODES 3
90 
91 
92 
93 
94 /*---------------------------------*
95  * Type Definitions *
96  *---------------------------------*/
97 
98 typedef struct /* Stream properties */
99 { /* ------------------------------ */
100  unsigned mode:2; /* see STREAM_MODE_xxx defs */
101  unsigned input:1; /* is it an input stream ? */
102  unsigned output:1; /* is it an output stream ? */
103  unsigned text:1; /* is it a text stream . (or bin) */
104  unsigned reposition:1; /* can it be repositioned ? */
105  unsigned eof_action:2; /* see STREAM_EOF_ACTION_xxx defs */
106  unsigned buffering:2; /* see STREAM_BUFFERING_xxx defs */
107  unsigned special_close:1; /* does it need a special close ? */
108  unsigned other:8; /* other prop (1,2,3=term_streams */
109 } /* 4=socket_stream) */
110 StmProp;
111 
112 
113 
114 
115 typedef struct /* Push Back stack */
116 { /* ------------------------------ */
117  int buff[STREAM_PB_SIZE]; /* the buffer */
118  int *ptr; /* pointer into the buffer */
119  int nb_elems; /* # of elements in the buffer */
120 }
121 PbStk;
122 
123 
124 
125 
126 typedef int (*StmFct) (); /* generic type for file fctions */
127 
128 typedef struct stm_lst *PStmLst;
129 
130 typedef struct stm_lst /* Chained stream list */
131 { /* ------------------------------ */
132  int stm; /* the stream */
133  PStmLst next; /* next entry */
134 } StmLst;
135 
136 
137 typedef struct stm_inf /* Stream information */
138 { /* ------------------------------ */
139  int atom_file_name; /* atom associated to filename */
140  PlLong file; /* accessor (FILE *,TTYInf *) != 0*/
141  StmProp prop; /* assoctiated properties */
142  StmLst *mirror; /* mirror streams */
143  StmLst *mirror_of; /* streams this stream as mirror */
144  /* ----- Basic I/O functions ---- */
145  StmFct fct_getc; /* get char function (mandatory) */
146  StmFct fct_putc; /* put char function (mandatory) */
147  StmFct fct_flush; /* flush function (optional) */
148  StmFct fct_close; /* close function (optional) */
149  StmFct fct_tell; /* tell function (optional) */
150  StmFct fct_seek; /* seek function (optional) */
151  StmFct fct_clearerr; /* clearerr function (optional) */
152  /* ------ Read information ----- */
153  Bool eof_reached; /* has eof char been read ? */
154  PbStk pb_char; /* character push back stack */
155  /* ---- Position information --- */
156  PlLong char_count; /* character read count */
157  PlLong line_count; /* line read count */
158  PlLong line_pos; /* line position */
159  PbStk pb_line_pos; /* line position push back stack */
160 }
161 StmInf;
162 
163 
164 
165 
166 typedef struct /* Alias information */
167 { /* ------------------------------ */
168  PlLong atom; /* atom of the alias (the key) */
169  int stm; /* associated stream */
170 }
171 AliasInf;
172 
173 
174 
175 
176 typedef struct /* String Stream information */
177 { /* ------------------------------ */
178  char *buff; /* the I/O buffer */
179  char *ptr; /* current position into the buff */
180  Bool buff_alloc_size; /* mallocated size (iff output) */
181 }
182 StrSInf;
183 
184 
185 
186 
187 /*---------------------------------*
188  * Global Variables *
189  *---------------------------------*/
190 
191 #ifdef STREAM_SUPP_FILE
192 
194 int pl_stm_tbl_size;
195 int pl_stm_last_used;
196 
197 char *pl_alias_tbl;
198 
201 
202 int pl_stm_stdin;
203 int pl_stm_stdout;
204 int pl_stm_stderr;
205 
206 int pl_stm_input;
207 int pl_stm_output;
208 int pl_stm_error;
209 
214 
216 char *pl_le_prompt;
217 int pl_use_le_prompt;
218 
219 int pl_atom_stream;
220 
223 int pl_atom_user_error;
224 
227 
230 
231 int pl_atom_read;
232 int pl_atom_write;
233 int pl_atom_append;
234 
236 
238 
239 int pl_atom_text;
240 int pl_atom_binary;
241 
242 int pl_atom_error;
243 int pl_atom_eof_code;
244 int pl_atom_reset;
245 
246 int pl_atom_none;
247 int pl_atom_line;
248 int pl_atom_block;
249 
250 int pl_atom_not;
251 int pl_atom_at;
252 int pl_atom_past;
253 
254 int pl_atom_bof;
255 int pl_atom_current;
256 int pl_atom_eof;
257 
258 #else
259 
260 extern StmInf **pl_stm_tbl;
261 extern int pl_stm_tbl_size;
262 extern int pl_stm_last_used;
263 
264 extern char *pl_alias_tbl;
265 
266 
269 
270 extern int pl_stm_stdin;
271 extern int pl_stm_stdout;
272 extern int pl_stm_stderr;
273 
274 extern int pl_stm_input;
275 extern int pl_stm_output;
276 extern int pl_stm_error;
277 
278 extern int pl_stm_top_level_input;
279 extern int pl_stm_top_level_output;
280 extern int pl_stm_debugger_input;
281 extern int pl_stm_debugger_output;
282 
284 extern char *pl_le_prompt;
285 extern int pl_use_le_prompt;
286 
287 extern int pl_atom_stream;
288 
289 extern int pl_atom_user_input;
290 extern int pl_atom_user_output;
291 
292 extern int pl_atom_top_level_input;
293 extern int pl_atom_top_level_output;
294 
295 extern int pl_atom_debugger_input;
296 extern int pl_atom_debugger_output;
297 
298 extern int pl_atom_read;
299 extern int pl_atom_write;
300 extern int pl_atom_append;
301 
302 extern int pl_atom_reposition;
303 
304 extern int pl_atom_stream_position;
305 
306 extern int pl_atom_text;
307 extern int pl_atom_binary;
308 
309 extern int pl_atom_error;
310 extern int pl_atom_eof_code;
311 extern int pl_atom_reset;
312 
313 extern int pl_atom_none;
314 extern int pl_atom_line;
315 extern int pl_atom_block;
316 
317 extern int pl_atom_not;
318 extern int pl_atom_at;
319 extern int pl_atom_past;
320 
321 extern int pl_atom_bof;
322 extern int pl_atom_current;
323 extern int pl_atom_eof;
324 
325 #endif
326 
327 
328 
329 
330 /*---------------------------------*
331  * Function Prototypes *
332  *---------------------------------*/
333 
334 int Pl_Add_Stream(int atom_file_name, PlLong file, StmProp prop,
335  StmFct fct_getc, StmFct fct_putc,
336  StmFct fct_flush, StmFct fct_close,
337  StmFct fct_tell, StmFct fct_seek, StmFct fct_clearerr);
338 
339 int Pl_Add_Stream_For_Stdio_Desc(FILE *f, int atom_path, int mode, int text);
340 
341 int Pl_Add_Stream_For_Stdio_File(char *path, int mode, Bool text);
342 
343 void Pl_Delete_Stream(int stm);
344 
345 int Pl_Find_Stream_By_Alias(int atom_alias);
346 
347 Bool Pl_Add_Alias_To_Stream(int atom_alias, int stm);
348 
349 void Pl_Reassign_Alias(int atom_alias, int stm);
350 
351 void Pl_Add_Mirror_To_Stream(int stm, int m_stm);
352 
353 Bool Pl_Del_Mirror_From_Stream(int stm, int m_stm);
354 
356 
357 void Pl_Flush_All_Streams(void);
358 
359 void Pl_Set_Stream_Buffering(int stm);
360 
361 int Pl_Get_Stream_Or_Alias(WamWord sora_word, int test_mask);
362 
363 void Pl_Check_Stream_Type(int stm, Bool check_text, Bool for_input);
364 
366 
368 
369 void Pl_Stdio_Set_Buffering(FILE *f, int buffering);
370 
371 FILE *Pl_Stdio_Desc_Of_Stream(int stm);
372 
373 int Pl_Io_Fileno_Of_Stream(int stm);
374 
375 
376 
377 void Pl_PB_Empty_Buffer(StmInf *pstm);
378 
379 CHAR32_T Pl_Stream_Get_Key(StmInf *pstm, Bool echo, Bool catch_ctrl_c);
380 
382 
383 void Pl_Stream_Ungetc(CHAR32_T c, StmInf *pstm);
384 
386 
387 char *Pl_Stream_Gets(char *str, int size, StmInf *pstm);
388 
389 char *Pl_Stream_Gets_Prompt(char *prompt, StmInf *pstm_o,
390  char *str, int size, StmInf *pstm_i);
391 
392 void Pl_Stream_Putc(CHAR32_T c, StmInf *pstm);
393 
394 int Pl_Stream_Puts(char *str, StmInf *pstm);
395 
396 int Pl_Stream_Printf(StmInf *pstm, char *format, ...);
397 
398 void Pl_Stream_Flush(StmInf *pstm);
399 
400 int Pl_Stream_Close(StmInf *pstm);
401 
403 
404 
405 
406 void Pl_Stream_Get_Position(StmInf *pstm, PlLong *offset,
407  PlLong *char_count, PlLong *line_count, PlLong *line_pos);
408 
409 int Pl_Stream_Set_Position(StmInf *pstm, int whence, PlLong offset,
410  PlLong char_count, PlLong line_count, PlLong line_pos);
411 
412 int Pl_Stream_Set_Position_LC(StmInf *pstm, PlLong line_count, PlLong line_pos);
413 
414 
415 
416 int Pl_Add_Str_Stream(char *buff, int prop_other);
417 
418 void Pl_Delete_Str_Stream(int stm);
419 
420 char *Pl_Term_Write_Str_Stream(int stm);
421 
422 
423 void Pl_Close_Stm(int stm, Bool force); /* from close_c.c */
424 
425 
426 #define PB_Init(pb) pb.ptr = pb.buff, pb.nb_elems = 0;
427 
428 
429 
430 #define PB_Is_Empty(pb) (pb.nb_elems == 0)
431 
432 
433 
434 #define PB_Push(pb, elem) \
435  do \
436  { \
437  *(pb.ptr) = (elem); \
438  if (pb.ptr != pb.buff + STREAM_PB_SIZE - 1) \
439  pb.ptr++; \
440  else \
441  pb.ptr = pb.buff; \
442  if (pb.nb_elems < STREAM_PB_SIZE) \
443  pb.nb_elems++; \
444  } \
445  while (0)
446 
447 
448 
449 
450 #define PB_Pop(pb, elem) \
451  do \
452  { \
453  if (pb.ptr != pb.buff) \
454  pb.ptr--; \
455  else \
456  pb.ptr = pb.buff + STREAM_PB_SIZE - 1; \
457  (elem) = *pb.ptr; \
458  pb.nb_elems--; \
459  } \
460  while (0)
461 
462 
463 
464 
465 #define PB_Top(pb, elem) \
466  do \
467  { \
468  if (pb.ptr != pb.buff) \
469  (elem) = pb.ptr[-1]; \
470  else \
471  (elem) = pb.buff[STREAM_PB_SIZE - 1]; \
472  } \
473  while (0)
474 
WamWord pl_last_input_sora
int Pl_Io_Fileno_Of_Stream(int stm)
Definition: stream_supp.c:1007
PlLong file
Definition: stream_supp.h:140
StmFct fct_seek
Definition: stream_supp.h:150
CHAR32_T Pl_Stream_Get_Key(StmInf *pstm, Bool echo, Bool catch_ctrl_c)
Definition: stream_supp.c:1275
PlLong char_count
Definition: stream_supp.h:156
PStmLst next
Definition: stream_supp.h:133
#define CHAR32_T
Definition: pl_wchar.h:9
int pl_stm_top_level_input
Bool Pl_Add_Alias_To_Stream(int atom_alias, int stm)
Definition: stream_supp.c:541
void Pl_Close_Stm(int stm, Bool force)
Definition: stream_c.c:496
WamWord pl_last_output_sora
int pl_atom_at
char * Pl_Term_Write_Str_Stream(int stm)
Definition: stream_supp.c:1912
PbStk pb_char
Definition: stream_supp.h:154
StmLst * mirror_of
Definition: stream_supp.h:143
StmLst * mirror
Definition: stream_supp.h:142
int Pl_Stream_Set_Position_LC(StmInf *pstm, PlLong line_count, PlLong line_pos)
Definition: stream_supp.c:1724
void Pl_Stream_Putc(CHAR32_T c, StmInf *pstm)
Definition: stream_supp.c:1512
int(* StmFct)()
Definition: stream_supp.h:126
int pl_stm_tbl_size
int pl_atom_write
#define STREAM_PB_SIZE
Definition: stream_supp.h:48
int pl_atom_stream
static CHAR32_T c
Definition: scan_supp.c:65
Definition: stream_supp.h:115
int pl_atom_bof
void Pl_Delete_Str_Stream(int stm)
Definition: stream_supp.c:1884
void Pl_Add_Mirror_To_Stream(int stm, int m_stm)
Definition: stream_supp.c:605
int pl_atom_reset
WamWord Pl_Make_Stream_Tagged_Word(int stm)
Definition: stream_supp.c:915
int pl_atom_reposition
int pl_stm_debugger_input
int pl_atom_not
Definition: stream_supp.h:176
int pl_atom_text
int pl_stm_last_used
int pl_stm_top_level_output
StmFct fct_close
Definition: stream_supp.h:148
char * format
Definition: hexfilter.c:78
int pl_atom_past
void Pl_Stream_Flush(StmInf *pstm)
Definition: stream_supp.c:1584
int pl_atom_error
Definition: stream_supp.h:130
StmFct fct_clearerr
Definition: stream_supp.h:151
Bool buff_alloc_size
Definition: stream_supp.h:180
char * ptr
Definition: stream_supp.h:179
int pl_atom_eof
int Pl_Add_Stream_For_Stdio_File(char *path, int mode, Bool text)
Definition: stream_supp.c:377
int pl_atom_stream_position
int Pl_Add_Str_Stream(char *buff, int prop_other)
Definition: stream_supp.c:1820
intptr_t PlLong
Definition: gprolog.h:88
char * buff
Definition: stream_supp.h:178
int Pl_Get_Stream_Or_Alias(WamWord sora_word, int test_mask)
Definition: stream_supp.c:793
int Pl_Add_Stream_For_Stdio_Desc(FILE *f, int atom_path, int mode, int text)
Definition: stream_supp.c:352
StmFct fct_getc
Definition: stream_supp.h:145
PlLong atom
Definition: stream_supp.h:168
void Pl_Stream_Get_Position(StmInf *pstm, PlLong *offset, PlLong *char_count, PlLong *line_count, PlLong *line_pos)
Definition: stream_supp.c:1644
static StmInf * pstm_o
Definition: debugger_c.c:107
char * pl_le_prompt
void Pl_Check_Stream_Type(int stm, Bool check_text, Bool for_input)
Definition: stream_supp.c:868
StmProp prop
Definition: stream_supp.h:141
int pl_atom_read
int nb_elems
Definition: stream_supp.h:119
StmFct fct_flush
Definition: stream_supp.h:147
void Pl_Stream_Ungetc(CHAR32_T c, StmInf *pstm)
Definition: stream_supp.c:1382
int pl_stm_debugger_output
char * pl_alias_tbl
StmInf ** pl_stm_tbl
PlLong line_count
Definition: stream_supp.h:157
int pl_atom_none
void Pl_Reassign_Alias(int atom_alias, int stm)
Definition: stream_supp.c:568
void Pl_Stdio_Set_Buffering(FILE *f, int buffering)
Definition: stream_supp.c:948
int pl_stm_stdout
Bool Pl_Del_Mirror_From_Stream(int stm, int m_stm)
Definition: stream_supp.c:638
int pl_atom_eof_code
struct stm_inf StmInf
int pl_atom_user_input
Bool eof_reached
Definition: stream_supp.h:153
int pl_atom_debugger_input
int pl_atom_current
struct stm_lst StmLst
CHAR32_T Pl_Stream_Peekc(StmInf *pstm)
Definition: stream_supp.c:1417
int pl_stm_input
int Pl_Find_Stream_By_Alias(int atom_alias)
Definition: stream_supp.c:524
int pl_stm_error
PbStk pb_line_pos
Definition: stream_supp.h:159
Definition: stream_supp.h:166
int pl_atom_append
StmFct fct_putc
Definition: stream_supp.h:146
void Pl_Set_Stream_Buffering(int stm)
Definition: stream_supp.c:761
int Pl_Stream_Puts(char *str, StmInf *pstm)
Definition: stream_supp.c:1531
static StmInf * pstm_i
Definition: debugger_c.c:106
int stm
Definition: stream_supp.h:132
void Pl_Delete_Stream(int stm)
Definition: stream_supp.c:476
int Pl_Stream_End_Of_Stream(StmInf *pstm)
Definition: stream_supp.c:1619
int pl_stm_stdin
int pl_atom_user_output
int pl_atom_top_level_input
int pl_atom_top_level_output
int pl_atom_debugger_output
int pl_stm_stderr
Definition: stream_supp.h:137
int Pl_Stream_Printf(StmInf *pstm, char *format,...)
Definition: stream_supp.c:1554
Definition: stream_supp.h:98
void Pl_Flush_All_Streams(void)
Definition: stream_supp.c:744
void Pl_PB_Empty_Buffer(StmInf *pstm)
Definition: stream_supp.c:1262
StmFct fct_tell
Definition: stream_supp.h:149
CHAR32_T Pl_Stream_Getc(StmInf *pstm)
Definition: stream_supp.c:1334
int stm
Definition: stream_supp.h:169
int pl_atom_binary
long WamWord
Definition: LINUX_SIGSEGV.c:4
FILE * Pl_Stdio_Desc_Of_Stream(int stm)
Definition: stream_supp.c:985
FILE * f
Definition: sparc64-setx.c:6
char * Pl_Stream_Gets_Prompt(char *prompt, StmInf *pstm_o, char *str, int size, StmInf *pstm_i)
Definition: stream_supp.c:1480
int pl_use_le_prompt
int Pl_Find_Stream_From_PStm(StmInf *pstm)
Definition: stream_supp.c:725
Bool Pl_Stdio_Is_Repositionable(FILE *f)
Definition: stream_supp.c:933
int Bool
Definition: bool.h:65
struct stm_lst * PStmLst
Definition: stream_supp.h:128
Bool pl_stream_use_linedit
int Pl_Stream_Set_Position(StmInf *pstm, int whence, PlLong offset, PlLong char_count, PlLong line_count, PlLong line_pos)
Definition: stream_supp.c:1684
int * ptr
Definition: stream_supp.h:118
int Pl_Add_Stream(int atom_file_name, PlLong file, StmProp prop, StmFct fct_getc, StmFct fct_putc, StmFct fct_flush, StmFct fct_close, StmFct fct_tell, StmFct fct_seek, StmFct fct_clearerr)
Definition: stream_supp.c:445
int pl_atom_line
char buff[4096]
Definition: cpp_headers.c:83
PlLong line_pos
Definition: stream_supp.h:158
char * Pl_Stream_Gets(char *str, int size, StmInf *pstm)
Definition: stream_supp.c:1444
int pl_stm_output
int Pl_Stream_Close(StmInf *pstm)
Definition: stream_supp.c:1600
int pl_atom_block
int atom_file_name
Definition: stream_supp.h:139