153354cdbSmrg/*
253354cdbSmrg *
353354cdbSmrgCopyright 1989, 1998  The Open Group
453354cdbSmrg
553354cdbSmrgPermission to use, copy, modify, distribute, and sell this software and its
653354cdbSmrgdocumentation for any purpose is hereby granted without fee, provided that
753354cdbSmrgthe above copyright notice appear in all copies and that both that
853354cdbSmrgcopyright notice and this permission notice appear in supporting
953354cdbSmrgdocumentation.
1053354cdbSmrg
1153354cdbSmrgThe above copyright notice and this permission notice shall be included in
1253354cdbSmrgall copies or substantial portions of the Software.
1353354cdbSmrg
1453354cdbSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1553354cdbSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1653354cdbSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1753354cdbSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1853354cdbSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1953354cdbSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2053354cdbSmrg
2153354cdbSmrgExcept as contained in this notice, the name of The Open Group shall not be
2253354cdbSmrgused in advertising or otherwise to promote the sale, use or other dealings
2353354cdbSmrgin this Software without prior written authorization from The Open Group.
2453354cdbSmrg *
2553354cdbSmrg * Author:  Jim Fulton, MIT X Consortium
2653354cdbSmrg */
2753354cdbSmrg
2853354cdbSmrg#ifdef HAVE_CONFIG_H
2953354cdbSmrg# include "config.h"
3053354cdbSmrg#endif
3153354cdbSmrg
3253354cdbSmrg#ifndef USE_RGB_TXT
3353354cdbSmrg#include DBM_HEADER
3453354cdbSmrg#ifndef NDBM
3553354cdbSmrg#define dbm_open(name,flags,mode) (!dbminit(name))
3653354cdbSmrg#define dbm_firstkey(db) (firstkey())
3753354cdbSmrg#define dbm_fetch(db,key) (fetch(key))
3853354cdbSmrg#define dbm_close(db) dbmclose()
3953354cdbSmrg#endif
4053354cdbSmrg#endif /* USE_RGB_TXT */
4153354cdbSmrg
4253354cdbSmrg#undef NULL
4353354cdbSmrg#include <stdio.h>
4453354cdbSmrg#include <X11/Xos.h>
4553354cdbSmrg#include <stdlib.h>
4653354cdbSmrg#ifndef USE_RGB_TXT
4753354cdbSmrg#include "rgb.h"			/* off in server/include/ */
4853354cdbSmrg#endif
4953354cdbSmrg#include <X11/Xfuncs.h>
5053354cdbSmrg
5153354cdbSmrgstatic char *ProgramName;
5261c899d4Smrgstatic void dumprgb(const char *filename);
5353354cdbSmrg
5453354cdbSmrgint
5553354cdbSmrgmain (int argc, char *argv[])
5653354cdbSmrg{
5761c899d4Smrg    const char *dbname = RGB_DB;
5853354cdbSmrg
5953354cdbSmrg    ProgramName = argv[0];
6053354cdbSmrg    if (argc == 2)
6153354cdbSmrg	dbname = argv[1];
6253354cdbSmrg
6353354cdbSmrg    dumprgb (dbname);
6453354cdbSmrg    exit (0);
6553354cdbSmrg}
6653354cdbSmrg
6753354cdbSmrg#ifndef USE_RGB_TXT
6853354cdbSmrgstatic void
6961c899d4Smrgdumprgb (const char *filename)
7053354cdbSmrg{
7153354cdbSmrg#ifdef NDBM
7253354cdbSmrg    DBM *rgb_dbm;
7353354cdbSmrg#else
7453354cdbSmrg    int rgb_dbm;
7553354cdbSmrg#endif
7653354cdbSmrg    datum key;
7753354cdbSmrg
7853354cdbSmrg    rgb_dbm = dbm_open (filename, O_RDONLY, 0);
7953354cdbSmrg    if (!rgb_dbm) {
8053354cdbSmrg	fprintf (stderr, "%s:  unable to open rgb database \"%s\"\n",
8153354cdbSmrg		 ProgramName, filename);
8253354cdbSmrg	exit (1);
8353354cdbSmrg    }
8453354cdbSmrg
8553354cdbSmrg#ifndef NDBM
8653354cdbSmrg#define dbm_nextkey(db) (nextkey(key))	/* need variable called key */
8753354cdbSmrg#endif
8853354cdbSmrg
8953354cdbSmrg    for (key = dbm_firstkey(rgb_dbm); key.dptr != NULL;
9053354cdbSmrg	 key = dbm_nextkey(rgb_dbm)) {
9153354cdbSmrg	datum value;
9253354cdbSmrg
9353354cdbSmrg	value = dbm_fetch(rgb_dbm, key);
9453354cdbSmrg	if (value.dptr) {
9553354cdbSmrg	    RGB rgb;
9653354cdbSmrg	    unsigned short r, g, b;
97afb14e9bSmrg	    memcpy(&rgb, value.dptr, sizeof rgb);
9853354cdbSmrg#define N(x) (((x) >> 8) & 0xff)
9953354cdbSmrg	    r = N(rgb.red);
10053354cdbSmrg	    g = N(rgb.green);
10153354cdbSmrg	    b = N(rgb.blue);
10253354cdbSmrg#undef N
10353354cdbSmrg	    printf ("%3u %3u %3u\t\t", r, g, b);
10453354cdbSmrg	    fwrite (key.dptr, 1, key.dsize, stdout);
10553354cdbSmrg	    putchar ('\n');
10653354cdbSmrg	} else {
10753354cdbSmrg	    fprintf (stderr, "%s:  no value found for key \"", ProgramName);
10853354cdbSmrg	    fwrite (key.dptr, 1, key.dsize, stderr);
10953354cdbSmrg	    fprintf (stderr, "\"\n");
11053354cdbSmrg	}
11153354cdbSmrg    }
11253354cdbSmrg
11353354cdbSmrg    dbm_close (rgb_dbm);
11453354cdbSmrg}
11553354cdbSmrg
11653354cdbSmrg#else /* USE_RGB_TXT */
11753354cdbSmrgstatic void
11861c899d4Smrgdumprgb (const char *filename)
11953354cdbSmrg{
12053354cdbSmrg    FILE *rgb;
12153354cdbSmrg    char *path;
12253354cdbSmrg    char line[BUFSIZ];
12353354cdbSmrg    char name[BUFSIZ];
12453354cdbSmrg    int lineno = 0;
12553354cdbSmrg    int red, green, blue;
12653354cdbSmrg
127ecb85c42Smrg#ifdef HAVE_ASPRINTF
128ecb85c42Smrg    if (asprintf(&path, "%s.txt", filename) == -1) {
129ecb85c42Smrg        perror (ProgramName);
130ecb85c42Smrg        exit (1);
131ecb85c42Smrg    }
132ecb85c42Smrg#else
133afb14e9bSmrg    path = malloc(strlen(filename) + 5);
13453354cdbSmrg    strcpy(path, filename);
13553354cdbSmrg    strcat(path, ".txt");
136ecb85c42Smrg#endif
13753354cdbSmrg
138ecb85c42Smrg    if (!(rgb = fopen(path, "r")) && !(rgb = fopen(filename, "r"))) {
13953354cdbSmrg	fprintf (stderr, "%s:  unable to open rgb database \"%s\"\n",
14053354cdbSmrg		 ProgramName, filename);
14153354cdbSmrg	free(path);
14253354cdbSmrg	exit (1);
14353354cdbSmrg    }
14453354cdbSmrg
14553354cdbSmrg    while(fgets(line, sizeof(line), rgb)) {
14653354cdbSmrg	lineno++;
14753354cdbSmrg	if (sscanf(line, "%d %d %d %[^\n]\n", &red, &green, &blue, name) == 4) {
14853354cdbSmrg	    if (red >= 0 && red <= 0xff &&
14953354cdbSmrg		green >= 0 && green <= 0xff &&
15053354cdbSmrg		blue >= 0 && blue <= 0xff) {
15153354cdbSmrg		printf ("%3u %3u %3u\t\t%s\n", red, green, blue, name);
15253354cdbSmrg	    } else {
15353354cdbSmrg		fprintf(stderr, "%s:  value for \"%s\" out of range: %s:%d\n",
15453354cdbSmrg		        ProgramName, name, path, lineno);
15553354cdbSmrg	    }
15653354cdbSmrg	} else if (*line && *line != '!') {
15753354cdbSmrg	    fprintf(stderr, "%s:  syntax error: %s:%d\n", ProgramName,
15853354cdbSmrg		    path, lineno);
15953354cdbSmrg	}
16053354cdbSmrg    }
16153354cdbSmrg
16253354cdbSmrg    free(path);
16353354cdbSmrg    fclose(rgb);
16453354cdbSmrg}
16553354cdbSmrg
16653354cdbSmrg#endif /* USE_RGB_TXT */
167