[ LUGOS ] xterm mavrica

Damjan Lampret Damjan.Lampret na arnes.si
Sre Apr 1 13:44:00 CEST 1998


Aloha !

Danes sem se odlocil, da si spremenim barvo fontov v xtermu, pa se nisem
mogel odlociti za katero. In sem napisal programcek, ki to naredi namesto
mene (izbere barvo). Program mora teci v backgroundu in v $HOME/.Xdefaults
mora biti vpisana vrstica xterm*foreground neka_barva.

 lp, 
	Damjan

--------------------------------------------------------------------------

/*
 * Programcek se mora startati v ozadje. Poskrbi pa, da se v .Xdefaults
 * spremeni xterm*foreground vrstica, ki opisuje barvo fontov v xtermu. Z 
 * vsakim pognanim xterm-om se spremeni access time .Xdefaults datoteki in
 * v njej popravimo xterm*foreground vrstico. Tako ima vsak xterm svojo barvo.
 *  
 * Barve so opisane v /usr/lib/X11/rgb.txt in jih dodate s pripisom v array
 * nizov 'colors'.
 *
 * Pod GNU General Public License, Damjan Lampret, 1998
 * damjan.lampret na arnes.si
 * 
 * Garancije za ta program ni. Ce vam pobrise disk ali pokonca psa, je to vas
 * problem. Tega programa raje ne uporabljajte na racunalnikih, ki skrbijo
 * za zivljensko pomembne zadeve (nadzor atomskega reaktorja ipd.).
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>

char 	colors[][100] = {"orange", "green", "yellow", "SteelBlue", "#a0c0a0" };
unsigned short int color = 0;
	
int freadln(FILE *infile, char *buf, int maxcount)
{
        char    ch;
        int     count, fe;

        count = 0;
        
        while ((fread(&ch,1,1,infile) != 0) && ((fe=feof(infile)) == 0) && 
               (ch != '\n') &&(count < maxcount))
               buf[count++]=ch;
               
        buf[count] = '\n';
        buf[++count] = '\0';
        
        if (fe != 0) {
        
                buf[0]='\0';
                return(EOF);
                
        }
        return(0);
}

int createfile(char *filename)
{
        FILE    *fd;

        if ((fd = fopen(filename,"w")) != NULL) {
        
                if (fclose(fd) != 0)
                	perror("createfile");
                return(0);
                
        }
        else perror("createfile");
        return(-1);
}

int appendtofile(char *filename, char *addline)
{
        FILE    *fd;

        if ((fd = fopen(filename,"a")) != NULL) {

                fprintf(fd, "%s", addline);
                /*	perror("appendtofile"); */

                if (fclose(fd) != 0)
                	perror("appendtofile");
                	
                return(0);

        }
        else perror("appendtofile");
        return(-1);
}

int xdef_copy(char *filename1, char *filename2)
{
        FILE    *fd;
        char	buf[512];

        if ((fd = fopen(filename1, "r")) != NULL) {
        	
        	createfile(filename2);
		if (++color >= sizeof(colors)/sizeof(colors[0]))
			color = 0;
        	
                while (!feof(fd)) {
                
                	if (freadln(fd, buf, sizeof(buf) - 1 ) == EOF)
                		break;
                	if ((char *)strstr(buf, "xterm*foreground:") != NULL)
                		snprintf(buf, sizeof(buf), "xterm*foreground: %s\n", &colors[color]);
			appendtofile(filename2, buf);
                }
                
                if (fclose(fd) != 0)
			perror("xdef_copy");
		
		if (rename(filename2, filename1) != 0)
			perror("xdef_copy");
                
        }
        else perror("xdef_copy");
        return(-1);
}

void main()
{
	char		xdef_filename[200];
	char		xdef_tmpfile[200];
	struct stat 	buf;
	time_t		old_atime;
	
	
	snprintf(xdef_filename, sizeof(xdef_filename), "%s/.Xdefaults", getenv("HOME"));
	snprintf(xdef_tmpfile, sizeof(xdef_tmpfile), "%s.tmp", xdef_filename);

	while (1) {
	
		sleep(1);	/* vsako sekundo preverimo zadnji dostop */
		old_atime = buf.st_atime;
		if (stat(xdef_filename, &buf) != 0)
			perror("main");

		if (buf.st_atime != old_atime) {
		
			printf("changing xterm font color.\n");
			xdef_copy(xdef_filename, xdef_tmpfile);
			if (stat(xdef_filename, &buf) != 0)
				perror("main");
			old_atime = buf.st_atime;
			
		}
			
	}
	
	/* do sem ne pridemo */
	/* razen, ce je z vaso masino nekaj narobe ;-) */
}




Dodatne informacije o seznamu Starilist