on a177213 help sign in
87 lines 3.1 kB history raw
1#include <stdarg.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
6/* a shared library designed to be LD_PRELOADed into SQLite CLI
8If PRYBAR_QUIET is set in the environment, then this suppresses the "Loading
9resources" message due to the CLI's `-init` flag, and also hides the version
10header and help message.
12What happens in SQLite CLI:
141. fprintf to stderr containing the "Loading resources..." message
152. printf containing version and help string
17We need to suppress the output if the PRYBAR_QUIET env var is set. So, we wait
18for the CLI to make calls to libc print functions and intercept them. If they're
19what we expect, then we don't print anything and then advance the state as
20necessary.
22Check out the SQLite CLI code to see what it does:
23https://www.sqlite.org/src/artifact/4e1bcf8c70b8fb97
25*/
27// this macro lets us roll up the varargs once instead of in four separate
28// places below.
29#define VAR_PRINT(out, format) \
30 { \
31 va_list arg; \
32 va_start(arg, format); \
33 int rc = vfprintf(out, format, arg); \
34 va_end(arg); \
35 return rc; \
36 }
38/* three states:
39 - no output suppressed
40 - suppressed "Loading resources..." message on stderr
41 - suppressed version and help string on stdout
42*/
43static enum {
44 INITIAL,
45 LOADING_SUPPRESSED,
46 VERSION_SUPPRESSED
47} current_state = INITIAL;
49int __fprintf_chk(FILE *out, int flag, const char *format, ...) {
50 // don't do anything; pass through print
51 if (current_state != INITIAL) {
52 VAR_PRINT(out, format);
53 }
55 // check if this is the first time we're seeing the output that we're aiming
56 // to suppress
57 const char *expected = "-- Loading resources from %s\n";
58 if (getenv("PRYBAR_QUIET") && out == stderr &&
59 strcmp(format, expected) == 0) {
60 // advance to next state and suppress output
61 current_state = LOADING_SUPPRESSED;
62 return 0;
63 }
65 // this isn't the output we were looking for...
66 VAR_PRINT(out, format);
69int __printf_chk(int flag, const char *format, ...) {
70 // don't do anything; pass through print
71 if (current_state != LOADING_SUPPRESSED) {
72 VAR_PRINT(stdout, format);
73 }
75 // check if this is the first time we're seeing the output that we're aiming
76 // to suppress
77 const char *expected = "SQLite version %s %.19s\nEnter \".help\" for usage "
78 "hints.\n";
79 if (getenv("PRYBAR_QUIET") && strcmp(format, expected) == 0) {
80 // advance to next state and suppress output
81 current_state = VERSION_SUPPRESSED;
82 return 0;
83 }
85 // this isn't the output we were looking for...
86 VAR_PRINT(stdout, format);