1*592efe25SPierre Pronchery /*
2*592efe25SPierre Pronchery * output.c
3*592efe25SPierre Pronchery * I/O abstraction layer
4*592efe25SPierre Pronchery *
5*592efe25SPierre Pronchery * SPDX-License-Identifier: pkgconf
6*592efe25SPierre Pronchery *
7*592efe25SPierre Pronchery * Copyright (c) 2025 pkgconf authors (see AUTHORS).
8*592efe25SPierre Pronchery *
9*592efe25SPierre Pronchery * Permission to use, copy, modify, and/or distribute this software for any
10*592efe25SPierre Pronchery * purpose with or without fee is hereby granted, provided that the above
11*592efe25SPierre Pronchery * copyright notice and this permission notice appear in all copies.
12*592efe25SPierre Pronchery *
13*592efe25SPierre Pronchery * This software is provided 'as is' and without any warranty, express or
14*592efe25SPierre Pronchery * implied. In no event shall the authors be liable for any damages arising
15*592efe25SPierre Pronchery * from the use of this software.
16*592efe25SPierre Pronchery */
17*592efe25SPierre Pronchery
18*592efe25SPierre Pronchery #include <libpkgconf/stdinc.h>
19*592efe25SPierre Pronchery #include <libpkgconf/libpkgconf.h>
20*592efe25SPierre Pronchery
21*592efe25SPierre Pronchery bool
pkgconf_output_putbuf(pkgconf_output_t * output,pkgconf_output_stream_t stream,const pkgconf_buffer_t * buffer,bool newline)22*592efe25SPierre Pronchery pkgconf_output_putbuf(pkgconf_output_t *output, pkgconf_output_stream_t stream, const pkgconf_buffer_t *buffer, bool newline)
23*592efe25SPierre Pronchery {
24*592efe25SPierre Pronchery bool ret;
25*592efe25SPierre Pronchery pkgconf_buffer_t buf = PKGCONF_BUFFER_INITIALIZER;
26*592efe25SPierre Pronchery
27*592efe25SPierre Pronchery if (pkgconf_buffer_len(buffer) != 0)
28*592efe25SPierre Pronchery pkgconf_buffer_append(&buf, pkgconf_buffer_str(buffer));
29*592efe25SPierre Pronchery
30*592efe25SPierre Pronchery if (newline)
31*592efe25SPierre Pronchery pkgconf_buffer_push_byte(&buf, '\n');
32*592efe25SPierre Pronchery
33*592efe25SPierre Pronchery ret = output->write(output, stream, &buf);
34*592efe25SPierre Pronchery pkgconf_buffer_finalize(&buf);
35*592efe25SPierre Pronchery
36*592efe25SPierre Pronchery return ret;
37*592efe25SPierre Pronchery }
38*592efe25SPierre Pronchery
39*592efe25SPierre Pronchery bool
pkgconf_output_puts(pkgconf_output_t * output,pkgconf_output_stream_t stream,const char * str)40*592efe25SPierre Pronchery pkgconf_output_puts(pkgconf_output_t *output, pkgconf_output_stream_t stream, const char *str)
41*592efe25SPierre Pronchery {
42*592efe25SPierre Pronchery bool ret;
43*592efe25SPierre Pronchery pkgconf_buffer_t buf = PKGCONF_BUFFER_INITIALIZER;
44*592efe25SPierre Pronchery
45*592efe25SPierre Pronchery pkgconf_buffer_append(&buf, str);
46*592efe25SPierre Pronchery pkgconf_buffer_push_byte(&buf, '\n');
47*592efe25SPierre Pronchery ret = output->write(output, stream, &buf);
48*592efe25SPierre Pronchery pkgconf_buffer_finalize(&buf);
49*592efe25SPierre Pronchery
50*592efe25SPierre Pronchery return ret;
51*592efe25SPierre Pronchery }
52*592efe25SPierre Pronchery
53*592efe25SPierre Pronchery bool
pkgconf_output_fmt(pkgconf_output_t * output,pkgconf_output_stream_t stream,const char * fmt,...)54*592efe25SPierre Pronchery pkgconf_output_fmt(pkgconf_output_t *output, pkgconf_output_stream_t stream, const char *fmt, ...)
55*592efe25SPierre Pronchery {
56*592efe25SPierre Pronchery bool ret;
57*592efe25SPierre Pronchery va_list va;
58*592efe25SPierre Pronchery
59*592efe25SPierre Pronchery va_start(va, fmt);
60*592efe25SPierre Pronchery ret = pkgconf_output_vfmt(output, stream, fmt, va);
61*592efe25SPierre Pronchery va_end(va);
62*592efe25SPierre Pronchery
63*592efe25SPierre Pronchery return ret;
64*592efe25SPierre Pronchery }
65*592efe25SPierre Pronchery
66*592efe25SPierre Pronchery bool
pkgconf_output_vfmt(pkgconf_output_t * output,pkgconf_output_stream_t stream,const char * fmt,va_list src_va)67*592efe25SPierre Pronchery pkgconf_output_vfmt(pkgconf_output_t *output, pkgconf_output_stream_t stream, const char *fmt, va_list src_va)
68*592efe25SPierre Pronchery {
69*592efe25SPierre Pronchery va_list va;
70*592efe25SPierre Pronchery bool ret;
71*592efe25SPierre Pronchery pkgconf_buffer_t buf = PKGCONF_BUFFER_INITIALIZER;
72*592efe25SPierre Pronchery
73*592efe25SPierre Pronchery va_copy(va, src_va);
74*592efe25SPierre Pronchery pkgconf_buffer_append_vfmt(&buf, fmt, va);
75*592efe25SPierre Pronchery va_end(va);
76*592efe25SPierre Pronchery
77*592efe25SPierre Pronchery ret = output->write(output, stream, &buf);
78*592efe25SPierre Pronchery pkgconf_buffer_finalize(&buf);
79*592efe25SPierre Pronchery
80*592efe25SPierre Pronchery return ret;
81*592efe25SPierre Pronchery }
82*592efe25SPierre Pronchery
83*592efe25SPierre Pronchery static bool
pkgconf_output_stdio_write(pkgconf_output_t * output,pkgconf_output_stream_t stream,const pkgconf_buffer_t * buffer)84*592efe25SPierre Pronchery pkgconf_output_stdio_write(pkgconf_output_t *output, pkgconf_output_stream_t stream, const pkgconf_buffer_t *buffer)
85*592efe25SPierre Pronchery {
86*592efe25SPierre Pronchery (void) output;
87*592efe25SPierre Pronchery
88*592efe25SPierre Pronchery FILE *target = stream == PKGCONF_OUTPUT_STDERR ? stderr : stdout;
89*592efe25SPierre Pronchery
90*592efe25SPierre Pronchery if (buffer != NULL)
91*592efe25SPierre Pronchery {
92*592efe25SPierre Pronchery const char *str = pkgconf_buffer_str(buffer);
93*592efe25SPierre Pronchery size_t size = pkgconf_buffer_len(buffer);
94*592efe25SPierre Pronchery
95*592efe25SPierre Pronchery if (size > 0 && !fwrite(str, size, 1, target))
96*592efe25SPierre Pronchery return false;
97*592efe25SPierre Pronchery }
98*592efe25SPierre Pronchery
99*592efe25SPierre Pronchery fflush(target);
100*592efe25SPierre Pronchery return true;
101*592efe25SPierre Pronchery }
102*592efe25SPierre Pronchery
103*592efe25SPierre Pronchery static pkgconf_output_t pkgconf_default_output = {
104*592efe25SPierre Pronchery .privdata = NULL,
105*592efe25SPierre Pronchery .write = pkgconf_output_stdio_write,
106*592efe25SPierre Pronchery };
107*592efe25SPierre Pronchery
108*592efe25SPierre Pronchery pkgconf_output_t *
pkgconf_output_default(void)109*592efe25SPierre Pronchery pkgconf_output_default(void)
110*592efe25SPierre Pronchery {
111*592efe25SPierre Pronchery return &pkgconf_default_output;
112*592efe25SPierre Pronchery }
113*592efe25SPierre Pronchery
114*592efe25SPierre Pronchery /*
115*592efe25SPierre Pronchery * !doc
116*592efe25SPierre Pronchery *
117*592efe25SPierre Pronchery * .. c:function:: bool pkgconf_output_file_vfmt(FILE *f, const char *fmt, va_list va)
118*592efe25SPierre Pronchery *
119*592efe25SPierre Pronchery * Wrapper around :code:`vfprintf` that returns a boolean.
120*592efe25SPierre Pronchery *
121*592efe25SPierre Pronchery * :param FILE *f: Pointer to an open `FILE` pointer.
122*592efe25SPierre Pronchery * :param: const char *fmt: Format string.
123*592efe25SPierre Pronchery * :param va_list va: Variable list.
124*592efe25SPierre Pronchery * :return: :code:`true` on success, :code:`false` on failure.
125*592efe25SPierre Pronchery */
126*592efe25SPierre Pronchery bool
pkgconf_output_file_vfmt(FILE * f,const char * fmt,va_list va)127*592efe25SPierre Pronchery pkgconf_output_file_vfmt(FILE *f, const char *fmt, va_list va)
128*592efe25SPierre Pronchery {
129*592efe25SPierre Pronchery int ret = vfprintf(f, fmt, va);
130*592efe25SPierre Pronchery return ret >= 0;
131*592efe25SPierre Pronchery }
132*592efe25SPierre Pronchery
133*592efe25SPierre Pronchery /*
134*592efe25SPierre Pronchery * !doc
135*592efe25SPierre Pronchery *
136*592efe25SPierre Pronchery * .. c:function:: bool pkgconf_output_file_fmt(FILE *f, const char *fmt, va_list va)
137*592efe25SPierre Pronchery *
138*592efe25SPierre Pronchery * Wrapper around :code:`fprintf` that returns a boolean.
139*592efe25SPierre Pronchery *
140*592efe25SPierre Pronchery * :param FILE *f: Pointer to an open `FILE` pointer.
141*592efe25SPierre Pronchery * :param: const char *fmt: Format string.
142*592efe25SPierre Pronchery * :return: :code:`true` on success, :code:`false` on failure.
143*592efe25SPierre Pronchery */
144*592efe25SPierre Pronchery bool
pkgconf_output_file_fmt(FILE * f,const char * fmt,...)145*592efe25SPierre Pronchery pkgconf_output_file_fmt(FILE *f, const char *fmt, ...)
146*592efe25SPierre Pronchery {
147*592efe25SPierre Pronchery va_list va;
148*592efe25SPierre Pronchery va_start(va, fmt);
149*592efe25SPierre Pronchery bool ret = pkgconf_output_file_vfmt(f, fmt, va);
150*592efe25SPierre Pronchery va_end(va);
151*592efe25SPierre Pronchery
152*592efe25SPierre Pronchery return ret;
153*592efe25SPierre Pronchery }
154