2010-08-14 21:59:25 +04:00
/*-*- 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
2012-04-12 02:20:58 +04:00
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
2012-04-12 02:20:58 +04:00
Lesser General Public License for more details .
2010-04-15 05:11:11 +04:00
2012-04-12 02:20:58 +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"
2015-05-18 18:10:07 +03:00
# 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 .
*
*/
2013-09-17 19:03:46 +04:00
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 ;
2013-09-17 19:03:46 +04:00
int r ;
2010-04-15 05:11:11 +04:00
assert ( text ) ;
assert ( table ) ;
l = strlen ( text ) ;
2013-09-17 19:03:46 +04:00
ret = new ( char , l + 1 ) ;
if ( ! ret )
return - ENOMEM ;
2010-04-15 05:11:11 +04:00
2013-09-17 19:03:46 +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 ) {
2013-09-17 19:03:46 +04:00
_cleanup_free_ char * w = NULL ;
char * n ;
2010-04-15 05:11:11 +04:00
size_t k , j ;
2013-09-17 19:03:46 +04:00
r = i - > lookup ( i - > specifier , i - > data , userdata , & w ) ;
if ( r < 0 ) {
free ( ret ) ;
return r ;
2010-04-15 05:11:11 +04:00
}
2013-09-17 19:03:46 +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 ) {
2013-09-17 19:03:46 +04:00
free ( ret ) ;
return - ENOMEM ;
2010-04-15 05:11:11 +04:00
}
2013-09-17 19:03:46 +04:00
memcpy ( n , ret , j ) ;
2010-04-15 05:11:11 +04:00
memcpy ( n + j , w , k ) ;
2013-09-17 19:03:46 +04:00
free ( ret ) ;
2010-04-15 05:11:11 +04:00
2013-09-17 19:03:46 +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 ;
2013-09-17 19:03:46 +04:00
* _ret = ret ;
return 0 ;
2010-04-15 05:11:11 +04:00
}
/* Generic handler for simple string replacements */
2013-09-17 19:03:46 +04:00
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
}
2013-01-28 04:44:52 +04:00
2013-09-17 19:03:46 +04:00
int specifier_machine_id ( char specifier , void * data , void * userdata , char * * ret ) {
2013-01-28 04:44:52 +04:00
sd_id128_t id ;
2013-09-17 19:03:46 +04:00
char * n ;
2013-01-28 04:44:52 +04:00
int r ;
r = sd_id128_get_machine ( & id ) ;
if ( r < 0 )
2013-09-17 19:03:46 +04:00
return r ;
2013-01-28 04:44:52 +04:00
2013-09-17 19:03:46 +04:00
n = new ( char , 33 ) ;
if ( ! n )
return - ENOMEM ;
2013-01-28 04:44:52 +04:00
2013-09-17 19:03:46 +04:00
* ret = sd_id128_to_string ( id , n ) ;
return 0 ;
2013-01-28 04:44:52 +04:00
}
2013-09-17 19:03:46 +04:00
int specifier_boot_id ( char specifier , void * data , void * userdata , char * * ret ) {
2013-01-28 04:44:52 +04:00
sd_id128_t id ;
2013-09-17 19:03:46 +04:00
char * n ;
2013-01-28 04:44:52 +04:00
int r ;
r = sd_id128_get_boot ( & id ) ;
if ( r < 0 )
2013-09-17 19:03:46 +04:00
return r ;
2013-01-28 04:44:52 +04:00
2013-09-17 19:03:46 +04:00
n = new ( char , 33 ) ;
if ( ! n )
return - ENOMEM ;
2013-01-28 04:44:52 +04:00
2013-09-17 19:03:46 +04:00
* ret = sd_id128_to_string ( id , n ) ;
return 0 ;
2013-01-28 04:44:52 +04:00
}
2013-09-17 19:03:46 +04:00
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-01-28 04:44:52 +04:00
}
2013-07-19 10:45:27 +04:00
2013-09-17 19:03:46 +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 ;
2013-09-17 19:03:46 +04:00
char * n ;
2013-07-19 10:45:27 +04:00
int r ;
r = uname ( & uts ) ;
if ( r < 0 )
2013-09-17 19:03:46 +04:00
return - errno ;
n = strdup ( uts . release ) ;
if ( ! n )
return - ENOMEM ;
2013-07-19 10:45:27 +04:00
2013-09-17 19:03:46 +04:00
* ret = n ;
return 0 ;
2013-07-19 10:45:27 +04:00
}