File: vfile.c

package info (click to toggle)
slrn 1.0.3+dfsg-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,412 kB
  • sloc: ansic: 39,852; sh: 3,777; perl: 467; makefile: 229; sed: 24
file content (384 lines) | stat: -rw-r--r-- 7,276 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* -*- mode: C; mode: fold; -*- */
/* Copyright (c) 1992, 1998, 2000, 2007-2016 John E. Davis
 * This file is part of JED editor library source.
 *
 * You may distribute this file under the terms the GNU General Public
 * License.  See the file COPYING for more information.
 */
#include "config.h"
#ifdef JED
# include "jed-feat.h"
#endif
/*{{{ Include Files */

#include <stdio.h>
#include <slang.h>

#include "jdmacros.h"
#include "util.h"

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <string.h>
#include <limits.h>

#include <errno.h>

#if defined(__DECC) && defined(VMS)
# include <unixio.h>
#endif

#ifdef VMS
# include <file.h>
#endif

#ifdef __unix__
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/file.h>
# ifdef HAVE_FCNTL_H
#  include <fcntl.h>
# endif
# ifdef HAVE_SYS_FCNTL_H
#  include <sys/fcntl.h>
# endif
#endif

#if defined(__BORLANDC__) || defined(_MSC_VER)
# include <fcntl.h>
# include <io.h>
# include <sys/stat.h>
#endif

#ifdef __os2__
# include <fcntl.h>
# include <io.h>
# include <sys/types.h>
# include <sys/stat.h>
#endif

#ifndef O_RDONLY
# ifdef VMS
#  include <file.h>
# else
#  include <fcntl.h>
# endif
#endif

#include "vfile.h"

/*}}}*/

unsigned int VFile_Mode = VFILE_TEXT;

struct VFILE
{
   /* If has_unget is set, then next call to vgets will return with
    * ungetbuf and unget_len.
    */
   int has_unget;
   char *unget_buf;
   unsigned int unget_len;

   char *buf;			       /* buffer for stream */
   char *bmax;			       /* pointer to end buffer */
   char *bp;			       /* current pointer in stream */
   char *eof;			       /* pointer to EOF if non NULL */
   int fd;			       /* file descrip for stream */
   unsigned int size;		       /* default buffer size */
   unsigned int mode;
   unsigned int cr_flag;	       /* true if lines end in cr */
};

VFILE *vopen(char *file, unsigned int size, unsigned int fmode) /*{{{*/
{
   int fd;
   unsigned int mode;

#ifdef O_BINARY
   mode = O_BINARY;
#else
   mode = 0;
#endif

   if ((fd = open(file, mode | O_RDONLY, 0)) < 0) return(NULL);
   return vstream(fd, size, fmode);
}

/*}}}*/

void vclose(VFILE *v) /*{{{*/
{
   (void) close(v->fd);
   SLfree(v->buf);
   SLfree ((char *)v);
}

/*}}}*/

VFILE *vstream(int fd, unsigned int size, unsigned int mode) /*{{{*/
{
   VFILE *v;

   if (NULL == (v = (VFILE *) SLmalloc(sizeof(VFILE)))) return(NULL);
   v->has_unget = 0;
   v->bmax = v->bp = v->buf = NULL;
   v->fd = fd;
   v->eof = NULL;
   v->size = size;
   if (mode == 0) mode = VFile_Mode;
   v->mode = mode;
   v->cr_flag = 0;
   return v;
}

/*}}}*/

/* Here is how vgets appears to work (disclaimer: it came without developer
 * documentation and I did not analyse it completely):
 * If you want to nul-terminate the returned string, check if position num-1
 * contains the '\n' character. If so, overwrite it with nul; otherwise, write
 * nul at position num.
 * Do _not_ write at position num when there is a '\n' at num-1, or you will
 * corrupt the line obtained by the next call to vgets! (Actually, you _can_
 * write there, but make sure you save and restore the character.)
 */
