/**************************************************************
 *
 *      GetAutoDoc.c - Gets an autodoc given the function name
 *
 *      Copyright (c) 1989, Peter Cherna
 *
 *      Created:  March 26, 1989
 *      Modified: August 23, 1989       Release 1.2:  August 29, 1989
 *
 *      Auto: cc -q -o RAM:<file>.o <path><file>
 *      Auto: ln RAM:<file>.o -lc -o <path><file>
 *
 **************************************************************/

#include <functions.h>
#include <stdio.h>
#include <ctype.h>
#ifndef EXEC_MEMORY_H
#include <exec/memory.h>
#endif

extern char *index();

#define BUFFERSIZE 200
#define NAMESIZE 60
#define FORMFEED 12

main(argc,argv)

    int argc;
    char *argv[];

    {
    FILE *file;
    char *buffer, *filename, *ch1, *ch2;
    LONG offset;
    int error;
    int done;

    error = 0;

    if (argc < 2)
        {
        /* insufficient parameters */
        puts("- GetAutoDoc Copyright (c) 1989, Peter Cherna");
        fputs("    Usage:  ",stdout);
        fputs(argv[0],stdout);
        puts(" function");
        puts("Displays the AutoDoc for the requested function.");
        error = 1;
        }
    else
        {
        filename = AllocMem((LONG) NAMESIZE, MEMF_CLEAR);
        buffer = AllocMem((LONG) BUFFERSIZE, MEMF_CLEAR);
        if ((!filename) || (!buffer))
            {
            fputs("- ",stdout);
            fputs(argv[0],stdout);
            puts(":  Unable to allocate buffer.");
            error = 20;
            }
        else
            {
            sprintf(filename,"AutoDocs:Index/Index%c",toupper(*argv[1]));
            file = fopen(filename,"r");
            if (!file)
                {
                /*  If none of the AutoDocs are on line, this could
                    be a serious error.  However, we have to assume that
                    there are no autodocs for this particular letter,
                    (for example, as of 1.2, there is no IndexZ).  Thus
                    the message returned is "<function> not found." */
                fputs("- ",stdout);
                fputs(argv[1],stdout);
                puts(" not found.");
                error = 5;
                }
            else
                {
                done = 0;
                while ((!feof(file)) && (!done))
                    {
                    if (!fgets(buffer,BUFFERSIZE,file))
                        {
                        if (!feof(file))
                            {
                            fputs("- ",stdout);
                            fputs(argv[0],stdout);
                            puts(":  Error reading index file.");
                            done = TRUE;
                            error = 20;
                            }
                        }
                    else
                        {
                        ch1 = buffer;
                        ch2 = argv[1];
                        while ((*ch1 != ';') && (toupper(*ch1) == toupper(*ch2)))
                            {
                            ch1++;
                            ch2++;
                            }
                        if ((*ch1 == ';') && (*ch2 == '\0'))
                            done = TRUE;
                        }                       
                    }
                if (!done)
                    {
                    fputs("- ",stdout);
                    fputs(argv[1],stdout);
                    puts(" not found.");
                    error = 5;
                    }
                fclose(file);
                if (!error)
                    {
                    /*  Skip function name: */
                    ch1 = index(buffer,';');
                    ch1++;
                    /*  Skip additional info (not used): */
                    ch1 = index(ch1,';');
                    ch1++;
                    ch1++;

                    ch2 = filename;
                    while ((*ch2 = *ch1) != ' ')
                        {
                        ch1++;
                        ch2++;
                        }

                    /*  Terminate the file name: */
                    *ch2++ = '\0';
                    ch1++;

                    file = fopen(filename,"r");
                    if (!file)
                        {
                        fputs("- ",stdout);
                        fputs(argv[0],stdout);
                        puts(":  AutoDoc file not found.");
                        error = 20;
                        }
                    else
                        {
                        offset = 0;
                        while ((*ch1 >= '0') && (*ch1 <= '9'))
                            offset = 10 * offset + *ch1++ - '0';
                        fseek(file,offset,0);

                        while ((!feof(file)) && (*buffer != FORMFEED) && (!error))
                            {
                            if ((!fgets(buffer,BUFFERSIZE,file)) && (!feof(file)))
                                {
                                fputs("- ",stdout);
                                fputs(argv[0],stdout);
                                puts(":  Error reading AutoDoc file.");
                                error = 20;
                                }
                            else if (*buffer != FORMFEED)
                                fputs(buffer,stdout);
                            }
                        fclose(file);
                        }
                    }
                }
            if (buffer)
                FreeMem(buffer, (LONG) BUFFERSIZE);
            if (filename)
                FreeMem(filename, (LONG) NAMESIZE);
            }
        }
    exit(error);
    }
