rgb.c revision 61c899d4
1/*
2
3Copyright 1985, 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
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28
29
30/* reads from standard input lines of the form:
31	red green blue name
32   where red/green/blue are decimal values, and inserts them in a database.
33 */
34
35#ifdef HAVE_CONFIG_H
36# include "config.h"
37#endif
38
39#include DBM_HEADER
40#ifndef NDBM
41#define dbm_open(name,flags,mode) (!dbminit(name))
42#define dbm_store(db,key,content,flags) (store(key,content))
43#define dbm_close(db) dbmclose()
44#endif
45
46#undef NULL
47#include <stdio.h>
48#include <stdlib.h>
49#include <X11/Xos.h>
50#include "rgb.h"
51#include <ctype.h>
52
53#include <errno.h>
54
55static char *ProgramName;
56
57int
58main(int argc, char **argv)
59{
60    char *dbname;
61    char line[512];
62    int red, green, blue;
63    RGB rgb;
64    datum key, content;
65    char name[512];
66    int items;
67    int lineno;
68    int i, n;
69    int fd;
70#ifdef NDBM
71    DBM *rgb_dbm;
72#else
73    int rgb_dbm;
74#endif
75
76    ProgramName = argv[0];
77
78    if (argc == 2)
79	dbname = argv[1];
80    else
81	dbname = RGB_DB;
82
83    snprintf (name, sizeof(name), "%s.dir", dbname);
84    fd = open (name, O_WRONLY|O_CREAT, 0666);
85    if (fd < 0) {
86	fprintf (stderr,
87		 "%s:  unable to create dbm file \"%s\" (error %d, %s)\n",
88		 ProgramName, name, errno, strerror(errno));
89	exit (1);
90    }
91    (void) close (fd);
92
93    snprintf (name, sizeof(name), "%s.pag", dbname);
94    fd = open (name, O_WRONLY|O_CREAT, 0666);
95    if (fd < 0) {
96	fprintf (stderr,
97		 "%s:  unable to create dbm file \"%s\" (error %d, %s)\n",
98		 ProgramName, name, errno, strerror(errno));
99	exit (1);
100    }
101    (void) close (fd);
102
103    rgb_dbm = dbm_open (dbname, O_RDWR|O_CREAT, 0666);
104    if (!rgb_dbm) {
105	fprintf (stderr,
106		 "%s:  unable to open dbm database \"%s\" (error %d, %s)\n",
107		 ProgramName, dbname, errno, strerror(errno));
108	exit (1);
109    }
110
111    key.dptr = name;
112    content.dptr = (char *) &rgb;
113    content.dsize = sizeof (rgb);
114    lineno = 0;
115    while (fgets (line, sizeof (line), stdin)) {
116	lineno++;
117	if (line[0] == '!')
118	    continue;
119	items = sscanf (line, "%d %d %d %[^\n]\n", &red, &green, &blue, name);
120	if (items != 4) {
121	    fprintf (stderr, "syntax error on line %d\n", lineno);
122	    fflush (stderr);
123	    continue;
124	}
125	if (red < 0 || red > 0xff ||
126	    green < 0 || green > 0xff ||
127	    blue < 0 || blue > 0xff) {
128	    fprintf (stderr, "value for %s out of range\n", name);
129	    fflush (stderr);
130	    continue;
131	}
132	n = strlen (name);
133	for (i = 0; i < n; i++) {
134	    if (isupper (name[i]))
135		name[i] = tolower (name[i]);
136	}
137	key.dsize = n;
138	rgb.red = (red * 65535) / 255;
139	rgb.green = (green * 65535) / 255;
140	rgb.blue = (blue * 65535) / 255;
141	if (dbm_store (rgb_dbm, key, content, DBM_REPLACE)) {
142	    fprintf (stderr, "%s:  store of entry \"%s\" failed\n",
143		     ProgramName, name);
144	    fflush (stderr);
145	}
146    }
147
148    dbm_close(rgb_dbm);
149
150    exit(0);
151}
152