File: input.c

package info (click to toggle)
fhist 1.18-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 2,564 kB
  • ctags: 1,332
  • sloc: ansic: 15,161; sh: 4,749; makefile: 1,027; awk: 154; yacc: 101
file content (311 lines) | stat: -rw-r--r-- 7,029 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
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
/*
 *      fhist - file history and comparison tools
 *      Copyright (C) 2000-2002, 2008 Peter Miller
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      This program 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 General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program. If not, see
 *      <http://www.gnu.org/licenses/>.
 */

#include <ac/ctype.h>
#include <ac/string.h>

#include <fcheck.h>
#include <input/private.h>
#include <mem.h>
#include <trace.h>


#ifdef input_name
#undef input_name
#endif

const char *
input_name(input_ty *fp)
{
    const char      *result;

    trace(("input_name(fp = %8.8lX)\n{\n", (long)fp));
    result = fp->vptr->name(fp);
    trace(("return \"%s\";\n", result));
    trace(("}\n"));
    return result;
}


#ifdef input_length
#undef input_length
#endif

long
input_length(input_ty *fp)
{
    long            result;

    trace(("input_length(fp = %8.8lX)\n{\n", (long)fp));
    result = fp->vptr->length(fp);
    trace(("return %ld;\n", result));
    trace(("}\n"));
    return result;
}


#ifdef input_ftell
#undef input_ftell
#endif

long
input_ftell(input_ty *fp)
{
    long            result;

    trace(("input_ftell(fp = %8.8lX)\n{\n", (long)fp));
    result = fp->vptr->ftell(fp) - fp->pushback_len;
    trace(("return %ld;\n", result));
    trace(("}\n"));
    return result;
}


#ifdef input_read
#undef input_read
#endif

long
input_read(input_ty *fp, void *data, long len)
{
    long            result;

    trace(("input_read(fp = %8.8lX, data = %8.8lX, len = %8.8lX)\n{\n",
        (long)fp, (long)data, len));
    if (len <= 0)
    {
        result = 0;
    }
    else
    {
        if (fp->pushback_len > 0)
        {
            fp->pushback_len--;
            *(char *)data = fp->pushback_buf[fp->pushback_len];
            result = 1;
        }
        else
        {
            result = fp->vptr->read(fp, data, len);
        }
    }
    trace(("return %ld;\n", result));
    trace(("}\n"));
    return result;
}


#ifdef DEBUG

static char *
unctrl(int c)
{
    static char     buffer[10];

    if (c < 0 || c >= 256)
    {
        snprintf(buffer, sizeof(buffer), "%d", c);
        return buffer;
    }
    switch (c)
    {
    case '\'':
    case '\\':
        escape:
        buffer[0] = '\'';
        buffer[1] = '\\';
        buffer[2] = c;
        buffer[3] = '\'';
        buffer[4] = 0;
        break;

    case '\b': c = 'b'; goto escape;
    case '\f': c = 'f'; goto escape;
    case '\n': c = 'n'; goto escape;
    case '\r': c = 'r'; goto escape;
    case '\t': c = 't'; goto escape;

    default:
        if (isprint((unsigned char)c))
        {
            buffer[0] = '\'';
            buffer[1] = c;
            buffer[2] = '\'';
            buffer[3] = 0;
        }
        else
            snprintf(buffer, sizeof(buffer), "'\\%o'", c);
        break;
    }
    return buffer;
}

#endif



#ifdef input_getc
#undef input_getc
#endif

int
input_getc(input_ty *fp)
{
    int             result;

    trace(("input_getc(fp = %8.8lX)\n{\n", (long)fp));
    if (fp->pushback_len > 0)
    {
        fp->pushback_len--;
        result = fp->pushback_buf[fp->pushback_len];
    }
    else
        result = fp->vptr->get(fp);
    trace(("return %s;\n", unctrl(result)));
    trace(("}\n"));
    return result;
}


#ifdef input_ungetc
#undef input_ungetc
#endif

void
input_ungetc(input_ty *fp, int c)
{
    trace(("input_ungetc(fp = %8.8lX, c = %s)\n{\n", (long)fp, unctrl(c)));
    if (c >= 0)
    {
        if (fp->pushback_len >= fp->pushback_max)
        {
            fp->pushback_max = 16 + 2 * fp->pushback_max;
            fp->pushback_buf =
                    mem_change_size(fp->pushback_buf, fp->pushback_max);
        }
        fp->pushback_buf[fp->pushback_len++] = c;
    }
    trace(("}\n"));
}


void
input_delete(input_ty *fp)
{
    trace(("input_delete(fp = %8.8lX)\n{\n", (long)fp));
    if (fp->vptr->destruct)
        fp->vptr->destruct(fp);
    if (fp->pushback_buf)
        mem_free(fp->pushback_buf);
    fp->pushback_buf = 0;
    fp->pushback_len = 0;
    fp->pushback_max = 0;
    fp->vptr = 0; /* paranoia */
    mem_free(fp);
    trace(("}\n"));
}


#include <cmalloc.h>

/*
 * Routine to read in the next line from a file, no matter how long it is.
 * This returns a pointer to the string, and also indirectly its length.
 * The string is normally returned from a static string, which is reused
 * for each call.  But if keep is non-zero, then the returned string
 * belongs to the caller and must later be freed.  The returned line
 * is ended with a newline and null character, but the returned length
 * does not count the null character.  Returns 0 on an error, with the
 * error stored in the fp structure.
 */

char *
input_readline(input_ty *fp, long *retlen, int keep, int *is_bin_file)
{
    char            *dp;        /* destination pointer */
    size_t          totallen;   /* total length of data */
    static char *linebuffer;    /* common line buffer */
    static size_t    linelength; /* total length of line */

    trace(("input_readline(fp = %8.8lX, retlen = %8.8lX, keep = %d)\n{\n",
        (long)fp, (long)retlen, keep));
    if (linelength == 0)
    {
        /* allocate line buffer */
        linelength = 16;
        linebuffer = r_alloc_and_check(linelength + 1);
    }
    totallen = 0;
    for (;;)
    {
        int c = input_getc(fp);
        if (c < 0)
        {
            if (totallen == 0)
                return 0;
            warning_last_line_unterminated(input_name(fp));
            c = '\n';
        }
        if (c == 0)
        {
            *is_bin_file = 1;
            c = 0x80;
        }
        if (totallen >= linelength)
        {
            /*
             * doubling gives O(1) behaviour
             * over the life of the prog
             */
            linelength = linelength * 2 + 16;
            linebuffer = r_realloc_and_check(linebuffer, linelength + 1);
        }
        linebuffer[totallen++] = c;
        if (c == '\n')
            break;
    }
    linebuffer[totallen] = 0;

    if (retlen)
            *retlen = totallen;
    if (!keep)
        dp = linebuffer;
    else
    {
        dp = cm_alloc_and_check(totallen + 1);
        memcpy(dp, linebuffer, (size_t)totallen);
    }
    trace(("return %8.8lX;\n", (long)dp));
    trace(("}\n"));
    return dp;
}


void
input_skip_lines(input_ty *fp, int numlines)
{
    while (numlines > 0)
    {
        int c = input_getc(fp);
        if (c < 0)
            break;
        if (c == '\n')
            --numlines;
    }
}