1 /*-
2  * Copyright (c) 2006 Verdens Gang AS
3  * Copyright (c) 2006-2015 Varnish Software AS
4  * All rights reserved.
5  *
6  * Author: Martin Blix Grydeland <martin@varnish-software.com>
7  *
8  * SPDX-License-Identifier: BSD-2-Clause
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * Common functions for the utilities
32  */
33 
34 struct VUT;
35 struct vopt_spec;
36 
37 typedef void VUT_sighandler_f(int);
38 typedef int VUT_cb_f(struct VUT *);
39 typedef void VUT_error_f(struct VUT *, int, const char *, va_list);
40 
41 struct VUT {
42 	unsigned	magic;
43 #define VUT_MAGIC	0xdf3b3de8
44 	const char	*progname;
45 
46 	/* Options */
47 	int		d_opt;
48 	int		D_opt;
49 	int		g_arg;
50 	int		k_arg;
51 	char		*n_arg;
52 	char		*P_arg;
53 	char		*q_arg;
54 	char		*r_arg;
55 	char		*t_arg;
56 
57 	/* State */
58 	struct VSL_data	*vsl;
59 	struct vsm	*vsm;
60 	struct VSLQ	*vslq;
61 
62 	sig_atomic_t	last_sighup;
63 	sig_atomic_t	last_sigusr1;
64 
65 	/* Callback functions */
66 	VUT_cb_f	*idle_f;
67 	VUT_cb_f	*sighup_f;
68 	VUT_error_f	*error_f;
69 	VSLQ_dispatch_f	*dispatch_f;
70 	void		*dispatch_priv;
71 };
72 
73 void VUT_Error(struct VUT *, int status, const char *fmt, ...)
74     v_noreturn_ v_printflike_(3, 4);
75 
76 int VUT_Arg(struct VUT *, int opt, const char *arg);
77 
78 //lint -sem(VUT_Usage, r_no)
79 void VUT_Usage(const struct VUT *, const struct vopt_spec *,
80     int status) v_noreturn_;
81 
82 #define VUT_InitProg(argc, argv, spec) VUT_Init(argv[0], argc, argv, spec)
83 
84 struct VUT * VUT_Init(const char *progname, int argc, char * const *argv,
85     const struct vopt_spec *);
86 
87 void VUT_Signal(VUT_sighandler_f);
88 void VUT_Signaled(struct VUT *, int);
89 
90 void VUT_Setup(struct VUT *);
91 int  VUT_Main(struct VUT *);
92 void VUT_Fini(struct VUT **);
93