Skip to content

Commit

Permalink
Merge branch 'dev/cmd/get/opts'
Browse files Browse the repository at this point in the history
  • Loading branch information
nickolasburr committed Jan 13, 2018
2 parents 25622f5 + c12a1e8 commit 76c971d
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 77 deletions.
24 changes: 21 additions & 3 deletions include/argv.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
#include "utils.h"

/**
* Commands
* ASCII character codes.
*/
#define ASCII_PERCENT 37

/**
* Commands
*/
#define NUM_CMDS 6

/**
Expand All @@ -31,6 +35,9 @@
* Command options
*/

/**
* Number of 'sbctl ls' options.
*/
#define NUM_LS_OPTS 5

/**
Expand All @@ -43,9 +50,19 @@
#define MASK_CMD_LIST_OPT_USB ((0xFF << 0x4))

/**
* ASCII character codes.
* Number of 'sbctl get' options.
*/
#define ASCII_PERCENT 37
#define NUM_GET_OPTS 1

/**
* Hex masks for 'sbctl get' options.
*/
#define MASK_CMD_GET_OPT_HELP ((0xFE << 0x0))

/**
* Example usage for 'sbctl get'.
*/
#define EXAMPLE_USAGE_CMD_GET "sbctl get %1"

typedef struct {
char *value;
Expand All @@ -63,6 +80,7 @@ typedef struct {
} Command_T;

static Command_T commands[NUM_CMDS];
static Option_T get_opts[NUM_GET_OPTS];
static Option_T ls_opts[NUM_LS_OPTS];

int ARGV_get_command_bitmask(const char *);
Expand Down
2 changes: 1 addition & 1 deletion include/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#ifndef SBCTL_FORMAT_H
#define SBCTL_FORMAT_H

#define COLUMN_LINE "---"
#define LIST_HEADER "-------------------------------------------------------------------------------------------------------------------------------------------\n" \
"| ### | Spec | Mode | Type | Bus | Address | Port | Power [mA] | Speed [Mbps] | Serial Number | Device ID | Vendor | Product Description |\n" \
"-------------------------------------------------------------------------------------------------------------------------------------------\n"
#define LIST_FOOTER "-------------------------------------------------------------------------------------------------------------------------------------------\n"
#define NOT_SPECIFIED "Not Specified"


#endif /* SBCTL_FORMAT_H */
5 changes: 5 additions & 0 deletions include/thun.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
#include "common.h"
#include "serial.h"

#define THUN_MODE_NAME "thun"
#define THUN_PORT_TYPE "port"
#define THUN_BRID_TYPE "bridge"
#define THUN_SWIT_TYPE "switch"

#define THUN_V1 0x1
#define THUN_V2 0x2
#define THUN_V3 0x3
Expand Down
77 changes: 71 additions & 6 deletions src/argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,19 @@ static Option_T ls_opts[] = {
};

/**
* Initialize command->options[index] structs.
* sbctl get options.
*/
static Option_T get_opts[] = {
{
"--help",
"-h",
"Show usage information.",
MASK_CMD_GET_OPT_HELP,
},
};

/**
* Initialize command->options pointer array.
*/
void ARGV_init (void) {
int index;
Expand All @@ -99,11 +111,15 @@ void ARGV_init (void) {
* sbctl ls
*/
case MASK_CMD_LIST:
/**
* Make command->options point to ls_opts array.
*/
command->options = &ls_opts;

break;
/**
* sbctl get
*/
case MASK_CMD_GET:
command->options = &get_opts;

break;
default:
break;
Expand Down Expand Up @@ -152,9 +168,19 @@ int ARGV_get_option_bitmask (const char *cmd, const char *value) {
* Length of command->options array.
*/
switch (ARGV_get_command_bitmask(cmd)) {
/**
* sbctl ls
*/
case MASK_CMD_LIST:
length = (sizeof(ls_opts) / sizeof(ls_opts[0]));

break;
/**
* sbctl get
*/
case MASK_CMD_GET:
length = (sizeof(get_opts) / sizeof(get_opts[0]));

break;
default:
length = 0;
Expand Down Expand Up @@ -185,9 +211,19 @@ int ARGV_get_option_bitmask (const char *cmd, const char *value) {
* Length of command->options array, based on command.
*/
switch (ARGV_get_command_bitmask(cmd)) {
/**
* sbctl ls
*/
case MASK_CMD_LIST:
length = (sizeof(ls_opts) / sizeof(ls_opts[0]));

break;
/**
* sbctl get
*/
case MASK_CMD_GET:
length = (sizeof(get_opts) / sizeof(get_opts[0]));

break;
default:
length = 0;
Expand Down Expand Up @@ -218,19 +254,32 @@ int ARGV_get_option_bitmask (const char *cmd, const char *value) {
* Print command-specific usage information.
*/
void ARGV_command_usage (const char *cmd) {
int index, length;
int index, length, bitmask;
char value[40];
Option_T *cmd_opts = NULL;
Option_T *option = NULL;

bitmask = ARGV_get_command_bitmask(cmd);

fprintf(stdout, "Usage: sbctl %s [OPTIONS]\n\n", cmd);
fprintf(stdout, "Options:\n\n");

switch (ARGV_get_command_bitmask(cmd)) {
switch (bitmask) {
/**
* sbctl ls
*/
case MASK_CMD_LIST:
cmd_opts = (Option_T *) &ls_opts;
length = (sizeof(ls_opts) / sizeof(ls_opts[0]));

break;
/**
* sbctl get
*/
case MASK_CMD_GET:
cmd_opts = (Option_T *) &get_opts;
length = (sizeof(get_opts) / sizeof(get_opts[0]));

break;
default:
return;
Expand All @@ -257,6 +306,22 @@ void ARGV_command_usage (const char *cmd) {
fprintf(stdout, "%4s%-s%s: %-24s\n", "", value, option->alias, option->desc);
}

/**
* Example usage details.
*/
switch (bitmask) {
/**
* sbctl get
*/
case MASK_CMD_GET:
fprintf(stdout, "\nExamples:\n\n");
fprintf(stdout, "%4s%-s\n", "", EXAMPLE_USAGE_CMD_GET);

break;
default:
break;
}

return;
}

Expand Down
Loading

0 comments on commit 76c971d

Please sign in to comment.