1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-08 02:57:16 +03:00
systemd/src/shared/specifier.c

184 lines
4.9 KiB
C
Raw Normal View History

/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2010-04-15 05:11:11 +04:00
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
2010-04-15 05:11:11 +04:00
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
2010-04-15 05:11:11 +04:00
You should have received a copy of the GNU Lesser General Public License
2010-04-15 05:11:11 +04:00
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <string.h>
2013-07-19 10:45:27 +04:00
#include <sys/utsname.h>
2010-04-15 05:11:11 +04:00
#include "macro.h"
#include "util.h"
#include "hostname-util.h"
2010-04-15 05:11:11 +04:00
#include "specifier.h"
/*
* Generic infrastructure for replacing %x style specifiers in
* strings. Will call a callback for each replacement.
*
*/
int specifier_printf(const char *text, const Specifier table[], void *userdata, char **_ret) {
char *ret, *t;
2010-04-15 05:11:11 +04:00
const char *f;
bool percent = false;
size_t l;
int r;
2010-04-15 05:11:11 +04:00
assert(text);
assert(table);
l = strlen(text);
ret = new(char, l+1);
if (!ret)
return -ENOMEM;
2010-04-15 05:11:11 +04:00
t = ret;
2010-04-15 05:11:11 +04:00
for (f = text; *f; f++, l--) {
if (percent) {
if (*f == '%')
*(t++) = '%';
else {
const Specifier *i;
for (i = table; i->specifier; i++)
if (i->specifier == *f)
break;
if (i->lookup) {
_cleanup_free_ char *w = NULL;
char *n;
2010-04-15 05:11:11 +04:00
size_t k, j;
r = i->lookup(i->specifier, i->data, userdata, &w);
if (r < 0) {
free(ret);
return r;
2010-04-15 05:11:11 +04:00
}
j = t - ret;
2010-04-15 05:11:11 +04:00
k = strlen(w);
2012-11-15 01:15:35 +04:00
n = new(char, j + k + l + 1);
if (!n) {
free(ret);
return -ENOMEM;
2010-04-15 05:11:11 +04:00
}
memcpy(n, ret, j);
2010-04-15 05:11:11 +04:00
memcpy(n + j, w, k);
free(ret);
2010-04-15 05:11:11 +04:00
ret = n;
2010-04-15 05:11:11 +04:00
t = n + j + k;
} else {
*(t++) = '%';
*(t++) = *f;
}
}
percent = false;
} else if (*f == '%')
percent = true;
else
*(t++) = *f;
}
*t = 0;
*_ret = ret;
return 0;
2010-04-15 05:11:11 +04:00
}
/* Generic handler for simple string replacements */
int specifier_string(char specifier, void *data, void *userdata, char **ret) {
char *n;
n = strdup(strempty(data));
if (!n)
return -ENOMEM;
*ret = n;
return 0;
2010-04-15 05:11:11 +04:00
}
int specifier_machine_id(char specifier, void *data, void *userdata, char **ret) {
sd_id128_t id;
char *n;
int r;
r = sd_id128_get_machine(&id);
if (r < 0)
return r;
n = new(char, 33);
if (!n)
return -ENOMEM;
*ret = sd_id128_to_string(id, n);
return 0;
}
int specifier_boot_id(char specifier, void *data, void *userdata, char **ret) {
sd_id128_t id;
char *n;
int r;
r = sd_id128_get_boot(&id);
if (r < 0)
return r;
n = new(char, 33);
if (!n)
return -ENOMEM;
*ret = sd_id128_to_string(id, n);
return 0;
}
int specifier_host_name(char specifier, void *data, void *userdata, char **ret) {
char *n;
n = gethostname_malloc();
if (!n)
return -ENOMEM;
*ret = n;
return 0;
}
2013-07-19 10:45:27 +04:00
int specifier_kernel_release(char specifier, void *data, void *userdata, char **ret) {
2013-07-19 10:45:27 +04:00
struct utsname uts;
char *n;
2013-07-19 10:45:27 +04:00
int r;
r = uname(&uts);
if (r < 0)
return -errno;
n = strdup(uts.release);
if (!n)
return -ENOMEM;
2013-07-19 10:45:27 +04:00
*ret = n;
return 0;
2013-07-19 10:45:27 +04:00
}