Tizen 2.0 Release
[framework/uifw/xorg/util/x11-xserver-utils.git] / rgb / rgb.c
1 /*
2
3 Copyright 1985, 1998  The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of The Open Group shall
23 not be used in advertising or otherwise to promote the sale, use or
24 other dealings in this Software without prior written authorization
25 from 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
55 static char *ProgramName;
56
57 int
58 main(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     strcpy (name, dbname);
84     strcat (name, ".dir");
85     fd = open (name, O_WRONLY|O_CREAT, 0666);
86     if (fd < 0) {
87         fprintf (stderr, 
88                  "%s:  unable to create dbm file \"%s\" (error %d, %s)\n",
89                  ProgramName, name, errno, strerror(errno));
90         exit (1);
91     }
92     (void) close (fd);
93
94     strcpy (name, dbname);
95     strcat (name, ".pag");
96     fd = open (name, O_WRONLY|O_CREAT, 0666);
97     if (fd < 0) {
98         fprintf (stderr, 
99                  "%s:  unable to create dbm file \"%s\" (error %d, %s)\n",
100                  ProgramName, name, errno, strerror(errno));
101         exit (1);
102     }
103     (void) close (fd);
104
105     rgb_dbm = dbm_open (dbname, O_RDWR|O_CREAT, 0666);
106     if (!rgb_dbm) {
107         fprintf (stderr,
108                  "%s:  unable to open dbm database \"%s\" (error %d, %s)\n",
109                  ProgramName, dbname, errno, strerror(errno));
110         exit (1);
111     }
112
113     key.dptr = name;
114     content.dptr = (char *) &rgb;
115     content.dsize = sizeof (rgb);
116     lineno = 0;
117     while (fgets (line, sizeof (line), stdin)) {
118         lineno++;
119         if (line[0] == '!')
120             continue;
121         items = sscanf (line, "%d %d %d %[^\n]\n", &red, &green, &blue, name);
122         if (items != 4) {
123             fprintf (stderr, "syntax error on line %d\n", lineno);
124             fflush (stderr);
125             continue;
126         }
127         if (red < 0 || red > 0xff ||
128             green < 0 || green > 0xff ||
129             blue < 0 || blue > 0xff) {
130             fprintf (stderr, "value for %s out of range\n", name);
131             fflush (stderr);
132             continue;
133         }
134         n = strlen (name);
135         for (i = 0; i < n; i++) {
136             if (isupper (name[i]))
137                 name[i] = tolower (name[i]);
138         }
139         key.dsize = n;
140         rgb.red = (red * 65535) / 255;
141         rgb.green = (green * 65535) / 255;
142         rgb.blue = (blue * 65535) / 255;
143         if (dbm_store (rgb_dbm, key, content, DBM_REPLACE)) {
144             fprintf (stderr, "%s:  store of entry \"%s\" failed\n",
145                      ProgramName, name);
146             fflush (stderr);
147         }
148     }
149
150     dbm_close(rgb_dbm);
151
152     exit(0);
153 }