/* arclynx - simple version of Lynx for Acorn machines
 * (c) 1996 Andrew Wood
 *
 * Routines contained in this file (a_fetch.c):
 *   lynx_fetch()   - last modified 96/07/18
 *   lynx_method()  - last modified 96/07/16
 *   lynx_arcpath() - last modified 96/07/17
 */

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


int lynx_method(char *);            /* return fetching method (in a_fetch.c) */
void lynx_arcpath(char *);      /* convert Unix path to Acorn (in a_fetch.c) */


/******************************************************************************
 * lynx_fetch(status,url) - fetch file 'url'; if file, do all download options;
 *                          if document, call parser and set 'changed' flag
 *
 * NB this routine uses status->temp
 */
void lynx_fetch(Status *status,char *url) {
 static int method;                                          /* fetch method */
 static char temp[MAX_FILENAME_LENGTH];                    /* temp. filename */
 static int i,j;                               /* general iteration counters */
 static char *p;                                        /* temporary pointer */
 if (url[0]==0) return;                      /* return immediately if no URL */
 method=lynx_method(url);                    /* work out which method needed */
 switch(method) {
  case '#':                                                 /* internal jump */
   status->display.current_link=0; status->display.screen_top=0;
   if (strchr(status->build.url,'#')!=NULL) {  /* remove # from current URL: */
    for(i=0; i<strlen(status->build.url); i++) {
     if (status->build.url[i]=='#') status->build.url[i]=0;
    }
   }
   while (url[0]=='#') {url++;}  /* remove leading #'s from given jump point */
   for(i=0; i<status->build.anchor_names; i++) {  /* search for anchor name: */
    if (status->build.anchor_name[i]!=NULL) {
     if (strcmp(status->build.anchor_name[i],url)==0)
      status->display.current_link=i;
    }
   }
   if (status->display.current_link>0) {               /* if name was valid: */
    status->display.changed=1;
    strcat(status->build.url,"#"); strcat(status->build.url,url); /* new URL */
    for(i=0; i<status->build.lines; i++) {   /* find line containing anchor: */
     j=0; while(status->build.line[i][j]!=0) {
      switch(status->build.line[i][j]) {
       case LYNX_A_HREF_CODE:j++; j++; break;
       case LYNX_SPACES_CODE:j++; break;
       case LYNX_A_NAME_CODE:
        j++;
        if ((status->build.line[i][j]+256*status->build.line[i][j+1])
            == status->display.current_link) {
         status->display.screen_top=i; i=status->build.lines;
        }
        j++;
       default:break;
      }
      j++;
     }
    }
   }
   break;
  case LYNX_METHOD_FILE:
   strcpy(temp,url);                                             /* copy URL */
   lynx_arcpath(temp);                            /* convert to Acorn format */
   status->build.fptr=fopen(temp,"r");                          /* open file */
   if (status->build.fptr==NULL) {                                /* failed? */
    p=strrchr(temp,'/');
    if (p!=NULL) {
     p[0]=0; status->build.fptr=fopen(temp,"r");   /* try again without /??? */
    }
   }
   if (status->build.fptr==NULL) {                                /* failed? */
    sprintf(status->temp,"Failed to open file '%s'",temp);   /* yes - report */
    awterm_status(status->temp); awterm_pause(); awterm_status("");
   } else {
    if (strcmp(temp,status->build.filename)!=0) {  /* if filename different: */
     strcpy(status->build.filename,temp);                    /* set filename */
     strcpy(status->build.url,url);                               /* set URL */
     lynx_parse(status);                                   /* parse document */
     status->display.changed=1;                          /* document changed */
     status->build.temporary=0;                      /* not a temporary file */
     if (status->build.fptr!=NULL) fclose(status->build.fptr); /* close file */
     status->build.fptr=NULL;
     status->display.screen_top=0;
     status->display.current_link=0;
    }
    if (strchr(url,'#')!=NULL) {             /* if internal jump too, do it: */
     strcpy(status->temp,strchr(url,'#'));
     lynx_fetch(status,status->temp);
    }
   }
   break;
  case LYNX_METHOD_FTP:
  case LYNX_METHOD_HTTP:
  case LYNX_METHOD_NEWS:
  case LYNX_METHOD_TELNET:
  case LYNX_METHOD_MAILTO:
   awterm_status("Sorry, that fetch method is not yet supported");
   awterm_pause(); awterm_status("");
   break;
  default:                                                /* unknown method: */
   sprintf(status->temp,"Unknown fetch method for URL '%s'",url);
   awterm_status(status->temp);
   awterm_pause(); awterm_status("");
   break;
 }
}


/******************************************************************************
 * lynx_method(url) - return code for which fetch method should be used
 */
int lynx_method(char *url) {
 static char temp[16];                     /* temporary copy of start of URL */
 if (url[0]=='#') return('#');                              /* internal jump */
 strncpy(temp,url,15); temp[15]=0;                 /* copy it; terminate end */
 string_lowercase(temp);                       /* convert copy to lower case */
 if (strncmp(temp,"ftp:",4)==0) return(LYNX_METHOD_FTP);             /* ftp? */
 if (strncmp(temp,"http:",5)==0) return(LYNX_METHOD_HTTP);          /* http? */
 if (strncmp(temp,"news:",5)==0) return(LYNX_METHOD_NEWS);          /* news? */
 if (strncmp(temp,"telnet:",7)==0) return(LYNX_METHOD_TELNET);    /* telnet? */
 if (strncmp(temp,"mailto:",7)==0) return(LYNX_METHOD_MAILTO);    /* mailto? */
 return(LYNX_METHOD_FILE);                            /* default: local file */
}


/******************************************************************************
 * lynx_arcpath(url) - convert Unix-style file path 'url' to Acorn style
 */
void lynx_arcpath(char *url) {
 static char temp[MAX_URL_LENGTH];                  /* temporary copy of URL */
 static int i,j;                                       /* iteration counters */
 strcpy(temp,url);                                       /* make copy of URL */
 for(i=0; i<strlen(temp); i++) {            /* convert separator characters: */
  switch(temp[i]) {
   case '.':temp[i]='/'; break;
   case '/':temp[i]='.'; break;
   case '#':temp[i]=0; break;
   default:break;
  }
 }
 j=0;
 for(i=0; i<strlen(temp); i++) {          /* convert ./ and ../ directories: */
  if (temp[i]!='/') {
   url[j]=temp[i]; j++;
  } else {
   if ((temp[i+1]=='/') & (temp[i+2]=='.')) {                         /* ../ */
    url[j]='^'; j++; url[j]='.'; j++; i+=2;
   } else {
    if (temp[i+1]=='.') {                                              /* ./ */
     i++;
    } else {
     url[j]=temp[i]; j++;
    }
   }
  }
 }
 if (j==0) {url[j]='@'; j++; url[j]='.'; j++;}
 url[j]=0;
 if (url[j-1]=='.') strcat(url,"index/html");    /* if directory, read index */
}
