Commit f4afea25 authored by Lucas Russo's avatar Lucas Russo

examples/acq.c: switch to getopt instead of custom parsing

parent 63b2299c
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
* * a client and the FPGA device * * a client and the FPGA device
* */ * */
#include <getopt.h>
#include <czmq.h> #include <czmq.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
...@@ -75,18 +76,43 @@ void print_data (uint32_t chan, uint32_t *data, uint32_t size, file_fmt_e file_f ...@@ -75,18 +76,43 @@ void print_data (uint32_t chan, uint32_t *data, uint32_t size, file_fmt_e file_f
} }
} }
static struct option long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"brokerendp", required_argument, NULL, 'b'},
{"verbose", no_argument, NULL, 'v'},
{"bpmnumber", required_argument, NULL, 's'},
{"boardslot", required_argument, NULL, 'o'},
{"channumber", required_argument, NULL, 'c'},
{"numsamples", required_argument, NULL, 'n'},
{"filefmt", required_argument, NULL, 'f'},
{NULL, 0, NULL, 0}
};
static const char* shortopt = "hb:vo:s:c:n:f:";
void print_help (char *program_name) void print_help (char *program_name)
{ {
printf( "Usage: %s [options]\n" fprintf (stdout, "EBPM Acquisition Utility\n"
"\t-h This help message\n" "Usage: %s [options]\n"
"\t-v Verbose output\n" "\n"
"\t-b <broker_endpoint> Broker endpoint\n" " -h --help Display this usage information\n"
"\t-s <num_samples_str> Number of samples\n" " -b --brokerendp <Broker endpoint> Broker endpoint\n"
"\t-board <AMC board = [0|1|2|3|4|5]>\n" " -v --verbose Verbose output\n"
"\t-bpm <BPM number = [0|1]>\n" " -o --boardslot <Board slot number = [1-12]> \n"
"\t-ch <chan_str> Acquisition channel\n" " -s --bpmnumber <BPM number = [0|1]> BPM number\n"
"\t-filefmt <File format = [0 = {text} |1 = {binary}]> File Format\n" " Board slot number\n"
, program_name); " -c --channumber <Channel> Channel number\n"
" <Channel> must be one of the following:\n"
" 0 -> ADC; 1 -> ADC_SWAP; 2 -> Mixer IQ120; 3 -> Mixer IQ340;\n"
" 4 -> TBT Decim IQ120; 5 -> TBT Decim IQ340; 6 -> TBT Amp;\n"
" 7 -> TBT Phase; 8 -> TBT Pos; 9 -> FOFB Decim IQ120;\n"
" 10 -> FOFB Decim IQ340; 11 -> FOFB Amp; 12 -> FOFB Pha;\n"
" 13 -> FOFB Pos; 14 -> Monit Amp; 15 -> Monit Pha; 16 -> Monit Pos]\n"
" -n --numsamples <Number of samples> Number of samples\n"
" -f --filefmt <Output format = [0 = text | 1=binary]>\n"
" Output format\n",
program_name);
} }
int main (int argc, char *argv []) int main (int argc, char *argv [])
...@@ -98,55 +124,62 @@ int main (int argc, char *argv []) ...@@ -98,55 +124,62 @@ int main (int argc, char *argv [])
char *bpm_number_str = NULL; char *bpm_number_str = NULL;
char *chan_str = NULL; char *chan_str = NULL;
char *file_fmt_str = NULL; char *file_fmt_str = NULL;
char **str_p = NULL; int opt;
while ((opt = getopt_long (argc, argv, shortopt, long_options, NULL)) != -1) {
/* Get the user selected options */
switch (opt) {
/* Display Help */
case 'h':
print_help (argv [0]);
exit (1);
break;
if (argc < 3) { case 'b':
print_help (argv[0]); broker_endp = strdup (optarg);
exit (1); break;
}
case 'v':
verbose = 1;
break;
/* FIXME: This is rather buggy! */ case 'o':
/* Simple handling of command-line options. This should be done board_number_str = strdup (optarg);
* * with getopt, for instance*/ break;
int i;
for (i = 1; i < argc; i++) case 's':
{ bpm_number_str = strdup (optarg);
if (streq(argv[i], "-v")) { break;
verbose = 1;
} case 'c':
else if (streq(argv[i], "-h")) chan_str = strdup (optarg);
{ break;
print_help (argv [0]);
exit (1); case 'n':
} num_samples_str = strdup (optarg);
else if (streq (argv[i], "-b")) { break;
str_p = &broker_endp;
} case 'f':
else if (streq (argv[i], "-s")) { /* s: samples */ file_fmt_str = strdup (optarg);
str_p = &num_samples_str; break;
}
else if (streq (argv[i], "-ch")) { /* ch: channel */ case '?':
str_p = &chan_str; fprintf (stderr, "[client:acq] Option not recognized or missing argument\n");
} print_help (argv [0]);
else if (streq (argv[i], "-board")) { /* board_number: board number */ exit (1);
str_p = &board_number_str; break;
}
else if (streq(argv[i], "-bpm")) default:
{ fprintf (stderr, "[client:acq] Could not parse options\n");
str_p = &bpm_number_str; print_help (argv [0]);
} exit (1);
else if (streq(argv[i], "-filefmt")) }
{
str_p = &file_fmt_str;
}
/* Fallout for options with parameters */
else {
*str_p = strdup (argv[i]);
}
} }
/* Set default broker address */ /* Set default broker address */
if (broker_endp == NULL) { if (broker_endp == NULL) {
fprintf (stderr, "[client:acq]: Setting default broker endpoint: %s\n",
"ipc://"DFLT_BIND_FOLDER);
broker_endp = strdup ("ipc://"DFLT_BIND_FOLDER); broker_endp = strdup ("ipc://"DFLT_BIND_FOLDER);
} }
...@@ -204,7 +237,7 @@ int main (int argc, char *argv []) ...@@ -204,7 +237,7 @@ int main (int argc, char *argv [])
/* Set default bpm number */ /* Set default bpm number */
uint32_t bpm_number; uint32_t bpm_number;
if (bpm_number_str == NULL) { if (bpm_number_str == NULL) {
fprintf (stderr, "[client:leds]: Setting default value to BPM number: %u\n", fprintf (stderr, "[client:acq]: Setting default value to BPM number: %u\n",
DFLT_BPM_NUMBER); DFLT_BPM_NUMBER);
bpm_number = DFLT_BPM_NUMBER; bpm_number = DFLT_BPM_NUMBER;
} }
...@@ -212,7 +245,7 @@ int main (int argc, char *argv []) ...@@ -212,7 +245,7 @@ int main (int argc, char *argv [])
bpm_number = strtoul (bpm_number_str, NULL, 10); bpm_number = strtoul (bpm_number_str, NULL, 10);
if (bpm_number > MAX_BPM_NUMBER) { if (bpm_number > MAX_BPM_NUMBER) {
fprintf (stderr, "[client:leds]: BPM number too big! Defaulting to: %u\n", fprintf (stderr, "[client:acq]: BPM number too big! Defaulting to: %u\n",
MAX_BPM_NUMBER); MAX_BPM_NUMBER);
bpm_number = MAX_BPM_NUMBER; bpm_number = MAX_BPM_NUMBER;
} }
...@@ -284,23 +317,17 @@ err_set_file_mode: ...@@ -284,23 +317,17 @@ err_set_file_mode:
err_bpm_get_curve: err_bpm_get_curve:
err_bpm_set_acq_trig: err_bpm_set_acq_trig:
err_bpm_client_new: err_bpm_client_new:
str_p = &file_fmt_str; free (file_fmt_str);
free (*str_p);
file_fmt_str = NULL; file_fmt_str = NULL;
str_p = &chan_str; free (chan_str);
free (*str_p);
chan_str = NULL; chan_str = NULL;
str_p = &board_number_str; free (board_number_str);
free (*str_p);
board_number_str = NULL; board_number_str = NULL;
str_p = &bpm_number_str; free (bpm_number_str);
free (*str_p);
bpm_number_str = NULL; bpm_number_str = NULL;
str_p = &num_samples_str; free (num_samples_str);
free (*str_p);
num_samples_str = NULL; num_samples_str = NULL;
str_p = &broker_endp; free (broker_endp);
free (*str_p);
broker_endp = NULL; broker_endp = NULL;
bpm_client_destroy (&bpm_client); bpm_client_destroy (&bpm_client);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment