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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <locale.h>
#if HAVE_NCURSES_H
#include <ncurses.h>
#else
#include <curses.h>
#endif
#if HAVE_TERMIO_H
#include <termio.h>
#endif
#if HAVE_TERM_H
#include <term.h>
#elif HAVE_NCURSES_TERM_H
#include <ncurses/term.h>
#endif
#include <errno.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#if HAVE_LIBINTL_H
#include <libintl.h>
#endif
#include "fallback.h"
/*
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 *))
/**
Program name
*/
#define SET_COLOR "set_color"
/**
Getopt short switches for set_color
*/
#define GETOPT_STRING "b:hvocu"
#define _(string) gettext(string)
char *col[]=
{
"black",
"red",
"green",
"brown",
"yellow",
"blue",
"magenta",
"purple",
"cyan",
"white",
"normal"
}
;
int col_idx[]=
{
0,
1,
2,
3,
3,
4,
5,
5,
6,
7,
8
}
;
void print_help();
int translate_color( char *str )
{
char *endptr;
int color;
if( !str )
return -1;
color = strtol( str, &endptr, 10 );
if(endptr<=str)
{
int i;
color = -1;
for( i=0; i<COLORS; i++ )
{
if( strcasecmp( col[i], str ) == 0 )
{
color = col_idx[i];
break;
}
}
}
return color;
}
void print_colors()
{
int i;
for( i=0; i<COLORS; i++ )
{
printf( "%s\n", col[i] );
}
}
static void check_locale_init()
{
static int is_init = 0;
if( is_init )
return;
is_init = 1;
setlocale( LC_ALL, "" );
bindtextdomain( PACKAGE_NAME, LOCALEDIR );
textdomain( PACKAGE_NAME );
}
int main( int argc, char **argv )
{
char *bgcolor=0;
char *fgcolor=0;
int fg, bg;
int bold=0;
int underline=0;
char *bg_seq, *fg_seq;
while( 1 )
{
static struct option
long_options[] =
{
{
"background", required_argument, 0, 'b'
}
,
{
"help", no_argument, 0, 'h'
}
,
{
"bold", no_argument, 0, 'o'
}
,
{
"underline", no_argument, 0, 'u'
}
,
{
"version", no_argument, 0, 'v'
}
,
{
"print-colors", no_argument, 0, 'c'
}
,
{
0, 0, 0, 0
}
}
;
int opt_index = 0;
int opt = getopt_long( argc,
argv,
GETOPT_STRING,
long_options,
&opt_index );
if( opt == -1 )
break;
switch( opt )
{
case 0:
break;
case 'b':
bgcolor = optarg;
break;
case 'h':
print_help();
exit(0);
case 'o':
bold=1;
break;
case 'u':
underline=1;
break;
case 'v':
check_locale_init();
fprintf( stderr, _("%s, version %s\n"), SET_COLOR, PACKAGE_VERSION );
exit( 0 );
case 'c':
print_colors();
exit(0);
case '?':
return 1;
}
}
switch( argc-optind)
{
case 0:
// printf( "no fg\n" );
break;
case 1:
fgcolor=argv[optind];
// printf( "fg %s\n", fgcolor );
break;
default:
check_locale_init();
printf( _("%s: Too many arguments\n"), SET_COLOR );
return 1;
}
if( !fgcolor && !bgcolor && !bold && !underline )
{
check_locale_init();
fprintf( stderr, _("%s: Expected an argument\n"), SET_COLOR );
print_help();
return 1;
}
fg = translate_color(fgcolor);
if( fgcolor && (fg==-1))
{
check_locale_init();
fprintf( stderr, _("%s: Unknown color '%s'\n"), SET_COLOR, fgcolor );
return 1;
}
bg = translate_color(bgcolor);
if( bgcolor && (bg==-1))
{
check_locale_init();
fprintf( stderr, _("%s: Unknown color '%s'\n"), SET_COLOR, bgcolor );
return 1;
}
setupterm( 0, STDOUT_FILENO, 0);
fg_seq = set_a_foreground?set_a_foreground:set_foreground;
bg_seq = set_a_background?set_a_background:set_background;
if( bold )
{
if( enter_bold_mode )
putp( enter_bold_mode );
}
if( underline )
{
if( enter_underline_mode )
putp( enter_underline_mode );
}
if( bgcolor )
{
if( bg == 8 )
{
if( bg_seq )
putp( tparm( bg_seq, 0) );
putp( tparm(exit_attribute_mode) );
}
}
if( fgcolor )
{
if( fg == 8 )
{
if( fg_seq )
putp( tparm( fg_seq, 0) );
putp( tparm(exit_attribute_mode) );
}
else
{
if( fg_seq )
putp( tparm( fg_seq, fg) );
}
}
if( bgcolor )
{
if( bg != 8 )
{
if( bg_seq )
putp( tparm( bg_seq, bg) );
}
}
if( del_curterm( cur_term ) == ERR )
{
fprintf( stderr, _("Error while closing terminfo") );
}
}
|