Debian Codegraph @

src/suggest/util.c

raw symbols SCIP
1 /*
2 Copyright (C) 2010-2012 Brazil
3 Copyright (C) 2022 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <sys/wait.h>
26 #include <sys/queue.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30
31 #include "util.h"
32
33 #define DEFAULT_FREQUENCY_THRESHOLD 100
34 #define DEFAULT_CONDITIONAL_PROBABILITY_THRESHOLD 0.2
35
36 int
37 print_error(const char *format, ...)
38 {
39 int r;
40 va_list l;
41
42 va_start(l, format);
43 vfprintf(stderr, format, l);
44 r = fprintf(stderr, "\n");
45 fflush(stderr);
46 va_end(l);
47
48 return r;
49 }
50
51 int
52 daemonize(void)
53 {
54 pid_t pid;
55
56 switch (fork()) {
57 case 0:
58 break;
59 case -1:
60 print_error("fork failed.");
61 return -1;
62 default:
63 wait(NULL);
64 _exit(0);
65 }
66 switch ((pid = fork())) {
67 case 0:
68 break;
69 case -1:
70 perror("fork");
71 return -1;
72 default:
73 fprintf(stderr, "%d\n", pid);
74 _exit(0);
75 }
76 {
77 int null_fd = open("/dev/null", O_RDWR, 0);
78 if (null_fd != -1) {
79 dup2(null_fd, 0);
80 dup2(null_fd, 1);
81 dup2(null_fd, 2);
82 if (null_fd > 2) { close(null_fd); }
83 }
84 }
85 return 1;
86 }
87
88 static uint64_t
89 atouint64_t(const char *s)
90 {
91 uint64_t r;
92 for (r = 0; *s; s++) {
93 r *= 10;
94 r += (uint64_t)(*s - '0');
95 }
96 return r;
97 }
98
99 void
100 parse_keyval(grn_ctx *ctx,
101 struct evkeyvalq *get_args,
102 const char **query, const char **types,
103 const char **client_id, const char **target_name,
104 const char **learn_target_name,
105 const char **callback,
106 uint64_t *millisec,
107 int *frequency_threshold,
108 double *conditional_probability_threshold,
109 int *limit,
110 grn_obj *pass_through_parameters)
111 {
112 struct evkeyval *get;
113
114 if (query) { *query = NULL; }
115 if (types) { *types = NULL; }
116 if (client_id) { *client_id = NULL; }
117 if (target_name) { *target_name = NULL; }
118 if (learn_target_name) { *learn_target_name = NULL; }
119 if (callback) { *callback = NULL; }
120 if (millisec) { *millisec = 0; }
121 if (frequency_threshold) {
122 *frequency_threshold = DEFAULT_FREQUENCY_THRESHOLD;
123 }
124 if (conditional_probability_threshold) {
125 *conditional_probability_threshold = DEFAULT_CONDITIONAL_PROBABILITY_THRESHOLD;
126 }
127 if (limit) { *limit = -1; }
128
129 TAILQ_FOREACH(get, get_args, next) {
130 bool is_pass_through_parameter = false;
131 size_t key_length;
132
133 key_length = strlen(get->key);
134 switch (key_length) {
135 case 0:
136 break;
137 case 1:
138 switch(get->key[0]) {
139 case 'q':
140 if (query) {
141 *query = get->value;
142 }
143 break;
144 case 't':
145 /* TODO: check types */
146 if (types) {
147 *types = get->value;
148 }
149 break;
150 case 'i':
151 if (client_id) {
152 *client_id = get->value;
153 }
154 break;
155 case 's':
156 if (millisec) {
157 *millisec = atouint64_t(get->value);
158 }
159 break;
160 case 'n':
161 /* TODO: check target_name */
162 if (target_name) {
163 *target_name = get->value;
164 }
165 break;
166 case 'l':
167 if (learn_target_name) {
168 *learn_target_name = get->value;
169 }
170 break;
171 case 'h':
172 if (frequency_threshold) {
173 *frequency_threshold = atoi(get->value);
174 }
175 break;
176 case 'p':
177 if (conditional_probability_threshold) {
178 *conditional_probability_threshold = strtod(get->value, NULL);
179 }
180 break;
181 case 'm':
182 if (limit) {
183 *limit = atoi(get->value);
184 }
185 break;
186 default:
187 is_pass_through_parameter = true;
188 break;
189 }
190 break;
191 default:
192 switch (get->key[0]) {
193 case 'c':
194 if (!strcmp(get->key, "callback")) {
195 if (callback) {
196 *callback = get->value;
197 }
198 } else {
199 is_pass_through_parameter = true;
200 }
201 break;
202 default:
203 is_pass_through_parameter = true;
204 }
205 }
206
207 if (is_pass_through_parameter && pass_through_parameters) {
208 if (GRN_TEXT_LEN(pass_through_parameters) > 0) {
209 GRN_TEXT_PUTS(ctx, pass_through_parameters, "&");
210 }
211 grn_text_urlenc(ctx, pass_through_parameters, get->key, strlen(get->key));
212 GRN_TEXT_PUTS(ctx, pass_through_parameters, "=");
213 grn_text_urlenc(ctx, pass_through_parameters,
214 get->value, strlen(get->value));
215 }
216 }
217 }