forked from jj1bdx/dbskkd-cdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbskkd-cdb.c
187 lines (174 loc) · 5.17 KB
/
dbskkd-cdb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* dbskkd-cdb.c
* SKK dictionary Server
* with Daniel J. Bernstein's cdb database
*/
/*
* MIT License (MIT)
* Copyright © 2016 Kenji Rikitake <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sysexits.h>
/* tinycdb required */
#include "cdb.h"
#ifdef USE_TCPD
# include <tcpd.h>
#endif
/* dbskkd dictionary file name */
/* default value is for FreeBSD port japanese/ddskk */
#ifndef JISYO_FILE
#define JISYO_FILE "/usr/local/share/skk/SKK-JISYO.L.cdb"
#endif /* JISYO_FILE */
/* max size of a request */
#define BUFSIZE (1024)
/* max size of a result */
#define DATASIZE (4096)
#define CLIENT_END '0'
#define CLIENT_REQUEST '1'
#define CLIENT_VERSION '2'
#define CLIENT_HOST '3'
#define SERVER_FOUND '1'
#define SERVER_NOT_FOUND '4'
/* these strings must be terminated with space */
#define VERSION "dbskkd-cdb-3.00 "
#define DUMMYHOSTNAME "novalue: "
/* main program */
int main(int argc, char *argv[]) {
char combuf[BUFSIZE], data[DATASIZE];
char *pbuf, *key, *p;
struct cdb diccdb;
int dicfd, ex, length;
unsigned int keylen, datalen;
#ifdef USE_TCPD
struct request_info request;
request_init(&request, RQ_DAEMON, argv[0], RQ_FILE, STDIN_FILENO, 0);
fromhost(&request);
if (!hosts_access(&request))
refuse(&request);
#endif
/* open dictionary cdb file */
if ((dicfd = open(JISYO_FILE, O_RDONLY, S_IRUSR)) < 0) {
exit(EX_CONFIG);
}
/* init cdb */
cdb_init(&diccdb, dicfd);
/* To exit, set ex to non-zero */
ex = 0;
/* command loop */
while (!ex) {
/* Read from stdin */
length = read(STDIN_FILENO, &combuf[0], BUFSIZE - 1);
if (length < 0) {
exit(EX_NOINPUT);
} else if (length == 0) {
/* EOF detected */
/* exit from the while loop */
ex = 1;
break;
}
/* parse request code */
switch (combuf[0]) {
case CLIENT_END:
/* End of conversion requested*/
/* exit from the while loop */
ex = 1;
break;
case CLIENT_VERSION:
if (write(STDOUT_FILENO, VERSION, sizeof(VERSION) - 1) < 0) {
exit(EX_IOERR);
}
break;
case CLIENT_HOST:
if (write(STDOUT_FILENO, DUMMYHOSTNAME, sizeof(DUMMYHOSTNAME) - 1) < 0) {
exit(EX_IOERR);
}
break;
case CLIENT_REQUEST:
/* get size of key */
key = &combuf[1];
for (pbuf = &combuf[1]; *pbuf != ' ' && pbuf != &combuf[length - 1];
pbuf++) {
}
keylen = pbuf - &combuf[1];
if (keylen <= 0) { /* invalid keysize */
exit(EX_PROTOCOL);
}
/* lookup the cdb database */
switch (cdb_find(&diccdb, key, keylen)) {
case -1: /* fatal error on cdb_find() */
exit(EX_SOFTWARE);
break;
case 1: /* found */
if ((datalen = cdb_datalen(&diccdb)) >= DATASIZE - 2) {
exit(EX_PROTOCOL);
}
/* generate the answer string */
p = data;
*p++ = SERVER_FOUND;
if (cdb_read(&diccdb, p, datalen, cdb_datapos(&diccdb)) < 0) {
exit(EX_SOFTWARE);
}
p += datalen;
*p = '\n';
/* sending found code and the result data string with LF */
if (write(STDOUT_FILENO, data, datalen + 2) < 0) {
exit(EX_IOERR);
}
break;
case 0: /* NOT found */
/* generate the answer string */
combuf[0] = SERVER_NOT_FOUND;
*pbuf = '\n';
/* sending error code and the key string with LF */
/* this action is required by skkinput */
if (write(STDOUT_FILENO, combuf, keylen + 2) < 0) {
exit(EX_IOERR);
}
break;
default: /* unknown cdb return value */
exit(EX_SOFTWARE);
break;
}
break;
/* end CLIENT_REQUEST switch clause */
/* unknown request code */
default:
exit(EX_PROTOCOL);
}
/* end request code parsing */
}
/* end while loop */
/* close dictionary db file */
cdb_free(&diccdb);
if (close(dicfd) != 0) {
exit(EX_IOERR);
}
/* normal exit, finally */
exit(EX_OK);
}
/* end of program */