static void test_specifier_printf(void) { static const Specifier table[] = { { 'a', specifier_string, (char*) "AAAA" }, { 'b', specifier_string, (char*) "BBBB" }, { 'm', specifier_machine_id, NULL }, { 'B', specifier_boot_id, NULL }, { 'H', specifier_host_name, NULL }, { 'v', specifier_kernel_release, NULL }, {} }; _cleanup_free_ char *w = NULL; int r; r = specifier_printf("xxx a=%a b=%b yyy", table, NULL, &w); assert_se(r >= 0); assert_se(w); puts(w); assert_se(streq(w, "xxx a=AAAA b=BBBB yyy")); free(w); r = specifier_printf("machine=%m, boot=%B, host=%H, version=%v", table, NULL, &w); assert_se(r >= 0); assert_se(w); puts(w); }
char *unit_name_printf(Unit *u, const char* format) { /* * This will use the passed string as format string and * replace the following specifiers: * * %n: the full id of the unit ([email protected]) * %N: the id of the unit without the suffix (foo@bar) * %p: the prefix (foo) * %i: the instance (bar) */ const Specifier table[] = { { 'n', specifier_string, u->id }, { 'N', specifier_prefix_and_instance, NULL }, { 'p', specifier_prefix, NULL }, { 'i', specifier_string, u->instance }, { 0, NULL, NULL } }; assert(u); assert(format); return specifier_printf(format, table, u); }
char *unit_full_printf(Unit *u, const char *format) { /* This is similar to unit_name_printf() but also supports * unescaping. Also, adds a couple of additional codes: * * %f the the instance if set, otherwise the id * %c cgroup path of unit * %r root cgroup path of this systemd instance (e.g. "/user/lennart/shared/systemd-4711") * %R parent of root cgroup path (e.g. "/usr/lennart/shared") * %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR) * %U the UID of the configured user or running user * %u the username of the configured user or running user * %h the homedir of the configured user or running user * %s the shell of the configured user or running user * %m the machine ID of the running system * %H the host name of the running system * %b the boot ID of the running system */ const Specifier table[] = { { 'n', specifier_string, u->id }, { 'N', specifier_prefix_and_instance, NULL }, { 'p', specifier_prefix, NULL }, { 'P', specifier_prefix_unescaped, NULL }, { 'i', specifier_string, u->instance }, { 'I', specifier_instance_unescaped, NULL }, { 'f', specifier_filename, NULL }, { 'c', specifier_cgroup, NULL }, { 'r', specifier_cgroup_root, NULL }, { 'R', specifier_cgroup_root, NULL }, { 't', specifier_runtime, NULL }, { 'U', specifier_user_name, NULL }, { 'u', specifier_user_name, NULL }, { 'h', specifier_user_home, NULL }, { 's', specifier_user_shell, NULL }, { 'm', specifier_machine_id, NULL }, { 'H', specifier_host_name, NULL }, { 'b', specifier_boot_id, NULL }, { 0, NULL, NULL } }; assert(format); return specifier_printf(format, table, u); }
int install_full_printf(InstallInfo *i, const char *format, char **ret) { /* This is similar to unit_full_printf() but does not support * anything path-related. * * %n: the full id of the unit ([email protected]) * %N: the id of the unit without the suffix (foo@bar) * %p: the prefix (foo) * %i: the instance (bar) * %U the UID of the configured user or running user * %u the username of the configured user or running user * %m the machine ID of the running system * %H the host name of the running system * %b the boot ID of the running system * %v `uname -r` of the running system */ const Specifier table[] = { { 'n', specifier_string, i->name }, { 'N', specifier_prefix_and_instance, NULL }, { 'p', specifier_prefix, NULL }, { 'i', specifier_instance, NULL }, { 'U', specifier_user_name, NULL }, { 'u', specifier_user_name, NULL }, { 'm', specifier_machine_id, NULL }, { 'H', specifier_host_name, NULL }, { 'b', specifier_boot_id, NULL }, { 'v', specifier_kernel_release, NULL }, {} }; assert(i); assert(format); assert(ret); return specifier_printf(format, table, i, ret); }
int config_parse_dnssd_service_name(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) { static const Specifier specifier_table[] = { { 'b', specifier_boot_id, NULL }, { 'H', specifier_host_name, NULL }, { 'm', specifier_machine_id, NULL }, { 'v', specifier_kernel_release, NULL }, {} }; DnssdService *s = userdata; _cleanup_free_ char *name = NULL; int r; assert(filename); assert(lvalue); assert(rvalue); assert(s); if (isempty(rvalue)) { log_syntax(unit, LOG_ERR, filename, line, 0, "Service instance name can't be empty. Ignoring."); return -EINVAL; } r = free_and_strdup(&s->name_template, rvalue); if (r < 0) return log_oom(); r = specifier_printf(s->name_template, specifier_table, NULL, &name); if (r < 0) return log_debug_errno(r, "Failed to replace specifiers: %m"); if (!dns_service_name_is_valid(name)) { log_syntax(unit, LOG_ERR, filename, line, 0, "Service instance name template renders to invalid name '%s'. Ignoring.", name); return -EINVAL; } return 0; }
printf("<%s>\n", test); FOREACH_WORD_QUOTED(w, l, test, state) { char *t; assert_se(t = strndup(w, l)); printf("<%s>\n", t); free(t); } printf("%s\n", default_term_for_tty("/dev/tty23")); printf("%s\n", default_term_for_tty("/dev/ttyS23")); printf("%s\n", default_term_for_tty("/dev/tty0")); printf("%s\n", default_term_for_tty("/dev/pty0")); printf("%s\n", default_term_for_tty("/dev/pts/0")); printf("%s\n", default_term_for_tty("/dev/console")); printf("%s\n", default_term_for_tty("tty23")); printf("%s\n", default_term_for_tty("ttyS23")); printf("%s\n", default_term_for_tty("tty0")); printf("%s\n", default_term_for_tty("pty0")); printf("%s\n", default_term_for_tty("pts/0")); printf("%s\n", default_term_for_tty("console")); w = specifier_printf("xxx a=%a b=%b yyy", table, NULL); printf("<%s>\n", w); free(w); return 0; }