Save 56.7 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   : Save.c                                 */
17 18 19
/*                                                 */
/* Purpose: Save functions for the browser.        */
/*                                                 */
20
/* Author : A.D.Hodgkinson                         */
21 22
/*                                                 */
/* History: 04-Dec-96: Created.                    */
23 24 25 26 27 28 29 30 31
/***************************************************/

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

#include "swis.h"
#include "flex.h"

32 33
#include "HTMLLib.h" /* HTML library API, Which will include html2_ext.h, tags.h and struct.h */

34
#include "wimp.h"
35
#include "wimplib.h"
36 37 38 39 40 41 42 43 44 45 46
#include "event.h"

#include "toolbox.h"
#include "window.h"
#include "menu.h"
#include "saveas.h"

#include "svcprint.h"
#include "Global.h"
#include "Utils.h"

47
#include "ChoiceDefs.h"
48
#include "Fetch.h" /* (Which itself includes URLstat.h) */
49
#include "Filetypes.h"
50
#include "Fontmanage.h"
51
#include "Hotlist.h"
52
#include "Protocols.h"
53 54 55 56
#include "URLutils.h"

#include "Save.h"

57 58
/* Static function prototypes */

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static int  save_save_string  (FILE * file, char * string);
static int  save_save_colour  (FILE * file, int colour);
static int  save_save_number  (FILE * file, int number);
static int  save_save_yes_no  (FILE * file, int yes);
static int  save_save_general (FILE * file, int how_many, const int numbers[], const char * strings[], int match, int def);

/* Locals */

static char * last_path = NULL;

/*************************************************/
/* save_record_path()                            */
/*                                               */
/* Record the given pathname for future use. No  */
/* errors are raised if the memory allocation to */
/* do this fails.                                */
/*                                               */
/* Note that <Wimp$Scrap>... and                 */
/* <Wimp$ScrapDir>... are two exceptions - paths */
/* starting with these are not recorded.         */
/*                                               */
/* Parameters: Pointer to a pathname to record.  */
/*************************************************/

void save_record_path(const char * path)
{
  if (!path) return;

  /* Check for the special cases */

  if (!strncmp(path, "<Wimp$Scrap>", 12) || !strncmp(path, "<Wimp$ScrapDir>", 15)) return;

  /* Otherwise, record the path, failing silently if */
  /* we can't claim enough memory for it.            */

  free(last_path);

  last_path = malloc(strlen(path) + 1);

  if (last_path) strcpy(last_path, path);
}

/*************************************************/
/* save_return_last_path()                       */
/*                                               */
/* Used mostly for filling in writables of save  */
/* dialogues with paths based on previous saves. */
/*                                               */
/* Returns:    Pointer to the last pathname that */
/*             was used for saving, or NULL if   */
/*             no such record is available.      */
/*************************************************/

const char * save_return_last_path(void)
{
  return last_path;
}
116

117
/*************************************************/
118 119 120 121 122
/* save_save_source()                            */
/*                                               */
/* Saves the document source for a given browser */
/* to the given path, setting the filetype as    */
/* HTML or text as appropriate.                  */
123
/*                                               */
124 125
/* Parameters: Pointer to a null-terminated      */
/*             pathname to save to;              */
126
/*                                               */
127 128
/*             Pointer to a browser_data struct  */
/*             owning the source to save.        */
129 130
/*************************************************/

131
_kernel_oserror * save_save_source(char * path, browser_data * b)
132
{
133
  _kernel_oserror * e;
134

135
  /* Sanity checks */
136

137
  if (!b || !b->source || !path || !*path)
138
  {
139 140 141 142 143 144 145 146 147
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Invalid parameters to save_save_source: %p, %p (source %p)",
              path,
              b,
              b ? b->source : NULL);
148

149
      return &erb;
150

151 152 153
    #else

      return NULL;
154 155

    #endif
156
  }
157

158 159
  save_record_path(path);

160
  /* Save the file - lock flex heap to ensure save transfer */
161

162
  flex_set_budge(0);
163

164 165
  e = _swix(OS_File,
            _INR(0,2) | _INR(4,5),
166

167 168 169 170 171
            10, /* Save block of memory as a typed file */
            path,
            b->page_is_text ? FileType_TEXT : FileType_HTML,
            b->source,
            ((char *) b->source) + flex_size((flex_ptr) &b->source));
172

173
  /* Unlock flex */
174

175 176 177
  flex_set_budge(1);

  return e;
178 179 180
}

/*************************************************/
181 182 183 184 185 186 187 188 189 190 191
/* save_transfer_source()                        */
/*                                               */
/* Save a browser's page source as an HTML file, */
/* through a RAM transfer buffer with            */
/* Wimp_TransferBlock.                           */
/*                                               */
/* Intended to be called as a response to a      */
/* Message_RAMFetch from another task.           */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the document source;  */
192
/*                                               */
193 194 195
/*             Pointer to an int, in which the   */
/*             amount of data transferred so     */
/*             far should be stored on entry;    */
196
/*                                               */
197 198 199 200
/*             Pointer to the WimpMessage struct */
/*             corresponding to the              */
/*             Message_RAMFetch that led to this */
/*             function being called.            */
201
/*                                               */
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
/* Returns:    The contents of the int holding   */
/*             the amount of data transferred    */
/*             prior to the function call are    */
/*             updated with the new amount       */
/*             transferred. Callers should use   */
/*             this in any future calls, and it  */
/*             *must* be checked for a value of  */
/*             -1, which indicates the transfer  */
/*             is complete (so any tidying up    */
/*             should be done if there is an     */
/*             error returned, or if the int is  */
/*             filled in with a value of -1).    */
/*                                               */
/* Assumes:    The various pointers may be NULL, */
/*             though if they are the function   */
/*             does nothing (it just exits).     */
218 219
/*************************************************/

