libamxrt  0.4.2
Ambiorix Run Time Library
amxrt_user_output.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #ifndef _GNU_SOURCE
56 #define _GNU_SOURCE
57 #endif
58 
59 #include <stdlib.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include <errno.h>
63 #include <syslog.h>
64 
65 #include <amxrt/amxrt.h>
66 #include "amxrt_priv.h"
67 
68 static const char* colors[] = {
76 };
77 
78 static void amxrt_cmd_line_arg(const char so,
79  const char* lo,
80  const char* args,
81  const char* description) {
82  int pre_length = 4 + 2 + 4 + strlen(lo);
83 
84  printf(" %s-%c%s %s--%s%s",
85  c(GREEN), so, c(RESET),
86  c(GREEN), lo, c(RESET));
87 
88  if(args != NULL) {
89  pre_length += 3 + strlen(args);
90  printf("%s %s %s ", c(BLUE), args, c(RESET));
91  }
92  for(int i = 39 - pre_length; i > 0; i--) {
93  printf(" ");
94  }
95  printf("%s%s%s\n", c(WHITE), description, c(RESET));
96 }
97 
98 static void amxrt_print_notes(void) {
99  printf("\n");
100  printf("%sNotes:%s\n", c(CYAN), c(RESET));
101  printf(" %s*%s Short and long options take the same arguments\n", c(YELLOW), c(RESET));
102  printf("\n");
103  printf(" %s*%s At least one odl file or odl string must be specified\n", c(YELLOW), c(RESET));
104  printf("\n");
105  printf(" %s*%s Each bus backend can only be specified once\n", c(YELLOW), c(RESET));
106  printf("\n");
107  printf(" %s*%s The process daemizes (-D) after the entry points are called, but before\n", c(YELLOW), c(RESET));
108  printf(" the data model is registered to the busses.\n");
109  printf("\n");
110  printf(" %s*%s All command line options can be put in the config section of one\n", c(YELLOW), c(RESET));
111  printf(" of the main odls.\n");
112 }
113 
114 static void amxrt_print_config_example(void) {
115  printf("\n");
116  printf("%sExample config section:%s\n", c(CYAN), c(RESET));
117  printf("\n");
118  printf("%s%%config%s {\n", c(GREEN), c(RESET));
119  printf(" %suris%s = %s\"pcb:/var/run/pcb_sys,ubus:/var/run/ubus/ubus.sock\"%s;\n", c(BLUE), c(RESET), c(CYAN), c(RESET));
120  printf(" %sbackends%s = %s\"/usr/bin/mods/amxb/mod-amxb-pcb.so,/usr/bin/mods/amxb/mod-amxb-ubus.so\"%s;\n", c(BLUE), c(RESET), c(CYAN), c(RESET));
121  printf(" %sauto-detect%s = %strue%s;\n", c(BLUE), c(RESET), c(CYAN), c(RESET));
122  printf(" %sauto-connect%s = %strue%s;\n", c(BLUE), c(RESET), c(CYAN), c(RESET));
123  printf(" %sinclude-dirs%s = %s\".\"%s;\n", c(BLUE), c(RESET), c(CYAN), c(RESET));
124  printf(" %simport-dirs%s = %s\".\"%s;\n", c(BLUE), c(RESET), c(CYAN), c(RESET));
125  printf(" %sdaemon%s = %sfalse%s;\n", c(BLUE), c(RESET), c(CYAN), c(RESET));
126  printf(" %spriority%s = %s0%s;\n", c(BLUE), c(RESET), c(CYAN), c(RESET));
127  printf("}\n");
128 }
129 
130 const char* get_color(uint32_t cc) {
131  if(cc < RESET) {
132  return colors[cc];
133  }
134  return colors[RESET];
135 }
136 
137 void amxrt_print_usage(void) {
138  amxrt_t* rt = amxrt_get();
139  const char* name = GET_CHAR(&rt->parser.config, "name");
140 
141  printf("%s %s\n", name, rt->usage_doc);
142 
143  if(!amxc_llist_is_empty(&rt->cmd_line_args)) {
144  printf("\n");
145  printf("%sOptions%s:\n", c(CYAN), c(RESET));
146  printf("\n");
147 
148  amxc_llist_for_each(it, &rt->cmd_line_args) {
149  amxrt_arg_t* arg = amxc_container_of(it, amxrt_arg_t, it);
151  arg->long_option == NULL ? "" : arg->long_option,
152  arg->arg_doc,
153  arg->doc);
154  }
155  }
156 
157  printf("\n");
158 }
159 
160 void amxrt_print_help(void) {
164 }
165 
167  amxrt_t* rt = amxrt_get();
168 
169  fprintf(stderr, "\n%sConfiguration:%s\n", c(GREEN), c(RESET));
170  fflush(stderr);
171  amxc_var_dump(&rt->parser.config, STDERR_FILENO);
172 }
173 
174 void amxrt_print_error(const char* fmt, ...) {
175  amxc_var_t* config = amxrt_get_config();
176  va_list args;
177 
178  if(GET_BOOL(config, AMXRT_COPT_LOG)) {
179  va_start(args, fmt);
180  vsyslog(LOG_USER | LOG_ERR, fmt, args);
181  va_end(args);
182  } else {
183  fprintf(stderr, "%sERROR%s -- %s", c(RED), c(RESET), c(WHITE));
184 
185  va_start(args, fmt);
186  vfprintf(stderr, fmt, args);
187  va_end(args);
188 
189  fprintf(stderr, "%s\n", c(RESET));
190  }
191 }
192 
193 void amxrt_print_message(const char* fmt, ...) {
194  amxc_var_t* config = amxrt_get_config();
195  va_list args;
196 
197  if(GET_BOOL(config, AMXRT_COPT_LOG)) {
198  va_start(args, fmt);
199  vsyslog(LOG_USER | LOG_NOTICE, "%s", args);
200  va_end(args);
201  } else {
202  fprintf(stderr, "%sINFO%s -- %s", c(YELLOW), c(RESET), c(WHITE));
203 
204  va_start(args, fmt);
205  vfprintf(stderr, fmt, args);
206  va_end(args);
207 
208  fprintf(stderr, "%s\n", c(RESET));
209  }
210 }
211 
212 
213 void amxrt_print_failure(amxo_parser_t* parser, const char* string) {
214  amxc_var_t* config = amxrt_get_config();
215  const char* msg = amxo_parser_get_message(parser);
216 
217  if(GET_BOOL(config, AMXRT_COPT_LOG)) {
218  syslog(LOG_USER | LOG_CRIT, "Failed parsing - %s", string == NULL ? "###" : string);
219  syslog(LOG_USER | LOG_CRIT, "Reason - %s", msg == NULL ? "###" : msg);
220  } else {
221  fprintf(stderr, "%sERROR%s -- Failed parsing %s%s%s\n",
222  c(RED), c(RESET),
223  c(CYAN), string == NULL ? "###" : string, c(RESET));
224 
225  fprintf(stderr, "%sREASON%s -- %s%s%s\n",
226  c(BLUE), c(RESET),
227  c(CYAN), msg == NULL ? "###" : msg, c(RESET));
228  }
229 }
static amxrt_t rt
Definition: amxrt.c:74
#define AMXRT_COPT_LOG
Definition: amxrt.h:98
#define COLOR_BRIGHT_RED
Definition: amxrt_priv.h:79
#define COLOR_RESET
Definition: amxrt_priv.h:87
#define COLOR_BRIGHT_YELLOW
Definition: amxrt_priv.h:81
#define COLOR_BRIGHT_GREEN
Definition: amxrt_priv.h:80
#define BLUE
Definition: amxrt_priv.h:89
PRIVATE amxrt_t * amxrt_get(void)
Definition: amxrt.c:297
#define WHITE
Definition: amxrt_priv.h:92
#define RED
Definition: amxrt_priv.h:91
#define COLOR_BRIGHT_WHITE
Definition: amxrt_priv.h:85
#define RESET
Definition: amxrt_priv.h:95
#define YELLOW
Definition: amxrt_priv.h:94
#define COLOR_BRIGHT_CYAN
Definition: amxrt_priv.h:84
#define GREEN
Definition: amxrt_priv.h:90
#define CYAN
Definition: amxrt_priv.h:93
#define COLOR_BRIGHT_BLUE
Definition: amxrt_priv.h:82
#define c(x)
Definition: amxrt_priv.h:97
void amxrt_print_error(const char *fmt,...)
static void amxrt_print_config_example(void)
const char * get_color(uint32_t cc)
void amxrt_print_message(const char *fmt,...)
static void amxrt_print_notes(void)
static const char * colors[]
void amxrt_print_configuration(void)
static void amxrt_cmd_line_arg(const char so, const char *lo, const char *args, const char *description)
void amxrt_print_help(void)
void amxrt_print_failure(amxo_parser_t *parser, const char *string)
void amxrt_print_usage(void)
Prints the usage information and all available options to stdout.
amxc_var_t * amxrt_get_config(void)
Gets the htable variant containing the configuration options.
Definition: amxrt.c:301
const char * arg_doc
Definition: amxrt_priv.h:114
const char * long_option
Definition: amxrt_priv.h:110
const char * doc
Definition: amxrt_priv.h:113
amxc_llist_t cmd_line_args
Definition: amxrt_priv.h:102
amxo_parser_t parser
Definition: amxrt_priv.h:101
const char * usage_doc
Definition: amxrt_priv.h:105
config
Definition: test.odl:54