2005-09-20 13:26:39 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2006-01-23 17:37:20 +00:00
|
|
|
#include <locale.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
#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 <errno.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
|
|
|
|
2006-01-23 17:37:20 +00:00
|
|
|
#if HAVE_LIBINTL_H
|
|
|
|
#include <libintl.h>
|
|
|
|
#endif
|
|
|
|
|
2006-06-15 13:49:15 +00:00
|
|
|
#include "fallback.h"
|
2006-11-17 16:24:38 +00:00
|
|
|
#include "print_help.h"
|
2006-06-15 13:49:15 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
|
|
|
Small utility for setting the color.
|
|
|
|
Usage: set_color COLOR
|
|
|
|
where COLOR is either an integer from 0 to seven or one of the strings in the col array.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define COLORS (sizeof(col)/sizeof(char *))
|
|
|
|
|
2006-01-23 17:37:20 +00:00
|
|
|
/**
|
|
|
|
Program name
|
|
|
|
*/
|
|
|
|
#define SET_COLOR "set_color"
|
|
|
|
|
2006-05-26 14:55:51 +00:00
|
|
|
/**
|
|
|
|
Getopt short switches for set_color
|
|
|
|
*/
|
2006-06-15 13:49:15 +00:00
|
|
|
#define GETOPT_STRING "b:hvocu"
|
2006-05-26 14:55:51 +00:00
|
|
|
|
2006-11-29 14:18:22 +00:00
|
|
|
#ifdef USE_GETTEXT
|
2006-01-23 17:37:20 +00:00
|
|
|
#define _(string) gettext(string)
|
2006-11-29 14:18:22 +00:00
|
|
|
#else
|
|
|
|
#define _(string) (string)
|
|
|
|
#endif
|
2006-01-23 17:37:20 +00:00
|
|
|
|
2012-01-15 06:32:45 +00:00
|
|
|
const char *col[]=
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
"black",
|
|
|
|
"red",
|
|
|
|
"green",
|
|
|
|
"brown",
|
|
|
|
"yellow",
|
|
|
|
"blue",
|
|
|
|
"magenta",
|
|
|
|
"purple",
|
|
|
|
"cyan",
|
|
|
|
"white",
|
|
|
|
"normal"
|
2012-02-12 01:07:56 +00:00
|
|
|
};
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-01-15 06:32:45 +00:00
|
|
|
const int col_idx[]=
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
0,
|
2005-09-20 13:26:39 +00:00
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
5,
|
|
|
|
5,
|
|
|
|
6,
|
|
|
|
7,
|
|
|
|
8
|
2012-02-12 01:07:56 +00:00
|
|
|
};
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
int translate_color( char *str )
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
int color;
|
|
|
|
|
|
|
|
if( !str )
|
|
|
|
return -1;
|
|
|
|
|
2007-01-09 13:41:17 +00:00
|
|
|
errno = 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
color = strtol( str, &endptr, 10 );
|
2007-01-09 13:41:17 +00:00
|
|
|
|
|
|
|
if( *endptr || color<0 || errno )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-01-15 06:32:45 +00:00
|
|
|
size_t i;
|
2005-09-20 13:26:39 +00:00
|
|
|
color = -1;
|
|
|
|
for( i=0; i<COLORS; i++ )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( strcasecmp( col[i], str ) == 0 )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
color = col_idx[i];
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
return color;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2005-10-24 17:51:08 +00:00
|
|
|
void print_colors()
|
|
|
|
{
|
2012-01-15 06:32:45 +00:00
|
|
|
size_t i;
|
2005-10-24 17:51:08 +00:00
|
|
|
for( i=0; i<COLORS; i++ )
|
|
|
|
{
|
|
|
|
printf( "%s\n", col[i] );
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2005-10-24 17:51:08 +00:00
|
|
|
}
|
|
|
|
|
2006-02-04 11:42:56 +00:00
|
|
|
static void check_locale_init()
|
2006-01-23 17:37:20 +00:00
|
|
|
{
|
2006-02-04 11:42:56 +00:00
|
|
|
static int is_init = 0;
|
|
|
|
if( is_init )
|
|
|
|
return;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-02-04 11:42:56 +00:00
|
|
|
is_init = 1;
|
2006-01-23 17:37:20 +00:00
|
|
|
setlocale( LC_ALL, "" );
|
|
|
|
bindtextdomain( PACKAGE_NAME, LOCALEDIR );
|
|
|
|
textdomain( PACKAGE_NAME );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
char *bgcolor=0;
|
|
|
|
char *fgcolor=0;
|
|
|
|
int fg, bg;
|
2005-09-24 01:28:34 +00:00
|
|
|
int bold=0;
|
2006-06-15 13:49:15 +00:00
|
|
|
int underline=0;
|
|
|
|
char *bg_seq, *fg_seq;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
static struct option
|
|
|
|
long_options[] =
|
|
|
|
{
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
"background", required_argument, 0, 'b'
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
"help", no_argument, 0, 'h'
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
,
|
2005-09-24 01:28:34 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
"bold", no_argument, 0, 'o'
|
2005-09-24 01:28:34 +00:00
|
|
|
}
|
|
|
|
,
|
2006-06-15 13:49:15 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
"underline", no_argument, 0, 'u'
|
2006-06-15 13:49:15 +00:00
|
|
|
}
|
|
|
|
,
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
"version", no_argument, 0, 'v'
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
,
|
2005-10-24 17:51:08 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
"print-colors", no_argument, 0, 'c'
|
2005-10-24 17:51:08 +00:00
|
|
|
}
|
|
|
|
,
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
0, 0, 0, 0
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
int opt_index = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
int opt = getopt_long( argc,
|
2011-12-27 03:18:46 +00:00
|
|
|
argv,
|
|
|
|
GETOPT_STRING,
|
|
|
|
long_options,
|
2005-09-20 13:26:39 +00:00
|
|
|
&opt_index );
|
2006-08-28 11:43:05 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( opt == -1 )
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
switch( opt )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
case 'b':
|
2005-09-20 13:26:39 +00:00
|
|
|
bgcolor = optarg;
|
|
|
|
break;
|
|
|
|
case 'h':
|
2006-11-17 16:24:38 +00:00
|
|
|
print_help( argv[0], 1 );
|
2011-12-27 03:18:46 +00:00
|
|
|
exit(0);
|
|
|
|
|
2005-09-24 01:28:34 +00:00
|
|
|
case 'o':
|
|
|
|
bold=1;
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-06-15 13:49:15 +00:00
|
|
|
case 'u':
|
|
|
|
underline=1;
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case 'v':
|
2006-02-04 11:42:56 +00:00
|
|
|
check_locale_init();
|
2006-01-23 17:37:20 +00:00
|
|
|
fprintf( stderr, _("%s, version %s\n"), SET_COLOR, PACKAGE_VERSION );
|
2011-12-27 03:18:46 +00:00
|
|
|
exit( 0 );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-10-24 17:51:08 +00:00
|
|
|
case 'c':
|
|
|
|
print_colors();
|
|
|
|
exit(0);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case '?':
|
|
|
|
return 1;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
switch( argc-optind)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// printf( "no fg\n" );
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case 1:
|
|
|
|
fgcolor=argv[optind];
|
|
|
|
// printf( "fg %s\n", fgcolor );
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
default:
|
2006-02-04 11:42:56 +00:00
|
|
|
check_locale_init();
|
2006-01-23 17:37:20 +00:00
|
|
|
printf( _("%s: Too many arguments\n"), SET_COLOR );
|
2005-09-20 13:26:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-06-15 13:49:15 +00:00
|
|
|
if( !fgcolor && !bgcolor && !bold && !underline )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-02-04 11:42:56 +00:00
|
|
|
check_locale_init();
|
2006-01-23 17:37:20 +00:00
|
|
|
fprintf( stderr, _("%s: Expected an argument\n"), SET_COLOR );
|
2011-12-27 03:18:46 +00:00
|
|
|
print_help( argv[0], 2 );
|
2005-09-20 13:26:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
fg = translate_color(fgcolor);
|
|
|
|
if( fgcolor && (fg==-1))
|
|
|
|
{
|
2006-02-04 11:42:56 +00:00
|
|
|
check_locale_init();
|
2006-01-23 17:37:20 +00:00
|
|
|
fprintf( stderr, _("%s: Unknown color '%s'\n"), SET_COLOR, fgcolor );
|
2011-12-27 03:18:46 +00:00
|
|
|
return 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bg = translate_color(bgcolor);
|
|
|
|
if( bgcolor && (bg==-1))
|
|
|
|
{
|
2006-02-04 11:42:56 +00:00
|
|
|
check_locale_init();
|
2006-01-23 17:37:20 +00:00
|
|
|
fprintf( stderr, _("%s: Unknown color '%s'\n"), SET_COLOR, bgcolor );
|
2011-12-27 03:18:46 +00:00
|
|
|
return 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setupterm( 0, STDOUT_FILENO, 0);
|
2005-09-24 01:28:34 +00:00
|
|
|
|
2006-06-15 13:49:15 +00:00
|
|
|
fg_seq = set_a_foreground?set_a_foreground:set_foreground;
|
|
|
|
bg_seq = set_a_background?set_a_background:set_background;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-06-15 13:49:15 +00:00
|
|
|
|
2005-09-24 01:28:34 +00:00
|
|
|
if( bold )
|
|
|
|
{
|
2006-06-15 13:49:15 +00:00
|
|
|
if( enter_bold_mode )
|
|
|
|
putp( enter_bold_mode );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( underline )
|
|
|
|
{
|
|
|
|
if( enter_underline_mode )
|
|
|
|
putp( enter_underline_mode );
|
2005-09-24 01:28:34 +00:00
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( bgcolor )
|
|
|
|
{
|
|
|
|
if( bg == 8 )
|
|
|
|
{
|
2006-06-15 13:49:15 +00:00
|
|
|
if( bg_seq )
|
|
|
|
putp( tparm( bg_seq, 0) );
|
2011-12-27 03:18:46 +00:00
|
|
|
putp( tparm(exit_attribute_mode) );
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( fgcolor )
|
|
|
|
{
|
|
|
|
if( fg == 8 )
|
|
|
|
{
|
2006-06-15 13:49:15 +00:00
|
|
|
if( fg_seq )
|
|
|
|
putp( tparm( fg_seq, 0) );
|
2011-12-27 03:18:46 +00:00
|
|
|
putp( tparm(exit_attribute_mode) );
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
else
|
|
|
|
{
|
2006-06-15 13:49:15 +00:00
|
|
|
if( fg_seq )
|
|
|
|
putp( tparm( fg_seq, fg) );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( bgcolor )
|
|
|
|
{
|
|
|
|
if( bg != 8 )
|
|
|
|
{
|
2006-06-15 13:49:15 +00:00
|
|
|
if( bg_seq )
|
|
|
|
putp( tparm( bg_seq, bg) );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-15 13:49:15 +00:00
|
|
|
|
2006-08-13 23:05:02 +00:00
|
|
|
if( del_curterm( cur_term ) == ERR )
|
|
|
|
{
|
2009-12-03 11:01:59 +00:00
|
|
|
fprintf( stderr, "%s", _("Error while closing terminfo") );
|
2006-08-13 23:05:02 +00:00
|
|
|
}
|
2006-06-15 13:49:15 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|