1/*
2 *
3Copyright 1989, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24 *
25 * Author:  Jim Fulton, MIT X Consortium
26 */
27
28#ifdef HAVE_CONFIG_H
29# include "config.h"
30#endif
31
32#ifndef USE_RGB_TXT
33#include DBM_HEADER
34#ifndef NDBM
35#define dbm_open(name,flags,mode) (!dbminit(name))
36#define dbm_firstkey(db) (firstkey())
37#define dbm_fetch(db,key) (fetch(key))
38#define dbm_close(db) dbmclose()
39#endif
40#endif /* USE_RGB_TXT */
41
42#undef NULL
43#include <stdio.h>
44#include <X11/Xos.h>
45#include <stdlib.h>
46#ifndef USE_RGB_TXT
47#include "rgb.h"			/* off in server/include/ */
48#endif
49#include <X11/Xfuncs.h>
50
51static char *ProgramName;
52static void dumprgb(const char *filename);
53
54int
55main (int argc, char *argv[])
56{
57    const char *dbname = RGB_DB;
58
59    ProgramName = argv[0];
60    if (argc == 2)
61	dbname = argv[1];
62
63    dumprgb (dbname);
64    exit (0);
65}
66
67#ifndef USE_RGB_TXT
68static void
69dumprgb (const char *filename)
70{
71#ifdef NDBM
72    DBM *rgb_dbm;
73#else
74    int rgb_dbm;
75#endif
76    datum key;
77
78    rgb_dbm = dbm_open (filename, O_RDONLY, 0);
79    if (!rgb_dbm) {
80	fprintf (stderr, "%s:  unable to open rgb database \"%s\"\n",
81		 ProgramName, filename);
82	exit (1);
83    }
84
85#ifndef NDBM
86#define dbm_nextkey(db) (nextkey(key))	/* need variable called key */
87#endif
88
89    for (key = dbm_firstkey(rgb_dbm); key.dptr != NULL;
90	 key = dbm_nextkey(rgb_dbm)) {
91	datum value;
92
93	value = dbm_fetch(rgb_dbm, key);
94	if (value.dptr) {
95	    RGB rgb;
96	    unsigned short r, g, b;
97	    memcpy(&rgb, value.dptr, sizeof rgb);
98#define N(x) (((x) >> 8) & 0xff)
99	    r = N(rgb.red);
100	    g = N(rgb.green);
101	    b = N(rgb.blue);
102#undef N
103	    printf ("%3u %3u %3u\t\t", r, g, b);
104	    fwrite (key.dptr, 1, key.dsize, stdout);
105	    putchar ('\n');
106	} else {
107	    fprintf (stderr, "%s:  no value found for key \"", ProgramName);
108	    fwrite (key.dptr, 1, key.dsize, stderr);
109	    fprintf (stderr, "\"\n");
110	}
111    }
112
113    dbm_close (rgb_dbm);
114}
115
116#else /* USE_RGB_TXT */
117static void
118dumprgb (const char *filename)
119{
120    FILE *rgb;
121    char *path;
122    char line[BUFSIZ];
123    char name[BUFSIZ];
124    int lineno = 0;
125    int red, green, blue;
126
127#ifdef HAVE_ASPRINTF
128    if (asprintf(&path, "%s.txt", filename) == -1) {
129        perror (ProgramName);
130        exit (1);
131    }
132#else
133    path = malloc(strlen(filename) + 5);
134    strcpy(path, filename);
135    strcat(path, ".txt");
136#endif
137
138    if (!(rgb = fopen(path, "r")) && !(rgb = fopen(filename, "r"))) {
139	fprintf (stderr, "%s:  unable to open rgb database \"%s\"\n",
140		 ProgramName, filename);
141	free(path);
142	exit (1);
143    }
144
145    while(fgets(line, sizeof(line), rgb)) {
146	lineno++;
147	if (sscanf(line, "%d %d %d %[^\n]\n", &red, &green, &blue, name) == 4) {
148	    if (red >= 0 && red <= 0xff &&
149		green >= 0 && green <= 0xff &&
150		blue >= 0 && blue <= 0xff) {
151		printf ("%3u %3u %3u\t\t%s\n", red, green, blue, name);
152	    } else {
153		fprintf(stderr, "%s:  value for \"%s\" out of range: %s:%d\n",
154		        ProgramName, name, path, lineno);
155	    }
156	} else if (*line && *line != '!') {
157	    fprintf(stderr, "%s:  syntax error: %s:%d\n", ProgramName,
158		    path, lineno);
159	}
160    }
161
162    free(path);
163    fclose(rgb);
164}
165
166#endif /* USE_RGB_TXT */
167