#module NEWS "V4.02"
/*----------------------------------------------------------------------
        N E W S . C

 Author:
     Geoff Huston
       Computer Services Centre
       Australian National University

 Functionality:
        Initialization and main control loop, and shutdown procedures
        for NEWS.

 Version:
    V1.0    17-Jul-1986
    V2.0    31-Jan-1987     GIH
    V3.0     6-Apr-1987     GIH     Version 3 Release
    V4.0    30-Oct-1987     GIH     NEWS Version 4.0 Release
    V4.02   17-Nov-1987     GIH     Changes to make DIR/NEW directory
                                    more consistent in its behaviour
----------------------------------------------------------------------*/

#include "newsinclude.h"
#include "newsdefine.h"

#define extrnl
#define iev(v)   = v
#define iv(v)   v
#include "newsvariables.h"

$DESCRIPTOR(command,response);

#define varying(MAXSTRLEN) struct {unsigned short curlen; char body[MAXSTRLEN];}
#define $VARDESCRIPTOR(name,str) struct dsc$descriptor_vsa name = { sizeof(str)-3,DSC$K_DTYPE_VT,DSC$K_CLASS_VS,&str}

globalref int newscmd;


/*----------------------------------------------------------------------
 *  m a i n
 *
 *  MAIN PROGRAM
 *----------------------------------------------------------------------
 */

main(argc,argv)
int argc;
char *argv[];
{
    varying(132) mcommand,temp;
    $VARDESCRIPTOR(comdsc,mcommand);
    $VARDESCRIPTOR(tempdsc,temp);
    $DESCRIPTOR(newsdsc,"INVOKENEWS ");

    int cmd_status,
        one_pass = 0,
        cli_catch();


    nosysprv();                         /* turn off sysprv */
                                        /* set up run strings */
    site_get_env();

                                        /* set up SMG area */
    _c$cks(smg$create_pasteboard(&pid,c$dsc("TT"),&devrow,&devcol,c$rfi(0)));
    _c$cks(smg$create_virtual_keyboard(&kid,0,0,0));
    _c$cks(smg$create_key_table(&keytab));
    create_keydefs();
    smg$load_key_defs(&keytab,c$dsc(News_ini),0,c$rfi(1));
    VAXC$ESTABLISH(cli_catch);

                                        /* open newsfiles */
    openfiles();
                                        /* select the first newsgroup with new items */
    set_level(1);

    lib$get_foreign(&tempdsc);
    str$concat(&comdsc,&newsdsc,&tempdsc);

    cli$dcl_parse(&comdsc,&newscmd);

    if (cli$present(c$dsc("SCREEN")) & 1) init_screen();

    selnew(1);

    if (1 & cli$get_value(c$dsc("COMMAND"),&command,&response_length))
    {
        one_pass = 1;
        status = 1;
    }

                                        /* MAIN execution loop */
    for (;;)
    {
                                        /* jump return to this point from inner levels */
        cmd_status = setjmp(env);

                                        /* catch command line command */
        if (one_pass)
        {
            if (one_pass++ > 1) break;
        }

                                        /* clear off the call stack temporaries */
        c$cks(1);

                                        /* close any open files */
        if (fp_open)
        {
            fclose(fp);
            fp_open = 0;
        }

                                        /* cmd_status indicates whether a command has already been read - if so
                                           skip the SMG$READ */
        if (screen_active)
        {
            smg$erase_display(&trailer_vd,c$rfi(1),c$rfi(6),c$rfi(2),c$rfi(80));
            smg$set_cursor_abs(&trailer_vd,c$rfi(1),c$rfi(1));
        }
        if ((cmd_status != 2) && (!one_pass))
        {
            if (screen_active)
            {
                if ((news_context == 1) && (cur_dir_type == DIR_NEW)) dirnew_check();
                smg$end_pasteboard_update(&pid);
            }
            cmd_status = smg$read_composed_line(&kid,&keytab,&command,c$dsc("NEWS> "),&response_length,((screen_active) ? (&trailer_vd) : (0)));
            if (screen_active) smg$begin_pasteboard_update(&pid);
        }
        else cmd_status = status;

        old_context = news_context;
        if (news_context == 3) set_level(2);

                                        /* EXIT on EOF */
        if (cmd_status == SMG$_EOF) break;
        response[response_length] = '\0';
        if (do_parse(response)) break;
    }
    closefiles();
}
