showrgb.c revision 53354cdb
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(char *filename);
53
54int
55main (int argc, char *argv[])
56{
57    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 (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( (char *)&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 (filename)
119    char *filename;
120{
121    FILE *rgb;
122    char *path;
123    char line[BUFSIZ];
124    char name[BUFSIZ];
125    int lineno = 0;
126    int red, green, blue;
127
128#ifdef __UNIXOS2__
129    char *root = (char*)getenv("X11ROOT");
130    sprintf(line,"%s%s.txt",root,filename);
131    path = (char *)malloc(strlen(line) + 1);
132    strcpy(path,line);
133#else
134    path = (char *)malloc(strlen(filename) + 5);
135    strcpy(path, filename);
136    strcat(path, ".txt");
137#endif
138
139    if (!(rgb = fopen(path, "r"))) {
140	fprintf (stderr, "%s:  unable to open rgb database \"%s\"\n",
141		 ProgramName, filename);
142	free(path);
143	exit (1);
144    }
145
146    while(fgets(line, sizeof(line), rgb)) {
147	lineno++;
148#ifndef __UNIXOS2__
149	if (sscanf(line, "%d %d %d %[^\n]\n", &red, &green, &blue, name) == 4) {
150#else
151	if (sscanf(line, "%d %d %d %[^\n\r]\n", &red, &green, &blue, name) == 4) {
152#endif
153	    if (red >= 0 && red <= 0xff &&
154		green >= 0 && green <= 0xff &&
155		blue >= 0 && blue <= 0xff) {
156		printf ("%3u %3u %3u\t\t%s\n", red, green, blue, name);
157	    } else {
158		fprintf(stderr, "%s:  value for \"%s\" out of range: %s:%d\n",
159		        ProgramName, name, path, lineno);
160	    }
161	} else if (*line && *line != '!') {
162	    fprintf(stderr, "%s:  syntax error: %s:%d\n", ProgramName,
163		    path, lineno);
164	}
165    }
166
167    free(path);
168    fclose(rgb);
169}
170
171#endif /* USE_RGB_TXT */
172