adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2002, Adam Dunkels. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following |
| 12 | * disclaimer in the documentation and/or other materials provided |
| 13 | * with the distribution. |
| 14 | * 3. All advertising materials mentioning features or use of this |
| 15 | * software must display the following acknowledgement: |
| 16 | * This product includes software developed by Adam Dunkels. |
| 17 | * 4. The name of the author may not be used to endorse or promote |
| 18 | * products derived from this software without specific prior |
| 19 | * written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 22 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 25 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 27 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | * This file is part of the Contiki desktop environment |
| 34 | * |
adamdunkels | 75c276c | 2003-09-04 19:35:32 +0000 | [diff] [blame^] | 35 | * $Id: www.c,v 1.21 2003/09/04 19:35:32 adamdunkels Exp $ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 36 | * |
| 37 | */ |
| 38 | |
| 39 | |
| 40 | #include "ctk.h" |
| 41 | #include "dispatcher.h" |
| 42 | #include "webclient.h" |
| 43 | #include "htmlparser.h" |
| 44 | #include "http-strings.h" |
| 45 | #include "resolv.h" |
| 46 | |
| 47 | #include "petsciiconv.h" |
| 48 | |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 49 | #include "program-handler.h" |
| 50 | |
| 51 | |
adamdunkels | 8af703e | 2003-04-08 11:50:20 +0000 | [diff] [blame] | 52 | #include "loader.h" |
| 53 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 54 | #include "www-conf.h" |
| 55 | |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 56 | #if 1 |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 57 | #define PRINTF(x) |
| 58 | #else |
| 59 | #include <stdio.h> |
| 60 | #define PRINTF(x) printf x |
| 61 | #endif |
| 62 | |
| 63 | |
| 64 | /* The array that holds the current URL. */ |
| 65 | static char url[WWW_CONF_MAX_URLLEN + 1]; |
| 66 | static char tmpurl[WWW_CONF_MAX_URLLEN + 1]; |
| 67 | |
| 68 | /* The array that holds the web page text. */ |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 69 | static char webpage[WWW_CONF_WEBPAGE_WIDTH * |
| 70 | WWW_CONF_WEBPAGE_HEIGHT + 1]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 71 | |
| 72 | /* The CTK widgets for the main window. */ |
| 73 | static struct ctk_window mainwindow; |
| 74 | |
| 75 | static struct ctk_button backbutton = |
| 76 | {CTK_BUTTON(0, 0, 4, "Back")}; |
| 77 | static struct ctk_button downbutton = |
| 78 | {CTK_BUTTON(10, 0, 4, "Down")}; |
| 79 | static struct ctk_button stopbutton = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 80 | {CTK_BUTTON(WWW_CONF_WEBPAGE_WIDTH - 16, 0, 4, "Stop")}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 81 | static struct ctk_button gobutton = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 82 | {CTK_BUTTON(WWW_CONF_WEBPAGE_WIDTH - 4, 0, 2, "Go")}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 83 | |
| 84 | static struct ctk_separator sep1 = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 85 | {CTK_SEPARATOR(0, 2, WWW_CONF_WEBPAGE_WIDTH)}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 86 | |
| 87 | static char editurl[WWW_CONF_MAX_URLLEN + 1]; |
| 88 | static struct ctk_textentry urlentry = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 89 | {CTK_TEXTENTRY(0, 1, WWW_CONF_WEBPAGE_WIDTH - 2, |
| 90 | 1, editurl, WWW_CONF_MAX_URLLEN)}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 91 | static struct ctk_label webpagelabel = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 92 | {CTK_LABEL(0, 3, WWW_CONF_WEBPAGE_WIDTH, |
| 93 | WWW_CONF_WEBPAGE_HEIGHT, webpage)}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 94 | |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 95 | static char statustexturl[WWW_CONF_WEBPAGE_WIDTH]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 96 | static struct ctk_label statustext = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 97 | {CTK_LABEL(0, WWW_CONF_WEBPAGE_HEIGHT + 4, |
| 98 | WWW_CONF_WEBPAGE_WIDTH, 1, "")}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 99 | static struct ctk_separator sep2 = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 100 | {CTK_SEPARATOR(0, WWW_CONF_WEBPAGE_HEIGHT + 3, |
| 101 | WWW_CONF_WEBPAGE_WIDTH)}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 102 | |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 103 | static struct ctk_window wgetdialog; |
| 104 | static struct ctk_label wgetlabel1 = |
adamdunkels | 77b7a69 | 2003-08-29 20:36:23 +0000 | [diff] [blame] | 105 | {CTK_LABEL(1, 1, 34, 1, "This web page cannot be displayed.")}; |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 106 | static struct ctk_label wgetlabel2 = |
adamdunkels | 77b7a69 | 2003-08-29 20:36:23 +0000 | [diff] [blame] | 107 | {CTK_LABEL(1, 3, 35, 1, "Would you like to download instead?")}; |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 108 | static struct ctk_button wgetnobutton = |
adamdunkels | 77b7a69 | 2003-08-29 20:36:23 +0000 | [diff] [blame] | 109 | {CTK_BUTTON(1, 5, 6, "Cancel")}; |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 110 | static struct ctk_button wgetyesbutton = |
adamdunkels | 77b7a69 | 2003-08-29 20:36:23 +0000 | [diff] [blame] | 111 | {CTK_BUTTON(11, 5, 24, "Close browser & download")}; |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 112 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 113 | /* The char arrays that hold the history of visited URLs. */ |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 114 | static char history[WWW_CONF_HISTORY_SIZE][WWW_CONF_MAX_URLLEN]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 115 | static char history_last, history_first; |
| 116 | |
| 117 | |
| 118 | /* The CTK widget definitions for the hyperlinks and the char arrays |
| 119 | that hold the link URLs. */ |
| 120 | struct formattribs { |
| 121 | char formaction[WWW_CONF_MAX_FORMACTIONLEN]; |
| 122 | char formname[WWW_CONF_MAX_FORMNAMELEN]; |
| 123 | #define FORMINPUTTYPE_SUBMITBUTTON 1 |
| 124 | #define FORMINPUTTYPE_INPUTFIELD 2 |
| 125 | unsigned char inputtype; |
| 126 | char inputname[WWW_CONF_MAX_INPUTNAMELEN]; |
| 127 | char *inputvalue; |
| 128 | }; |
| 129 | |
| 130 | union pagewidgetattrib { |
| 131 | char url[WWW_CONF_MAX_URLLEN]; |
adamdunkels | d59dadf | 2003-08-15 18:48:35 +0000 | [diff] [blame] | 132 | #if WWW_CONF_FORMS |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 133 | struct formattribs form; |
adamdunkels | d59dadf | 2003-08-15 18:48:35 +0000 | [diff] [blame] | 134 | #endif /* WWW_CONF_FORMS */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 135 | }; |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 136 | static struct ctk_widget pagewidgets[WWW_CONF_MAX_NUMPAGEWIDGETS]; |
| 137 | static union pagewidgetattrib pagewidgetattribs[WWW_CONF_MAX_NUMPAGEWIDGETS]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 138 | static unsigned char pagewidgetptr; |
| 139 | |
| 140 | |
| 141 | /* The "scrolly" variable holds the line number (in the web page) of |
| 142 | the first line of text shown on screen. */ |
| 143 | static unsigned short scrolly; |
| 144 | |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 145 | /* The "scrollend" variable contains the web page line number of the |
| 146 | last line that should be shown on screen, before the download |
| 147 | should stop. */ |
| 148 | #if WWW_CONF_PAGEVIEW |
| 149 | static unsigned short scrollend; |
| 150 | #endif /* WWW_CONF_PAGEVIEW */ |
| 151 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 152 | #if WWW_CONF_RENDERSTATE |
| 153 | static unsigned char renderstate; |
| 154 | #endif /* WWW_CONF_RENDERSTATE */ |
| 155 | |
| 156 | /* The "run" flag is used to determine if the web page should be |
| 157 | continuosly scrolled upward with new data coming in from below. */ |
| 158 | static unsigned char run; |
| 159 | |
| 160 | #define ISO_nl 0x0a |
| 161 | #define ISO_space 0x20 |
| 162 | #define ISO_ampersand 0x26 |
| 163 | #define ISO_plus 0x2b |
| 164 | #define ISO_slash 0x2f |
| 165 | #define ISO_eq 0x3d |
| 166 | #define ISO_questionmark 0x3f |
| 167 | |
| 168 | /* The state of the rendering code. */ |
| 169 | static u8_t x; |
| 170 | static u16_t starty; |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 171 | static char nextword[WWW_CONF_WEBPAGE_WIDTH + 1]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 172 | static unsigned char nextwordptr; |
| 173 | |
| 174 | static unsigned char count; |
| 175 | static char receivingmsgs[4][23] = { |
| 176 | "Receiving web page ...", |
| 177 | "Receiving web page. ..", |
| 178 | "Receiving web page.. .", |
| 179 | "Receiving web page... " |
| 180 | }; |
| 181 | |
| 182 | |
adamdunkels | 78c03dc | 2003-04-09 13:45:05 +0000 | [diff] [blame] | 183 | static DISPATCHER_SIGHANDLER(www_sighandler, s, data); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 184 | static struct dispatcher_proc p = |
adamdunkels | 78c03dc | 2003-04-09 13:45:05 +0000 | [diff] [blame] | 185 | {DISPATCHER_PROC("Web browser", NULL, www_sighandler, webclient_appcall)}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 186 | static ek_id_t id; |
| 187 | |
| 188 | |
| 189 | static void formsubmit(struct formattribs *attribs); |
| 190 | /*-----------------------------------------------------------------------------------*/ |
| 191 | /* make_window() |
| 192 | * |
| 193 | * Creates the web browser's window. |
| 194 | */ |
| 195 | static void |
| 196 | make_window(void) |
| 197 | { |
| 198 | |
| 199 | CTK_WIDGET_ADD(&mainwindow, &backbutton); |
| 200 | CTK_WIDGET_ADD(&mainwindow, &downbutton); |
| 201 | CTK_WIDGET_ADD(&mainwindow, &stopbutton); |
| 202 | CTK_WIDGET_ADD(&mainwindow, &gobutton); |
| 203 | CTK_WIDGET_ADD(&mainwindow, &urlentry); |
| 204 | CTK_WIDGET_ADD(&mainwindow, &sep1); |
| 205 | CTK_WIDGET_ADD(&mainwindow, &webpagelabel); |
| 206 | CTK_WIDGET_ADD(&mainwindow, &sep2); |
| 207 | CTK_WIDGET_ADD(&mainwindow, &statustext); |
| 208 | |
| 209 | CTK_WIDGET_FOCUS(&mainwindow, &stopbutton); |
| 210 | |
| 211 | pagewidgetptr = 0; |
| 212 | } |
| 213 | /*-----------------------------------------------------------------------------------*/ |
| 214 | /* redraw_window(): |
| 215 | * |
| 216 | * Convenience function that calls upon CTK to redraw the browser |
| 217 | * window. */ |
| 218 | static void |
| 219 | redraw_window(void) |
| 220 | { |
| 221 | ctk_window_redraw(&mainwindow); |
| 222 | } |
| 223 | /*-----------------------------------------------------------------------------------*/ |
| 224 | /* www_init(); |
| 225 | * |
| 226 | * Initializes and starts the web browser. Called either at startup or |
| 227 | * to open the browser window. |
| 228 | */ |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 229 | LOADER_INIT_FUNC(www_init, arg) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 230 | { |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 231 | arg_free(arg); |
| 232 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 233 | if(id == EK_ID_NONE) { |
| 234 | id = dispatcher_start(&p); |
| 235 | |
| 236 | /* Create the main window. */ |
| 237 | memset(webpage, 0, sizeof(webpage)); |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 238 | ctk_window_new(&mainwindow, WWW_CONF_WEBPAGE_WIDTH, |
| 239 | WWW_CONF_WEBPAGE_HEIGHT+5, "Web browser"); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 240 | make_window(); |
adamdunkels | 77b7a69 | 2003-08-29 20:36:23 +0000 | [diff] [blame] | 241 | #ifdef WWW_CONF_HOMEPAGE |
| 242 | strncpy(editurl, WWW_CONF_HOMEPAGE, sizeof(editurl)); |
| 243 | #endif /* WWW_CONF_HOMEPAGE */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 244 | CTK_WIDGET_FOCUS(&mainwindow, &urlentry); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 245 | |
| 246 | /* Create download dialog.*/ |
adamdunkels | 77b7a69 | 2003-08-29 20:36:23 +0000 | [diff] [blame] | 247 | ctk_dialog_new(&wgetdialog, 38, 7); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 248 | CTK_WIDGET_ADD(&wgetdialog, &wgetlabel1); |
| 249 | CTK_WIDGET_ADD(&wgetdialog, &wgetlabel2); |
| 250 | CTK_WIDGET_ADD(&wgetdialog, &wgetnobutton); |
| 251 | CTK_WIDGET_ADD(&wgetdialog, &wgetyesbutton); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 252 | |
| 253 | /* Attach as a listener to a number of signals ("Button activate", |
| 254 | "Hyperlink activate" and "Hyperlink hover", and the resolver's |
| 255 | signal. */ |
adamdunkels | 62b6499 | 2003-04-08 07:20:11 +0000 | [diff] [blame] | 256 | dispatcher_listen(ctk_signal_window_close); |
adamdunkels | 328e535 | 2003-08-20 20:54:46 +0000 | [diff] [blame] | 257 | dispatcher_listen(ctk_signal_widget_activate); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 258 | dispatcher_listen(ctk_signal_hyperlink_activate); |
| 259 | dispatcher_listen(ctk_signal_hyperlink_hover); |
| 260 | dispatcher_listen(resolv_signal_found); |
adamdunkels | 77b7a69 | 2003-08-29 20:36:23 +0000 | [diff] [blame] | 261 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 262 | } |
| 263 | ctk_window_open(&mainwindow); |
| 264 | } |
| 265 | /*-----------------------------------------------------------------------------------*/ |
| 266 | static void |
| 267 | clear_page(void) |
| 268 | { |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 269 | /* if(ctk_window_isopen(&mainwindow)) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 270 | ctk_window_close(&mainwindow); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 271 | }*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 272 | ctk_window_clear(&mainwindow); |
| 273 | make_window(); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 274 | /* ctk_window_open(&mainwindow);*/ |
| 275 | ctk_window_redraw(&mainwindow); |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 276 | memset(webpage, 0, WWW_CONF_WEBPAGE_WIDTH * WWW_CONF_WEBPAGE_HEIGHT); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 277 | } |
| 278 | /*-----------------------------------------------------------------------------------*/ |
| 279 | static void |
| 280 | show_url(void) |
| 281 | { |
| 282 | memcpy(editurl, url, WWW_CONF_MAX_URLLEN); |
| 283 | strncpy(editurl, "http://", 7); |
| 284 | petsciiconv_topetscii(editurl + 7, WWW_CONF_MAX_URLLEN - 7); |
| 285 | CTK_WIDGET_REDRAW(&urlentry); |
| 286 | } |
| 287 | /*-----------------------------------------------------------------------------------*/ |
| 288 | static void |
| 289 | show_statustext(char *text) |
| 290 | { |
| 291 | ctk_label_set_text(&statustext, text); |
| 292 | CTK_WIDGET_REDRAW(&statustext); |
| 293 | } |
| 294 | /*-----------------------------------------------------------------------------------*/ |
| 295 | /* open_url(): |
| 296 | * |
| 297 | * Called when the URL present in the global "url" variable should be |
| 298 | * opened. It will call the hostname resolver as well as the HTTP |
| 299 | * client requester. |
| 300 | */ |
| 301 | static void |
| 302 | open_url(void) |
| 303 | { |
| 304 | unsigned char i; |
| 305 | static char host[32]; |
| 306 | char *file; |
| 307 | register char *urlptr; |
| 308 | unsigned short port; |
adamdunkels | 75c276c | 2003-09-04 19:35:32 +0000 | [diff] [blame^] | 309 | static u16_t addr[2]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 310 | |
| 311 | /* Trim off any spaces in the end of the url. */ |
| 312 | urlptr = url + strlen(url) - 1; |
| 313 | while(*urlptr == ' ' && urlptr > url) { |
| 314 | *urlptr = 0; |
| 315 | --urlptr; |
| 316 | } |
| 317 | |
| 318 | /* Don't even try to go further if the URL is empty. */ |
| 319 | if(urlptr == url) { |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | /* See if the URL starts with http://, otherwise prepend it. */ |
| 324 | if(strncmp(url, http_http, 7) != 0) { |
| 325 | while(urlptr >= url) { |
| 326 | *(urlptr + 7) = *urlptr; |
| 327 | --urlptr; |
| 328 | } |
| 329 | strncpy(url, http_http, 7); |
| 330 | } |
| 331 | |
| 332 | /* Find host part of the URL. */ |
| 333 | urlptr = &url[7]; |
| 334 | for(i = 0; i < sizeof(host); ++i) { |
| 335 | if(*urlptr == 0 || |
| 336 | *urlptr == '/' || |
| 337 | *urlptr == ' ' || |
| 338 | *urlptr == ':') { |
| 339 | host[i] = 0; |
| 340 | break; |
| 341 | } |
| 342 | host[i] = *urlptr; |
| 343 | ++urlptr; |
| 344 | } |
| 345 | |
| 346 | /* XXX: Here we should find the port part of the URL, but this isn't |
| 347 | currently done because of laziness from the programmer's side |
| 348 | :-) */ |
| 349 | |
| 350 | /* Find file part of the URL. */ |
| 351 | while(*urlptr != '/' && *urlptr != 0) { |
| 352 | ++urlptr; |
| 353 | } |
| 354 | if(*urlptr == '/') { |
| 355 | file = urlptr; |
| 356 | } else { |
| 357 | file = "/"; |
| 358 | } |
| 359 | |
| 360 | /* Try to lookup the hostname. If it fails, we initiate a hostname |
| 361 | lookup and print out an informative message on the statusbar. */ |
adamdunkels | 75c276c | 2003-09-04 19:35:32 +0000 | [diff] [blame^] | 362 | if(uip_main_ipaddrconv(host, (unsigned char *)addr) == 0) { |
| 363 | if(resolv_lookup(host) == NULL) { |
| 364 | resolv_query(host); |
| 365 | show_statustext("Resolving host..."); |
| 366 | return; |
| 367 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* The hostname we present in the hostname table, so we send out the |
| 371 | initial GET request. */ |
| 372 | if(webclient_get(host, 80, file) == 0) { |
| 373 | show_statustext("Out of memory error."); |
| 374 | } else { |
| 375 | show_statustext("Connecting..."); |
| 376 | } |
| 377 | redraw_window(); |
| 378 | } |
| 379 | /*-----------------------------------------------------------------------------------*/ |
| 380 | /* open_link(link): |
| 381 | * |
| 382 | * Will format a link from the current web pages so that it suits the |
| 383 | * open_url() function and finally call it to open the requested URL. |
| 384 | */ |
| 385 | static void |
| 386 | open_link(char *link) |
| 387 | { |
| 388 | char *urlptr; |
| 389 | |
| 390 | if(strncmp(link, http_http, 7) == 0) { |
| 391 | /* The link starts with http://. We just copy the contents of the |
| 392 | link into the url string and jump away. */ |
| 393 | strncpy(url, link, WWW_CONF_MAX_URLLEN); |
| 394 | } else if(*link == ISO_slash && |
| 395 | *(link + 1) == ISO_slash) { |
| 396 | /* The link starts with //, so we'll copy it into the url |
| 397 | variable, starting after the http (which already is present in |
| 398 | the url variable since we were able to open the web page on |
| 399 | which this link was found in the first place). */ |
| 400 | strncpy(&url[5], link, WWW_CONF_MAX_URLLEN); |
| 401 | } else if(*link == ISO_slash) { |
| 402 | /* The link starts with a slash, so it is a non-relative link |
| 403 | within the same web site. We find the start of the filename of |
| 404 | the current URL and paste the contents of this link there, and |
| 405 | head off to the new URL. */ |
| 406 | for(urlptr = &url[7]; |
| 407 | *urlptr != 0 && *urlptr != ISO_slash; |
| 408 | ++urlptr); |
| 409 | strncpy(urlptr, link, WWW_CONF_MAX_URLLEN - (urlptr - url)); |
| 410 | } else { |
| 411 | /* A fully relative link is found. We find the last slash in the |
| 412 | current URL and paste the link there. */ |
| 413 | |
| 414 | /* XXX: we should really parse any ../ in the link as well. */ |
| 415 | for(urlptr = url + strlen(url); |
| 416 | urlptr != url && *urlptr != ISO_slash; |
| 417 | --urlptr); |
| 418 | ++urlptr; |
| 419 | strncpy(urlptr, link, WWW_CONF_MAX_URLLEN - (urlptr - url)); |
| 420 | } |
| 421 | |
| 422 | /* Open the URL. */ |
| 423 | scrolly = 0; |
| 424 | show_url(); |
| 425 | open_url(); |
| 426 | } |
| 427 | /*-----------------------------------------------------------------------------------*/ |
| 428 | /* log_back(): |
| 429 | * |
| 430 | * Copies the current URL from the url variable and into the log for |
| 431 | * the back button. |
| 432 | */ |
| 433 | static void |
| 434 | log_back(void) |
| 435 | { |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 436 | if(strncmp(url, history[(int)history_last], WWW_CONF_MAX_URLLEN) != 0) { |
| 437 | memcpy(history[(int)history_last], url, WWW_CONF_MAX_URLLEN); |
| 438 | ++history_last; |
| 439 | if(history_last >= WWW_CONF_HISTORY_SIZE) { |
| 440 | history_last = 0; |
| 441 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 445 | static void |
| 446 | quit(void) |
| 447 | { |
| 448 | ctk_window_close(&mainwindow); |
| 449 | dispatcher_exit(&p); |
| 450 | id = EK_ID_NONE; |
| 451 | LOADER_UNLOAD(); |
| 452 | } |
| 453 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 454 | /* www_dispatcher(): |
| 455 | * |
| 456 | * The program's signal dispatcher function. Is called by the ek |
| 457 | * dispatcher whenever a signal arrives. |
| 458 | */ |
adamdunkels | 78c03dc | 2003-04-09 13:45:05 +0000 | [diff] [blame] | 459 | static |
| 460 | DISPATCHER_SIGHANDLER(www_sighandler, s, data) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 461 | { |
| 462 | static struct ctk_widget *w; |
| 463 | static unsigned char i; |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 464 | static char *argptr; |
| 465 | |
adamdunkels | 78c03dc | 2003-04-09 13:45:05 +0000 | [diff] [blame] | 466 | DISPATCHER_SIGHANDLER_ARGS(s, data); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 467 | |
| 468 | |
| 469 | w = (struct ctk_widget *)data; |
adamdunkels | 328e535 | 2003-08-20 20:54:46 +0000 | [diff] [blame] | 470 | if(s == ctk_signal_widget_activate) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 471 | if(w == (struct ctk_widget *)&backbutton) { |
| 472 | scrolly = 0; |
| 473 | run = 1; |
| 474 | |
| 475 | --history_last; |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 476 | if(history_last > WWW_CONF_HISTORY_SIZE) { |
| 477 | history_last = WWW_CONF_HISTORY_SIZE - 1; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 478 | } |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 479 | memcpy(url, history[(int)history_last], WWW_CONF_MAX_URLLEN); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 480 | open_url(); |
| 481 | CTK_WIDGET_FOCUS(&mainwindow, &backbutton); |
| 482 | } else if(w == (struct ctk_widget *)&downbutton) { |
| 483 | run = 1; |
| 484 | open_url(); |
| 485 | CTK_WIDGET_FOCUS(&mainwindow, &downbutton); |
adamdunkels | 328e535 | 2003-08-20 20:54:46 +0000 | [diff] [blame] | 486 | } else if(w == (struct ctk_widget *)&gobutton || |
| 487 | w == (struct ctk_widget *)&urlentry) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 488 | scrolly = 0; |
adamdunkels | fb565d9 | 2003-08-13 22:51:07 +0000 | [diff] [blame] | 489 | #if WWW_CONF_PAGEVIEW |
| 490 | starty = 0; |
| 491 | #endif /* WWW_CONF_PAGEVIEW */ |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 492 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 493 | run = 1; |
| 494 | log_back(); |
| 495 | memcpy(url, editurl, WWW_CONF_MAX_URLLEN); |
| 496 | petsciiconv_toascii(url, WWW_CONF_MAX_URLLEN); |
| 497 | open_url(); |
| 498 | CTK_WIDGET_FOCUS(&mainwindow, &gobutton); |
| 499 | } else if(w == (struct ctk_widget *)&stopbutton) { |
| 500 | run = 0; |
| 501 | webclient_close(); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 502 | } else if(w == (struct ctk_widget *)&wgetnobutton) { |
| 503 | ctk_dialog_close(); |
| 504 | } else if(w == (struct ctk_widget *)&wgetyesbutton) { |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 505 | ctk_dialog_close(); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 506 | quit(); |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 507 | argptr = arg_alloc(WWW_CONF_MAX_URLLEN); |
| 508 | if(argptr != NULL) { |
| 509 | strncpy(argptr, url, WWW_CONF_MAX_URLLEN); |
| 510 | } |
| 511 | program_handler_load("wget.prg", argptr); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 512 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 513 | #if WWW_CONF_FORMS |
| 514 | } else { |
| 515 | /* Check form buttons */ |
| 516 | for(i = 0; i < pagewidgetptr; ++i) { |
| 517 | if(&pagewidgets[i] == w) { |
| 518 | formsubmit(&pagewidgetattribs[i].form); |
| 519 | /* show_statustext(pagewidgetattribs[i].form.formaction);*/ |
| 520 | /* PRINTF(("Formaction %s formname %s inputname %s\n", |
| 521 | pagewidgetattribs[i].form.formaction, |
| 522 | pagewidgetattribs[i].form.formname, |
| 523 | pagewidgetattribs[i].form.inputname));*/ |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | #endif /* WWW_CONF_FORMS */ |
| 528 | } |
| 529 | } else if(s == ctk_signal_hyperlink_activate) { |
| 530 | log_back(); |
| 531 | open_link(w->widget.hyperlink.url); |
| 532 | CTK_WIDGET_FOCUS(&mainwindow, &stopbutton); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 533 | /* ctk_window_open(&mainwindow);*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 534 | run = 1; |
| 535 | } else if(s == ctk_signal_hyperlink_hover) { |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 536 | if(CTK_WIDGET_TYPE((struct ctk_widget *)data) == |
| 537 | CTK_WIDGET_HYPERLINK) { |
adamdunkels | c4bf5ca | 2003-04-18 00:20:22 +0000 | [diff] [blame] | 538 | strncpy(statustexturl, w->widget.hyperlink.url, |
| 539 | sizeof(statustexturl)); |
| 540 | petsciiconv_topetscii(statustexturl, sizeof(statustexturl)); |
| 541 | show_statustext(statustexturl); |
| 542 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 543 | } else if(s == resolv_signal_found) { |
| 544 | /* Either found a hostname, or not. */ |
| 545 | if((char *)data != NULL && |
| 546 | resolv_lookup((char *)data) != NULL) { |
| 547 | open_url(); |
| 548 | } else { |
| 549 | show_statustext("Host not found."); |
| 550 | } |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 551 | } else if(s == ctk_signal_window_close || |
| 552 | s == dispatcher_signal_quit) { |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 553 | quit(); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | /*-----------------------------------------------------------------------------------*/ |
| 557 | /* set_url(): |
| 558 | * |
| 559 | * Constructs an URL from the arguments and puts it into the global |
| 560 | * "url" variable and the visible "editurl" (which is shown in the URL |
| 561 | * text entry widget in the browser window). |
| 562 | */ |
| 563 | static void |
| 564 | set_url(char *host, u16_t port, char *file) |
| 565 | { |
| 566 | char *urlptr; |
| 567 | |
| 568 | memset(url, 0, WWW_CONF_MAX_URLLEN); |
| 569 | |
| 570 | if(strncmp(file, http_http, 7) == 0) { |
| 571 | strncpy(url, file, sizeof(url)); |
| 572 | } else { |
| 573 | strncpy(url, http_http, 7); |
| 574 | urlptr = url + 7; |
| 575 | strcpy(urlptr, host); |
| 576 | urlptr += strlen(host); |
| 577 | strcpy(urlptr, file); |
| 578 | } |
| 579 | |
| 580 | show_url(); |
| 581 | } |
| 582 | /*-----------------------------------------------------------------------------------*/ |
| 583 | /* webclient_aborted(): |
| 584 | * |
| 585 | * Callback function. Called from the webclient when the HTTP |
| 586 | * connection was abruptly aborted. |
| 587 | */ |
| 588 | void |
| 589 | webclient_aborted(void) |
| 590 | { |
| 591 | show_statustext("Connection reset by peer"); |
| 592 | } |
| 593 | /*-----------------------------------------------------------------------------------*/ |
| 594 | /* webclient_timedout(): |
| 595 | * |
| 596 | * Callback function. Called from the webclient when the HTTP |
| 597 | * connection timed out. |
| 598 | */ |
| 599 | void |
| 600 | webclient_timedout(void) |
| 601 | { |
| 602 | show_statustext("Connection timed out"); |
| 603 | } |
| 604 | /*-----------------------------------------------------------------------------------*/ |
| 605 | /* webclient_closed(): |
| 606 | * |
| 607 | * Callback function. Called from the webclient when the HTTP |
| 608 | * connection was closed after a request from the "webclient_close()" |
| 609 | * function. . |
| 610 | */ |
| 611 | void |
| 612 | webclient_closed(void) |
| 613 | { |
| 614 | show_statustext("Stopped."); |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 615 | petsciiconv_topetscii(&webpage[(WWW_CONF_WEBPAGE_HEIGHT - 1) * |
| 616 | WWW_CONF_WEBPAGE_WIDTH], WWW_CONF_WEBPAGE_WIDTH); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 617 | redraw_window(); |
| 618 | } |
| 619 | /*-----------------------------------------------------------------------------------*/ |
| 620 | /* webclient_closed(): |
| 621 | * |
| 622 | * Callback function. Called from the webclient when the HTTP |
| 623 | * connection is connected. |
| 624 | */ |
| 625 | void |
| 626 | webclient_connected(void) |
| 627 | { |
| 628 | x = nextwordptr = 0; |
| 629 | starty = scrolly; |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 630 | |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 631 | #if WWW_CONF_PAGEVIEW |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 632 | if(starty > 4) { |
| 633 | starty = scrolly - 4; |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 634 | } |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 635 | scrollend = starty + WWW_CONF_WEBPAGE_HEIGHT - 4; |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 636 | #endif /* WWW_CONF_PAGEVIEW */ |
| 637 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 638 | nextword[0] = 0; |
| 639 | |
| 640 | if(scrolly == 0) { |
| 641 | clear_page(); |
| 642 | redraw_window(); |
| 643 | } |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 644 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 645 | show_statustext("Request sent..."); |
| 646 | set_url(webclient_hostname(), webclient_port(), webclient_filename()); |
| 647 | |
| 648 | #if WWW_CONF_RENDERSTATE |
| 649 | renderstate = HTMLPARSER_RENDERSTATE_NONE; |
| 650 | #endif /* WWW_CONF_RENDERSTATE */ |
| 651 | htmlparser_init(); |
| 652 | } |
| 653 | /*-----------------------------------------------------------------------------------*/ |
| 654 | /* scroll(): |
| 655 | * |
| 656 | * Scrolls the entire web page display (text and hyperlinks) one line |
| 657 | * upwards. |
| 658 | */ |
| 659 | static void |
| 660 | scroll(void) |
| 661 | { |
| 662 | unsigned char i; |
| 663 | unsigned char lptr, linkptrtmp; |
| 664 | struct ctk_widget *linksptr; |
| 665 | char *statustexttext; |
| 666 | struct ctk_widget *focuswidget; |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 667 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 668 | /* Scroll text up. */ |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 669 | memcpy(webpage, &webpage[WWW_CONF_WEBPAGE_WIDTH], |
| 670 | (WWW_CONF_WEBPAGE_HEIGHT - 1) * WWW_CONF_WEBPAGE_WIDTH); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 671 | |
| 672 | /* Clear last line of text. */ |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 673 | memset(&webpage[(WWW_CONF_WEBPAGE_HEIGHT - 1) * |
| 674 | WWW_CONF_WEBPAGE_WIDTH], |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 675 | ' ', WWW_CONF_WEBPAGE_WIDTH); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 676 | |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 677 | /* Scroll links and form widgets up. */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 678 | |
| 679 | lptr = 0; |
| 680 | linksptr = pagewidgets; |
| 681 | for(i = 0; i < pagewidgetptr; ++i) { |
| 682 | /* First, check which links that scroll off the top of the page |
| 683 | and should be removed. */ |
| 684 | if(CTK_WIDGET_YPOS(linksptr) == 3) { |
| 685 | lptr = i + 1; |
| 686 | } else { |
| 687 | /* Else, move them upward one notch. */ |
| 688 | |
| 689 | /* XXX: this is really a hack! These values should not be used |
| 690 | like this, but should be obtained and set using some CTK API |
| 691 | function. */ |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 692 | linksptr->widget.hyperlink.text -= WWW_CONF_WEBPAGE_WIDTH; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 693 | |
| 694 | --(linksptr->y); |
| 695 | } |
| 696 | ++linksptr; |
| 697 | } |
| 698 | |
| 699 | /* See if there are any links that scroll off the top. */ |
| 700 | if(lptr != 0) { |
| 701 | memcpy(pagewidgets, &pagewidgets[lptr], |
adamdunkels | 1a23b5b | 2003-08-05 13:50:02 +0000 | [diff] [blame] | 702 | sizeof(struct ctk_widget) * |
| 703 | (WWW_CONF_MAX_NUMPAGEWIDGETS - lptr)); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 704 | memcpy(pagewidgetattribs, &pagewidgetattribs[lptr], |
adamdunkels | 1a23b5b | 2003-08-05 13:50:02 +0000 | [diff] [blame] | 705 | sizeof(union pagewidgetattrib) * |
| 706 | (WWW_CONF_MAX_NUMPAGEWIDGETS - lptr)); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 707 | |
| 708 | /* Compute new value of linkptr and tuck it away in |
| 709 | linkptrtmp. make_window() destroys linkptr, so we need to |
| 710 | restore it after the call. */ |
| 711 | linkptrtmp = pagewidgetptr - lptr; |
| 712 | |
| 713 | /* XXX: hack - these values should *not* be obtained this way, but |
| 714 | through some CTK API instead! */ |
| 715 | statustexttext = statustext.text; |
| 716 | focuswidget = mainwindow.focused; |
| 717 | ctk_window_clear(&mainwindow); |
| 718 | make_window(); |
| 719 | CTK_WIDGET_FOCUS(&mainwindow, focuswidget); |
| 720 | show_statustext(statustexttext); |
| 721 | |
| 722 | pagewidgetptr = linkptrtmp; |
| 723 | |
| 724 | linksptr = pagewidgets; |
| 725 | for(i = 0; i < pagewidgetptr; ++i) { |
| 726 | if(linksptr->type == CTK_WIDGET_HYPERLINK) { |
| 727 | linksptr->widget.hyperlink.url -= lptr * |
| 728 | sizeof(union pagewidgetattrib); |
| 729 | } |
| 730 | CTK_WIDGET_ADD(&mainwindow, linksptr); |
| 731 | ++linksptr; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 736 | static char tmpcenterline[WWW_CONF_WEBPAGE_WIDTH]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 737 | /* inc_y(): |
| 738 | * |
| 739 | * Called from the rendering code when it is time to move on line |
| 740 | * downwards. |
| 741 | */ |
| 742 | static void |
| 743 | inc_y(void) |
| 744 | { |
| 745 | unsigned char spaces, i; |
| 746 | char *cptr; |
adamdunkels | 1a23b5b | 2003-08-05 13:50:02 +0000 | [diff] [blame] | 747 | register struct ctk_widget *linksptr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 748 | |
| 749 | if(starty > 0) { |
| 750 | --starty; |
| 751 | } else { |
| 752 | #if WWW_CONF_RENDERSTATE |
| 753 | /* Check if current line should be centered and if so, center |
| 754 | it. */ |
| 755 | if(renderstate & HTMLPARSER_RENDERSTATE_CENTER) { |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 756 | cptr = &webpage[(WWW_CONF_WEBPAGE_HEIGHT - 0) * |
| 757 | WWW_CONF_WEBPAGE_WIDTH - 1]; |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 758 | for(spaces = 0; spaces < WWW_CONF_WEBPAGE_WIDTH; ++spaces) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 759 | if(*cptr-- != ' ') { |
| 760 | break; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | spaces = spaces / 2; |
| 765 | |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 766 | memcpy(tmpcenterline, |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 767 | &webpage[(WWW_CONF_WEBPAGE_HEIGHT - 1) * |
| 768 | WWW_CONF_WEBPAGE_WIDTH], |
| 769 | WWW_CONF_WEBPAGE_WIDTH); |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 770 | memcpy(&webpage[(WWW_CONF_WEBPAGE_HEIGHT - 1) * |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 771 | WWW_CONF_WEBPAGE_WIDTH] + spaces, |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 772 | tmpcenterline, |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 773 | WWW_CONF_WEBPAGE_WIDTH - spaces); |
| 774 | memset(&webpage[(WWW_CONF_WEBPAGE_HEIGHT - 1) * |
| 775 | WWW_CONF_WEBPAGE_WIDTH], ' ', spaces); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 776 | |
| 777 | linksptr = pagewidgets; |
| 778 | for(i = 0; i < pagewidgetptr; ++i) { |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 779 | if(CTK_WIDGET_YPOS(linksptr) == 3 + WWW_CONF_WEBPAGE_HEIGHT - 1) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 780 | linksptr->x += spaces; |
| 781 | linksptr->widget.hyperlink.text += spaces; |
| 782 | } |
| 783 | ++linksptr; |
| 784 | } |
| 785 | } |
| 786 | #endif /* WWW_CONF_RENDERSTATE */ |
| 787 | |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 788 | petsciiconv_topetscii(&webpage[(WWW_CONF_WEBPAGE_HEIGHT - 1) * |
| 789 | WWW_CONF_WEBPAGE_WIDTH], WWW_CONF_WEBPAGE_WIDTH); |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 790 | /* redraw_window();*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 791 | scroll(); |
| 792 | ++scrolly; |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 793 | #if WWW_CONF_PAGEVIEW |
| 794 | if(scrolly == scrollend) { |
| 795 | run = 0; |
| 796 | webclient_close(); |
| 797 | } |
adamdunkels | 11bb946 | 2003-08-11 22:25:07 +0000 | [diff] [blame] | 798 | #else /* WWW_CONF_PAGEVIEW */ |
| 799 | redraw_window(); |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 800 | #endif /* WWW_CONF_PAGEVIEW */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | /*-----------------------------------------------------------------------------------*/ |
| 804 | /* webclient_datahandler(): |
| 805 | * |
| 806 | * Callback function. Called from the webclient module when HTTP data |
| 807 | * has arrived. |
| 808 | */ |
| 809 | void |
| 810 | webclient_datahandler(char *data, u16_t len) |
| 811 | { |
| 812 | if(len > 0) { |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 813 | if(strcmp(webclient_mimetype(), http_texthtml) == 0) { |
| 814 | count = (count + 1) & 3; |
| 815 | show_statustext(receivingmsgs[count]); |
| 816 | htmlparser_parse(data, len); |
| 817 | } else { |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 818 | uip_abort(); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 819 | ctk_dialog_open(&wgetdialog); |
| 820 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 821 | } else { |
| 822 | /* Clear remaining parts of page. */ |
| 823 | run = 0; |
| 824 | } |
| 825 | |
| 826 | if(data == NULL) { |
| 827 | run = 0; |
| 828 | show_statustext("Done."); |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 829 | petsciiconv_topetscii(&webpage[(WWW_CONF_WEBPAGE_HEIGHT - 1) * |
| 830 | WWW_CONF_WEBPAGE_WIDTH], WWW_CONF_WEBPAGE_WIDTH); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 831 | redraw_window(); |
| 832 | } |
| 833 | } |
| 834 | /*-----------------------------------------------------------------------------------*/ |
| 835 | /* output_word(): |
| 836 | * |
| 837 | * Called from the rendering code when a full word has been received |
| 838 | * and should be put on screen. |
| 839 | */ |
| 840 | static void |
| 841 | output_word(char c) |
| 842 | { |
| 843 | char *webpageptr; |
| 844 | |
| 845 | if(nextwordptr == 0) { |
| 846 | if(c == ISO_nl) { |
| 847 | x = 0; |
| 848 | inc_y(); |
| 849 | } |
| 850 | return; |
| 851 | } |
| 852 | |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 853 | if(x + nextwordptr > WWW_CONF_WEBPAGE_WIDTH) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 854 | inc_y(); |
| 855 | x = 0; |
| 856 | } |
| 857 | |
| 858 | nextword[nextwordptr] = 0; |
| 859 | if(starty == 0) { |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 860 | webpageptr = &webpage[(WWW_CONF_WEBPAGE_HEIGHT - 1) * WWW_CONF_WEBPAGE_WIDTH + x]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 861 | if(nextwordptr > 0) { |
| 862 | strcpy(webpageptr, nextword); |
| 863 | } |
| 864 | webpageptr[nextwordptr] = ' '; |
| 865 | } |
| 866 | if(c == ISO_nl) { |
| 867 | x = 0; |
| 868 | inc_y(); |
| 869 | } else { |
| 870 | x += nextwordptr + 1; |
| 871 | } |
| 872 | nextwordptr = 0; |
| 873 | } |
| 874 | /*-----------------------------------------------------------------------------------*/ |
| 875 | static void * |
| 876 | add_pagewidget(char *text, unsigned char type, |
| 877 | unsigned char border) |
| 878 | { |
adamdunkels | d59dadf | 2003-08-15 18:48:35 +0000 | [diff] [blame] | 879 | register struct ctk_widget *lptr; |
adamdunkels | 1a23b5b | 2003-08-05 13:50:02 +0000 | [diff] [blame] | 880 | register unsigned char *webpageptr; |
adamdunkels | fb565d9 | 2003-08-13 22:51:07 +0000 | [diff] [blame] | 881 | static unsigned char len, maxwidth; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 882 | static void *dataptr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 883 | |
| 884 | len = strlen(text); |
| 885 | |
| 886 | if(len + border == 0) { |
| 887 | return NULL; |
| 888 | } |
| 889 | |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 890 | maxwidth = WWW_CONF_WEBPAGE_WIDTH - (1 + 2 * border); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 891 | |
| 892 | /* If the text of the link is too long so that it does not fit into |
| 893 | the width of the current window, counting from the current x |
| 894 | coordinate, we first try to jump to the next line. */ |
| 895 | if(len + x > maxwidth) { |
| 896 | output_word(ISO_nl); |
| 897 | } |
| 898 | |
| 899 | /* If the text of the link still is too long, we just chop it off! |
| 900 | XXX: this is not really the right thing to do, we should probably |
| 901 | either make the link a multiline button, or add multiple |
| 902 | buttons. But this will do for now. */ |
| 903 | if(len > maxwidth) { |
| 904 | text[maxwidth] = 0; |
| 905 | len = maxwidth; |
| 906 | } |
| 907 | |
| 908 | dataptr = NULL; |
| 909 | |
| 910 | if(starty == 0) { |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 911 | webpageptr = &webpage[(WWW_CONF_WEBPAGE_HEIGHT - 1) * |
| 912 | WWW_CONF_WEBPAGE_WIDTH + x]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 913 | /* To save memory, we'll copy the widget text to the web page |
| 914 | drawing area and reference it from there. */ |
| 915 | webpageptr[0] = 0; |
| 916 | webpageptr += border; |
| 917 | strncpy(webpageptr, text, len); |
| 918 | webpageptr[len] = 0; |
| 919 | webpageptr[len + border] = ' '; |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 920 | if(pagewidgetptr < WWW_CONF_MAX_NUMPAGEWIDGETS) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 921 | dataptr = &pagewidgetattribs[pagewidgetptr]; |
| 922 | lptr = &pagewidgets[pagewidgetptr]; |
| 923 | |
| 924 | switch(type) { |
| 925 | case CTK_WIDGET_HYPERLINK: |
| 926 | CTK_HYPERLINK_NEW((struct ctk_hyperlink *)lptr, x, |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 927 | WWW_CONF_WEBPAGE_HEIGHT + 2, len, |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 928 | webpageptr, dataptr); |
| 929 | break; |
| 930 | case CTK_WIDGET_BUTTON: |
| 931 | CTK_BUTTON_NEW((struct ctk_button *)lptr, x, |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 932 | WWW_CONF_WEBPAGE_HEIGHT + 2, len, |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 933 | webpageptr); |
| 934 | ((struct formattribs *)dataptr)->inputvalue = webpageptr; |
| 935 | break; |
| 936 | case CTK_WIDGET_TEXTENTRY: |
| 937 | CTK_TEXTENTRY_NEW((struct ctk_textentry *)lptr, |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 938 | x, WWW_CONF_WEBPAGE_HEIGHT + 2, len, 1, |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 939 | webpageptr, len); |
| 940 | ((struct formattribs *)dataptr)->inputvalue = webpageptr; |
| 941 | break; |
| 942 | } |
| 943 | CTK_WIDGET_ADD(&mainwindow, lptr); |
| 944 | |
| 945 | ++pagewidgetptr; |
| 946 | } |
| 947 | } |
| 948 | /* Increase the x coordinate with the length of the link text plus |
| 949 | the extra space behind it and the CTK button markers. */ |
| 950 | x += len + 1 + 2 * border; |
| 951 | |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 952 | if(x >= WWW_CONF_WEBPAGE_WIDTH) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 953 | inc_y(); |
| 954 | x = 0; |
| 955 | } |
| 956 | |
| 957 | return dataptr; |
| 958 | } |
| 959 | /*-----------------------------------------------------------------------------------*/ |
| 960 | /* htmlparser_link: |
| 961 | * |
| 962 | * Callback function. Will be called when the HTML parser has parsed a |
| 963 | * link. We will put a CTK hyperlink widget at the appropriate position |
| 964 | * in the window. |
| 965 | */ |
| 966 | void |
| 967 | htmlparser_link(char *text, char *url) |
| 968 | { |
| 969 | static unsigned char *linkurlptr; |
| 970 | |
| 971 | linkurlptr = add_pagewidget(text, CTK_WIDGET_HYPERLINK, 0); |
| 972 | if(linkurlptr != NULL && |
| 973 | strlen(url) < WWW_CONF_MAX_URLLEN) { |
| 974 | strcpy(linkurlptr, url); |
| 975 | } |
| 976 | } |
| 977 | /*-----------------------------------------------------------------------------------*/ |
| 978 | /* htmlparser_char(): |
| 979 | * |
| 980 | * Callback function. Called by the HTML parser module for every |
| 981 | * printable character in the HTML file. |
| 982 | */ |
| 983 | void |
| 984 | htmlparser_char(char c) |
| 985 | { |
| 986 | if(c == ' ' || |
| 987 | c == ISO_nl) { |
| 988 | output_word(c); |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 989 | } else if(c != 0 && (c & 0x80) == 0) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 990 | nextword[nextwordptr] = c; |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 991 | if(nextwordptr < WWW_CONF_WEBPAGE_WIDTH) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 992 | ++nextwordptr; |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | /*-----------------------------------------------------------------------------------*/ |
| 997 | #if WWW_CONF_RENDERSTATE |
| 998 | void |
| 999 | htmlparser_renderstate(unsigned char s) |
| 1000 | { |
| 1001 | if((s & HTMLPARSER_RENDERSTATE_STATUSMASK) == |
| 1002 | HTMLPARSER_RENDERSTATE_BEGIN) { |
| 1003 | renderstate |= s & ~HTMLPARSER_RENDERSTATE_STATUSMASK; |
| 1004 | } else { |
| 1005 | renderstate &= ~(s & ~HTMLPARSER_RENDERSTATE_STATUSMASK); |
| 1006 | } |
| 1007 | } |
| 1008 | #endif /* WWW_CONF_RENDERSTATE */ |
| 1009 | |
| 1010 | #if WWW_CONF_FORMS |
| 1011 | /*-----------------------------------------------------------------------------------*/ |
| 1012 | void |
| 1013 | htmlparser_submitbutton(char *text, char *name, |
| 1014 | char *formname, char *formaction) |
| 1015 | { |
| 1016 | register struct formattribs *form; |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 1017 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1018 | form = add_pagewidget(text, CTK_WIDGET_BUTTON, 1); |
| 1019 | if(form != NULL) { |
| 1020 | strncpy(form->formaction, formaction, WWW_CONF_MAX_FORMACTIONLEN); |
| 1021 | strncpy(form->formname, formname, WWW_CONF_MAX_FORMNAMELEN); |
| 1022 | strncpy(form->inputname, name, WWW_CONF_MAX_INPUTNAMELEN); |
| 1023 | form->inputtype = FORMINPUTTYPE_SUBMITBUTTON; |
| 1024 | } |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 1025 | |
| 1026 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1027 | } |
| 1028 | /*-----------------------------------------------------------------------------------*/ |
| 1029 | void |
| 1030 | htmlparser_inputfield(char *text, char *name, |
| 1031 | char *formname, char *formaction) |
| 1032 | { |
| 1033 | register struct formattribs *form; |
| 1034 | |
| 1035 | form = add_pagewidget(text, CTK_WIDGET_TEXTENTRY, 1); |
| 1036 | if(form != NULL) { |
| 1037 | strncpy(form->formaction, formaction, WWW_CONF_MAX_FORMACTIONLEN); |
| 1038 | strncpy(form->formname, formname, WWW_CONF_MAX_FORMNAMELEN); |
| 1039 | strncpy(form->inputname, name, WWW_CONF_MAX_INPUTNAMELEN); |
| 1040 | form->inputtype = FORMINPUTTYPE_INPUTFIELD; |
| 1041 | } |
| 1042 | } |
| 1043 | /*-----------------------------------------------------------------------------------*/ |
| 1044 | static void |
| 1045 | formsubmit(struct formattribs *attribs) |
| 1046 | { |
| 1047 | unsigned char i, j; |
adamdunkels | 1a23b5b | 2003-08-05 13:50:02 +0000 | [diff] [blame] | 1048 | register char *urlptr, *valueptr; |
| 1049 | register struct formattribs *faptr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1050 | |
| 1051 | urlptr = &tmpurl[0]; |
| 1052 | |
| 1053 | strncpy(urlptr, attribs->formaction, WWW_CONF_MAX_URLLEN); |
| 1054 | tmpurl[WWW_CONF_MAX_URLLEN] = 0; |
| 1055 | urlptr += strlen(urlptr); |
| 1056 | *urlptr = ISO_questionmark; |
| 1057 | ++urlptr; |
| 1058 | |
| 1059 | |
| 1060 | /* Construct an URL by finding all input field forms with the same |
| 1061 | formname as the current submit button, and add the submit button |
| 1062 | URL stuff as well. */ |
| 1063 | for(i = 0; i < pagewidgetptr; ++i) { |
| 1064 | if(urlptr - &tmpurl[0] >= WWW_CONF_MAX_URLLEN) { |
| 1065 | break; |
| 1066 | } |
| 1067 | |
| 1068 | faptr = &pagewidgetattribs[i].form; |
| 1069 | |
| 1070 | if(strcmp(attribs->formaction, faptr->formaction) == 0 && |
| 1071 | strcmp(attribs->formname, faptr->formname) == 0 && |
| 1072 | (faptr->inputtype == FORMINPUTTYPE_INPUTFIELD || |
| 1073 | faptr == attribs)) { |
| 1074 | |
| 1075 | /* Copy the name of the input field into the URL and append a |
| 1076 | questionmark. */ |
| 1077 | strncpy(urlptr, faptr->inputname, WWW_CONF_MAX_URLLEN - strlen(tmpurl)); |
| 1078 | tmpurl[WWW_CONF_MAX_URLLEN] = 0; |
| 1079 | urlptr += strlen(urlptr); |
| 1080 | *urlptr = ISO_eq; |
| 1081 | ++urlptr; |
| 1082 | |
| 1083 | /* Convert and copy the contents of the input field to the URL |
| 1084 | and append an ampersand. */ |
| 1085 | valueptr = pagewidgets[i].widget.textentry.text; |
| 1086 | petsciiconv_toascii(valueptr, WWW_CONF_MAX_INPUTVALUELEN); |
| 1087 | for(j = 0; j < WWW_CONF_MAX_INPUTVALUELEN; ++j) { |
| 1088 | if(urlptr - &tmpurl[0] >= WWW_CONF_MAX_URLLEN) { |
| 1089 | break; |
| 1090 | } |
| 1091 | *urlptr = *valueptr; |
| 1092 | if(*urlptr == ISO_space) { |
| 1093 | *urlptr = ISO_plus; |
| 1094 | } |
| 1095 | if(*urlptr == 0) { |
| 1096 | break; |
| 1097 | } |
| 1098 | ++urlptr; |
| 1099 | ++valueptr; |
| 1100 | } |
| 1101 | |
| 1102 | *urlptr = ISO_ampersand; |
| 1103 | ++urlptr; |
| 1104 | } |
| 1105 | } |
| 1106 | --urlptr; |
| 1107 | *urlptr = 0; |
| 1108 | /* PRINTF(("formsubmit: URL '%s'\n", tmpurl));*/ |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 1109 | log_back(); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1110 | open_link(tmpurl); |
| 1111 | } |
| 1112 | /*-----------------------------------------------------------------------------------*/ |
| 1113 | #endif /* WWW_CONF_FORMS */ |