About 15.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 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 522 523 524 525 526 527 528 529 530 531 532 533 534 535 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 562 563 564
/* Copyright 1998 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   : About.c                                */
/*                                                 */
/* Purpose: Compile an HTML 'About' page which     */
/*          describes available Plug-Ins.          */
/*                                                 */
/* Author : Piers Wombwell                         */
/*          This source adapted by A.D.Hodgkinson  */
/*                                                 */
/* History: 30-Jan-98: Adaptation complete (ADH)   */
/***************************************************/

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

#include "kernel.h"
#include "swis.h"

#include "flex.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 "Utils.h"

/* Local statics and definitions */

#define FLAGS_IMAGE (1u<<0)

static struct _abouts
{
  char           * dirname;

  unsigned int     count;
  unsigned int     flags;
  unsigned int     width;
  unsigned int     height;

  struct _abouts * next;

} * abouts = NULL;

/* Static function prototypes */

static void   about_free_abouts    (void);
static char * about_html_fname     (const char * dirname, unsigned int count, unsigned int width, unsigned int height);
static int    about_copy_info_file (char ** output, int output_size, const char * dirname, unsigned int count);

/*************************************************/
/* about_free_abouts()                           */
/*                                               */
/* Free all of the temporary structures used to  */
/* compile the About page.                       */
/*************************************************/

static void about_free_abouts(void)
{
  struct _abouts * next;

  if (abouts)
  {
    while (abouts)
    {
      next = abouts->next;

      free(abouts->dirname);
      free(abouts);

      abouts = next;
    }
  }
}

/*************************************************/
/* about_html_fname()                            */
/*                                               */
/* Creates a URL to point to an About item logo. */
/*                                               */
/* Parameters: Directory to look in;             */
/*                                               */
/*             Number of the file;               */
/*                                               */
/*             Item width;                       */
/*                                               */
/*             Item height (the last two form    */
/*             part of the filename).            */
/*                                               */
/* Returns:    Pointer to the URL in a *global*  */
/*             malloc block - if you need a      */
/*             local copy before calling the     */
/*             function again, you must take it  */
/*             there and then.                   */
/*************************************************/

static char * about_html_fname(const char * dirname, unsigned int count,
                               unsigned int width, unsigned int height)
{
  static char * fname = NULL;
  char        * s;
  int           len;

  if (fname) free(fname);
  fname = NULL;

  if (width == 0 && height == 0)
  {
    len = strlen(dirname) + sizeof("file:/.00");

    fname = malloc(len);
    if (!fname) return "";

    sprintf(fname, "file:/%s.%02d", dirname, count);
  }
  else
  {
    len = strlen(dirname) + sizeof("file:/.0000000000");

    fname = malloc(len);
    if (!fname) return "";

    sprintf(fname, "file:/%s.%02d%04d%04d", dirname, count, width, height);
  }

  if (fname && *fname)
  {
    for (s = fname + sizeof("file:/"); *s != '\0'; s++)
    {
      if (*s == '.')
      *s = '/';
    }
  }

  return fname;
}

/*************************************************/
/* about_copy_info_file                          */
/*                                               */
/* Copy the 'About' info file into the given     */
/* flex block.                                   */
/*                                               */
/* Paramters: Pointer to the flex block;         */
/*                                               */
/*            Size of the block so far;          */
/*                                               */
/*            Name of the directory to look in;  */
/*                                               */
/*            Number of the 'About' file to      */
/*            read (will cope with just 'About'  */
/*            rather than 'AboutXX').            */
/*                                               */
/* Returns:   Size of block after addition of    */
/*            the file.                          */
/*************************************************/

static int about_copy_info_file(char ** output, int output_size,
                                const char * dirname, unsigned int count)
{
  FILE * input;
  char   fname[Limits_OS_Pathname];
  int    length;
  int    old_state;

  /* Write 'AboutXX' as a leafname if it'll fit */

  if (strlen(dirname) + sizeof(".About00") <= sizeof(fname))
  {
    sprintf(fname, "%s.About%02d", dirname, count);
  }
  else return output_size;

  /* Try to open it */

  input = fopen(fname, "r");

  /* If this fails, write just 'About' (we know it'll fit */
  /* if 'AboutXX' did!)                                   */

  if (input == NULL && count == 0)
  {
    sprintf(fname, "%s.About", dirname);
    input = fopen(fname, "r");
  }

  /* If this fails, bail out */

  if (input == NULL) return output_size;

  fseek(input, 0L, SEEK_END);
  length = (int) ftell(input);
  rewind(input);

  /* Try to get the amount of memory the file requires */

  if (flex_extend((void **) output, output_size + length + 1) != 1) /* + 1 for the terminator */
  {
    fclose(input);
    return output_size;
  }

  /* Read the file, zero terminate it in the block, and close the file */

  old_state = flex_set_budge(0);

  fread(*output + output_size, length, 1, input);
  *(*output + output_size + length) = '\0';

  flex_set_budge(old_state);

  fclose(input);

  /* Return the original size plus the added data */

  return output_size + length;
}

