-
Notifications
You must be signed in to change notification settings - Fork 0
/
cprint.c
195 lines (167 loc) · 4.7 KB
/
cprint.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#ifndef CPRINT_STREAM
# define CPRINT_STREAM stdout
#endif
#ifndef LUA_QL
# define LUA_QL(s) "'" s "'"
#endif
#define w( s, n ) (fwrite( (s), sizeof( char ), (n), CPRINT_STREAM ))
#define tab() (putc( '\t', CPRINT_STREAM ))
#define nl() (putc( '\n', CPRINT_STREAM ), fflush( CPRINT_STREAM ))
/* Try to figure out if we are a posix or windows environment */
#if defined( _WIN32 ) || defined( _WIN64 ) || defined( __WIN32__ ) || \
defined( __TOS_WIN__ ) || defined( __WINDOWS__ )
/* windows */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <io.h>
static int color_mapping[] = {
0, /* black */
4, /* red */
2, /* green */
6, /* yellow */
1, /* blue */
5, /* magenta/purple */
3, /* cyan/aqua */
7, /* white */
0, 0 /* unused */
};
typedef struct {
int enabled;
HANDLE hConsole;
int last_fg;
int last_bg;
int last_md;
} cprint_info;
static void cprint_init( cprint_info* info ) {
int fd = _fileno( CPRINT_STREAM );
info->last_fg = 7;
info->last_bg = 0;
info->last_md = 0;
info->enabled = _isatty( fd );
info->hConsole = (HANDLE)_get_osfhandle( fd );
if( info->hConsole == INVALID_HANDLE_VALUE )
info->enabled = 0;
}
static void cprint_doansi( cprint_info* info,
char const* esc, size_t n ) {
if( info->enabled ) {
char const* p = esc;
int high, inverse;
p += 2; /* skip ESC and [ */
while( *p != 'm' ) {
if( *p == '3' && '0' <= p[1] && p[1] <= '9' ) {
int fg = p[1] - '0';
if( fg > 7 )
fg = 7; /* default foreground */
info->last_fg = color_mapping[ fg ];
p += 2;
} else if( *p == '4' && '0' <= p[1] && p[1] <= '9' ) {
int bg = p[1] - '0';
if( bg > 7 )
bg = 0; /* default background */
info->last_bg = color_mapping[ bg ];
p += 2;
} else if( *p != ';' ) {
switch( *p ) {
case '0':
if( p == esc+2 ) {
info->last_fg = 7;
info->last_bg = 0;
}
info->last_md = 0;
break;
case '1':
info->last_md = 1;
break;
case '7':
info->last_md = 7;
break;
default:
info->last_md = 0;
break;
}
p += 1;
} else
p += 1;
}
high = info->last_md == 1;
inverse = info->last_md == 7;
SetConsoleTextAttribute( info->hConsole,
info->last_fg*(1+inverse*15)+high*8 +
info->last_bg*(1+(!inverse)*15) );
}
}
#elif defined( unix ) || defined( __unix ) || defined( __unix__ ) || \
defined( __TOS_AIX__ ) || defined( _SYSTYPE_BSD )
/* some form of unix */
#include <unistd.h>
#include <stdlib.h>
typedef struct {
int enabled;
} cprint_info;
/* defined in pr_ansi.h */
static int terminal_supported( char const* term );
static void cprint_init( cprint_info* info ) {
char const* term = getenv( "TERM" );
info->enabled = term != NULL && terminal_supported( term ) &&
isatty( fileno( CPRINT_STREAM ) );
}
static void cprint_doansi( cprint_info* info,
char const* esc, size_t n ) {
if( info->enabled )
w( esc, n );
}
#else
/* unknown/unsupported OS */
typedef struct {
int dummy;
} cprint_info;
static void cprint_init( cprint_info* info ) {
info->dummy = 0;
}
static void cprint_doansi( cprint_info* info,
char const* esc, size_t n ) {
(void)info;
(void)esc;
(void)n;
}
#endif
#include "pr_ansi.h"
static int cprint( lua_State* L ) {
cprint_info* info = lua_touserdata( L, lua_upvalueindex( 1 ) );
int top = lua_gettop( L );
int i = 0;
lua_getglobal( L, "tostring" );
for( i = 1; i <= top; ++i ) {
size_t n = 0;
char const* s = NULL;
lua_pushvalue( L, -1 );
lua_pushvalue( L, i );
lua_call( L, 1, 1 );
s = lua_tolstring( L, -1, &n );
if( s == NULL )
return luaL_error( L, LUA_QL("tostring")
" must return a string to "
LUA_QL("print") );
if( i > 1 )
tab();
write_ansi( info, s, n );
lua_pop( L, 1 );
}
nl();
return 0;
}
#ifndef CPRINT_API
# define CPRINT_API
#endif
CPRINT_API int luaopen_cprint( lua_State* L ) {
cprint_info* info = lua_newuserdata( L, sizeof( cprint_info ) );
cprint_init( info );
lua_pushcclosure( L, cprint, 1 );
return 1;
}