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

#include <exec/memory.h>
#include <exec/types.h>

#define MAXLEN 256
#define MEMTYPE MEMF_PUBLIC | MEMF_CLEAR

static char strbuf[MAXLEN];         /* string buffer */
extern int debug;                   /* debug flag */

/*
 * int *findstring(lineptr, nlines, findstr, flag_whole, flag_case, flag_code)
 *
 * return TRUE if findstr found in array of lines linptr
 * flag_whole: look for whole word
 * flag_case:  look for matching case
 * flag_code:  search includes HTML code
 *
*/
int findstring(lineptr, nlines, findstr, flag_whole, flag_case, flag_code)

char *lineptr[];
int nlines;
char *findstr;
int flag_whole;
int flag_case;
int flag_code;
{
    extern int strindex(), isword(), iscode();

    int flag_found = 0,         /* flag whether string found */
        code_status = 0,        /* flag whether HTML code or text */
        pos=0,                  /* position marker */
        i;                      /* status of a given line */

    while (--nlines >= 0) {

        if ((pos = strindex(*lineptr, findstr, flag_case)) > -1) {
            if (debug) printf ("Found it at pos=%d!\n",pos);

            /* OK, there is a match, but how good? */
            flag_found = 1;

            /* check for whole word matches, if required */
            if (flag_whole)
                if (!(isword(*lineptr, findstr, pos))) flag_found = 0;

            /* check whether HTML code, if required */
            if (!flag_code) {

                /* check line for <> markers to left of pos */
                i = iscode(*lineptr, pos);

                /* if next left delimiter is < we're in code */
                if (i == 1) flag_found = 0;
                    else
                        /* if no delimiter found, but code_status TRUE, we're in code */
                        if ((i == 0) && (code_status == 1)) flag_found = 0;
            }
        }

        /* no possible matches on this line.. */

        /* check whole line for <> markers */
        i = iscode(*lineptr, strlen(*lineptr));

        /* if last delimiter was < next line will be code */
        if (i == 1) code_status = 1;

            /* if last delimiter was > next line is not code */
            else if (i == -1) code_status = 0;
                /* else no change to code status */

        /* advance to next line */
        *lineptr++;
    }

    return(flag_found);
}

int findtitle(filename, lineptr, nlines, fp)

char *filename,
     *lineptr[];
int nlines;
FILE *fp;

{
    extern int strindex(), strcmptext();
    extern char *midstr(), *solidstr();
    extern void add_url();

    int pos = 0, startpos = 0, flag_found = 0;
    char titlestr[MAXLEN];

    while ((--nlines > 0) && (*lineptr)) {

        if (debug) printf("found flag = %d\n",flag_found);

        /* check for <TITLE> tag */
        if ((pos = strindex(*lineptr, "<TITLE>", 0)) > -1) {
            if (debug) printf("Got <TITLE>!\n");
            flag_found = 1;
            startpos = pos + 7;

            /* anything else on this line? */
            if (startpos != strlen(*lineptr)) {

                 /* yes there is, get it */
                 if (debug) printf("remainder was %s\n",midstr(*lineptr, startpos, strlen(*lineptr)));

                 /* check for </TITLE> tag */
                 if ((pos = strindex(*lineptr, "</TITLE>", 0)) > -1) {

                     nlines = 0;
                 } else pos = strlen(*lineptr);

                 if (debug) printf("reading title..%s\n",midstr(*lineptr,startpos,pos));

                 strcpy(titlestr,(char *)midstr(*lineptr,startpos,pos));

            } /* else nothing */

        }

        /* proceed through subsequent line(s) */
        *lineptr++;
        startpos = 0;

        if (flag_found && nlines) {

            /* check for </TITLE> tag */
            if ((pos = strindex(*lineptr, "</TITLE>", 0)) > -1) {
                nlines=0;
                startpos = pos + 8;

                /* anthing preceeding the tag */
                if (strcmptext("</TITLE>",*lineptr))
                    strcpy(titlestr,(char *)midstr(*lineptr,startpos,pos));
                else nlines = 0;

            } else strcpy(titlestr,*lineptr);

        }
    }

    if (flag_found) {
        if (debug) printf("Title is '%s'\n",(char *)solidstr(titlestr));

        /* add title to report file */
        add_url(fp, filename, solidstr(titlestr));
    }

    return(flag_found);
}

