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"
|
2012-03-13 21:22:53 +00:00
|
|
|
#include "color.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
|
|
|
|
2012-08-01 23:32:52 +00:00
|
|
|
#ifdef _
|
2012-11-19 00:30:30 +00:00
|
|
|
#undef _
|
2012-08-01 23:32:52 +00:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
2012-11-19 00:30:30 +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
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
0,
|
|
|
|
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
|
|
|
|
2005-10-24 17:51:08 +00:00
|
|
|
void print_colors()
|
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
size_t i;
|
|
|
|
for (i=0; i<COLORS; i++)
|
|
|
|
{
|
|
|
|
printf("%s\n", col[i]);
|
|
|
|
}
|
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
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
static int is_init = 0;
|
|
|
|
if (is_init)
|
|
|
|
return;
|
|
|
|
|
|
|
|
is_init = 1;
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE_NAME, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE_NAME);
|
2006-01-23 17:37:20 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 21:22:53 +00:00
|
|
|
/* A lot of this code is taken straight from output.cpp; it sure would be nice to factor these together. */
|
|
|
|
|
|
|
|
static bool support_term256;
|
2012-11-19 00:30:30 +00:00
|
|
|
static bool output_get_supports_term256(void)
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
return support_term256;
|
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
static bool term256_support_is_native(void)
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
/* Return YES if we think the term256 support is "native" as opposed to forced. */
|
|
|
|
return max_colors == 256;
|
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
static bool write_color(char *todo, unsigned char idx, bool is_fg)
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
bool result = false;
|
2012-11-19 00:30:30 +00:00
|
|
|
if (idx < 16 || term256_support_is_native())
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
/* Use tparm */
|
2012-11-19 00:30:30 +00:00
|
|
|
putp(tparm(todo, idx));
|
2012-03-13 21:22:53 +00:00
|
|
|
result = true;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
/* We are attempting to bypass the term here. Generate the ANSI escape sequence ourself. */
|
|
|
|
char stridx[128];
|
|
|
|
format_long_safe(stridx, (long)idx);
|
|
|
|
char buff[128] = "\x1b[";
|
|
|
|
strcat(buff, is_fg ? "38;5;" : "48;5;");
|
|
|
|
strcat(buff, stridx);
|
|
|
|
strcat(buff, "m");
|
|
|
|
write_loop(STDOUT_FILENO, buff, strlen(buff));
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
static bool write_foreground_color(unsigned char idx)
|
|
|
|
{
|
|
|
|
if (set_a_foreground && set_a_foreground[0])
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
return write_color(set_a_foreground, idx, true);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
else if (set_foreground && set_foreground[0])
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
return write_color(set_foreground, idx, true);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
static bool write_background_color(unsigned char idx)
|
|
|
|
{
|
|
|
|
if (set_a_background && set_a_background[0])
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
return write_color(set_a_background, idx, false);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
else if (set_background && set_background[0])
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
return write_color(set_background, idx, false);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
static unsigned char index_for_color(rgb_color_t c)
|
|
|
|
{
|
|
|
|
if (c.is_named() || ! output_get_supports_term256())
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
return c.to_name_index();
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
return c.to_term256_index();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-23 17:37:20 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
int main(int argc, char **argv)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-10-17 08:25:21 +00:00
|
|
|
/* Some code passes variables to set_color that don't exist, like $fish_user_whatever. As a hack, quietly return failure. */
|
|
|
|
if (argc <= 1)
|
|
|
|
return EXIT_FAILURE;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
char *bgcolor=0;
|
|
|
|
char *fgcolor=0;
|
|
|
|
bool bold=false;
|
|
|
|
bool underline=false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
static struct option
|
|
|
|
long_options[] =
|
2012-11-18 10:23:22 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
{
|
|
|
|
"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
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
int opt_index = 0;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
int opt = getopt_long(argc,
|
|
|
|
argv,
|
|
|
|
GETOPT_STRING,
|
|
|
|
long_options,
|
|
|
|
&opt_index);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
if (opt == -1)
|
|
|
|
break;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
switch (opt)
|
|
|
|
{
|
2012-11-19 08:31:03 +00:00
|
|
|
case 0:
|
|
|
|
break;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 08:31:03 +00:00
|
|
|
case 'b':
|
|
|
|
bgcolor = optarg;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
print_help(argv[0], 1);
|
|
|
|
exit(0);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 08:31:03 +00:00
|
|
|
case 'o':
|
|
|
|
bold=true;
|
|
|
|
break;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 08:31:03 +00:00
|
|
|
case 'u':
|
|
|
|
underline=true;
|
|
|
|
break;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 08:31:03 +00:00
|
|
|
case 'v':
|
|
|
|
check_locale_init();
|
|
|
|
fprintf(stderr, _("%s, version %s\n"), SET_COLOR, PACKAGE_VERSION);
|
|
|
|
exit(0);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 08:31:03 +00:00
|
|
|
case 'c':
|
|
|
|
print_colors();
|
|
|
|
exit(0);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 08:31:03 +00:00
|
|
|
case '?':
|
|
|
|
return 1;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
switch (argc-optind)
|
|
|
|
{
|
2012-11-19 08:31:03 +00:00
|
|
|
case 0:
|
2012-11-18 10:23:22 +00:00
|
|
|
// printf( "no fg\n" );
|
2012-11-19 08:31:03 +00:00
|
|
|
break;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 08:31:03 +00:00
|
|
|
case 1:
|
|
|
|
fgcolor=argv[optind];
|
2012-11-18 10:23:22 +00:00
|
|
|
// printf( "fg %s\n", fgcolor );
|
2012-11-19 08:31:03 +00:00
|
|
|
break;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 08:31:03 +00:00
|
|
|
default:
|
|
|
|
check_locale_init();
|
|
|
|
printf(_("%s: Too many arguments\n"), SET_COLOR);
|
|
|
|
return 1;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-03-13 21:22:53 +00:00
|
|
|
/* Infer term256 support */
|
|
|
|
char *fish_term256 = getenv("fish_term256");
|
2012-11-19 00:30:30 +00:00
|
|
|
if (fish_term256)
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
support_term256 = from_string<bool>(fish_term256);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
const char *term = getenv("TERM");
|
|
|
|
support_term256 = term && strstr(term, "256color");
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
if (!fgcolor && !bgcolor && !bold && !underline)
|
|
|
|
{
|
|
|
|
check_locale_init();
|
|
|
|
fprintf(stderr, _("%s: Expected an argument\n"), SET_COLOR);
|
|
|
|
print_help(argv[0], 2);
|
|
|
|
return 1;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-03-13 21:22:53 +00:00
|
|
|
rgb_color_t fg = rgb_color_t(fgcolor ? fgcolor : "");
|
2012-11-19 00:30:30 +00:00
|
|
|
if (fgcolor && fg.is_none())
|
|
|
|
{
|
|
|
|
check_locale_init();
|
|
|
|
fprintf(stderr, _("%s: Unknown color '%s'\n"), SET_COLOR, fgcolor);
|
|
|
|
return 1;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-03-13 21:22:53 +00:00
|
|
|
rgb_color_t bg = rgb_color_t(bgcolor ? bgcolor : "");
|
2012-11-19 00:30:30 +00:00
|
|
|
if (bgcolor && bg.is_none())
|
2012-11-18 10:23:22 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
check_locale_init();
|
|
|
|
fprintf(stderr, _("%s: Unknown color '%s'\n"), SET_COLOR, bgcolor);
|
|
|
|
return 1;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
setupterm(0, STDOUT_FILENO, 0);
|
|
|
|
|
|
|
|
if (bold)
|
2012-11-18 10:23:22 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
if (enter_bold_mode)
|
|
|
|
putp(enter_bold_mode);
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
|
|
|
if (underline)
|
|
|
|
{
|
|
|
|
if (enter_underline_mode)
|
|
|
|
putp(enter_underline_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bgcolor)
|
2012-11-18 10:23:22 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
if (bg.is_normal())
|
|
|
|
{
|
|
|
|
write_background_color(0);
|
|
|
|
putp(tparm(exit_attribute_mode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fgcolor)
|
|
|
|
{
|
|
|
|
if (fg.is_normal())
|
|
|
|
{
|
|
|
|
write_foreground_color(0);
|
|
|
|
putp(tparm(exit_attribute_mode));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
write_foreground_color(index_for_color(fg));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
if (bgcolor)
|
2012-11-18 10:23:22 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
if (! bg.is_normal())
|
|
|
|
{
|
2012-03-13 21:22:53 +00:00
|
|
|
write_background_color(index_for_color(bg));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2006-06-15 13:49:15 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
if (del_curterm(cur_term) == ERR)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s", _("Error while closing terminfo"));
|
|
|
|
}
|
2006-06-15 13:49:15 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|