220
_kernel_oserror * save_transfer_source(browser_data * b, int * transferred, WimpMessage * m)
221
{
222 223 224 225
  _kernel_oserror * e     = NULL;
  int               size  = save_source_size(b);
  int               left;
  int               write;
226

227
  /* Sanity check */
228

229 230 231
  if (!b || !b->source || !transferred || !m)
  {
    #ifdef TRACE
232

233
      erb.errnum = Utils_Error_Custom_Normal;
234

235 236 237 238 239 240
      sprintf(erb.errmess,
              "Invalid parameters to save_transfer_source: %p (source %p), %p, %p",
              b,
              b ? b->source : NULL,
              transferred,
              m);
241

242
      return &erb;
243

244
    #else
245

246
      return NULL;
247

248 249 250 251
    #endif
  }

  left = size - *transferred;
252

253
  /* If we have data to transfer, do so */
254

255
  if (left >= 0)
256
  {
257 258
    /* Use either the buffer size, or the bytes left, whichever */
    /* is the smallest.                                         */
259

260
    write = left > m->data.ram_fetch.buffer_size ? m->data.ram_fetch.buffer_size : left;
261

262 263 264 265
    if (write)
    {
      /* Transfer the data - must lock the flex heap for the */
      /* duration of this...                                 */
266

267
      flex_set_budge(0);
268

269 270 271 272 273
      e = wimp_transfer_block(task_handle,
                              (char *) b->source + (*transferred),
                              m->hdr.sender,
                              m->data.ram_fetch.buffer,
                              write);
274

275 276 277 278 279
      /* Unlock flex and report any errors */

      flex_set_budge(1);

      if (e) return e;
280
    }
281

282
    /* If we have any data left to send, reply with a Message_RAMTransmit */
283

284
    e = protocols_atats_send_ram_transmit(m, write, write < m->data.ram_fetch.buffer_size);
285

286 287 288 289
    /* Increment the transferred counter */

    *transferred += write;
    left         -= write;
290 291
  }

292 293 294
  /* Finished */

  return e;
295 296
}

297
/*************************************************/
298
/* save_source_size()                            */
299
/*                                               */
300 301
/* Returns the size that a given HTML source     */
/* document would take on disc.                  */
302 303
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
304
/*             owning the source to save.        */
305
/*                                               */
306
/* Returns:    Size the file will be.            */
307 308
/*************************************************/

309
int save_source_size(browser_data * b)
310
{
311 312 313 314 315
  /* Sanity check */

  if (!b || !b->source)
  {
    #ifdef TRACE
316

317
      erb.errnum = Utils_Error_Custom_Normal;
318

319 320 321 322
      sprintf(erb.errmess,
              "Invalid parameters to save_source_size: %p (source %p)",
              b,
              b ? b->source : NULL);
323

324 325 326 327 328
      show_error_ret(&erb);

    #endif

    return 0;
329 330
  }

331 332 333
  /* For now this is trivially simple, but in future it */
  /* could take account of, for example, inserting a    */
  /* <BASE> tag into the document.                      */
334

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
  return flex_size((flex_ptr) &b->source);
}

/*************************************************/
/* save_save_object()                            */
/*                                               */
/* Called when a fetch is to be spooled to disc. */
/* Handles saving as much data as is already     */
/* fetched, and setting the relevant parts of    */
/* the given browser_data structure up with the  */
/* output file details.                          */
/*                                               */
/* Parameters: Pointer to a null-terminated      */
/*             pathname to save to;              */
/*                                               */
/*             Pointer to a browser_data struct  */
/*             relevant to the object to save.   */
/*************************************************/

_kernel_oserror * save_save_object(char * path, browser_data * b)
{
  /* Sanity check */

  if (!b || !path || !*path)
359
  {
360 361 362 363 364 365 366 367 368 369
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Invalid parameters to save_save_object: %p, %p",
              path,
              b);

      return &erb;
370

371
    #else
372

373
      return NULL;
374

375
    #endif
376 377
  }

378 379
  save_record_path(path);

380
  /* If using a small fetch window, set the title to the save pathname */
381

382 383 384
  if (b->small_fetch)
  {
    char title[Limits_Title];
385

386 387 388 389 390 391
    StrNCpy0(title, path);

    /* Don't treat any errors here as fatal */

    show_error_ret(window_set_title(0, b->self_id, title));
  }
392

393
  /* Open the file */
394

395
  b->save_file = fopen(path, "wb");
396

397
  if (!b->save_file)
398
  {
399
    fetch_stop(b, 0);
400
    RetLastE;
401
  }
402
  else
403 404
  {
    int bytes;
405

406 407 408
    /* Set the filetype to DEADDEAD, to represent an incomplete */
    /* file (particularly good on later Filers, which display   */
    /* a special sprite for this).                              */
409

410 411
    _swix(OS_File,
          _INR(0,2),
412

413 414 415
          2, /* Set load address */
          path,
          0xdeaddead);
416

417 418
    _swix(OS_File,
          _INR(0,1) | _IN(3),
419

420 421 422
          3, /* Set exec address */
          path,
          0xdeaddead);
423

424 425 426 427 428 429
    if (b->source)
    {
      /* Any data in the source store represents already */
      /* fetched bits of the file. Must lock flex down   */
      /* over the save to make sure the heap doesn't     */
      /* shift over the call to fwrite.                  */
430

431
      flex_set_budge(0);
432

433 434 435 436
      bytes = fwrite(b->source,
                     1,
                     flex_size((flex_ptr) &b->source),
                     b->save_file);
437

438
      flex_set_budge(1);
439

440
      /* If we didn't transfer as much as we expected, complain */
441

442 443 444
      if (bytes != flex_size((flex_ptr) &b->source))
      {
        /* Report any errors */
445

446 447 448 449 450 451 452
        fetch_stop(b, 0);
        RetLastE;
      }
      else
      {
        /* Otherwise, get rid of the data in the source store */
        /* as it's been written to the file.                  */
453

454 455 456
        flex_free((flex_ptr) &b->source);
        b->source = NULL;
      }
457 458
    }
  }
459

460 461
  return NULL;
}
462

463 464 465 466 467 468 469 470 471 472 473 474
/*************************************************/
/* save_object_size()                            */
/*                                               */
/* Returns the estimated size that a given       */
/* object will be after being spooled through    */
/* the fetcher.                                  */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the object to save.   */
/*                                               */
/* Returns:    *Estimated* size of the object.   */
/*************************************************/
475

476 477 478
int save_object_size(browser_data * b)
{
  /* For now, we just don't know this information... */
479

480
  return 4096;
481
}
482

483 484 485 486 487 488 489 490
/*************************************************/
/* save_save_uri()                               */
/*                                               */
/* Save the contents of a link as a URI file.    */
/*                                               */
/* Parameters: Pointer to a null-terminated      */
/*             pathname to save to;              */
/*                                               */
491 492 493 494
/*             Pointer to the URI to save;       */
/*                                               */
/*             Pointer to the URL title, or NULL */
/*             / null string for no title;       */
495 496 497 498 499 500
/*                                               */
/*             0 to save as a URI file, else     */
/*             save as a non-terminated string   */
/*             with type FileType_URL (ANT       */
/*             suite URL file).                  */
/*************************************************/
501

