/* arclynx - simple version of Lynx for Acorn machines
 * (c) 1996 Andrew Wood
 *
 * Still to do:
 *   * faster parsing of documents
 *   * proper handling of heading elements
 *   * forms
 *   * proper handling of ALIGN in IMG elements
 *   * tables
 *   * HTTP, FTP, etc protocols
 *
 * Anyone want to give these a go?
 *
 * I really only wrote this to get a simple idea of how documents I start
 * writing at home, like the Vivaldi manuals, will look in HTML format,
 * and to check that my HTML coding is at least half right. ArcWeb et al
 * are big, slow, clunky and crash occasionally; they are oversized for
 * what I wanted to do, plus I wanted a text-only viewer too for backwards
 * HTML compatibility. Anyway, here it is - even if it is pretty slow and
 * primitive.
 *
 * By the way, a file supposedly ending in ".html" ("/html" Acorn-style)
 * will still be found if it has no suffix. See "c.a_fetch" for the
 * relevant routine.
 *
 * Routines contained in this file (arclynx.c):
 *   main()                  - last modified 96/07/16
 *   lynx_commandline_help() - last modified 96/07/18
 */

#include "arclynx.h"
#include <string.h>


#define LYNX_VERSION        "0.20"                 /* current version number */
#define LYNX_LAST_MODIFIED  "23 July 1996"      /* date source last modified */


void lynx_commandline_help(void);/* display command line help (in arclynx.c) */


/******************************************************************************
 * main() - initialise status structure, read command line parameters, call all
 *          necessary subroutines
 *        - 1 is returned if an error occurred; 0 is returned if not
 */
void main(int argc,char *argv[]) {
 int i;                                         /* general iteration counter */
 Status status;               /* structure containing all status information */
 lynx_initialise(&status);                              /* initialise status */
 if (status.finished) {lynx_finalise(&status); exit(1);}    /* quit on error */
 for(i=1; i<argc; i++) {                 /* for each command line parameter: */
  switch(argv[i][0]) {                              /* test first character: */
   case '-':                                                       /* option */
    switch(argv[i][1]) {                                     /* what option? */
     case '?':                                                       /* help */
     case 'h':                                                       /**/
     case 'H':                                                       /**/
      lynx_commandline_help(); status.finished=1; break;     /* display help */
     default:                                              /* unknown option */
      fprintf(stderr,"Unknown option '%s'\n",argv[i]+1);           /* report */
      status.finished=1;
      break;
    }
    break;
   default:                                                     /* parameter */
    strcpy(status.build.url,argv[i]); break;           /* copy URL to memory */
  }
 }
 if (status.finished) {lynx_finalise(&status); exit(1);}    /* quit on error */
 awterm_initialise();                             /* initialise terminal I/O */
 if (status.finished) {lynx_finalise(&status); exit(1);}    /* quit on error */
 if (strstr(status.build.url,"::")!=NULL) {      /* if URL is an Acorn path: */
  for(i=0; i<strlen(status.build.url); i++) {     /* swap '.' and '/' around */
   switch(status.build.url[i]) {
    case '.':status.build.url[i]='/'; break;
    case '/':status.build.url[i]='.'; break;
    default:break;
   }
  }
 }
 lynx_fetch(&status,status.build.url);             /* fetch initial document */
 lynx_main(&status);                                         /* main program */
 awterm_finalise();                                 /* finalise terminal I/O */
 lynx_finalise(&status);                      /* finalise (free memory, etc) */
 exit(status.finished);                        /* quit, returning error flag */
}


/******************************************************************************
 * lynx_commandline_help() - show command line help
 */
void lynx_commandline_help(void) {
 printf("Usage: arclynx [URL] [-options]\n\n");
 printf("URL may be a local filename or a full URL. Options are:\n");
 printf("  -h, -?: display this help text\n");
 printf("\n");
 printf("Browser commands:\n");
 printf(" Up, Down - move up and down links\n");
 printf(" Left - move to previous document\n");
 printf(" Right, Return - follow link\n");
 printf(" -, Space - move up/down a page\n");
 printf(" b, 0 - move to top of document\n");
 printf(" d - output debugging file \"debug\" in CSD\n");
 printf(" g - go to a specific URL\n");
 printf(" p - output text file version of document\n");
 printf(" q - quit browser\n");
 printf(" z - move to bottom of document\n");
 printf("\n");
 printf("NB This version does not support remote file transfer, so it is ");
 printf("only of\n   use as an offline browser.\n");
 printf("   Tables, forms, frames and Java are not supported either.\n");
 printf("\n");
 printf("Future improvements may include better document history ");
 printf("management, and\npossibly support for tables and forms...\n");
 printf("\n");
 printf("arclynx version %s (%s) ",LYNX_VERSION,LYNX_LAST_MODIFIED);
 printf("by Andrew Wood <woody@ps.cus.umist.ac.uk>\n");
}
