Merge pull request #9 from doegox/addhelpm

Merged Addhelp -h / Markdown help -m, it now does not crash on windows (http://www.proxmark.org/forum/viewtopic.php?pid=10461#p10461)
This commit is contained in:
Martin Holst Swende 2014-03-28 12:27:38 +01:00
commit 046f3dfa7c
8 changed files with 120 additions and 10 deletions

View file

@ -851,7 +851,7 @@ static command_t CommandTable[] =
{
{"help", CmdHelp, 1, "This help"},
{"amp", CmdAmp, 1, "Amplify peaks"},
{"askdemod", Cmdaskdemod, 1, "<0|1> -- Attempt to demodulate simple ASK tags"},
{"askdemod", Cmdaskdemod, 1, "<0 or 1> -- Attempt to demodulate simple ASK tags"},
{"autocorr", CmdAutoCorr, 1, "<window length> -- Autocorrelation over window"},
{"bitsamples", CmdBitsamples, 0, "Get raw samples as bitstring"},
{"bitstream", CmdBitstream, 1, "[clock rate] -- Convert waveform into a bitstream"},

View file

@ -11,6 +11,8 @@
#ifndef CMDDATA_H__
#define CMDDATA_H__
command_t * CmdDataCommands();
int CmdData(const char *Cmd);
int CmdAmp(const char *Cmd);

View file

@ -536,7 +536,7 @@ static command_t CommandTable[] =
{"io", CmdLFIO, 1, "{ ioProx tags... }"},
{"indalademod", CmdIndalaDemod, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
{"indalaclone", CmdIndalaClone, 1, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
{"read", CmdLFRead, 0, "['h'|<divisor>] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134, alternatively: f=12MHz/(divisor+1))"},
{"read", CmdLFRead, 0, "['h' or <divisor>] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134, alternatively: f=12MHz/(divisor+1))"},
{"sim", CmdLFSim, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
{"simbidir", CmdLFSimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
{"simman", CmdLFSimManchester, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},

View file

@ -45,17 +45,21 @@ static int cmd_tail;//Starts as 0
static command_t CommandTable[] =
{
{"help", CmdHelp, 1, "This help. Use '<command> help' for details of the following commands:\n"},
{"help", CmdHelp, 1, "This help. Use '<command> help' for details of a particular command."},
{"data", CmdData, 1, "{ Plot window / data buffer manipulation... }"},
{"exit", CmdQuit, 1, "Exit program"},
{"hf", CmdHF, 1, "{ HF commands... }"},
{"hw", CmdHW, 1, "{ Hardware commands... }"},
{"lf", CmdLF, 1, "{ LF commands... }"},
{"quit", CmdQuit, 1, "Quit program"},
{"script", CmdScript, 1,"Run script"},
{"script", CmdScript, 1,"{ Scripting commands }"},
{"quit", CmdQuit, 1, "Exit program"},
{"exit", CmdQuit, 1, "Exit program"},
{NULL, NULL, 0, NULL}
};
command_t* getTopLevelCommandTable()
{
return CommandTable;
}
int CmdHelp(const char *Cmd)
{
CmdsHelp(CommandTable);

View file

@ -12,10 +12,11 @@
#define CMDMAIN_H__
#include "usb_cmd.h"
#include "cmdparser.h"
void UsbCommandReceived(UsbCommand *UC);
void CommandReceived(char *Cmd);
bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout);
bool WaitForResponse(uint32_t cmd, UsbCommand* response);
void clearCommandBuffer();
command_t* getTopLevelCommandTable();
#endif

View file

@ -9,6 +9,7 @@
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ui.h"
#include "cmdparser.h"
@ -22,13 +23,23 @@ void CmdsHelp(const command_t Commands[])
while (Commands[i].Name)
{
if (!offline || Commands[i].Offline)
PrintAndLog("%-16s %s", Commands[i].Name, Commands[i].Help);
PrintAndLog("%-16s %s", Commands[i].Name, Commands[i].Help);
++i;
}
}
void CmdsParse(const command_t Commands[], const char *Cmd)
{
if(strcmp( Cmd, "XX_internal_command_dump_XX") == 0)
{// Help dump children
dumpCommandsRecursive(Commands, 0);
return;
}
if(strcmp( Cmd, "XX_internal_command_dump_markdown_XX") == 0)
{// Markdown help dump children
dumpCommandsRecursive(Commands, 1);
return;
}
char cmd_name[32];
int len = 0;
memset(cmd_name, 0, 32);
@ -60,3 +71,62 @@ void CmdsParse(const command_t Commands[], const char *Cmd)
CmdsHelp(Commands);
}
}
char pparent[512] = {0};
char *parent = pparent;
void dumpCommandsRecursive(const command_t cmds[], int markdown)
{
if (cmds[0].Name == NULL)
return;
int i = 0;
int w_cmd=25;
int w_off=8;
// First, dump all single commands, which are not a container for
// other commands
if (markdown) {
printf("|%-*s|%-*s|%s\n",w_cmd,"command",w_off,"offline","description");
printf("|%-*s|%-*s|%s\n",w_cmd,"-------",w_off,"-------","-----------");
} else {
printf("%-*s|%-*s|%s\n",w_cmd,"command",w_off,"offline","description");
printf("%-*s|%-*s|%s\n",w_cmd,"-------",w_off,"-------","-----------");
}
while (cmds[i].Name)
{
char* cmd_offline = "N";
if(cmds[i].Help[0] == '{' && ++i) continue;
if ( cmds[i].Offline) cmd_offline = "Y";
if (markdown)
printf("|`%s%-*s`|%-*s|`%s`\n", parent, w_cmd-(int)strlen(parent)-2, cmds[i].Name, w_off, cmd_offline, cmds[i].Help);
else
printf("%s%-*s|%-*s|%s\n", parent, w_cmd-(int)strlen(parent), cmds[i].Name, w_off, cmd_offline, cmds[i].Help);
++i;
}
printf("\n\n");
i=0;
// Then, print the categories. These will go into subsections with their own tables
while (cmds[i].Name)
{
if(cmds[i].Help[0] != '{' && ++i) continue;
printf("### %s%s\n\n %s\n\n", parent, cmds[i].Name, cmds[i].Help);
char currentparent[512] = {0};
snprintf(currentparent, sizeof currentparent, "%s%s ", parent, cmds[i].Name);
char *old_parent = parent;
parent = currentparent;
// This is what causes the recursion, since commands Parse-implementation
// in turn calls the CmdsParse above.
if (markdown)
cmds[i].Parse("XX_internal_command_dump_markdown_XX");
else
cmds[i].Parse("XX_internal_command_dump_XX");
parent = old_parent;
++i;
}
}

View file

@ -25,5 +25,6 @@ typedef struct command_s
void CmdsHelp(const command_t Commands[]);
// Parse a command line
void CmdsParse(const command_t Commands[], const char *Cmd);
void dumpCommandsRecursive(const command_t cmds[], int markdown);
#endif

View file

@ -23,6 +23,8 @@
#include "uart.h"
#include "ui.h"
#include "sleep.h"
#include "cmdparser.h"
#include "cmdmain.h"
// a global mutex to prevent interlaced printing from different threads
pthread_mutex_t print_lock;
@ -31,7 +33,6 @@ static serial_port sp;
static UsbCommand txcmd;
volatile static bool txcmd_pending = false;
void SendCommand(UsbCommand *c) {
#if 0
printf("Sending %d bytes\n", sizeof(UsbCommand));
@ -206,15 +207,46 @@ static void *main_loop(void *targ) {
return NULL;
}
//static void dumpHelp(char *parent, ...)
//{
// printf("## %s\n\n", parent);
// CommandReceived(parent);
//
// printf("\n");
//}
static void dumpAllHelp(int markdown)
{
printf("\n%sProxmark3 command dump%s\n\n",markdown?"# ":"",markdown?"":"\n======================");
printf("Some commands are available only if a Proxmark is actually connected.%s\n",markdown?" ":"");
printf("Check column \"offline\" for their availability.\n");
printf("\n");
command_t *cmds = getTopLevelCommandTable();
dumpCommandsRecursive(cmds, markdown);
}
int main(int argc, char* argv[]) {
srand(time(0));
if (argc < 2) {
printf("syntax: %s <port>\n\n",argv[0]);
printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]);
printf("help: %s -h\n\n", argv[0]);
printf("\tDump all interactive help at once\n");
printf("markdown: %s -m\n\n", argv[0]);
printf("\tDump all interactive help at once in markdown syntax\n");
return 1;
}
if (strcmp(argv[1], "-h") == 0) {
printf("syntax: %s <port>\n\n",argv[0]);
printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]);
dumpAllHelp(0);
return 0;
}
if (strcmp(argv[1], "-m") == 0) {
dumpAllHelp(1);
return 0;
}
// Make sure to initialize
struct main_loop_arg marg = {
.usb_present = 0,