2005-10-17 13:24:12 +00:00
|
|
|
/** \file output.c
|
|
|
|
Generic output functions
|
|
|
|
*/
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wctype.h>
|
|
|
|
|
|
|
|
#if HAVE_NCURSES_H
|
|
|
|
#include <ncurses.h>
|
|
|
|
#else
|
|
|
|
#include <curses.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_TERMIO_H
|
|
|
|
#include <termio.h>
|
|
|
|
#endif
|
|
|
|
|
2006-01-19 12:22:07 +00:00
|
|
|
#if HAVE_TERM_H
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <term.h>
|
2006-01-19 12:22:07 +00:00
|
|
|
#elif HAVE_NCURSES_TERM_H
|
|
|
|
#include <ncurses/term.h>
|
|
|
|
#endif
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "wutil.h"
|
|
|
|
#include "expand.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "output.h"
|
2006-02-09 15:50:20 +00:00
|
|
|
#include "halloc_util.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "highlight.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
Number of color names in the col array
|
|
|
|
*/
|
|
|
|
#define COLORS (sizeof(col)/sizeof(wchar_t *))
|
|
|
|
|
2006-02-16 14:21:00 +00:00
|
|
|
static int writeb_internal( char c );
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Names of different colors.
|
|
|
|
*/
|
|
|
|
static wchar_t *col[]=
|
|
|
|
{
|
|
|
|
L"black",
|
|
|
|
L"red",
|
|
|
|
L"green",
|
|
|
|
L"brown",
|
|
|
|
L"yellow",
|
|
|
|
L"blue",
|
|
|
|
L"magenta",
|
|
|
|
L"purple",
|
|
|
|
L"cyan",
|
|
|
|
L"white"
|
|
|
|
L"normal"
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Mapping from color name (the 'col' array) to color index as used in
|
|
|
|
ANSI color terminals, and also the fish_color_* constants defined
|
|
|
|
in highlight.h. Non-ANSI terminals will display the wrong colors,
|
|
|
|
since they use a different mapping.
|
|
|
|
*/
|
|
|
|
static int col_idx[]=
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
5,
|
|
|
|
5,
|
|
|
|
6,
|
|
|
|
7,
|
|
|
|
FISH_COLOR_NORMAL,
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2005-10-17 13:24:12 +00:00
|
|
|
/**
|
|
|
|
Size of writestr_buff
|
|
|
|
*/
|
2005-10-13 14:08:33 +00:00
|
|
|
static size_t writestr_buff_sz=0;
|
2005-10-17 13:24:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Temp buffer used for converting from wide to narrow strings
|
|
|
|
*/
|
2005-10-13 14:08:33 +00:00
|
|
|
static char *writestr_buff = 0;
|
|
|
|
|
2006-02-16 13:40:25 +00:00
|
|
|
/**
|
|
|
|
The function used for output
|
|
|
|
*/
|
|
|
|
|
2006-02-16 14:21:00 +00:00
|
|
|
static int (*out)(char c) = &writeb_internal;
|
2006-02-16 13:40:25 +00:00
|
|
|
|
2006-02-09 15:50:20 +00:00
|
|
|
static void output_destroy()
|
2005-10-13 14:08:33 +00:00
|
|
|
{
|
|
|
|
free( writestr_buff );
|
|
|
|
}
|
|
|
|
|
2006-02-16 14:21:00 +00:00
|
|
|
void output_set_writer( int (*writer)(char) )
|
2006-02-16 13:40:25 +00:00
|
|
|
{
|
|
|
|
out = writer;
|
|
|
|
}
|
|
|
|
|
2005-10-13 14:08:33 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
void set_color( int c, int c2 )
|
|
|
|
{
|
2005-10-17 13:24:12 +00:00
|
|
|
static int last_color = FISH_COLOR_NORMAL;
|
|
|
|
static int last_color2 = FISH_COLOR_NORMAL;
|
2005-09-20 13:26:39 +00:00
|
|
|
int bg_set=0, last_bg_set=0;
|
|
|
|
char *fg = 0, *bg=0;
|
|
|
|
|
|
|
|
if( (set_a_foreground != 0) && (strlen( set_a_foreground) != 0 ) )
|
|
|
|
{
|
|
|
|
fg = set_a_foreground;
|
|
|
|
bg = set_a_background;
|
|
|
|
}
|
|
|
|
else if( (set_foreground != 0) && (strlen( set_foreground) != 0 ) )
|
|
|
|
{
|
|
|
|
fg = set_foreground;
|
|
|
|
bg = set_background;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (c == FISH_COLOR_RESET) || (c2 == FISH_COLOR_RESET))
|
|
|
|
{
|
|
|
|
c = c2 = FISH_COLOR_NORMAL;
|
|
|
|
if( fg )
|
2006-02-20 13:11:46 +00:00
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs( tparm( fg, 0 ) );
|
2006-02-20 13:11:46 +00:00
|
|
|
}
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs( exit_attribute_mode );
|
2005-09-20 13:26:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( last_color2 != FISH_COLOR_NORMAL &&
|
|
|
|
last_color2 != FISH_COLOR_RESET &&
|
|
|
|
last_color2 != FISH_COLOR_IGNORE )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Background was set
|
|
|
|
*/
|
|
|
|
last_bg_set=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( c2 != FISH_COLOR_NORMAL &&
|
|
|
|
c2 != FISH_COLOR_RESET &&
|
|
|
|
c2 != FISH_COLOR_IGNORE )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Background is set
|
|
|
|
*/
|
|
|
|
bg_set=1;
|
|
|
|
c = (c2==FISH_COLOR_WHITE)?FISH_COLOR_BLACK:FISH_COLOR_WHITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0))
|
|
|
|
{
|
|
|
|
if(bg_set && !last_bg_set)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-17 13:24:12 +00:00
|
|
|
Background color changed and is set, so we enter bold
|
|
|
|
mode to make reading easier
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs( enter_bold_mode );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
if(!bg_set && last_bg_set)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-17 13:24:12 +00:00
|
|
|
Background color changed and is no longer set, so we
|
|
|
|
exit bold mode
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs( exit_attribute_mode );
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
|
|
|
We don't know if exit_attribute_mode resets colors, so
|
|
|
|
we set it to something known.
|
|
|
|
*/
|
|
|
|
if( fg )
|
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs( tparm( fg, 0 ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
last_color=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( last_color != c )
|
|
|
|
{
|
|
|
|
if( c==FISH_COLOR_NORMAL )
|
|
|
|
{
|
|
|
|
if( fg )
|
2006-02-20 13:11:46 +00:00
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs( tparm( fg, 0 ) );
|
2006-02-20 13:11:46 +00:00
|
|
|
}
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs( exit_attribute_mode );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
last_color2 = FISH_COLOR_NORMAL;
|
|
|
|
}
|
2006-02-20 13:11:46 +00:00
|
|
|
else if( ( c >= 0 ) && ( c < FISH_COLOR_NORMAL ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
if( fg )
|
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs( tparm( fg, c ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
last_color = c;
|
|
|
|
|
|
|
|
if( last_color2 != c2 )
|
|
|
|
{
|
|
|
|
if( c2 == FISH_COLOR_NORMAL )
|
|
|
|
{
|
|
|
|
if( bg )
|
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs( tparm( bg, 0 ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-02-20 13:11:46 +00:00
|
|
|
writembs( exit_attribute_mode );
|
|
|
|
if( ( last_color != FISH_COLOR_NORMAL ) && fg )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-02-20 13:11:46 +00:00
|
|
|
writembs( tparm( fg, last_color ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
last_color2 = c2;
|
|
|
|
}
|
2006-02-20 13:11:46 +00:00
|
|
|
else if ( ( c2 >= 0 ) && ( c2 < FISH_COLOR_NORMAL ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
if( bg )
|
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs( tparm( bg, c2 ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
last_color2 = c2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-16 13:40:25 +00:00
|
|
|
/**
|
|
|
|
perm_left_cursor and parm_right_cursor don't seem to be properly
|
|
|
|
emulated by many terminal emulators, so we only use plain
|
|
|
|
curor_left, curor_right...
|
|
|
|
*/
|
|
|
|
void move_cursor( int steps )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if( !steps )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( steps < 0 ){
|
|
|
|
for( i=0; i>steps; i--)
|
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs(cursor_left);
|
2006-02-16 13:40:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( i=0; i<steps; i++)
|
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs(cursor_right);
|
2006-02-16 13:40:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-16 14:21:00 +00:00
|
|
|
static int writeb_internal( char c )
|
|
|
|
{
|
|
|
|
write( 1, &c, 1 );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-02-16 13:40:25 +00:00
|
|
|
int writeb( tputs_arg_t b )
|
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
out( b );
|
2006-02-16 13:40:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
int writembs( char *str )
|
|
|
|
{
|
|
|
|
#ifdef TPUTS_KLUDGE
|
2006-02-16 14:21:00 +00:00
|
|
|
while( *str )
|
2006-02-20 13:11:46 +00:00
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
out( *str );
|
2006-02-20 13:11:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
#else
|
2006-02-16 14:21:00 +00:00
|
|
|
tputs(str,1,writeb);
|
2005-09-20 13:26:39 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int writech( wint_t ch )
|
|
|
|
{
|
|
|
|
static mbstate_t out_state;
|
|
|
|
char buff[MB_CUR_MAX];
|
|
|
|
size_t bytes = wcrtomb( buff, ch, &out_state );
|
2006-02-16 14:21:00 +00:00
|
|
|
int i;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-02-16 14:21:00 +00:00
|
|
|
for( i=0; i<bytes; i++ )
|
2006-02-20 13:11:46 +00:00
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
out( buff[i] );
|
2006-02-20 13:11:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writestr( const wchar_t *str )
|
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
char *pos;
|
|
|
|
|
2005-10-15 00:51:26 +00:00
|
|
|
// while( *str )
|
|
|
|
// writech( *str++ );
|
2005-10-17 13:24:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Check amount of needed space
|
|
|
|
*/
|
|
|
|
size_t len = wcstombs( 0, str, 0 );
|
|
|
|
|
|
|
|
if( len == (size_t)-1 )
|
|
|
|
{
|
|
|
|
debug( 1, L"Tried to print invalid wide character string" );
|
|
|
|
return;
|
|
|
|
}
|
2005-10-15 00:51:26 +00:00
|
|
|
|
2005-10-17 13:24:12 +00:00
|
|
|
len++;
|
2005-10-13 14:08:33 +00:00
|
|
|
|
2005-10-17 13:24:12 +00:00
|
|
|
/*
|
|
|
|
Reallocate if needed
|
|
|
|
*/
|
2005-10-13 14:08:33 +00:00
|
|
|
if( writestr_buff_sz < len )
|
|
|
|
{
|
2006-02-09 15:50:20 +00:00
|
|
|
if( !writestr_buff )
|
2006-02-20 13:11:46 +00:00
|
|
|
{
|
2006-02-09 15:50:20 +00:00
|
|
|
halloc_register_function_void( global_context, &output_destroy );
|
2006-02-20 13:11:46 +00:00
|
|
|
}
|
2006-02-09 15:50:20 +00:00
|
|
|
|
2005-10-17 13:24:12 +00:00
|
|
|
writestr_buff = realloc( writestr_buff, len );
|
2005-10-13 14:08:33 +00:00
|
|
|
if( !writestr_buff )
|
2006-02-20 13:11:46 +00:00
|
|
|
{
|
2005-10-13 14:08:33 +00:00
|
|
|
die_mem();
|
2006-02-20 13:11:46 +00:00
|
|
|
}
|
2005-10-13 14:08:33 +00:00
|
|
|
writestr_buff_sz = len;
|
|
|
|
}
|
|
|
|
|
2005-10-17 13:24:12 +00:00
|
|
|
/*
|
|
|
|
Convert
|
|
|
|
*/
|
2005-10-13 14:08:33 +00:00
|
|
|
wcstombs( writestr_buff,
|
|
|
|
str,
|
|
|
|
writestr_buff_sz );
|
|
|
|
|
2005-10-17 13:24:12 +00:00
|
|
|
/*
|
|
|
|
Write
|
|
|
|
*/
|
2006-02-16 14:21:00 +00:00
|
|
|
for( pos = writestr_buff; *pos; pos++ )
|
2006-02-20 13:11:46 +00:00
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
out( *pos );
|
2006-02-20 13:11:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void writestr_ellipsis( const wchar_t *str, int max_width )
|
|
|
|
{
|
|
|
|
int written=0;
|
|
|
|
int tot = my_wcswidth(str);
|
|
|
|
|
|
|
|
if( tot <= max_width )
|
|
|
|
{
|
|
|
|
writestr( str );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while( *str != 0 )
|
|
|
|
{
|
|
|
|
int w = wcwidth( *str );
|
|
|
|
if( written+w+wcwidth( ellipsis_char )>max_width )
|
2006-02-20 13:11:46 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
2006-02-20 13:11:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
written+=w;
|
|
|
|
writech( *(str++) );
|
|
|
|
}
|
|
|
|
|
|
|
|
written += wcwidth( ellipsis_char );
|
|
|
|
writech( ellipsis_char );
|
|
|
|
|
|
|
|
while( written < max_width )
|
|
|
|
{
|
|
|
|
written++;
|
|
|
|
writestr( L" " );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int write_escaped_str( const wchar_t *str, int max_len )
|
|
|
|
{
|
|
|
|
|
2005-10-07 10:36:51 +00:00
|
|
|
wchar_t *out = escape( str, 1 );
|
2005-09-20 13:26:39 +00:00
|
|
|
int i;
|
|
|
|
int len = my_wcswidth( out );
|
|
|
|
int written=0;
|
|
|
|
|
|
|
|
if( max_len && (max_len < len))
|
|
|
|
{
|
|
|
|
for( i=0; (written+wcwidth(out[i]))<=(max_len-1); i++ )
|
|
|
|
{
|
|
|
|
writech( out[i] );
|
|
|
|
written += wcwidth( out[i] );
|
|
|
|
}
|
|
|
|
writech( ellipsis_char );
|
|
|
|
written += wcwidth( ellipsis_char );
|
|
|
|
|
|
|
|
for( i=written; i<max_len; i++ )
|
|
|
|
{
|
|
|
|
writech( L' ' );
|
|
|
|
written++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
written = len;
|
|
|
|
writestr( out );
|
|
|
|
}
|
|
|
|
|
|
|
|
free( out );
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int writespace( int c )
|
|
|
|
{
|
|
|
|
if( repeat_char && strlen(repeat_char) )
|
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
writembs( tparm( repeat_char, ' ', c ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i=0; i<c; i++ )
|
2006-02-20 13:11:46 +00:00
|
|
|
{
|
2006-02-16 14:21:00 +00:00
|
|
|
out( ' ' );
|
2006-02-20 13:11:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int output_color_code( const wchar_t *val )
|
|
|
|
{
|
|
|
|
int i, color=-1;
|
|
|
|
|
|
|
|
for( i=0; i<COLORS; i++ )
|
|
|
|
{
|
|
|
|
if( wcscasecmp( col[i], val ) == 0 )
|
|
|
|
{
|
|
|
|
color = col_idx[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( color >= 0 )
|
2006-02-20 13:11:46 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
return color;
|
2006-02-20 13:11:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
else
|
2006-02-20 13:11:46 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
return FISH_COLOR_NORMAL;
|
2006-02-20 13:11:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|