View Single Post
  #2   (View Single Post)  
Old 20th October 2013
LeFrettchen's Avatar
LeFrettchen LeFrettchen is offline
Marveled user
 
Join Date: Aug 2012
Location: France
Posts: 408
Default

Oops, sorry, the above code isn't the good one.

Just have checked, and this can't work, cause the set_default_colors function is not declared

So, here is the working one :

Code:
/*   OSC : Open Shell Color  */
/* Thiery SAMPY - 07/04/2013 */

#include <stdio.h>
#include <getopt.h>

struct color_maps
{
	char *nom;
	int  tColor;
	int  bgColor;
};

static struct color_maps color_map[] =
{
	{"BLACK", 30, 40},
	{"RED", 31, 41},
	{"GREEN", 32, 42},
	{"YELLOW", 33, 43},
	{"BLUE", 34, 44},
	{"MAGENTA", 35, 45},
	{"CYAN", 36, 46},
	{"WHITE", 37, 47}
};



void uppercase(char *sPtr)
{  
    while (*sPtr != '\0')
    {
         *sPtr++ = toupper(*sPtr);
    }
}



void set_colors(int text_color,int background_color)
{
	printf("\033[0;%i;%im", text_color, background_color);
}

void set_default_colors()
{
	set_colors(37, 40);
}



void help()
{
	printf("usage : osc [ -h | -H | -? ] [text_color] [background_color]\n");
	printf("possible colors : black, red, green, yellow, blue, magenta, cyan, white\n");
}



int main(int argc, char **argv)
{
	int gsc_option;
	int count_argc;
	int background_color,text_color;
	
	while ((gsc_option = getopt(argc, argv, "DHhd?")) != -1)
	{
		switch (gsc_option)
		{
			case '?':
				help();
				break;
			case 'h':
				help();
				break;
			case 'H':
				help();
				break;
			default:
				set_default_colors();
		}
	}
	
	 // default colors
	background_color = 40; // black
	text_color       = 37;     // white
	
	if (optind < argc)
	{
		char *arg;
		int  i;
		
		count_argc = 0;
		
		while (optind < argc)
		{
			count_argc++;
			uppercase(arg = argv[optind++]);
			
			i = 0;
			while (i < 8)
			{
				if (strcmp(arg, color_map[i].nom) == 0)
				{
					if (count_argc == 1)
						text_color = color_map[i].tColor;
					else background_color = color_map[i].bgColor;
				}
				i++;
			}
		}
		
		set_colors(text_color, background_color);
	}
	else set_default_colors();
	
	return 0;
}
__________________
ThinkPad W500 P8700 6GB HD3650 - faultry
ThinkStation P700 2x2620v3 32GB 1050ti 3xSSD 1xHDD
Reply With Quote