summaryrefslogtreecommitdiff
path: root/src/c_init.c
blob: 510d20e6e51cd185b278d3132b5f628bc988910a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* c_init.c --

   This file is part of the lzop file compressor.

   Copyright (C) 1996-2010 Markus Franz Xaver Johannes Oberhumer
   All Rights Reserved.

   lzop and the LZO library are free software; you can redistribute them
   and/or modify them under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.
   If not, write to the Free Software Foundation, Inc.,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   Markus F.X.J. Oberhumer
   <markus@oberhumer.com>
   http://www.oberhumer.com/opensource/lzop/
 */


#include "conf.h"

#if defined(USE_CONSOLE)

/*************************************************************************
//
**************************************************************************/

static console_t * const me = &console_init;
console_t * con = &console_init;

int con_mode = CON_INIT;


static void try_init(console_t *c, FILE *f)
{
    int k;

    assert(c);
    assert(c->init);
    k = c->init(f,opt_console,con_mode);
    if (k == CON_INIT)
        return;
#if 0
    if (con_mode != CON_INIT && opt_console != CON_INIT)
        if (k != opt_console)
            return;
#endif
    if (k > con_mode)
    {
        con_mode = k;
        con = c;
        con->init = 0;
        if (!con->set_fg)
            con->set_fg = console_none.set_fg;
        if (!con->print0)
            con->print0 = console_none.print0;
        if (!con->intro)
            con->intro = console_none.intro;
    }
}


static int do_init(FILE *f)
{
    assert(con_mode == CON_INIT);

    try_init(&console_none,f);
    assert(con != me);
    assert(con == &console_none);
    assert(con_mode == CON_NONE);
    if (opt_console == CON_NONE)
        return con_mode;
    if (!acc_isatty(STDIN_FILENO) || !acc_isatty(STDOUT_FILENO) || !acc_isatty(STDERR_FILENO))
        return con_mode;

#if defined(USE_ANSI)
    try_init(&console_ansi_mono,f);
    try_init(&console_ansi_color,f);
#endif
#if defined(USE_SCREEN)
    try_init(&console_screen,f);
#endif
#if defined(USE_AALIB)
    try_init(&console_aalib,f);
#endif

    return con_mode;
}


/*************************************************************************
//
**************************************************************************/

static int init(FILE *f, int o, int now)
{
    if (con != me)
        return con_mode;
    assert(o == -1);
    assert(now == -1);
    UNUSED(o);
    UNUSED(now);
    return do_init(f);
}


static int set_fg(FILE *f, int fg)
{
    if (con == me)
        init(f,-1,-1);
    assert(con != me);
    return con->set_fg(f,fg);
}


static lzo_bool intro(FILE *f)
{
    if (con == me)
        init(f,-1,-1);
    assert(con != me);
    return con->intro(f);
}


console_t console_init =
{
    init,
    set_fg,
    0,
    intro
};


void con_fprintf(FILE *f, const char *format, ...)
{
    va_list args;
    char s[80*25];

    va_start(args,format);
#if defined(HAVE_VSNPRINTF)
    vsnprintf(s,sizeof(s),format,args);
#else
    vsprintf(s,format,args);
#endif
    s[sizeof(s)-1] = 0;
    va_end(args);

    if (con == me)
        init(f,-1,-1);
    assert(con != me);
    con->print0(f,s);
}

#endif /* USE_CONSOLE */


/*
vi:ts=4:et
*/