Add missing includes
[contiki-2.x.git] / apps / webbrowser / www.c
blobed28e145ee2dc82f17834a306d0773b7f6023f88
1 /*
2 * Copyright (c) 2002, Adam Dunkels.
3 * All rights reserved.
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. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior
16 * written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * This file is part of the Contiki desktop environment
32 * $Id: www.c,v 1.14 2010/07/21 21:03:06 oliverschmidt Exp $
36 #include <string.h>
38 #include "ctk/ctk.h"
39 #include "lib/ctk-textentry-cmdline.h"
40 #include "contiki-net.h"
41 #include "lib/petsciiconv.h"
42 #include "sys/arg.h"
43 #if WWW_CONF_WITH_WGET
44 #include "program-handler.h"
45 #endif /* WWW_CONF_WITH_WGET */
47 #include "webclient.h"
48 #include "htmlparser.h"
49 #include "http-strings.h"
51 #include "www.h"
53 #if 1
54 #define PRINTF(x)
55 #else
56 #include <stdio.h>
57 #define PRINTF(x) printf x
58 #endif
61 /* The array that holds the current URL. */
62 static char url[WWW_CONF_MAX_URLLEN + 1];
63 static char tmpurl[WWW_CONF_MAX_URLLEN + 1];
65 /* The array that holds the web page text. */
66 static char webpage[WWW_CONF_WEBPAGE_WIDTH *
67 WWW_CONF_WEBPAGE_HEIGHT + 1];
70 /* The CTK widgets for the main window. */
71 static struct ctk_window mainwindow;
73 #if WWW_CONF_HISTORY_SIZE > 0
74 static struct ctk_button backbutton =
75 {CTK_BUTTON(0, 0, 4, "Back")};
76 static struct ctk_button downbutton =
77 {CTK_BUTTON(10, 0, 4, "Down")};
78 #else /* WWW_CONF_HISTORY_SIZE > 0 */
79 static struct ctk_button downbutton =
80 {CTK_BUTTON(0, 0, 4, "Down")};
81 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
82 static struct ctk_button stopbutton =
83 {CTK_BUTTON(WWW_CONF_WEBPAGE_WIDTH - 16, 0, 4, "Stop")};
84 static struct ctk_button gobutton =
85 {CTK_BUTTON(WWW_CONF_WEBPAGE_WIDTH - 4, 0, 2, "Go")};
87 static struct ctk_separator sep1 =
88 {CTK_SEPARATOR(0, 2, WWW_CONF_WEBPAGE_WIDTH)};
90 static char editurl[WWW_CONF_MAX_URLLEN + 1];
91 static struct ctk_textentry urlentry =
92 {CTK_TEXTENTRY(0, 1, WWW_CONF_WEBPAGE_WIDTH - 2,
93 1, editurl, WWW_CONF_MAX_URLLEN)};
94 static struct ctk_label webpagelabel =
95 {CTK_LABEL(0, 3, WWW_CONF_WEBPAGE_WIDTH,
96 WWW_CONF_WEBPAGE_HEIGHT, webpage)};
98 static char statustexturl[WWW_CONF_WEBPAGE_WIDTH];
99 static struct ctk_label statustext =
100 {CTK_LABEL(0, WWW_CONF_WEBPAGE_HEIGHT + 4,
101 WWW_CONF_WEBPAGE_WIDTH, 1, "")};
102 static struct ctk_separator sep2 =
103 {CTK_SEPARATOR(0, WWW_CONF_WEBPAGE_HEIGHT + 3,
104 WWW_CONF_WEBPAGE_WIDTH)};
106 #if WWW_CONF_WITH_WGET
107 static struct ctk_window wgetdialog;
108 static struct ctk_label wgetlabel1 =
109 {CTK_LABEL(1, 1, 34, 1, "This web page cannot be displayed.")};
110 static struct ctk_label wgetlabel2 =
111 {CTK_LABEL(1, 3, 35, 1, "Would you like to download instead?")};
112 static struct ctk_button wgetnobutton =
113 {CTK_BUTTON(1, 5, 6, "Cancel")};
114 static struct ctk_button wgetyesbutton =
115 {CTK_BUTTON(11, 5, 24, "Close browser & download")};
116 #endif /* WWW_CONF_WITH_WGET */
118 #if WWW_CONF_HISTORY_SIZE > 0
119 /* The char arrays that hold the history of visited URLs. */
120 static char history[WWW_CONF_HISTORY_SIZE][WWW_CONF_MAX_URLLEN];
121 static char history_last;
122 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
124 /* The CTK widget definitions for the hyperlinks and the char arrays
125 that hold the link URLs. */
126 struct formattribs {
127 char formaction[WWW_CONF_MAX_FORMACTIONLEN];
128 char formname[WWW_CONF_MAX_FORMNAMELEN];
129 #define FORMINPUTTYPE_SUBMITBUTTON 1
130 #define FORMINPUTTYPE_INPUTFIELD 2
131 unsigned char inputtype;
132 char inputname[WWW_CONF_MAX_INPUTNAMELEN];
133 char *inputvalue;
136 union pagewidgetattrib {
137 char url[WWW_CONF_MAX_URLLEN];
138 #if WWW_CONF_FORMS
139 struct formattribs form;
140 #endif /* WWW_CONF_FORMS */
142 static struct ctk_widget pagewidgets[WWW_CONF_MAX_NUMPAGEWIDGETS];
143 static union pagewidgetattrib pagewidgetattribs[WWW_CONF_MAX_NUMPAGEWIDGETS];
144 static unsigned char pagewidgetptr;
146 #if WWW_CONF_RENDERSTATE
147 static unsigned char renderstate;
148 #endif /* WWW_CONF_RENDERSTATE */
150 #define ISO_nl 0x0a
151 #define ISO_space 0x20
152 #define ISO_ampersand 0x26
153 #define ISO_plus 0x2b
154 #define ISO_slash 0x2f
155 #define ISO_eq 0x3d
156 #define ISO_questionmark 0x3f
158 /* The state of the rendering code. */
159 static char *webpageptr;
160 static unsigned char x, y;
161 static unsigned char loading;
162 static unsigned short firsty, pagey;
164 static unsigned char count;
165 static char receivingmsgs[4][23] = {
166 "Receiving web page ...",
167 "Receiving web page. ..",
168 "Receiving web page.. .",
169 "Receiving web page... "
172 PROCESS(www_process, "Web browser");
174 AUTOSTART_PROCESSES(&www_process);
176 static void formsubmit(struct formattribs *attribs);
178 /*-----------------------------------------------------------------------------------*/
179 /* make_window()
181 * Creates the web browser's window.
183 static void
184 make_window(void)
186 #if WWW_CONF_HISTORY_SIZE > 0
187 CTK_WIDGET_ADD(&mainwindow, &backbutton);
188 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
189 CTK_WIDGET_ADD(&mainwindow, &downbutton);
190 CTK_WIDGET_ADD(&mainwindow, &stopbutton);
191 CTK_WIDGET_ADD(&mainwindow, &gobutton);
192 CTK_WIDGET_ADD(&mainwindow, &urlentry);
193 CTK_WIDGET_ADD(&mainwindow, &sep1);
194 CTK_WIDGET_ADD(&mainwindow, &webpagelabel);
195 CTK_WIDGET_SET_FLAG(&webpagelabel, CTK_WIDGET_FLAG_MONOSPACE);
196 CTK_WIDGET_ADD(&mainwindow, &sep2);
197 CTK_WIDGET_ADD(&mainwindow, &statustext);
199 pagewidgetptr = 0;
201 /*-----------------------------------------------------------------------------------*/
202 /* redraw_window():
204 * Convenience function that calls upon CTK to redraw the browser
205 * window. */
206 static void
207 redraw_window(void)
209 ctk_window_redraw(&mainwindow);
211 /*-----------------------------------------------------------------------------------*/
212 static void
213 clear_page(void)
215 ctk_window_clear(&mainwindow);
216 make_window();
217 redraw_window();
218 memset(webpage, 0, WWW_CONF_WEBPAGE_WIDTH * WWW_CONF_WEBPAGE_HEIGHT);
220 /*-----------------------------------------------------------------------------------*/
221 static void
222 show_url(void)
224 memcpy(editurl, url, WWW_CONF_MAX_URLLEN);
225 strncpy(editurl, "http://", 7);
226 petsciiconv_topetscii(editurl + 7, WWW_CONF_MAX_URLLEN - 7);
227 CTK_WIDGET_REDRAW(&urlentry);
229 /*-----------------------------------------------------------------------------------*/
230 static void
231 start_loading(void)
233 loading = 1;
234 x = y = 0;
235 pagey = 0;
236 webpageptr = webpage;
238 clear_page();
240 /*-----------------------------------------------------------------------------------*/
241 static void
242 show_statustext(char *text)
244 ctk_label_set_text(&statustext, text);
245 CTK_WIDGET_REDRAW(&statustext);
247 /*-----------------------------------------------------------------------------------*/
248 /* open_url():
250 * Called when the URL present in the global "url" variable should be
251 * opened. It will call the hostname resolver as well as the HTTP
252 * client requester.
254 static void
255 open_url(void)
257 unsigned char i;
258 static char host[32];
259 char *file;
260 register char *urlptr;
261 static uip_ipaddr_t addr;
263 /* Trim off any spaces in the end of the url. */
264 urlptr = url + strlen(url) - 1;
265 while(*urlptr == ' ' && urlptr > url) {
266 *urlptr = 0;
267 --urlptr;
270 /* Don't even try to go further if the URL is empty. */
271 if(urlptr == url) {
272 return;
275 /* See if the URL starts with http://, otherwise prepend it. */
276 if(strncmp(url, http_http, 7) != 0) {
277 while(urlptr >= url) {
278 *(urlptr + 7) = *urlptr;
279 --urlptr;
281 strncpy(url, http_http, 7);
284 /* Find host part of the URL. */
285 urlptr = &url[7];
286 for(i = 0; i < sizeof(host); ++i) {
287 if(*urlptr == 0 ||
288 *urlptr == '/' ||
289 *urlptr == ' ' ||
290 *urlptr == ':') {
291 host[i] = 0;
292 break;
294 host[i] = *urlptr;
295 ++urlptr;
298 /* XXX: Here we should find the port part of the URL, but this isn't
299 currently done because of laziness from the programmer's side
300 :-) */
302 /* Find file part of the URL. */
303 while(*urlptr != '/' && *urlptr != 0) {
304 ++urlptr;
306 if(*urlptr == '/') {
307 file = urlptr;
308 } else {
309 file = "/";
312 #if UIP_UDP
313 /* Try to lookup the hostname. If it fails, we initiate a hostname
314 lookup and print out an informative message on the statusbar. */
315 if(uiplib_ipaddrconv(host, &addr) == 0) {
316 if(resolv_lookup(host) == NULL) {
317 resolv_query(host);
318 show_statustext("Resolving host...");
319 return;
322 #else /* UIP_UDP */
323 uiplib_ipaddrconv(host, &addr);
324 #endif /* UIP_UDP */
326 /* The hostname we present in the hostname table, so we send out the
327 initial GET request. */
328 if(webclient_get(host, 80, file) == 0) {
329 show_statustext("Out of memory error.");
330 } else {
331 show_statustext("Connecting...");
333 redraw_window();
335 /*-----------------------------------------------------------------------------------*/
336 /* open_link(link):
338 * Will format a link from the current web pages so that it suits the
339 * open_url() function and finally call it to open the requested URL.
341 static void
342 open_link(char *link)
344 char *urlptr;
346 if(strncmp(link, http_http, 7) == 0) {
347 /* The link starts with http://. We just copy the contents of the
348 link into the url string and jump away. */
349 strncpy(url, link, WWW_CONF_MAX_URLLEN);
350 } else if(*link == ISO_slash &&
351 *(link + 1) == ISO_slash) {
352 /* The link starts with //, so we'll copy it into the url
353 variable, starting after the http (which already is present in
354 the url variable since we were able to open the web page on
355 which this link was found in the first place). */
356 strncpy(&url[5], link, WWW_CONF_MAX_URLLEN);
357 } else if(*link == ISO_slash) {
358 /* The link starts with a slash, so it is a non-relative link
359 within the same web site. We find the start of the filename of
360 the current URL and paste the contents of this link there, and
361 head off to the new URL. */
362 for(urlptr = &url[7];
363 *urlptr != 0 && *urlptr != ISO_slash;
364 ++urlptr);
365 strncpy(urlptr, link, WWW_CONF_MAX_URLLEN - (urlptr - url));
366 } else {
367 /* A fully relative link is found. We find the last slash in the
368 current URL and paste the link there. */
370 /* XXX: we should really parse any ../ in the link as well. */
371 for(urlptr = url + strlen(url);
372 urlptr != url && *urlptr != ISO_slash;
373 --urlptr);
374 ++urlptr;
375 strncpy(urlptr, link, WWW_CONF_MAX_URLLEN - (urlptr - url));
378 /* Open the URL. */
379 show_url();
380 open_url();
382 start_loading();
384 /*-----------------------------------------------------------------------------------*/
385 #if WWW_CONF_HISTORY_SIZE > 0
386 /* log_back():
388 * Copies the current URL from the url variable and into the log for
389 * the back button.
391 static void
392 log_back(void)
394 if(strncmp(url, history[(int)history_last], WWW_CONF_MAX_URLLEN) != 0) {
395 memcpy(history[(int)history_last], url, WWW_CONF_MAX_URLLEN);
396 ++history_last;
397 if(history_last >= WWW_CONF_HISTORY_SIZE) {
398 history_last = 0;
402 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
403 /*-----------------------------------------------------------------------------------*/
404 static void
405 quit(void)
407 ctk_window_close(&mainwindow);
408 process_exit(&www_process);
409 LOADER_UNLOAD();
411 /*-----------------------------------------------------------------------------------*/
412 /* www_process():
414 * The program's signal dispatcher function. Is called whenever a signal arrives.
416 PROCESS_THREAD(www_process, ev, data)
418 static struct ctk_widget *w;
419 static unsigned char i;
420 #if WWW_CONF_WITH_WGET
421 static char *argptr;
422 #endif /* WWW_CONF_WITH_WGET */
424 w = (struct ctk_widget *)data;
426 PROCESS_BEGIN();
428 /* Create the main window. */
429 memset(webpage, 0, sizeof(webpage));
430 ctk_window_new(&mainwindow, WWW_CONF_WEBPAGE_WIDTH,
431 WWW_CONF_WEBPAGE_HEIGHT+5, "Web browser");
432 make_window();
433 #ifdef WWW_CONF_HOMEPAGE
434 strncpy(editurl, WWW_CONF_HOMEPAGE, sizeof(editurl));
435 #endif /* WWW_CONF_HOMEPAGE */
436 CTK_WIDGET_FOCUS(&mainwindow, &urlentry);
438 #if WWW_CONF_WITH_WGET
439 /* Create download dialog.*/
440 ctk_dialog_new(&wgetdialog, 38, 7);
441 CTK_WIDGET_ADD(&wgetdialog, &wgetlabel1);
442 CTK_WIDGET_ADD(&wgetdialog, &wgetlabel2);
443 CTK_WIDGET_ADD(&wgetdialog, &wgetnobutton);
444 CTK_WIDGET_ADD(&wgetdialog, &wgetyesbutton);
445 #endif /* WWW_CONF_WITH_WGET */
447 ctk_window_open(&mainwindow);
449 while(1) {
451 PROCESS_WAIT_EVENT();
453 if(ev == tcpip_event) {
454 webclient_appcall(data);
455 } else if(ev == ctk_signal_widget_activate) {
456 if(w == (struct ctk_widget *)&gobutton ||
457 w == (struct ctk_widget *)&urlentry) {
458 start_loading();
459 firsty = 0;
460 #if WWW_CONF_HISTORY_SIZE > 0
461 log_back();
462 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
463 memcpy(url, editurl, WWW_CONF_MAX_URLLEN);
464 petsciiconv_toascii(url, WWW_CONF_MAX_URLLEN);
465 open_url();
466 CTK_WIDGET_FOCUS(&mainwindow, &gobutton);
467 #if WWW_CONF_HISTORY_SIZE > 0
468 } else if(w == (struct ctk_widget *)&backbutton) {
469 firsty = 0;
470 start_loading();
471 --history_last;
472 if(history_last > WWW_CONF_HISTORY_SIZE) {
473 history_last = WWW_CONF_HISTORY_SIZE - 1;
475 memcpy(url, history[(int)history_last], WWW_CONF_MAX_URLLEN);
476 open_url();
477 CTK_WIDGET_FOCUS(&mainwindow, &backbutton);
478 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
479 } else if(w == (struct ctk_widget *)&downbutton) {
480 firsty = pagey + WWW_CONF_WEBPAGE_HEIGHT - 4;
481 start_loading();
482 open_url();
483 CTK_WIDGET_FOCUS(&mainwindow, &downbutton);
484 } else if(w == (struct ctk_widget *)&stopbutton) {
485 loading = 0;
486 webclient_close();
487 #if WWW_CONF_WITH_WGET
488 } else if(w == (struct ctk_widget *)&wgetnobutton) {
489 ctk_dialog_close();
490 } else if(w == (struct ctk_widget *)&wgetyesbutton) {
491 ctk_dialog_close();
492 quit();
493 argptr = arg_alloc((char)WWW_CONF_MAX_URLLEN);
494 if(argptr != NULL) {
495 strncpy(argptr, url, WWW_CONF_MAX_URLLEN);
497 program_handler_load("wget.prg", argptr);
498 #endif /* WWW_CONF_WITH_WGET */
499 #if WWW_CONF_FORMS
500 } else {
501 /* Check form buttons */
502 for(i = 0; i < pagewidgetptr; ++i) {
503 if(&pagewidgets[i] == w) {
504 formsubmit(&pagewidgetattribs[i].form);
505 /* show_statustext(pagewidgetattribs[i].form.formaction);*/
506 /* PRINTF(("Formaction %s formname %s inputname %s\n",
507 pagewidgetattribs[i].form.formaction,
508 pagewidgetattribs[i].form.formname,
509 pagewidgetattribs[i].form.inputname));*/
510 break;
513 #endif /* WWW_CONF_FORMS */
515 } else if(ev == ctk_signal_hyperlink_activate) {
516 firsty = 0;
517 #if WWW_CONF_HISTORY_SIZE > 0
518 log_back();
519 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
520 open_link(w->widget.hyperlink.url);
521 CTK_WIDGET_FOCUS(&mainwindow, &stopbutton);
522 /* ctk_window_open(&mainwindow);*/
523 } else if(ev == ctk_signal_hyperlink_hover) {
524 if(CTK_WIDGET_TYPE((struct ctk_widget *)data) ==
525 CTK_WIDGET_HYPERLINK) {
526 strncpy(statustexturl, w->widget.hyperlink.url,
527 sizeof(statustexturl));
528 petsciiconv_topetscii(statustexturl, sizeof(statustexturl));
529 show_statustext(statustexturl);
531 #if UIP_UDP
532 } else if(ev == resolv_event_found) {
533 /* Either found a hostname, or not. */
534 if((char *)data != NULL &&
535 resolv_lookup((char *)data) != NULL) {
536 open_url();
537 } else {
538 show_statustext("Host not found.");
540 #endif /* UIP_UDP */
541 } else if(ev == ctk_signal_window_close ||
542 ev == PROCESS_EVENT_EXIT) {
543 quit();
546 PROCESS_END();
548 /*-----------------------------------------------------------------------------------*/
549 /* set_url():
551 * Constructs an URL from the arguments and puts it into the global
552 * "url" variable and the visible "editurl" (which is shown in the URL
553 * text entry widget in the browser window).
555 static void
556 set_url(char *host, u16_t port, char *file)
558 char *urlptr;
560 memset(url, 0, WWW_CONF_MAX_URLLEN);
562 if(strncmp(file, http_http, 7) == 0) {
563 strncpy(url, file, sizeof(url));
564 } else {
565 strncpy(url, http_http, 7);
566 urlptr = url + 7;
567 strcpy(urlptr, host);
568 urlptr += strlen(host);
569 strcpy(urlptr, file);
572 show_url();
574 /*-----------------------------------------------------------------------------------*/
575 /* webclient_aborted():
577 * Callback function. Called from the webclient when the HTTP
578 * connection was abruptly aborted.
580 void
581 webclient_aborted(void)
583 show_statustext("Connection reset by peer");
585 /*-----------------------------------------------------------------------------------*/
586 /* webclient_timedout():
588 * Callback function. Called from the webclient when the HTTP
589 * connection timed out.
591 void
592 webclient_timedout(void)
594 show_statustext("Connection timed out");
596 /*-----------------------------------------------------------------------------------*/
597 /* webclient_closed():
599 * Callback function. Called from the webclient when the HTTP
600 * connection was closed after a request from the "webclient_close()"
601 * function.
603 void
604 webclient_closed(void)
606 show_statustext("Stopped.");
607 petsciiconv_topetscii(webpageptr - x, x);
608 CTK_WIDGET_FOCUS(&mainwindow, &downbutton);
609 redraw_window();
611 /*-----------------------------------------------------------------------------------*/
612 /* webclient_connected():
614 * Callback function. Called from the webclient when the HTTP
615 * connection is connected.
617 void
618 webclient_connected(void)
620 start_loading();
622 clear_page();
624 show_statustext("Request sent...");
625 set_url(webclient_hostname(), webclient_port(), webclient_filename());
627 #if WWW_CONF_RENDERSTATE
628 renderstate = HTMLPARSER_RENDERSTATE_NONE;
629 #endif /* WWW_CONF_RENDERSTATE */
630 htmlparser_init();
632 /*-----------------------------------------------------------------------------------*/
633 /* webclient_datahandler():
635 * Callback function. Called from the webclient module when HTTP data
636 * has arrived.
638 void
639 webclient_datahandler(char *data, u16_t len)
641 if(len > 0) {
642 if(strcmp(webclient_mimetype(), http_texthtml) == 0) {
643 count = (count + 1) & 3;
644 show_statustext(receivingmsgs[count]);
645 htmlparser_parse(data, len);
646 redraw_window();
647 } else {
648 uip_abort();
649 #if WWW_CONF_WITH_WGET
650 ctk_dialog_open(&wgetdialog);
651 #endif /* WWW_CONF_WITH_WGET */
653 } else {
654 /* Clear remaining parts of page. */
655 loading = 0;
658 if(data == NULL) {
659 loading = 0;
660 show_statustext("Done.");
661 petsciiconv_topetscii(webpageptr - x, x);
662 CTK_WIDGET_FOCUS(&mainwindow, &urlentry);
663 redraw_window();
666 /*-----------------------------------------------------------------------------------*/
667 static void *
668 add_pagewidget(char *text, unsigned char len, unsigned char type,
669 unsigned char border)
671 register struct ctk_widget *lptr;
672 register char *wptr;
673 static unsigned char maxwidth;
674 static void *dataptr;
676 if(!loading) {
677 return NULL;
680 if(len + border == 0) {
681 return NULL;
684 maxwidth = WWW_CONF_WEBPAGE_WIDTH - (1 + 2 * border);
686 /* If the text of the link is too long so that it does not fit into
687 the width of the current window, counting from the current x
688 coordinate, we first try to jump to the next line. */
689 if(len + x > maxwidth) {
690 htmlparser_newline();
691 if(!loading) {
692 return NULL;
696 /* If the text of the link still is too long, we just chop it off!
697 XXX: this is not really the right thing to do, we should probably
698 either make a link into a multiline link, or add multiple
699 buttons. But this will do for now. */
700 if(len > maxwidth) {
701 text[maxwidth] = 0;
702 len = maxwidth;
705 dataptr = NULL;
707 if(firsty == pagey) {
708 wptr = webpageptr;
709 /* To save memory, we'll copy the widget text to the web page
710 drawing area and reference it from there. */
711 wptr[0] = 0;
712 wptr += border;
713 memcpy(wptr, text, len);
714 wptr[len] = 0;
715 wptr[len + border] = ' ';
716 if(pagewidgetptr < WWW_CONF_MAX_NUMPAGEWIDGETS) {
717 dataptr = &pagewidgetattribs[pagewidgetptr];
718 lptr = &pagewidgets[pagewidgetptr];
720 switch(type) {
721 case CTK_WIDGET_HYPERLINK:
722 CTK_HYPERLINK_NEW((struct ctk_hyperlink *)lptr, x,
723 y + 3, len,
724 wptr, dataptr);
725 break;
726 case CTK_WIDGET_BUTTON:
727 CTK_BUTTON_NEW((struct ctk_button *)lptr, x,
728 y + 3, len,
729 wptr);
730 ((struct formattribs *)dataptr)->inputvalue = wptr;
731 break;
732 case CTK_WIDGET_TEXTENTRY:
733 CTK_TEXTENTRY_NEW((struct ctk_textentry *)lptr, x,
734 y + 3, len, 1,
735 wptr, len);
736 ((struct formattribs *)dataptr)->inputvalue = wptr;
737 break;
739 CTK_WIDGET_SET_FLAG(lptr, CTK_WIDGET_FLAG_MONOSPACE);
740 CTK_WIDGET_ADD(&mainwindow, lptr);
742 ++pagewidgetptr;
745 /* Increase the x coordinate with the length of the link text plus
746 the extra space behind it and the CTK button markers. */
747 len = len + 1 + 2 * border;
748 x += len;
750 if(firsty == pagey) {
751 webpageptr += len;
754 if(x == WWW_CONF_WEBPAGE_WIDTH) {
755 htmlparser_newline();
758 return dataptr;
760 /*-----------------------------------------------------------------------------------*/
761 #if WWW_CONF_RENDERSTATE
762 static void
763 centerline(char *wptr)
765 unsigned char spaces, i;
766 char *cptr;
767 register struct ctk_widget *linksptr;
769 cptr = wptr + WWW_CONF_WEBPAGE_WIDTH;
770 for(spaces = 0; spaces < WWW_CONF_WEBPAGE_WIDTH; ++spaces) {
771 if(*--cptr != 0) {
772 break;
776 spaces /= 2;
778 while(cptr >= wptr) {
779 *(cptr + spaces) = *cptr;
780 --cptr;
783 memset(wptr, ' ', spaces);
785 linksptr = pagewidgets;
787 for(i = 0; i < pagewidgetptr; ++i) {
788 if(CTK_WIDGET_YPOS(linksptr) == y + 2) {
789 linksptr->x += spaces;
790 linksptr->widget.hyperlink.text += spaces;
792 ++linksptr;
795 #endif /* WWW_CONF_RENDERSTATE */
796 /*-----------------------------------------------------------------------------------*/
797 void
798 htmlparser_newline(void)
800 char *wptr;
802 if(pagey < firsty) {
803 ++pagey;
804 x = 0;
805 return;
808 if(!loading) {
809 return;
812 webpageptr += (WWW_CONF_WEBPAGE_WIDTH - x);
813 ++y;
814 x = 0;
816 wptr = webpageptr - WWW_CONF_WEBPAGE_WIDTH;
817 petsciiconv_topetscii(wptr,
818 WWW_CONF_WEBPAGE_WIDTH);
819 #if WWW_CONF_RENDERSTATE
820 if(renderstate & HTMLPARSER_RENDERSTATE_CENTER) {
821 centerline(wptr);
823 #endif /* WWW_CONF_RENDERSTATE */
825 if(y == WWW_CONF_WEBPAGE_HEIGHT) {
826 loading = 0;
827 webclient_close();
830 /*-----------------------------------------------------------------------------------*/
831 void
832 htmlparser_word(char *word, unsigned char wordlen)
835 if(loading) {
836 if(wordlen + 1 > WWW_CONF_WEBPAGE_WIDTH - x) {
837 htmlparser_newline();
840 if(loading) {
841 if(pagey == firsty) {
842 memcpy(webpageptr, word, wordlen);
843 webpageptr += wordlen;
844 *webpageptr = ' ';
845 ++webpageptr;
847 x += wordlen + 1;
848 if(x == WWW_CONF_WEBPAGE_WIDTH) {
849 htmlparser_newline();
854 /*-----------------------------------------------------------------------------------*/
855 void
856 htmlparser_link(char *text, unsigned char textlen, char *url)
858 static char *linkurlptr;
860 linkurlptr = add_pagewidget(text, textlen, CTK_WIDGET_HYPERLINK, 0);
861 if(linkurlptr != NULL &&
862 strlen(url) < WWW_CONF_MAX_URLLEN) {
863 strcpy(linkurlptr, url);
866 /*-----------------------------------------------------------------------------------*/
867 #if WWW_CONF_RENDERSTATE
868 void
869 htmlparser_renderstate(unsigned char s)
871 if((s & HTMLPARSER_RENDERSTATE_STATUSMASK) ==
872 HTMLPARSER_RENDERSTATE_BEGIN) {
873 renderstate |= s & ~HTMLPARSER_RENDERSTATE_STATUSMASK;
874 } else {
875 renderstate &= ~(s & ~HTMLPARSER_RENDERSTATE_STATUSMASK);
878 #endif /* WWW_CONF_RENDERSTATE */
880 /*-----------------------------------------------------------------------------------*/
881 #if WWW_CONF_FORMS
882 void
883 htmlparser_submitbutton(char *text, char *name,
884 char *formname, char *formaction)
886 register struct formattribs *form;
888 form = add_pagewidget(text, (unsigned char)strlen(text), CTK_WIDGET_BUTTON, 1);
889 if(form != NULL) {
890 strncpy(form->formaction, formaction, WWW_CONF_MAX_FORMACTIONLEN);
891 strncpy(form->formname, formname, WWW_CONF_MAX_FORMNAMELEN);
892 strncpy(form->inputname, name, WWW_CONF_MAX_INPUTNAMELEN);
893 form->inputtype = FORMINPUTTYPE_SUBMITBUTTON;
896 /*-----------------------------------------------------------------------------------*/
897 void
898 htmlparser_inputfield(unsigned char size, char *text, char *name,
899 char *formname, char *formaction)
901 register struct formattribs *form;
903 form = add_pagewidget(text, size, CTK_WIDGET_TEXTENTRY, 1);
904 if(form != NULL) {
905 strncpy(form->formaction, formaction, WWW_CONF_MAX_FORMACTIONLEN);
906 strncpy(form->formname, formname, WWW_CONF_MAX_FORMNAMELEN);
907 strncpy(form->inputname, name, WWW_CONF_MAX_INPUTNAMELEN);
908 form->inputtype = FORMINPUTTYPE_INPUTFIELD;
911 /*-----------------------------------------------------------------------------------*/
912 static void
913 formsubmit(struct formattribs *attribs)
915 unsigned char i, j;
916 register char *urlptr, *valueptr;
917 register struct formattribs *faptr;
919 urlptr = &tmpurl[0];
921 strncpy(urlptr, attribs->formaction, WWW_CONF_MAX_URLLEN);
922 tmpurl[WWW_CONF_MAX_URLLEN] = 0;
923 urlptr += strlen(urlptr);
924 *urlptr = ISO_questionmark;
925 ++urlptr;
927 /* Construct an URL by finding all input field forms with the same
928 formname as the current submit button, and add the submit button
929 URL stuff as well. */
930 for(i = 0; i < pagewidgetptr; ++i) {
931 if(urlptr - &tmpurl[0] >= WWW_CONF_MAX_URLLEN) {
932 break;
935 faptr = &pagewidgetattribs[i].form;
937 if(strcmp(attribs->formaction, faptr->formaction) == 0 &&
938 strcmp(attribs->formname, faptr->formname) == 0 &&
939 (faptr->inputtype == FORMINPUTTYPE_INPUTFIELD ||
940 faptr == attribs)) {
942 /* Copy the name of the input field into the URL and append a
943 questionmark. */
944 strncpy(urlptr, faptr->inputname, WWW_CONF_MAX_URLLEN - strlen(tmpurl));
945 tmpurl[WWW_CONF_MAX_URLLEN] = 0;
946 urlptr += strlen(urlptr);
947 *urlptr = ISO_eq;
948 ++urlptr;
950 /* Convert and copy the contents of the input field to the URL
951 and append an ampersand. */
952 valueptr = pagewidgets[i].widget.textentry.text;
953 petsciiconv_toascii(valueptr, WWW_CONF_MAX_INPUTVALUELEN);
954 for(j = 0; j < WWW_CONF_MAX_INPUTVALUELEN; ++j) {
955 if(urlptr - &tmpurl[0] >= WWW_CONF_MAX_URLLEN) {
956 break;
958 *urlptr = *valueptr;
959 if(*urlptr == ISO_space) {
960 *urlptr = ISO_plus;
962 if(*urlptr == 0) {
963 break;
965 ++urlptr;
966 ++valueptr;
969 *urlptr = ISO_ampersand;
970 ++urlptr;
973 --urlptr;
974 *urlptr = 0;
975 #if WWW_CONF_HISTORY_SIZE > 0
976 log_back();
977 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
978 open_link(tmpurl);
980 #endif /* WWW_CONF_FORMS */
981 /*-----------------------------------------------------------------------------------*/