502
_kernel_oserror * save_save_uri(char * path, char * url, char * title, int write_url)
503 504 505
{
  _kernel_oserror * e = NULL;
  FILE            * file;
506

507
  /* Sanity check */
508

509 510
  if (title && !*title) title = NULL;

511 512 513
  if (!path || !*path || !url || !*url)
  {
    #ifdef TRACE
514

515
      erb.errnum = Utils_Error_Custom_Normal;
516

517 518 519 520 521
      sprintf(erb.errmess,
              "Invalid parameters to save_save_uri: %p, %p, %d",
              path,
              url,
              write_url);
522

523
      return &erb;
524

525
    #else
526

527
      return NULL;
528

529 530
    #endif
  }
531

532 533
  save_record_path(path);

534
  /* Try and open the file */
535

536
  file = fopen(path, "wb");
537
  if (!file) RetLastE;
538

539
  /* Write the contents */
540

541 542 543 544 545 546
  if (!write_url)
  {
    /* URI file */

    if (
         fprintf(file,
547
                 "URI\t100\n\t# %s v",
548 549
                 lookup_token("_TaskName",1,0)) < 0
       )
550
       erb = *_kernel_last_oserror(), e = &erb;
551 552 553

    else if (
              fprintf(file,
554
                      "%s\n\n\t%s",
555 556 557
                      lookup_token("Version:(Unknown!)",0,0),
                      url) < 0
            )
558
            erb = *_kernel_last_oserror(), e = &erb;
559 560 561 562 563 564 565

    else if (
              fprintf(file,
                      "\n\t%s",
                      title ? title : "*") < 0
            )
            erb = *_kernel_last_oserror(), e = &erb;
566 567 568 569
  }
  else
  {
    /* URL file */
570

571
    if (fprintf(file, url) < 0) erb = *_kernel_last_oserror(), e = &erb;
572
  }
573

574 575
  /* Close the file and return any error that may have */
  /* happened during the file writing stage            */
576

577
  fclose(file);
578

579
  if (e) return &erb;
580

581
  /* Exit via. setting the filetype */
582

583 584
  return _swix(OS_File,
               _INR(0,2),
585

586 587 588 589
               18, /* Set type of named object */
               path,
               write_url ? FileType_URL : FileType_URI);
}
590

591 592 593 594 595 596 597 598 599 600 601 602
/*************************************************/
/* save_transfer_uri()                           */
/*                                               */
/* Save the contents of a link as a URI file,    */
/* through a RAM transfer buffer with            */
/* Wimp_TransferBlock.                           */
/*                                               */
/* Intended to be called as a response to a      */
/* Message_RAMFetch from another task.           */
/*                                               */
/* Parameters: Pointer to the URL to save;       */
/*                                               */
603 604 605
/*             Pointer to the URL title, or NULL */
/*             / null string for no title;       */
/*                                               */
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
/*             0 to save as a URI file, else     */
/*             save as a non-terminated string   */
/*             with type FileType_URL (ANT       */
/*             suite URL file);                  */
/*                                               */
/*             Pointer to an int, in which the   */
/*             amount of data transferred so     */
/*             far should be stored on entry;    */
/*                                               */
/*             Pointer to the WimpMessage struct */
/*             corresponding to the              */
/*             Message_RAMFetch that led to this */
/*             function being called.            */
/*                                               */
/* Returns:    The contents of the int holding   */
/*             the amount of data transferred    */
/*             prior to the function call are    */
/*             updated with the new amount       */
/*             transferred. Callers should use   */
/*             this in any future calls, and it  */
/*             *must* be checked for a value of  */
/*             -1, which indicates the transfer  */
/*             is complete (so any tidying up    */
/*             should be done if there is an     */
/*             error returned, or if the int is  */
/*             filled in with a value of -1).    */
/*                                               */
/* Assumes:    The various pointers may be NULL, */
/*             though if they are the function   */
/*             does nothing (it just exits).     */
/*************************************************/
637

638
_kernel_oserror * save_transfer_uri(char * url, char * title, int write_url, int * transferred, WimpMessage * m)
639 640 641
{
  _kernel_oserror * e        = NULL;
  char            * uri_file = NULL;
642
  int               size     = save_uri_size(url, title, write_url);
643 644
  int               left;
  int               write;
645

646
  /* Sanity check */
647

648 649
  if (title && !*title) title = NULL;

650 651 652
  if (!url || !*url || !transferred || !m)
  {
    #ifdef TRACE
653

654
      erb.errnum = Utils_Error_Custom_Normal;
655

656 657 658 659 660 661
      sprintf(erb.errmess,
              "Invalid parameters to save_transfer_uri: %p, %d, %p, %p",
              url,
              write_url,
              transferred,
              m);
662

663
      return &erb;
664

665
    #else
666

667
      return NULL;
668

669 670
    #endif
  }
671

672
  left = size - *transferred;
673

674 675
  /* Each time the function is called, rebuild the URI file in a */
  /* temporary buffer.                                           */
676

677 678 679
  if (size > 0 && left > 0)
  {
    uri_file = malloc(size + 1); /* + 1 to account for terminators */
680

681 682 683 684 685
    if (!uri_file)
    {
      e = make_no_memory_error(10);
      goto save_transfer_uri_exit;
    }
686

687
    /* Build the URI or URL file */
688

689 690 691
    if (!write_url)
    {
      int written;
692

693
      /* URI file */
694

695
      written = sprintf(uri_file,
696
                        "URI\t100\n\t# %s v",
697
                        lookup_token("_TaskName",1,0));
698

699 700 701 702 703 704 705 706
      written += sprintf(uri_file + written,
                         "%s\n\n\t%s",
                         lookup_token("Version:(Unknown!)",0,0),
                         url);

      written += sprintf(uri_file + written,
                         "\n\t%s",
                         title ? title : "*");
707 708 709 710
    }
    else
    {
      /* URL file */
711

712 713 714
      strcpy(uri_file, url);
    }
  }
715

716
  /* If we have data to transfer, do so */
717

718
  if (left >= 0)
719
  {
720 721
    /* Use either the buffer size, or the bytes left, whichever */
    /* is the smallest.                                         */
722

723
    write = left > m->data.ram_fetch.buffer_size ? m->data.ram_fetch.buffer_size : left;
724

725
    if (write)
726
    {
727 728 729 730 731 732 733 734 735
      /* Transfer the data */

      e = wimp_transfer_block(task_handle,
                              uri_file + (*transferred),
                              m->hdr.sender,
                              m->data.ram_fetch.buffer,
                              write);

      if (e) goto save_transfer_uri_exit;
736 737
    }

738
    /* If we have any data left to send, reply with a Message_RAMTransmit */
739

740
    e = protocols_atats_send_ram_transmit(m, write, write < m->data.ram_fetch.buffer_size);
741

742
    /* Increment the transferred counter */
743

744 745 746
    *transferred += write;
    left         -= write;
  }
747

748
  /* Finished */
749

750
save_transfer_uri_exit:
751

752
  /* Free the URI file buffer, if we claimed one */
753

754
  if (uri_file) free (uri_file);
755

756
  return e;
757 758 759
}

