Adjusted to use the new Transfer() instead of the old Download()
[platform/upstream/curl.git] / lib / dict.c
1 /*****************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  *  The contents of this file are subject to the Mozilla Public License
9  *  Version 1.0 (the "License"); you may not use this file except in
10  *  compliance with the License. You may obtain a copy of the License at
11  *  http://www.mozilla.org/MPL/
12  *
13  *  Software distributed under the License is distributed on an "AS IS"
14  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15  *  License for the specific language governing rights and limitations
16  *  under the License.
17  *
18  *  The Original Code is Curl.
19  *
20  *  The Initial Developer of the Original Code is Daniel Stenberg.
21  *
22  *  Portions created by the Initial Developer are Copyright (C) 1998.
23  *  All Rights Reserved.
24  *
25  * ------------------------------------------------------------
26  * Main author:
27  * - Daniel Stenberg <Daniel.Stenberg@haxx.nu>
28  *
29  *      http://curl.haxx.nu
30  *
31  * $Source$
32  * $Revision$
33  * $Date$
34  * $Author$
35  * $State$
36  * $Locker$
37  *
38  * ------------------------------------------------------------
39  ****************************************************************************/
40
41 /* -- WIN32 approved -- */
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <ctype.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49
50 #include <errno.h>
51
52 #include "setup.h"
53
54 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
55 #include <winsock.h>
56 #include <time.h>
57 #include <io.h>
58 #else
59 #ifdef HAVE_SYS_SOCKET_H
60 #include <sys/socket.h>
61 #endif
62 #include <netinet/in.h>
63 #include <sys/time.h>
64 #include <sys/resource.h>
65 #ifdef HAVE_UNISTD_H
66 #include <unistd.h>
67 #endif
68 #include <netdb.h>
69 #ifdef HAVE_ARPA_INET_H
70 #include <arpa/inet.h>
71 #endif
72 #ifdef HAVE_NET_IF_H
73 #include <net/if.h>
74 #endif
75 #include <sys/ioctl.h>
76 #include <signal.h>
77
78 #ifdef HAVE_SYS_PARAM_H
79 #include <sys/param.h>
80 #endif
81
82 #ifdef HAVE_SYS_SELECT_H
83 #include <sys/select.h>
84 #endif
85
86
87 #endif
88
89 #include "urldata.h"
90 #include <curl/curl.h>
91 #include "download.h"
92 #include "sendf.h"
93
94 #include "progress.h"
95
96 #define _MPRINTF_REPLACE /* use our functions only */
97 #include <curl/mprintf.h>
98
99
100 UrgError dict(struct UrlData *data, char *path, long *bytecount)
101 {
102   int nth;
103   char *word;
104   char *ppath;
105   char *database = NULL;
106   char *strategy = NULL;
107   char *nthdef = NULL; /* This is not part of the protocol, but required
108                           by RFC 2229 */
109   UrgError result=URG_OK;
110     
111   if(data->conf & CONF_USERPWD) {
112     /* AUTH is missing */
113   }
114
115   if (strnequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
116       strnequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
117       strnequal(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
118       
119     word = strchr(path, ':');
120     if (word) {
121       word++;
122       database = strchr(word, ':');
123       if (database) {
124         *database++ = (char)0;
125         strategy = strchr(database, ':');
126         if (strategy) {
127           *strategy++ = (char)0;
128           nthdef = strchr(strategy, ':');
129           if (nthdef) {
130             *nthdef++ = (char)0;
131           }
132         }
133       }
134     }
135       
136     if ((word == NULL) || (*word == (char)0)) {
137       failf(data, "lookup word is missing\n");
138     }
139     if ((database == NULL) || (*database == (char)0)) {
140       database = "!";
141     }
142     if ((strategy == NULL) || (*strategy == (char)0)) {
143       strategy = ".";
144     }
145     if ((nthdef == NULL) || (*nthdef == (char)0)) {
146       nth = 0;
147     }
148     else {
149       nth = atoi(nthdef);
150     }
151       
152     sendf(data->firstsocket, data,
153           "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\n"
154           "MATCH "
155           "%s "    /* database */
156           "%s "    /* strategy */
157           "%s\n"   /* word */
158           "QUIT\n",
159             
160           database,
161           strategy,
162           word
163           );
164     
165     result = Transfer(data, data->firstsocket, -1, FALSE, bytecount,
166                       -1, NULL); /* no upload */
167       
168     if(result)
169       return result;
170     
171   }
172   else if (strnequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
173            strnequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
174            strnequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
175     
176     word = strchr(path, ':');
177     if (word) {
178       word++;
179       database = strchr(word, ':');
180       if (database) {
181         *database++ = (char)0;
182         nthdef = strchr(database, ':');
183         if (nthdef) {
184           *nthdef++ = (char)0;
185         }
186       }
187     }
188       
189     if ((word == NULL) || (*word == (char)0)) {
190       failf(data, "lookup word is missing\n");
191     }
192     if ((database == NULL) || (*database == (char)0)) {
193       database = "!";
194     }
195     if ((nthdef == NULL) || (*nthdef == (char)0)) {
196       nth = 0;
197     }
198     else {
199       nth = atoi(nthdef);
200     }
201       
202     sendf(data->firstsocket, data,
203           "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\n"
204           "DEFINE "
205           "%s "     /* database */
206           "%s\n"    /* word */
207           "QUIT\n",
208           
209           database,
210           word
211           );
212     
213     result = Transfer(data, data->firstsocket, -1, FALSE, bytecount,
214                       -1, NULL); /* no upload */
215       
216     if(result)
217       return result;
218       
219   }
220   else {
221       
222     ppath = strchr(path, '/');
223     if (ppath) {
224       int i;
225         
226       ppath++;
227       for (i = 0; (i < URL_MAX_LENGTH) && (ppath[i]); i++) {
228         if (ppath[i] == ':')
229           ppath[i] = ' ';
230       }
231       sendf(data->firstsocket, data,
232             "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\n"
233             "%s\n"
234             "QUIT\n",
235             ppath);
236       
237       result = Transfer(data, data->firstsocket, -1, FALSE, bytecount,
238                         -1, NULL);
239       
240       if(result)
241         return result;
242       
243     }
244   }
245
246   ProgressEnd(data);
247   return URG_OK;
248 }