Rizin
unix-like reverse engineering framework and cli tools
main.c
Go to the documentation of this file.
1 /* MIT pancake <pancake@nopcode.org> (C) 2009-2020 */
2 
3 #include "../spp.h"
4 #include "../rz_api.h"
5 
6 extern struct Proc *procs[];
7 extern struct Proc *proc;
8 extern struct Arg *args;
9 
10 static void spp_help(char *argv0) {
11  size_t i;
12  char supported[128] = "";
13  for (i = 0; procs[i]; ++i ) {
14  if (i) strcat (supported, ",");
15  strcat (supported, procs[i]->name);
16  }
17  printf("Usage: %s [-othesv] [file] [...]\n", argv0);
18  printf( " -o [file] set output file (stdout)\n"
19  " -t [type] define processor type (%s)\n"
20  " -e [str] evaluate this string with the selected proc\n"
21  " -s [str] show this string before anything\n"
22  " -l list all built-in preprocessors\n"
23  " -L list keywords registered by the processor\n"
24  " -n do not read from stdin\n"
25  " -v show version information\n", supported);
26  if (proc) {
27  printf ("%s specific flags:\n", proc->name);
28  for(i = 0; args[i].flag; i++) {
29  printf (" %s %s\n", args[i].flag, args[i].desc);
30  }
31  }
32  exit(0);
33 }
34 
35 int main(int argc, char **argv) {
36  int dostdin = 1;
37  int i, j;
38  Output out;
39  out.fout = stdout;
40  char *arg;
41 
42  spp_proc_set (proc, argv[0], 0);
43 
44  if (argc < 2)
45  spp_io (stdin, &out);
46  else {
47  for(i = 1; i < argc; i++) {
48  /* check preprocessor args */
49  if (args)
50  for(j = 0; args[j].flag; j++) {
51  if (!memcmp (argv[i], args[j].flag, 2)) {
52  if (args[j].has_arg) {
53  GET_ARG (arg, argv, i);
54  args[j].callback (arg);
55  } else {
56  args[j].callback (NULL);
57  }
58  continue;
59  }
60  }
61 
62  /* TODO: Add these flags in Arg[] */
63  if (!memcmp (argv[i], "-o", 2)) {
64  GET_ARG (arg, argv, i);
65  if (arg != NULL) {
66  if (!strcmp (arg, "buff")) {
67  out.fout = NULL;
68  out.cout = rz_strbuf_new ("");
69  rz_strbuf_init (out.cout);
70  } else {
71  out.cout = NULL;
72  out.fout = fopen (arg, "w");
73  }
74  }
75  if (!out.cout && (arg == NULL || out.fout == NULL)) {
76  fprintf (stderr, "Cannot open output file\n");
77  exit (1);
78  }
79  } else
80  if (!memcmp (argv[i],"-t", 2)) {
81  GET_ARG (arg, argv, i);
82  spp_proc_set (proc, arg, 1);
83  } else
84  if (!strcmp (argv[i],"-v")) {
85  printf ("%s\n", VERSION);
86  exit (1);
87  } else
88  if (!strcmp (argv[i],"-h")) {
89  /* show help */
90  spp_help (argv[0]);
91  } else
92  if (!strcmp (argv[i],"-n")) {
93  dostdin = 0;
94  } else
95  if (!strcmp (argv[i],"-l")) {
96  spp_proc_list ();
97  exit (0);
98  } else
99  if (!strcmp (argv[i],"-L")) {
100  spp_proc_list_kw ();
101  exit (0);
102  } else
103  if (!strcmp (argv[i],"-s")) {
104  GET_ARG (arg, argv, i);
105  if (arg == NULL) arg = "";
106  fprintf (out.fout, "%s\n", arg);
107  } else
108  if (!strcmp (argv[i],"-e")) {
109  GET_ARG (arg, argv, i);
110  if (arg == NULL) {
111  arg = "";
112  }
113  spp_eval (arg, &out);
114  } else {
115  if (i == argc) {
116  fprintf (stderr, "No file specified.\n");
117  } else {
118  spp_file (argv[i], &out);
119  dostdin = 0;
120 
121  if (!out.fout) {
122  D printf ("%s\n", rz_strbuf_get (out.cout));
123  rz_strbuf_free (out.cout);
124  }
125  }
126  }
127  }
128  if (dostdin) {
129  spp_io (stdin, &out);
130  }
131  }
132 
133  if (proc->eof) {
134  proc->eof (&proc->state, &out, "");
135  }
136  if (out.fout) {
137  fclose (out.fout);
138  }
139 
140  return 0;
141 }
lzma_index ** i
Definition: index.h:629
static const char * arg(RzAnalysis *a, csh *handle, cs_insn *insn, char *buf, int n)
Definition: arm_esil32.c:136
const char * desc
Definition: bin_vsf.c:19
#define D
Definition: block.c:38
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define VERSION
Definition: config.h:54
#define NULL
Definition: cris-opc.c:27
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
int main(int argc, const char **argv)
Definition: main.c:340
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
RZ_API char * rz_strbuf_get(RzStrBuf *sb)
Definition: strbuf.c:321
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API void rz_strbuf_free(RzStrBuf *sb)
Definition: strbuf.c:358
RZ_API void rz_strbuf_init(RzStrBuf *sb)
Definition: strbuf.c:33
S_API void spp_eval(char *buf, Output *out)
Definition: spp.c:109
S_API void spp_proc_list()
Definition: spp.c:291
S_API void spp_proc_list_kw()
Definition: spp.c:284
S_API void spp_proc_set(SppProc *p, const char *arg, int fail)
Definition: spp.c:298
S_API int spp_file(const char *file, Output *out)
Definition: spp.c:272
S_API void spp_io(FILE *in, Output *out)
Definition: spp.c:224
#define GET_ARG(x, y, i)
Definition: spp.h:73
Definition: spp.h:121
int has_arg
Definition: spp.h:124
const char * flag
Definition: spp.h:122
Definition: spp.h:92
Definition: spp.h:128
SppState state
Definition: spp.h:141
const char * name
Definition: spp.h:129
Definition: z80asm.h:102
struct Proc * proc
static void spp_help(char *argv0)
Definition: main.c:10
struct Arg * args
Definition: mipsasm.c:18
struct Proc * procs[]
Definition: config.h:24