/*************************************************/
760 761 762 763
/* save_uri_size()                               */
/*                                               */
/* Returns the size, in bytes, that a URI or URL */
/* file will be.                                 */
764
/*                                               */
765 766 767
/* Parameters: Pointer to the URL that would be  */
/*             saved;                            */
/*                                               */
768 769 770
/*             Pointer to the URL title, or NULL */
/*             / null string for no title;       */
/*                                               */
771 772 773 774 775 776
/*             0 to find the length of a URI     */
/*             file, 1 to find the length of a   */
/*             URL file (as in save_save_uri).   */
/*                                               */
/* Returns:    The size, in bytes, that the file */
/*             will be.                          */
777 778
/*************************************************/

779
int save_uri_size(char * url, char * title, int write_url)
780
{
781
  int len = 0;
782

783
  /* Sanity check */
784

785 786
  if (title && !*title) title = NULL;

787 788 789
  if (!url || !*url)
  {
    #ifdef TRACE
790

791
      erb.errnum = Utils_Error_Custom_Normal;
792

793 794 795 796
      sprintf(erb.errmess,
              "Invalid parameters to save_uri_size: %p, %d",
              url,
              write_url);
797

798
      show_error_ret(&erb);
799

800
    #endif
801

802 803
    return 0;
  }
804

805 806 807
  if (!write_url)
  {
    /* URI file */
808

809
    len =  strlen("URI\t100\n\t# ");
810
    len += strlen(lookup_token("_TaskName",1,0));
811

812 813
    len += strlen(" v");
    len += strlen(lookup_token("Version:(Unknown!)",0,0));
814

815
    len += strlen("\n\n\t");
816
    len += strlen(url);
817 818 819

    len += strlen("\n\t");
    len += strlen(title ? title : "*");
820
  }
821 822 823
  else
  {
    /* URL file */
824

825 826
    len = strlen(url); /* (No terminator needed in URL files, so no '+ 1') */
  }
827

828
  return len;
829
}
830 831 832 833 834 835 836 837 838

/*************************************************/
/* save_build_messages_path()                    */
/*                                               */
/* Builds a pathname in a malloc block through   */
/* which the Choices or Controls file may be     */
/* accessed. The caller is responsible for       */
/* freeing the block.                            */
/*                                               */
839 840 841 842 843
/* Parameters: 0 for the Choices file (to load), */
/*             1 for the Controls file (to       */
/*             load), 2 for the Choices file (to */
/*             save), 3 for the Controls file    */
/*             (to save).                        */
844 845 846 847 848 849 850 851 852 853 854 855 856
/*                                               */
/* Returns:    Pointer to a null-terminated      */
/*             pathname, in a malloc block, or   */
/*             NULL if there was an error.       */
/*************************************************/

char * save_build_messages_path(int which)
{
  int    len, exists;
  char * path;
  char * sysvar;
  char * defpath;

857 858 859 860 861 862 863 864
  len =  strlen(lookup_token("_TaskName", 1, 0));
  len += sizeof("$ControlsFile"); /* Longer than "$ChoicesFile"! */

  sysvar = malloc(len);

  if (!sysvar) return NULL;
  else strcpy(sysvar, tokens);

865 866 867 868
  switch (which)
  {
    case 0:
    {
869
      strcat(sysvar, "$ChoicesFile");
870 871 872 873 874 875
      defpath = ".Choices";
    }
    break;

    case 1:
    {
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
      strcat(sysvar, "$ControlsFile");
      defpath = ".Controls";
    }
    break;

    case 2:
    {
      strcat(sysvar, "$ChoicesSave");
      defpath = ".Choices";
    }
    break;

    case 3:
    {
      strcat(sysvar, "$ControlsSave");
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
      defpath = ".Controls";
    }
    break;

    default:
    {
      #ifdef TRACE

        erb.errnum = Utils_Error_Custom_Fatal;

        sprintf(erb.errmess,
                "In open_messages_file, passed unrecognised parameter value '%d'",
                which);

        /* OK, so you'll get the above error and then callers will */
        /* probably think this call ran out of memory due to the   */
        /* NULL return (assuming the below call doesn't cause an   */
        /* immediate exit - it depends on what stage of            */
        /* initialisation the browser is at). But at least you get */
        /* to see what is happening.                               */

        show_error_ret(&erb);
913
        free(sysvar);
914 915 916 917 918

        return NULL;

      #else

919 920
        free(sysvar);

921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
        return NULL;

      #endif
    }
  }

  /* First, find the system variable length. Must use _kernel_swi */
  /* here for various reasons.                                    */

  {
    _kernel_swi_regs r;

    r.r[0] = (int) sysvar;
    r.r[1] = (int) NULL;
    r.r[2] = -1;
    r.r[3] = 0;
    r.r[4] = 0;

    /* Equivalent to getenv(), but the RISC OS implementation evaluates */
    /* the system variable as an expression which we don't want (at     */
    /* least, not under RISC OS); hence the direct use of the SWI.      */

    _kernel_swi(OS_ReadVarVal, &r, &r);

    len = -r.r[2]; /* This includes a terminator */
  }

  if (!len)
  {
    /* Variable doesn't exist */

    len    = strlen(task_dir) + strlen(defpath) + 1;
    exists = 0;
  }
  else exists = 1;

  /* Allocate space */

959
  path = calloc(len + 1, 1);
960

961 962 963 964 965
  if (!path)
  {
    free(sysvar);
    return NULL;
  }
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982

  /* Read the variable or copy the data in */

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

                    sysvar, /* Variable name                      */
                    path,   /* Buffer                             */
                    len,    /* Size of buffer                     */
                    0,      /* Name pointer (0 for 1st call)      */
                    4);     /* Variable type (4 = literal string) */
  else
  {
    strcpy(path, task_dir);
    strcat(path, defpath);
  }

983 984
  free(sysvar);

985 986 987
  return path;
}

