summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTimo Savinen <tjsa@iki.fi>2005-09-27 14:29:01 +0000
committerHadrien Dorio <hadrien.dorio@gmail.com>2017-12-16 00:24:05 +0100
commit11460c1f56ed4939c8f0df0482059f5fecd289fa (patch)
tree4734a3d9bd595f2e0b9c4a2d57ba04a3c1b8c287 /src
parentb9eb6e4429575318d3c510b99961093c42321529 (diff)
downloadbinary-block-editor-11460c1f56ed4939c8f0df0482059f5fecd289fa.tar.gz
binary-block-editor-11460c1f56ed4939c8f0df0482059f5fecd289fa.zip
0.1.2
Diffstat (limited to 'src')
-rw-r--r--src/bbe.c44
-rw-r--r--src/bbe.h8
-rw-r--r--src/buffer.c20
-rw-r--r--src/execute.c132
4 files changed, 156 insertions, 48 deletions
diff --git a/src/bbe.c b/src/bbe.c
index 9743ac2..6a57fab 100644
--- a/src/bbe.c
+++ b/src/bbe.c
@@ -20,7 +20,7 @@
*
*/
-/* $Id: bbe.c,v 1.30 2005/09/25 10:03:47 timo Exp $ */
+/* $Id: bbe.c,v 1.33 2005/09/30 10:58:15 timo Exp $ */
#include "bbe.h"
#ifdef HAVE_GETOPT_H
@@ -43,7 +43,7 @@ static char *program = "bbe";
#ifdef VERSION
static char *version = VERSION;
#else
-static char *version = "0.1.1";
+static char *version = "0.1.2";
#endif
#ifdef PACKAGE_BUGREPORT
@@ -176,9 +176,35 @@ parse_string(char *string,off_t *length)
if(*p == '\\')
{
p++;
- if(*p == '\\' || *p == ';')
+ if(strchr("\\;abtnvfr",*p) != NULL)
{
- buf[i] = *p++;
+ switch(*p)
+ {
+ case 'a':
+ buf[i] = '\a';
+ break;
+ case 'b':
+ buf[i] = '\b';
+ break;
+ case 't':
+ buf[i] = '\t';
+ break;
+ case 'n':
+ buf[i] = '\n';
+ break;
+ case 'v':
+ buf[i] = '\v';
+ break;
+ case 'f':
+ buf[i] = '\f';
+ break;
+ case 'r':
+ buf[i] = '\r';
+ break;
+ default:
+ buf[i] = *p;
+ }
+ p++;
} else
{
j = 0;
@@ -509,6 +535,16 @@ parse_command(char *command_string)
case 'N':
if(i != 1 || strlen(token[0]) > 1) panic("Error in command",command_string,NULL);
break;
+ case '&':
+ case '|':
+ case '^':
+ if(i != 2 || strlen(token[0]) > 1) panic("Error in command",command_string,NULL);
+ new->s1 = parse_string(token[1],&new->s1_len);
+ if(new->s1_len != 1) panic("Error in command",command_string,NULL);
+ break;
+ case '~':
+ if(i != 1 || strlen(token[0]) > 1) panic("Error in command",command_string,NULL);
+ break;
default:
panic("Unknown command",command_string,NULL);
break;
diff --git a/src/bbe.h b/src/bbe.h
index 721ad66..02a166f 100644
--- a/src/bbe.h
+++ b/src/bbe.h
@@ -20,7 +20,7 @@
*
*/
-/* $Id: bbe.h,v 1.20 2005/09/25 10:03:47 timo Exp $ */
+/* $Id: bbe.h,v 1.24 2005/09/29 11:27:01 timo Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -215,6 +215,12 @@ write_string(char *string);
extern char *
get_current_file(void);
+extern inline unsigned char *
+read_pos();
+
+extern inline unsigned char *
+block_end_pos();
+
/* global variables */
extern struct block block;
extern struct command *commands;
diff --git a/src/buffer.c b/src/buffer.c
index b5798d3..d7a5b4e 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -20,7 +20,7 @@
*
*/
-/* $Id: buffer.c,v 1.25 2005/09/25 10:03:47 timo Exp $ */
+/* $Id: buffer.c,v 1.30 2005/09/30 10:58:15 timo Exp $ */
#include "bbe.h"
#include <stdlib.h>
@@ -113,6 +113,8 @@ get_current_file(void)
struct io_file *prev;
off_t current_offset = in_buffer.stream_offset + (off_t) (in_buffer.read_pos-in_buffer.buffer);
+ if(f == NULL) return "";
+
while(f != NULL)
{
prev = f;
@@ -196,6 +198,20 @@ read_byte()
return *in_buffer.read_pos;
}
+/* returns pointer to the read position */
+inline unsigned char *
+read_pos()
+{
+ return in_buffer.read_pos;
+}
+
+/* return the block end pointer */
+inline unsigned char *
+block_end_pos()
+{
+ return in_buffer.block_end;
+}
+
/* advances the read pointer, if buffer has reached low water, get more from stream to buffer */
/* returns false in case of end of stream */
@@ -451,6 +467,8 @@ write_buffer(unsigned char *buf,off_t length)
{
unsigned char *save_pos;
+ if(!length) return;
+
if(length > (off_t) (OUTPUT_BUFFER_SIZE - (out_buffer.write_pos - out_buffer.buffer)))
{
save_pos = out_buffer.cycle_start < out_buffer.low_pos ? out_buffer.cycle_start : out_buffer.low_pos;
diff --git a/src/execute.c b/src/execute.c
index 9d43bc5..a69932b 100644
--- a/src/execute.c
+++ b/src/execute.c
@@ -1,4 +1,4 @@
-/*
+/*
* bbe - Binary block editor
*
* Copyright (C) 2005 Timo Savinen
@@ -20,7 +20,7 @@
*
*/
-/* $Id: execute.c,v 1.17 2005/09/25 10:03:47 timo Exp $ */
+/* $Id: execute.c,v 1.23 2005/09/30 10:58:15 timo Exp $ */
#include "bbe.h"
#include <stdlib.h>
@@ -35,18 +35,21 @@ 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 "DAJLFBN"
+#define BLOCK_START_COMMANDS "DIJLFBN"
/* commands to be executed for each byte */
-#define BYTE_COMMANDS "acdirsywjplJL"
+#define BYTE_COMMANDS "acdirsywjplJL&|^~"
/* commands to be executed at end of buffer */
-#define BLOCK_END_COMMANDS "IJL"
+#define BLOCK_END_COMMANDS "AJL"
/* byte_to_string, convert byte value to visible string,
@@ -63,7 +66,7 @@ byte_to_string(unsigned char byte,char format)
sprintf(string,"x%02x",(int) byte);
break;
case 'D':
- sprintf(string,"% 3d",(int) byte);
+ sprintf(string,"%3d",(int) byte);
break;
case 'O':
sprintf(string,"%03o",(int) byte);
@@ -111,7 +114,7 @@ execute_commands(struct command *c,char *command_letters)
{
register int i;
unsigned char a,b;
- char *f;
+ unsigned char *p;
char *str;
while(c != NULL)
@@ -141,51 +144,82 @@ execute_commands(struct command *c,char *command_letters)
if(c->offset == in_buffer.block_num || c->offset == 0) delete_this_block = 1;
break;
case 'i':
- if(c->offset == in_buffer.block_offset)
+ 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 (!delete_this_byte) write_next_byte();
- write_buffer(c->s1,c->s1_len);
- reverse_bytes(1);
+ 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(c->offset == in_buffer.block_offset)
+ if(in_buffer.block_offset >= c->offset &&
+ in_buffer.block_offset < c->offset + c->s1_len)
{
- put_byte(c->s1[0]);
- c->rpos=1;
- delete_this_byte = 0;
- } else if(c->rpos)
+ put_byte(c->s1[in_buffer.block_offset - c->offset]);
+ }
+ break;
+ case 's':
+ if(c->rpos)
{
- if(c->rpos < c->s1_len)
+ if(c->rpos < c->s1_len && c->rpos < c->s2_len)
{
- put_byte(c->s1[c->rpos]);
- c->rpos++;
- delete_this_byte = 0;
- } else
+ 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;
}
- break;
- case 's':
- if(delete_this_byte || out_buffer.block_offset + 1 < c->s1_len) break;
+ if(delete_this_byte) break;
+ p = out_buffer.write_pos;
i = 0;
- while(out_buffer.write_pos[i - c->s1_len + 1] == c->s1[i] && i < c->s1_len) i++;
- if(i < c->s1_len) break;
- reverse_bytes(c->s1_len - 1);
- if(c->s2_len)
+ while(*p == c->s1[i] && i < c->s1_len)
{
- write_buffer(c->s2,c->s2_len);
- reverse_bytes(1);
- } else
+ 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)
{
- delete_this_byte = 1;
+ 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] != read_byte() && i < c->s1_len) i++;
- if(c->s1[i] == read_byte() && i < c->s1_len) put_byte(c->s2[i]);
+ 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])
@@ -276,13 +310,14 @@ execute_commands(struct command *c,char *command_letters)
break;
case 'p':
if (delete_this_byte) break;
- f = c->s1;
- while(*f != 0)
+ i = 0;
+ a = *out_buffer.write_pos;
+ while(i < c->s1_len)
{
- str = byte_to_string(read_byte(),*f);
+ str = byte_to_string(a,c->s1[i]);
write_string(str);
- f++;
- if (*f != 0)
+ i++;
+ if (i < c->s1_len)
{
put_byte('-');
write_next_byte();
@@ -307,6 +342,18 @@ execute_commands(struct command *c,char *command_letters)
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;
}
@@ -400,15 +447,16 @@ execute_program(struct command *c)
{
set_cycle_start();
delete_this_byte = 0;
+ inserting = 0;
block_end = last_byte();
- put_byte(read_byte()); // as default write current byte from input
+ 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) get_next_byte();
- } while (!block_end);
+ if(!block_end && !inserting) get_next_byte();
+ } while (!block_end || inserting);
execute_commands(c,BLOCK_END_COMMANDS);
flush_buffer();
}
OpenPOWER on IntegriCloud