~ubuntu-branches/ubuntu/maverick/radare/maverick

« back to all changes in this revision

Viewing changes to src/visual.c

  • Committer: Bazaar Package Importer
  • Author(s): SevenMachines
  • Date: 2010-09-07 15:44:27 UTC
  • mfrom: (1.1.3 upstream) (2.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100907154427-37u92vu12tqabqqz
Tags: 1:1.5.2-3ubuntu1
* Merge from debian testing (LP: #621016)
* debian/control:
     + libvala-dev transition to libval-0.10-dev (LP: #618809) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2006, 2007, 2008
 
2
 * Copyright (C) 2006, 2007, 2008, 2009
3
3
 *       pancake <youterm.com>
4
4
 *
5
5
 * radare is free software; you can redistribute it and/or modify
49
49
        char *cmd;
50
50
};
51
51
 
52
 
static u64 visual_seeks[255];
 
52
static ut64 visual_seeks[255];
53
53
static int visual_seeks_init = 0;
54
54
static int cursorseek=1; /* MUST BE IN SCR.CURSORSEEK */
55
55
static unsigned char *yank_buffer = NULL;
62
62
static int nbds = 0;
63
63
static struct binding *bds = NULL;
64
64
static int inv = 0;
65
 
static u64 mark = 0;
 
65
static ut64 mark = 0;
66
66
#define VMODES 12
67
67
static char modes[VMODES] =
68
68
{ FMT_HEXB, FMT_VISUAL, FMT_UDIS, FMT_OCT, FMT_CSTR,
97
97
//CMD_DECL(trace);
98
98
CMD_DECL(zoom_reset);
99
99
CMD_DECL(edit_screen_filter);
100
 
CMD_DECL(zoom_reset);
101
100
CMD_DECL(setblocksize);
102
101
CMD_DECL(visual_bind_run);
103
102
 
130
129
        COMMAND('\0',0, NULL, seek_to_flag)
131
130
};
132
131
 
133
 
 
134
132
#define TITLE if (config.color) cons_printf("\x1b[36m");
135
133
#define TITLE_END if (config.color) cons_printf("\x1b[0m");
136
134
 
145
143
        cons_printf(
146
144
        " :<cmd>     radare command (vi like)\n"
147
145
        " ;          edit or add comment\n"
148
 
        " \"         change metadata of current cursor seek\n"
 
146
        " \"          change metadata of current cursor seek\n"
149
147
        " ,.         ',' marks an offset, '.' seeks to mark or eip if no mark\n"
150
148
        " m'         m1 -> mark current position as char '1'. '1 will jump to mark 1\n"
151
149
        " g,G        seek to beggining or end of file\n"
157
155
        " f,F        seek between flag list (f = forward, F = backward)\n"
158
156
        " n,N        seek between hits of the hit0 search\n"
159
157
        " t          visual track/browse flagspaces and flags\n"
160
 
        " e          visual eval configuration variables\n"
 
158
        " e,E        visual eval configuration variables, Edit notes file\n"
161
159
        " C          toggle scr.color\n"
162
160
        " d          convert cursor selected bytes to ascii, code or hex\n"
163
161
//      " b[k][cmd]  binds key 'k' to the specified command\n"
170
168
        " H,L        scroll left, right by 2 bytes (16 bits).\n"
171
169
        " p,P        switch between hex, bin and string formats\n"
172
170
        " x          show xrefs of the current offset\n"
 
171
        " v          visual code analysis mode (edit/view functions, variables, ...)\n"
173
172
        " w          write hexpair bytes in cursor\n"
174
 
        " q          exits visual mode\n");
 
173
        " q          exits visual mode\n"
 
174
        " z, Z       zoom full file, reset zoom preferences\n");
175
175
        TITLE
176
176
        cons_printf("\nCursor mode:\n");
177
177
        TITLE_END
205
205
{
206
206
        const char *cmd = config_get("cmd.visualbind");
207
207
        if (cmd && cmd[0])
208
 
                radare_cmd_raw(cmd, 0);
 
208
                radare_cmd (cmd, 0);
209
209
}
210
210
 
211
211
CMD_DECL(edit_screen_filter)
214
214
        const char *ed = config_get("cfg.editor");
215
215
        if (!strnull(ed)) {
216
216
                /* TODO: Handle errors, put a different file name for */
217
 
                snprintf(buf, 1023, "%s/.radare/screen-filter.txt", getenv("HOME"));
 
217
                snprintf(buf, 1023, "%s/.radare/screen-filter.txt", get_home_directory());
218
218
                config_set("file.scrfilter", buf);
219
219
                eprintf(":!!%s '%s'\n", ed, buf);
220
220
                {
266
266
 
267
267
CMD_DECL(invert)
268
268
{
269
 
        int inv = config_get_i("cfg.inverse")^1;
270
 
        config_set("cfg.inverse", inv?"true":"false");
 
269
        int inv = (int) config_get_i ("cfg.inverse")^1;
 
270
        config_set ("cfg.inverse", inv?"true":"false");
271
271
        return 0;
272
272
}
273
273
 
274
274
CMD_DECL(zoom_reset)
275
275
{
276
 
        config.zoom.from = 0;
277
 
        config.zoom.size = config.size;
278
 
        config.zoom.piece = config.zoom.size/config.block_size;
 
276
        config_set_i ("zoom.from", 0);
 
277
        config_set_i ("zoom.to", config.size);
279
278
        return 0;
280
279
}
281
280
 
 
281
static void visual_edit ()
 
282
{
 
283
        int ret;
 
284
        char file[1024];
 
285
        char cmd[1024];
 
286
 
 
287
        snprintf (file, sizeof (file), "%s.txt", config_get("file.project"));
 
288
        // TODO: append screen dump to file
 
289
        snprintf (cmd, sizeof (cmd), "%s '%s'", config_get("cfg.editor"), file);
 
290
        if (system (cmd)) {
 
291
                eprintf ("command error (%s)\n", cmd);
 
292
                sleep (2);
 
293
        }
 
294
}
 
295
 
282
296
static void visual_convert_bytes(int fmt, int defkey)
283
297
{
284
298
        char argstr[128];
285
299
        int c;
286
 
        u64 off, len;
 
300
        ut64 off, len;
287
301
        if (fmt == -1) {
288
302
                if (defkey) {
289
303
                        c = defkey;
348
362
 
349
363
CMD_DECL(zoom)
350
364
{
351
 
        if (config.zoom.size == 0)
352
 
                config.zoom.size = config.size;
353
 
 
354
365
        CLRSCR();
355
 
        config.zoom.enabled ^= 1;
356
 
        if (config.zoom.enabled) {
357
 
                config.cursor = config.block_size * config.seek/config.size;
358
 
        //      config.cursor_mode = 1;
359
 
                config.zoom.size = config.block_size;
 
366
        if (config.cursor_mode && config.ocursor == -1) {
 
367
                config_set_i("zoom.from", 
 
368
                                config.zoom.from + config.cursor * config.zoom.piece);
 
369
                config_set_i("zoom.to", config.zoom.from + config.zoom.piece); 
 
370
                config.seek = config.zoom.from;
360
371
                radare_cmd("pO", 0);
361
 
        } else {
362
 
                if (config.cursor_mode && ( config.ocursor != -1)) {
363
 
                        config.zoom.from = config.cursor * config.zoom.piece;
364
 
                        config.zoom.size = config.cursor-config.ocursor;
365
 
                        if (config.zoom.size<0) config.zoom.size *= -1;
366
 
                        config.zoom.size *=config.zoom.piece;
367
 
                        config.zoom.piece = (config.zoom.size-config.zoom.from)/config.block_size;
368
 
                        config.zoom.enabled = 1;
369
 
                        config.seek = config.zoom.from;
370
 
                        radare_cmd("pO", 0);
 
372
        } else if (config.cursor_mode) {
 
373
                int cfrom, cto;
 
374
                if (config.ocursor > config.cursor) {
 
375
                        cfrom = config.cursor;
 
376
                        cto = config.ocursor;
371
377
                } else {
372
 
                        if (config.cursor_mode)
373
 
                                config.seek = config.zoom.from + config.cursor * config.zoom.piece;
374
 
                        radare_cmd("x", 0);
 
378
                        cfrom = config.ocursor;
 
379
                        cto = config.cursor;
375
380
                }
376
 
                /* reset cursor */
377
 
                config.cursor_mode = 0;
378
 
                config.cursor = 0;
379
 
                config.ocursor = -1;
 
381
                config_set_i("zoom.from", 
 
382
                                config.zoom.from + cfrom * config.zoom.piece);
 
383
                config_set_i("zoom.to",
 
384
                                config.zoom.from + (cto - cfrom) * config.zoom.piece);
 
385
                config.seek = config.zoom.from;
 
386
                radare_cmd("pO", 0);
 
387
        } else {
 
388
                config_set_i("zoom.from", 0);
 
389
                config_set_i("zoom.to", config.size);
 
390
                radare_cmd("pO", 0);
380
391
        }
 
392
        config.cursor = 0;
 
393
        config.ocursor = -1;
381
394
        CLRSCR();
382
395
        return 0;
383
396
}
516
529
        } else {
517
530
                if (config_get("file.write")) {
518
531
                        char *ptr = strchr(input, ' ');
519
 
                        u64 sz;
 
532
                        ut64 sz;
520
533
                        int off = 0;
521
 
                        u64 old = config.seek;
 
534
                        ut64 old = config.seek;
522
535
                        sz = get_math(ptr?ptr:input);
523
536
                        if (sz == 0)
524
537
                                sz = yank_buffer_size;
543
556
/* deprecated!! */
544
557
CMD_DECL(xrefs_here)
545
558
{
546
 
        u64 addr;
 
559
        ut64 addr;
547
560
        int foo;
548
561
        cons_printf("Select XREF from list:\n");
549
562
        data_xrefs_here(config.seek);
578
591
CMD_DECL(step_in_dbg)
579
592
{
580
593
        if (!config.debug) {
581
 
                eprintf("not in debugger\n");
582
 
                cons_any_key();
583
 
        } else
584
 
                radare_cmd("!step", 0);
585
 
        radare_sync();
 
594
                char *str = strdup (config_get("asm.profile"));
 
595
#warning non-debugger code emulation is not integrated correctly
 
596
                /* this is not working properly yet */
 
597
                radare_cmd ("avx 1", 0); // USE THE VM HERE
 
598
                radare_cmd (".av*", 0);
 
599
                radare_cmd("f vm.eip@vm.eip+$$$", 0);
 
600
                config_set ("asm.profile", str);
 
601
        } else radare_cmd ("!step", 0);
 
602
        radare_sync ();
586
603
        //trace_add(get_offset("eip"));
587
604
        return 0;
588
605
}
592
609
        if (!config.debug) {
593
610
                eprintf("not in debugger\n");
594
611
                cons_any_key();
595
 
        } else
596
 
                radare_cmd("!stepo", 0);
 
612
        } else radare_cmd("!stepo", 0);
597
613
        radare_sync();
598
614
        return 0;
599
615
}
601
617
CMD_DECL(seek_to_flag)
602
618
{
603
619
        flag_t *flg;
604
 
        unsigned char key;
605
 
 
606
 
        flag_list("");
607
 
        key = cons_readchar();
608
 
 
 
620
        ut8 key;
 
621
 
 
622
        flag_list ("");
 
623
        key = cons_readchar ();
609
624
        if (key>='0' && key<='9') {
610
625
                flg = flag_get_i(key-'0');
611
626
                if (flg) {
620
635
{
621
636
        if (config.cursor_mode)
622
637
                config.cursor = config.block_size - 1;
623
 
        else    config.seek = config.size - config.block_size;
 
638
        else config.seek = config.size - config.block_size;
624
639
        return 0;
625
640
}
626
641
 
631
646
                cons_any_key();
632
647
                return 1;
633
648
        }
634
 
 
635
649
        printf("write assembly (end with ^d):\n");
636
650
        fflush(stdout);
637
651
        cons_set_raw(0);
680
694
#if DEBUGGER
681
695
        char buf[16];
682
696
        if (config.debug) {
683
 
                radare_cmd("!hack", 0);
 
697
                radare_cmd ("!hack", 0);
684
698
        } else {
685
699
                /* for non debugger -- all of them must be available !! */
686
 
                printf(" 0 - nop one opcode\n");
687
 
                printf(" 1 - negate jump (jz->jnz , ja->jbe, ..)\n");
688
 
                printf(" 2 - force jmp (only for { 0f, 0x80-0x8f })\n");
689
 
                printf(" 5 - add ret\n");
690
 
                printf(" 6 - add ret with eax=0\n");
691
 
                printf(" 7 - negate zero flag (TODO)\n");
 
700
                printf (" 0 - nop one opcode\n");
 
701
                printf (" 1 - negate jump (jz->jnz , ja->jbe, ..)\n");
 
702
                printf (" 2 - force jmp (only for { 0f, 0x80-0x8f })\n");
 
703
                printf (" 5 - add ret\n");
 
704
                printf (" 6 - add ret with eax=0\n");
 
705
                printf (" 7 - negate zero flag (TODO)\n");
692
706
        }
693
707
        
694
 
        buf[0]='\0';
695
 
        buf[0]=cons_readchar();
696
 
        buf[1]='\0';
697
 
        arch_hack(buf);
698
 
        CLRSCR();
 
708
        buf[0] = '\0';
 
709
        buf[0] = cons_readchar();
 
710
        buf[1] = '\0';
 
711
        arch_hack (buf);
 
712
        CLRSCR ();
699
713
#endif
700
714
        return 0;
701
715
}
730
744
{
731
745
        char buf[1025];
732
746
        const char *dl_prompt_old = dl_prompt;
733
 
        u64 oseek = config.seek;
 
747
        ut64 oseek = config.seek;
734
748
 
735
749
        if (!config_get("file.write")) {
736
750
                eprintf("Not in write mode.\n");
759
773
                        io_write(config.fd, tmp, size);
760
774
                        free(tmp);
761
775
                        radare_seek(oseek, SEEK_SET);
762
 
                } else {
763
 
                        radare_cmd(buf, 0);
764
 
                }
 
776
                } else radare_cmd(buf, 0);
765
777
        }
766
778
        
767
779
        cons_set_raw(1);
787
799
        int i;
788
800
        if (last_print_format == FMT_ZOOM)
789
801
                return zoom_string;
790
 
 
791
802
        for(i=0;i<VMODES;i++)
792
803
                if (modes[i] == j)
793
804
                        return modestr[i];
808
819
static int keystroke_run(unsigned char key) {
809
820
        command_t cmd;
810
821
        int i = 0;
811
 
 
812
822
        for (cmd = keystrokes[0]; cmd.sname ; cmd = keystrokes[i++])
813
823
                if (cmd.sname == key) {
814
 
                        (void)cmd.hook(""); // no args
 
824
                        cmd.hook (""); // no args
815
825
                        if (cmd.options)
816
 
                                cons_any_key();
 
826
                                cons_any_key ();
817
827
                        return 1;
818
828
                }
819
 
 
820
829
        return 0;
821
830
}
822
831
 
826
835
#if HAVE_LIB_READLINE
827
836
        char *ptr;
828
837
#endif
829
 
        char key;
830
 
        char buf[1024];
 
838
        char key, buf[1024];
831
839
        int i, n;
832
840
 
833
841
        printf("Press a key to bind (? for listing): ");
848
856
                }
849
857
        }
850
858
        if (key=='?') {
851
 
                if (nbds == 0) {
852
 
                        cons_printf("No user keybindings defined.\n");
853
 
                } else {
 
859
                if (nbds != 0) {
854
860
                        cons_printf("Keybindings:");
855
861
                        cons_newline();
856
 
                        for(i=0;i<nbds;i++) {
857
 
                                cons_printf(" key '%c' = \"%s\"", bds[i].key, bds[i].cmd);
858
 
                                cons_newline();
859
 
                        }
860
 
                }
 
862
                        for(i=0;i<nbds;i++)
 
863
                                cons_printf(" key '%c' = \"%s\"\n", bds[i].key, bds[i].cmd);
 
864
                } else cons_printf("No user keybindings defined.\n");
861
865
                cons_any_key();
862
866
                return;
863
867
        }
934
938
                strcpy(buf, "<insert assembly> ('tab')");
935
939
                break;
936
940
        default:
937
 
                string_flag_offset(buf, config.seek, -2);
 
941
                string_flag_offset(NULL, buf, config.seek, -2);
938
942
                if (config.cursor!=-1)
939
 
                        string_flag_offset(buf2, config.seek+config.cursor, -1);
 
943
                        string_flag_offset(NULL, buf2, config.seek+config.cursor, -1);
940
944
        }
941
945
 
942
946
#if 0
943
947
        ptr = config_get("scr.seek");
944
948
        if (ptr&&ptr[0]&&last_print_format==FMT_REF) {
945
 
                u64 off = get_math(ptr);
 
949
                ut64 off = get_math(ptr);
946
950
                if (off != 0)
947
951
                radare_seek(off, SEEK_SET);
948
952
        }
950
954
 
951
955
        monitors_run();
952
956
 
953
 
        /* HUH */
954
957
        cons_clear00();
955
 
        //V cons_printf("\x1b[0m");
956
958
 
957
959
        if (inc >config.block_size)
958
960
                inc = config.block_size;
970
972
        cons_printf("[ 0x%llx (bs=%d mark=0x%llx) %s %s] %s            \n",
971
973
                (config.seek+config.vaddr),
972
974
                (unsigned int)config.block_size,
973
 
                mark, get_print_format_name(last_print_format),
974
 
                (inv)?"inv ":"", buf);
 
975
                mark, strget(get_print_format_name(last_print_format)),
 
976
                strget((char *)(size_t)inv),
 
977
                strget(buf));
975
978
        /* Spaguetti monster lives here ! */
976
979
        ptr = config_get("cmd.vprompt");
977
980
        if (ptr&&ptr[0]) {
1049
1052
 
1050
1053
static char *visual_prompt_entry(const char *title, char *buf, int len)
1051
1054
{
1052
 
        char *o =dl_prompt;
 
1055
        const char *o = dl_prompt;
1053
1056
        dl_prompt = title;
1054
1057
        cons_set_raw(1);
1055
1058
        cons_fgets(buf, len, 0, NULL);
1058
1061
        return buf;
1059
1062
}
1060
1063
 
1061
 
void visual_change_metadata(u64 seek)
 
1064
void visual_change_metadata(ut64 seek)
1062
1065
{
1063
 
        u64 off;
 
1066
        ut64 off;
1064
1067
        char buf[128];
1065
1068
        char buf2[128];
1066
1069
        int key;
1067
1070
        eprintf("\nCurrent seek: 0x%08llx\n\n", seek);
1068
 
        eprintf("q quit this menu\n");
1069
 
        eprintf("f Analyze code here to define a function\n");
1070
 
        eprintf("F Remove function definition\n");
1071
 
        eprintf("; Add comment\n");
1072
 
        eprintf("- Remove comment\n");
1073
 
        eprintf("c Convert to code (remove previous type conversions)\n");
1074
 
        eprintf("d Convert type of full block or selected bytes to hex data\n");
1075
 
        eprintf("x Add xref\n");
1076
 
        eprintf("X Remove xref\n");
1077
 
        eprintf("\n=> Press key: \n");
 
1071
        eprintf("q quit this menu\n"
 
1072
                "f Analyze code here to define a function\n"
 
1073
                "F Remove function definition\n"
 
1074
                "; Add comment\n"
 
1075
                "- Remove comment\n"
 
1076
                "c Convert to code (remove previous type conversions)\n"
 
1077
                "d Convert type of full block or selected bytes to hex data\n"
 
1078
                "x Add xref\n"
 
1079
                "X Remove xref\n"
 
1080
                "\n=> Press key: \n");
1078
1081
        key = cons_readchar();
1079
1082
        switch(key) {
1080
1083
        case 'q':
1101
1104
                radare_cmd(buf, 0);
1102
1105
                break;
1103
1106
        case 'x':
1104
 
                {
1105
 
                int type = 0;
1106
1107
                visual_prompt_entry("type (code/data): ", buf2, sizeof(buf2));
1107
 
                switch(buf2[0]) {
1108
 
                case 'c': type = 0; break;
1109
 
                case 'd': type = 1; break;
1110
 
                }
 
1108
                if (buf2[0] == 'd') key = 1; else key = 0;
1111
1109
                off = get_math(visual_prompt_entry("xref addr: ", buf, sizeof(buf)));
1112
 
                data_xrefs_add(seek, off, type);
1113
 
                }
 
1110
                data_xrefs_add(seek, off, key);
1114
1111
                break;
1115
1112
        case 'X':
1116
1113
                off = get_math(visual_prompt_entry("xref addr: ", buf, sizeof(buf)));
1121
1118
 
1122
1119
void visual_f(int f)
1123
1120
{
1124
 
        u64 addr;
 
1121
        ut64 addr;
1125
1122
        struct bp_t *bp;
1126
1123
        char line[128];
1127
1124
 
1128
 
        switch(f) {
1129
 
                case 1:
1130
 
                        cons_clear();
1131
 
                        radare_cmd("!help", 0);
1132
 
                        cons_any_key();
1133
 
                        cons_clear();
1134
 
                        break;
 
1125
        switch (f) {
 
1126
        case 1:
 
1127
                cons_clear();
 
1128
                radare_cmd("!help", 0);
 
1129
                cons_any_key();
 
1130
                cons_clear();
 
1131
                break;
1135
1132
#if DEBUGGER
1136
 
                case 2:
1137
 
                        addr = config.seek + (config.cursor_mode?config.acursor:0);
1138
 
                        bp = debug_bp_get(addr);
1139
 
                        if (bp) {
1140
 
                                sprintf(line, "!bp -0x%08x", (unsigned int)addr);
1141
 
                                radare_cmd(line,0);
1142
 
                                //debug_rm_bp(addr, 0);
1143
 
                        } else {
1144
 
                                sprintf(line, "!bp 0x%08x", (unsigned int)addr);
1145
 
                                radare_cmd(line,0);
1146
 
                        }
1147
 
                        if (!debug_bp_get(addr))
1148
 
                                flag_clear_by_addr(addr);
1149
 
                        cons_clear();
1150
 
                        break;
1151
 
                case 3:
1152
 
                        cons_set_raw(0);
1153
 
                        printf("Watchpoint at: ");
1154
 
                        strcpy(line, "!drw ");
1155
 
                        fgets(line+5, sizeof(line), stdin);
1156
 
                        line[strlen(line)-1]='\0';
1157
 
                        radare_cmd(line,0);
1158
 
                        cons_set_raw(1);
1159
 
                        cons_any_key();
1160
 
                        cons_clear();
1161
 
                        break;
1162
 
                case 4:
1163
 
                        if (config.cursor_mode) {
1164
 
                                sprintf(line, "!bp 0x%08llx", config.seek+config.cursor);
1165
 
                                radare_cmd_raw(line, 0);
1166
 
                                radare_cmd_raw("!cont", 0);
1167
 
                                sprintf(line, "!bp -0x%08llx", config.seek+config.cursor);
1168
 
                                radare_cmd_raw(line, 0);
1169
 
                        } else {
1170
 
                                radare_cmd("!contuh", 0);
1171
 
                                cons_clear();
1172
 
                        }
1173
 
                        break;
1174
 
                case 5:
1175
 
                        arch_jmp(config.seek + (config.cursor_mode?config.acursor:0));
1176
 
                        break;
 
1133
        case 2:
 
1134
                addr = config.seek + (config.cursor_mode?config.acursor:0);
 
1135
                bp = debug_bp_get(addr);
 
1136
                if (bp) {
 
1137
                        sprintf(line, "!bp -0x%08x", (unsigned int)addr);
 
1138
                        radare_cmd(line,0);
 
1139
                        //debug_rm_bp(addr, 0);
 
1140
                } else {
 
1141
                        sprintf(line, "!bp 0x%08x", (unsigned int)addr);
 
1142
                        radare_cmd(line,0);
 
1143
                }
 
1144
                if (!debug_bp_get(addr))
 
1145
                        flag_clear_by_addr(addr);
 
1146
                cons_clear();
 
1147
                break;
 
1148
        case 3:
 
1149
                cons_set_raw(0);
 
1150
                printf("Watchpoint at: ");
 
1151
                strcpy(line, "!drw ");
 
1152
                fgets(line+5, sizeof(line), stdin);
 
1153
                line[strlen(line)-1]='\0';
 
1154
                radare_cmd(line,0);
 
1155
                cons_set_raw(1);
 
1156
                cons_any_key();
 
1157
                cons_clear();
 
1158
                break;
 
1159
        case 4:
 
1160
                if (config.cursor_mode) {
 
1161
                        sprintf(line, "!bp 0x%08llx", config.seek+config.cursor);
 
1162
                        radare_cmd_raw(line, 0);
 
1163
                        radare_cmd_raw("!cont", 0);
 
1164
                        sprintf(line, "!bp -0x%08llx", config.seek+config.cursor);
 
1165
                        radare_cmd_raw(line, 0);
 
1166
                } else {
 
1167
                        radare_cmd("!contuh", 0);
 
1168
                        cons_clear();
 
1169
                }
 
1170
                break;
 
1171
        case 5:
 
1172
                arch_jmp(config.seek + (config.cursor_mode?config.acursor:0));
 
1173
                break;
1177
1174
#endif
1178
 
                case 6:
1179
 
                        radare_cmd("!contsc", 0);
1180
 
                        break;
1181
 
                case 7:
1182
 
                        if (config_get("trace.libs")) {
1183
 
                                //CMD_NAME(step)(NULL);
1184
 
                                debug_step(1);
1185
 
                        } else {
1186
 
                                CMD_NAME(stepu_in_dbg)(NULL);
1187
 
                        }
1188
 
                        break;
1189
 
                case 8:
1190
 
                        if (config.debug)
1191
 
                                CMD_NAME(stepo_in_dbg)(NULL);
1192
 
                        break;
 
1175
        case 6:
 
1176
                radare_cmd("!contsc", 0);
 
1177
                break;
 
1178
        case 7:
 
1179
                if (config_get("trace.libs")) {
 
1180
                        //CMD_NAME(step)(NULL);
 
1181
                        debug_step(1);
 
1182
                } else {
 
1183
                        CMD_NAME(stepu_in_dbg)(NULL);
 
1184
                }
 
1185
                break;
 
1186
        case 8:
 
1187
                if (config.debug)
 
1188
                        CMD_NAME(stepo_in_dbg)(NULL);
 
1189
                break;
1193
1190
        }
1194
1191
}
1195
1192
 
1198
1195
        unsigned char key;
1199
1196
        int i, lpf, ret;
1200
1197
        int nibble;
 
1198
        u8 byte;
1201
1199
        char line[1024];
1202
1200
        char tmpbuf[512];
1203
1201
#if HAVE_LIB_READLINE
1204
1202
        char *ptr, *optr;
1205
1203
#endif
1206
 
 
1207
1204
        switch(input[0]) {
1208
1205
        case 'g':
1209
1206
        case 'G':
1215
1212
        cons_get_real_columns();
1216
1213
        config_set_i("scr.width", config.width);
1217
1214
        config_set_i("scr.height", config.height);
1218
 
 
1219
1215
        nibble = 1; // high first
1220
 
 
1221
1216
        undo_push();
1222
1217
 
1223
 
#if 0
1224
 
        config.height = config_get_i("scr.height");
1225
 
        if (config.height<1)
1226
 
                config_set_i("scr.height", 24);
1227
 
#endif
1228
 
 
1229
1218
        if (bds == NULL)
1230
1219
                bds = (struct binding *)malloc(sizeof(struct binding));
1231
1220
 
1239
1228
        cons_set_raw(1);
1240
1229
        cons_flushable = 1;
1241
1230
        cons_skipxy(0,0);
1242
 
        while(1) {
 
1231
        while (1) {
1243
1232
                const char *scrseek = config_get("scr.seek");
1244
1233
                if (inc<1) inc = 1;
1245
1234
                dec = inc;
1253
1242
                visual_draw_screen();
1254
1243
                cons_flushit();
1255
1244
 
1256
 
                        // XXX 
1257
 
                if (config.debug&&last_print_format == FMT_VISUAL&&scrseek&&scrseek[0]) {
1258
 
                        u64 off = get_math(scrseek);
 
1245
                // XXX 
 
1246
                if (config.debug&&last_print_format==FMT_VISUAL&&scrseek&&scrseek[0]) {
 
1247
                        ut64 off = get_math(scrseek);
1259
1248
                        if ((off < config.seek) || ((config.seek+config.block_size) < off)) {
1260
1249
                                radare_seek(off, SEEK_SET);
1261
1250
                                continue;
1264
1253
 
1265
1254
        __go_read_a_key:
1266
1255
                /* user input */
1267
 
                key = cons_readchar();
 
1256
                if (input[0]) {
 
1257
                        key = input[0];
 
1258
                        strcpy(input, input+1);
 
1259
                } else key = cons_readchar();
1268
1260
 
1269
1261
                /* insert mode . 'i' key */
1270
 
                switch(config.insert_mode) {
 
1262
                switch (config.insert_mode) {
1271
1263
                case 1:
1272
1264
                        key = cons_get_arrow(key); // get ESC+char, return 'hjkl' char
1273
 
#if 0
1274
 
                        if (key==0x1b) {
1275
 
                                key = cons_readchar();
1276
 
                                if (key==0x5b) {
1277
 
                                        // TODO: must also work in interactive visual write ascii mode
1278
 
                                        key = cons_readchar();
1279
 
                                        switch(key) {
1280
 
                                        case 0x35: key='K'; break; // re.pag
1281
 
                                        case 0x36: key='J'; break; // av.pag
1282
 
                                        case 0x41: key='k'; break; // up
1283
 
                                        case 0x42: key='j'; break; // down
1284
 
                                        case 0x43: key='l'; break; // right
1285
 
                                        case 0x44: key='h'; break; // left
1286
 
                                        case 0x3b:
1287
 
                                                break;
1288
 
                                        default:
1289
 
                                                key = 0;
1290
 
                                        }
1291
 
                                }
1292
 
                        }
1293
 
#endif
1294
1265
                        switch(key) {
1295
1266
                        case 9: // TAB
1296
1267
                                if (last_print_format == FMT_DISAS
1590
1561
                        }
1591
1562
                }
1592
1563
 
1593
 
                if (inc<1)inc=1; // XXX dup check?
1594
 
                if (repeat==1)repeat=0;
 
1564
                if (inc<1) inc=1; // XXX dupped check?
 
1565
                if (repeat==1) repeat=0;
1595
1566
                //else repeat-=1;
1596
1567
                repeat = 1;
1597
1568
                for (i=0;i<repeat;i++)
1608
1579
                        cons_set_raw(0);
1609
1580
                        lpf = last_print_format;
1610
1581
                        config.visual=0;
1611
 
                cons_flushable = 0;
 
1582
                        cons_flushable = 0;
1612
1583
#if HAVE_LIB_READLINE
1613
1584
                        ptr = readline(VISUAL_PROMPT);
1614
1585
                        line[0]='\0';
1617
1588
                        //strcpy(line, ptr);
1618
1589
                        //ptr = line;
1619
1590
                        optr = ptr;
1620
 
                        
1621
1591
                        tmpbuf[0]='\0';
1622
1592
                        do {
1623
1593
                                if (ptr[strlen(ptr)-1]=='\\') {
1638
1608
                                ret = cons_fgets(ptr, 128, 0, NULL);
1639
1609
                        } while(ret);
1640
1610
                        free(optr);
1641
 
                        radare_cmd(line, 1);
1642
1611
#else
1643
1612
                        line[0]='\0';
1644
1613
                        dl_prompt = ":> ";
1645
1614
                        if (cons_fgets(line, 1000, 0, NULL) <0)
1646
1615
                                line[0]='\0';
1647
1616
                        //line[strlen(line)-1]='\0';
1648
 
                        radare_cmd(line, 1);
1649
1617
#endif
1650
 
                cons_flushable = 1;
1651
 
                cons_flush();
 
1618
                        if (strchr(line, '>') || strchr(line, '|') || strchr(line,'`'))
 
1619
                                eprintf("Cannot execute pipes in visual mode\n");
 
1620
                        else radare_cmd(line, 1);
 
1621
                        cons_flushable = 1;
 
1622
                        cons_flush();
1652
1623
                        config.visual=1;
1653
1624
                        last_print_format = lpf;
1654
1625
                        cons_set_raw(1);
1655
1626
                        if (line[0])
1656
1627
                                cons_any_key();
1657
 
                        cons_gotoxy(0,0);
 
1628
                        cons_gotoxy(0, 0);
1658
1629
                        cons_clear();
1659
1630
                        continue;
1660
1631
                case '"':
1677
1648
                                config.cursor = 0;
1678
1649
                        } else {
1679
1650
                                if (mark==0) {
1680
 
                                        u64 u = get_offset("eip");      
 
1651
                                        ut64 u = get_offset("eip");     
1681
1652
                                        if (u!=0) {
1682
1653
                                                undo_push();
1683
1654
                                                radare_seek(u, SEEK_SET);
1694
1665
                        break;
1695
1666
#endif
1696
1667
                case '\'':
1697
 
                        {
1698
 
                        u8 buf = (u8)cons_readchar();
1699
 
                        if (visual_seeks_init && visual_seeks[buf] != 0xFFFFFFFF)
 
1668
                        byte = (u8)cons_readchar();
 
1669
                        if (visual_seeks_init && visual_seeks[byte] != U64_MAX) {
1700
1670
                                undo_push();
1701
 
                                radare_seek(visual_seeks[buf], SEEK_SET);
 
1671
                                radare_seek(visual_seeks[byte], SEEK_SET);
1702
1672
                        }
1703
1673
                        break;
1704
1674
                case 'm':
1705
 
                        {
1706
 
                        u8 buf = (u8)cons_readchar();
 
1675
                        byte = (u8)cons_readchar();
1707
1676
                        if (visual_seeks_init == 0) {
1708
1677
                                int i;
1709
1678
                                for (i=0;i<255;i++)
1710
 
                                        visual_seeks[i] = 0xFFFFFFFF;
 
1679
                                        visual_seeks[i] = U64_MAX;
1711
1680
                                visual_seeks_init = 1;
1712
1681
                        }
1713
 
                        visual_seeks[buf] = config.seek;
1714
 
                        }
1715
 
                        break;
1716
 
#if 0
1717
 
                        printf("\nrfile magic:\n\n");
1718
 
                        radare_dump_and_process( DUMP_MAGIC, config.block_size);
1719
 
                        cons_any_key();
1720
 
                        break;
1721
 
#endif
 
1682
                        visual_seeks[byte] = config.seek;
 
1683
                        break;
1722
1684
                case 'd':
1723
1685
                        visual_convert_bytes(-1, 0);
1724
1686
                        break;
1772
1734
                                }
1773
1735
                                if (config.cursor >= config.block_size)
1774
1736
                                        config.cursor = config.block_size - 1;
1775
 
                        } else
1776
 
                                config.seek += 2;
 
1737
                        } else config.seek += 2;
1777
1738
                        break;
1778
1739
                case 0xd:
1779
1740
                        if (config.cursor_mode) {
1898
1859
                                        cons_clear(); }
1899
1860
                        }
1900
1861
                        break;
 
1862
                case 'E':
 
1863
                        visual_edit ();
 
1864
                        break;
1901
1865
                case 'e':
1902
1866
                        config_visual_menu();
1903
1867
                        break;
1904
1868
                case 't':
1905
1869
                        config.scrdelta += 20;
1906
 
                        flags_visual_menu();
 
1870
                        flags_visual_menu ();
1907
1871
                        config.scrdelta -= 20;
1908
1872
                        break;
 
1873
                case 'v':
 
1874
                        var_visual_menu ();
 
1875
                        break;
1909
1876
                case '<':
1910
1877
                        // fold
1911
1878
                        if (config.cursor_mode) {
1915
1882
                                        // unexpand function or close folder
1916
1883
                                        int type = data_type_range(config.seek+config.cursor);
1917
1884
                                        if (type == -1 || type == DATA_FOLD_O) {
1918
 
                                                data_set((u64)(config.seek+config.cursor), DATA_FOLD_C);
 
1885
                                                data_set((ut64)(config.seek+config.cursor), DATA_FOLD_C);
1919
1886
                                                cons_clear();
1920
1887
                                        }
1921
 
                                } else {
1922
 
                                        visual_convert_bytes(DATA_FOLD_C, 0);
1923
 
                                }
 
1888
                                } else visual_convert_bytes(DATA_FOLD_C, 0);
1924
1889
                        } else {
1925
1890
                                config.seek -= config.block_size;
1926
1891
                                if (config.seek % config.block_size)
1930
1895
                case '>':
1931
1896
                        if (config.cursor_mode) {
1932
1897
                                int type = data_type_range(config.seek+config.cursor);
1933
 
                                if (type == DATA_FOLD_C) {
 
1898
                                if (type == DATA_FOLD_C)
1934
1899
                                        data_set(config.seek+config.cursor, DATA_FOLD_O);
1935
 
                                }
1936
1900
                                cons_clear();
1937
 
                                
1938
1901
                                // unfold or expand
1939
1902
                                // check if current cursor position is jump -> open it
1940
1903
                                // check if current cursor position is a folder -> open it
1941
 
                        } else {
1942
 
                                config.seek += config.block_size - (config.seek % config.block_size);
1943
 
                        }
 
1904
                        } else config.seek += config.block_size - (config.seek % config.block_size);
1944
1905
                        break;
1945
1906
                case 'H':
1946
1907
                        if (config.cursor_mode) {
1954
1915
                                        config.ocursor = config.cursor+1;
1955
1916
                                if (IS_LTZ(config.cursor))
1956
1917
                                        config.cursor =0;
1957
 
                        } else
1958
 
                                config.seek -= 2;
 
1918
                        } else config.seek -= 2;
1959
1919
                        break;
1960
1920
                case 'l':
1961
1921
                        if (config.cursor_mode) {
1962
1922
                                if (++config.cursor>=config.block_size)
1963
1923
                                        config.cursor = config.block_size - 1;
1964
1924
                                config.ocursor = -1;
1965
 
                        } else
1966
 
                                config.seek++;
 
1925
                        } else config.seek++;
1967
1926
                        break;
1968
1927
                case '(': cons_skipxy(0,-1); break;
1969
1928
                case ')': cons_skipxy(0,1); break;
2004
1963
                                if (config.cursor>=0 && config.cursor< config.block_size) {
2005
1964
                                        c = config.cursor;
2006
1965
                                        ch = config.block[config.cursor];
2007
 
                                        sprintf(buf, "wx %02x @ 0x%llx", (unsigned char)(--ch), config.seek); //+config.cursor);
 
1966
                                        sprintf(buf, "wx %02x @ 0x%llx", (ut8)(--ch), config.seek);
2008
1967
                                        radare_cmd(buf, 0);
2009
1968
                                        config.cursor = c;
2010
1969
                                        config.ocursor = -1;
2011
1970
                                }
2012
 
                        } else radare_set_block_size_i(config.block_size-1);
 
1971
                        } else radare_set_block_size_i (config.block_size-1);
2013
1972
                        cons_clear();
2014
1973
                        break;
2015
1974
                case '!':
2032
1991
                        goto __visual_exit;
2033
1992
                default:
2034
1993
                        if (!keystroke_run(key)) {
2035
 
                                int i;
2036
1994
                                for(i=0;i<nbds;i++)
2037
1995
                                        if (bds[i].key == key)
2038
1996
                                                radare_cmd(bds[i].cmd, 0);
2039
1997
                                goto __go_read_a_key;
2040
1998
                        }
2041
1999
                }
2042
 
 
2043
2000
                if (config.seek<0)
2044
2001
                        config.seek = 0;
2045
2002
                do_repeat = 0;