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

main()
{
    char in[260], out[260], *inptr, *outptr;

    /* Make it identifyable as an AutoDoc */
    fputs("TABLE OF CONTENTS\n", stdout);

    /* Read the lines one by one. Compiler takes care of buffering. */
    while (fgets(in, 255, stdin))
    {
        inptr = in;
        outptr = out;

        if (strnicmp("@node", inptr, 5) == 0)
        {
            /* Transform a node into an AutoDoc title */
            char name[80], *nameptr = name, quote;

            inptr += 5;

            while (*++inptr == ' ')
                ;
            if (*inptr == '\"')
                inptr++, quote = 1;
            else
                quote = 0;

            /* Copy name until quotation mark or space */
            while ((*inptr != (quote ? '\"' : ' ')) && (*inptr != '\n'))
                if (inptr[0] == '(' && inptr[1] == ')')
                    inptr += 2;
                else
                    *nameptr++ = *inptr++;

            *nameptr = 0;

            fprintf(stdout, "%-37s%37s\n\n", name, name);
        } else if (strnicmp("@endnode", inptr, 8) == 0)
        {
            /* End a node; an AutoDoc entry is ended by a form feed */
            fputs("\f", stdout);
        } else
        {
            /* Just a normal line */

            while (*inptr)
            {
                if (inptr[0] == '@' && inptr[1] == '{' && inptr[2] == '\"')
                {
                    inptr += 3;

                    /* Copy link text */
                    while (*inptr != '\"')
                        *outptr++ = *inptr++;
                    /* Skip rest (actual link) */
                    while (*inptr++ != '}')
                        ;
                } else
                    *outptr++ = *inptr++; /* Normal text - just copy */
            }

            *outptr = 0;
            fputs(out, stdout);
        }
    }

    return 0;
}

