aboutsummaryrefslogtreecommitdiff
path: root/jim-bio.c
blob: 044fc22f1b106d1eb44dab3de9f749adf7b02146 (plain)
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
/*
 * jim-bio.c
 *
 * Implements the bio command for binary I/O
 *
 * bio read ?-hex? fd var length
 *
 *   Reads 'length' bytes of binary data from 'fd' and stores an encoded string
 *   in 'var' so that the string contains no internal null characters.
 *
 *   Returns the number of (original) chars read, which may be short if EOF is reached.
 *
 * bio write ?-hex? fd encoded
 *
 *   Writes the binary-encoded string 'encoded' to 'fd'
 *
 *   Returns the number of chars written (if no error)
 *
 * Note that by default no encoding is actually done since Jim supports strings containing nulls!
 *
 * Alternatively, if -hex is specified, the data is read and written as ascii hex
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>

#include <jim.h>
#include <jim-subcmd.h>

static int hex2char(const char *hex)
{
    int value;
    value = (hex[0] >= 'A' ? ((hex[0] & 0xdf) - 'A') + 10 : (hex[0] - '0'));
    value *= 16;
    value += (hex[1] >= 'A' ? ((hex[1] & 0xdf) - 'A') + 10 : (hex[1] - '0'));
    return value;
}

static void char2hex(int c, char hex[2])
{
    hex[1] = (c & 0x0F) >= 0x0A ? (c & 0x0F) + 'A' - 10 : (c & 0x0F) + '0';
    c /= 16;
    hex[0] = (c & 0x0F) >= 0x0A ? (c & 0x0F) + 'A' - 10 : (c & 0x0F) + '0';
}

/**
 * Modelled on Jim_ReadCmd
 *
 */
static int bio_cmd_read(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
    char buf[130];
    int count = 0;
    long len;
    int hex = 0;
    int total = 0;
    FILE *fh;
    Jim_Obj *result;

    if (Jim_CompareStringImmediate(interp, argv[0], "-hex")) {
        hex++;
        argv++;
        argc--;
    }
    else if (argc == 4) {
        return -1;
    }

    fh = Jim_AioFilehandle(interp, argv[0]);
    
    if (!fh) {
        return JIM_ERR;
    }

    if (Jim_GetLong(interp, argv[2], &len) != JIM_OK) {
        return JIM_ERR;
    }

    result = Jim_NewStringObj(interp, "", 0);

    /* Read one char at a time */
    while (len > 0) {
        int ch = fgetc(fh);
        if (ch == EOF) {
            break;
        }
        total++;

        if (hex) {
            char2hex(ch, buf + count);
            count += 2;
        }
        else {
            buf[count++] = ch;
        }

        if (count >= sizeof(buf)) {
            /* Buffer is full, so append it */
            Jim_AppendString(interp, result, buf, count);
            count = 0;
        }
        len--;
    }

    if (ferror(fh)) {
        Jim_SetResultString(interp, "error during read", -1);
        clearerr(fh);
        return JIM_ERR;
    }

    /* Add anything still pending */
    Jim_AppendString(interp, result, buf, count);

    if (Jim_SetVariable(interp, argv[1], result) != JIM_OK) {
        return JIM_ERR;
    }

    Jim_SetResultInt(interp, total);

    return JIM_OK;
}

static int bio_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
    long count = 0;
    long maxlen = LONG_MAX;

    FILE *infh = Jim_AioFilehandle(interp, argv[0]);
    FILE *outfh = Jim_AioFilehandle(interp, argv[1]);
    
    if (infh == NULL || outfh == NULL) {
        return JIM_ERR;
    }

    if (argc == 3) {
        if (Jim_GetLong(interp, argv[2], &maxlen) != JIM_OK) {
            return JIM_ERR;
        }
    }

    while (count < maxlen) {
        int ch = fgetc(infh);
        if (ch == EOF || fputc(ch, outfh) == EOF) {
            break;
        }
        count++;
    }

    if (ferror(infh)) {
        Jim_SetResultFormatted(interp, "error reading \"%#s\": %s", argv[0], strerror(errno));
        clearerr(infh);
        return JIM_ERR;
    }

    if (ferror(outfh)) {
        Jim_SetResultFormatted(interp, "error writing \"%#s\": %s", argv[1], strerror(errno));
        clearerr(outfh);
        return JIM_ERR;
    }

    Jim_SetResultInt(interp, count);

    return JIM_OK;
}

static int bio_cmd_write(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
    FILE *fh;
    const char *pt;
    int hex = 0;
    long total = 0;
    int len;


    if (Jim_CompareStringImmediate(interp, argv[0], "-hex")) {
        hex++;
        argv++;
        argc--;
    }
    else if (argc == 3) {
        return -1;
    }

    fh = Jim_AioFilehandle(interp, argv[0]);
    if (fh == NULL) {
        return JIM_ERR;
    }

    pt = Jim_GetString(argv[1], &len);
    while (len-- > 0) {
        int ch;

        if (hex) {
            ch = hex2char(pt);
            pt += 2;
            len--;
        }
        else {
            ch = *pt++;
        }
        if (fputc(ch, fh) == EOF) {
            break;
        }
        total++;
    }

    if (ferror(fh)) {
        Jim_SetResultString(interp, "error during write", -1);
        clearerr(fh);
        return JIM_ERR;
    }

    Jim_SetResultInt(interp, total);

    return JIM_OK;
}

static const jim_subcmd_type command_table[] = {
    {   .cmd = "read",
        .function = bio_cmd_read,
        .args = "?-hex? fd var numbytes",
        .minargs = 3,
        .maxargs = 4,
        .description = "Read binary bytes as an encoded string"
    },
    {   .cmd = "write",
        .function = bio_cmd_write,
        .args = "?-hex? fd buf",
        .minargs = 2,
        .maxargs = 3,
        .description = "Write an encoded string as binary bytes"
    },
    {   .cmd = "copy",
        .function = bio_cmd_copy,
        .args = "fromfd tofd ?bytes?",
        .minargs = 2,
        .maxargs = 3,
        .description = "Read from one fd and write to another"
    },
    { 0 }
};

int
Jim_bioInit(Jim_Interp *interp)
{
    if (Jim_PackageProvide(interp, "bio", "1.0", JIM_ERRMSG) != JIM_OK) {
        return JIM_ERR;
    }
    Jim_CreateCommand(interp, "bio", Jim_SubCmdProc, (void *)command_table, NULL);
    return JIM_OK;
}