We parse tristates all the time, let's add an explicit parser for them.
return -EINVAL;
}
+int parse_tristate_full(const char *v, const char *third, int *ret) {
+ int r;
+
+ if (isempty(v) || streq_ptr(v, third)) { /* Empty string is always taken as the third/invalid/auto state */
+ if (ret)
+ *ret = -1;
+ } else {
+ r = parse_boolean(v);
+ if (r < 0)
+ return r;
+
+ if (ret)
+ *ret = r;
+ }
+
+ return 0;
+}
+
int parse_pid(const char *s, pid_t* ret_pid) {
unsigned long ul = 0;
pid_t pid;
typedef unsigned long loadavg_t;
int parse_boolean(const char *v) _pure_;
+int parse_tristate_full(const char *v, const char *third, int *ret);
+static inline int parse_tristate(const char *v, int *ret) {
+ return parse_tristate_full(v, NULL, ret);
+}
int parse_pid(const char *s, pid_t* ret_pid);
int parse_mode(const char *s, mode_t *ret);
int parse_ifindex(const char *s);
_cleanup_(macsec_transmit_association_free_or_set_invalidp) TransmitAssociation *a = NULL;
_cleanup_(macsec_receive_association_free_or_set_invalidp) ReceiveAssociation *b = NULL;
MACsec *s = userdata;
- int *dest;
- int r;
+ int *dest, r;
assert(filename);
assert(section);
dest = a ? &a->sa.activate : &b->sa.activate;
- if (isempty(rvalue))
- r = -1;
- else {
- r = parse_boolean(rvalue);
- if (r < 0) {
- log_syntax(unit, LOG_WARNING, filename, line, r,
- "Failed to parse activation mode of %s security association. "
- "Ignoring assignment: %s",
- streq(section, "MACsecTransmitAssociation") ? "transmit" : "receive",
- rvalue);
- return 0;
- }
+ r = parse_tristate(rvalue, dest);
+ if (r < 0) {
+ log_syntax(unit, LOG_WARNING, filename, line, r,
+ "Failed to parse activation mode of %s security association. "
+ "Ignoring assignment: %s",
+ streq(section, "MACsecTransmitAssociation") ? "transmit" : "receive",
+ rvalue);
+ return 0;
}
- *dest = r;
TAKE_PTR(a);
TAKE_PTR(b);
return 0;
}
- r = parse_boolean(rvalue);
+ r = parse_tristate(rvalue, &a->sa.use_for_encoding);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse %s= setting. Ignoring assignment: %s",
return 0;
}
- a->sa.use_for_encoding = r;
if (a->sa.use_for_encoding > 0)
a->sa.activate = true;
if (r < 0)
return log_oom();
- if (isempty(rvalue)) {
- n->onlink = -1;
- TAKE_PTR(n);
- return 0;
- }
-
- r = parse_boolean(rvalue);
+ r = parse_tristate(rvalue, &n->onlink);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
return 0;
}
- n->onlink = r;
-
TAKE_PTR(n);
return 0;
}
else
assert_not_reached();
- if (isempty(rvalue)) {
- *dest = -1;
- TAKE_PTR(qdisc);
- return 0;
- }
-
- r = parse_boolean(rvalue);
+ r = parse_tristate(rvalue, dest);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse '%s=', ignoring assignment: %s",
return 0;
}
- *dest = r;
TAKE_PTR(qdisc);
return 0;
}
cd = CODEL(qdisc);
- if (isempty(rvalue)) {
- cd->ecn = -1;
-
- qdisc = NULL;
- return 0;
- }
-
- r = parse_boolean(rvalue);
+ r = parse_tristate(rvalue, &cd->ecn);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse '%s=', ignoring assignment: %s",
return 0;
}
- cd->ecn = r;
- qdisc = NULL;
+ TAKE_PTR(qdisc);
return 0;
}
fqcd = FQ_CODEL(qdisc);
- if (isempty(rvalue)) {
- fqcd->ecn = -1;
-
- TAKE_PTR(qdisc);
- return 0;
- }
-
- r = parse_boolean(rvalue);
+ r = parse_tristate(rvalue, &fqcd->ecn);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse '%s=', ignoring assignment: %s",
return 0;
}
- fqcd->ecn = r;
TAKE_PTR(qdisc);
return 0;
fq = FQ(qdisc);
- if (isempty(rvalue)) {
- fq->pacing = -1;
-
- qdisc = NULL;
- return 0;
- }
-
- r = parse_boolean(rvalue);
+ r = parse_tristate(rvalue, &fq->pacing);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse '%s=', ignoring assignment: %s",
}
fq->pacing = r;
- qdisc = NULL;
+ TAKE_PTR(qdisc);
return 0;
}
gred = GRED(qdisc);
- if (isempty(rvalue)) {
- gred->grio = -1;
-
- TAKE_PTR(qdisc);
- return 0;
- }
-
- r = parse_boolean(rvalue);
+ r = parse_tristate(rvalue, &gred->grio);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse '%s=', ignoring assignment: %s",
return 0;
}
- gred->grio = r;
TAKE_PTR(qdisc);
return 0;
void *data,
void *userdata) {
- int k, *t = ASSERT_PTR(data);
+ int r, *t = ASSERT_PTR(data);
assert(filename);
assert(lvalue);
return 0;
}
- k = parse_boolean(rvalue);
- if (k < 0) {
- log_syntax(unit, LOG_WARNING, filename, line, k,
+ r = parse_tristate(rvalue, t);
+ if (r < 0) {
+ log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse boolean value for %s=, ignoring: %s", lvalue, rvalue);
return 0;
}
- *t = k;
return 0;
}
break;
case ARG_CHECK_INHIBITORS:
- if (streq(optarg, "auto"))
- arg_check_inhibitors = -1;
- else {
- r = parse_boolean(optarg);
- if (r < 0)
- return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
- arg_check_inhibitors = r;
- }
+ r = parse_tristate_full(optarg, "auto", &arg_check_inhibitors);
+ if (r < 0)
+ return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
break;
case ARG_PLAIN: