2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2013-01-28 05:11:31 +01:00
|
|
|
|
2015-12-03 21:13:37 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2013-01-28 05:11:31 +01:00
|
|
|
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2015-10-25 22:32:30 +01:00
|
|
|
#include "install-printf.h"
|
2015-12-03 21:13:37 +01:00
|
|
|
#include "install.h"
|
|
|
|
#include "macro.h"
|
2013-01-28 05:11:31 +01:00
|
|
|
#include "specifier.h"
|
2016-10-20 06:27:46 +02:00
|
|
|
#include "string-util.h"
|
2013-01-28 05:11:31 +01:00
|
|
|
#include "unit-name.h"
|
2015-10-25 22:32:30 +01:00
|
|
|
#include "user-util.h"
|
2013-01-28 05:11:31 +01:00
|
|
|
|
2018-12-12 16:45:33 +01:00
|
|
|
static int specifier_prefix_and_instance(char specifier, const void *data, const void *userdata, char **ret) {
|
2016-10-20 06:27:46 +02:00
|
|
|
const UnitFileInstallInfo *i = userdata;
|
|
|
|
_cleanup_free_ char *prefix = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(i);
|
|
|
|
|
|
|
|
r = unit_name_to_prefix_and_instance(i->name, &prefix);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (endswith(prefix, "@") && i->default_instance) {
|
|
|
|
char *ans;
|
|
|
|
|
2016-10-23 17:43:27 +02:00
|
|
|
ans = strjoin(prefix, i->default_instance);
|
2016-10-20 06:27:46 +02:00
|
|
|
if (!ans)
|
|
|
|
return -ENOMEM;
|
|
|
|
*ret = ans;
|
2018-03-22 16:53:26 +01:00
|
|
|
} else
|
|
|
|
*ret = TAKE_PTR(prefix);
|
2016-10-20 06:27:46 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-12 16:45:33 +01:00
|
|
|
static int specifier_name(char specifier, const void *data, const void *userdata, char **ret) {
|
2016-10-20 06:27:46 +02:00
|
|
|
const UnitFileInstallInfo *i = userdata;
|
|
|
|
char *ans;
|
2013-09-17 17:03:46 +02:00
|
|
|
|
2013-01-28 05:11:31 +01:00
|
|
|
assert(i);
|
|
|
|
|
2016-10-20 06:27:46 +02:00
|
|
|
if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE) && i->default_instance)
|
|
|
|
return unit_name_replace_instance(i->name, i->default_instance, ret);
|
|
|
|
|
|
|
|
ans = strdup(i->name);
|
|
|
|
if (!ans)
|
|
|
|
return -ENOMEM;
|
|
|
|
*ret = ans;
|
|
|
|
return 0;
|
2013-01-28 05:11:31 +01:00
|
|
|
}
|
|
|
|
|
2018-12-12 16:45:33 +01:00
|
|
|
static int specifier_prefix(char specifier, const void *data, const void *userdata, char **ret) {
|
2016-10-20 06:27:46 +02:00
|
|
|
const UnitFileInstallInfo *i = userdata;
|
2013-09-17 17:03:46 +02:00
|
|
|
|
2013-01-28 05:11:31 +01:00
|
|
|
assert(i);
|
|
|
|
|
2015-04-30 20:21:00 +02:00
|
|
|
return unit_name_to_prefix(i->name, ret);
|
2013-01-28 05:11:31 +01:00
|
|
|
}
|
|
|
|
|
2018-12-12 16:45:33 +01:00
|
|
|
static int specifier_instance(char specifier, const void *data, const void *userdata, char **ret) {
|
2016-10-20 06:27:46 +02:00
|
|
|
const UnitFileInstallInfo *i = userdata;
|
2013-01-28 05:11:31 +01:00
|
|
|
char *instance;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(i);
|
|
|
|
|
|
|
|
r = unit_name_to_instance(i->name, &instance);
|
|
|
|
if (r < 0)
|
2013-09-17 17:03:46 +02:00
|
|
|
return r;
|
|
|
|
|
2016-10-20 06:27:46 +02:00
|
|
|
if (isempty(instance)) {
|
2017-11-24 10:31:08 +01:00
|
|
|
r = free_and_strdup(&instance, strempty(i->default_instance));
|
2017-01-15 19:42:53 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-09-17 17:03:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*ret = instance;
|
|
|
|
return 0;
|
2013-01-28 05:11:31 +01:00
|
|
|
}
|
|
|
|
|
2018-12-12 16:45:33 +01:00
|
|
|
static int specifier_last_component(char specifier, const void *data, const void *userdata, char **ret) {
|
2018-04-23 22:43:20 +02:00
|
|
|
_cleanup_free_ char *prefix = NULL;
|
|
|
|
char *dash;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = specifier_prefix(specifier, data, userdata, &prefix);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
dash = strrchr(prefix, '-');
|
|
|
|
if (dash) {
|
|
|
|
dash = strdup(dash + 1);
|
|
|
|
if (!dash)
|
|
|
|
return -ENOMEM;
|
|
|
|
*ret = dash;
|
|
|
|
} else
|
|
|
|
*ret = TAKE_PTR(prefix);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-12 16:45:33 +01:00
|
|
|
int install_full_printf(const UnitFileInstallInfo *i, const char *format, char **ret) {
|
2020-11-25 07:09:17 +01:00
|
|
|
/* This is similar to unit_name_printf() */
|
2013-01-28 05:11:31 +01:00
|
|
|
|
|
|
|
const Specifier table[] = {
|
2020-11-25 07:09:17 +01:00
|
|
|
{ 'i', specifier_instance, NULL },
|
|
|
|
{ 'j', specifier_last_component, NULL },
|
2016-10-20 06:27:46 +02:00
|
|
|
{ 'n', specifier_name, NULL },
|
2013-01-28 05:11:31 +01:00
|
|
|
{ 'N', specifier_prefix_and_instance, NULL },
|
|
|
|
{ 'p', specifier_prefix, NULL },
|
2020-11-20 15:15:24 +01:00
|
|
|
|
|
|
|
COMMON_SYSTEM_SPECIFIERS,
|
2020-11-25 07:09:17 +01:00
|
|
|
|
|
|
|
COMMON_CREDS_SPECIFIERS,
|
2013-07-19 08:45:27 +02:00
|
|
|
{}
|
2013-01-28 05:11:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(i);
|
|
|
|
assert(format);
|
2013-09-17 17:03:46 +02:00
|
|
|
assert(ret);
|
2013-01-28 05:11:31 +01:00
|
|
|
|
2013-09-17 17:03:46 +02:00
|
|
|
return specifier_printf(format, table, i, ret);
|
2013-01-28 05:11:31 +01:00
|
|
|
}
|