FetchPage 35.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright 1997 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/***************************************************/
/* File   : FetchPage.c                            */
17 18 19 20 21 22
/*                                                 */
/* Purpose: High-level fetch functions; the main   */
/*          interface for initiating and control-  */
/*          ling full page fetches. Compare with   */
/*          lower level Fetch.c and FetchHTML.c.   */
/*                                                 */
23
/* Author : A.D.Hodgkinson                         */
24 25
/*                                                 */
/* History: 25-Nov-96: Created.                    */
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/***************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "flex.h"

#include "swis.h"

#include "URI.h"     /* URI handler API, in URILib:h */

#include "wimp.h"
#include "wimplib.h"
#include "event.h"

#include "toolbox.h"
#include "window.h"
#include "gadgets.h"

#include "svcprint.h"
#include "Global.h"
#include "FromROSLib.h"
49
#include "MiscDefs.h"
50 51 52
#include "Utils.h"

#include "Browser.h"
53
#include "Fetch.h" /* (Which itself includes URLstat.h) */
54 55 56
#include "Frames.h"
#include "History.h"
#include "Images.h"
57
#include "JavaScript.h"
58
#include "Memory.h"
59
#include "PlugIn.h"
60
#include "Protocols.h"
61
#include "Reformat.h"
62
#include "SaveText.h"
63 64 65 66 67 68 69 70 71 72 73 74 75
#include "Toolbars.h"
#include "URLutils.h"
#include "Windows.h"

#include "FetchPage.h"

/* Locals */

char * url_buffer = NULL;

/* Static function prototypes */

static _kernel_oserror * fetchpage_process_internal (browser_data * b);
76
static _kernel_oserror * fetchpage_preprocessed     (browser_data * b, int record, int stop);
77 78 79 80 81 82 83 84 85 86 87 88 89 90
static _kernel_oserror * fetchpage_postprocessed    (browser_data * b, int record);

/*************************************************/
/* fetchpage_fetch()                             */
/*                                               */
/* Handles the initiation of a fetch and the     */
/* display of the result in a browser window.    */
/*                                               */
/* Parameters are as standard for a Wimp event   */
/* handler (this is called on null events).      */
/*************************************************/