/*************************************************/
/* about_build_page()                            */
/*                                               */
/* Builds an 'About' page enumerating Plug-Ins   */
/* to a given flex anchor.                       */
/*                                               */
/* There may already be data in the flex block,  */
/* or not. If the function bails out due to lack */
/* of memory or some other error, the flex block */
/* won't be freed and may have been added to.    */
/*                                               */
/* Parameters: The flex anchor (as a void **).   */
/*************************************************/

_kernel_oserror * about_build_page(void ** block)
{
  _kernel_swi_regs   regs; /* OS_ReadVarVal doesn't work with _swix */

  struct _abouts   * about;

  int                plugin_count = -1;
  int                max_width    = 0;
  int                output_size  = 0;
  int                len;
  int                start;

  if (!block) return NULL;

  regs.r[3] = 0; /* Count */

  do
  {
    plugin_count++;

    regs.r[0] = (int) "Plugin$About_*";

    regs.r[1] = NULL; /* No buffer                  */
    regs.r[2] = -1;   /* Verify whether it exists   */
    regs.r[4] = 0;    /* Returned unexpanded string */

    _kernel_swi(OS_ReadVarVal, &regs, &regs);

    if (regs.r[2] != 0)
    {
      /* Got directory name - let's enumerate About files... */

      char leafname[Limits_OS_Pathname];
      int  nobjs, index = 0;

      while (index != -1)
      {
        /* Read full info on the one 'About[XX]' file in */
        /* the directory.                                */

        if (
             _swix(OS_GBPB,
                   _INR(0,6) | _OUTR(3,4),

                   12,
                   getenv((const char*) regs.r[3]),
                   leafname,
                   1,
                   index,
                   sizeof(leafname),
                   "About*",

                   &nobjs, /* No. objects read so far */
                   &index) /* Where to continue       */
           )
           break;

        /* We may have nothing left to read (index == -1) */
        /* or have read no object this time               */

        if (index == -1) break;
        if (nobjs == 0)  continue;

        /* Build an 'about' structure for the entry */

        about = malloc(sizeof(struct _abouts));

        if (!about)
        {
          about_free_abouts();

          return make_no_cont_memory_error(12);
        }

        about->dirname = malloc(~regs.r[2] + 1);

        if (!about->dirname)
        {
          about_free_abouts();

          return make_no_cont_memory_error(12);
        }

        /* Read the expanded path into the buffer */

        _kernel_getenv((const char *) regs.r[3],
                       about->dirname,
                       (unsigned int) ~regs.r[2] + 1);

        /* We may have an 'About' file or an 'AboutXX' file */

        if (!strcmp(leafname + 24, "About")) about->count = 0;
        else                                 about->count = atoi(leafname + 24 + sizeof("About"));

        about->flags = 0;
        about->next  = abouts;
        abouts       = about;
      }
    }
  }
  while (regs.r[2] != 0);

  /* Now we've got all the 'About' files, we can look for the logos */

  for (about = abouts; about != NULL; about = about->next)
  {
    char leafname[Limits_OS_Pathname];
    int  nobjs, index = 0;

    while (index != -1)
    {
      sprintf(leafname, "%02d*", about->count);

      /* Similar to the above code, only the files are */
      /* identified as 'XXWWWWHHHH' (Ws = width, Hs =  */
      /* height).                                      */

      if (
           _swix(OS_GBPB,
                 _INR(0,6) | _OUTR(3,4),
                 12,

                 about->dirname,
                 leafname,
                 1,
                 index,
                 sizeof(leafname),
                 leafname,

                 &nobjs,
                 &index)
         )
         break;

      if (index == -1) break;
      if (nobjs == 0)  continue;

      /* Check if leafname is just two digits long, and therefore */
      /* has no width and height                                  */

      if (strlen(leafname + 24) == 2)
      {
        about->width = about->height = 0;
        about->flags = FLAGS_IMAGE;

        break;
      }

      /* Break up leafname into width & height */

      strncpy(leafname, leafname + 24 + 2, 4);
      leafname[5] = '\0';
      about->width = atoi(leafname);

      strncpy(leafname, leafname + 24 + 6, 4);
      leafname[5] = '\0';
      about->height = atoi(leafname);

      if (about->width > max_width)
      max_width = about->width;

      /* We now have the sprite widths. Mark About object as good */

      about->flags = FLAGS_IMAGE;
    }
  }

  /* Build the HTML - first the header */

  output_size = strlen(lookup_token("pabouthdr",1,0)) + 1;

  {
    int success;

    if (!*block) start   = 0,
                 success = flex_alloc (block, output_size);

    else         start   = flex_size  (block),
                 success = flex_extend(block, start + output_size);

    if (!success) goto panic;

    sprintf(((char *) *block) + start, tokens);

    output_size += start - 1; /* (Next item should overwrite the terminator) */
  }

  /* Next the browser's own entry. We need to substitute one a Messages */
  /* file entry into another Messages file entry, so some juggling      */
  /* about of buffers is needed - we'll store the string to substitute  */
  /* at block + output_size, compile the final string above this, then  */
  /* move the final string down into place.                             */

  len  = strlen(lookup_token("paboutbrw",1,0)) + 1;
  len += strlen(lookup_token("Version",  1,0)) * 2 + 1;

  if (!flex_extend(block, output_size + len)) goto panic;

  sprintf(((char *) *block) + output_size, tokens);

  len = strlen(tokens) + 1;
  sprintf(((char *) *block) + output_size + len,
          lookup_token("paboutbrw",1,0),
          ((char *) *block) + output_size);

  memmove(((char *) *block) + output_size,
          ((char *) *block) + output_size + len,
          strlen(((char *) *block) + output_size + len) + 1);

  output_size = start + strlen(((char *) *block) + start);

  /* Now the individual items */

  for (about = abouts; about != NULL; about = about->next)
  {
    if (!(about->flags & FLAGS_IMAGE))
    {
      len = strlen(lookup_token("paboutpl1",1,0)) + 1;

      if (!flex_extend(block, output_size + len)) goto panic;

      sprintf(((char *) *block) + output_size,
              tokens);

      output_size = start + strlen(((char *) *block) + start);
    }
    else if (about->width == 0 && about->height == 0)
    {
      len = strlen(about_html_fname(about->dirname, about->count, 0, 0)) +
            strlen(lookup_token("paboutpl2",1,0)) + 1;

      if (!flex_extend(block, output_size + len)) goto panic;

      sprintf(((char *) *block) + output_size,
              tokens,
              about_html_fname(about->dirname, about->count, 0, 0));

      output_size = start + strlen(((char *) *block) + start);
    }
    else
    {
      len = strlen(about_html_fname(about->dirname,
                                    about->count,
                                    about->width,
                                    about->height)) +
            4                                       + /* Width and height are 4 characters or less as numbers, */
            4                                       + /* due to the way the filenames encode them.             */
            strlen(lookup_token("paboutpl3",1,0)) + 1;

      if (!flex_extend(block, output_size + len)) goto panic;

      sprintf(((char *) *block) + output_size,
              tokens,
              about_html_fname(about->dirname,
                               about->count,
                               about->width,
                               about->height),
              about->width,
              about->height);

      output_size = start + strlen(((char *) *block) + start);
    }

    /* Copy in HTML About file */

    output_size = about_copy_info_file((char **) block,
                                       output_size,
                                       about->dirname,
                                       about->count);

    /* Write the item footer */

    len = strlen(lookup_token("paboutift",1,0)) + 1;

    if (!flex_extend(block, output_size + len)) goto panic;

    sprintf(((char *) *block) + output_size, tokens);

    output_size = start + strlen(((char *) *block) + start);
  }

  /* Write the page footer; then we're finished */

  len = strlen(lookup_token("paboutpft",1,0)) + 1;

  if (!flex_extend(block, output_size + len)) goto panic;

  sprintf(((char *) *block) + output_size, tokens);

  output_size = start + strlen(((char *) *block) + start);

  /* Ensure the block is clipped to exactly the right size (we're */
  /* building HTML, so it shouldn't have a zero terminator!)      */

  if (!flex_extend(block, output_size)) goto panic;

  /* Clean up and exit without error */

  about_free_abouts();

  return NULL;

panic:

  /* Clean up and exit with an error */

  about_free_abouts();

  /* This was *almost* certainly the error...! */

  return make_no_cont_memory_error(12);
}