988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
/*************************************************/
/* save_save_string()                            */
/*                                               */
/* Output a string                               */
/*                                               */
/* Parameters: Pointer to a FILE struct holding  */
/*             info on the file to write to;     */
/*                                               */
/*             String number to write.           */
/*                                               */
/* Returns:    Number of characters written, or  */
/*             EOF if there was an error.        */
/*************************************************/

1002
static int save_save_string(FILE * file, char * string)
1003 1004 1005 1006 1007 1008 1009 1010 1011
{
  int length;
  if ((length = strlen(string)) == 0) return 0;

  if (fprintf(file, "%s", string) < length) return EOF;

  return length;
}

1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
/*************************************************/
/* save_save_colour()                            */
/*                                               */
/* Output a colour number in the format seen in  */
/* the Choices file, to a given file.            */
/*                                               */
/* Parameters: Pointer to a FILE struct holding  */
/*             info on the file to write to;     */
/*                                               */
/*             Colour number to write.           */
/*                                               */
/* Returns:    Number of characters written, or  */
/*             EOF if there was an error.        */
/*************************************************/

1027
static int save_save_colour(FILE * file, int colour)
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
{
  if (fprintf(file, "0x%08x", colour) < 10) return EOF;

  return 10;
}

/*************************************************/
/* save_save_number()                            */
/*                                               */
/* Output a number as ASCII, to a given file.    */
/*                                               */
/* Parameters: Pointer to a FILE struct holding  */
/*             info on the file to write to;     */
/*                                               */
/*             Number to write.                  */
/*                                               */
/* Returns:    Number of characters written, or  */
/*             EOF if there was an error.        */
/*************************************************/

1048
static int save_save_number(FILE * file, int number)
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
{
  char buffer[64];
  int  len;

  len = sprintf(buffer, "%d", number);

  if (fprintf(file, "%s", buffer) < len) return EOF;

  return len;
}

/*************************************************/
/* save_save_yes_no()                            */
/*                                               */
/* Output either 'yes' or 'no' to a given file.  */
/*                                               */
/* Parameters: Pointer to a FILE struct holding  */
/*             info on the file to write to;     */
/*                                               */
/*             0 to write 'no', else 'yes'.      */
/*                                               */
/* Returns:    Number of characters written, or  */
/*             EOF if there was an error.        */
/*************************************************/

1074
static int save_save_yes_no(FILE * file, int yes)
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
{
  if (!yes)
  {
    if (fprintf(file, "no") < 2) return EOF;
  }
  else
  {
    if (fprintf(file, "yes") < 3) return EOF;
  }

  return !yes ? 2 : 3;
}

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
/*************************************************/
/* save_save_font()                              */
/*                                               */
/* Saves a typeface definition in the form       */
/* required by the browser font manager.         */
/*                                               */
/* Parameters: Pointer to a FILE struct holding  */
/*             info on the file to write to;     */
/*                                               */
/*             The name of the typeface to save. */
/*                                               */
/* Returns:    Number of characters written, or  */
/*             EOF if there was an error.        */
/*                                               */
/* Assumes:    No sanity checking is given on    */
/*             any of the supplied parameters... */
/*************************************************/

static int save_save_font(FILE * file, char * typefacename)
{
  fm_typeface * tfptr;

  tfptr = fm_find_typeface(typefacename);
1111

1112 1113
  if (!tfptr) return EOF;

1114 1115 1116 1117 1118 1119 1120 1121
  return fprintf(file,
                 "%s=%s:%s:%s:%s;%s",
                 tfptr->name,
                 tfptr->fontnames[0],
                 tfptr->fontnames[1],
                 tfptr->fontnames[2],
                 tfptr->fontnames[3],
                 tfptr->alternative);
1122 1123
}

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
/*************************************************/
/* save_save_general()                           */
/*                                               */
/* Save a general item to the given file. This   */
/* is for items with a discrete set of values    */
/* that map to a discrete set of strings.        */
/*                                               */
/* Parameters: Pointer to a FILE struct holding  */
/*             info on the file to write to;     */
/*                                               */
/*             The number of discrete values     */
/*             that the item may take;           */
/*                                               */
/*             Pointer to an array of the above  */
/*             size holding the values;          */
/*                                               */
/*             Pointer to an array of (char *)'s */
/*             pointing to the strings that map  */
/*             to the values in the int array;   */
/*                                               */
/*             Value to match out of those given */
/*             in the int array;                 */
/*                                               */
/*             Default item (from 1 to the value */
/*             given in the second parameter) to */
/*             use if the match value is not     */
/*             found in the int array.           */
/*                                               */
/* Returns:    Number of characters written, or  */
/*             EOF if there was an error.        */
/*                                               */
/* Assumes:    No sanity checking is given on    */
/*             any of the supplied parameters... */
/*************************************************/

static int save_save_general(FILE * file, int how_many, const int numbers[], const char * strings[], int match, int def)
{
  int          i, found = 0;
  const char * string   = NULL;
  int          sl;

  /* Try to find the match value in the array of ints */

  for (i = 0; i < how_many && !found; i++)
  {
    if (numbers[i] == match) string = strings[i], found = 1;
  }

  /* If not found, raise an error in TRACE builds, and pick */
  /* the given default string                               */

  if (!found)
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Match value '%d' not found in save_save_general (",
              match);

      for (
            i = 0;
            i < how_many && strlen(erb.errmess) + strlen(strings[i]) + 9 < sizeof(erb.errmess);
            i++
          )
      {
        strcat(erb.errmess, strings[i]);
        if (i != how_many - 1) strcat(erb.errmess, ", ");

        if (i == how_many - 2) strcat(erb.errmess, "or ");
      }

      strcat(erb.errmess, ").");

      show_error_ret(&erb);

    #endif

    if (def > how_many) def = how_many;

    string = strings[def - 1];
  }

  /* Output the item to the file */

  if (!string) return EOF;

  sl = strlen(string);

  if (fprintf(file, "%s", string) < sl) return EOF;

  return sl;
}

1219 1220 1221 1222
/*************************************************/
/* save_save_choices()                           */
/*                                               */
/* Saves out the Choices file to either the      */
1223 1224 1225 1226
/* given path or to the path constructed in      */
/* save_build_messages_page. Uses the global     */
/* Choices block as the source of what should be */
/* written.                                      */
1227 1228 1229
/*                                               */
/* Parameters: Pointer to a null-terminated path */
/*             to write the file to, or NULL for */
1230
/*             default (see description above).  */
1231 1232 1233 1234
/*************************************************/

