Allow // in device and output host params

This commit is contained in:
Christian W. Zuckschwerdt 2019-03-20 10:14:45 +00:00
parent 9ae2db7e38
commit b896da1f11
5 changed files with 78 additions and 65 deletions

View file

@ -39,18 +39,6 @@
#endif
#endif
/*
* The only place '<strings.h>' is currenly needed is in 'src/devices/flex.c'.
* But it's cleaner to keep such trivia here.
*/
#ifdef _MSC_VER
#include <string.h>
#define strcasecmp(s1,s2) _stricmp(s1,s2)
#define strncasecmp(s1,s2,n) _strnicmp(s1,s2,n)
#else
#include <strings.h>
#endif
typedef enum {
DATA_DATA, /**< pointer to data is stored */
DATA_INT, /**< pointer to integer is stored */

View file

@ -14,6 +14,15 @@
#include <stdint.h>
// makes strcasecmp() and strncasecmp() available when including optparse.h
#ifdef _MSC_VER
#include <string.h>
#define strcasecmp(s1,s2) _stricmp(s1,s2)
#define strncasecmp(s1,s2,n) _strnicmp(s1,s2,n)
#else
#include <strings.h>
#endif
/// Convert string to bool with fallback default.
/// Parses "true", "yes", "on", "enable" (not case-sensitive) to 1, atoi() otherwise.
int atobv(char *arg, int def);
@ -21,9 +30,12 @@ int atobv(char *arg, int def);
/// Get the next colon separated arg, NULL otherwise.
char *arg_param(char *arg);
/// Parse parm string to host and port.
/// E.g. ":514", "localhost", "[::1]", "127.0.0.1:514", "[::1]:514"
void hostport_param(char *param, char **host, char **port);
/// Parse param string to host and port.
/// E.g. ":514", "localhost", "[::1]", "127.0.0.1:514", "[::1]:514",
/// also "//localhost", "//localhost:514", "//:514".
/// Host or port are terminated at a comma, if found.
/// @return the remaining options
char *hostport_param(char *param, char **host, char **port);
/// Convert a string to an unsigned integer, uses strtod() and accepts
/// metric suffixes of 'k', 'M', and 'G' (also 'K', 'm', and 'g').
@ -63,4 +75,16 @@ char *asepc(char **stringp, char delim);
/// @return the original value of *stringp (the keyword found)
char *getkwargs(char **s, char **key, char **val);
/// Trim left and right whitespace in string.
///
/// @param[in,out] str
/// @return the trimmed value of str
char *trim_ws(char *str);
/// Remove all whitespace from string.
///
/// @param[in,out] str
/// @return the stripped value of str
char *remove_ws(char *str);
#endif /* INCLUDE_OPTPARSE_H_ */

View file

@ -419,40 +419,6 @@ static void parse_getter(const char *arg, struct flex_get *getter)
*/
}
static char *strip_ws(char *str)
{
if (!str)
return str;
while (*str == ' ' || *str == '\t' || *str == '\r' || *str == '\n')
++str;
char *e = str; // end pointer (last non ws)
char *p = str; // scanning pointer
while (*p) {
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
++p;
if (*p)
e = p++;
}
*++e = '\0';
return str;
}
static char *remove_ws(char *str)
{
if (!str)
return str;
char *d = str; // dst pointer
char *s = str; // src pointer
while (*s) {
while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
++s;
if (*s)
*d++ = *s++;
}
*d++ = '\0';
return str;
}
r_device *flex_create_device(char *spec)
{
if (!spec || !*spec || *spec == '?' || !strncasecmp(spec, "help", strlen(spec))) {
@ -472,7 +438,7 @@ r_device *flex_create_device(char *spec)
*args++ = '\0';
}
c = strip_ws(strtok(spec, ":"));
c = trim_ws(strtok(spec, ":"));
if (c == NULL) {
fprintf(stderr, "Bad flex spec, missing name!\n");
usage();
@ -558,7 +524,7 @@ r_device *flex_create_device(char *spec)
char *key, *val;
while (getkwargs(&args, &key, &val)) {
key = remove_ws(key);
val = strip_ws(val);
val = trim_ws(val);
if (!key || !*key)
continue;
else if (!strcasecmp(key, "m") || !strcasecmp(key, "modulation"))

View file

@ -15,14 +15,6 @@
#include <limits.h>
#include <string.h>
#ifdef _MSC_VER
#include <string.h>
#define strcasecmp(s1,s2) _stricmp(s1,s2)
#define strncasecmp(s1,s2,n) _strnicmp(s1,s2,n)
#else
#include <strings.h>
#endif
int atobv(char *arg, int def)
{
if (!arg)
@ -41,9 +33,12 @@ char *arg_param(char *arg)
return p;
}
void hostport_param(char *param, char **host, char **port)
char *hostport_param(char *param, char **host, char **port)
{
if (param && *param) {
if (param[0] == '/' && param[1] == '/') {
param += 2;
}
if (*param != ':') {
*host = param;
if (*param == '[') {
@ -58,12 +53,18 @@ void hostport_param(char *param, char **host, char **port)
}
}
}
param = strchr(param, ':');
if (param) {
*param++ = '\0';
*port = param;
char *colon = strchr(param, ':');
char *comma = strchr(param, ',');
if (colon && (!comma || colon < comma)) {
*colon++ = '\0';
*port = colon;
}
if (comma) {
*comma++ = '\0';
return comma;
}
}
return NULL;
}
uint32_t atouint32_metric(const char *str, const char *error_hint)
@ -201,6 +202,40 @@ char *getkwargs(char **s, char **key, char **val)
return k;
}
char *trim_ws(char *str)
{
if (!str)
return str;
while (*str == ' ' || *str == '\t' || *str == '\r' || *str == '\n')
++str;
char *e = str; // end pointer (last non ws)
char *p = str; // scanning pointer
while (*p) {
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
++p;
if (*p)
e = p++;
}
*++e = '\0';
return str;
}
char *remove_ws(char *str)
{
if (!str)
return str;
char *d = str; // dst pointer
char *s = str; // src pointer
while (*s) {
while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
++s;
if (*s)
*d++ = *s++;
}
*d++ = '\0';
return str;
}
// Unit testing
#ifdef _TEST
#define ASSERT_EQUALS(a,b) if ((a) == (b)) { ++passed; } else { ++failed; fprintf(stderr, "FAIL: %d <> %d\n", (a), (b)); }

View file

@ -220,7 +220,7 @@ static void help_device(void)
"[-d \"\" Open default SoapySDR device\n"
"[-d driver=rtlsdr Open e.g. specific SoapySDR device\n"
"\tTo set gain for SoapySDR use -g ELEM=val,ELEM=val,... e.g. -g LNA=20,TIA=8,PGA=2 (for LimeSDR).\n"
"[-d rtl_tcp[:host[:port]] (default: localhost:1234)\n"
"[-d rtl_tcp[:[//]host[:port]] (default: localhost:1234)\n"
"\tSpecify host/port to connect to with e.g. -d rtl_tcp:127.0.0.1:1234\n");
exit(0);
}