<?xml version='1.0'?>
-<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
-"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
-
-<!--
- SPDX-License-Identifier: LGPL-2.1+
--->
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
+ "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+<!-- SPDX-License-Identifier: LGPL-2.1+ -->
<refentry id="sd_bus_message_read"
xmlns:xi="http://www.w3.org/2001/XInclude">
<refnamediv>
<refname>sd_bus_message_read</refname>
<refname>sd_bus_message_readv</refname>
+ <refname>sd_bus_message_peek_type</refname>
<refpurpose>Read a sequence of values from a message</refpurpose>
</refnamediv>
<funcprototype>
<funcdef>int <function>sd_bus_message_read</function></funcdef>
<paramdef>sd_bus_message *<parameter>m</parameter></paramdef>
- <paramdef>char char *<parameter>types</parameter></paramdef>
+ <paramdef>const char *<parameter>types</parameter></paramdef>
<paramdef>...</paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_bus_message_readv</function></funcdef>
<paramdef>sd_bus_message *<parameter>m</parameter></paramdef>
- <paramdef>char char *<parameter>types</parameter></paramdef>
+ <paramdef>const char *<parameter>types</parameter></paramdef>
<paramdef>va_list <parameter>ap</parameter></paramdef>
</funcprototype>
+
+ <funcprototype>
+ <funcdef>int <function>sd_bus_message_peek_type</function></funcdef>
+ <paramdef>char *<parameter>type</parameter></paramdef>
+ <paramdef>const char **<parameter>contents</parameter></paramdef>
+ </funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
- <para><function>sd_bus_message_read()</function> reads a sequence of fields from
- the D-Bus message object <parameter>m</parameter> and advances the read position
- in the message. The type string <parameter>types</parameter> describes the types
- of items expected in the message and the field arguments that follow. The type
- string may be <constant>NULL</constant> or empty, in which case nothing is
- read.</para>
+ <para><function>sd_bus_message_read()</function> reads a sequence of fields from the D-Bus message object
+ <parameter>m</parameter> and advances the read position in the message. The type string
+ <parameter>types</parameter> describes the types of items expected in the message and the field arguments
+ that follow. The type string may be <constant>NULL</constant> or empty, in which case nothing is read.
+ </para>
<para>The type string is composed of the elements described in
<citerefentry><refentrytitle>sd_bus_message_append</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- i.e. basic and container types. It must contain zero or more single "complete
- types". The type string is <constant>NUL</constant>-terminated.</para>
-
- <para>For each type specified in the type string, one or more arguments need to be specified
- after the <parameter>types</parameter> parameter, in the same order. The arguments must be
- pointers to appropriate types (a pointer to <code>int8_t</code> for a <literal>y</literal> in
- the type string, a pointer to <code>int32_t</code> for an <literal>i</literal>, a pointer to
- <code>const char*</code> for an <literal>s</literal>, ...) which are set based on the values in
- the message. As an exception, in case or array and variant types, the first argument is an
- "input" argument that further specifies how the message should be read. See the table below for
- a complete list of allowed arguments and their types. Note that, if the basic type is a pointer
- (e.g., <code>const char *</code> in the case of a string), the argument is a pointer to a
- pointer, and also the pointer value that is written is only borrowed and the contents must be
- copied if they are to be used after the end of the messages lifetime.</para>
-
- <para>Each argument may also be <constant>NULL</constant>, in which case the value is read and
- ignored.</para>
+ i.e. basic and container types. It must contain zero or more single "complete types". The type string is
+ <constant>NUL</constant>-terminated.</para>
+
+ <para>For each type specified in the type string, one or more arguments need to be specified after the
+ <parameter>types</parameter> parameter, in the same order. The arguments must be pointers to appropriate
+ types (a pointer to <type>int8_t</type> for a <literal>y</literal> in the type string, a pointer to
+ <type>int32_t</type> for an <literal>i</literal>, a pointer to <type>const char*</type> for an
+ <literal>s</literal>, ...) which are set based on the values in the message. As an exception, in case of
+ array and variant types, the first argument is an "input" argument that further specifies how the message
+ should be read. See the table below for a complete list of allowed arguments and their types. Note that,
+ if the basic type is a pointer (e.g., <type>const char *</type> in the case of a string), the argument is
+ a pointer to a pointer, and also the pointer value that is written is only borrowed and the contents must
+ be copied if they are to be used after the end of the messages lifetime.</para>
+
+ <para>Each argument may also be <constant>NULL</constant>, in which case the value is read and ignored.
+ </para>
<table>
<title>Item type specifiers</title>
<entry><literal>a</literal></entry>
<entry><constant>SD_BUS_TYPE_ARRAY</constant></entry>
<entry>array</entry>
- <entry>int, which specifies the expected length <parameter>n</parameter> of the array</entry>
+ <entry><type>int</type>, which specifies the expected length <parameter>n</parameter> of the array</entry>
<entry><parameter>n</parameter> sets of arguments appropriate for the array element type</entry>
</row>
</tgroup>
</table>
- <para>If objects of the specified types are not present at the current position
- in the message, an error is returned.
- </para>
+ <para>If objects of the specified types are not present at the current position in the message, an error
+ is returned.</para>
<para>The <function>sd_bus_message_readv()</function> is equivalent to the
- <function>sd_bus_message_read()</function>, except that it is called with a
- <literal>va_list</literal> instead of a variable number of arguments. This
- function does not call the <function>va_end()</function> macro. Because it
- invokes the <function>va_arg()</function> macro, the value of
- <parameter>ap</parameter> is undefined after the call.</para>
+ <function>sd_bus_message_read()</function>, except that it is called with a <literal>va_list</literal>
+ instead of a variable number of arguments. This function does not call the <function>va_end()</function>
+ macro. Because it invokes the <function>va_arg()</function> macro, the value of <parameter>ap</parameter>
+ is undefined after the call.</para>
+
+ <para><function>sd_bus_message_peek_type()</function> determines the type of the next element in
+ <parameter>m</parameter> to be read by <function>sd_bus_message_read()</function> or similar functions.
+ On success, the type is stored in <parameter>type</parameter>, if it is not <constant>NULL</constant>.
+ If the type is a container type, the type of its elements is stored in <parameter>contents</parameter>,
+ if it is not <constant>NULL</constant>. If this function successfully determines the type of the next
+ element in <parameter>m</parameter>, it returns a positive integer. If there are no more elements to be
+ read, it returns zero.</para>
</refsect1>
<refsect1>
<title>Return Value</title>
- <para>
- On success, <function>sd_bus_message_read()</function> and
- <function>sd_bus_message_readv()</function> return 0 or a positive integer. On
- failure, they return a negative errno-style error code.
- </para>
- </refsect1>
+ <para>On success, these functions return a non-negative integer. On failure, they return a negative
+ errno-style error code.</para>
- <xi:include href="sd_bus_message_read_basic.xml" xpointer="errors" />
+ <xi:include href="sd_bus_message_read_basic.xml" xpointer="errors" />
+ </refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
sd_bus_message_read(m, "x", &x);</programlisting>
+ <para>Read a boolean value:</para>
+
+ <programlisting>sd_bus_message *m;
+int x; /* Do not use C99 'bool' type here, it's typically smaller
+ in memory and would cause memory corruption */
+
+sd_bus_message_read(m, "b", &x);</programlisting>
+
<para>Read all types of integers:</para>
<programlisting>uint8_t y;
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_message_read_basic</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>sd_bus_message_skip</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_message_append</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
</refsect1>