From f46c3663218b685d7d5ed83781daea1360595261 Mon Sep 17 00:00:00 2001
From: marshmellow42 <marshmellowrf@gmail.com>
Date: Sun, 7 Jun 2015 00:10:54 -0400
Subject: [PATCH 1/1] Fix reveng

---
 client/cmdcrc.c        | 72 ++++++++++++++++++++++++++----------------
 client/cmdcrc.h        |  1 -
 client/cmdmain.c       | 11 +++++--
 client/reveng/cli.c    | 12 ++++---
 client/reveng/config.h |  5 ++-
 client/reveng/getopt.c |  4 +--
 client/reveng/getopt.h |  2 +-
 7 files changed, 68 insertions(+), 39 deletions(-)

diff --git a/client/cmdcrc.c b/client/cmdcrc.c
index 22eed561..7d021965 100644
--- a/client/cmdcrc.c
+++ b/client/cmdcrc.c
@@ -10,43 +10,59 @@
 
 #include <stdio.h>
 #include <string.h>
+//#include <stdlib.h>
+//#include <ctype.h>
 #include "cmdmain.h"
-#include "cmdparser.h"
 #include "cmdcrc.h"
 #include "reveng/reveng.h"
-//#include "reveng/cli.h"
-static int CmdHelp(const char *Cmd);
+#include "ui.h"
+#include "util.h"
 
