adb3254a39d833c0c17bd0365a29ec9064a01c6c
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 * + this is an extra option when running in Fix mode. It
51 * causes a change of semantics when saving: instead of
52 * constructing a new backup file and writing it over the old
53 * one, we simply seek within the original file and write out
54 * all the pieces that have changed.
55 * + Primarily useful for editing disk devices directly
56 * (yikes!), or other situations where you actually cannot
57 * create a fresh copy of the file and rename(2) it into
59 * + I had intended to suggest that in Fix mode this would be
60 * nice and easy, since every element of the buffer tree is
61 * either a literal block (needs writing) or a from-file
62 * block denoting the same file _in the same position_.
63 * However, this is not in fact the case because you can cut
64 * and paste, so it's not that easy.
65 * + So I'm forced to the conclusion that when operating in
66 * this mode, it becomes illegal to cut and paste from-file
67 * blocks: they must be loaded in full at some point.
68 * * Thinking ahead about multiple-buffer operation: it
69 * would be a bad idea to keep a from-file block
70 * referencing /dev/hda and paste it into another ordinary
71 * buffer. But _also_ it would be a bad idea to paste a
72 * from-file block referencing a file stored _on_ /dev/hda
73 * into the in-place buffer dealing with /dev/hda itself.
74 * * So I'm forced to another odd conclusion, which is that
75 * from-file blocks must be eliminated in _two_ places:
76 * when copying a cut buffer _from_ an in-place buffer,
77 * _and_ when pasting a cut buffer _into_ one.
86 #if defined(unix) && !defined(GO32)
88 #include <sys/ioctl.h>
97 static void init(void);
98 static void done(void);
99 static void load_file (char *);
101 char toprint
[256]; /* LUT: printable versions of chars */
102 char hex
[256][3]; /* LUT: binary to hex, 1 byte */
106 char decstatus
[] = "%s TWEAK "VER
": %-18.18s %s posn=%-10d size=%-10d";
107 char hexstatus
[] = "%s TWEAK "VER
": %-18.18s %s posn=0x%-8X size=0x%-8X";
108 char *statfmt
= hexstatus
;
112 char *filename
= NULL
;
113 buffer
*filedata
, *cutbuffer
= NULL
;
114 int fix_mode
= FALSE
;
115 int look_mode
= FALSE
;
116 int eager_mode
= FALSE
;
117 int insert_mode
= FALSE
;
118 int edit_type
= 1; /* 1,2 are hex digits, 0=ascii */
119 int finished
= FALSE
;
121 int modified
= FALSE
;
122 int new_file
= FALSE
; /* shouldn't need initialisation -
123 * but let's not take chances :-) */
125 int realoffset
= 0, offset
= 16;
127 int ascii_enabled
= TRUE
;
129 long file_size
= 0, top_pos
= 0, cur_pos
= 0, mark_point
= 0;
136 int main(int argc
, char **argv
) {
137 int newoffset
= -1, newwidth
= -1;
140 * Parse command line arguments
142 pname
= *argv
; /* program name */
145 "usage: %s [-f] [-l] [-e] filename\n"
146 " or %s -D to write default tweak.rc to stdout\n",
152 char c
, *p
= *++argv
, *value
;
156 while (*p
) switch (c
= *p
++) {
160 * these parameters require arguments
167 fprintf(stderr
, "%s: option `-%c' requires an argument\n",
173 newoffset
= strtol(value
, NULL
, 0); /* allow `0xXX' */
176 newwidth
= strtol(value
, NULL
, 0);
196 fprintf(stderr
, "%s: multiple filenames specified\n", pname
);
204 fprintf(stderr
, "%s: no filename specified\n", pname
);
210 realoffset
= newoffset
;
213 load_file (filename
);
226 * Fix up `offset' to match `realoffset'. Also, while we're here,
227 * enable or disable ASCII mode and sanity-check the width.
229 void fix_offset(void) {
230 if (3*width
+11 > display_cols
) {
231 width
= (display_cols
-11) / 3;
232 sprintf (message
, "Width reduced to %d to fit on the screen", width
);
234 if (4*width
+14 > display_cols
) {
235 ascii_enabled
= FALSE
;
237 edit_type
= 1; /* force to hex mode */
239 ascii_enabled
= TRUE
;
240 offset
= realoffset
% width
;
246 * Initialise stuff at the beginning of the program: mostly the
249 static void init(void) {
254 display_define_colour(COL_BUFFER
, -1, -1);
255 display_define_colour(COL_SELECT
, 0, 7);
256 display_define_colour(COL_STATUS
, 11, 4);
257 display_define_colour(COL_ESCAPE
, 9, 0);
258 display_define_colour(COL_INVALID
, 11, 0);
260 for (i
=0; i
<256; i
++) {
261 sprintf(hex
[i
], "%02X", i
);
262 toprint
[i
] = (i
>=32 && i
<127 ? i
: '.');
267 * Clean up all the stuff that init() did.
269 static void done(void) {
274 * Load the file specified on the command line.
276 static void load_file (char *fname
) {
280 if ( (fp
= fopen (fname
, "rb")) ) {
283 static char buffer
[4096];
285 filedata
= buf_new_empty();
290 * We've opened the file. Load it.
292 while ( (len
= fread (buffer
, 1, sizeof(buffer
), fp
)) > 0 ) {
293 buf_insert_data (filedata
, buffer
, len
, file_size
);
297 assert(file_size
== buf_length(filedata
));
298 sprintf(message
, "loaded %s (size %ld == 0x%lX).",
299 fname
, file_size
, file_size
);
301 filedata
= buf_new_from_file(fp
);
302 file_size
= buf_length(filedata
);
303 sprintf(message
, "opened %s (size %ld == 0x%lX).",
304 fname
, file_size
, file_size
);
308 if (look_mode
|| fix_mode
) {
309 fprintf(stderr
, "%s: file %s not found, and %s mode active\n",
310 pname
, fname
, (look_mode ?
"LOOK" : "FIX"));
313 filedata
= buf_new_empty();
314 sprintf(message
, "New file %s.", fname
);
320 * Save the file. Return TRUE on success, FALSE on error.
322 int save_file (void) {
327 return FALSE
; /* do nothing! */
329 if ( (fp
= fopen (filename
, "wb")) ) {
330 static char buffer
[SAVE_BLKSIZ
];
332 while (pos
< file_size
) {
333 long size
= file_size
- pos
;
334 if (size
> SAVE_BLKSIZ
)
337 buf_fetch_data (filedata
, buffer
, size
, pos
);
338 if (size
!= fwrite (buffer
, 1, size
, fp
)) {
351 * Make a backup of the file, if such has not already been done.
352 * Return TRUE on success, FALSE on error.
354 int backup_file (void) {
355 char backup_name
[FILENAME_MAX
];
358 return TRUE
; /* unnecessary - pretend it's done */
359 strcpy (backup_name
, filename
);
360 #if defined(unix) && !defined(GO32)
361 strcat (backup_name
, ".bak");
367 for (p
= backup_name
; *p
; p
++) {
378 remove (backup_name
); /* don't care if this fails */
379 return !rename (filename
, backup_name
);
382 static unsigned char *scrbuf
= NULL
;
383 static int scrbuflines
= 0;
386 * Draw the screen, for normal usage.
388 void draw_scr (void) {
389 int scrsize
, scroff
, llen
, i
, j
;
391 int marktop
, markbot
, mark
;
396 scrlines
= display_rows
- 2;
397 if (scrlines
> scrbuflines
) {
399 realloc(scrbuf
, scrlines
*width
) :
400 malloc(scrlines
*width
));
403 fprintf(stderr
, "%s: out of memory!\n", pname
);
406 scrbuflines
= scrlines
;
409 linebuf
= malloc(width
*4+20);
412 fprintf(stderr
, "%s: out of memory!\n", pname
);
415 memset (linebuf
, ' ', width
*4+13);
416 linebuf
[width
*4+13] = '\0';
419 scroff
= width
- offset
;
422 scrsize
= scrlines
* width
- scroff
;
423 if (scrsize
> file_size
- top_pos
)
424 scrsize
= file_size
- top_pos
;
426 buf_fetch_data (filedata
, scrbuf
, scrsize
, top_pos
);
428 scrsize
+= scroff
; /* hack but it'll work */
430 mark
= marking
&& (cur_pos
!= mark_point
);
432 if (cur_pos
> mark_point
)
433 marktop
= mark_point
, markbot
= cur_pos
;
435 marktop
= cur_pos
, markbot
= mark_point
;
437 marktop
= markbot
= 0; /* placate gcc */
442 for (i
=0; i
<scrlines
; i
++) {
443 display_moveto (i
, 0);
444 if (currpos
<=cur_pos
|| currpos
<file_size
) {
445 p
= hex
[(currpos
>> 24) & 0xFF];
448 p
= hex
[(currpos
>> 16) & 0xFF];
451 p
= hex
[(currpos
>> 8) & 0xFF];
454 p
= hex
[currpos
& 0xFF];
457 for (j
=0; j
<width
; j
++) {
459 if (currpos
== 0 && j
< width
-offset
)
462 p
= hex
[*q
], c
= *q
++;
467 linebuf
[11+3*j
]=p
[0];
468 linebuf
[12+3*j
]=p
[1];
469 linebuf
[13+3*width
+j
]=toprint
[c
];
471 llen
= (currpos ? width
: offset
);
472 if (mark
&& currpos
<markbot
&& currpos
+llen
>marktop
) {
474 * Some of this line is marked. Maybe all. Whatever
475 * the precise details, there will be two regions
476 * requiring highlighting: a hex bit and an ascii
479 int localstart
= (currpos
<marktop?marktop
:currpos
) - currpos
;
480 int localstop
= (currpos
+llen
>markbot ? markbot
:
481 currpos
+llen
) - currpos
;
482 localstart
+= width
-llen
;
483 localstop
+= width
-llen
;
484 display_write_chars(linebuf
, 11+3*localstart
);
485 display_set_colour(COL_SELECT
);
486 display_write_chars(linebuf
+11+3*localstart
,
487 3*(localstop
-localstart
)-1);
488 display_set_colour(COL_BUFFER
);
490 display_write_chars(linebuf
+10+3*localstop
,
491 3+3*width
+localstart
-3*localstop
);
492 display_set_colour(COL_SELECT
);
493 display_write_chars(linebuf
+13+3*width
+localstart
,
494 localstop
-localstart
);
495 display_set_colour(COL_BUFFER
);
496 display_write_chars(linebuf
+13+3*width
+localstop
,
499 display_write_chars(linebuf
+10+3*localstop
,
500 2+3*width
-3*localstop
);
503 display_set_colour(COL_BUFFER
);
504 display_write_chars(linebuf
,
505 ascii_enabled ?
13+4*width
: 10+3*width
);
508 currpos
+= (currpos ? width
: offset
);
509 display_clear_to_eol();
515 display_moveto (display_rows
-2, 0);
516 display_set_colour(COL_STATUS
);
517 sprintf(status
, statfmt
,
518 (modified ?
"**" : " "),
520 (insert_mode ?
"(Insert)" :
521 look_mode ?
"(LOOK) " :
522 fix_mode ?
"(FIX) " : "(Ovrwrt)"),
524 slen
= strlen(status
);
525 if (slen
> display_cols
)
527 display_write_chars(status
, slen
);
528 while (slen
++ < display_cols
)
529 display_write_str(" ");
530 display_set_colour(COL_BUFFER
);
533 display_moveto (display_rows
-1, 0);
534 display_write_str (message
);
535 display_clear_to_eol();
538 i
= cur_pos
- top_pos
;
541 j
= (edit_type ?
(i
%width
)*3+10+edit_type
: (i
%width
)+13+3*width
);
542 if (j
>= display_cols
)
545 display_moveto (i
/width
, j
);
550 * Get a string, in the "minibuffer". Return TRUE on success, FALSE
551 * on break. Possibly syntax-highlight the entered string for
552 * backslash-escapes, depending on the "highlight" parameter.
554 int get_str (char *prompt
, char *buf
, int highlight
) {
555 int maxlen
= 79 - strlen(prompt
); /* limit to 80 - who cares? :) */
560 display_moveto (display_rows
-1, 0);
561 display_set_colour (COL_MINIBUF
);
562 display_write_str (prompt
);
564 char *q
, *p
= buf
, *r
= buf
+len
;
569 if (p
<r
&& *p
== '\\')
570 p
++, display_set_colour(COL_ESCAPE
);
571 else if (p
>=r
|| !isxdigit (*p
))
572 display_set_colour(COL_INVALID
);
573 else if (p
+1>=r
|| !isxdigit (p
[1]))
574 p
++, display_set_colour(COL_INVALID
);
576 p
+=2, display_set_colour(COL_ESCAPE
);
578 while (p
<r
&& *p
!= '\\')
580 display_set_colour (COL_MINIBUF
);
582 display_write_chars (q
, p
-q
);
585 display_write_chars (buf
, len
);
586 display_set_colour (COL_MINIBUF
);
587 display_clear_to_eol();
592 c
= display_getkey();
594 if (c
== 13 || c
== 10) {
597 } else if (c
== 27 || c
== 7) {
599 display_post_error();
600 strcpy (message
, "User Break!");
604 if (c
>= 32 && c
<= 126) {
611 if ((c
== 127 || c
== 8) && len
> 0)
614 if (c
== 'U'-'@') /* ^U kill line */
620 * Take a buffer containing possible backslash-escapes, and return
621 * a buffer containing a (binary!) string. Since the string is
622 * binary, it cannot be null terminated: hence the length is
623 * returned from the function. The string is processed in place.
625 * Escapes are simple: a backslash followed by two hex digits
626 * represents that character; a doubled backslash represents a
627 * backslash itself; a backslash followed by anything else is
628 * invalid. (-1 is returned if an invalid sequence is detected.)
630 int parse_quoted (char *buffer
) {
635 while (*p
&& *p
!= '\\')
641 else if (p
[1] && isxdigit(*p
) && isxdigit(p
[1])) {
646 *q
++ = strtol(buf
, NULL
, 16);
655 * Suspend program. (Or shell out, depending on OS, of course.)
658 #if defined(unix) && !defined(GO32)
664 spawnl (P_WAIT
, getenv("COMSPEC"), "", NULL
);
668 strcpy(message
, "Suspend function not yet implemented.");
672 volatile int safe_update
, update_required
;
675 display_recheck_size();
680 void schedule_update(void) {
684 update_required
= TRUE
;
687 long parse_num (char *buffer
, int *error
) {
690 if (!buffer
[strspn(buffer
, "0123456789")]) {
691 /* interpret as decimal */
693 } else if (buffer
[0]=='0' && (buffer
[1]=='X' || buffer
[1]=='x') &&
694 !buffer
[2+strspn(buffer
+2,"0123456789ABCDEFabcdef")]) {
695 return strtol(buffer
+2, NULL
, 16);
696 } else if (buffer
[0]=='$' &&
697 !buffer
[1+strspn(buffer
+1,"0123456789ABCDEFabcdef")]) {
698 return strtol(buffer
+1, NULL
, 16);