/*
 * int strcmptext(s,t)
 *
 * return <0 if s<t, 0 if s==t, >0 if s>t
 * ignoring whitespace in t
 *
*/

int strcmptext(s, t)

char *s, *t;

{
    int i = 0, j = 0;

    while(s[i] != '\0')
       if (!(s[i] == t[j])) {
           if ((t[j] == ' ')||(t[j] == '\t')) j++; else return(s[i]-t[j]);
       } else {
           i++; j++;
       }
    return(0);
}

/*
 * char *midstr(s,startpos,endpos)
 *
 * return a substring of s from position startpos to endpos
 * use a string buffer to avoid hacking up arguments!
 *
*/
char *midstr(s, startp, endp)

char *s;
int startp, endp;

{
    extern char strbuf[MAXLEN];
    int i=0, pos=0;

    pos = startp;

    while ((strbuf[i++] = s[pos++]) != '\0' && pos<endp);

    strbuf[i] = '\0';

    return(strbuf);

}

/*
 * char *solidstr(s)
 *
 * return a substring of s without leading or trailing whitespace
 * use a string buffer to avoid hacking up arguements!
 *
*/
char *solidstr(s)

char *s;
{
    extern char strbuf[MAXLEN];
    int i=0, j=0;

    /* skip leading whitespace */
    while (isspace(s[j])) j++;

    /* copy remainder of string */
    while ((strbuf[i] = s[j++]) != '\0') i++;

    /* backtrack to confirm end of string */
    while (isspace(strbuf[i])) strbuf[i--] = '\0';

    return(strbuf);
}

/*
 * int strindex(s, t, flag)
 *
 * return position of string s in t or -1 if not found
 * if flag is TRUE match case
 *
*/
int strindex(s, t, flag)

char *s, *t;
int flag;
{
    int i, j, k;

    for (i = 0; s[i] != '\0'; i++) {
        if (flag)
            for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++);
        else
            for (j=i, k=0; t[k]!='\0' && tolower(s[j])==tolower(t[k]); j++, k++);
        if (t[k] == '\0') return(i);
    }
    return(-1);
}

/*
 * int iscode(s,pos)
 *
 * Determines whether a line is HTML code or otherwise.
 * Search for < or > character within string s, searching left from s[pos]
 * Returns: 1 if '<' found
 *          -1 if '>' found
 *          0 if no "<>" markers found.
*/
int iscode(s, pos)

char *s;
int pos;
{
    int i;

    for (i = pos; i >= 0; i--) {
        if (s[i] == '<') return(1);
        if (s[i] == '>') return(-1);
    }

    /* no code delimiters found */
    return(0);
}

/*
 * int isword(s,t)
 *
 * returns TRUE if string t was a complete word in s at position pos
*/
int isword(s, t, pos)

char *s, *t;
int pos;
{
    if (pos > 0)
        if (isalnum(s[pos-1])) return(0);

    if (isalnum(s[pos + strlen(t)])) return(0);

    return(1);
}

/*
 * int readlines(lineptr, maxlines, fp)
 *
 * read file fp with maximum lines maxlines into array lineptr
 * return number of lines
*/
int readlines(lineptr, maxlines, fp)

char *lineptr[];
int maxlines;
FILE *fp;

{
    extern struct Remember *RememberLinesPtr;  /* Lines memory area */
    int len,                    /* length of current line */
        nlines;                 /* number of lines found */
    char line[MAXLEN];          /* storage for each line */

    UBYTE *AllocRemember();

    nlines=0;       /* reset lines counter */

    while((fgets(line,MAXLEN,fp)) != NULL)

        if (nlines >= maxlines)
            return(-1);
        else {

            len = strlen(line);

            if (lineptr[nlines] = AllocRemember(&RememberLinesPtr, (ULONG)len, MEMTYPE)) {

                line[len-1] = '\0';
                strcpy(lineptr[nlines++],line);

            } else {

                printf("Out of Memory in readlines!\n");
                return(-1);
            }
        }

    return(nlines);
}
/*
 * void writelines(lineptr, nlines)
 *
 * print nlines lines of array lineptr
*/
void writelines(lineptr, nlines)

char *lineptr[];
int nlines;
{
    while (--nlines >= 0)
        printf("%s\n", *lineptr++);
}


