From 417cb98d2eed6d756ac3683cb9f09b9ed1053d4e Mon Sep 17 00:00:00 2001 From: Nate Bargmann Date: Mon, 13 Feb 2012 15:22:02 -0600 Subject: [PATCH] Tidy up rigctl --list output Implemented hash table to temporarily store and sort the rig models by ID to print the --list by model numbers. Hash is implemented using uthash.h, see http://uthash.sourceforge.net/ Suppressed rig backend register output by setting rig_debug_level to 0 for list output. Removed riglist definition of RPC backend. --- include/hamlib/riglist.h | 12 ------ tests/rigctl.c | 39 +++++++++--------- tests/rigctl_parse.c | 86 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 101 insertions(+), 36 deletions(-) diff --git a/include/hamlib/riglist.h b/include/hamlib/riglist.h index 2f74e6056..fb63af57d 100644 --- a/include/hamlib/riglist.h +++ b/include/hamlib/riglist.h @@ -408,18 +408,6 @@ #define RIG_MODEL_505DSP RIG_MAKE_MODEL(RIG_KACHINA, 1) -/*! \def RIG_MODEL_RPC - * \brief A macro that returns the model number of the RPC Network pseudo-backend. - * - * The RPC backend can be used to connect and send commands to a rig server, - * \c rpc.rigd, running on a remote machine. Using this client/server scheme, - * several clients can control and monitor the same rig hardware. - * \deprecated - */ -#define RIG_RPC 19 -#define RIG_BACKEND_RPC "rpcrig" -#define RIG_MODEL_RPC RIG_MAKE_MODEL(RIG_RPC, 1) - /* * Gnuradio backend */ diff --git a/tests/rigctl.c b/tests/rigctl.c index 82d314a9b..9cb885b39 100644 --- a/tests/rigctl.c +++ b/tests/rigctl.c @@ -124,36 +124,36 @@ int main (int argc, char *argv[]) exit(0); case 'm': if (!optarg) { - usage(); /* wrong arg count */ - exit(1); + usage(); /* wrong arg count */ + exit(1); } my_model = atoi(optarg); break; case 'r': if (!optarg) { - usage(); /* wrong arg count */ - exit(1); + usage(); /* wrong arg count */ + exit(1); } rig_file = optarg; break; case 'p': if (!optarg) { - usage(); /* wrong arg count */ - exit(1); + usage(); /* wrong arg count */ + exit(1); } ptt_file = optarg; break; case 'd': if (!optarg) { - usage(); /* wrong arg count */ - exit(1); + usage(); /* wrong arg count */ + exit(1); } dcd_file = optarg; break; case 'P': if (!optarg) { - usage(); /* wrong arg count */ - exit(1); + usage(); /* wrong arg count */ + exit(1); } if (!strcmp(optarg, "RIG")) ptt_type = RIG_PTT_RIG; @@ -172,8 +172,8 @@ int main (int argc, char *argv[]) break; case 'D': if (!optarg) { - usage(); /* wrong arg count */ - exit(1); + usage(); /* wrong arg count */ + exit(1); } if (!strcmp(optarg, "RIG")) dcd_type = RIG_DCD_RIG; @@ -194,8 +194,8 @@ int main (int argc, char *argv[]) break; case 'c': if (!optarg) { - usage(); /* wrong arg count */ - exit(1); + usage(); /* wrong arg count */ + exit(1); } civaddr = optarg; break; @@ -211,18 +211,18 @@ int main (int argc, char *argv[]) break; case 's': if (!optarg) { - usage(); /* wrong arg count */ - exit(1); + usage(); /* wrong arg count */ + exit(1); } serial_rate = atoi(optarg); break; case 'C': if (!optarg) { - usage(); /* wrong arg count */ - exit(1); + usage(); /* wrong arg count */ + exit(1); } if (*conf_parms != '\0') - strcat(conf_parms, ","); + strcat(conf_parms, ","); strncat(conf_parms, optarg, MAXCONFLEN-strlen(conf_parms)); break; case 'o': @@ -235,6 +235,7 @@ int main (int argc, char *argv[]) show_conf++; break; case 'l': + rig_set_debug(verbose); list_models(); exit(0); case 'u': diff --git a/tests/rigctl_parse.c b/tests/rigctl_parse.c index 90e7d4642..3cc65550e 100644 --- a/tests/rigctl_parse.c +++ b/tests/rigctl_parse.c @@ -45,6 +45,9 @@ #include "rigctl_parse.h" +/* Hash table implementation See: http://uthash.sourceforge.net/ */ +#include "uthash.h" + #ifdef HAVE_PTHREAD #include @@ -246,6 +249,64 @@ static struct test_table *find_cmd_entry(int cmd) return &test_list[i]; } + +/* Structure for hash table provided by uthash.h + * + * Structure and hash funtions patterned after/copied from example.c + * distributed with the uthash package. See: http://uthash.sourceforge.net/ + */ +struct mod_lst +{ + int id; /* caps->rig_model This is the hash key */ + char mfg_name[32]; /* caps->mfg_name */ + char model_name[32]; /* caps->model_name */ + char version[32]; /* caps->version */ + char status[32]; /* caps->status */ + UT_hash_handle hh; /* makes this structure hashable */ +}; + +/* Hash declaration. Must be initialized to NULL */ +struct mod_lst *models = NULL; + +/* Add model information to the hash */ +void hash_add_model(int id, const char *mfg_name, const char *model_name, + const char *version, const char *status) +{ + struct mod_lst *s; + + s = (struct mod_lst*)malloc(sizeof(struct mod_lst)); + + s->id = id; + snprintf(s->mfg_name, sizeof(s->mfg_name), "%s", mfg_name); + snprintf(s->model_name, sizeof(s->model_name), "%s", model_name); + snprintf(s->version, sizeof(s->version), "%s", version); + snprintf(s->status, sizeof(s->status), "%s", status); + + HASH_ADD_INT(models, id, s); /* id: name of key field */ +} + +/* Hash sorting functions */ +int hash_model_id_sort(struct mod_lst *a, struct mod_lst *b) +{ + return (a->id - b->id); +} + +void hash_sort_by_model_id() +{ + HASH_SORT(models, hash_model_id_sort); +} + +/* Delete hash */ +void hash_delete_all() { + struct mod_lst *current_model, *tmp; + + HASH_ITER(hh, models, current_model, tmp) { + HASH_DEL(models, current_model); /* delete it (models advances to next) */ + free(current_model); /* free it */ + } +} + + /* * TODO: use Lex? */ @@ -632,25 +693,40 @@ int print_conf_list(const struct confparams *cfp, rig_ptr_t data) return 1; /* !=0, we want them all ! */ } -static int print_model_list(const struct rig_caps *caps, void *data) +static int hash_model_list(const struct rig_caps *caps, void *data) { - printf("%d\t%-23s%-24s%-12s%s\n", caps->rig_model, caps->mfg_name, - caps->model_name, caps->version, rig_strstatus(caps->status)); + + hash_add_model(caps->rig_model, caps->mfg_name, caps->model_name, + caps->version, rig_strstatus(caps->status)); return 1; /* !=0, we want them all ! */ } +void print_model_list() +{ + struct mod_lst *s; + + for (s = models; s != NULL; s = (struct mod_lst *)(s->hh.next)) { + printf("%6d %-23s%-24s%-16s%s\n", s->id, s->mfg_name, + s->model_name, s->version, s->status); + } +} + void list_models() { int status; rig_load_all_backends(); - printf("Rig#\tMfgr Model Vers. Status\n"); - status = rig_list_foreach(print_model_list, NULL); + printf(" Rig # Mfg Model Version Status\n"); + status = rig_list_foreach(hash_model_list, NULL); if (status != RIG_OK ) { printf("rig_list_foreach: error = %s \n", rigerror(status)); exit(2); } + + hash_sort_by_model_id(); + print_model_list(); + hash_delete_all(); }