Bug 1082: mem_free_set in abort_preloading
[elinks.git] / src / network / state.c
blob92430b05397cc9a26952cb96bef72c4358d4c6c4
1 /* Status/error messages managment */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <string.h>
9 #include "elinks.h"
11 #include "intl/gettext/libintl.h"
12 #include "network/connection.h"
13 #include "network/state.h"
14 #include "terminal/terminal.h"
15 #include "util/conv.h"
16 #include "util/lists.h"
17 #include "util/memory.h"
18 #include "util/string.h"
21 /* Global variables */
22 struct s_msg_dsc {
23 int n;
24 unsigned char *msg;
26 static const struct s_msg_dsc msg_dsc[] = {
27 {S_WAIT, N_("Waiting in queue")},
28 {S_DNS, N_("Looking up host")},
29 {S_CONN, N_("Making connection")},
30 {S_SSL_NEG, N_("SSL negotiation")},
31 {S_SENT, N_("Request sent")},
32 {S_LOGIN, N_("Logging in")},
33 {S_GETH, N_("Getting headers")},
34 {S_PROC, N_("Server is processing request")},
35 {S_TRANS, N_("Transferring")},
36 #ifdef CONFIG_BITTORRENT
37 {S_RESUME, N_("Resuming")},
38 {S_CONN_PEERS, N_("Connecting to peers")},
39 {S_CONN_TRACKER, N_("Connecting to tracker")},
40 #endif
42 {S_WAIT_REDIR, N_("Waiting for redirect confirmation")},
43 {S_OK, N_("OK")},
44 {S_INTERRUPTED, N_("Interrupted")},
45 {S_EXCEPT, N_("Socket exception")},
46 {S_INTERNAL, N_("Internal error")},
47 {S_OUT_OF_MEM, N_("Out of memory")},
48 {S_NO_DNS, N_("Host not found")},
49 {S_CANT_WRITE, N_("Error writing to socket")},
50 {S_CANT_READ, N_("Error reading from socket")},
51 {S_MODIFIED, N_("Data modified")},
52 {S_BAD_URL, N_("Bad URL syntax")},
53 {S_TIMEOUT, N_("Receive timeout")},
54 {S_RESTART, N_("Request must be restarted")},
55 {S_STATE, N_("Can't get socket state")},
56 {S_LOCAL_ONLY, N_("Only local connections are permitted")},
57 {S_NO_FORCED_DNS, N_("No host in the specified IP family was found")},
58 #if defined(CONFIG_GZIP) || defined(CONFIG_BZIP2)
59 {S_ENCODE_ERROR, N_("Error while decoding file. This might be caused\n"
60 "by the encoded file being corrupt.") },
61 #endif
62 {S_UNKNOWN_PROTOCOL, N_("This URL contains a protocol not yet known by ELinks.\n"
63 "You can configure an external handler for it through\n"
64 "the options system.")},
66 {S_EXTERNAL_PROTOCOL, N_("This URL contains a protocol that is not natively known\n"
67 "by ELinks which means that ELinks relies on external\n"
68 "programs for handling it. Downloading URLs using external\n"
69 "programs is not supported.")},
71 {S_HTTP_ERROR, N_("Bad HTTP response")},
72 {S_HTTP_204, N_("No content")},
73 {S_HTTP_UPLOAD_RESIZED, N_("File was resized during upload")},
75 {S_FILE_TYPE, N_("Unknown file type")},
76 {S_FILE_ERROR, N_("Error opening file")},
77 {S_FILE_CGI_BAD_PATH, N_("CGI script not in CGI path")},
78 {S_FILE_ANONYMOUS, N_("Local file access is not allowed in anonymous mode")},
80 #ifdef CONFIG_FTP
81 {S_FTP_ERROR, N_("Bad FTP response")},
82 {S_FTP_UNAVAIL, N_("FTP service unavailable")},
83 {S_FTP_LOGIN, N_("Bad FTP login")},
84 {S_FTP_PORT, N_("FTP PORT command failed")},
85 {S_FTP_NO_FILE, N_("File not found")},
86 {S_FTP_FILE_ERROR, N_("FTP file error")},
87 #endif
89 #ifdef CONFIG_SSL
90 {S_SSL_ERROR, N_("SSL error")},
91 #else
92 {S_SSL_ERROR, N_("This version of ELinks does not contain SSL/TLS support")},
93 #endif
95 {S_NO_JAVASCRIPT, N_("JavaScript support is not enabled")},
97 #ifdef CONFIG_NNTP
98 {S_NNTP_ERROR, N_("Bad NNTP response")},
99 {S_NNTP_NEWS_SERVER, N_("Unable to handle news: URI because no news server has been\n"
100 "been configured. Either set the option protocol.nntp.server\n"
101 "or set the NNTPSERVER environment variable.")},
102 {S_NNTP_SERVER_HANG_UP, N_("Server hung up for some reason")},
103 {S_NNTP_GROUP_UNKNOWN, N_("No such newsgroup")},
104 {S_NNTP_ARTICLE_UNKNOWN,N_("No such article")},
105 {S_NNTP_TRANSFER_ERROR, N_("Transfer failed")},
106 {S_NNTP_AUTH_REQUIRED, N_("Authorization required")},
107 {S_NNTP_ACCESS_DENIED, N_("Access to server denied")},
108 #endif
110 #ifdef CONFIG_GOPHER
111 {S_GOPHER_CSO_ERROR, N_("The CSO phone-book protocol is not supported.")},
112 #endif
114 {S_PROXY_ERROR, N_("Configuration of the proxy server failed.\n"
115 "This might be caused by an incorrect proxy\n"
116 "setting specified by an environment variable\n"
117 "or returned by a scripting proxy hook.\n"
118 "\n"
119 "The correct syntax for proxy settings are\n"
120 "a host name optionally followed by a colon\n"
121 "and a port number. Example: 'localhost:8080'.")},
123 #ifdef CONFIG_BITTORRENT
124 {S_BITTORRENT_ERROR, N_("BitTorrent error")},
125 {S_BITTORRENT_METAINFO, N_("The BitTorrent metainfo file contained errors")},
126 {S_BITTORRENT_TRACKER, N_("The tracker requesting failed")},
127 {S_BITTORRENT_BAD_URL, N_("The BitTorrent URL does not point to a valid URL")},
128 {S_BITTORRENT_PEER_URL, N_("The bittorrent-peer URL scheme is for internal use only")},
129 #endif
131 /* fsp_open_session() failed but did not set errno.
132 * fsp_open_session() never sends anything to the FSP server,
133 * so this error does not mean the server itself does not work.
134 * More likely, there was some problem in asking a DNS server
135 * about the address of the FSP server. */
136 {S_FSP_OPEN_SESSION_UNKN, N_("FSP server not found")},
138 {0, NULL}
142 struct strerror_val {
143 LIST_HEAD(struct strerror_val);
145 unsigned char msg[1]; /* must be last */
148 static INIT_LIST_OF(struct strerror_val, strerror_buf);
150 /* It returns convenient error message, depending on @state.
151 * It never returns NULL (if one changes that, be warn that
152 * callers may not test for this condition) --Zas */
153 unsigned char *
154 get_state_message(struct connection_state state, struct terminal *term)
156 unsigned char *e;
157 struct strerror_val *s;
158 int len;
159 unsigned char *unknown_error = _("Unknown error", term);
161 if (!is_system_error(state)) {
162 int i;
164 for (i = 0; msg_dsc[i].msg; i++)
165 if (msg_dsc[i].n == state.basic)
166 return _(msg_dsc[i].msg, term);
168 return unknown_error;
171 e = (unsigned char *) strerror(state.syserr);
172 if (!e || !*e) return unknown_error;
174 len = strlen(e);
176 foreach (s, strerror_buf)
177 if (!strlcmp(s->msg, -1, e, len))
178 return s->msg;
180 s = mem_calloc(1, sizeof(*s) + len);
181 if (!s) return unknown_error;
183 memcpy(s->msg, e, len + 1);
184 add_to_list(strerror_buf, s);
186 return s->msg;
189 void
190 done_state_message(void)
192 free_list(strerror_buf);