/* I malloc one extra so that I can always add a null character to last line */
static char *vgets_internal(VFILE *vp, unsigned int *num) /*{{{*/
{
   register char *bp, *bp1;
   register char *bmax, *bpmax;
   char *neew;
   int fd;
   unsigned int n, max, fmode;
   int doread = 0;

   if (vp == NULL)
     {
	*num = 0;
	return NULL;
     }

   fd = vp->fd;
   n = vp->size;
   fmode = vp->mode;

   *num = 0;
   if (NULL == vp->buf)
     {
#if defined (__MSDOS_16BIT__)
	if (!n) n = 512;
#else
	if (!n) n = 64 * 1024;
#endif

	if (NULL == (neew = SLmalloc(n + 1)))
	  return NULL;

	vp->bp = vp->buf = neew;
	vp->bmax = neew + n;
	doread = 1;
     }

   bp = vp->bp;
   if ((vp->eof != NULL) && (bp >= vp->eof)) return (NULL);
   bp1 = vp->buf;
   bmax = vp->bmax;

   while (1)
     {
	if (doread)
	  {
	     max = (int) (vp->bmax - bp);
	     while (max > 0)
	       {
		  int nread;

		  nread = read(fd, bp, max);
		  if (nread == 0)
		    break;

		  if (nread == -1)
		    {
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__BORLANDC__))
# ifdef EPIPE
		       if (errno == EPIPE)
			 break;
# endif
#endif
#ifndef IBMPC_SYSTEM
# ifdef EINTR
		       if (errno == EINTR)
			 continue;
# endif
# ifdef EAGAIN
		       if (errno == EAGAIN)
			 {
			    slrn_sleep (1);
			    continue;
			 }
# endif
#endif
		       return NULL;
		    }

		  max -= nread;
		  bp += nread;
	       }
	     if (max) vp->eof = bp;
	     if (bp == bp1)
	       {
		  return(NULL);
	       }
	     bp = bp1;
	  }
	else bp1 = bp;

	/* extract a line */
	if (vp->eof != NULL) bmax = vp->eof;

	n = (unsigned int) (bmax - bp);
#if defined(__MSDOS__)
	if (n)
	  {
	     bpmax = bp;
#if defined(__BORLANDC__) && !defined(__WIN32__)
	     asm  {
		mov bx, di
		mov al, 10
		mov cx, n
		les di, bpmax
		cld
		repne scasb
		inc cx
		sub n, cx
		mov di, bx
	     }
	     bp += n;
#else
 	     if (NULL == (bpmax = (char *)SLMEMCHR(bp, '\n', n)))
 	       bp += n;
 	     else
 	       bp = bpmax;
#endif /* __WIN32__ */
	     if (*bp != '\n') bp++;
	  }

	if (bp < bmax)
	  {
	     vp->bp = ++bp;
	     *num = (unsigned int) (bp - bp1);

	     /* if it is text, replace the carriage return by a newline
	        and adjust the number read by 1 */
	     bp -= 2;
	     if ((fmode == VFILE_TEXT) && (*num > 1) && (*bp == '\r'))
	       {
		  *bp = '\n';
		  *num -= 1;
		  vp->cr_flag = 1;
	       }
	     return bp1;
	  }
#else
	if (NULL != (bpmax = (char *)SLMEMCHR(bp, '\n', n)))
	  {
	     bpmax++;
	     vp->bp = bpmax;
	     *num = (unsigned int) (bpmax - bp1);

	     if ((fmode == VFILE_TEXT) && (*num > 1))
	       {
		  bpmax -= 2;
		  if (*bpmax == '\r')
		    {
		       vp->cr_flag = 1;
		       *bpmax = '\n'; (*num)--;
		    }
	       }
	     return bp1;
	  }
	bp = bp + n;
#endif	/* __MSDOS__ */
	if (vp->eof != NULL)
	  {
	     *num = (unsigned int) (bp - bp1);
	     vp->bp = bp;

#if defined(IBMPC_SYSTEM)
	     /* kill ^Z at EOF if present */
	     if ((fmode == VFILE_TEXT) && (*num) && (26 == *(bp - 1)))
	       {
		  *num -= 1;
		  if (!*num) bp1 = NULL;
	       }
#endif
	     return(bp1);
	  }

	doread = 1;

	bp = bp1;
	bp1 = vp->buf;
	if (bp != bp1)
	  {
	     /* shift to beginning */
	     while (bp < bmax) *bp1++ = *bp++;
	     bp = bp1;
	     bp1 = vp->buf;
	  }
	else
	  {
	     bp = bmax;
	     vp->bmax += 2 * (int) (vp->bmax - vp->buf);
	     neew = SLrealloc (vp->buf, 1 + (unsigned int) (vp->bmax - vp->buf));
	     if (neew == NULL)
	       return NULL;

	     bp = neew + (int) (bmax - vp->buf);
	     bmax = vp->bmax = neew + (int) (vp->bmax - vp->buf);
	     bp1 = vp->buf = neew;
	  }
     }
}

/*}}}*/

char *vgets (VFILE *vp, unsigned int *nump)
{
   if (vp == NULL)
     {
	*nump = (unsigned int)-1;
	return NULL;
     }

   if (vp->has_unget)
     {
	vp->has_unget = 0;
	*nump = vp->unget_len;
	return vp->unget_buf;
     }

   vp->unget_buf = vgets_internal (vp, &vp->unget_len);
   *nump = vp->unget_len;
   return vp->unget_buf;
}

int vungets (VFILE *v)
{
   if (v == NULL)
     return -1;

   if (v->has_unget)
     return -1;

   v->has_unget = 1;
   return 0;
}