15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering#pragma once
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering/***
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering This file is part of systemd.
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering Copyright 2010 Lennart Poettering
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering systemd is free software; you can redistribute it and/or modify it
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering under the terms of the GNU Lesser General Public License as published by
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering (at your option) any later version.
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering systemd is distributed in the hope that it will be useful, but
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering Lesser General Public License for more details.
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering You should have received a copy of the GNU Lesser General Public License
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering***/
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering#include <printf.h>
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering#include <stdarg.h>
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering#include <stdio.h>
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering#include <sys/types.h>
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering#include "macro.h"
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering#define xsprintf(buf, fmt, ...) \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering assert_message_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf), "xsprintf: " #buf "[] must be big enough")
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering#define VA_FORMAT_ADVANCE(format, ap) \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poetteringdo { \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering int _argtypes[128]; \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering size_t _i, _k; \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering _k = parse_printf_format((format), ELEMENTSOF(_argtypes), _argtypes); \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering assert(_k < ELEMENTSOF(_argtypes)); \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering for (_i = 0; _i < _k; _i++) { \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering if (_argtypes[_i] & PA_FLAG_PTR) { \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering (void) va_arg(ap, void*); \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering continue; \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering } \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering switch (_argtypes[_i]) { \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering case PA_INT: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering case PA_INT|PA_FLAG_SHORT: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering case PA_CHAR: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering (void) va_arg(ap, int); \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering break; \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering case PA_INT|PA_FLAG_LONG: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering (void) va_arg(ap, long int); \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering break; \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering case PA_INT|PA_FLAG_LONG_LONG: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering (void) va_arg(ap, long long int); \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering break; \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering case PA_WCHAR: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering (void) va_arg(ap, wchar_t); \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering break; \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering case PA_WSTRING: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering case PA_STRING: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering case PA_POINTER: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering (void) va_arg(ap, void*); \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering break; \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering case PA_FLOAT: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering case PA_DOUBLE: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering (void) va_arg(ap, double); \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering break; \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering case PA_DOUBLE|PA_FLAG_LONG_DOUBLE: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering (void) va_arg(ap, long double); \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering break; \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering default: \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering assert_not_reached("Unknown format string argument."); \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering } \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering } \
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering} while(false)