153354cdbSmrg/*
253354cdbSmrg
353354cdbSmrgCopyright 1985, 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
1253354cdbSmrgin all copies or substantial portions of the Software.
1353354cdbSmrg
1453354cdbSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1553354cdbSmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1653354cdbSmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
1753354cdbSmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
1853354cdbSmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1953354cdbSmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2053354cdbSmrgOTHER DEALINGS IN THE SOFTWARE.
2153354cdbSmrg
2253354cdbSmrgExcept as contained in this notice, the name of The Open Group shall
2353354cdbSmrgnot be used in advertising or otherwise to promote the sale, use or
2453354cdbSmrgother dealings in this Software without prior written authorization
2553354cdbSmrgfrom The Open Group.
2653354cdbSmrg
2753354cdbSmrg*/
2853354cdbSmrg
2953354cdbSmrg
3053354cdbSmrg/* reads from standard input lines of the form:
3153354cdbSmrg	red green blue name
3253354cdbSmrg   where red/green/blue are decimal values, and inserts them in a database.
3353354cdbSmrg */
3453354cdbSmrg
3553354cdbSmrg#ifdef HAVE_CONFIG_H
3653354cdbSmrg# include "config.h"
3753354cdbSmrg#endif
3853354cdbSmrg
3953354cdbSmrg#include DBM_HEADER
4053354cdbSmrg#ifndef NDBM
4153354cdbSmrg#define dbm_open(name,flags,mode) (!dbminit(name))
4253354cdbSmrg#define dbm_store(db,key,content,flags) (store(key,content))
4353354cdbSmrg#define dbm_close(db) dbmclose()
4453354cdbSmrg#endif
4553354cdbSmrg
4653354cdbSmrg#undef NULL
4753354cdbSmrg#include <stdio.h>
4853354cdbSmrg#include <stdlib.h>
4953354cdbSmrg#include <X11/Xos.h>
5053354cdbSmrg#include "rgb.h"
5153354cdbSmrg#include <ctype.h>
5253354cdbSmrg
5353354cdbSmrg#include <errno.h>
5453354cdbSmrg
5553354cdbSmrgstatic char *ProgramName;
5653354cdbSmrg
5753354cdbSmrgint
5853354cdbSmrgmain(int argc, char **argv)
5953354cdbSmrg{
60afb14e9bSmrg    const char *dbname;
6153354cdbSmrg    char line[512];
6253354cdbSmrg    int red, green, blue;
6353354cdbSmrg    RGB rgb;
6453354cdbSmrg    datum key, content;
6553354cdbSmrg    char name[512];
6653354cdbSmrg    int items;
6753354cdbSmrg    int lineno;
6853354cdbSmrg    int i, n;
6953354cdbSmrg    int fd;
7053354cdbSmrg#ifdef NDBM
7153354cdbSmrg    DBM *rgb_dbm;
7253354cdbSmrg#else
7353354cdbSmrg    int rgb_dbm;
7453354cdbSmrg#endif
7553354cdbSmrg
7653354cdbSmrg    ProgramName = argv[0];
7753354cdbSmrg
7853354cdbSmrg    if (argc == 2)
7953354cdbSmrg	dbname = argv[1];
8053354cdbSmrg    else
8153354cdbSmrg	dbname = RGB_DB;
8253354cdbSmrg
83afb14e9bSmrg    if (strlen(dbname) > (sizeof(name) - 5)) {
84afb14e9bSmrg        fprintf (stderr,
85afb14e9bSmrg		 "%s:  dbm file name too long: \"%s\" (%lu max allowed)\n",
86afb14e9bSmrg		 ProgramName, dbname, sizeof(name) - 5);
87afb14e9bSmrg	exit (1);
88afb14e9bSmrg    }
89afb14e9bSmrg
9061c899d4Smrg    snprintf (name, sizeof(name), "%s.dir", dbname);
9153354cdbSmrg    fd = open (name, O_WRONLY|O_CREAT, 0666);
9253354cdbSmrg    if (fd < 0) {
9353354cdbSmrg	fprintf (stderr,
9453354cdbSmrg		 "%s:  unable to create dbm file \"%s\" (error %d, %s)\n",
9553354cdbSmrg		 ProgramName, name, errno, strerror(errno));
9653354cdbSmrg	exit (1);
9753354cdbSmrg    }
9853354cdbSmrg    (void) close (fd);
9953354cdbSmrg
10061c899d4Smrg    snprintf (name, sizeof(name), "%s.pag", dbname);
10153354cdbSmrg    fd = open (name, O_WRONLY|O_CREAT, 0666);
10253354cdbSmrg    if (fd < 0) {
10353354cdbSmrg	fprintf (stderr,
10453354cdbSmrg		 "%s:  unable to create dbm file \"%s\" (error %d, %s)\n",
10553354cdbSmrg		 ProgramName, name, errno, strerror(errno));
10653354cdbSmrg	exit (1);
10753354cdbSmrg    }
10853354cdbSmrg    (void) close (fd);
10953354cdbSmrg
11053354cdbSmrg    rgb_dbm = dbm_open (dbname, O_RDWR|O_CREAT, 0666);
11153354cdbSmrg    if (!rgb_dbm) {
11253354cdbSmrg	fprintf (stderr,
11353354cdbSmrg		 "%s:  unable to open dbm database \"%s\" (error %d, %s)\n",
11453354cdbSmrg		 ProgramName, dbname, errno, strerror(errno));
11553354cdbSmrg	exit (1);
11653354cdbSmrg    }
11753354cdbSmrg
11853354cdbSmrg    key.dptr = name;
11953354cdbSmrg    content.dptr = (char *) &rgb;
12053354cdbSmrg    content.dsize = sizeof (rgb);
12153354cdbSmrg    lineno = 0;
12253354cdbSmrg    while (fgets (line, sizeof (line), stdin)) {
12353354cdbSmrg	lineno++;
12453354cdbSmrg	if (line[0] == '!')
12553354cdbSmrg	    continue;
12653354cdbSmrg	items = sscanf (line, "%d %d %d %[^\n]\n", &red, &green, &blue, name);
12753354cdbSmrg	if (items != 4) {
12853354cdbSmrg	    fprintf (stderr, "syntax error on line %d\n", lineno);
12953354cdbSmrg	    fflush (stderr);
13053354cdbSmrg	    continue;
13153354cdbSmrg	}
13253354cdbSmrg	if (red < 0 || red > 0xff ||
13353354cdbSmrg	    green < 0 || green > 0xff ||
13453354cdbSmrg	    blue < 0 || blue > 0xff) {
13553354cdbSmrg	    fprintf (stderr, "value for %s out of range\n", name);
13653354cdbSmrg	    fflush (stderr);
13753354cdbSmrg	    continue;
13853354cdbSmrg	}
13953354cdbSmrg	n = strlen (name);
14053354cdbSmrg	for (i = 0; i < n; i++) {
14153354cdbSmrg	    if (isupper (name[i]))
14253354cdbSmrg		name[i] = tolower (name[i]);
14353354cdbSmrg	}
14453354cdbSmrg	key.dsize = n;
14553354cdbSmrg	rgb.red = (red * 65535) / 255;
14653354cdbSmrg	rgb.green = (green * 65535) / 255;
14753354cdbSmrg	rgb.blue = (blue * 65535) / 255;
14853354cdbSmrg	if (dbm_store (rgb_dbm, key, content, DBM_REPLACE)) {
14953354cdbSmrg	    fprintf (stderr, "%s:  store of entry \"%s\" failed\n",
15053354cdbSmrg		     ProgramName, name);
15153354cdbSmrg	    fflush (stderr);
15253354cdbSmrg	}
15353354cdbSmrg    }
15453354cdbSmrg
15553354cdbSmrg    dbm_close(rgb_dbm);
15653354cdbSmrg
15753354cdbSmrg    exit(0);
15853354cdbSmrg}
159