summaryrefslogtreecommitdiffstats
path: root/src/execute.c
blob: a69932b40d6eb81f9163445fa2c759fa0c820742 (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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* 
 *    bbe - Binary block editor
 *
 *    Copyright (C) 2005 Timo Savinen
 *    This file is part of bbe.
 * 
 *    bbe 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 2 of the License, or
 *    (at your option) any later version.
 *
 *    bbe 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 bbe; if not, write to the Free Software
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/* $Id: execute.c,v 1.23 2005/09/30 10:58:15 timo Exp $ */

#include "bbe.h"
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>

/* tells if current byte should be deleted */
static int delete_this_byte;

/* tells if current block should be deleted */
static int delete_this_block;

/* tells if i or s commands are inserting bytes, meaningfull at end of the block */
static int inserting;

/* command list for write_w_command */
static struct command *current_commands;

/* commands to be executed at start of buffer */
/* note J and L must be in every string becaus ethey affect the whole block */
#define BLOCK_START_COMMANDS "DIJLFBN"

/* commands to be executed for each byte  */
#define BYTE_COMMANDS "acdirsywjplJL&|^~"

/* commands to be executed at end of buffer  */
#define BLOCK_END_COMMANDS "AJL"

 
/* byte_to_string, convert byte value to visible string,
   either hex (H), decimal (D), octal (O) or ascii (A)
   */
char *
byte_to_string(unsigned char byte,char format)
{
    static char string[10];

    switch(format)
    {
        case 'H':
            sprintf(string,"x%02x",(int) byte);
            break;
        case 'D':
            sprintf(string,"%3d",(int) byte);
            break;
        case 'O':
            sprintf(string,"%03o",(int) byte);
            break;
        case 'A':
            sprintf(string,"%c",isprint(byte) ? byte : ' ');
            break;
        default:
            string[0] = 0;
            break;
    }
    return string;
}

/* convert off_t to string  */
char *
off_t_to_string(off_t number,char format)
{
    static char string[16];

    switch(format)
    {
        case 'H':
             sprintf(string,"x%llx",(long long) number);
             break;
        case 'D':
             sprintf(string,"%lld",(long long) number);
             break;
        case 'O':
             sprintf(string,"0%llo",(long long) number);
             break;
        default:
             string[0] = 0;
             break;
    }
    return string;
}




/* execute given commands */
void
execute_commands(struct command *c,char *command_letters)
{
    register int i;
    unsigned char a,b;
    unsigned char *p;
    char *str;

    while(c != NULL)
    {
        if(strchr(command_letters,c->letter) !=  NULL)
        {
            switch(c->letter)
            {
                case 'A':
                case 'I':
                    write_buffer(c->s1,c->s1_len);
                    break;
                case 'd':
                    if(c->rpos || c->offset == in_buffer.block_offset) 
                    {
                        if(c->rpos < c->count)
                        {
                            delete_this_byte = 1;
                            c->rpos++;
                        } else
                        {
                            c->rpos = 0;
                        }
                    }
                    break;
                case 'D':
                    if(c->offset == in_buffer.block_num || c->offset == 0) delete_this_block = 1;
                    break;
                case 'i':
                    if(c->offset == in_buffer.block_offset && !c->rpos) 
                    {
                        c->rpos = 1;
                        inserting = 1;
                        break;
                    } 
                    if(c->rpos > 0 && c->rpos <= c->s1_len)
                    {
                        if(c->rpos <= c->s1_len)
                        {
                            put_byte(c->s1[c->rpos - 1]);
                            if(c->rpos < c->s1_len) inserting = 1;
                        }
                        c->rpos++;
                    }
                    break;
                case 'r':
                    if(in_buffer.block_offset >= c->offset &&
                       in_buffer.block_offset < c->offset + c->s1_len)
                    {
                        put_byte(c->s1[in_buffer.block_offset - c->offset]);
                    }
                    break;
                case 's':
                    if(c->rpos)
                    {
                        if(c->rpos < c->s1_len && c->rpos < c->s2_len)
                        {
                            put_byte(c->s2[c->rpos]);
                        } else if (c->rpos < c->s1_len && c->rpos >= c->s2_len)
                        {
                            delete_this_byte = 1;
                        } else if(c->rpos >= c->s1_len && c->rpos < c->s2_len)
                        {
                            put_byte(c->s2[c->rpos]);
                        } 

                        if(c->rpos >= c->s1_len - 1 && c->rpos < c->s2_len - 1)
                        {
                            inserting = 1;
                        }

                        c->rpos++;
                        if(c->rpos >= c->s1_len && c->rpos >= c->s2_len)
                        {
                            c->rpos = 0;
                        }
                        break;
                    }
                    if(delete_this_byte) break;
                    p = out_buffer.write_pos;
                    i = 0;
                    while(*p == c->s1[i] && i < c->s1_len)
                    {
                        if(p == out_buffer.write_pos) p = read_pos();
                        if(p == block_end_pos() && c->s1_len - 1 > i) break;
                        i++;
                        p++;
                    }
                    if(i == c->s1_len) 
                    {
                        if(c->s1_len > 1 || c->s2_len > 1) c->rpos = 1;
                        if(c->s2_len) 
                        {
                            put_byte(c->s2[0]);
                            if(c->s1_len == 1 && c->s2_len > 1) inserting = 1;
                        } else
                        {
                            delete_this_byte = 1;
                        }
                    }
                    break;
                case 'y':
                    i = 0;
                    while(c->s1[i] != *out_buffer.write_pos && i < c->s1_len) i++;
                    if(c->s1[i] == *out_buffer.write_pos && i < c->s1_len) put_byte(c->s2[i]);
                    break;
                case 'c':
                    switch(c->s1[0])
                    {
                        case 'A':                   // from ascii
                            switch(c->s1[3])
                            {
                                case 'B':           // to bcd
                                    if(c->rpos || (last_byte() && out_buffer.block_offset == 0))     // skip first nibble
                                    {
                                        c->rpos = 0;
                                        if(last_byte())  // unless last byte of block
                                        {
                                            if(*out_buffer.write_pos >= '0' && *out_buffer.write_pos <= '9')
                                            {
                                                a = *out_buffer.write_pos - '0';
                                                a = (a << 4) & 0xf0;
                                                b = 0x0f;
                                                *out_buffer.write_pos = a | b;
                                            }
                                        }
                                        break;
                                    }
                                    if(out_buffer.block_offset == 0 || delete_this_byte) break;
                                    if((out_buffer.write_pos[-1] >= '0' && out_buffer.write_pos[-1] <= '9'))
                                    {
                                        a = out_buffer.write_pos[-1] - '0';
                                        a = (a << 4) & 0xf0;
                                        if(*out_buffer.write_pos >= '0' && *out_buffer.write_pos <= '9')
                                        {
                                            b = *out_buffer.write_pos - '0';
                                            b &= 0x0f;
                                            delete_this_byte = 1;
                                            c->rpos = 1;
                                        } else
                                        {
                                            b = 0x0f;
                                        }
                                        out_buffer.write_pos[-1] = a | b;
                                    }
                                    break;
                            }
                            break;
                        case 'B':               // from bcd
                            switch(c->s1[3])
                            {
                                case 'A':       // to ascii
                                    if(((*out_buffer.write_pos >> 4) & 0x0f) <= 9 && 
                                            ((*out_buffer.write_pos & 0x0f) <= 9 || (*out_buffer.write_pos & 0x0f) == 0x0f))
                                    {
                                        a = (*out_buffer.write_pos >> 4) & 0x0f;
                                        b = *out_buffer.write_pos & 0x0f;
                                        *out_buffer.write_pos = '0' + a;
                                        if(!delete_this_byte && b != 0x0f) 
                                        {
                                            write_next_byte();
                                            *out_buffer.write_pos = '0' + b;
                                        }
                                    }
                                    break;
                            }
                            break;
                    }
                    break;
                case 'j':
                    if(in_buffer.block_offset < c->count)
                    {
                        while(c->next != NULL) c = c->next;     // skip rest of commands
                    }
                    break;
                case 'J':
                    if(in_buffer.block_num <= c->count)
                    {
                        while(c->next != NULL) c = c->next;     // skip rest of commands
                    }
                    break;
                case 'l':
                    if(in_buffer.block_offset >= c->count)
                    {
                        while(c->next != NULL) c = c->next;     // skip rest of commands
                    }
                    break;
                case 'L':
                    if(in_buffer.block_num > c->count)
                    {
                        while(c->next != NULL) c = c->next;     // skip rest of commands
                    }
                    break;
                case 'p':
                    if (delete_this_byte) break;
                    i = 0;
                    a = *out_buffer.write_pos;
                    while(i < c->s1_len)
                    {
                        str = byte_to_string(a,c->s1[i]);
                        write_string(str);
                        i++;
                        if (i < c->s1_len) 
                        {
                            put_byte('-');
                            write_next_byte();
                        }
                    }
                    put_byte(' ');
                    break;
                case 'F':
                    str = off_t_to_string(in_buffer.stream_offset + (off_t) (in_buffer.read_pos-in_buffer.buffer),c->s1[0]);
                    write_string(str);
                    put_byte(':');
                    write_next_byte();
                    break;
                case 'B':
                    str = off_t_to_string(in_buffer.block_num,c->s1[0]);
                    write_string(str);
                    put_byte(':');
                    write_next_byte();
                    break;
                case 'N':
                    write_string(get_current_file());
                    put_byte(':');
                    write_next_byte();
                    break;
                case '&':
                    put_byte(*out_buffer.write_pos & c->s1[0]);
                    break;
                case '|':
                    put_byte(*out_buffer.write_pos | c->s1[0]);
                    break;
                case '^':
                    put_byte(*out_buffer.write_pos ^ c->s1[0]);
                    break;
                case '~':
                    put_byte(~*out_buffer.write_pos);
                    break;
                case 'w':
                    break;
            }
        }
        c = c->next;
    }
}

/* write w command, will be called when output_buffer is written, same will be written to w-command files */
void
write_w_command(unsigned char *buf,size_t length)
{
    struct command *c;

    c = current_commands;

    while(c != NULL)
    {
        if(c->letter == 'w')
        {
            if(fwrite(buf,1,length,c->fd) != length) panic("Cannot write to file",c->s1,strerror(errno));
        }
        c = c->next;
    }
}



/* init_commands, initialize those wich need it, currently w - open file and rpos=0 for all */
void
init_commands(struct command *c)
{
    while(c != NULL)
    {
        switch(c->letter)
        {
            case 'w':
                c->fd = fopen(c->s1,"w");
                if(c->fd == NULL) panic("Cannot open file for writing",c->s1,strerror(errno));
                break;
        }
        c = c->next;
    }
}


/* close_commands, close those wich need it, currently w - close file */
void
close_commands(struct command *c)
{
    while(c != NULL)
    {
        switch(c->letter)
        {
            case 'w':
                if(fclose(c->fd) != 0) panic("Error in closing file",c->s1,strerror(errno));
                break;
        }
        c = c->next;
    }
}

/* reset the rpos counter for next block, in case block was shorter eg. delete count */
inline void
reset_rpos(struct command *c)
{
    while(c != NULL)
    {
        c->rpos = 0;
        c = c->next;
    }
}



/* main execution loop */
void
execute_program(struct command *c)
{
    int block_end;

    current_commands = c;

    while(find_block())
    {
        reset_rpos(c);
        delete_this_block = 0;
        out_buffer.block_offset = 0;
        execute_commands(c,BLOCK_START_COMMANDS);
        do
        {
            set_cycle_start();
            delete_this_byte = 0;
            inserting = 0;
            block_end = last_byte();
            put_byte(read_byte());     // as default write current byte from input
            execute_commands(c,BYTE_COMMANDS);
            if(!delete_this_byte && !delete_this_block)
            {
               write_next_byte();           // advance the write pointer if byte is not marked for del
            }
            if(!block_end && !inserting) get_next_byte();
        } while (!block_end || inserting);
        execute_commands(c,BLOCK_END_COMMANDS);
        flush_buffer();
    }
    close_output_stream();
}
OpenPOWER on IntegriCloud