int fetchpage_fetch(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
{
91 92
  _kernel_oserror * e;
  int               tf_start, tf_now, priority;
93
  int               fetching, formatting;
94 95 96

  #ifdef TRACE
    {
97 98 99 100 101 102 103
      static oldstatus;

      if ((tl & (1u<<6)) && (handle->fetch_status != oldstatus))
      {
        Printf("\nfetchpage_fetch: Called with new status %d\n",handle->fetch_status);
        oldstatus = handle->fetch_status;
      }
104 105 106
    }
  #endif

107 108 109 110 111 112
  if (handle->fetch_status == BS_START)
  {
    e = fetch_start(handle);

    if (e)
    {
113
      handle->save_link = 0;
114 115 116
      show_error_cont(e);
    }
  }
117 118

  /* Call the fetcher / reformatter, allowing a certain */
119
  /* amount of time inside each only.                   */
120 121 122 123

  _swix(OS_ReadMonotonicTime, _OUT(0), &tf_start);
  tf_now = tf_start;

124 125 126 127 128 129 130
  /* Some fairly crude load balancing. If the fetcher is idle, */
  /* but this function is being called, chances are we're      */
  /* formatting. Otherwise, we may be processing tokens - so   */
  /* the actual fetch is complete but the fetcher is still     */
  /* working on the data from HTMLLib. Otherwise, it may be    */
  /* that the main fetch is active; or we could be spooling    */
  /* data to a file.                                           */
131

132 133 134 135 136
  if      (handle->fetch_status == BS_IDLE)                           priority = 15; /* Not fetching, may be solid formatting    */
  else if (handle->fetch_status == BS_PROCESS)                        priority = 8;  /* Processing tokens, but fetch is complete */
  else if (handle->fetch_status == BS_FETCHING)                       priority = 3;  /* Fetching tokens (so fetch is incomplete) */
  else if (handle->fetch_status == BS_DATAFETCH && handle->save_file) priority = 2;  /* Saving data to a file (fetch incomplete) */
  else                                                                priority = 0;
137

138 139 140 141 142 143
  /* The main fetch/reformat loop */

  fetching   = fetch_fetching(handle);
  formatting = reformat_formatting(handle);

  do
144
  {
145

146 147
    /* If fetching, call the reformatter. */

148
    if (fetching) fetch_fetcher(handle);
149 150 151

    /* If reformatting, call the reformatter. */

152 153 154 155 156 157 158 159 160 161 162 163
    if (formatting)
    {
      reformat_reformatter(handle);
      ChkError(windows_check_tools(handle, NULL));
    }

    fetching   = fetch_fetching(handle);
    formatting = reformat_formatting(handle);

    /* Re-read the time, and keep going whilst we're   */
    /* inside the maximum time specified by 'priority' */
    /* and fetching and/or formatting.                 */
164 165 166

    _swix(OS_ReadMonotonicTime, _OUT(0), &tf_now);
  }
167 168 169 170
  while (
          tf_now - tf_start < priority &&
          (fetching || formatting)
        );
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

  /* Process images on a lower priority */

  if (image_count_specific_pending(handle)) ChkError(image_process_null(handle));

  /* Handle jumping to any specified named anchors */

  if (handle->display_request == DISPLAY_NAMED)
  {
    char    * p;
    HStream * t;

    t = 0;

    p = fetch_find_name_tag(browser_current_url(handle)) + 1;
    t = fetch_find_anchor_token(handle, p);

    if (t)
    {
      handle->display_request = t;
      handle->display_offset  = 0;
    }
  }
  else
  {
    if (
         handle->display_request &&
         browser_show_token(handle,
                            handle->display_request,
                            handle->display_offset)
       )
    {
      WimpGetWindowStateBlock s;

      s.window_handle = handle->window_handle;
      wimp_get_window_state(&s);

      if (s.yscroll != handle->display_vscroll) handle->display_vscroll = s.yscroll;
      else handle->display_request = 0, handle->display_vscroll = 0;
    }
  }

  /* Various actions as things become inactive... */

  if (!fetch_fetching(handle))
  {
217 218 219 220
    /* If we have a JavaScript onLoad command, deal with it */

    if (handle->onload) ChkError(javascript_body_onload(handle));

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    /* Garbage collect images if the main page fetch has finished */

    if (handle->clear_images)
    {
//      image_discard_unused(handle);
      handle->clear_images = 0;
    }

    if (!reformat_formatting(handle))
    {
      if (!image_count_specific_pending(handle))
      {
        /* There are no pending images, so we seem to have finished - */
        /* but is there a reformat pending?                           */

236
        if (handle->refo_time)
237 238 239
        {
          /* Yes, so flush the queue */

240
          reformat_format_from(handle, handle->refo_line, 1, -1);
241 242 243
        }
        else
        {
244 245
          /* No; get rid of null claimants (the call will *install* a new */
          /* animation drift handler if the Controls require it).         */
246 247 248

          if (handle->fetch_handler) fetchpage_release_nulls(handle);

249 250 251 252
          /* We may have a pending messages to send */

          e = protocols_atats_send_any_pendings(handle);

253
          /* If this is a small fetch window, close it */
254

255 256
          if (handle->small_fetch)
          {
257
            int close = 1;
258

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
            /* If this is a fetch for a Plug-In, tell the Plug-In about it */

            if (
                 handle->pstream             &&
                 handle->pstream->active     &&
                 !handle->pstream->abandoned
               )
            {
              plugin_fetch_completed(handle);

              /* Only close the window if the stream won't close itself */

              if (handle->pstream->will_close_itself) close = 0;
            }

            /* Close the window *afterwards* - or you've  */
            /* just freed up message structures etc. that */
            /* need to be dealt with first.               */

            if (close) windows_close_browser(handle);
279
          }
280 281 282

          /* Otherwise, proceed as normal */

283 284 285
          else
          {
            /* Check the page's vertical extent is correct */
286

287
            ChkError(reformat_check_extent(handle));
288

289
            /* Update the status bar */
290

291
            toolbars_cancel_all(handle);
292 293
            toolbars_update_status(handle, Toolbars_Status_Viewing);
          }
294 295 296 297

          /* Report any errors from the pending message sends */

          ChkError(e);
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        }
      }

      /* Sort out window tool presence */

      ChkError(windows_check_tools(handle, NULL));
    }
  }

  /* Keep the buttons as up to date as possible throughout the fetch */

  toolbars_set_button_states(handle);

  return 0;
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
/*************************************************/
/* fetchpage_fetch_targetted()                   */
/*                                               */
/* Fetch a given URL, possibly appending some    */
/* given extra data, into a given target window, */
/* an ancestor browser, or a new view.           */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which acts as a parent;           */
/*                                               */
/*             Pointer to the URL to fetch;      */
/*                                               */
/*             Pointer to a window target name,  */
/*             or NULL to open in the parent -   */
/*             if full screen, note that any     */
/*             target names which would          */
/*             otherwise have opened a new       */
/*             window will not do so;            */
/*                                               */
/*             Pointer to any extra data to      */
/*             append to the URL, or NULL;       */
/*                                               */
/*             1 to open the URL in a new window */
/*             (with no name) regardless of the  */
/*             targetting, else 0 (this will not */
/*             be overridden when running full   */
/*             screen).                          */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the fetch.            */
/*************************************************/

346
_kernel_oserror * fetchpage_fetch_targetted(browser_data * parent, const char * url, const char * target, const char * appnddata, int new)
347 348 349 350 351 352 353 354 355 356
{
  _kernel_oserror * e;
  browser_data    * ancestor;
  browser_data    * targetted;

  /* If we've been asked to open a new window, only the extra data */
  /* parameter matters.                                            */

  if (new)
  {
357
    if (appnddata) return fetchpage_new_add(NULL,
358 359 360
                                            url,
                                            1,
                                            1,
361
                                            appnddata,
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
                                            1,
                                            NULL);
    else
    {
      RetError(windows_create_browser(url,
                                      NULL,
                                      NULL,
                                      NULL,
                                      Windows_CreateBrowser_Normal));

      RetError(browser_inherit(parent, last_browser));

      return NULL;
    }
  }

  /* Otherwise, have a bit more thinking to do */

  else
  {
    if (!parent) return NULL;

    ancestor = utils_ancestor(parent);

    /* Deal with the targetted window case first */

    if (target && *target)
    {
      targetted = frames_find_target(parent, target);

      if (targetted)
      {
        /* We have a specific browser to open the URL in */

396
        if (appnddata) return fetchpage_new_add(targetted,
397 398 399
                                                url,
                                                1,
                                                1,
400
                                                appnddata,
401 402 403 404 405 406 407 408 409 410 411 412 413 414
                                                0,
                                                NULL);

        else           return fetchpage_new(targetted,
                                            url,
                                            1,
                                            1);

      }
      else
      {
        /* Nothing was found with that name, so create a new window */
        /* with the given target name instead.                      */

415
        if (appnddata) return fetchpage_new_add(parent,
416 417 418
                                                url,
                                                1,
                                                1,
419
                                                appnddata,
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
                                                1,
                                                target);
        else
        {
          RetError(windows_create_browser(url,
                                          NULL,
                                          NULL,
                                          target,
                                          Windows_CreateBrowser_Normal));

          RetError(browser_inherit(parent, last_browser));

          return NULL;
        }
      }
    }

    /* Now the untargetted case - open in the parent */

    else
    {
441
      if (appnddata) return fetchpage_new_add(parent,
442 443 444
                                              url,
                                              1,
                                              1,
445
                                              appnddata,
446 447 448 449 450 451 452 453 454 455 456
                                              0,
                                              NULL);

      else           return fetchpage_new(parent,
                                          url,
                                          1,
                                          1);
    }
  }
}

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
/*************************************************/
/* fetchpage_process_internal()                  */
/*                                               */
/* Some internal URLs involve just substituting  */
/* the internal URL for some known or easily     */
/* discoverable alternative early in the fetch   */
/* stage. This function handles such changes.    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the fetch.            */
/*************************************************/

static _kernel_oserror * fetchpage_process_internal(browser_data * b)
{
  if (
       b->displayed == Display_Recovered_Page ||
       b->displayed == Display_Home_Page
     )
  {
476
    char alt_url[Limits_URL];
477 478 479 480 481 482 483 484 485 486

    memset(alt_url, 0, sizeof(alt_url));

    if (b->displayed == Display_Recovered_Page)
    {
      /* For a recovered page, try to get back to the page detailed  */
      /* in Browse$PreviousPage.                                     */
      /*                                                             */
      /* If the variable is unset / can't be read, can't load a page */
      /* so set the buffer to hold a null string.                    */
487 488 489 490 491
      /*                                                             */
      /* The call is quivalent to getenv, but the RISC OS            */
      /* implementation evaluates the system variable as an          */
      /* expression which we don't want; hence the direct use of the */
      /* SWI call.                                                   */
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

      if (
           _swix(OS_ReadVarVal,
                 _INR(0,4),

                 "Browse$PreviousPage",
                 alt_url,
                 sizeof(alt_url),
                 0,
                 4)

         )
         *alt_url = 0;
    }
    else
    {
      /* Alternatively, get the Home Page URL */

      urlutils_create_home_url(alt_url, sizeof(alt_url));
    }

    /* Ensure the URL is terminated */

    alt_url[sizeof(alt_url) - 1] = 0;

    /* Reallocate URL buffer space */

    if (url_buffer)
    {
      #ifdef TRACE
522
        malloccount -= strlen(url_buffer) + 128;
523
        if (tl & (1u<<13)) Printf("** malloccount (fetchpage_process_internal): \0212%d\0217\n",malloccount);
524 525 526 527 528 529 530
      #endif

      free(url_buffer);
    }

    url_buffer = malloc(strlen(alt_url) + 128);

531
    if (!url_buffer) return make_no_fetch_memory_error(10);
532 533 534

    #ifdef TRACE
      malloccount += (strlen(alt_url) + 128);
535
      if (tl & (1u<<13)) Printf("** malloccount (fetchpage_process_internal): \0211%d\0217\n",malloccount);
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    #endif

    /* Copy the new URL into the buffer */

    strcpy(url_buffer, alt_url);
  }

  return NULL;
}

/*************************************************/
/* fetchpage_preprocessed()                      */
/*                                               */
/* Fetches a URL, which must be in the           */
/* 'url_buffer' malloced block of memory.        */
/* Intended to be called from functions such     */
/* as fetchpage_new or fetchpage_new_add.        */
/*                                               */
/* If using the URI handler, the URL will be     */
/* sent through that and won't actually fetch    */
/* at this stage, therefore.                     */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             to which the new URL refers;      */
/*                                               */
/*             1 to record the previous URL in   */
562 563 564 565 566 567
/*             the history list, else 0;         */
/*                                               */
/*             1 to stop other page fetches in   */
/*             any other frames related to this  */
/*             document, 0 to only stop those in */
/*             this frame.                       */
568 569
/*************************************************/

570
static _kernel_oserror * fetchpage_preprocessed(browser_data * b, int record, int stop)
571 572 573 574
{
  _kernel_oserror * e;

  if (
575
       !b->save_link &&
576 577 578 579 580
       browser_display_local_reference(b,
                                       url_buffer,
                                       browser_current_url(b))
     )
  {
581 582 583
    // Huh? This just causes redraw glitches... Why was it ever put here?
    //
    // if (choices.keyboard_ctrl) browser_move_selection(b, akbd_RightK);
584 585 586 587

    return NULL;
  }

588
  /* Only cancel stuff if we're allowed to */
589

590
  if (b->allow_cancel)
591
  {
592 593 594 595
    e = fetch_cancel(b);
    if (e) return e;

    /* If required, stop all fetching in all frames, else leave */
596 597 598 599 600 601
    /* images but stop everything else. Note that we don't      */
    /* touch file fetching - clicking on links will allow those */
    /* to continue, so several frames in a frameset could fetch */
    /* to a file simultaneously (the progress display in the    */
    /* status bar is aware of this, and will alter its display  */
    /* appropriately).                                          */
602 603 604

    if (stop)
    {
605 606
      if (controls.brick_wall) frames_abort_fetching(utils_ancestor(b), 1, 0);
      else                     frames_abort_fetching(utils_ancestor(b), 0, 0);
607
    }
608
  }
609

610 611 612 613
  /* Set the allow_cancel flag back to 1 for future fetches */

  b->allow_cancel = 1;

614 615 616
  /* Set the displayed type for internal / normal URLs, and */
  /* carry out any required special actions for the former. */

617
  if (!b->save_link)
618
  {
619 620 621 622 623 624 625 626
    /* Before we reset the display type, check if we're displaying */
    /* a temporary file. If so, force the 'record' flag off, as    */
    /* there's no way this can make a meaningful History entry.    */

    if (b->displayed == Display_Scrap_File) record = 0;

    /* Right, now go ahead and set the display type */

627
    urlutils_set_displayed(b, url_buffer);
628

629 630
    if (b->displayed == Display_Previous_Page) return history_fetch_backwards(b, 0);
  }
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

  e = fetchpage_process_internal(b);
  if (e) return e;

  /* If merging the URL writable and status display, put */
  /* it back to status.                                  */

  if (b->merged_url)
  {
    toolbars_merged_to_status(b, toolbars_get_upper(b));
    browser_give_general_focus(b);
  }

  if (uri_module_present && strncmp(url_buffer, Internal_URL, Int_URL_Len))
  {
    /* Send the URL through the URI handler if the module is present */
    /* and the URL isn't an internal one.                            */

    return urlutils_dispatch(b,
                             url_buffer,
                             record ? URIQueue_RecordInHistory : 0);
  }
  else
  {
    /* Without the URI handler, deal with the URL immediately */

    return fetchpage_postprocessed(b, record);
  }
}

/*************************************************/
/* fetchpage_postprocessed()                     */
/*                                               */
/* Working end to fetchpage_preprocessed, which  */
/* will fetch the url in the url_buffer block.   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             to which the new URL refers;      */
/*                                               */
/*             1 to record the previous URL in   */
/*             the history list, else 0.         */
/*************************************************/

static _kernel_oserror * fetchpage_postprocessed(browser_data * b, int record)
{
  _kernel_oserror * e;

  #ifdef TRACE
679
    if (tl & (1u<<12)) Printf("fetchpage_postprocessed: Chunk CK_FURL set to %d\n",strlen(url_buffer) + 128);
680 681
  #endif

682
  e = memory_set_chunk_size(b, NULL, CK_FURL, strlen(url_buffer) + 128);
683 684 685 686 687 688 689 690 691 692 693 694
  if (e) return e;

  strcpy(b->urlfdata, url_buffer);

  /* Make sure the URL bar is updated with the current URL. */

  toolbars_update_url(b);

  /* Set the fetch status */

  b->fetch_status = BS_START;

695 696 697 698 699 700 701 702 703 704 705 706 707
  /* Record the start of the fetch, for a parent browser window. */

  if (!b->ancestor)
  {
    _swix(OS_SetVarVal,
          _INR(0,4),

          "Browse$CurrentFetch",
          url_buffer,
          strlen(url_buffer),
          0,
          4);
  }
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

  /* Register event handlers to start off the new fetch */

  if (!b->fetch_handler) fetchpage_claim_nulls(b);

  return NULL;
}

/*************************************************/
/* fetchpage_postprocess_uri()                   */
/*                                               */
/* If the URI handler comes back with a          */
/* URI_MProcess message and we can handle the    */
/* URI it details, then that URI may be fetched  */
/* through this function - it is first copied    */
/* locally and then passed over to               */
/* fetchpage_postprocessed.                      */
/*                                               */
/* Parameters: Pointer to a browser_data         */
/*             struct relevant to the URI;       */
/*                                               */
/*             Pointer to the URI string;        */
/*                                               */
/*             1 to record the previous URL in   */
/*             the history list, else 0.         */
/*************************************************/

_kernel_oserror * fetchpage_postprocess_uri(browser_data * b, char * uri, int record)
{
  /* Reallocate URL buffer space */

  if (url_buffer)
  {
    #ifdef TRACE
742
      malloccount -= strlen(url_buffer) + 128;
743
      if (tl & (1u<<13)) Printf("** malloccount (fetchpage_postprocess_uri): \0212%d\0217\n",malloccount);
744 745 746 747 748
    #endif

    free(url_buffer);
  }

749
  url_buffer = malloc(strlen(uri) + 128);
750

751
  if (!url_buffer) return make_no_fetch_memory_error(14);
752

753 754
  #ifdef TRACE
    malloccount += (strlen(uri) + 128);
755
    if (tl & (1u<<13)) Printf("** malloccount (fetchpage_postprocess_uri): \0211%d\0217\n",malloccount);
756 757
  #endif

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
  /* Copy the URI over and fetch it */

  strcpy(url_buffer, uri);

  return fetchpage_postprocessed(b, record);
}

/*************************************************/
/* fetchpage_new()                               */
/*                                               */
/* Cancels any old fetch and starts a new one    */
/* the given URL.                                */
/*                                               */
/* The URL is copied to a malloc buffer before   */
/* being used, so the pointer to it can be from  */
/* pretty much anything (though beware of flex   */
/* blocks shifting over the actual function call */
/* boundary...).                                 */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             to which the new URL refers;      */
/*                                               */
/*             Pointer to the new URL string;    */
/*                                               */
/*             1 to record the previous URL in   */
783 784 785 786 787 788
/*             the history list, else 0;         */
/*                                               */
/*             1 to stop other page fetches in   */
/*             any other frames related to this  */
/*             document, 0 to only stop those in */
/*             this frame.                       */
789 790
/*************************************************/

791
_kernel_oserror * fetchpage_new(browser_data * b, const char * url, int record, int stop)
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
{
  /* Don't proceed unless there's something to fetch */

  if (!url || !(*url)) return fetch_cancel(b);

  /* The URL may have been passed in from the 'tokens' buffer, */
  /* and fetch cancels etc. might corrupt it. So take a copy   */
  /* of it before proceeding further, if the URL didn't come   */
  /* from this buffer already...!                              */

  if (url != url_buffer)
  {
    if (url_buffer)
    {
      #ifdef TRACE
807
        malloccount -= strlen(url_buffer) + 128;
808
        if (tl & (1u<<13)) Printf("** malloccount (fetchpage_new): \0212%d\0217\n",malloccount);
809 810 811 812 813 814 815
      #endif

      free(url_buffer);
    }

    url_buffer = malloc(strlen(url) + 128);

816
    if (!url_buffer) return make_no_fetch_memory_error(7);
817 818 819

    #ifdef TRACE
      malloccount += (strlen(url) + 128);
820
      if (tl & (1u<<13)) Printf("** malloccount (fetchpage_new): \0211%d\0217\n",malloccount);
821 822 823 824
    #endif

    strcpy(url_buffer, url);
  }
825

826
  #ifdef TRACE
827 828 829 830 831 832 833 834

    else
    {
      erb.errnum = Utils_Error_Custom_Normal;
      strcpy(erb.errmess, "Used same buffer in fetchpage_new!");
      show_error_ret(&erb);
    }

835 836 837 838
  #endif

  urlutils_fix_url(url_buffer, strlen(url_buffer) + 128);

839
  return fetchpage_preprocessed(b, record, stop);
840 841 842 843 844
}

/*************************************************/
/* fetchpage_new_add()                           */
/*                                               */
845 846 847 848
/* As fetchpage_new, but takes a second string,  */
/* which is data to be concatenated onto the end */
/* of the given URL. This may be useful for      */
/* image maps or forms data. You may also        */
849 850 851
/* specify whether this URL is to be fetched in  */
/* a new browser window or not.                  */
/*                                               */
852
/* Restrictions as for fetchpage_new.            */
853 854 855 856 857 858 859 860 861
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             to which the new URL refers;      */
/*                                               */
/*             Pointer to the new URL string;    */
/*                                               */
/*             1 to record the previous URL in   */
/*             the history list, else 0;         */
/*                                               */
862 863 864 865 866
/*             1 to stop other page fetches in   */
/*             any other frames related to this  */
/*             document, 0 to only stop those in */
/*             this frame;                       */
/*                                               */
867 868 869 870
/*             Pointer to the data to add onto   */
/*             the end of the URL string;        */
/*                                               */
/*             1 to fetch the URL in a new       */
871 872 873 874 875
/*             window, else 0;                   */
/*                                               */
/*             If opening in a new window,       */
/*             pointer to the window name (if    */
/*             wanted), or NULL.                 */
876 877
/*************************************************/

878
_kernel_oserror * fetchpage_new_add(browser_data * b, const char * url, int record, int stop, const char * add, int new_window, const char * name)
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
{
  /* Don't proceed unless there's something to fetch */

  if (!url) return fetch_cancel(b);

  /* The URL may have been passed in from the 'tokens' buffer, */
  /* and fetch cancels etc. might corrupt it. So take a copy   */
  /* of it before proceeding further, if the URL didn't come   */
  /* from this buffer already.                                 */

  if (url != url_buffer)
  {
    if (url_buffer)
    {
      #ifdef TRACE
894
        malloccount -= strlen(url_buffer) + 128;
895
        if (tl & (1u<<13)) Printf("** malloccount (fetchpage_new_add): \0212%d\0217\n",malloccount);
896 897 898 899 900 901 902
      #endif

      free(url_buffer);
    }

    url_buffer = malloc(strlen(url) + strlen(add) + 128);

903
    if (!url_buffer) return make_no_fetch_memory_error(7);
904 905 906

    #ifdef TRACE
      malloccount += (strlen(url) + strlen(add) + 128);
907
      if (tl & (1u<<13)) Printf("** malloccount (fetchpage_new_add): \0211%d\0217\n",malloccount);
908 909 910 911 912
    #endif

    strcpy(url_buffer, url);
    strcat(url_buffer, add);
  }
913

914
  #ifdef TRACE
915 916 917 918 919 920 921 922

    else
    {
      erb.errnum = Utils_Error_Custom_Normal;
      strcpy(erb.errmess, "Used same buffer in fetchpage_new_add!");
      show_error_ret(&erb);
    }

923 924 925 926
  #endif

  urlutils_fix_url(url_buffer, strlen(url_buffer) + 128);

927
  if (!new_window || b->full_screen) return fetchpage_preprocessed(b, record, stop);
928 929 930 931 932 933 934 935 936 937 938 939 940 941
  else
  {
    _kernel_oserror * e;

    RetError(windows_create_browser(url_buffer,
                                    NULL,
                                    NULL,
                                    name,
                                    Windows_CreateBrowser_Normal));

    RetError(browser_inherit(b, last_browser));

    return NULL;
  }
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
}

/*************************************************/
/* fetchpage_new_raw()                           */
/*                                               */
/* Starts a fetch of a given URL, without doing  */
/* anything to that URL at all except copying it */
/* over to a malloc buffer (to ensure it doesn't */
/* move around, as it would in a flex block).    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             to which the new URL refers;      */
/*                                               */
/*             Pointer to the new URL string;    */
/*                                               */
/*             1 to record the previous URL in   */
958 959 960 961 962 963
/*             the history list, else 0;         */
/*                                               */
/*             1 to stop other page fetches in   */
/*             any other frames related to this  */
/*             document, 0 to only stop those in */
/*             this frame.                       */
964 965
/*************************************************/

966
_kernel_oserror * fetchpage_new_raw(browser_data * b, const char * url, int record, int stop)
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
{
  /* Don't proceed unless there's something to fetch */

  if (!url || !(*url)) return fetch_cancel(b);

  /* The URL may have been passed in from the 'tokens' buffer, */
  /* and fetch cancels etc. might corrupt it. So take a copy   */
  /* of it before proceeding further, if the URL didn't come   */
  /* from this buffer already...!                              */

  if (url != url_buffer)
  {
    if (url_buffer)
    {
      #ifdef TRACE
982
        malloccount -= strlen(url_buffer) + 128;
983
        if (tl & (1u<<13)) Printf("** malloccount (fetchpage_new_raw): \0212%d\0217\n",malloccount);
984 985 986 987 988 989 990
      #endif

      free(url_buffer);
    }

    url_buffer = malloc(strlen(url) + 128);

991
    if (!url_buffer) return make_no_fetch_memory_error(7);
992 993 994

    #ifdef TRACE
      malloccount += (strlen(url) + 128);
995
      if (tl & (1u<<13)) Printf("** malloccount (fetchpage_new_raw): \0211%d\0217\n",malloccount);
996 997 998 999
    #endif

    strcpy(url_buffer, url);
  }
1000

1001
  #ifdef TRACE
1002 1003 1004 1005 1006 1007 1008 1009

    else
    {
      erb.errnum = Utils_Error_Custom_Normal;
      strcpy(erb.errmess, "Used same buffer in fetchpage_new_raw!");
      show_error_ret(&erb);
    }

1010 1011
  #endif

1012
  return fetchpage_preprocessed(b, record, stop);
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
}

/*************************************************/
/* fetchpage_claim_nulls()                       */
/*                                               */
/* Installs the relevant null event handlers so  */
/* that a fetch may proceed in the Desktop.      */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the fetch.            */
/*************************************************/

void fetchpage_claim_nulls(browser_data * b)
{
  /* Don't register the same handler twice... */

  if (!b->fetch_handler)
  {
    register_null_claimant(Wimp_ENull,(WimpEventHandler *) fetchpage_fetch,b);
    b->fetch_handler = 1;
  }

  /* Animations only apply to an ancestor window, not frames */

1037
  b = utils_ancestor(b);
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052

  /* If the 'drift' handler, to advance the animation to the */
  /* first frame and then stop, is active, remove it as the  */
  /* full-time animation handler is about to take over.      */

  if (b->anim_drift)
  {
    deregister_null_claimant(Wimp_ENull,(WimpEventHandler *) toolbars_animation_drift,b);
    b->anim_drift = 0;
  }

  /* Register the full time animation handler */

  if (!b->anim_handler)
  {
1053 1054 1055 1056 1057
    /* Is there an appropriate gadget in the status bar? */

    ObjectId lower = toolbars_get_lower(b);
    BBox     box;

1058
    if (!gadget_get_bbox(0, lower, StatusBarAnimAnim, &box))
1059 1060 1061 1062 1063 1064
    {
      /* Yes, so install an animation handler. */

      register_null_claimant(Wimp_ENull,(WimpEventHandler *) toolbars_animation,b);
      b->anim_handler = 1;
    }
1065 1066
  }

1067 1068 1069 1070
  /* Update the button bar */

  toolbars_set_button_states(b);

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
  /* Record the usage of the animation handler. This will increment */
  /* once in the ancestor object for every child fetch, so that     */
  /* the animation handler can finally be released when all the     */
  /* child fetches have stopped.                                    */

  b->current_fetches++;
}

/*************************************************/
/* fetchpage_release_nulls()                     */
/*                                               */
/* Releases all relevant null event handlers     */
1083 1084 1085
/* used for a fetch in the Desktop, *EXCEPT* for */
/* an animation drift handler, which, if the     */
/* Controls dictate it, will be installed.       */
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the fetch.            */
/*************************************************/

void fetchpage_release_nulls(browser_data * b)
{
  /* Don't register the same handler twice... */

  if (b->fetch_handler)
  {
    deregister_null_claimant(Wimp_ENull,(WimpEventHandler *) fetchpage_fetch,b);
    b->fetch_handler = 0;
  }

  /* Animations only apply to an ancestor window, not frames */

1103
  b = utils_ancestor(b);
1104 1105 1106 1107 1108 1109

  /* Only remove the handlers if there are no fetches in any */
  /* children, etc. (see fetchpage_claim_nulls comments).    */

  b->current_fetches--;

1110
  if (!b->current_fetches)
1111 1112 1113 1114 1115 1116 1117 1118 1119
  {
    /* Deregister the full time animation handler */

    if (b->anim_handler)
    {
      deregister_null_claimant(Wimp_ENull,(WimpEventHandler *) toolbars_animation,b);
      b->anim_handler = 0;
    }

1120
    /* If the Controls say to install the 'drift' handler to ensure   */
1121 1122 1123
    /* the animation finishes on the first frame, and that handler is */
    /* not already installed, install it.                             */

1124
    if (controls.anim_drift && !b->anim_drift)
1125 1126 1127 1128 1129
    {
      register_null_claimant(Wimp_ENull,(WimpEventHandler *) toolbars_animation_drift,b);
      b->anim_drift = 1;
    }
  }
1130 1131 1132 1133

  /* Update the button bar */

  toolbars_set_button_states(b);
1134 1135
}