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