adae91379f8c978bf7cf6fd1d458303d04ed0592
2 * TODO possibly after that:
4 * - Need to handle >2Gb files! Up the `filesize' type to long
5 * long, and use it everywhere (not just in buffer.c).
7 * - Multiple buffers, multiple on-screen windows.
8 * + ^X^F to open new file
9 * + ^X^R to open new file RO
10 * + ^X b to switch buffers in a window
11 * + ^X o to switch windows
12 * + ^X 2 to split a window
13 * + ^X 1 to destroy all windows but this
14 * + ^X 0 to destroy this window
15 * + ^X ^ to enlarge this window by one line
16 * + width settings vary per buffer (aha, _that's_ why I wanted
17 * a buffer structure surrounding the raw B-tree)
18 * + hex-editor-style minibuffer for entering search terms,
19 * rather than the current rather crap one; in particular
20 * this enables pasting into the search string.
21 * + er, how exactly do we deal with the problem of saving over
22 * a file which we're maintaining references to in another
23 * buffer? The _current_ buffer can at least be sorted out by
24 * replacing it with a fresh tree containing a single
25 * file-data block, but other buffers are in trouble.
26 * * if we can rely on Unix fd semantics, this isn't too
27 * bad; we can just keep the fd open on the original file,
28 * and then the data stays around even after we rename(2)
29 * our new version over the top. Disk space usage gets
30 * silly after a few iterations, but it's better than
34 * + this actually doesn't seem _too_ horrid. For a start, one
35 * simple approach would be to clone the entire buffer B-tree
36 * every time we perform an operation! That's actually not
37 * _too_ expensive, if we maintain a limit on the number of
38 * operations we may undo.
39 * + I had also thought of cloning the tree we insert for each
40 * buf_insert_data and cloning the one removed for each
41 * buf_delete_data (both must be cloned for an overwrite),
42 * but I'm not convinced that simply cloning the entire thing
43 * isn't a superior option.
44 * + this really starts to show up the distinction between a
45 * `buffer' and a bare tree. A buffer is something which has
46 * an undo chain attached; so, in particular, the cut buffer
47 * shouldn't be one. Sort that out.
50 * + need to construct a reverse DFA.
51 * + probably should construct both every time, so that you can
52 * search forward for a thing and then immediately change
53 * your mind and search backward for the same thing.
56 * + this is an extra option when running in Fix mode. It
57 * causes a change of semantics when saving: instead of
58 * constructing a new backup file and writing it over the old
59 * one, we simply seek within the original file and write out
60 * all the pieces that have changed.
61 * + Primarily useful for editing disk devices directly
62 * (yikes!), or other situations where you actually cannot
63 * create a fresh copy of the file and rename(2) it into
65 * + I had intended to suggest that in Fix mode this would be
66 * nice and easy, since every element of the buffer tree is
67 * either a literal block (needs writing) or a from-file
68 * block denoting the same file _in the same position_.
69 * However, this is not in fact the case because you can cut
70 * and paste, so it's not that easy.
71 * + So I'm forced to the conclusion that when operating in
72 * this mode, it becomes illegal to cut and paste from-file
73 * blocks: they must be loaded in full at some point.
74 * * Thinking ahead about multiple-buffer operation: it
75 * would be a bad idea to keep a from-file block
76 * referencing /dev/hda and paste it into another ordinary
77 * buffer. But _also_ it would be a bad idea to paste a
78 * from-file block referencing a file stored _on_ /dev/hda
79 * into the in-place buffer dealing with /dev/hda itself.
80 * * So I'm forced to another odd conclusion, which is that
81 * from-file blocks must be eliminated in _two_ places:
82 * when copying a cut buffer _from_ an in-place buffer,
83 * _and_ when pasting a cut buffer _into_ one.
92 #if defined(unix) && !defined(GO32)
94 #include <sys/ioctl.h>
103 static void init(void);
104 static void done(void);
105 static void load_file (char *);
107 char toprint
[256]; /* LUT: printable versions of chars */
108 char hex
[256][3]; /* LUT: binary to hex, 1 byte */
112 char decstatus
[] = "%s TWEAK "VER
": %-18.18s %s posn=%-10d size=%-10d";
113 char hexstatus
[] = "%s TWEAK "VER
": %-18.18s %s posn=0x%-8X size=0x%-8X";
114 char *statfmt
= hexstatus
;
118 char *filename
= NULL
;
119 buffer
*filedata
, *cutbuffer
= NULL
;
120 int fix_mode
= FALSE
;
121 int look_mode
= FALSE
;
122 int eager_mode
= FALSE
;
123 int insert_mode
= FALSE
;
124 int edit_type
= 1; /* 1,2 are hex digits, 0=ascii */
125 int finished
= FALSE
;
127 int modified
= FALSE
;
128 int new_file
= FALSE
; /* shouldn't need initialisation -
129 * but let's not take chances :-) */
131 int realoffset
= 0, offset
= 16;
133 int ascii_enabled
= TRUE
;
135 long file_size
= 0, top_pos
= 0, cur_pos
= 0, mark_point
= 0;
142 int main(int argc
, char **argv
) {
143 int newoffset
= -1, newwidth
= -1;
146 * Parse command line arguments
148 pname
= *argv
; /* program name */
151 "usage: %s [-f] [-l] [-e] filename\n"
152 " or %s -D to write default tweak.rc to stdout\n",
158 char c
, *p
= *++argv
, *value
;
162 while (*p
) switch (c
= *p
++) {
166 * these parameters require arguments
173 fprintf(stderr
, "%s: option `-%c' requires an argument\n",
179 newoffset
= strtol(value
, NULL
, 0); /* allow `0xXX' */
182 newwidth
= strtol(value
, NULL
, 0);
202 fprintf(stderr
, "%s: multiple filenames specified\n", pname
);
210 fprintf(stderr
, "%s: no filename specified\n", pname
);
216 realoffset
= newoffset
;
219 load_file (filename
);
232 * Fix up `offset' to match `realoffset'. Also, while we're here,
233 * enable or disable ASCII mode and sanity-check the width.
235 void fix_offset(void) {
236 if (3*width
+11 > display_cols
) {
237 width
= (display_cols
-11) / 3;
238 sprintf (message
, "Width reduced to %d to fit on the screen", width
);
240 if (4*width
+14 > display_cols
) {
241 ascii_enabled
= FALSE
;
243 edit_type
= 1; /* force to hex mode */
245 ascii_enabled
= TRUE
;
246 offset
= realoffset
% width
;
252 * Initialise stuff at the beginning of the program: mostly the
255 static void init(void) {
260 display_define_colour(COL_BUFFER
, 7, 0);
261 display_define_colour(COL_SELECT
, 0, 7);
262 display_define_colour(COL_STATUS
, 11, 4);
263 display_define_colour(COL_ESCAPE
, 9, 0);
264 display_define_colour(COL_INVALID
, 11, 0);
266 for (i
=0; i
<256; i
++) {
267 sprintf(hex
[i
], "%02X", i
);
268 toprint
[i
] = (i
>=32 && i
<127 ? i
: '.');
273 * Clean up all the stuff that init() did.
275 static void done(void) {
280 * Load the file specified on the command line.
282 static void load_file (char *fname
) {
286 if ( (fp
= fopen (fname
, "rb")) ) {
289 static char buffer
[4096];
291 filedata
= buf_new_empty();
296 * We've opened the file. Load it.
298 while ( (len
= fread (buffer
, 1, sizeof(buffer
), fp
)) > 0 ) {
299 buf_insert_data (filedata
, buffer
, len
, file_size
);
303 assert(file_size
== buf_length(filedata
));
304 sprintf(message
, "loaded %s (size %ld == 0x%lX).",
305 fname
, file_size
, file_size
);
307 filedata
= buf_new_from_file(fp
);
308 file_size
= buf_length(filedata
);
309 sprintf(message
, "opened %s (size %ld == 0x%lX).",
310 fname
, file_size
, file_size
);
314 if (look_mode
|| fix_mode
) {
315 fprintf(stderr
, "%s: file %s not found, and %s mode active\n",
316 pname
, fname
, (look_mode ?
"LOOK" : "FIX"));
319 filedata
= buf_new_empty();
320 sprintf(message
, "New file %s.", fname
);
326 * Save the file. Return TRUE on success, FALSE on error.
328 int save_file (void) {
333 return FALSE
; /* do nothing! */
335 if ( (fp
= fopen (filename
, "wb")) ) {
336 static char buffer
[SAVE_BLKSIZ
];
338 while (pos
< file_size
) {
339 long size
= file_size
- pos
;
340 if (size
> SAVE_BLKSIZ
)
343 buf_fetch_data (filedata
, buffer
, size
, pos
);
344 if (size
!= fwrite (buffer
, 1, size
, fp
)) {
357 * Make a backup of the file, if such has not already been done.
358 * Return TRUE on success, FALSE on error.
360 int backup_file (void) {
361 char backup_name
[FILENAME_MAX
];
364 return TRUE
; /* unnecessary - pretend it's done */
365 strcpy (backup_name
, filename
);
366 #if defined(unix) && !defined(GO32)
367 strcat (backup_name
, ".bak");
373 for (p
= backup_name
; *p
; p
++) {
384 remove (backup_name
); /* don't care if this fails */
385 return !rename (filename
, backup_name
);
388 static unsigned char *scrbuf
= NULL
;
389 static int scrbuflines
= 0;
392 * Draw the screen, for normal usage.
394 void draw_scr (void) {
395 int scrsize
, scroff
, llen
, i
, j
;
397 int marktop
, markbot
, mark
;
402 scrlines
= display_rows
- 2;
403 if (scrlines
> scrbuflines
) {
405 realloc(scrbuf
, scrlines
*width
) :
406 malloc(scrlines
*width
));
409 fprintf(stderr
, "%s: out of memory!\n", pname
);
412 scrbuflines
= scrlines
;
415 linebuf
= malloc(width
*4+20);
418 fprintf(stderr
, "%s: out of memory!\n", pname
);
421 memset (linebuf
, ' ', width
*4+13);
422 linebuf
[width
*4+13] = '\0';
425 scroff
= width
- offset
;
428 scrsize
= scrlines
* width
- scroff
;
429 if (scrsize
> file_size
- top_pos
)
430 scrsize
= file_size
- top_pos
;
432 buf_fetch_data (filedata
, scrbuf
, scrsize
, top_pos
);
434 scrsize
+= scroff
; /* hack but it'll work */
436 mark
= marking
&& (cur_pos
!= mark_point
);
438 if (cur_pos
> mark_point
)
439 marktop
= mark_point
, markbot
= cur_pos
;
441 marktop
= cur_pos
, markbot
= mark_point
;
443 marktop
= markbot
= 0; /* placate gcc */
448 for (i
=0; i
<scrlines
; i
++) {
449 display_moveto (i
, 0);
450 if (currpos
<=cur_pos
|| currpos
<file_size
) {
451 p
= hex
[(currpos
>> 24) & 0xFF];
454 p
= hex
[(currpos
>> 16) & 0xFF];
457 p
= hex
[(currpos
>> 8) & 0xFF];
460 p
= hex
[currpos
& 0xFF];
463 for (j
=0; j
<width
; j
++) {
465 if (currpos
== 0 && j
< width
-offset
)
468 p
= hex
[*q
], c
= *q
++;
473 linebuf
[11+3*j
]=p
[0];
474 linebuf
[12+3*j
]=p
[1];
475 linebuf
[13+3*width
+j
]=toprint
[c
];
477 llen
= (currpos ? width
: offset
);
478 if (mark
&& currpos
<markbot
&& currpos
+llen
>marktop
) {
480 * Some of this line is marked. Maybe all. Whatever
481 * the precise details, there will be two regions
482 * requiring highlighting: a hex bit and an ascii
485 int localstart
= (currpos
<marktop?marktop
:currpos
) - currpos
;
486 int localstop
= (currpos
+llen
>markbot ? markbot
:
487 currpos
+llen
) - currpos
;
488 localstart
+= width
-llen
;
489 localstop
+= width
-llen
;
490 display_write_chars(linebuf
, 11+3*localstart
);
491 display_set_colour(COL_SELECT
);
492 display_write_chars(linebuf
+11+3*localstart
,
493 3*(localstop
-localstart
)-1);
494 display_set_colour(COL_BUFFER
);
496 display_write_chars(linebuf
+10+3*localstop
,
497 3+3*width
+localstart
-3*localstop
);
498 display_set_colour(COL_SELECT
);
499 display_write_chars(linebuf
+13+3*width
+localstart
,
500 localstop
-localstart
);
501 display_set_colour(COL_BUFFER
);
502 display_write_chars(linebuf
+13+3*width
+localstop
,
505 display_write_chars(linebuf
+10+3*localstop
,
506 2+3*width
-3*localstop
);
509 display_write_chars(linebuf
,
510 ascii_enabled ?
13+4*width
: 10+3*width
);
512 currpos
+= (currpos ? width
: offset
);
513 display_clear_to_eol();
519 display_moveto (display_rows
-2, 0);
520 display_set_colour(COL_STATUS
);
521 sprintf(status
, statfmt
,
522 (modified ?
"**" : " "),
524 (insert_mode ?
"(Insert)" :
525 look_mode ?
"(LOOK) " :
526 fix_mode ?
"(FIX) " : "(Ovrwrt)"),
528 slen
= strlen(status
);
529 if (slen
> display_cols
)
531 display_write_chars(status
, slen
);
532 while (slen
++ < display_cols
)
533 display_write_str(" ");
534 display_set_colour(COL_BUFFER
);
537 display_moveto (display_rows
-1, 0);
538 display_write_str (message
);
539 display_clear_to_eol();
542 i
= cur_pos
- top_pos
;
545 j
= (edit_type ?
(i
%width
)*3+10+edit_type
: (i
%width
)+13+3*width
);
546 if (j
>= display_cols
)
549 display_moveto (i
/width
, j
);
554 * Get a string, in the "minibuffer". Return TRUE on success, FALSE
555 * on break. Possibly syntax-highlight the entered string for
556 * backslash-escapes, depending on the "highlight" parameter.
558 int get_str (char *prompt
, char *buf
, int highlight
) {
559 int maxlen
= 79 - strlen(prompt
); /* limit to 80 - who cares? :) */
564 display_moveto (display_rows
-1, 0);
565 display_set_colour (COL_MINIBUF
);
566 display_write_str (prompt
);
568 char *q
, *p
= buf
, *r
= buf
+len
;
573 if (p
<r
&& *p
== '\\')
574 p
++, display_set_colour(COL_ESCAPE
);
575 else if (p
>=r
|| !isxdigit (*p
))
576 display_set_colour(COL_INVALID
);
577 else if (p
+1>=r
|| !isxdigit (p
[1]))
578 p
++, display_set_colour(COL_INVALID
);
580 p
+=2, display_set_colour(COL_ESCAPE
);
582 while (p
<r
&& *p
!= '\\')
584 display_set_colour (COL_MINIBUF
);
586 display_write_chars (q
, p
-q
);
589 display_write_chars (buf
, len
);
590 display_set_colour (COL_MINIBUF
);
591 display_clear_to_eol();
596 c
= display_getkey();
598 if (c
== 13 || c
== 10) {
601 } else if (c
== 27 || c
== 7) {
603 display_post_error();
604 strcpy (message
, "User Break!");
608 if (c
>= 32 && c
<= 126) {
615 if ((c
== 127 || c
== 8) && len
> 0)
618 if (c
== 'U'-'@') /* ^U kill line */
624 * Take a buffer containing possible backslash-escapes, and return
625 * a buffer containing a (binary!) string. Since the string is
626 * binary, it cannot be null terminated: hence the length is
627 * returned from the function. The string is processed in place.
629 * Escapes are simple: a backslash followed by two hex digits
630 * represents that character; a doubled backslash represents a
631 * backslash itself; a backslash followed by anything else is
632 * invalid. (-1 is returned if an invalid sequence is detected.)
634 int parse_quoted (char *buffer
) {
639 while (*p
&& *p
!= '\\')
645 else if (p
[1] && isxdigit(*p
) && isxdigit(p
[1])) {
650 *q
++ = strtol(buf
, NULL
, 16);
659 * Suspend program. (Or shell out, depending on OS, of course.)
662 #if defined(unix) && !defined(GO32)
668 spawnl (P_WAIT
, getenv("COMSPEC"), "", NULL
);
672 strcpy(message
, "Suspend function not yet implemented.");
676 volatile int safe_update
, update_required
;
679 display_recheck_size();
684 void schedule_update(void) {
688 update_required
= TRUE
;
691 long parse_num (char *buffer
, int *error
) {
694 if (!buffer
[strspn(buffer
, "0123456789")]) {
695 /* interpret as decimal */
697 } else if (buffer
[0]=='0' && (buffer
[1]=='X' || buffer
[1]=='x') &&
698 !buffer
[2+strspn(buffer
+2,"0123456789ABCDEFabcdef")]) {
699 return strtol(buffer
+2, NULL
, 16);
700 } else if (buffer
[0]=='$' &&
701 !buffer
[1+strspn(buffer
+1,"0123456789ABCDEFabcdef")]) {
702 return strtol(buffer
+1, NULL
, 16);