1 /* radare - LGPL - Copyright 2015 - julien (jvoisin) voisin */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include <r_lib.h>
8 #include <r_util.h>
9 #include <r_flag.h>
10 #include <r_anal.h>
11 #include <r_parse.h>
12
replace(int argc,const char * argv[],char * newstr)13 static int replace(int argc, const char *argv[], char *newstr) {
14 int i,j,k;
15 struct {
16 char *op;
17 char *str;
18 } ops[] = {
19 { "adc", "1 = 1 + 2"},
20 { "add", "1 = 1 + 2"},
21 { "and", "1 = 1 & 2"},
22 { "cpl", "1 = ~1"},
23 { "ex", "swap(1, 2)"},
24 { "in", "1 = [2]"},
25 { "jp", "goto [1]"},
26 { "jp", "goto 1"},
27 { "jr", "goto +1"},
28 { "ld", "1 = 2"},
29 { "ldd", "1 = 2--"},
30 { "neg", "1 = -1"},
31 { "nop", ""},
32 { "or", "1 = 1 | 2"},
33 { "pop", "pop 1"},
34 { "push", "push 1"},
35 { "rr", "1 = 1 << 2"},
36 { "sbc", "1 = 1 - 2"},
37 { "sla", "1 = 1 << 2"},
38 { "sra", "1 = 1 >> 2"},
39 { "srl", "1 = 1 >> 2"},
40 { "sub", "1 = 1 - 2"},
41 { "xor", "1 = 1 ^ 2"},
42 { NULL }
43 };
44
45 for (i=0; ops[i].op != NULL; i++) {
46 if (!strcmp (ops[i].op, argv[0])) {
47 if (newstr != NULL) {
48 for (j=k=0;ops[i].str[j]!='\0';j++,k++) {
49 if (ops[i].str[j]>='1' && ops[i].str[j]<='9') {
50 const char *w = argv[ ops[i].str[j]-'0' ];
51 if (w != NULL) {
52 strcpy (newstr+k, w);
53 k += strlen(w)-1;
54 }
55 } else {
56 newstr[k] = ops[i].str[j];
57 }
58 }
59 newstr[k]='\0';
60 }
61 return true;
62 }
63 }
64
65 /* TODO: this is slow */
66 if (newstr != NULL) {
67 newstr[0] = '\0';
68 for (i=0; i<argc; i++) {
69 strcat (newstr, argv[i]);
70 strcat (newstr, (i == 0 || i== argc - 1)?" ":", ");
71 }
72 }
73
74 return false;
75 }
76
77 RParsePlugin r_parse_plugin_z80_pseudo = {
78 .name = "z80.pseudo",
79 .desc = "z80 pseudo syntax",
80 .init = NULL,
81 .fini = NULL,
82 .replace = replace,
83 };
84
85 #ifndef R2_PLUGIN_INCORE
86 R_API RLibStruct radare_plugin = {
87 .type = R_LIB_TYPE_PARSE,
88 .data = &r_parse_plugin_z80_pseudo,
89 .version = R2_VERSION
90 };
91 #endif
92