6f71356a1047ebbd5a99cbd68451e55bf9aea8cc
2 * Potential future TODO items. Points marked ISSUE need to be
3 * resolved one way or another, with good justification for the
4 * decision made, before implementation begins.
6 * - Multiple buffers, multiple on-screen windows.
7 * + ^X^F to open new file
8 * + ^X^R to open new file RO
9 * + ^X b to switch buffers in a window
10 * + ^X o to switch windows
11 * + ^X 2 to split a window
12 * + ^X 1 to destroy all windows but this
13 * + ^X 0 to destroy this window
14 * + ^X ^ to enlarge this window by one line
15 * + width settings vary per buffer (aha, _that's_ why I wanted
16 * a buffer structure surrounding the raw B-tree)
17 * + hex-editor-style minibuffer for entering search terms,
18 * rather than the current rather crap one; in particular
19 * this enables pasting into the search string.
20 * + ISSUE: how exactly do we deal with the problem of saving
21 * over a file which we're maintaining references to in
22 * another buffer? The _current_ buffer can at least be
23 * sorted out by replacing it with a fresh tree containing a
24 * single file-data block, but other buffers are in trouble.
25 * * if we can rely on Unix fd semantics, one option is just
26 * to keep the fd open on the original file, and then the
27 * data stays around even after we rename(2) our new
28 * version over the top. Disk space usage gets silly after
29 * a few iterations, but it's better than nothing.
32 * + this actually doesn't seem _too_ horrid. For a start, one
33 * simple approach would be to clone the entire buffer B-tree
34 * every time we perform an operation! That's actually not
35 * _too_ expensive, if we maintain a limit on the number of
36 * operations we may undo.
37 * + I had also thought of cloning the tree we insert for each
38 * buf_insert_data and cloning the one removed for each
39 * buf_delete_data (both must be cloned for an overwrite),
40 * but I'm not convinced that simply cloning the entire thing
41 * isn't a superior option.
42 * + this really starts to show up the distinction between a
43 * `buffer' and a bare tree. A buffer is something which has
44 * an undo chain attached; so, in particular, the cut buffer
45 * shouldn't be one. Sort that out.
48 * + this is an extra option useful for editing disk devices
49 * directly (!), or other situation in which it's impossible
50 * or impractical to rename(2) your new file over the old
51 * one. It causes a change of semantics when saving: instead
52 * of constructing a new backup file and writing it over the
53 * old one, we simply seek within the original file and write
54 * out all the pieces that have changed.
55 * + Saving the file involves identifying the bits of the file
56 * that need to change, and changing them. A piece of file
57 * can be discarded as `no change required' if it's
58 * represented in the buffer by a from-file block whose file
59 * offset is equal to its offset in the buffer.
60 * * Once we have identified all the bits that do need to
61 * change, we have to draw up a dependency graph to
62 * indicate which bits want to be copied from which other
63 * bits. (You don't want to overwrite a piece of file if
64 * you still have from-file blocks pointing at that
65 * piece.) This is a directed graph with nodes
66 * corresponding to intervals of the file, and edges
67 * indicating that the source node's interval is intended
68 * to end up containing the data from the target node's
69 * interval in the original file. Another node type is
70 * `literal data', which can be the target of an edge but
72 * - note that this means any two nodes connected by an
73 * edge must represent intervals of the same length.
74 * Sometimes this means that an interval must be split
75 * into pieces even though it is represented in the
76 * buffer by a single large from-file block (if
77 * from-file blocks copying _from_ it don't cover the
78 * whole of it). I suspect the simplest approach here
79 * is just to start by making a B-tree of division
80 * points in the file: every from-file block adds four
81 * division points (for start and end of both source
82 * and dest interval), and once the tree is complete,
83 * each graph node represents the interval between two
84 * adjacent division points.
85 * - ISSUE: actually, that strategy is inadequate:
86 * consider a large from-file block displaced by only
87 * one byte from its source location. The above
88 * strategy gives division points at x, x+1, x+y,
89 * x+y+1, but the interval [x,x+1] actually wants to
90 * point to [x+1,x+2] and we don't have a division
91 * point for that. Worse still, finding a way to add
92 * the remaining division points is also undesirable
93 * because there'd be so many of them. Needs design
95 * * Then, any node which is not the target of any edge
96 * represents a piece of file which it's safe to write
97 * over, so we do so and throw away the node.
98 * * If we run out of such nodes and the graph is still
99 * non-empty, it's because all remaining nodes are part of
100 * loops. A loop must represent a set of disjoint
101 * intervals in the file, all the same length, which need
102 * to be permuted cyclically. So we deal with such a loop
103 * by reading a chunk of data from the start of one of the
104 * intervals and holding it, then copying from the next
105 * interval to that one, and so on until we've gone round
107 * + the intervals in the loop might be far too big to
108 * hold an entire interval's worth of real data in
109 * memory, so we might have to do it piecewise.
110 * + ISSUE: I wonder if a warning of some sort might be in
111 * order for if you accidentally request most of the file be
112 * moved about. This sort of trickery is really intended for
113 * small changes to a large file; if you (say) enable insert
114 * mode while editing a hard disk and accidentally leave
115 * everything one byte further up, you _really_ don't want to
116 * hit Save. The semantics of the warning are difficult,
128 #if defined(unix) && !defined(GO32)
130 #include <sys/ioctl.h>
137 static void init(void);
138 static void done(void);
139 static void load_file (char *);
141 char toprint
[256]; /* LUT: printable versions of chars */
142 char hex
[256][3]; /* LUT: binary to hex, 1 byte */
146 char decstatus
[] = "%s TWEAK "VER
": %-18.18s %s posn=%-10"OFF
"d size=%-10"OFF
"d";
147 char hexstatus
[] = "%s TWEAK "VER
": %-18.18s %s posn=0x%-8"OFF
"X size=0x%-8"OFF
"X";
148 char *statfmt
= hexstatus
;
152 char *filename
= NULL
;
153 buffer
*filedata
, *cutbuffer
= NULL
;
154 int fix_mode
= FALSE
;
155 int look_mode
= FALSE
;
156 int eager_mode
= FALSE
;
157 int insert_mode
= FALSE
;
158 int edit_type
= 1; /* 1,2 are hex digits, 0=ascii */
159 int finished
= FALSE
;
161 int modified
= FALSE
;
162 int new_file
= FALSE
; /* shouldn't need initialisation -
163 * but let's not take chances :-) */
164 fileoffset_t width
= 16;
165 fileoffset_t realoffset
= 0, offset
= 16;
167 int ascii_enabled
= TRUE
;
169 fileoffset_t file_size
= 0, top_pos
= 0, cur_pos
= 0, mark_point
= 0;
176 int main(int argc
, char **argv
) {
177 fileoffset_t newoffset
= -1, newwidth
= -1;
180 * Parse command line arguments
182 pname
= *argv
; /* program name */
185 "usage: %s [-f] [-l] [-e] filename\n"
186 " or %s -D to write default tweak.rc to stdout\n",
192 char c
, *p
= *++argv
, *value
;
196 while (*p
) switch (c
= *p
++) {
200 * these parameters require arguments
207 fprintf(stderr
, "%s: option `-%c' requires an argument\n",
213 newoffset
= parse_num(value
, NULL
);
216 newwidth
= parse_num(value
, NULL
);
236 fprintf(stderr
, "%s: multiple filenames specified\n", pname
);
244 fprintf(stderr
, "%s: no filename specified\n", pname
);
250 realoffset
= newoffset
;
253 load_file (filename
);
266 * Fix up `offset' to match `realoffset'. Also, while we're here,
267 * enable or disable ASCII mode and sanity-check the width.
269 void fix_offset(void) {
270 if (3*width
+11 > display_cols
) {
271 width
= (display_cols
-11) / 3;
272 sprintf (message
, "Width reduced to %"OFF
"d to fit on the screen", width
);
274 if (4*width
+14 > display_cols
) {
275 ascii_enabled
= FALSE
;
277 edit_type
= 1; /* force to hex mode */
279 ascii_enabled
= TRUE
;
280 offset
= realoffset
% width
;
286 * Initialise stuff at the beginning of the program: mostly the
289 static void init(void) {
294 display_define_colour(COL_BUFFER
, -1, -1, FALSE
);
295 display_define_colour(COL_SELECT
, 0, 7, TRUE
);
296 display_define_colour(COL_STATUS
, 11, 4, TRUE
);
297 display_define_colour(COL_ESCAPE
, 9, 0, FALSE
);
298 display_define_colour(COL_INVALID
, 11, 0, FALSE
);
300 for (i
=0; i
<256; i
++) {
301 sprintf(hex
[i
], "%02X", i
);
302 toprint
[i
] = (i
>=32 && i
<127 ? i
: '.');
307 * Clean up all the stuff that init() did.
309 static void done(void) {
314 * Load the file specified on the command line.
316 static void load_file (char *fname
) {
320 if ( (fp
= fopen (fname
, "rb")) ) {
323 static char buffer
[4096];
325 filedata
= buf_new_empty();
330 * We've opened the file. Load it.
332 while ( (len
= fread (buffer
, 1, sizeof(buffer
), fp
)) > 0 ) {
333 buf_insert_data (filedata
, buffer
, len
, file_size
);
337 assert(file_size
== buf_length(filedata
));
338 sprintf(message
, "loaded %s (size %"OFF
"d == 0x%"OFF
"X).",
339 fname
, file_size
, file_size
);
341 filedata
= buf_new_from_file(fp
);
342 file_size
= buf_length(filedata
);
343 sprintf(message
, "opened %s (size %"OFF
"d == 0x%"OFF
"X).",
344 fname
, file_size
, file_size
);
348 if (look_mode
|| fix_mode
) {
349 fprintf(stderr
, "%s: file %s not found, and %s mode active\n",
350 pname
, fname
, (look_mode ?
"LOOK" : "FIX"));
353 filedata
= buf_new_empty();
354 sprintf(message
, "New file %s.", fname
);
360 * Save the file. Return TRUE on success, FALSE on error.
362 int save_file (void) {
364 fileoffset_t pos
= 0;
367 return FALSE
; /* do nothing! */
369 if ( (fp
= fopen (filename
, "wb")) ) {
370 static char buffer
[SAVE_BLKSIZ
];
372 while (pos
< file_size
) {
373 fileoffset_t size
= file_size
- pos
;
374 if (size
> SAVE_BLKSIZ
)
377 buf_fetch_data (filedata
, buffer
, size
, pos
);
378 if (size
!= fwrite (buffer
, 1, size
, fp
)) {
391 * Make a backup of the file, if such has not already been done.
392 * Return TRUE on success, FALSE on error.
394 int backup_file (void) {
395 char backup_name
[FILENAME_MAX
];
398 return TRUE
; /* unnecessary - pretend it's done */
399 strcpy (backup_name
, filename
);
400 #if defined(unix) && !defined(GO32)
401 strcat (backup_name
, ".bak");
407 for (p
= backup_name
; *p
; p
++) {
418 remove (backup_name
); /* don't care if this fails */
419 return !rename (filename
, backup_name
);
422 static unsigned char *scrbuf
= NULL
;
423 static int scrbuflines
= 0;
426 * Draw the screen, for normal usage.
428 void draw_scr (void) {
429 int scrsize
, scroff
, llen
, i
, j
;
430 fileoffset_t currpos
;
431 fileoffset_t marktop
, markbot
;
437 scrlines
= display_rows
- 2;
438 if (scrlines
> scrbuflines
) {
440 realloc(scrbuf
, scrlines
*width
) :
441 malloc(scrlines
*width
));
444 fprintf(stderr
, "%s: out of memory!\n", pname
);
447 scrbuflines
= scrlines
;
450 linebuf
= malloc(width
*4+20);
453 fprintf(stderr
, "%s: out of memory!\n", pname
);
456 memset (linebuf
, ' ', width
*4+13);
457 linebuf
[width
*4+13] = '\0';
460 scroff
= width
- offset
;
463 scrsize
= scrlines
* width
- scroff
;
464 if (scrsize
> file_size
- top_pos
)
465 scrsize
= file_size
- top_pos
;
467 buf_fetch_data (filedata
, scrbuf
, scrsize
, top_pos
);
469 scrsize
+= scroff
; /* hack but it'll work */
471 mark
= marking
&& (cur_pos
!= mark_point
);
473 if (cur_pos
> mark_point
)
474 marktop
= mark_point
, markbot
= cur_pos
;
476 marktop
= cur_pos
, markbot
= mark_point
;
478 marktop
= markbot
= 0; /* placate gcc */
483 for (i
=0; i
<scrlines
; i
++) {
484 display_moveto (i
, 0);
485 if (currpos
<=cur_pos
|| currpos
<file_size
) {
486 p
= hex
[(currpos
>> 24) & 0xFF];
489 p
= hex
[(currpos
>> 16) & 0xFF];
492 p
= hex
[(currpos
>> 8) & 0xFF];
495 p
= hex
[currpos
& 0xFF];
498 for (j
=0; j
<width
; j
++) {
500 if (currpos
== 0 && j
< width
-offset
)
503 p
= hex
[*q
], c
= *q
++;
508 linebuf
[11+3*j
]=p
[0];
509 linebuf
[12+3*j
]=p
[1];
510 linebuf
[13+3*width
+j
]=toprint
[c
];
512 llen
= (currpos ? width
: offset
);
513 if (mark
&& currpos
<markbot
&& currpos
+llen
>marktop
) {
515 * Some of this line is marked. Maybe all. Whatever
516 * the precise details, there will be two regions
517 * requiring highlighting: a hex bit and an ascii
520 fileoffset_t localstart
= (currpos
<marktop ? marktop
:
522 fileoffset_t localstop
= (currpos
+llen
>markbot ? markbot
:
523 currpos
+llen
) - currpos
;
524 localstart
+= width
-llen
;
525 localstop
+= width
-llen
;
526 display_write_chars(linebuf
, 11+3*localstart
);
527 display_set_colour(COL_SELECT
);
528 display_write_chars(linebuf
+11+3*localstart
,
529 3*(localstop
-localstart
)-1);
530 display_set_colour(COL_BUFFER
);
532 display_write_chars(linebuf
+10+3*localstop
,
533 3+3*width
+localstart
-3*localstop
);
534 display_set_colour(COL_SELECT
);
535 display_write_chars(linebuf
+13+3*width
+localstart
,
536 localstop
-localstart
);
537 display_set_colour(COL_BUFFER
);
538 display_write_chars(linebuf
+13+3*width
+localstop
,
541 display_write_chars(linebuf
+10+3*localstop
,
542 2+3*width
-3*localstop
);
545 display_set_colour(COL_BUFFER
);
546 display_write_chars(linebuf
,
547 ascii_enabled ?
13+4*width
: 10+3*width
);
550 currpos
+= (currpos ? width
: offset
);
551 display_clear_to_eol();
557 display_moveto (display_rows
-2, 0);
558 display_set_colour(COL_STATUS
);
559 sprintf(status
, statfmt
,
560 (modified ?
"**" : " "),
562 (insert_mode ?
"(Insert)" :
563 look_mode ?
"(LOOK) " :
564 fix_mode ?
"(FIX) " : "(Ovrwrt)"),
566 slen
= strlen(status
);
567 if (slen
> display_cols
)
569 display_write_chars(status
, slen
);
570 while (slen
++ < display_cols
)
571 display_write_str(" ");
572 display_set_colour(COL_BUFFER
);
575 display_moveto (display_rows
-1, 0);
576 display_write_str (message
);
577 display_clear_to_eol();
580 i
= cur_pos
- top_pos
;
583 j
= (edit_type ?
(i
%width
)*3+10+edit_type
: (i
%width
)+13+3*width
);
584 if (j
>= display_cols
)
587 display_moveto (i
/width
, j
);
591 volatile int safe_update
, update_required
;
595 * Get a string, in the "minibuffer". Return TRUE on success, FALSE
596 * on break. Possibly syntax-highlight the entered string for
597 * backslash-escapes, depending on the "highlight" parameter.
599 int get_str (char *prompt
, char *buf
, int highlight
) {
600 int maxlen
= 79 - strlen(prompt
); /* limit to 80 - who cares? :) */
605 display_moveto (display_rows
-1, 0);
606 display_set_colour (COL_MINIBUF
);
607 display_write_str (prompt
);
609 char *q
, *p
= buf
, *r
= buf
+len
;
614 if (p
<r
&& *p
== '\\')
615 p
++, display_set_colour(COL_ESCAPE
);
616 else if (p
>=r
|| !isxdigit ((unsigned char)*p
))
617 display_set_colour(COL_INVALID
);
618 else if (p
+1>=r
|| !isxdigit ((unsigned char)p
[1]))
619 p
++, display_set_colour(COL_INVALID
);
621 p
+=2, display_set_colour(COL_ESCAPE
);
623 while (p
<r
&& *p
!= '\\')
625 display_set_colour (COL_MINIBUF
);
627 display_write_chars (q
, p
-q
);
630 display_write_chars (buf
, len
);
631 display_set_colour (COL_MINIBUF
);
632 display_clear_to_eol();
637 c
= display_getkey();
639 if (c
== 13 || c
== 10) {
642 } else if (c
== 27 || c
== 7) {
644 display_post_error();
645 strcpy (message
, "User Break!");
649 if (c
>= 32 && c
<= 126) {
656 if ((c
== 127 || c
== 8) && len
> 0)
659 if (c
== 'U'-'@') /* ^U kill line */
665 * Take a buffer containing possible backslash-escapes, and return
666 * a buffer containing a (binary!) string. Since the string is
667 * binary, it cannot be null terminated: hence the length is
668 * returned from the function. The string is processed in place.
670 * Escapes are simple: a backslash followed by two hex digits
671 * represents that character; a doubled backslash represents a
672 * backslash itself; a backslash followed by anything else is
673 * invalid. (-1 is returned if an invalid sequence is detected.)
675 int parse_quoted (char *buffer
) {
680 while (*p
&& *p
!= '\\')
686 else if (p
[1] && isxdigit((unsigned char)*p
) &&
687 isxdigit((unsigned char)p
[1])) {
692 *q
++ = strtol(buf
, NULL
, 16);
701 * Suspend program. (Or shell out, depending on OS, of course.)
704 #if defined(unix) && !defined(GO32)
710 spawnl (P_WAIT
, getenv("COMSPEC"), "", NULL
);
714 strcpy(message
, "Suspend function not yet implemented.");
719 display_recheck_size();
724 void schedule_update(void) {
728 update_required
= TRUE
;
731 fileoffset_t
parse_num (char *buffer
, int *error
) {
734 if (!buffer
[strspn(buffer
, "0123456789")]) {
735 /* interpret as decimal */
736 return ATOOFF(buffer
);
737 } else if (buffer
[0]=='0' && (buffer
[1]=='X' || buffer
[1]=='x') &&
738 !buffer
[2+strspn(buffer
+2,"0123456789ABCDEFabcdef")]) {
739 return STRTOOFF(buffer
+2, NULL
, 16);
740 } else if (buffer
[0]=='$' &&
741 !buffer
[1+strspn(buffer
+1,"0123456789ABCDEFabcdef")]) {
742 return STRTOOFF(buffer
+1, NULL
, 16);