Rizin
unix-like reverse engineering framework and cli tools
cpp.h
Go to the documentation of this file.
1 /* CPP */
2 
3 static TAG_CALLBACK(cpp_default) {
4  out_printf (out, "DEFAULT: (%s)\n", buf);
5  return 0;
6 }
7 
8 static TAG_CALLBACK(cpp_error) {
9  out_printf (out, "\n");
10  if (state->echo[state->ifl] && buf) {
11  out_printf (out, "ERROR: %s (line=%d)\n", buf, state->lineno);
12  return -1;
13  }
14  return 0;
15 }
16 
17 static TAG_CALLBACK(cpp_warning) {
18  out_printf (out,"\n");
19  if (state->echo[state->ifl] && buf != NULL) {
20  out_printf (out, "WARNING: line %d: %s\n", state->lineno, buf);
21  }
22  return 0;
23 }
24 
25 static TAG_CALLBACK(cpp_if) {
26  char *var = getenv (buf + ((*buf == '!') ? 1 : 0));
27  if (var && *var == '1') {
28  state->echo[state->ifl + 1] = 1;
29  } else {
30  state->echo[state->ifl + 1] = 0;
31  }
32  if (*buf == '!') {
33  state->echo[state->ifl + 1] = !!!state->echo[state->ifl + 1];
34  }
35  return 1;
36 }
37 
38 static TAG_CALLBACK(cpp_ifdef) {
39  char *var = getenv (buf);
40  state->echo[state->ifl + 1] = var? 1: 0;
41  return 1;
42 }
43 
44 static TAG_CALLBACK(cpp_else) {
45  state->echo[state->ifl] = state->echo[state->ifl]? 0: 1;
46  return 0;
47 }
48 
49 static TAG_CALLBACK(cpp_ifndef) {
50  cpp_ifdef (state, out, buf);
51  cpp_else (state, out, buf);
52  return 1;
53 }
54 
55 static struct cpp_macro_t {
56  char *name;
57  char *args;
58  char *body;
59 } cpp_macros[10];
60 
61 static int cpp_macros_n = 0;
62 
63 static void cpp_macro_add(char *name, char *args, char *body) {
64  char *ptr;
67  ptr = strchr (name, '(');
68  if (ptr) {
69  ptr[1] = '\0';
70  }
72  cpp_macros_n++;
73 }
74 
75 static PUT_CALLBACK(cpp_fputs) {
76  int i;
77  for (i = 0; i < cpp_macros_n; i++) {
78  if (strstr(buf, cpp_macros[i].name)) {
79  fprintf (stderr, "MACRO (%s) HIT\n",
80  cpp_macros[i].name);
81  }
82  }
83  out_printf (out, "%s", buf);
84  return 0;
85 }
86 
87 static TAG_CALLBACK(cpp_define) {
88  char *eq = strchr (buf, ' ');
89  if (eq) {
90  char *ptr = eq + 1;
91  char *macro = strchr(buf, '(');
92  *eq = '\0';
93  if (macro) {
94  /*macro[0]='\0'; */
95  ptr = strchr (macro + 1, ')');
96  if (!ptr) {
97  fprintf(stderr, "Invalid syntax\n");
98  return 1;
99  }
100  ptr = ptr + 1;
101  fprintf(stderr, "REGISTER MACRO:\n");
102  fprintf(stderr, " name: %s\n", buf);
103  fprintf(stderr, " args: %s\n", macro);
104  fprintf(stderr, " body: %s\n", ptr+1);
105  cpp_macro_add(buf,macro,ptr+1);
106  /* TODO: Name is "BUF(". for funny strstr */
107  }
108  rz_sys_setenv (buf, ptr);
109  } else rz_sys_setenv (buf, "");
110  return 0;
111 }
112 
113 static TAG_CALLBACK(cpp_endif) {
114  return -1;
115 }
116 
117 static TAG_CALLBACK(cpp_include) {
118  if (state->echo[state->ifl]) {
119  spp_file (buf, out);
120  }
121  return 0;
122 }
123 
124 DLL_LOCAL struct Tag cpp_tags[] = {
125  { "ifdef", cpp_ifdef },
126  { "ifndef", cpp_ifndef },
127  { "endif", cpp_endif },
128  { "if", cpp_if },
129  { "else", cpp_else },
130  { "include", cpp_include },
131  { "define", cpp_define },
132  { "error", cpp_error },
133  { "warning", cpp_warning },
134  { NULL, cpp_default },
135  { NULL }
136 };
137 
138 /* arguments */
139 
140 static ARG_CALLBACK(cpp_arg_i) {
141  printf("INCLUDEDIR(%s)\n", arg);
142  return 0;
143 }
144 
145 static ARG_CALLBACK(cpp_arg_d) {
146  // TODO: handle rz_sys_setenv==-1
147  char *eq = strchr (arg, '=');
148  if (eq) {
149  *eq = '\0';
150  rz_sys_setenv (arg, eq + 1);
151  } else {
152  rz_sys_setenv (arg, "");
153  }
154  return 0;
155 }
156 
157 static struct Arg cpp_args[] = {
158  { "-I", "add include directory", 1, cpp_arg_i },
159  { "-D", "define value of variable", 1, cpp_arg_d },
160  { NULL }
161 };
162 
164  .name = "cpp",
165  .tags = (struct Tag **)cpp_tags,
166  .args = (struct Arg **)cpp_args,
167  .token = " ",
168  .eof = NULL,
169  .tag_pre = "#",
170  .tag_post = "\n",
171  .multiline = "\\\n",
172  .default_echo = 1,
173  .fputs = cpp_fputs,
174  .chop = 0,
175  .tag_begin = 1,
176 };
lzma_index ** i
Definition: index.h:629
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
static TAG_CALLBACK(cpp_default)
Definition: cpp.h:3
static struct cpp_macro_t cpp_macros[10]
static PUT_CALLBACK(cpp_fputs)
Definition: cpp.h:75
static int cpp_macros_n
Definition: cpp.h:61
DLL_LOCAL struct Proc cpp_proc
Definition: cpp.h:163
static struct Arg cpp_args[]
Definition: cpp.h:157
DLL_LOCAL struct Tag cpp_tags[]
Definition: cpp.h:124
static void cpp_macro_add(char *name, char *args, char *body)
Definition: cpp.h:63
static ARG_CALLBACK(cpp_arg_i)
Definition: cpp.h:140
#define NULL
Definition: cris-opc.c:27
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
voidpf void * buf
Definition: ioapi.h:138
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
int args
Definition: mipsasm.c:18
void out_printf(Output *out, char *str,...) __attribute__((format(printf
RZ_API int rz_sys_setenv(const char *key, const char *value)
Set an environment variable in the calling process.
Definition: sys.c:405
S_API int spp_file(const char *file, Output *out)
Definition: spp.c:272
#define DLL_LOCAL
Definition: spp.h:69
Definition: spp.h:121
Definition: spp.h:128
const char * name
Definition: spp.h:129
Definition: spp.h:116
char * body
Definition: cpp.h:58
char * args
Definition: cpp.h:57
char * name
Definition: cpp.h:56
Definition: z80asm.h:130
Definition: z80asm.h:102
Definition: dis.h:43
char * getenv()