-int CmdCrcCalc(const char *Cmd)
-{
-	int argc = 0;
-	char Cmd2[CMD_BUFFER_SIZE] = {0x00};
-	char *argv[3];
+#define MAX_ARGS 20
 
-	for (int i = 0; i < 50; i++)
-		if (Cmd[i]==0x00) argc=i;
+int split(char *str, char *arr[MAX_ARGS]){
+    int beginIndex = 0;
+    int endIndex;
+    int maxWords = MAX_ARGS;
+    int wordCnt = 0;
 
-	memcpy(Cmd2, Cmd, argc);
-	argv[1]=Cmd2;
-	reveng_main(argc, argv);
-	return 0;
+    while(1){
+        while(isspace(str[beginIndex])){
+            ++beginIndex;
+        }
+        if(str[beginIndex] == '\0')
+            break;
+        endIndex = beginIndex;
+        while (str[endIndex] && !isspace(str[endIndex])){
+            ++endIndex;
+        }
+        int len = endIndex - beginIndex;
+        char *tmp = calloc(len + 1, sizeof(char));
+        memcpy(tmp, &str[beginIndex], len);
+        arr[wordCnt++] = tmp;
+        //PrintAndLog("cnt: %d, %s",wordCnt-1, arr[wordCnt-1]);
+        beginIndex = endIndex;
+        if (wordCnt == maxWords)
+            break;
+    }
+    return wordCnt;
 }
 
-static command_t CommandTable[] = 
-{
-	{"help",	CmdHelp,	1, "This help"},
-	{"calc",	CmdCrcCalc,	1, "{ Calculate CRC's }"},
-	{NULL, NULL, 0, NULL}
-};
-
 int CmdCrc(const char *Cmd)
 {
-  CmdsParse(CommandTable, Cmd);
-  return 0; 
-}
+	char name[] = {"reveng "};
+	char Cmd2[50 + 7];
+	memcpy(Cmd2, name, 7);
+	memcpy(Cmd2 + 7, Cmd, 50);
+	char *argv[MAX_ARGS];
+	int argc = split(Cmd2, argv);
+	//PrintAndLog("argc: %d, %s %s Cmd: %s",argc, argv[0], Cmd2, Cmd);
+	reveng_main(argc, argv);
+	for(int i = 0; i < argc; ++i){
+		//puts(arr[i]);
+		free(argv[i]);
+	}
 
-int CmdHelp(const char *Cmd)
-{
-  CmdsHelp(CommandTable);
-  return 0;
+	return 0;
 }
+
diff --git a/client/cmdcrc.h b/client/cmdcrc.h
index c37ba2b1..ea02f884 100644
--- a/client/cmdcrc.h
+++ b/client/cmdcrc.h
@@ -12,5 +12,4 @@
 #define CMDCRC_H__
 
 int CmdCrc(const char *Cmd);
-int CmdCrcCalc(const char *Cmd);
 #endif
diff --git a/client/cmdmain.c b/client/cmdmain.c
index 0cc578f6..e2a3358c 100644
--- a/client/cmdmain.c
+++ b/client/cmdmain.c
@@ -32,6 +32,7 @@ unsigned int current_command = CMD_UNKNOWN;
 
 static int CmdHelp(const char *Cmd);
 static int CmdQuit(const char *Cmd);
+static int CmdRev(const char *Cmd);
 
 //For storing command that are received from the device
 static UsbCommand cmdBuffer[CMD_BUFFER_SIZE];
@@ -43,12 +44,12 @@ static int cmd_tail;//Starts as 0
 static command_t CommandTable[] = 
 {
   {"help",  CmdHelp,  1, "This help. Use '<command> help' for details of a particular command."},
-  {"crc",   CmdCrc,   1, "Crc calculations from the software reveng1-30"},
   {"data",  CmdData,  1, "{ Plot window / data buffer manipulation... }"},
   {"hf",    CmdHF,    1, "{ High Frequency commands... }"},
   {"hw",    CmdHW,    1, "{ Hardware commands... }"},
   {"lf",    CmdLF,    1, "{ Low Frequency commands... }"},
-  {"script",CmdScript,1,"{ Scripting commands }"},
+  {"reveng",CmdRev,   1, "Crc calculations from the software reveng1-30"},
+  {"script",CmdScript,1, "{ Scripting commands }"},
   {"quit",  CmdQuit,  1, "Exit program"},
   {"exit",  CmdQuit,  1, "Exit program"},
   {NULL, NULL, 0, NULL}
@@ -69,6 +70,12 @@ int CmdQuit(const char *Cmd)
   exit(0);
   return 0;
 }
+
+int CmdRev(const char *Cmd)
+{
+  CmdCrc(Cmd);
+  return 0;
+}
 /**
  * @brief This method should be called when sending a new command to the pm3. In case any old
  *  responses from previous commands are stored in the buffer, a call to this method should clear them.
diff --git a/client/reveng/cli.c b/client/reveng/cli.c
index a24445db..7747b9af 100644
--- a/client/reveng/cli.c
+++ b/client/reveng/cli.c
@@ -89,7 +89,7 @@ int reveng_main(int argc, char *argv[]) {
 	model_t pset = model, *candmods, *mptr;
 	char *string;
 
-	//myname = argv[0];
+	myname = argv[0];
 
 	/* stdin must be binary */
 #ifdef _WIN32
@@ -97,7 +97,9 @@ int reveng_main(int argc, char *argv[]) {
 #endif /* _WIN32 */
 
 	SETBMP();
-
+	
+	pos=0;
+	optind=1;
 	do {
 		c=getopt(argc, argv, "?A:BDFLMP:SVXa:bcdefhi:k:lm:p:q:rstuvw:x:yz");
 		switch(c) {
@@ -487,7 +489,8 @@ void
 uerror(const char *msg) {
 	/* Callback function to report fatal errors */
 	fprintf(stderr, "%s: %s\n", myname, msg);
-	exit(EXIT_FAILURE);
+	return;
+	//exit(EXIT_FAILURE);
 }
 
 void
@@ -545,7 +548,8 @@ oread(const char *name) {
 		return(stdin);
 	if(!(handle = fopen(name, "rb"))) {
 		fprintf(stderr, "%s: cannot open '%s' for reading\n", myname, name);
-		exit(EXIT_FAILURE);
+		return 0;
+		//exit(EXIT_FAILURE);
 	}
 	return(handle);
 }
diff --git a/client/reveng/config.h b/client/reveng/config.h
index 88957e8c..084c79ab 100644
--- a/client/reveng/config.h
+++ b/client/reveng/config.h
@@ -61,7 +61,10 @@
  * specific.
  */
 
-/* #define PRESETS  1 */
+#ifdef _WIN32
+ #define PRESETS  1 //
+#endif
+
 
 /* Macros defining the size of a bmp_t.
  * Their values only matter if PRESETS and/or BMPMACRO are defined, in
diff --git a/client/reveng/getopt.c b/client/reveng/getopt.c
index 55d8e8c5..abe99634 100644
--- a/client/reveng/getopt.c
+++ b/client/reveng/getopt.c
@@ -30,10 +30,10 @@
 
 char *optarg;
 int optind = 1, opterr, optopt;
-
+int pos = 0;
 int getopt(int argc, char *argv[], const char *optstring)
 {
-    static int pos = 0;
+    //static int pos = 0;
     char *str;
     
     if (pos == 0) {
diff --git a/client/reveng/getopt.h b/client/reveng/getopt.h
index e9a83540..bf66d9b1 100644
--- a/client/reveng/getopt.h
+++ b/client/reveng/getopt.h
@@ -21,5 +21,5 @@
 */
 
 extern char *optarg;
-extern int optind, opterr, optopt;
+extern int optind, opterr, optopt, pos;
 int getopt(int argc, char *argv[], const char *optstring);
-- 
2.39.5