_kernel_oserror * save_save_choices(char * path)
{
1235 1236 1237 1238 1239
  _kernel_oserror * e                 = NULL;

  FILE            * file              = NULL;
  char            * out               = NULL;
  char            * canonicalised_out = NULL;
1240

1241 1242 1243
  char            * p                 = NULL; /* Moves through the file loaded into a malloc block. */
  char            * op                = NULL; /* Remembers the start address of the block.          */
  char            * en;                       /* Remembers the end address of the block.            */
1244

1245 1246
  int               size;
  int               type;
1247 1248 1249 1250 1251 1252

  #ifdef TRACE
    if (tl & (1u<<26)) Printf("save_save_choices: Called\n");
  #endif

  /* Find out where to get the file from */
1253 1254

  if (path && *path) out = path;
1255
  else               out = save_build_messages_path(2);
1256

1257 1258
  if (!out) return make_no_memory_error(14);

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
  /* Now ensure the path is there */

  (utils_canonicalise_path(out, &canonicalised_out));

  /* Can get rid of 'out' if we allocated it locally */

  if (!path || !*path)
  {
    free(out);
    out = NULL;
  }

  if (!canonicalised_out) return make_no_memory_error(14);

1273
  #ifdef TRACE
1274
    if (tl & (1u<<26)) Printf("save_save_choices: Working with file '%s'\n", canonicalised_out);
1275 1276
  #endif

1277
  /* Ensure this path is available */
1278

1279 1280
  e = utils_build_tree(canonicalised_out);
  if (e) goto save_save_choices_read_error;
1281

1282 1283 1284 1285 1286 1287 1288 1289
  /* Find out how big the file is - for this of course, use */
  /* the *load* path, not the canonicalised save path       */
  {
    char * load_path = save_build_messages_path(0);

    if (!load_path)
    {
      free(canonicalised_out);
1290

1291 1292
      return make_no_memory_error(14);
    }
1293

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
    e = _swix(OS_File,
              _INR(0,1) | _OUT(0) | _OUT(4),

              17,
              load_path,

              &type,
              &size);

    /* Throw the local path away - want to minimise how many */
    /* malloc blocks we're juggling for fear of opening up a */
    /* memory leak in here                                   */

    free(load_path);

    if (e || type != 1) goto save_save_choices_read_error;
  }
1311 1312 1313 1314 1315 1316 1317

  /* Allocate memory for the file */

  p = malloc(size + 1);

  if (!p)
  {
1318
    free(canonicalised_out);
1319 1320 1321 1322 1323 1324 1325

    return make_no_memory_error(14);
  }

  en = p + size;
  op = p;

1326 1327 1328 1329 1330
  /* Read the file into memory - again, use the load path */
  /* not the save path. Rebuild this path to avoid having */
  /* any more malloc blocks flying about (see comments    */
  /* earlier) - it doesn't take long to do this, and the  */
  /* function isn't speed critical anyway.                */
1331

1332 1333
  {
    char * load_path = save_build_messages_path(0);
1334

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
    if (!load_path)
    {
      free(canonicalised_out);
      free(op);

      return make_no_memory_error(14);
    }

    e = _swix(OS_File,
              _INR(0,3),

              16,
              load_path,
              p,
              0);

    free(load_path);

    if (e) goto save_save_choices_read_error;
  }
1355 1356 1357 1358 1359 1360 1361 1362 1363

  /* Force a null terminator at the end, so strchr() calls */
  /* (or equivalent code fragments) don't run off the end  */
  /* of the block.                                         */

  p[size] = 0;

  /* Open it for writing */

1364
  file = fopen(canonicalised_out, "wb");
1365

1366
  if (!file) goto save_save_choices_write_error;
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418

  /* Use the original file in memory as a reference for */
  /* writing the new one.                               */

  do
  {
    /* Comments or white space */

    if (*p == '#' || *p < ' ')
    {
      /* Output until an end of line marker */

      while (*p >= ' ')
      {
        if (fputc(*p++, file) == EOF) goto save_save_choices_write_error;
      }

      /* Output any control characters - we thus preserve   */
      /* CR, LF, CR+LF or whatever was in the original file */

      while (*p < ' ' && p < en)
      {
        if (fputc(*p++, file) == EOF) goto save_save_choices_write_error;
      }
    }

    /* If not comments or white space, must have a token */

    else
    {
      char * token_end;
      int    len;

      token_end = p;

      /* Search for a ':', but don't go off the end of a line */

      while (*token_end > 31 && *token_end != ':') token_end++;

      len = token_end - p; /* Doesn't include ':' in the count */

      if (*token_end == ':' && len)
      {
        int i, result = 0;

        for (i = 0; i <= len; i++) /* '<=' to include the ':' */
        {
          if (fputc(p[i], file) == EOF) goto save_save_choices_write_error;
        }

        /* Now it just gets tedious...! */

1419
        if      (!strncmp(p, "HomePage",         len)) result = save_save_string(file, choices.home_page);
1420

1421
        else if (!strncmp(p, "BackColour",       len)) result = save_save_colour(file, choices.background_colour);
1422 1423 1424 1425 1426 1427
        else if (!strncmp(p, "TextColour",       len)) result = save_save_colour(file, choices.text_colour);
        else if (!strncmp(p, "LinkColour",       len)) result = save_save_colour(file, choices.link_colour);
        else if (!strncmp(p, "UsedColour",       len)) result = save_save_colour(file, choices.used_colour);
        else if (!strncmp(p, "FollColour",       len)) result = save_save_colour(file, choices.followed_colour);
        else if (!strncmp(p, "SeleColour",       len)) result = save_save_colour(file, choices.selected_colour);

1428
        else if (!strncmp(p, "SupportTables",    len)) result = save_save_yes_no(file, choices.support_tables);
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
        else if (!strncmp(p, "TableOuter",       len))
        {
          static const char * s[] = {
                                      "2d",
                                      "auto",
                                      "3d",
                                      "never"
                                    };

          static const int    v[] = {
                                      Choices_TableOuter_Always2D,
                                      Choices_TableOuter_Auto,
                                      Choices_TableOuter_Always3D,
                                      Choices_TableOuter_Never
                                    };

          result = save_save_general(file, 4, v, s, choices.table_outer, 3);
        }
        else if (!strncmp(p, "TableInner",       len))
        {
          static const char * s[] = {
                                      "2d",
                                      "auto",
                                      "3d",
                                      "never"
                                    };

          static const int    v[] = {
                                      Choices_TableInner_Always2D,
                                      Choices_TableInner_Auto,
                                      Choices_TableInner_Always3D,
                                      Choices_TableInner_Never
                                    };

          result = save_save_general(file, 4, v, s, choices.table_inner, 3);
        }

1466
        else if (!strncmp(p, "FontSize",         len)) result = save_save_number(file, choices.font_size);
1467
        else if (!strncmp(p, "TTAspect",         len)) result = save_save_number(file, choices.tt_aspect);
1468
        else if (!strncmp(p, "SystemFont",       len)) result = save_save_yes_no(file, choices.system_font);
1469 1470 1471
        else if (!strncmp(p, "Typeface1",        len)) result = save_save_font(file, "fixed");
        else if (!strncmp(p, "Typeface2",        len)) result = save_save_font(file, "sans");
        else if (!strncmp(p, "Typeface3",        len)) result = save_save_font(file, "serif");
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
        else if (!strncmp(p, "Encoding",         len)) result = save_save_number(file, choices.encoding);

        else if (!strncmp(p, "UnderlineLinks",   len)) result = save_save_yes_no(file, choices.underline_links);
        else if (!strncmp(p, "UseSourceCols",    len)) result = save_save_yes_no(file, choices.use_source_cols);
        else if (!strncmp(p, "ShowForeground",   len)) result = save_save_yes_no(file, choices.show_foreground);
        else if (!strncmp(p, "ShowBackground",   len)) result = save_save_yes_no(file, choices.show_background);

        else if (!strncmp(p, "LeftMargin",       len)) result = save_save_number(file, choices.left_margin);
        else if (!strncmp(p, "RightMargin",      len)) result = save_save_number(file, choices.right_margin);
        else if (!strncmp(p, "QuoteMargin",      len)) result = save_save_number(file, choices.quote_margin);
        else if (!strncmp(p, "Leading",          len)) result = save_save_number(file, choices.leading);
        else if (!strncmp(p, "LeftIndent",       len)) result = save_save_number(file, choices.left_indent);

        else if (!strncmp(p, "MaxImages",        len)) result = save_save_number(file, choices.maximages);
        else if (!strncmp(p, "ClientPull",       len)) result = save_save_yes_no(file, choices.client_pull);
1487 1488 1489 1490 1491 1492 1493 1494 1495
        else if (!strncmp(p, "SupportFrames",    len)) result = save_save_yes_no(file, choices.support_frames);
        else if (!strncmp(p, "SupportObject",    len)) result = save_save_yes_no(file, choices.support_object);
        else if (!strncmp(p, "PlugInControl",    len))
        {
          static const char * s[] = {
                                      "never",
                                      "viewed",
                                      "asap"
                                    };
1496

1497 1498 1499 1500 1501 1502 1503 1504
          static const int    v[] = {
                                      Choices_PlugIns_Never,
                                      Choices_PlugIns_Viewed,
                                      Choices_PlugIns_ASAP
                                    };

          result = save_save_general(file, 3, v, s, choices.plugin_control, 3);
        }
1505 1506
        else if (!strncmp(p, "SeeFetches",       len)) result = save_save_yes_no(file, choices.see_fetches);

1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
        else if (!strncmp(p, "SaveHotlist",      len))
        {
          static const char * s[] = {
                                      "never",
                                      "once",
                                      "always"
                                    };

          static const int    v[] = {
                                      Choices_SaveHotlist_Never,
                                      Choices_SaveHotlist_Once,
                                      Choices_SaveHotlist_Always
                                    };

          result = save_save_general(file, 3, v, s, choices.save_hotlist, 3);
        }
        else if (!strncmp(p, "AddHotlist", len))
        {
          static const char * s[] = {
                                      "top",
                                      "bottom"
                                    };

          static const int    v[] = {
                                      Choices_AddHotlist_Top,
                                      Choices_AddHotlist_Bottom
                                    };

          result = save_save_general(file, 2, v, s, choices.add_hotlist, 2);
        }
        else if (!strncmp(p, "HotlistType", len))
        {
          static const char * s[] = {
                                      "urls",
                                      "descriptions"
                                    };

          static const int    v[] = {
                                      Choices_HotlistType_URLs,
                                      Choices_HotlistType_Descriptions
                                    };

          result = save_save_general(file, 2, v, s, choices.hotlist_show, 2);
        }
        else if (!strncmp(p, "AutoOpenDelay",    len)) result = save_save_number(file, choices.auto_open_delay);
        else if (!strncmp(p, "AutoScrollDelay",  len)) result = save_save_number(file, choices.auto_scroll_delay);
        else if (!strncmp(p, "AutoScrollMargin", len)) result = save_save_number(file, choices.auto_scroll_margin);

1555 1556
        else if (!strncmp(p, "MaxSize",          len)) result = save_save_number(file, choices.max_size         / 1024); /* (Convert bytes to K)       */
        else if (!strncmp(p, "ImageMaxSize",     len)) result = save_save_number(file, choices.image_max_size   / 1024);
1557 1558
        else if (!strncmp(p, "ExpiryAge",        len)) result = save_save_number(file, choices.expiry_age);
        else if (!strncmp(p, "ImageExpiryAge",   len)) result = save_save_number(file, choices.image_expiry_age);
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
        else if (!strncmp(p, "ShowURLs",         len)) result = save_save_yes_no(file, choices.show_urls);
        else if (!strncmp(p, "SaveHistory",      len))
        {
          static const char * s[] = {
                                      "never",
                                      "once",
                                      "always"
                                    };

          static const int    v[] = {
                                      Choices_SaveHistory_Never,
                                      Choices_SaveHistory_Once,
                                      Choices_SaveHistory_Always
                                    };

          result = save_save_general(file, 3, v, s, choices.save_history, 3);
        }
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
        else if (!strncmp(p, "SaveImageHistory", len))
        {
          static const char * s[] = {
                                      "never",
                                      "once",
                                      "always"
                                    };

          static const int    v[] = {
                                      Choices_SaveImageHistory_Never,
                                      Choices_SaveImageHistory_Once,
                                      Choices_SaveImageHistory_Always
                                    };

          result = save_save_general(file, 3, v, s, choices.save_image_history, 3);
        }
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676

        else if (!strncmp(p, "URLbar",           len)) result = save_save_yes_no(file, choices.url_bar);
        else if (!strncmp(p, "ButtonBar",        len)) result = save_save_yes_no(file, choices.button_bar);
        else if (!strncmp(p, "StatusBar",        len)) result = save_save_yes_no(file, choices.status_bar);
        else if (!strncmp(p, "MoveGadgets",      len))
        {
          static const char * s[] = {
                                      "never",
                                      "atend",
                                      "during"
                                    };

          static const int    v[] = {
                                      Choices_MoveGadgets_Never,
                                      Choices_MoveGadgets_AtEnd,
                                      Choices_MoveGadgets_During
                                    };

          result = save_save_general(file, 3, v, s, choices.move_gadgets, 3);
        }

        else if (!strncmp(p, "Width",            len)) result = save_save_number(file, choices.width);
        else if (!strncmp(p, "Height",           len)) result = save_save_number(file, choices.height);
        else if (!strncmp(p, "OverrideX",        len)) result = save_save_number(file, choices.override_x);
        else if (!strncmp(p, "OverrideY",        len)) result = save_save_number(file, choices.override_y);
        else if (!strncmp(p, "SolidResize",      len))
        {
          static const char * s[] = {
                                      "no",
                                      "yes",
                                      "always"
                                    };

          static const int    v[] = {
                                      Choices_SolidResize_No,
                                      Choices_SolidResize_Yes,
                                      Choices_SolidResize_Always
                                    };

          result = save_save_general(file, 3, v, s, choices.solid_resize, 3);
        }
        else if (!strncmp(p, "FullScreen",       len)) result = save_save_yes_no(file, choices.full_screen);
        else if (!strncmp(p, "HScroll",          len))
        {
          static const char * s[] = {
                                      "no",
                                      "yes",
                                      "auto"
                                    };

          static const int    v[] = {
                                      Choices_HScroll_No,
                                      Choices_HScroll_Yes,
                                      Choices_HScroll_Auto
                                    };

          result = save_save_general(file, 3, v, s, choices.h_scroll, 3);
        }
        else if (!strncmp(p, "VScroll",          len))
        {
          static const char * s[] = {
                                      "no",
                                      "yes",
                                      "auto"
                                    };

          static const int    v[] = {
                                      Choices_VScroll_No,
                                      Choices_VScroll_Yes,
                                      Choices_VScroll_Auto
                                    };

          result = save_save_general(file, 3, v, s, choices.v_scroll, 3);
        }

        else if (!strncmp(p, "RefoWait",         len)) result = save_save_yes_no(file, choices.refo_wait);
        else if (!strncmp(p, "RefoHang",         len)) result = save_save_yes_no(file, choices.refo_hang);
        else if (!strncmp(p, "RefoTime",         len)) result = save_save_number(file, choices.refo_time);

        else if (!strncmp(p, "FixedPtr",         len)) result = save_save_yes_no(file, choices.fixed_pointer);
        else if (!strncmp(p, "HighlightLks",     len)) result = save_save_yes_no(file, choices.highlight_links);
        else if (!strncmp(p, "KeyboardCtl",      len)) result = save_save_yes_no(file, choices.keyboard_ctrl);

        else if (!strncmp(p, "Clone",            len)) result = save_save_yes_no(file, choices.clone);
        else if (!strncmp(p, "UseProxy",         len)) result = save_save_yes_no(file, choices.use_proxy);
1677
        else if (!strncmp(p, "ProxyAddress",     len)) result = save_save_string(file, choices.proxy_address);
1678
        else if (!strncmp(p, "StartProxy",       len)) result = save_save_yes_no(file, choices.start_proxy);
1679

1680 1681 1682 1683 1684 1685 1686
        #ifndef SINGLE_USER

          else if (!strncmp(p, "PostIn",         len)) result = save_save_string(file, choices.post_in);
          else if (!strncmp(p, "PostOut",        len)) result = save_save_string(file, choices.post_out);

        #endif

1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
        else
        {
          /* Token we don't understand - output the rest of the line */

          #ifdef TRACE

            if (tl & (1u<<26))
            {
              Printf("save_save_choices: Unrecognised token '");

              for (i = 0; i <= len; i++)
              {
                Printf("%c", p[i]);
              }

              Printf("'\n");
            }

          #endif

          p += len + 1;
          while (*p >= ' ' && result != EOF) result = fputc(*p++, file);
        }

        if (result == EOF) goto save_save_choices_write_error;

        /* Skip to the end of the line */

        while (*p >= ' ') p++;

        /* Output any control characters - we thus preserve   */
        /* CR, LF, CR+LF or whatever was in the original file */

        while (*p < ' ' && p < en)
        {
          if (fputc(*p++, file) == EOF) goto save_save_choices_write_error;
        }
      }
      else
      {
        /* Unrecognised line contents; spit out the whole line */

        #ifdef TRACE

          if (tl & (1u<<26))
          {
            int i = 0;

            Printf("save_save_choices: Unknown line fragment '");

            while (p[i] >= ' ')
            {
              Printf("%c", p[i++]);
            }

            Printf("'\n");
          }

        #endif

        while (*p >= ' ')
        {
          if (fputc(*p++, file) == EOF) goto save_save_choices_write_error;
        }
      }
    }
  }
  while (p < en);

  /* Close the output file */

  fclose(file);

1760 1761
  /* Set the filetype - not essential, just nice to have, so */
  /* let it fail silently.                                   */
1762

1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
  _swix(OS_File,
        _INR(0,2),

        18, /* Set type of named object */
        canonicalised_out,
        FileType_TEXT);

  /* Free the canonicalised path */

  free(canonicalised_out);
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782

  /* Free the old Choices file */

  free(op);

  /* Finished */

  #ifdef TRACE
    if (tl & (1u<<26)) Printf("save_save_choices: Successful\n");
  #endif
1783 1784

  return NULL;
1785 1786 1787 1788 1789 1790

  /* Error condition exits */

save_save_choices_read_error:

  if (!path || !*path) free(out);
1791 1792 1793

  free(op);
  free(canonicalised_out);
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828

  erb.errnum = Utils_Error_Custom_Message;

  StrNCpy0(erb.errmess,
           lookup_token("COChoices:Can't open the Choices file - the preferences cannot be saved.",
                        0,
                        0));

  #ifdef TRACE
    if (tl & (1u<<26)) Printf("save_save_choices: Can't open Choices file, exitting\n");
  #endif

  return &erb;

save_save_choices_write_error:

  erb = *_kernel_last_oserror();

  if (file) fclose(file);

  /* We really want to avoid leaving a broken Choices file, so */
  /* have a last ditch go at dumping the original file down.   */

  _swix(OS_File,
        _INR(0,2) | _INR(4,5),

        10,
        out,
        FileType_TEXT,
        op,
        en);

  /* Free any temporary blocks */

  if (!path || !*path) free(out);
1829 1830 1831

  free(op);
  free(canonicalised_out);
1832 1833 1834 1835 1836 1837

  #ifdef TRACE
    if (tl & (1u<<26)) Printf("save_save_choices: Exitting with error\n");
  #endif

  return &erb;
1838
}