escape: define main through macro
This commit is contained in:
+30
-50
@@ -163,7 +163,7 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
static int run(int argc, char *argv[]) {
|
||||
char **i;
|
||||
int r;
|
||||
|
||||
@@ -172,7 +172,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
r = parse_argv(argc, argv);
|
||||
if (r <= 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
STRV_FOREACH(i, argv + optind) {
|
||||
_cleanup_free_ char *e = NULL;
|
||||
@@ -182,26 +182,20 @@ int main(int argc, char *argv[]) {
|
||||
case ACTION_ESCAPE:
|
||||
if (arg_path) {
|
||||
r = unit_name_path_escape(*i, &e);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to escape string: %m");
|
||||
goto finish;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to escape string: %m");
|
||||
} else {
|
||||
e = unit_name_escape(*i);
|
||||
if (!e) {
|
||||
r = log_oom();
|
||||
goto finish;
|
||||
}
|
||||
if (!e)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
if (arg_template) {
|
||||
char *x;
|
||||
|
||||
r = unit_name_replace_instance(arg_template, e, &x);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to replace instance: %m");
|
||||
goto finish;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to replace instance: %m");
|
||||
|
||||
free(e);
|
||||
e = x;
|
||||
@@ -209,10 +203,8 @@ int main(int argc, char *argv[]) {
|
||||
char *x;
|
||||
|
||||
x = strjoin(e, ".", arg_suffix);
|
||||
if (!x) {
|
||||
r = log_oom();
|
||||
goto finish;
|
||||
}
|
||||
if (!x)
|
||||
return log_oom();
|
||||
|
||||
free(e);
|
||||
e = x;
|
||||
@@ -227,51 +219,38 @@ int main(int argc, char *argv[]) {
|
||||
_cleanup_free_ char *template = NULL;
|
||||
|
||||
r = unit_name_to_instance(*i, &name);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to extract instance: %m");
|
||||
goto finish;
|
||||
}
|
||||
if (isempty(name)) {
|
||||
log_error("Unit %s is missing the instance name.", *i);
|
||||
r = -EINVAL;
|
||||
goto finish;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to extract instance: %m");
|
||||
if (isempty(name))
|
||||
return log_error("Unit %s is missing the instance name.", *i);
|
||||
|
||||
r = unit_name_template(*i, &template);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to extract template: %m");
|
||||
goto finish;
|
||||
}
|
||||
if (arg_template && !streq(arg_template, template)) {
|
||||
log_error("Unit %s template %s does not match specified template %s.", *i, template, arg_template);
|
||||
r = -EINVAL;
|
||||
goto finish;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to extract template: %m");
|
||||
if (arg_template && !streq(arg_template, template))
|
||||
return log_error("Unit %s template %s does not match specified template %s.",
|
||||
*i, template, arg_template);
|
||||
} else {
|
||||
name = strdup(*i);
|
||||
if (!name) {
|
||||
r = log_oom();
|
||||
goto finish;
|
||||
}
|
||||
if (!name)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
if (arg_path)
|
||||
r = unit_name_path_unescape(name, &e);
|
||||
else
|
||||
r = unit_name_unescape(name, &e);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to unescape string: %m");
|
||||
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to unescape string: %m");
|
||||
goto finish;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ACTION_MANGLE:
|
||||
r = unit_name_mangle(*i, 0, &e);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to mangle name: %m");
|
||||
goto finish;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to mangle name: %m");
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -283,6 +262,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
fputc('\n', stdout);
|
||||
|
||||
finish:
|
||||
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEFINE_MAIN_FUNCTION(run);
|
||||
|
||||
Reference in New Issue
Block a user