Metropoli BBS
VIEWER: copen.c MODE: TEXT (ASCII)
/*
Copyright (C) Magna Carta Software, Inc. 1990, 1991.  All Rights Reserved
C COMMUNICATIONS TOOLKIT
COPEN.C -- Device-independent port open/close routines;
*/

#include <comm.h>
#if !defined(__NDPC__)
    #include <conio.h>
#endif
#include <stdlib.h>


/*
GLOBAL_COM_ERROR -- Global error variable.
Valid values:
     0 -- no error;
    -1 -- no COMM_PORT in this member of the array;
    -2 -- attempt to open an already open port in com_open();
*/
short global_com_error = 0;
WORD  portindex     = 0;
short use_ctrl_brk  = TRUE;
short checkdcd      = FALSE;    /* file xfer routines check for loss of carrier */



/*
C_OPEN -- Open a communications port in a way analogous to a file.
Return value:
    0                   Success;
    PORT_ALREADY_OPEN   Error;
    NO_AVAILABLE_PORT   Error;
*/
short c_open_(COMM_PORT *p)
{
    short i;

    /* CHECK IF PORT IS ALREADY OPEN */
    if (p->open == TRUE) return (PORT_OPEN);

    /* CHECK THAT WE HAVE NOT EXCEEDED THE MAXIMUM PORT CONFIGURATION */
    for (i=0; i< MAX_PORTS; i++) {
        if (mcport[i] == NULL) {
            mcport[i] = (COMM_PORT FAR_ *) p;
            break;
        }
    }
    if (i == MAX_PORTS) return (NO_PORT);

    p->portnum  = i;
    p->open     = TRUE;
    portindex++;

    set_loopback(p, OFF);
    set_dtr(p, p->dtr);
    set_rts(p, p->rts);
    set_speed(p, p->speed);
    set_parity(p, p->parity);
    set_stopbits(p, p->stopbits);
    set_databits(p, p->rxdatabits);

    /* CONSOLE DRIVER */
    if (p->con_out == NULL) p->con_out   = (short (*)(COMM_PORT *, short)) conoutc;

#if (!defined(CTF))
    /* INSTALL CTRL-BREAK AND CTRL-C HANDLERS */
    if (use_ctrl_brk && portindex == 1) install_ctrl_brk_((void FAR_ *) cct_ctrl_brk_);

    /* REGISTER THE 'atexit' FUNCTION */
    if (!p->portnum) atexit(deinit_all_ports);
#endif

    return (0);
}



/*
C_CLOSE -- Close the designated port.
Return values:
    0           -- normal return;
    PORT_CLOSED -- port is already closed;
*/
short c_close_(COMM_PORT *p)
{
    short ret = PORT_CLOSED;

    if (p->open == TRUE) {
        ret = (*p->deinit_port)(p);
        mcport[p->portnum] = NULL;
        portindex--;
        p->open = FALSE;
        p->portnum = EOF;
        if (!portindex) {
            if (cct_warpdrive) set_warpdrive(0);
#if !defined(CTF)
            if (use_ctrl_brk) deinstall_ctrl_brk_((void FAR_ *)cct_ctrl_brk_);
#endif
        }
    }

    return (ret);
}
[ RETURN TO DIRECTORY ]