Contiki 3.x
www.c
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. 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.
17  *
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.
29  *
30  * This file is part of the Contiki desktop environment
31  *
32  *
33  */
34 
35 #include <string.h>
36 #include <stddef.h>
37 #include <stdlib.h>
38 
39 #include "ctk/ctk.h"
40 #include "ctk/ctk-textentry-cmdline.h"
41 #include "contiki-net.h"
42 #include "lib/petsciiconv.h"
43 #include "sys/arg.h"
44 #include "sys/log.h"
45 #if WWW_CONF_WITH_WGET
46 #include "program-handler.h"
47 #endif /* WWW_CONF_WITH_WGET */
48 
49 #include "webclient.h"
50 #include "htmlparser.h"
51 #include "http-strings.h"
52 
53 #include "www.h"
54 
55 /* Explicitly declare itoa as it is non-standard and not necessarily in stdlib.h */
56 char *itoa(int value, char *str, int base);
57 
58 /* The array that holds the current URL. */
59 static char url[WWW_CONF_MAX_URLLEN + 1];
60 
61 /* The array that holds the web page text. */
62 static char webpage[WWW_CONF_WEBPAGE_WIDTH *
63  WWW_CONF_WEBPAGE_HEIGHT + 1];
64 
65 /* The CTK widgets for the main window. */
66 static struct ctk_window mainwindow;
67 
68 #if WWW_CONF_HISTORY_SIZE > 0
69 static struct ctk_button backbutton =
70  {CTK_BUTTON(0, 0, 4, "Back")};
71 static struct ctk_button downbutton =
72  {CTK_BUTTON(10, 0, 4, "Down")};
73 #else /* WWW_CONF_HISTORY_SIZE > 0 */
74 static struct ctk_button downbutton =
75  {CTK_BUTTON(0, 0, 4, "Down")};
76 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
77 static struct ctk_button stopbutton =
78  {CTK_BUTTON(WWW_CONF_WEBPAGE_WIDTH - 16, 0, 4, "Stop")};
79 static struct ctk_button gobutton =
80  {CTK_BUTTON(WWW_CONF_WEBPAGE_WIDTH - 4, 0, 2, "Go")};
81 
82 static struct ctk_separator sep1 =
83  {CTK_SEPARATOR(0, 2, WWW_CONF_WEBPAGE_WIDTH)};
84 
85 static char editurl[WWW_CONF_MAX_URLLEN + 1];
86 static struct ctk_textentry urlentry =
87  {CTK_TEXTENTRY(0, 1, WWW_CONF_WEBPAGE_WIDTH - 2,
88  1, editurl, WWW_CONF_MAX_URLLEN)};
89 static struct ctk_label webpagelabel =
90  {CTK_LABEL(0, 3, WWW_CONF_WEBPAGE_WIDTH,
91  WWW_CONF_WEBPAGE_HEIGHT, webpage)};
92 
93 static char statustexturl[WWW_CONF_WEBPAGE_WIDTH];
94 static struct ctk_label statustext =
95  {CTK_LABEL(0, WWW_CONF_WEBPAGE_HEIGHT + 4,
96  WWW_CONF_WEBPAGE_WIDTH, 1, "")};
97 static struct ctk_separator sep2 =
98  {CTK_SEPARATOR(0, WWW_CONF_WEBPAGE_HEIGHT + 3,
99  WWW_CONF_WEBPAGE_WIDTH)};
100 
101 #if WWW_CONF_WITH_WGET || defined(WWW_CONF_WGET_EXEC)
102 #if CTK_CONF_WINDOWS
103 static struct ctk_window wgetdialog;
104 static struct ctk_label wgetlabel1 =
105  {CTK_LABEL(1, 1, 34, 1, "This web page cannot be displayed.")};
106 static struct ctk_label wgetlabel2 =
107  {CTK_LABEL(1, 3, 35, 1, "Would you like to download instead?")};
108 static struct ctk_button wgetnobutton =
109  {CTK_BUTTON(1, 5, 6, "Cancel")};
110 static struct ctk_button wgetyesbutton =
111  {CTK_BUTTON(11, 5, 24, "Close browser & download")};
112 #else /* CTK_CONF_WINDOWS */
113 static struct ctk_button wgetnobutton =
114  {CTK_BUTTON((WWW_CONF_WEBPAGE_WIDTH - 38) / 2 + 1,
115  11, 6, "Cancel")};
116 static struct ctk_button wgetyesbutton =
117  {CTK_BUTTON((WWW_CONF_WEBPAGE_WIDTH - 38) / 2 + 11,
118  11, 24, "Close browser & download")};
119 #endif /* CTK_CONF_WINDOWS */
120 #endif /* WWW_CONF_WITH_WGET || WWW_CONF_WGET_EXEC */
121 
122 #if WWW_CONF_HISTORY_SIZE > 0
123 /* The char arrays that hold the history of visited URLs. */
124 static char history[WWW_CONF_HISTORY_SIZE][WWW_CONF_MAX_URLLEN];
125 static unsigned char history_last;
126 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
127 
128 struct linkattrib {
129  struct ctk_hyperlink hyperlink;
130  char url[1];
131 };
132 
133 struct inputattrib;
134 
135 struct formattrib {
136  struct inputattrib *nextptr;
137  char action[1];
138 };
139 
140 struct inputattrib {
141  struct inputattrib *nextptr;
142  struct formattrib *formptr;
143  struct ctk_widget widget;
144 };
145 
146 struct textattrib {
147  struct inputattrib *nextptr;
148  struct formattrib *formptr;
149  struct ctk_textentry textentry;
150  char name[1];
151 };
152 
153 struct submitattrib {
154  struct inputattrib *nextptr;
155  struct formattrib *formptr;
156  struct ctk_button button;
157  char name[1];
158 };
159 
160 static char pageattribs[WWW_CONF_PAGEATTRIB_SIZE];
161 static char *pageattribptr;
162 
163 #if WWW_CONF_FORMS
164 static struct formattrib *formptr;
165 static struct inputattrib *currptr;
166 #endif /* WWW_CONF_FORMS */
167 
168 #define ISO_nl 0x0a
169 #define ISO_space 0x20
170 #define ISO_hash 0x23
171 #define ISO_ampersand 0x26
172 #define ISO_plus 0x2b
173 #define ISO_slash 0x2f
174 #define ISO_eq 0x3d
175 #define ISO_questionmark 0x3f
176 
177 /* The state of the rendering code. */
178 static char *webpageptr;
179 static unsigned char x, y;
180 static unsigned char loading;
181 static unsigned short firsty, pagey;
182 static unsigned char newlines;
183 
184 static unsigned char count;
185 static char receivingmsgs[4][23] = {
186  "Receiving web page ...",
187  "Receiving web page. ..",
188  "Receiving web page.. .",
189  "Receiving web page... "
190 };
191 
192 PROCESS(www_process, "Web browser");
193 
194 AUTOSTART_PROCESSES(&www_process);
195 
196 static void formsubmit(struct inputattrib *trigger);
197 
198 /*-----------------------------------------------------------------------------------*/
199 /* make_window()
200  *
201  * Creates the web browser's window.
202  */
203 static void
204 make_window(void)
205 {
206 #if WWW_CONF_HISTORY_SIZE > 0
207  CTK_WIDGET_ADD(&mainwindow, &backbutton);
208 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
209  CTK_WIDGET_ADD(&mainwindow, &downbutton);
210  CTK_WIDGET_ADD(&mainwindow, &stopbutton);
211  CTK_WIDGET_ADD(&mainwindow, &gobutton);
212  CTK_WIDGET_ADD(&mainwindow, &urlentry);
213  CTK_WIDGET_ADD(&mainwindow, &sep1);
214  CTK_WIDGET_ADD(&mainwindow, &webpagelabel);
215  CTK_WIDGET_SET_FLAG(&webpagelabel, CTK_WIDGET_FLAG_MONOSPACE);
216  CTK_WIDGET_ADD(&mainwindow, &sep2);
217  CTK_WIDGET_ADD(&mainwindow, &statustext);
218 
219  pageattribptr = pageattribs;
220 }
221 /*-----------------------------------------------------------------------------------*/
222 /* redraw_window():
223  *
224  * Convenience function that calls upon CTK to redraw the browser
225  * window. */
226 static void
227 redraw_window(void)
228 {
229  ctk_window_redraw(&mainwindow);
230 }
231 /*-----------------------------------------------------------------------------------*/
232 static char *
233 add_pageattrib(unsigned size)
234 {
235  char *ptr;
236 
237  if(pageattribptr + size > pageattribs + sizeof(pageattribs)) {
238  return NULL;
239  }
240  ptr = pageattribptr;
241  pageattribptr += size;
242  return ptr;
243 }
244 /*-----------------------------------------------------------------------------------*/
245 #if WWW_CONF_FORMS
246 static void
247 add_forminput(struct inputattrib *inputptr)
248 {
249  inputptr->nextptr = NULL;
250  currptr->nextptr = inputptr;
251  currptr = inputptr;
252 }
253 #endif /* WWW_CONF_FORMS */
254 /*-----------------------------------------------------------------------------------*/
255 static void
256 clear_page(void)
257 {
258  ctk_window_clear(&mainwindow);
259  make_window();
260  memset(webpage, 0, WWW_CONF_WEBPAGE_WIDTH * WWW_CONF_WEBPAGE_HEIGHT);
261  redraw_window();
262 }
263 /*-----------------------------------------------------------------------------------*/
264 static void
265 show_url(void)
266 {
267  memcpy(editurl, url, WWW_CONF_MAX_URLLEN);
268  strncpy(editurl, "http://", 7);
269  petsciiconv_topetscii(editurl + 7, WWW_CONF_MAX_URLLEN - 7);
270  CTK_WIDGET_REDRAW(&urlentry);
271 }
272 /*-----------------------------------------------------------------------------------*/
273 static void
274 start_loading(void)
275 {
276  loading = 1;
277  x = y = 0;
278  pagey = 0;
279  newlines = 0;
280  webpageptr = webpage;
281 
282  clear_page();
283 }
284 /*-----------------------------------------------------------------------------------*/
285 static void
286 show_statustext(char *text)
287 {
288  ctk_label_set_text(&statustext, text);
289  CTK_WIDGET_REDRAW(&statustext);
290 }
291 /*-----------------------------------------------------------------------------------*/
292 static void
293 end_page(char *status, void *focus)
294 {
295  show_statustext(status);
296  petsciiconv_topetscii(webpageptr - x, x);
297  CTK_WIDGET_FOCUS(&mainwindow, focus);
298  redraw_window();
299  log_message("Page attribs free: ", itoa(pageattribs + sizeof(pageattribs) - pageattribptr,
300  pageattribs + sizeof(pageattribs) - 5, 10));
301 }
302 /*-----------------------------------------------------------------------------------*/
303 /* open_url():
304  *
305  * Called when the URL present in the global "url" variable should be
306  * opened. It will call the hostname resolver as well as the HTTP
307  * client requester.
308  */
309 static void
310 open_url(void)
311 {
312  unsigned char i;
313  static char host[32];
314  char *file;
315  register char *urlptr;
316  static uip_ipaddr_t addr;
317 
318  /* Trim off any spaces in the end of the url. */
319  urlptr = url + strlen(url);
320  while(urlptr > url) {
321  if(*(urlptr - 1) == ' ') {
322  *--urlptr = 0;
323  } else {
324  break;
325  }
326  }
327 
328  /* Don't even try to go further if the URL is empty. */
329  if(urlptr == url) {
330  return;
331  }
332 
333  /* See if the URL starts with http://, otherwise prepend it. */
334  if(strncmp(url, http_http, 7) != 0) {
335  while(urlptr >= url) {
336  *(urlptr + 7) = *urlptr;
337  --urlptr;
338  }
339  strncpy(url, http_http, 7);
340  }
341 
342  /* Find host part of the URL. */
343  urlptr = &url[7];
344  for(i = 0; i < sizeof(host); ++i) {
345  if(*urlptr == 0 ||
346  *urlptr == '/' ||
347  *urlptr == ' ' ||
348  *urlptr == ':') {
349  host[i] = 0;
350  break;
351  }
352  host[i] = *urlptr;
353  ++urlptr;
354  }
355 
356  /* XXX: Here we should find the port part of the URL, but this isn't
357  currently done because of laziness from the programmer's side
358  :-) */
359 
360  /* Find file part of the URL. */
361  while(*urlptr != '/' && *urlptr != 0) {
362  ++urlptr;
363  }
364  if(*urlptr == '/') {
365  file = urlptr;
366  } else {
367  file = "/";
368  }
369 
370 #if UIP_UDP
371  /* Try to lookup the hostname. If it fails, we initiate a hostname
372  lookup and print out an informative message on the statusbar. */
373  if(uiplib_ipaddrconv(host, &addr) == 0) {
374  uip_ipaddr_t *addrptr;
375  if(resolv_lookup(host, &addrptr) != RESOLV_STATUS_CACHED) {
376  resolv_query(host);
377  show_statustext("Resolving host...");
378  return;
379  }
380  uip_ipaddr_copy(&addr, addrptr);
381  }
382 #else /* UIP_UDP */
383  uiplib_ipaddrconv(host, &addr);
384 #endif /* UIP_UDP */
385 
386  /* The hostname we present in the hostname table, so we send out the
387  initial GET request. */
388  if(webclient_get(host, 80, file) == 0) {
389  show_statustext("Out of memory error");
390  } else {
391  show_statustext("Connecting...");
392  }
393 }
394 /*-----------------------------------------------------------------------------------*/
395 /* set_link(link):
396  *
397  * Will format a link from the current web pages so that it suits the
398  * open_url() function.
399  */
400 static void
401 set_link(char *link)
402 {
403  register char *urlptr;
404 
405  if(strncmp(link, http_http, 7) == 0) {
406  /* The link starts with http://. We just copy the contents of the
407  link into the url string and jump away. */
408  strncpy(url, link, WWW_CONF_MAX_URLLEN);
409  } else if(*link == ISO_slash &&
410  *(link + 1) == ISO_slash) {
411  /* The link starts with //, so we'll copy it into the url
412  variable, starting after the http (which already is present in
413  the url variable since we were able to open the web page on
414  which this link was found in the first place). */
415  strncpy(&url[5], link, WWW_CONF_MAX_URLLEN);
416  } else if(*link == ISO_slash) {
417  /* The link starts with a slash, so it is a non-relative link
418  within the same web site. We find the start of the filename of
419  the current URL and paste the contents of this link there, and
420  head off to the new URL. */
421  for(urlptr = &url[7];
422  *urlptr != 0 && *urlptr != ISO_slash;
423  ++urlptr);
424  strncpy(urlptr, link, WWW_CONF_MAX_URLLEN - (urlptr - url));
425  } else {
426  /* A fully relative link is found. We find the last slash in the
427  current URL and paste the link there. */
428 
429  /* XXX: we should really parse any ../ in the link as well. */
430  for(urlptr = url + strlen(url);
431  urlptr != url && *urlptr != ISO_slash;
432  --urlptr);
433  ++urlptr;
434  strncpy(urlptr, link, WWW_CONF_MAX_URLLEN - (urlptr - url));
435  }
436 }
437 /*-----------------------------------------------------------------------------------*/
438 #if WWW_CONF_HISTORY_SIZE > 0
439 /* log_back():
440  *
441  * Copies the current URL from the url variable and into the log for
442  * the back button.
443  */
444 static void
445 log_back(void)
446 {
447  if(strncmp(url, history[(int)history_last], WWW_CONF_MAX_URLLEN) != 0) {
448  memcpy(history[(int)history_last], url, WWW_CONF_MAX_URLLEN);
449  ++history_last;
450  if(history_last >= WWW_CONF_HISTORY_SIZE) {
451  history_last = 0;
452  }
453  }
454 }
455 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
456 /*-----------------------------------------------------------------------------------*/
457 static void
458 quit(void)
459 {
460  ctk_window_close(&mainwindow);
461  process_exit(&www_process);
462  LOADER_UNLOAD();
463 }
464 /*-----------------------------------------------------------------------------------*/
465 /* www_process():
466  *
467  * The program's signal dispatcher function. Is called whenever a signal arrives.
468  */
469 PROCESS_THREAD(www_process, ev, data)
470 {
471  static struct ctk_widget *w;
472 #if WWW_CONF_WITH_WGET
473  static char *argptr;
474 #endif /* WWW_CONF_WITH_WGET */
475 
476  w = (struct ctk_widget *)data;
477 
478  PROCESS_BEGIN();
479 
480  /* Create the main window. */
481  memset(webpage, 0, sizeof(webpage));
482  ctk_window_new(&mainwindow, WWW_CONF_WEBPAGE_WIDTH,
483  WWW_CONF_WEBPAGE_HEIGHT+5, "Web browser");
484  make_window();
485 #ifdef WWW_CONF_HOMEPAGE
486  strncpy(editurl, WWW_CONF_HOMEPAGE, sizeof(editurl));
487 #endif /* WWW_CONF_HOMEPAGE */
488  CTK_WIDGET_FOCUS(&mainwindow, &urlentry);
489 
490 #if WWW_CONF_WITH_WGET || defined(WWW_CONF_WGET_EXEC)
491 #if CTK_CONF_WINDOWS
492  /* Create download dialog.*/
493  ctk_dialog_new(&wgetdialog, 38, 7);
494  CTK_WIDGET_ADD(&wgetdialog, &wgetlabel1);
495  CTK_WIDGET_ADD(&wgetdialog, &wgetlabel2);
496  CTK_WIDGET_ADD(&wgetdialog, &wgetnobutton);
497  CTK_WIDGET_ADD(&wgetdialog, &wgetyesbutton);
498 #endif /* CTK_CONF_WINDOWS */
499 #endif /* WWW_CONF_WITH_WGET || WWW_CONF_WGET_EXEC */
500 
501  ctk_window_open(&mainwindow);
502 
503  while(1) {
504 
506 
507  if(ev == tcpip_event) {
508  webclient_appcall(data);
509  } else if(ev == ctk_signal_widget_activate) {
510  if(w == (struct ctk_widget *)&gobutton ||
511  w == (struct ctk_widget *)&urlentry) {
512  start_loading();
513  firsty = 0;
514 #if WWW_CONF_HISTORY_SIZE > 0
515  log_back();
516 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
517  memcpy(url, editurl, WWW_CONF_MAX_URLLEN);
518  petsciiconv_toascii(url, WWW_CONF_MAX_URLLEN);
519  open_url();
520  CTK_WIDGET_FOCUS(&mainwindow, &gobutton);
521 #if WWW_CONF_HISTORY_SIZE > 0
522  } else if(w == (struct ctk_widget *)&backbutton) {
523  firsty = 0;
524  start_loading();
525  --history_last;
526  /* Note: history_last is unsigned ! */
527  if(history_last > WWW_CONF_HISTORY_SIZE) {
528  history_last = WWW_CONF_HISTORY_SIZE - 1;
529  }
530  memcpy(url, history[(int)history_last], WWW_CONF_MAX_URLLEN);
531  *history[(int)history_last] = 0;
532  open_url();
533  CTK_WIDGET_FOCUS(&mainwindow, &backbutton);
534 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
535  } else if(w == (struct ctk_widget *)&downbutton) {
536  firsty = pagey + WWW_CONF_WEBPAGE_HEIGHT - 2;
537  start_loading();
538  open_url();
539  CTK_WIDGET_FOCUS(&mainwindow, &downbutton);
540  } else if(w == (struct ctk_widget *)&stopbutton) {
541  loading = 0;
542  webclient_close();
543 #if WWW_CONF_WITH_WGET || defined(WWW_CONF_WGET_EXEC)
544  } else if(w == (struct ctk_widget *)&wgetnobutton) {
545 #if CTK_CONF_WINDOWS
546  ctk_dialog_close();
547 #else /* CTK_CONF_WINDOWS */
548  clear_page();
549 #endif /* CTK_CONF_WINDOWS */
550  } else if(w == (struct ctk_widget *)&wgetyesbutton) {
551 #if CTK_CONF_WINDOWS
552  ctk_dialog_close();
553 #else /* CTK_CONF_WINDOWS */
554  clear_page();
555 #endif /* CTK_CONF_WINDOWS */
556 #if WWW_CONF_WITH_WGET
557  quit();
558  argptr = arg_alloc((char)WWW_CONF_MAX_URLLEN);
559  if(argptr != NULL) {
560  strncpy(argptr, url, WWW_CONF_MAX_URLLEN);
561  }
562  program_handler_load("wget.prg", argptr);
563 #else /* WWW_CONF_WITH_WGET */
564  petsciiconv_topetscii(url, sizeof(url));
565  /* Clear screen */
566  ctk_restore();
567  WWW_CONF_WGET_EXEC(url);
568  redraw_window();
569  show_statustext("Cannot exec wget");
570 #endif /* WWW_CONF_WITH_WGET */
571 #endif /* WWW_CONF_WITH_WGET || WWW_CONF_WGET_EXEC */
572 #if WWW_CONF_FORMS
573  } else {
574  /* Assume form widget. */
575  formsubmit((struct inputattrib *)
576  (((char *)w) - offsetof(struct inputattrib, widget)));
577 #endif /* WWW_CONF_FORMS */
578  }
579  } else if(ev == ctk_signal_hyperlink_activate) {
580  firsty = 0;
581 #if WWW_CONF_HISTORY_SIZE > 0
582  log_back();
583 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
584  set_link(w->widget.hyperlink.url);
585  show_url();
586  open_url();
587  start_loading();
588  CTK_WIDGET_FOCUS(&mainwindow, &stopbutton);
589  } else if(ev == ctk_signal_hyperlink_hover) {
590  if(CTK_WIDGET_TYPE((struct ctk_widget *)data) == CTK_WIDGET_HYPERLINK) {
591  strncpy(statustexturl, w->widget.hyperlink.url,
592  sizeof(statustexturl));
593  petsciiconv_topetscii(statustexturl, sizeof(statustexturl));
594  show_statustext(statustexturl);
595  }
596 #if UIP_UDP
597  } else if(ev == resolv_event_found) {
598  /* Either found a hostname, or not. */
599  if((char *)data != NULL &&
600  resolv_lookup((char *)data, NULL) == RESOLV_STATUS_CACHED) {
601  open_url();
602  } else {
603  show_statustext("Host not found");
604  }
605 #endif /* UIP_UDP */
606  } else if(ev == ctk_signal_window_close ||
607  ev == PROCESS_EVENT_EXIT) {
608  quit();
609  }
610  }
611  PROCESS_END();
612 }
613 /*-----------------------------------------------------------------------------------*/
614 /* set_url():
615  *
616  * Constructs an URL from the arguments and puts it into the global
617  * "url" variable and the visible "editurl" (which is shown in the URL
618  * text entry widget in the browser window).
619  */
620 static void
621 set_url(char *host, uint16_t port, char *file)
622 {
623  char *urlptr;
624 
625  memset(url, 0, WWW_CONF_MAX_URLLEN);
626 
627  if(strncmp(file, http_http, 7) == 0) {
628  strncpy(url, file, sizeof(url));
629  } else {
630  strncpy(url, http_http, 7);
631  urlptr = url + 7;
632  strcpy(urlptr, host);
633  urlptr += strlen(host);
634  strcpy(urlptr, file);
635  }
636 
637  show_url();
638 }
639 /*-----------------------------------------------------------------------------------*/
640 /* webclient_aborted():
641  *
642  * Callback function. Called from the webclient when the HTTP
643  * connection was abruptly aborted.
644  */
645 void
646 webclient_aborted(void)
647 {
648  show_statustext("Connection reset by peer");
649 }
650 /*-----------------------------------------------------------------------------------*/
651 /* webclient_timedout():
652  *
653  * Callback function. Called from the webclient when the HTTP
654  * connection timed out.
655  */
656 void
657 webclient_timedout(void)
658 {
659  show_statustext("Connection timed out");
660 }
661 /*-----------------------------------------------------------------------------------*/
662 /* webclient_closed():
663  *
664  * Callback function. Called from the webclient when the HTTP
665  * connection was closed after a request from the "webclient_close()"
666  * function.
667  */
668 void
669 webclient_closed(void)
670 {
671  end_page("Stopped", &downbutton);
672 }
673 /*-----------------------------------------------------------------------------------*/
674 /* webclient_connected():
675  *
676  * Callback function. Called from the webclient when the HTTP
677  * connection is connected.
678  */
679 void
680 webclient_connected(void)
681 {
682  start_loading();
683 
684  show_statustext("Request sent...");
685  set_url(webclient_hostname(), webclient_port(), webclient_filename());
686 
687  htmlparser_init();
688 }
689 /*-----------------------------------------------------------------------------------*/
690 /* webclient_datahandler():
691  *
692  * Callback function. Called from the webclient module when HTTP data
693  * has arrived.
694  */
695 void
696 webclient_datahandler(char *data, uint16_t len)
697 {
698  if(len > 0) {
699  if(strstr(webclient_mimetype(), http_html + 1) != 0) {
700  count = (count + 1) & 3;
701  show_statustext(receivingmsgs[count]);
702  htmlparser_parse(data, len);
703  redraw_window();
704  } else {
705  uip_abort();
706 #if WWW_CONF_WITH_WGET || defined(WWW_CONF_WGET_EXEC)
707 #if CTK_CONF_WINDOWS
708  ctk_dialog_open(&wgetdialog);
709 #else /* CTK_CONF_WINDOWS */
710  strcpy(webpage + WWW_CONF_WEBPAGE_WIDTH * 5,
711  (80 - WWW_CONF_WEBPAGE_WIDTH) / 2 +
712  " This web page cannot be displayed.");
713  strcpy(webpage + WWW_CONF_WEBPAGE_WIDTH * 6,
714  (80 - WWW_CONF_WEBPAGE_WIDTH) / 2 +
715  " Would you like to download instead?");
716  CTK_WIDGET_ADD(&mainwindow, &wgetnobutton);
717  CTK_WIDGET_ADD(&mainwindow, &wgetyesbutton);
718  CTK_WIDGET_FOCUS(&mainwindow, &wgetyesbutton);
719  redraw_window();
720 #endif /* CTK_CONF_WINDOWS */
721 #endif /* WWW_CONF_WITH_WGET || WWW_CONF_WGET_EXEC */
722  }
723  } else {
724  /* Clear remaining parts of page. */
725  loading = 0;
726  }
727 
728  if(data == NULL) {
729  loading = 0;
730  end_page("Done", &urlentry);
731  }
732 }
733 /*-----------------------------------------------------------------------------------*/
734 static void
735 add_pagewidget(char *text, unsigned char size, char *attrib, unsigned char type,
736  unsigned char border)
737 {
738  char *wptr;
739  static unsigned char maxwidth;
740 
741  newlines = 0;
742 
743  if(!loading) {
744  return;
745  }
746 
747  maxwidth = size ? WWW_CONF_WEBPAGE_WIDTH - (1 + 2 * border)
748  : WWW_CONF_WEBPAGE_WIDTH;
749 
750  /* If the text of the link is too long so that it does not fit into
751  the width of the current window, counting from the current x
752  coordinate, we first try to jump to the next line. */
753  if(size + x > maxwidth) {
754  htmlparser_newline();
755  if(!loading) {
756  return;
757  }
758  }
759 
760  /* If the text of the link still is too long, we just chop it off!
761  XXX: this is not really the right thing to do, we should probably
762  either make a link into a multiline link, or add multiple
763  buttons. But this will do for now. */
764  if(size > maxwidth) {
765  text[maxwidth] = 0;
766  size = maxwidth;
767  }
768 
769  if(firsty == pagey) {
770  unsigned char attriblen = strlen(attrib);
771 
772  wptr = webpageptr;
773  /* To save memory, we'll copy the widget text to the web page
774  drawing area and reference it from there. */
775  wptr[0] = 0;
776  wptr += border;
777  memcpy(wptr, text, size);
778  wptr[size] = 0;
779  wptr[size + border] = ' ';
780 
781  switch(type) {
782  case CTK_WIDGET_HYPERLINK: {
783  struct linkattrib *linkptr =
784  (struct linkattrib *)add_pageattrib(sizeof(struct linkattrib) /* incl 1 attrib char */ + attriblen);
785  if(linkptr != NULL) {
786  CTK_HYPERLINK_NEW(&linkptr->hyperlink, x, y + 3, size, wptr, linkptr->url);
787  strcpy(linkptr->url, attrib);
788  CTK_WIDGET_SET_FLAG(&linkptr->hyperlink, CTK_WIDGET_FLAG_MONOSPACE);
789  CTK_WIDGET_ADD(&mainwindow, &linkptr->hyperlink);
790  }
791  break;
792  }
793 #if WWW_CONF_FORMS
794  case CTK_WIDGET_BUTTON: {
795  struct submitattrib *submitptr =
796  (struct submitattrib *)add_pageattrib(sizeof(struct submitattrib) /* incl 1 attrib char */ + attriblen);
797  if(submitptr != NULL) {
798  CTK_BUTTON_NEW((struct ctk_button *)&submitptr->button, x, y + 3, size, wptr);
799  add_forminput((struct inputattrib *)submitptr);
800  submitptr->formptr = formptr;
801  strcpy(submitptr->name, attrib);
802  CTK_WIDGET_SET_FLAG(&submitptr->button, CTK_WIDGET_FLAG_MONOSPACE);
803  CTK_WIDGET_ADD(&mainwindow, &submitptr->button);
804  }
805  break;
806  }
807  case CTK_WIDGET_TEXTENTRY: {
808  struct textattrib *textptr =
809  (struct textattrib *)add_pageattrib(sizeof(struct textattrib) /* incl 1 attrib char */ + attriblen
810  + (size ? WWW_CONF_MAX_INPUTVALUELEN : strlen(text)) + 1);
811  if(textptr != NULL) {
812  CTK_TEXTENTRY_NEW((struct ctk_textentry *)&textptr->textentry, x, y + 3, size, 1,
813  textptr->name + attriblen + 1, WWW_CONF_MAX_INPUTVALUELEN);
814  add_forminput((struct inputattrib *)textptr);
815  textptr->formptr = formptr;
816  petsciiconv_topetscii(text, strlen(text));
817  strcpy(textptr->textentry.text, text);
818  strcpy(textptr->name, attrib);
819  if(size) {
820  CTK_WIDGET_SET_FLAG(&textptr->textentry, CTK_WIDGET_FLAG_MONOSPACE);
821  CTK_WIDGET_ADD(&mainwindow, &textptr->textentry);
822  }
823  }
824  break;
825  }
826 #endif /* WWW_CONF_FORMS */
827  }
828  }
829  /* Increase the x coordinate with the length of the link text plus
830  the extra space behind it and the CTK button markers. */
831  if(size) {
832  size += 1 + 2 * border;
833  }
834  x += size;
835 
836  if(firsty == pagey) {
837  webpageptr += size;
838  }
839 
840  if(x == WWW_CONF_WEBPAGE_WIDTH) {
841  htmlparser_newline();
842  }
843 }
844 /*-----------------------------------------------------------------------------------*/
845 void
846 htmlparser_newline(void)
847 {
848 #ifdef WITH_PETSCII
849  char *wptr;
850 #endif /* WITH_PETSCII */
851 
852  if(++newlines > 2) {
853  return;
854  }
855 
856  if(pagey < firsty) {
857  ++pagey;
858  x = 0;
859  return;
860  }
861 
862  if(!loading) {
863  return;
864  }
865 
866  webpageptr += (WWW_CONF_WEBPAGE_WIDTH - x);
867  ++y;
868  x = 0;
869 
870 #ifdef WITH_PETSCII
871  wptr = webpageptr - WWW_CONF_WEBPAGE_WIDTH;
872  petsciiconv_topetscii(wptr, WWW_CONF_WEBPAGE_WIDTH);
873 #endif /* WITH_PETSCII */
874 
875  if(y == WWW_CONF_WEBPAGE_HEIGHT) {
876  loading = 0;
877  webclient_close();
878  }
879 }
880 /*-----------------------------------------------------------------------------------*/
881 void
882 htmlparser_word(char *word, unsigned char wordlen)
883 {
884  newlines = 0;
885 
886  if(loading) {
887  if(wordlen + 1 > WWW_CONF_WEBPAGE_WIDTH - x) {
888  htmlparser_newline();
889  }
890 
891  if(loading) {
892  if(pagey == firsty) {
893  memcpy(webpageptr, word, wordlen);
894  webpageptr += wordlen;
895  *webpageptr = ' ';
896  ++webpageptr;
897  }
898  x += wordlen + 1;
899  if(x == WWW_CONF_WEBPAGE_WIDTH) {
900  htmlparser_newline();
901  }
902  }
903  }
904 }
905 /*-----------------------------------------------------------------------------------*/
906 void
907 htmlparser_link(char *text, unsigned char textlen, char *url)
908 {
909  /* No link for https or fragment-only as we would't be able to handle it anyway. */
910  if(url[0] == ISO_hash || strncmp(url, http_https, sizeof(http_https) - 1) == 0) {
911  htmlparser_word(text, textlen);
912  } else {
913  add_pagewidget(text, textlen, url, CTK_WIDGET_HYPERLINK, 0);
914  }
915 }
916 /*-----------------------------------------------------------------------------------*/
917 #if WWW_CONF_FORMS
918 void
919 htmlparser_form(char *action)
920 {
921  formptr = (struct formattrib *)add_pageattrib(sizeof(struct formattrib) + strlen(action));
922  if(formptr != NULL) {
923  formptr->nextptr = NULL;
924  currptr = (struct inputattrib *)formptr;
925  strcpy(formptr->action, action);
926  }
927 }
928 /*-----------------------------------------------------------------------------------*/
929 void
930 htmlparser_submitbutton(char *text, char *name)
931 {
932  add_pagewidget(text, (unsigned char)strlen(text), name, CTK_WIDGET_BUTTON, 1);
933 }
934 /*-----------------------------------------------------------------------------------*/
935 void
936 htmlparser_inputfield(unsigned char type, unsigned char size, char *text, char *name)
937 {
938  if(type == HTMLPARSER_INPUTTYPE_HIDDEN) {
939  add_pagewidget(text, 0, name, CTK_WIDGET_TEXTENTRY, 0);
940  } else {
941  add_pagewidget(text, size, name, CTK_WIDGET_TEXTENTRY, 1);
942  }
943 }
944 /*-----------------------------------------------------------------------------------*/
945 static void
946 add_query(char delimiter, char *string)
947 {
948  static char *query;
949  unsigned char length;
950 
951  if(delimiter == ISO_questionmark) {
952  query = url + strlen(url);
953  }
954 
955  length = strlen(string);
956  if(query - url + WWW_CONF_MAX_URLLEN - 1 /* delimiter */ < length) {
957  return;
958  }
959 
960  *query++ = delimiter;
961  strcpy(query, string);
962  if(delimiter == ISO_eq) {
963  char *space = query;
964 
965  petsciiconv_toascii(query, length);
966  while((space = strchr(space, ISO_space)) != NULL) {
967  *space = ISO_plus;
968  }
969  }
970  query += length;
971 }
972 /*-----------------------------------------------------------------------------------*/
973 static void
974 formsubmit(struct inputattrib *trigger)
975 {
976  struct inputattrib *input;
977  struct formattrib *form = trigger->formptr;
978  char delimiter = ISO_questionmark;
979 
980  set_link(form->action);
981 
982  /* No button pressed so prepare to look for default button. */
983  if(trigger->widget.type == CTK_WIDGET_TEXTENTRY) {
984  trigger = NULL;
985  }
986 
987  for(input = form->nextptr; input != NULL; input = input->nextptr) {
988  char *name;
989  char *value;
990 
991  if(input->widget.type == CTK_WIDGET_TEXTENTRY) {
992  name = ((struct textattrib *)input)->name;
993  value = ((struct textattrib *)input)->textentry.text;
994  } else {
995  /* Consider first button as default button. */
996  if(trigger == NULL) {
997  trigger = input;
998  }
999  if(input != trigger) {
1000  continue;
1001  }
1002  name = ((struct submitattrib *)input)->name;
1003  value = ((struct submitattrib *)input)->button.text;
1004  }
1005 
1006  add_query(delimiter, name);
1007  add_query(ISO_eq, value);
1008  delimiter = ISO_ampersand;
1009  }
1010 
1011 #if WWW_CONF_HISTORY_SIZE > 0
1012  log_back();
1013 #endif /* WWW_CONF_HISTORY_SIZE > 0 */
1014 
1015  show_url();
1016  open_url();
1017  start_loading();
1018 }
1019 #endif /* WWW_CONF_FORMS */
1020 /*-----------------------------------------------------------------------------------*/
process_event_t ctk_signal_hyperlink_hover
Same as ctk_signal_widget_select.
Definition: ctk.c:126
char * arg_alloc(char size)
Allocates an argument buffer.
Definition: arg.c:104
#define uip_abort()
Abort the current connection.
Definition: uip.h:683
void ctk_window_close(struct ctk_window *w)
Close a window if it is open.
Definition: ctk.c:400
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
process_event_t tcpip_event
The uIP event.
Definition: tcpip.c:80
#define CTK_WIDGET_ADD(win, widg)
Add a widget to a window.
Definition: ctk.h:752
#define CTK_BUTTON(x, y, w, text)
Instantiating macro for the ctk_button widget.
Definition: ctk.h:140
void ctk_window_new(struct ctk_window *window, unsigned char w, unsigned char h, char *title)
Create a new window.
Definition: ctk.c:732
process_event_t ctk_signal_widget_activate
Emitted when a widget is activated (pressed).
Definition: ctk.c:126
PETSCII/ASCII conversion functions.
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
process_event_t ctk_signal_hyperlink_activate
Emitted when a hyperlink is activated.
Definition: ctk.c:126
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1503
process_event_t ctk_signal_window_close
Emitted when a window is closed.
Definition: ctk.c:155
#define CTK_SEPARATOR(x, y, w)
Instantiating macro for the ctk_separator widget.
Definition: ctk.h:111
unsigned char type
The type of the widget: CTK_WIDGET_SEPARATOR, CTK_WIDGET_LABEL, CTK_WIDGET_BUTTON, CTK_WIDGET_HYPERLINK, CTK_WIDGET_TEXTENTRY, CTK_WIDGET_BITMAP or CTK_WIDGET_ICON.
Definition: ctk.h:456
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
void process_exit(struct process *p)
Cause a process to exit.
Definition: process.c:202
union ctk_widget::@34 widget
The union which contains the actual widget structure, as determined by the type field.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1027
CTK header file.
Representation of a CTK window.
Definition: ctk.h:506
process_event_t resolv_event_found
Event that is broadcasted when a DNS name has been resolved.
Definition: resolv.c:286
#define CTK_WIDGET_TYPE(w)
Obtain the type of a widget.
Definition: ctk.h:780
#define ctk_label_set_text(l, t)
Set the text of a label.
Definition: ctk.h:849
#define CTK_LABEL(x, y, w, h, text)
Instantiating macro for the ctk_label widget.
Definition: ctk.h:171
#define LOADER_UNLOAD()
Unload a program from memory.
Definition: loader.h:104
#define CTK_TEXTENTRY(x, y, w, h, text, len)
Instantiating macro for the ctk_textentry widget.
Definition: ctk.h:275
void program_handler_load(char *name, char *arg)
Loads a program and displays a dialog telling the user about it.
#define NULL
The null pointer.
void ctk_window_clear(struct ctk_window *w)
Remove all widgets from a window.
Definition: ctk.c:485
resolv_status_t resolv_lookup(const char *name, uip_ipaddr_t **ipaddr)
Look up a hostname in the array of known hostnames.
Definition: resolv.c:1349
#define CTK_WIDGET_FOCUS(win, widg)
Set focus to a widget.
Definition: ctk.h:763
#define CTK_WIDGET_BUTTON
Widget number: The CTK button widget.
Definition: ctk.h:68
Hostname is fresh and usable.
Definition: resolv.h:63
static volatile clock_time_t count
These routines define the AVR-specific calls declared in /core/sys/clock.h CLOCK_SECOND is the number...
Definition: clock.c:80
#define CTK_WIDGET_HYPERLINK
Widget number: The CTK hyperlink widget.
Definition: ctk.h:70
#define uiplib_ipaddrconv
Convert a textual representation of an IP address to a numerical representation.
Definition: uiplib.h:71
void resolv_query(const char *name)
Queues a name so that a question for the name will be sent out.
Definition: resolv.c:1270
void ctk_window_redraw(struct ctk_window *w)
Redraw a window.
Definition: ctk.c:652
void ctk_window_open(CC_REGISTER_ARG struct ctk_window *w)
Open a window, or bring window to front if already open.
Definition: ctk.c:347
unsigned char w
The width of the widget in character coordinates.
Definition: ctk.h:464
#define CTK_WIDGET_TEXTENTRY
Widget number: The CTK textentry widget.
Definition: ctk.h:72
The generic CTK widget structure that contains all other widget structures.
Definition: ctk.h:444
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#define CTK_WIDGET_REDRAW(widg)
Add a widget to the redraw queue.
Definition: ctk.h:771
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120