1 /* c_init.c --
2
3 This file is part of the lzop file compressor.
4
5 Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer
6 All Rights Reserved.
7
8 lzop and the LZO library are free software; you can redistribute them
9 and/or modify them under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING.
20 If not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
25 http://www.oberhumer.com/opensource/lzop/
26 */
27
28
29 #include "conf.h"
30
31 #if defined(USE_CONSOLE)
32
33 /*************************************************************************
34 //
35 **************************************************************************/
36
37 static console_t * const me = &console_init;
38 console_t * con = &console_init;
39
40 int con_mode = CON_INIT;
41
42
try_init(console_t * c,FILE * f)43 static void try_init(console_t *c, FILE *f)
44 {
45 int k;
46
47 assert(c);
48 assert(c->init);
49 k = c->init(f,opt_console,con_mode);
50 if (k == CON_INIT)
51 return;
52 #if 0
53 if (con_mode != CON_INIT && opt_console != CON_INIT)
54 if (k != opt_console)
55 return;
56 #endif
57 if (k > con_mode)
58 {
59 con_mode = k;
60 con = c;
61 con->init = 0;
62 if (!con->set_fg)
63 con->set_fg = console_none.set_fg;
64 if (!con->print0)
65 con->print0 = console_none.print0;
66 if (!con->intro)
67 con->intro = console_none.intro;
68 }
69 }
70
71
do_init(FILE * f)72 static int do_init(FILE *f)
73 {
74 assert(con_mode == CON_INIT);
75
76 try_init(&console_none,f);
77 assert(con != me);
78 assert(con == &console_none);
79 assert(con_mode == CON_NONE);
80 if (opt_console == CON_NONE)
81 return con_mode;
82 if (!acc_isatty(STDIN_FILENO) || !acc_isatty(STDOUT_FILENO) || !acc_isatty(STDERR_FILENO))
83 return con_mode;
84
85 #if defined(USE_ANSI)
86 try_init(&console_ansi_mono,f);
87 try_init(&console_ansi_color,f);
88 #endif
89 #if defined(USE_SCREEN)
90 try_init(&console_screen,f);
91 #endif
92 #if defined(USE_AALIB)
93 try_init(&console_aalib,f);
94 #endif
95
96 return con_mode;
97 }
98
99
100 /*************************************************************************
101 //
102 **************************************************************************/
103
init(FILE * f,int o,int now)104 static int init(FILE *f, int o, int now)
105 {
106 if (con != me)
107 return con_mode;
108 assert(o == -1);
109 assert(now == -1);
110 UNUSED(o);
111 UNUSED(now);
112 return do_init(f);
113 }
114
115
set_fg(FILE * f,int fg)116 static int set_fg(FILE *f, int fg)
117 {
118 if (con == me)
119 init(f,-1,-1);
120 assert(con != me);
121 return con->set_fg(f,fg);
122 }
123
124
intro(FILE * f)125 static lzo_bool intro(FILE *f)
126 {
127 if (con == me)
128 init(f,-1,-1);
129 assert(con != me);
130 return con->intro(f);
131 }
132
133
134 console_t console_init =
135 {
136 init,
137 set_fg,
138 0,
139 intro
140 };
141
142
con_fprintf(FILE * f,const char * format,...)143 void con_fprintf(FILE *f, const char *format, ...)
144 {
145 va_list args;
146 char s[80*25];
147
148 va_start(args,format);
149 #if defined(HAVE_VSNPRINTF)
150 vsnprintf(s,sizeof(s),format,args);
151 #else
152 vsprintf(s,format,args);
153 #endif
154 s[sizeof(s)-1] = 0;
155 va_end(args);
156
157 if (con == me)
158 init(f,-1,-1);
159 assert(con != me);
160 con->print0(f,s);
161 }
162
163 #endif /* USE_CONSOLE */
164
165
166 /* vim:set ts=4 sw=4 et: */
167