home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv197.zip / ckvtio.c < prev    next >
C/C++ Source or Header  |  1999-12-30  |  125KB  |  3,729 lines

  1. #ifndef VMS
  2. # error -- CKVTIO.C is used only on the VMS(tm) or OpenVMS(tm) Operating System
  3. #endif /* VMS */
  4.  
  5. /* REMINDER: "help system $blah" gives help about sys$blah() */
  6.  
  7. #ifdef __ALPHA
  8. # define CKVTIO_OS_ARCH_STRING " OpenVMS(tm) Alpha(tm)"
  9. #else
  10. # ifdef VAX
  11. #  define CKVTIO_OS_ARCH_STRING " OpenVMS(tm) VAX(tm)"
  12. # else
  13. #  ifdef __GNUC__
  14. #   define CKVTIO_OS_ARCH_STRING " OpenVMS(tm) VAX(tm) (GCC)"
  15. #  else
  16. #   error -- CKVTIO.C unknown architecture, neither VAX(tm) nor Alpha(tm)
  17. #  endif /* __GNUC__ */
  18. # endif /* VAX */
  19. #endif /* __ALPHA */
  20. /*
  21.   Module version number and date.
  22.   Also update the module number above accordingly.
  23. */
  24. char *ckxv = "Communications I/O 7.0.109, 18 Oct 1999";
  25. /*
  26.   This is the default architecture and operating system herald string.
  27.   It is redetermined dynamically in sysinit() below.
  28. */
  29. char *ckxsys = CKVTIO_OS_ARCH_STRING;
  30.  
  31. /*  C K V T I O  --  Terminal and Interrupt Functions for VAX/VMS  */
  32.  
  33. /* C-Kermit interrupt, terminal control & i/o functions for VMS systems */
  34.  
  35. /*
  36.   Author: Frank da Cruz <fdc@columbia.edu>
  37.   The Kermit Project, Columbia University, New York City.
  38.  
  39.   Copyright (C) 1985, 2000,
  40.     Trustees of Columbia University in the City of New York.
  41.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  42.     copyright text in the ckcmai.c module for disclaimer and permissions.
  43. */
  44.  
  45. /* Edit History
  46.  *
  47.  * Originally adapted to VMS by:
  48.  * Stew Rubenstein, Harvard University Chemical Labs, 1985.
  49.  *
  50.  * Cast of characters
  51.  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  52.  * cdh  Charles Don Hall  The Wollongong Group
  53.  * cf   Carl Friedberg    Comet & Co., NYC
  54.  * DS   Dan Schullman     Digital
  55.  * fdc  Frank da Cruz     Columbia U
  56.  * HG   Hunter Goatley    Western Kentucky University
  57.  * jh   James Harvey      Indiana / Purdue University
  58.  * js   James Sturdevant  (thru 1994)
  59.  * js   John Santos       (1996-...)
  60.  * LT   Lee Tibbert       Digital
  61.  * mab  Mark Buda         Digital
  62.  * mlo  Mike O'Malley     Digital
  63.  * MM   Martin Minow      Digital
  64.  * mpjz Martin PJ Zinzer  Gesellschaft fuer Schwerionenforschung GSI Darmstadt
  65.  * tmk  Terry Kennedy     Saint Peters College
  66.  * ttj  Tarjei T. Jensen  Norwegian Hydrographic Service
  67.  * wb   William Bader     Lehigh University
  68.  * wjm  Wolfgan J Moeller DECUS Germany
  69.  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  70.  * 006  8-May-85 MM   Got rid of "typeahead buffer" code as it didn't
  71.  *              solve the problem of data overruns at 4800 Baud.
  72.  *              Added vms "read a char" routine that checks for
  73.  *              CTRL/C, CTRL/Z, etc.
  74.  * 007 16-May-85 fdc  Changed calling convention of ttopen(), make it
  75.  *                    set value of its argument "lcl", to tell whether
  76.  *                    C-Kermit is in local or remote mode.
  77.  * 008 11 Jun 85 MM   Fix definition of CTTNAM
  78.  * 009 18 Jun 85 fdc  Move def of CTTNAM to ckcdeb.h so it can be shared.
  79.  * 010 25 Jun 85 MM   Added sysinit() to open console.
  80.  * 011  5 Jul 85 DS   Treat hangup of closed line as success.
  81.  * 012 11 Jul 85 fdc  Add gtimer(), rtimer() for timing statistics.
  82.  * 013 14 Sep 87 fdc  Add parity strip to ttinl(), add syscleanup().
  83.  * 014 14 Feb 89 mab  Make break REALLY work.  Add IOSB's to all QIO's.
  84.  * 015 26 Feb 89 mab  Add dcl exit handler and minor cleanup
  85.  * 016 23-Mar-89 mab  Add IO$M_BREAKTHRU to IO$_WRITEVBLK.
  86.  *              Add zchkspd() function to check for valid speed.
  87.  * 017 04-Apr-89 mab  Fix some minor bugs to local/remote code
  88.  * 018 23-Apr-89 mab  Add some of Valerie Mates' parity changes.
  89.  * 019 25-Apr-89 mab  Change baud to 110 for V4.x break routine as
  90.  *               50 baud is not supported on most Muxes by VMS.
  91.  * 020 13-Jun-89 mab  Fix on exquota problem on qiow(readvblk)
  92.  * 021 08-Jul-89 mab  Add ^C/^Y abort server mode code.
  93.  * 022 11-May-90 mab  Add V5A code
  94.  * 023 20-Jul-90 wb   Add support for old VAX C and VMS versions
  95.  * 024 22-Sep-90 wb   Fixes to cps/bps confusion
  96.  * 025 26-Sep-90 tmk  Fix the ztime() function
  97.  * 026 29-Sep-90 tmk  Edit 024 cause a server command to give some blither-
  98.  *              ings about unsupported line speeds.  Added a simple hack
  99.  *              to exit quietly if the passed speed is 0.  Adventurous
  100.  *              maintainers may want to look in ttpkt(), where ttsspd()
  101.  *              is being called with a value of -1/10.
  102.  * 027 11-Oct-90 fdc  Made ttol return number of chars successfully written.
  103.  *                    Made ztime() use ctime() in all cases.
  104.  *                    Another fix (from tmk) for bps/cps confusion.
  105.  *                    Wrapped source lines longer than 80 characters.
  106.  * 028 18-Oct-90 fdc  Added comments to concb() and vms_getchar() to show
  107.  *                    how to make Ctrl-C trap work.  Didn't actually do it,
  108.  *                    though, because Ctrl-Y apparently still can't be caught.
  109.  *                    Also, more minor reformatting.  Adjust time() declare.
  110.  *                    Added support for automatic parity sense in ttinl(),
  111.  *                    within #ifdef PARSENSE conditionals.  Built with PARSENSE
  112.  *                    defined, works ok.
  113.  * 029  5-Apr-91 fdc  Extensive reworking of many routines to allow for
  114.  *              network connections, addition of TGV MultiNet support.
  115.  * 030 31-Aug-91 tmk  Fix problem with INPUT statements not timing out due to
  116.  *              extraneous rtimer() inside gtimer().
  117.  * 032  6-Nov-91 fdc  Correct parity problem.
  118.  * 032  6-Nov-91 fdc  Cosmetic cleanup in sysinit().
  119.  * 033 14-Jan-91 fdc  Fix to get_qio_maxbuf_size to prevent crashes:
  120.  *                    remove "&" from "!test_qio(ttychn,max,&tmpbuf)",
  121.  *                    from John Schultz at 3M.
  122.  * 034  8-Feb-92 fdc  Don't change EIGHTBIT setting in ttvt, concb, or conbin.
  123.  *                    Set EIGHTBIT in ttpkt only if parity is not NONE.  From
  124.  *                    Alan Robiette, Oxford U, UK.
  125.  * 035 10-Jun-92 fdc  Added code from Ray Hunter of The Wollongong Group to
  126.  *                    support both WIN/TCP and TGV Multinet.  Network section
  127.  *                    of contti() redone to eliminate polling loop.  It's
  128.  *                    infinitely faster.
  129.  * 036 11-Jun-92 tmk  Fixed up edit 034 so 8-bit characters could be passed
  130.  *                    in connect mode.
  131.  * 037 19-Jun-92 fdc  Totally rewrote all the serial input and mode-setting
  132.  *                    routines in this module to use nonblocking, fully
  133.  *                    buffered input and to share a common buffer.  All
  134.  *                    serial-line input is localized to a single routine,
  135.  *                    txbufr(), which, in turn is known only to ttinc().  The
  136.  *                    other input routines, ttxin() and ttinl(), simply call
  137.  *                    ttinc().  ttchk() and ttflui() are totally cognizant of
  138.  *                    the buffer.  ttinl() now recognizes packets with
  139.  *                    printable start characters and/or lacking terminators,
  140.  *                    so VMS C-Kermit can now engage in "Doomsday Kermit"
  141.  *                    protocol.  ttvt() and ttpkt() were merged into a single
  142.  *                    new (static) routine, ttbin(), which no longer puts the
  143.  *                    device into PASALL mode (which defeats flow control).
  144.  *                    Added ttsndlb() to send a Long BREAK.  Much fine-tuning,
  145.  *                    testing, and filling-in remains to be done, including
  146.  *                    (a) make ttopen() and ttclos() aware of LAT devices; (b)
  147.  *                    check remaining BYTLM quota before issuing a read, (c)
  148.  *                    integrate network and serial buffers, and much more.
  149.  *                    Anyway, this code seems to run faster than ever before,
  150.  *                    and for the first time I can actually use sliding
  151.  *                    windows AND long packets on my 8-year old MicroVAX-II.
  152.  * 038 28-Jun-92 tmk  Additional work on edit 37, general cleanup of old defs.
  153.  * 039  1-Jul-92 wb   Changes for VMS 4.4.
  154.  * 040  4-Jul-92 tmk  Add modem signal support (ttgmdm routine).
  155.  * 041  4-Jul-92 tmk  Add tgetent(), worker routine for el-cheapo curses.
  156.  * 042  4-Jul-92 jh   Enable typeahead in ttbin().
  157.  * 043 21-Aug-92 fdc  Make default flow control be KEEP instead of Xon/Xoff.
  158.  * 044  6-Sep-92 fdc  Put default flow back to Xon/Xoff, but allow KEEP to be
  159.  *                    used to restore device's original flow-control setting.
  160.  * 045 23-Sep-92 fdc  Add sleep(1) to tthang().  Seems to fix HANGUP command.
  161.  *                    Suggested by Lee Tibbert.  Change ttbin() to use global
  162.  *                    flow variable rather than its flow parameter for setting
  163.  *                    flow control, to ensure the desired type of flow control
  164.  *                    is used during DIAL operations.
  165.  * 046 26-Sep-92 fdc  Change sleep(1) in tthang() to sleep(3).  Annoying but
  166.  *                    necessary.  IO$M_HANGUP takes about 3 seconds, but the
  167.  *                    sys$qiow() function returns immediately instead of
  168.  *                    waiting for the action to complete.
  169.  * 047 08-Oct-92 HG   Add call to sys$alloc in ttopen() to prevent user with
  170.  *              SHARE from getting port in use.  Some add'l cleanup.
  171.  * 048 12-Oct-92 LT   Minor changes to support DEC TCP/IP (nee UCX).
  172.  * 049 25-Oct-92 fdc  Adapt (ck_)cancio() to DEC TCP/IP.
  173.  *                    Remove superfluous ttflui() call from ttpkt().
  174.  *                    Add code from Lee Tibbert to sysinit() to figure out OS
  175.  *                    and architecture name at runtime.
  176.  * 050 18-Nov-92 fdc  Read from comm device in 1024-byte chunks, rather than
  177.  *                    trusting the qio_maxbuf_size.  This should reduce BYTLM
  178.  *                    quota-exceeded errors.  Suggested by tmk as a temporary
  179.  *                    workaround.
  180.  * 051 10-May-93 fdc  Add support for SET TRANSFER CANCELLATION.
  181.  * 052 16-May-93 fdc  Change VMSTCPIP to TCPIPLIB to agree with new CKCNET.H.
  182.  * 053 16-May-93 fdc  ANSIfication for GNU CC, from James Sturdevant.
  183.  * 054 08-Jun-83 fdc  Add TT$M_LOCALECHO and TT$M_ESCAPE to the terminal modes
  184.  *                    we handle, to prevent "getty babble" with modems, VAX
  185.  *                    PSI, etc.
  186.  * 055 16-Jun-93 fdc  Edit 054 only affected ttbin().  This edit does the same
  187.  *                    for conbin() and concb().  Fixes double echoing in
  188.  *                    command mode when coming in via VAX PSI.
  189.  * 056  8-Aug-93 fdc  Add types to all function declarations.
  190.  * 057 17-Aug-93 fdc  Add GET_SDC macro as in CKVIOC.C, accounting for GCC.
  191.  *                    From Tarjei T. Jensen <tarjeij@extern.uio.no>.
  192.  * 058 27-Sep-93 HG   Fix for real the SHARE issue when allocating terminal
  193.  *              by dropping SHARE before trying to assign the channel.
  194.  * 059  7-Oct-93 mlo  Added support for CMU-OpenVMS/IP ("CMU/Tek").  Requires
  195.  *                    CMU-OpenVMS/IP sockets library, also by mlo.
  196.  * 060  9-Oct-93 HG   Fix improper call to vms_assign_channel in edit 058,
  197.  *                    noticed by Fritz@GEMS.VCU.EDU.
  198.  * 061  9-Oct-93 fdc  For some reason, conbin() was turning off flow control.
  199.  *                    This caused massive data loss during CONNECT mode when
  200.  *                    running C-Kermit from a low-speed connection through
  201.  *                    a DECserver.  Now conbin() leaves the console flow
  202.  *                    control setting alone.
  203.  * 062 10-Oct-93 ttj  Change parameters to time() and ctime() from long to
  204.  *                    time_t (pointers).  Made prototype for vms_assign_channel
  205.  *                    and caught an erroneous function call.
  206.  * 063  9-Nov-93 fdc  In sysinit(), don't run sys$getdviw() on the "console"
  207.  *                    if the console is not a terminal (from tcwkw@sf.msc.edu).
  208.  *                    And (blame this one on me) _never_ set the backgrd flag.
  209.  * 064 25-Nov-93 fdc  Fixed coninc(n) to return -1 on timeout.
  210.  * 065  9-Dec-93 fdc  Fix transfer cancellation to account for parity, and
  211.  *                    allow it only in remote mode.
  212.  * 066 13-Dec-93 fdc  Make debug logging in ttol() show why packet writes fail.
  213.  * 067 15-Dec-93 fdc  New, MAXBUF-proof ttol() recovers from failures by
  214.  *                    writing the packet in chunks, whose size is computed
  215.  *                    dynamically.  This seems to cause no noticable slowdown
  216.  *                    in the transfer rate.
  217.  * 068 31-Dec-93 fdc  Fix bug in parity-detection code in ttinl().
  218.  * 069 14-Dec-93 fdc  Add ttgwsiz() routine, code from John Berryman.
  219.  * 070 14-MAR-94 mlo  CMU_TCPIP modifications: contti - return error (-1)
  220.  *                    if number of characters read is zero; (ck_)cancio - pass
  221.  *                    correct i/o channel for ttyfd to sys$cancel.
  222.  * 071 15-MAR-94 mlo  ttol() - add #ifdef DEBUG for compiles with NODEBUG
  223.  * 072 27-Mar-94 fdc  Straighten out some ttsspd()/ttgspd() confusion.
  224.  * 073 11-AUG-94 fdc  Make conoll() return a proper return code.
  225.  * 074 12-AUG-94 fdc  Make syscleanup() handle conres() error.  Make
  226.  * 075 20-AUG-94 js   Make congm() get terminal type, etc, to remove annoying
  227.  *                    "Sorry, terminal type not supported" messages when
  228.  *                    running from a .COM file, etc.
  229.  * 076 02-Sep-94 fdc  Call con_cancel() in syscleanup() to cancel any pending
  230.  *                    console i/o, hopefully eliminating zombies after
  231.  *                    after disconnection, etc.
  232.  * 077 22-Sep-94 mlo  Don't call sys$cancel() in (ck_k)cancio() on CMU/Tek
  233.  *                    network connections - it breaks the connection.
  234.  * 078 24-Feb-95 mpjz Fix for DECC on VAX.
  235.  * 079 08-Feb-96 fdc  Add symbols for higher serial speeds.
  236.  * 080 30-May-96 fdc  Fix netopen() to work with Rlogin.
  237.  * 081 25-Aug-96 mpjz More DECC/VAXC fixups.
  238.  * 082 05-Sep-96 fdc  Remove #module, change (ck_)cancio() declaration.
  239.  * 083 06-Sep-96 fdc  Fix loss of parity setting on network connections.
  240.  * 084 06-Sep-96 cf   Add missing speeds to ttspeeds[] (Carl Freidberg)
  241.  * 085 06-Sep-96 fdc  Add conspd() routine (for use by RLOGIN).
  242.  * 086 06-Sep-96 fdc  Try to fix parity confusion again.
  243.  * 087 06-Sep-96 fdc  And again: culprit is DECC sign-extension: see ttinc, etc
  244.  * 088 06-Sep-96 fdc  Unravel the network-file-descriptor reusability tangle
  245.  * 089 23-May-97 wjm  CMUIP & related fixes from Wolfgang Moeller
  246.  * 090 23-May-97 cdh  Patches for Wollongong/Attachmate Pathway
  247.  * 091 12-Jul-97 fdc  Try to avoid or at least identify terrible things that
  248.                       happen when (a) we have made a serial connection and the
  249.                       other side hangs it up (and so C-Kermit hangs in ttclos),
  250.                       and (b) when running in a DECwindow and user closes the
  251.                       DECwindow and so any attempt access to the console gets
  252.                       a DEVOFFLINE error, which in turn causes an error msg,
  253.                       which in turn gets another DEVOFFLINE error, etc, until
  254.                       the system grinds to halt.
  255.  * 092  6-Sep-97 fdc  Add ttspdlist() and startupdir[].
  256.  * 093 20-Sep-97 js   CMU/IP fixes (applied by fdc, hopefully correctly).
  257.  * 094 11-Dec-97 fdc  In ttgmdm(), omit controller-type test for Alpha.
  258.  *                    In txbufr(), treat hangup synchronously.
  259.  * 095 22-Dec-97 fdc  Add rftimer() and gftimer(), with help from James Puzzo.
  260.  *                    Fix ttclos() not to set network == 0, which makes
  261.  *                    subsequent ttchk() take the wrong path.
  262.  * 096  1-Jan-98 fdc  Add ok_to_share variable for ttopen() to get around
  263.  *                    an otherwise insuperable problem with DECIntact.
  264.  *                    Turn off broadcasts in conbin().  Set program name.
  265.  * 097 15-Jan-98 fdc  Don't set program name after all.  Use sys$getjpiw()
  266.  *                    instead of sys$getdviw("sys$login:") to get batch /
  267.  *                    interactive status.  More corrections to txbufr() carrier
  268.  *                    treatment.  Ditto for ttchk().
  269.  * 098  1-Feb-98 fdc  Fix contti() to not return a spurious character when
  270.  *                    CARRIER-WATCH is OFF and and sys$qiow() gives SS$_HANGUP
  271.  *                    and no character; return special code -2 and set up to
  272.  *                    queue a new read.
  273.  * 099  8-Apr-98 fdc  Add uname info in sysinit().  Commented out #includes
  274.  *                    for gftimer() in OLD_VMS (from lh).
  275.  * 100  3-May-98 fdc  Fix out-of-bounds tt_fulldevnam[]array reference.
  276.  * 101  7-May-98 lh   Improvements in hardware-name getting.
  277.  * 102  5-Jul-98 fdc  Illegal to take address of constant in GCC, reworked in
  278.  *                    gftimer() (from Kevin Handy).
  279.  * 103 25-Nov-98 fdc  Add support for CLOSE-ON-DISCONNECT.
  280.  * 104  9-Dec-98 fdc  Add support for NETLEBUF (see ckcnet.h).
  281.  * 105  5-Mar-99 fdc  Parameterize device name length TTNAMLEN.
  282.  *                    Fix ttchk() to avoid spurious "OK to exit?" after
  283.  *                    remote has hung up on us.
  284.  * 106 27-Apr-99 lh   Fix GCC warnings in gftimer().
  285.  * 107 22-Jul-99 fdc  Add code to sysinit() to get username.
  286.  * 108 23-Sep-99 fdc  Fix batch echoing & DCL image data execution.
  287.  * 109 18-Oct-99 fdc  Add setting of wasclosed variable.
  288.  */
  289.  
  290. /*
  291.  Variables available to outside world:
  292.  
  293.    dftty  -- Pointer to default tty name string, like "/dev/tty".
  294.    dfloc  -- 0 if dftty is console(remote), 1 if external line(local).
  295.    dfprty -- Default parity
  296.    dfflow -- Default flow control
  297.    ckxech -- Flag for who echoes console typein:
  298.      1 - The program (system echo is turned off)
  299.      0 - The system (or front end, or terminal).
  300.    functions that want to do their own echoing should check this flag
  301.    before doing so.
  302.  
  303.    backgrd
  304.      Flag indicating program not executing interactively.
  305.      Used to ignore INT and QUIT signals, suppress messages, etc.
  306.    vms_status
  307.      Status returned by most recent VMS system service,
  308.      can be used for error reporting.
  309.    batch
  310.      Nonzero if running in batch job, 0 otherwise.
  311.    itsatty
  312.      Nonzero if sys$input is the terminal, 0 if batch job or started with @.
  313.  
  314.  Functions for assigned communication line (either external or console tty):
  315.  
  316.    ttopen(ttname,local,mdmtyp) -- Open the named tty for exclusive access.
  317.    ttclos()                -- Close & reset the tty, releasing any access lock.
  318.    ttpkt(speed,flow)       -- Put the tty in packet mode and set the speed.
  319.    ttvt(speed,flow)        -- Put the tty in virtual terminal mode.
  320.                 or in DIALING or CONNECTED modem control state.
  321.    ttinl(dest,max,timo)    -- Timed read line from the tty.
  322.    ttinc(timo)             -- Timed read character from tty.
  323.    ttchk()                 -- See how many characters in tty input buffer.
  324.    ttxin(n,buf)            -- Read n characters from tty (untimed).
  325.    ttol(string,length)     -- Write a string to the tty.
  326.    ttoc(c)                 -- Write a character to the tty.
  327.    ttflui()                -- Flush tty input buffer.
  328.    tt_cancel()           -- Cancel any asynch I/O to tty
  329. */
  330.  
  331. /*
  332. Functions for console terminal:
  333.    congm()   -- Get console terminal modes.
  334.    concb(esc) -- Put the console in single-character wakeup mode with no echo.
  335.    conbin(esc) -- Put the console in binary (raw) mode.
  336.    conres()  -- Restore the console to mode obtained by congm().
  337.    conoc(c)  -- Unbuffered output, one character to console.
  338.    conol(s)  -- Unbuffered output, null-terminated string to the console.
  339.    conola(s) -- Unbuffered output, array of lines to the console, CRLFs added.
  340.    conxo(n,s) -- Unbuffered output, n characters to the console.
  341.    conchk()  -- Check if characters available at console (bsd 4.2).
  342.         Check if escape char (^\) typed at console (System III/V).
  343.    coninc(timo)  -- Timed get a character from the console.
  344.    conint()  -- Enable terminal interrupts on the console if not background.
  345.    connoi()  -- Disable terminal interrupts on the console if not background.
  346.    contti()  -- Get a character from either console or tty, whichever is first.
  347.  
  348. Time functions
  349.  
  350.    msleep(m) -- Millisecond sleep
  351.    ztime(&s) -- Return pointer to date/time string
  352.    rtimer()  -- Reset elapsed time counter
  353.    gtimer()  -- Get elapsed time
  354. */
  355.  
  356. /* Includes */
  357. #include "ckcdeb.h"            /* Formats for debug() */
  358. #include "ckcasc.h"
  359. #include "ckcker.h"
  360.  
  361. #include "ckvvms.h"
  362.  
  363. #include <stdio.h>            /* Unix Standard i/o */
  364. #include <jpidef.h>
  365. #include <signal.h>            /* Interrupts */
  366. #include <setjmp.h>            /* Longjumps */
  367. #include <iodef.h>
  368. #include <ttdef.h>
  369. #include <tt2def.h>
  370. #include <ssdef.h>
  371. #include <descrip.h>
  372. #include <dvidef.h>
  373. #include <dcdef.h>
  374. #include <devdef.h>
  375. #include <time.h>
  376. #include <syidef.h>
  377. #include <prvdef.h>
  378.  
  379. typedef struct {
  380.     int val[2];
  381. } QUAD;
  382.  
  383. /* lt 1992-10-08 Begin
  384.  */
  385. #ifndef __DECC                /* (was __ALPHA) */
  386. # ifndef __GNUC__
  387. #  define void int
  388. # endif /* __GNUC__ */
  389. #endif /* __DECC */
  390. /* lt 1992-10-08 End
  391.  */
  392.  
  393. #ifdef OLD_VMS
  394. #define IO$_TTY_PORT 41
  395. #ifdef COMMENT
  396. /* Now it's tested -- so don't */
  397. #include <libdtdef.h>            /* For gftimer() - Not tested! */
  398. #include <lib$routines.h>        /* ditto */
  399. #endif /* COMMENT */
  400. #else
  401. #include <starlet.h>
  402. #include <libdtdef.h>            /* OK in VMS 5.x & above */
  403. #include <lib$routines.h>        /* ditto */
  404. #endif /* OLD_VMS */
  405.  
  406. /* Macros */
  407.  
  408. #define xx_inc(timo) (--ttxbn>=0?(unsigned)(ttxbuf[ttxbp++]&0xff):txbufr(timo))
  409.  
  410. /* Declarations */
  411.  
  412. #ifndef __ALPHA
  413. #ifndef MULTINET
  414.     time_t time();
  415. #endif
  416. #endif
  417.     char *ctime();            /* Convert to asctime() string */
  418.  
  419.     void dcl_exit_h();            /* Exit handler */
  420.     unsigned short vms_assign_channel(struct dsc$descriptor_s *ttname);
  421.     VOID tt_cancel();
  422. /*
  423.   This is the device name for the console.
  424.   When we use SYS$INPUT, it prevents CONNECT from working in DCL
  425.   procedures that do not "$ DEFINE SYS$INPUT SYS$COMMAND".
  426.   When we use SYS$COMMAND, it allows CONNECT to work in DCL procedures,
  427.   but it forces the command parser to read from the terminal, and therefore
  428.   prevents inclusion of Kermit commands as "image data" in DCL procedures.
  429. */
  430. #define CONDEVICE    "SYS$INPUT"
  431. #define CONDEV_COLON "SYS$INPUT:"
  432.  
  433. /* dftty is the device name of the default device for file transfer */
  434. /* dfloc is 1 if dftty is the user's console terminal, 0 if an external line */
  435.  
  436. /* Note: another approach might be to get JPI$_TERMINAL from SYS$GETJPI */
  437.  
  438.     char *dftty = CTTNAM;
  439.     int dfloc = 0;            /* Default location is local */
  440.     int dfprty = 0;            /* Parity (0 = none) */
  441.     int ttprty = 0;            /* Parity in use */
  442.     int ttpflg = 0;            /* Parity not sensed yet. */
  443.     int backgrd = 0;            /* Running in "background" (no tty) */
  444.     static int ttpmsk = 0xff;        /* Communication device parity mask */
  445.     int ttmdm = 0;                      /* Modem in use. */
  446.     int dfflow = FLO_XONX;        /* Default flow control is Xon/Xoff */
  447.     int batch = 0;            /* Assume not batch */
  448.     int itsatty = 0;            /* isatty(0) result */
  449.     int ttcarr = CAR_AUT;        /* Carrier Handling Mode */
  450.     int tvtflg = 0;            /* Flag that ttvt has been called */
  451.     long ttspeed = -1;            /* For saving speed */
  452.     long conspd = -1;            /* Console terminal speed */
  453.     int ttflow = -9;            /* For saving flow */
  454.     static int ttdialing = 0;        /* Flag on when dialing */
  455.  
  456. int ckxech = 0; /* 0 if system normally echoes console characters, else 1 */
  457.  
  458. static int xhangup = 0;            /* Nonzero if hangup detected */
  459. int overruns = 0;            /* Data overruns detected */
  460.  
  461. unsigned int vms_status = SS$_NORMAL;    /* System service return status */
  462. unsigned int vms_lasterr = SS$_NORMAL;    /* Last error */
  463.  
  464. /* Structures used within this module */
  465.  
  466. #ifndef TT$C_BAUD_38400            /* For VMS versions higher than */
  467. #define TT$C_BAUD_38400    17        /* the one I'm compiling on... */
  468. #endif /* TT$C_BAUD_38400 */
  469.  
  470. #ifndef TT$C_BAUD_57600
  471. #define TT$C_BAUD_57600 18
  472. #endif /* TT$C_BAUD_57600 */
  473.  
  474. #ifndef TT$C_BAUD_76800
  475. #define TT$C_BAUD_76800 19
  476. #endif /* TT$C_BAUD_76800 */
  477.  
  478. #ifndef TT$C_BAUD_115200
  479. #define TT$C_BAUD_115200 20
  480. #endif /* TT$C_BAUD_115200 */
  481.  
  482. static struct {
  483.     unsigned char dec;
  484.     unsigned short int line;
  485.     } ttspeeds[] = {  /* Comments show TT$C_BAUD_xx symbol values */
  486.     {TT$C_BAUD_50,       5},    /*  1 */
  487.     {TT$C_BAUD_75,       7},    /*  2 */
  488.     {TT$C_BAUD_110,     11},    /*  3 */
  489.     {TT$C_BAUD_134,     13},    /*  4 */
  490.         {TT$C_BAUD_150,     15},    /*  5 */
  491.     {TT$C_BAUD_300,     30},    /*  6 */
  492.     {TT$C_BAUD_600,     60},    /*  7 */
  493.     {TT$C_BAUD_1200,   120},    /*  8 */
  494.     {TT$C_BAUD_1800,   180},    /*  9 */
  495.     {TT$C_BAUD_2000,   200},    /* 10 */
  496.     {TT$C_BAUD_2400,   240},    /* 11 */
  497.     {TT$C_BAUD_3600,   360},    /* 12 */
  498.     {TT$C_BAUD_4800,   480},    /* 13 */
  499.     {TT$C_BAUD_7200,   720},    /* 14 */
  500.     {TT$C_BAUD_9600,   960},    /* 15 */
  501.     {TT$C_BAUD_19200, 1920},    /* 16 */
  502.     {TT$C_BAUD_38400, 3840},    /* 17 */
  503.     {TT$C_BAUD_57600, 5760},    /* 18 */
  504.     {TT$C_BAUD_76800, 7680},    /* 19 */
  505.     {TT$C_BAUD_115200,11520},    /* 20 */
  506.     {0,                   0} };
  507.  
  508. /* Declarations of variables global within this module */
  509.  
  510. /* was long... */
  511. #define TIME_T time_t
  512. #ifdef __ALPHA
  513. #ifdef WINTCP
  514. #include <socket_aliases.h>
  515. #undef TIME_T
  516. #define TIME_T long
  517. #endif /* WINTCP */
  518. #endif /* __ALPHA */
  519.  
  520. static TIME_T tcount = 0;        /* For timing statistics */
  521.  
  522. static char brkarray[] = {        /* For simulating BREAK */
  523.  
  524.  '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
  525.  '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
  526.  '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
  527.  '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
  528. };
  529.  
  530. int ttyfd = -1;                /* TTY file descriptor */
  531.  
  532. #ifdef COMMENT /* old */
  533. static int conif = 0,            /* Console interrupts on/off flag */
  534.     conclass = 0,            /* Console device type */
  535.     cgmf = 0,                /* Flag that console modes saved */
  536.     xlocal = 0,                         /* Flag for tty local or remote */
  537.     ttychn = 0,                /* TTY i/o channe; */
  538.     conchn = 0,                /* Console i/o channel */
  539.     con_queued = 0,            /* console i/o queued in contti() */
  540.     tt_queued = 0,            /* tty i/o queued in contti() */
  541.     conch,                /* console input character buffer  */
  542.     curcarr = 0,            /* Carrier mode: require/ignore */
  543.     ttch;                /* tty input character buffer */
  544. #else /* new */
  545. static unsigned short
  546.     ttychn = 0,                /* TTY i/o channe; */
  547.     conchn = 0,                /* Console i/o channel */
  548.     con_queued = 0,            /* console i/o queued in contti() */
  549.     tt_queued = 0;            /* tty i/o queued in contti() */
  550.  
  551. static int conif = 0,            /* Console interrupts on/off flag */
  552.     conclass = 0,            /* Console device type */
  553.     cgmf = 0,                /* Flag that console modes saved */
  554.     xlocal = 0,                         /* Flag for tty local or remote */
  555.     conch,                /* console input character buffer  */
  556.     curcarr = 0,            /* Carrier mode: require/ignore */
  557.     ttch;                /* tty input character buffer */
  558. #endif /* COMMENT */
  559.  
  560. static unsigned char escchr;        /* Escape or attn character */
  561. static char ttnmsv[TTNAMLEN+1];        /* copy of open path for tthang */
  562. char cttnam[TTNAMLEN+1];        /* Controlling terminal name */
  563. static char tt_fulldevnam[TTNAMLEN+1];
  564. static struct dsc$descriptor_s tt_fulldevnam_d;    /* Descriptor for line name */
  565.  
  566. static long int qio_maxbuf_size;    /* Maximum size of QIO to succeed */
  567. static long dclexh_status;            /* Exit status for DCL exit handler */
  568. static struct iosb_struct coniosb, ttiosb, wrk_iosb;
  569. static struct tt_mode
  570.     ttold, ttraw, tttvt,        /* for communication line */
  571.     ccold, ccraw, cccbrk,        /* and for console */
  572.     cctmp;
  573.  
  574. /* Network support */
  575.  
  576. #include "ckcnet.h"            /* Network type symbols */
  577. extern int ttnet;            /* Network type */
  578. static int network = 0;            /* 1 if network connection */
  579. #ifdef TCPSOCKET
  580. extern int ttnproto;
  581. #endif /* TCPSOCKET */
  582. extern int xfrcan, xfrchr, xfrnum;    /* Transfer cancellation */
  583. extern int initflg, wasclosed;
  584.  
  585. /*
  586.   Select proper library function for getting socket device channel.
  587. */
  588. #ifdef TCPIPLIB
  589. #if defined (__DECC)
  590. # define GET_SDC (short)decc$get_sdc
  591. #elif (defined (VAXC) || defined (__VAXC) || defined (__GNUC__))
  592. # define GET_SDC vaxc$get_sdc
  593. #else
  594. # error unknown compiler, not DECC and not VAXC
  595. #endif /* __DECC */
  596. #endif /* TCPIPLIB */
  597.  
  598. /* Needed for parity fixes in edit 036 */
  599. extern int parity;            /* current parity setting */
  600.  
  601. /*
  602.   New buffered input scheme.
  603. */
  604. #define TTXBUF
  605.  
  606. #ifdef TTXBUF
  607. #define TTXBUFL RBSIZ            /* Internal buffer size */
  608.  
  609. CHAR     ttxbuf[TTXBUFL+1];        /* The buffer */
  610. int     ttxbp = 0, ttxbn = 0;        /* Buffer pointer and count */
  611. int     ok_to_share = 0;        /* Can be set by user interface */
  612.  
  613. int get_qio_maxbuf_size(unsigned long int ttychn);
  614. int test_qio(unsigned long int ttychn, long int max, unsigned char *dest);
  615. int ttispd();
  616.  
  617. /*
  618.   T X B U F R
  619.  
  620.   Read bytes from communication device into internal buffer ttxbuf[].
  621.   To be called only when input buffer is empty, i.e. when ttxbn == 0.
  622.  
  623.   Other comm-device reading routines, like ttinc, ttinl, ttxin, should check
  624.   the internal buffer first, and call this routine for a refill if necessary.
  625.  
  626.   When data is read successfully, the first character is returned and
  627.   the global buffer count, ttxbn, is set to the number of characters remaining
  628.   in ttxbuf after it, and the global buffer offset, ttxbp, is set to 1.
  629.  
  630.   When data is not read successfully, -1 is returned indicating a timeout,
  631.   or -2 indicating disconnection.
  632. */
  633. int
  634. txbufr(timo) int timo; {        /* TT Buffer Read */
  635.     int i, count;
  636.     int func;                /* Read function code */
  637.     int mask;                /* Event mask */
  638.     int vms_status;            /* Read QIO return code */
  639.     static int trmmsk[2] = {0,0};    /* Terminal read break mask */
  640.     static int trmlong[8] = {0,0,0,0,0,0,0,0}; /* Break on nothing */
  641.     char tmpbuf[16];
  642.  
  643.     debug(F101,"txbufr entry, ttxbn","",ttxbn);
  644.     if (ttxbn > 0) {            /* Should not be called */
  645.     debug(F101,"txbufr called with ttxbn","",ttxbn); /* if ttxbn > 0! */
  646.     ttxbn--;
  647.     return(ttxbuf[ttxbp++] & 0xff);
  648.     }
  649.     ttxbp = ttxbn = 0;            /* Reset buffer pointer and count */
  650.     ttxbuf[0] = NUL;            /* and the buffer itself */
  651.  
  652.     if (timo < 0)            /* Be safe */
  653.       timo = 0;
  654.     debug(F101,"txbufr timo","",timo);
  655.  
  656.     func = IO$_READVBLK | IO$M_NOFILTR;    /* Read virtual block, no filtering */
  657.     trmmsk[0] = sizeof(trmlong);    /* No terminators */
  658.     trmmsk[1] = (int)&trmlong;        /* Keep all characters */
  659. /*
  660.   We try to scoop up as many as we can in a nonblocking read (timed, but with
  661.   timeout value of 0).  This is supposed to return immediately with up to
  662.   "count" characters placed in our buffer.
  663. */
  664.     count = TTXBUFL;            /* Maximum characters to read */
  665.  
  666. #ifdef COMMENT
  667. /*
  668.   This causes problems because we are not adjusting according to the CURRENT
  669.   BYTLM quota, but rather to the one that was obtained when Kermit started.
  670.   Since the quota is dynamic, it might have been reduced since then.
  671. */
  672.     if (count > qio_maxbuf_size)
  673.     count = qio_maxbuf_size;
  674. #else
  675. /*
  676.   So for now we use 1024, which tends to be smaller than the value obtained
  677.   above.  Later, this should be changed to find out the remaining BYTLM quota
  678.   and use that instead (unless that is too expensive).  This size can be
  679.   overridden at compile time by defining the symbol...
  680. */
  681. #ifndef CKV_IO_SIZE
  682. #define CKV_IO_SIZE 1024
  683. #endif /* CKV_IO_SIZE */
  684.     if (count > CKV_IO_SIZE)
  685.     count = CKV_IO_SIZE;
  686. #endif /* COMMENT */
  687.  
  688.     debug(F101,"txbufr count","",count);
  689.  
  690.     for (i = 0; i < 2; i++) {        /* Two tries... */
  691.     sprintf(tmpbuf,"txbufr %d",i);
  692.     if (i == 0)
  693.       /* First read is nonblocking, timed, with timeout of 0 */
  694.       vms_status = sys$qiow(QIOW_EFN, ttychn,
  695.                 func|IO$M_TIMED, &wrk_iosb, 0, 0,
  696.                 ttxbuf, count, 0, &trmmsk, 0, 0);
  697.     else
  698.       /* Second read is blocking, for 1 byte, using the timeout given */
  699.       vms_status = sys$qiow(QIOW_EFN, ttychn,
  700.                 func, &wrk_iosb, 0, 0,
  701.                 ttxbuf, 1, timo, &trmmsk, 0, 0);
  702.  
  703.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  704.     debug(F111,tmpbuf,"sys$qiow status",vms_status);
  705.  
  706. #ifdef SS$_DATAOVERUN
  707.     if (vms_status == SS$_DATAOVERUN) { /* This shouldn't be fatal */
  708.         overruns++;
  709.         debug(F111,tmpbuf,"data overrun",overruns);
  710.     } else
  711. #endif /* SS$_DATAOVERUN */
  712.     if (vms_status != SS$_NORMAL && vms_status != SS$_HANGUP) {
  713.         debug(F111,tmpbuf,"fatal error",vms_status);
  714.         return(-2);
  715.     }
  716.     if (vms_status == SS$_HANGUP && ttcarr != CAR_OFF) {
  717.         xhangup = 1;        /* Remember for next time */
  718.         debug(F111,tmpbuf,"sys$qiow SS$_HANGUP",SS$_HANGUP);
  719.         debug(F111,tmpbuf,"sets xhangup",xhangup);
  720.     }
  721.     debug(F011,"txbufr ttxbuf",ttxbuf,wrk_iosb.size);
  722.     debug(F101,"txbufr iosb.size","",wrk_iosb.size);
  723.     debug(F101,"txbufr iosb.status","",wrk_iosb.status);
  724. /*
  725.   Did we get some data?  Even if SS$_HANGUP was indicated, we might also have
  726.   received some data at the same time (like a NO CARRIER message); if we did,
  727.   return it now and catch the HANGUP next time around.
  728. */
  729.     if (wrk_iosb.size > 0) {
  730.         ttxbn = wrk_iosb.size;    /* Set buffer count. */
  731.         ttxbn--;            /* Less one for the one we return */
  732.         return(ttxbuf[ttxbp++] & 0xff); /* Return it, bump offset */
  733.     }
  734. /*
  735.   Check for hangup only if no data was obtained.  Reports indicate that
  736.   that SS$_HANGUP is not indicated more than once, hence the flag.
  737. */
  738.     if (xhangup > 0) {        /* Hangup detected last time */
  739.         debug(F110,tmpbuf,"xhangup return",0);
  740.         return(-2);
  741.     } else if (wrk_iosb.status == SS$_HANGUP) { /* Or detected this time */
  742.         debug(F111,tmpbuf,"SS$_HANGUP: ttcarr",ttcarr);
  743.         debug(F111,tmpbuf,"SS$_HANGUP: ttdialing",ttdialing);
  744.         if (ttcarr != CAR_OFF && !ttdialing) {
  745.         /* Don't set xhangup here because apparently VMS returns */
  746.         /* this error even when the connection has not hung up. */
  747.         /* Experimentation shows that setting xhangup here prevents */
  748.         /* file transfer from working on dialout connections. */
  749.         /* (VMS 7.1, Alpha PWX 433/au) */
  750.         /* xhangup = 1; */
  751.         debug(F110,tmpbuf,"fatal: hangup and CARRIER-WATCH not OFF",0);
  752.         return(-2);
  753.         }
  754.     } else if (wrk_iosb.status != SS$_TIMEOUT) {
  755.         debug(F111,tmpbuf,"fatal: unexpected iosb status",wrk_iosb.status);
  756.         return(-2);            /* Call it a hangup */
  757.     }
  758. /*
  759.   No error and no characters either, so next time around try a blocking,
  760.   possibly timed, read for a single character.  Since this routine will be
  761.   called again very soon, the first read with a zero timeout will have the
  762.   rest of the user data in it.  Thus, this isn't as inefficient as it first
  763.   appears.
  764. */
  765.     if (timo > 0)
  766.       func |= IO$M_TIMED;
  767.     } /* Go back for second loop iteration */
  768.  
  769. /* Get here only if second read timed out - i.e. no characters arrived. */
  770.  
  771.     debug(F101, "txbufr timed out","",timo);
  772.     return(-1);
  773. }
  774.  
  775. /*  T T I N C  --  Read a character from the communication device  */
  776. /*
  777.   ttinc() maintains an internal buffer to minimize system calls.
  778.   Returns the next character, or -1 if there is a timeout, or -2
  779.   on communications disconnect.  Calls txbufr() to refill its buffer
  780.   when necessary.
  781. */
  782. int
  783. ttinc(timo) int timo; {
  784.     int x; unsigned char c;
  785.  
  786. #ifdef NETCONN
  787.     if (network) {
  788. #ifdef COMMENT
  789.     int x;
  790.     x = netinc(timo);
  791.     if ((ttnproto == NP_TELNET) && (x > -1))
  792.       return((unsigned)(x & 0xff));
  793.     else
  794.       return(x < 0 ? x : (unsigned)(x & ttpmsk));
  795. #else
  796.     return(netinc(timo));
  797. #endif /* COMMENT */
  798.     }
  799. #endif /* NETCONN */
  800.  
  801.     debug(F101,"ttinc ttxbn","",ttxbn);
  802.     if (ttxbn > 0) {            /* Something in internal buffer? */
  803.     ttxbn--;            /* Yes, deduct one from count. */
  804.     c = ttxbuf[ttxbp++];        /* Return the next character. */
  805.     debug(F101,"ttinc returns c","",c);
  806.     return((unsigned)(c & 0xff));
  807.     } else if ((x = txbufr(timo)) < 0) { /* No, fill buffer */
  808.     debug(F101,"ttinc txbufr failed","",x); /* Pass along failure. */
  809.     return(x);
  810.     } else {                /* Success. */
  811.     debug(F101,"ttinc returns x","",x);
  812.     return((unsigned)(x & 0xff));    /* Return the character */
  813.     }
  814. }
  815.  
  816. /*  T T X I N  --  Get n bytes from tty input buffer  */
  817. /*
  818.   Call with n = number of bytes to get, buf = where to put them.
  819.  
  820.   This routine assumes that the given number of bytes is available
  821.   and will not return until they are gotten.  You should only call this
  822.   routine after calling ttchk to find out how many bytes are waiting to
  823.   to be read.
  824.  
  825.   Returns:
  826.   -1 on error, number of chars gotten on success.
  827. */
  828. int
  829. ttxin(n,buf) int n; CHAR *buf; {
  830.     int i, x;
  831.  
  832.     debug(F101,"ttxin","",n);
  833.  
  834. #ifdef NETCONN
  835.     if (network) {
  836.     for (i = 0; i < n; i++) {
  837.         if ((x = ttinc(0)) < 0) return(-1);
  838.         buf[i] = (char) x;
  839.     }
  840.     } else {
  841. #endif /* NETCONN */
  842. /* xx_inc() is a macro */
  843.     for (i = 0; i < n; i++) {
  844.         if ((x = xx_inc(0)) < 0) return(-1);
  845.         buf[i] = (char) x;
  846.     }
  847. #ifdef NETCONN
  848.     }
  849. #endif /* NETCONN */
  850.     buf[i] = NUL;
  851.     return(i);
  852. }
  853.  
  854. /*  T T F L U I  --  Flush communication device input buffer  */
  855.  
  856. int
  857. ttflui() {
  858.     int n;
  859.     debug(F100,"ttflui","",0);
  860. #ifdef NETCONN
  861.     if (network)
  862.       return(netflui());
  863. #endif /* NETCONN */
  864.  
  865.     ttxbn = ttxbp = 0;            /* Flush internal buffer *FIRST* */
  866.     if ((n = ttchk()) > 0) {
  867.     debug(F101,"ttflui count","",n);
  868.     while ((n--) && xx_inc(2) > -1) ; /* Don't worry, it's buffered. */
  869.     }
  870.     return(0);
  871. }
  872.  
  873. /*  T T C H K  --  Check how many bytes are waiting to be read */
  874. /*
  875.   Returns number of bytes waiting, or < 0 if connection has dropped.
  876.   The number of bytes waiting includes those in our internal buffer plus
  877.   those in VMS's internal input buffer.
  878. */
  879. int                    /* Check how many bytes are ready */
  880. ttchk() {                /* for reading from network */
  881.     static struct {
  882.     unsigned short count;
  883.     unsigned char first;
  884.     unsigned char reserved1;
  885.     long reserved2;
  886.     } ttchk_struct;
  887.  
  888. #ifdef NETCONN
  889.     if (network) {            /* Network connection */
  890.     debug(F101,"ttchk network ttyfd","",ttyfd);
  891.     if (ttyfd < 0)
  892.       return(-2);
  893.     else
  894.       return(nettchk());
  895.     }
  896. #endif /* NETCONN */
  897.  
  898.     debug(F101,"ttchk ttxbn","",ttxbn);
  899.     debug(F101,"ttchk xhangup","",xhangup);
  900.  
  901.     if (xlocal && xhangup)
  902.       return(-2);
  903.  
  904.     if (!ttychn) {
  905.     debug(F101,"ttchk called with no ttychn","",0);
  906.     return(0);
  907.     }
  908.  
  909.     if (
  910. #ifdef COMMENT
  911. /*
  912.   I tried something like this in UNIX and it didn't work at all so I'm
  913.   leaving it as a comment here until it can be tested.  - fdc, Jan 98.
  914. */
  915.     ttxbn < 1 &&
  916. #endif /* COMMENT */
  917.     xlocal && ttcarr != CAR_OFF) { /* No data in buffer */
  918.     int x;            /* Serial connection */
  919.     extern int clsondisc;
  920.     x = ttgmdm();        /* with carrier checking */
  921.     debug(F101,"ttchk ttgmdm","",x);
  922.     if (x > -1) {        /* Then we better have carrier */
  923.         if (!(x & BM_DCD)) {
  924.         debug(F101,"ttchk carrier lost","",x);
  925.         if (clsondisc) {    /* If "close-on-disconnect" */
  926.             debug(F100,"ttchk close-on-disconnect","",0);
  927.             ttclos(0);    /* close device & release it. */
  928.         }
  929.         return(-2);
  930.         }
  931.     }
  932.     }
  933.     vms_status = sys$qiow(QIOW_EFN, ttychn, IO$_SENSEMODE|IO$M_TYPEAHDCNT,
  934.               &wrk_iosb, 0, 0, &ttchk_struct,
  935.               sizeof(ttchk_struct), 0, 0, 0, 0
  936.               );
  937.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  938. #ifdef DEBUG
  939.     if (deblog) {
  940.     debug(F101,"ttchk sys$qiow status","",vms_status);
  941.     debug(F101,"ttchk count","",(int)ttchk_struct.count);
  942.     if (ttchk_struct.count)
  943.       debug(F101,"ttchk first","",(int)ttchk_struct.first);
  944.     }
  945. #endif /* DEBUG */
  946.     return(vms_status & 1 ? ttchk_struct.count + ttxbn : ttxbn);
  947. }
  948.  
  949. #ifdef CTRLC
  950. #undef CTRLC
  951. #endif /* CTRLC */
  952. #define CTRLC '\03'
  953.  
  954. #ifndef NOXFER
  955. /*  T T I N L  --  Read a packet from the communications connection.  */
  956. /*
  957.   Reads up to "max" characters from the communication line, terminating on:
  958.  
  959.     (a) the packet length field if the "turn" argument is zero, or
  960.     (b) on the turnaround character (turn) if the "turn" argument is nonzero
  961.     (c) n repetitions of the interruption character (3 ^C's by default)
  962.  
  963.   and returns the number of characters read upon success, or if "max" was
  964.   exceeded or the timeout interval expired before (a) or (b), returns -1.
  965.  
  966.   The characters that were input are copied into "dest" with their parity bits
  967.   stripped if parity was selected.  Returns the number of characters read.
  968.   Characters after end of packet are available upon the next call to this
  969.   function.
  970. */
  971. int
  972. #ifdef CK_ANSIC
  973. ttinl(CHAR *dest, int max, int timo, CHAR eol, CHAR start, int turn)
  974. #else
  975. ttinl(dest,max,timo,eol,start,turn) int max,timo,turn; CHAR *dest,eol,start;
  976. #endif /* CK_ANSIC */
  977. /* ttinl */ {
  978.     int x, y, c, i, j;
  979.     int ccn = 0;        /* Control C counter */
  980.     int flag;
  981.     int cc;
  982.     unsigned char *cp;
  983.     int pktlen = -1;
  984.     int lplen = 0;
  985.     int havelen = 0;
  986.  
  987.     debug(F101,"ttinl start","",start);
  988.     debug(F101,"ttinl turn","",turn);
  989.     debug(F101,"ttinl timo","",timo);
  990.     i = j = flag = 0;
  991. #ifdef COMMENT
  992.     ttpmsk = (ttprty) ? 0177 : 0377;    /* Set parity stripping mask. */
  993. #endif /* COMMENT */
  994.     debug(F101,"ttinl loop entry, network","",network);
  995.     while (i < max) {
  996.     cc = network ? netinc(timo) : xx_inc(timo); /* Read a byte */
  997.     if (cc < 0) {
  998.         debug(F101,"ttinl cc","",cc);
  999.         if (cc == -1 || cc == -2) {
  1000.         return(cc);
  1001.         } else {            /* I hate C ... */
  1002.         debug(F100,
  1003.               "ttinl: SIGN EXTENSION BOTCH - FIX SOURCE CODE","",0
  1004.               );
  1005.         /* This doesn't really help because 255 becomes -1, etc. */
  1006.         cc &= 0xff;
  1007.         }
  1008.     }
  1009.  
  1010.     /* Check for cancellation */
  1011.     if (!xlocal && xfrcan && ((cc & ttpmsk) == xfrchr)) {
  1012.         if (++ccn >= xfrnum) {    /* If xfrnum in a row, bail out. */
  1013.         fprintf(stderr,"^C...\r\n"); /* Echo Ctrl-C */
  1014.         return(-2);
  1015.         }
  1016.     } else ccn = 0;            /* No cancel, so reset counter, */
  1017.  
  1018.     if ((flag == 0) && ((cc & 0x7f) == start)) {
  1019.         debug(F100,"ttinl got start","",0);
  1020.         flag = 1;            /* Got packet start. */
  1021.     }
  1022.     if (flag) {            /* If we're in a packet... */
  1023.         dest[i++] = cc & ttpmsk;
  1024.         if ((cc & 0x7f) == eol) { /* Stop at eol. */
  1025.         debug(F101,"ttinl got eol, i","",i);
  1026.         break;
  1027.         }
  1028.     }
  1029. /*
  1030.   If we have not been instructed to wait for a turnaround character, we
  1031.   can go by the packet length field.  If turn != 0, we must wait for the
  1032.   end of line (eol) character before returning.
  1033. */
  1034. #ifndef xunchar
  1035. #define xunchar(ch) (((ch) - 32 ) & 0xFF )    /* Character to number */
  1036. #endif /* xunchar */
  1037.  
  1038.     if (i == 2) {
  1039.         pktlen = xunchar(dest[1] & 0x7f);
  1040.         havelen = (pktlen > 1);
  1041.         debug(F101,"ttinl length","",pktlen);
  1042.     } else if (i == 5 && pktlen == 0) {
  1043.         lplen = xunchar(dest[4] & 0x7f);
  1044.     } else if (i == 6 && pktlen == 0) {
  1045.         pktlen = lplen * 95 + xunchar(dest[5] & 0x7f) + 5;
  1046.         havelen = 1;
  1047.         debug(F101,"ttinl length","",pktlen);
  1048.     }
  1049.     if (havelen && !turn && (i > pktlen+1)) { /* Use length field */
  1050.         debug(F101,"ttinl break on length","",i);
  1051.         break;
  1052.     }
  1053.     }
  1054.     dest[i] = '\0';            /* Terminate the string */
  1055.     debug(F101,"ttinl loop done, i","",i);
  1056.     debug(F101,"ttinl max","",max);
  1057.     debug(F101,"ttinl dest[i-1]","",dest[i-1]);
  1058.     debug(F101,"ttinl eol","",eol);
  1059.  
  1060.     if (i >= max) {
  1061.     debug(F100,"ttinl buffer overflow","",0);
  1062.     return(-1);    /* Overflowed dest buffer without getting eol */
  1063.     }
  1064.     x = i;                /* Size. */
  1065.     dest[x] = '\0';            /* Terminate with null */
  1066.  
  1067.     debug(F110,"ttinl packet",dest,0);
  1068.     debug(F101,"ttinl size","",x);    /* Log the size */
  1069.     debug(F101,"ttinl ttprty 1","",ttprty);
  1070.  
  1071.     if (ttpflg++ == 0 && ttprty == 0) {    /* Check and adjust the parity. */
  1072.         if ((ttprty = parchk(dest,start,x)) > 0) {
  1073.         debug(F000,"ttinl parchk senses parity","",ttprty);
  1074.         ttpmsk = 0x7f;
  1075.         for (i = 0; i < x; i++)    /* Strip parity from this packet */
  1076.           dest[i] &= 0x7f;
  1077.     }
  1078.     if (ttprty < 0) ttprty = 0;    /* Restore if parchk error */
  1079.     debug(F101,"ttinl ttprty 2","",ttprty);
  1080.     }
  1081.     return(x);                /* Return length */
  1082. }
  1083. #endif /* NOXFER */
  1084. #endif /* TTXBUF */
  1085.  
  1086. SIGTYP (*saval)() = NULL;        /* For saving alarm handler */
  1087.  
  1088. VOID
  1089. ttimoff() {                /* Turn off any timer interrupts */
  1090.     alarm(0);
  1091.     if (saval) {
  1092.     signal(SIGALRM,saval);
  1093.     saval = NULL;
  1094.     } else {
  1095.     signal(SIGALRM,SIG_IGN);    /* (was SIG_DFL) */
  1096.     }
  1097. }
  1098.  
  1099. /*  P R I N T _ M S G  --  Log an error message from VMS  */
  1100.  
  1101. int
  1102. print_msg(s) char *s; {
  1103.     long int blen = 0;
  1104.     char buf[PMSG_BUF_SIZE], msg[PMSG_MSG_SIZE];
  1105.     struct dsc$descriptor_s b = {
  1106.     PMSG_BUF_SIZE-1,
  1107.     DSC$K_DTYPE_T,
  1108.     DSC$K_CLASS_S,NULL
  1109.     };
  1110.  
  1111.     b.dsc$a_pointer = (char *)&buf;
  1112.     vms_status = sys$getmsg(vms_status, &blen, &b, 0, 0);
  1113.     if (!(vms_status & 1)) {
  1114.     vms_lasterr = vms_status;
  1115.     fprintf(stderr,"print_msg; sys$getmsg\n");
  1116.     return(-1);
  1117.     }
  1118.     buf[blen] = '\0';
  1119.     sprintf(msg, "%s: %s\n", s, buf);
  1120.     debug(F100,s,"",0);
  1121.     ermsg(msg);
  1122.     return(0);
  1123. }
  1124.  
  1125. /*  S Y S I N I T  --  System-dependent program initialization.  */
  1126.  
  1127. #ifndef DVI$_FULLDEVNAM
  1128. #define DVI$_FULLDEVNAM 232
  1129. #endif /* DVI$_FULLDEVNAM */
  1130.  
  1131. #ifndef DVI$_STS
  1132. #define DVI$_STS 226
  1133. #endif /* DVI$_STS */
  1134.  
  1135. int
  1136. ttgwsiz() {                /* Get console window (screen) size */
  1137.     int x = -1;
  1138.     extern int tt_rows, tt_cols;
  1139.     typedef struct {            /* define an item list struct */
  1140.     short length;            /* length of buffer */
  1141.     short code;            /* item code */
  1142.     void *ptr;            /* ptr to buffer */
  1143.     void *retlen;            /* ptr to return length */
  1144.     } item_list;
  1145.     int status, iosb[2];
  1146.     item_list tt_dvi[] = {        /* Item list for GETDVI */
  1147.     {4,DVI$_DEVBUFSIZ,&tt_cols,0},
  1148.     {4,DVI$_TT_PAGE,&tt_rows,0},
  1149.     {0,0,0,0}
  1150.     };
  1151.     $DESCRIPTOR(sysin,CONDEVICE);
  1152.     status = sys$getdviw(0,0,&sysin,&tt_dvi,&iosb,0,0,0);
  1153.     if (!(status & 1)) {
  1154.     vms_lasterr = status;
  1155.     return(-1);
  1156.     }
  1157.     if (tt_rows < 1 || tt_cols < 1) return(0);
  1158.     return(1);
  1159. }
  1160.  
  1161. #define CK_SYSNMLN 31
  1162. char unm_mch[CK_SYSNMLN+1] = { '\0', '\0' };
  1163. char unm_mod[CK_SYSNMLN+1] = { '\0', '\0' };
  1164. char unm_nam[CK_SYSNMLN+1] = { '\0', '\0' };
  1165. char unm_rel[CK_SYSNMLN+1] = { '\0', '\0' };
  1166. char unm_ver[CK_SYSNMLN+1] = { '\0', '\0' };
  1167.  
  1168. int
  1169. sysinit() {
  1170.     extern int speed;
  1171.     extern char ttname[], uidbuf[];
  1172. #ifdef CK_ENVIRONMENT
  1173.     extern char tn_env_sys[];
  1174. #endif /* CK_ENVIRONMENT */
  1175.     extern char startupdir[];
  1176.     extern char *ckzsys, *ck_s_name, *ck_s_ver;
  1177.     int i, x, n;
  1178.     char * p;
  1179.     struct dsc$descriptor_s prcname;
  1180.     char ckname[24];
  1181.  
  1182.     static struct desblk {
  1183.     long int *fl;            /* Forward link.  Used by VMS only */
  1184.     void (*fncpnt)();        /* Function to call */
  1185.     unsigned char argcnt;        /* Only one arg allowed */
  1186.     unsigned char filler[3];    /* Filler.  Must be zero */
  1187.     long int *sts;            /* Address of sts (written by VMS) */
  1188.     } dclexh_ = {0,dcl_exit_h,1,{0,0,0},&dclexh_status};
  1189.  
  1190. #define GETCKXSYS
  1191. /*
  1192.   Get architecture and operating system name.
  1193. */
  1194. #ifdef GETCKXSYS
  1195.  
  1196. /* OK, we have a VAX so what is the name of the OS? */
  1197.  
  1198. #ifndef SYI$_ARCH_NAME    /* Should be in syidef.h but is not there yet */
  1199. # define SYI$_ARCH_NAME 4454
  1200. #endif /* SYI$_ARCH_NAME */
  1201.  
  1202.     struct iosb_t {
  1203.     short int status;        /* System service status */
  1204.     short int unused[3];
  1205.     } iosb;
  1206.  
  1207.     struct itmlst_t {
  1208.     short unsigned int buffer_len;    /* Buffer length */
  1209.     short unsigned int item_code;    /* Item code */
  1210.     char *buffer;            /* Where to write SYI info */
  1211.     long unsigned int *ret_len;    /* Pointer to returned length */
  1212.     long unsigned int mbz;        /* Must Be Zero */
  1213.  
  1214.     } itmlst;
  1215.  
  1216.     struct itmlst dviitm[] = {{64,DVI$_FULLDEVNAM,(char *)&cttnam,0},
  1217.             {sizeof(conclass),DVI$_DEVCLASS,(char *)&conclass,0},
  1218.                 {0,0,0,0}};
  1219.  
  1220.     long unsigned int ret_len;        /* Returned length */
  1221. /*
  1222.   $getsyi of "hw_arch" will fail prior to VMS 5.5.  This failure indicates that
  1223.   the OS name is "VAX/VMS" (sic).  Use success or failure of $getsyi "hw_arch"
  1224.   rather than the more straight forward $getsyi "node_swvers" because latter
  1225.   is defined as four (4) characters and will get strange representing VMS
  1226.   10.0.
  1227. */
  1228.  
  1229. /*  Default */
  1230.  
  1231. #ifdef __ALPHA
  1232.     ckxsys = " OpenVMS Alpha";
  1233. #else
  1234.     /* Not strictly correct but traditional & familiar... */
  1235.     ckxsys = " VAX/VMS";
  1236. #endif /* __ALPHA */
  1237.  
  1238.     itmlst.buffer_len = CK_SYSNMLN;
  1239.     itmlst.item_code = SYI$_ARCH_NAME;
  1240.     itmlst.buffer = unm_mch;
  1241.     itmlst.ret_len = &ret_len;
  1242.     itmlst.mbz = 0;
  1243.  
  1244.     if ((sys$getsyiw (0, 0, 0, &itmlst, &iosb, 0, 0) & 1) == 1)
  1245.       if ((iosb.status & 1) == 1)
  1246. #ifdef __ALPHA
  1247.     ckxsys = " OpenVMS Alpha";
  1248. #else
  1249.     ckxsys = " OpenVMS VAX";
  1250. #endif /* __ALPHA */
  1251.  
  1252.     if (unm_mch[0] == '\0') {        /* supply a default */
  1253.         for (p = ckxsys; *p == ' '; p++) ;
  1254.         ckstrncpy(unm_mch,p,3);
  1255.     }
  1256.  
  1257.     for (p = ckxsys; *p == ' '; p++) ;    /* Strip leading blanks */
  1258.     ckstrncpy(unm_nam,p,CK_SYSNMLN);    /* For uname info */
  1259.     ckzsys = ckxsys;            /* Same deal for file module */
  1260.  
  1261. #ifdef CK_ENVIRONMENT
  1262.     strcpy(tn_env_sys,"VMS");
  1263. #ifdef CK_SNDLOC
  1264.     {
  1265.     extern char * tn_loc;
  1266.     char *p;
  1267.     if (p = getenv("LOCATION"))
  1268.       if (tn_loc = (char *)malloc((int)strlen(p)+1))
  1269.         strcpy(tn_loc,p);
  1270.     }
  1271. #endif /* CK_SNDLOC */
  1272. #endif /* CK_ENVIRONMENT */
  1273.  
  1274.     itmlst.buffer_len = CK_SYSNMLN;    /* Get hardware model */
  1275. #ifdef SYI$_HW_NAME                     /* as of V5.0 ? */
  1276.     itmlst.item_code = SYI$_HW_NAME;
  1277. #else
  1278. #ifdef SYI$_NODE_HWTYPE
  1279.     itmlst.item_code = SYI$_NODE_HWTYPE;
  1280. #else
  1281.     itmlst.item_code = 0;        /* shouldn't happen... */
  1282. #endif /* SYI$_NODE_HWTYPE */
  1283. #endif /* SYI$_HW_NAME */
  1284.     itmlst.buffer = unm_mod;
  1285.     itmlst.ret_len = &ret_len;
  1286.     itmlst.mbz = 0;
  1287.     x = sys$getsyiw (0, 0, 0, &itmlst, &iosb, 0, 0);
  1288.  
  1289.     itmlst.buffer_len = CK_SYSNMLN;    /* Get VMS release */
  1290.     itmlst.item_code = SYI$_VERSION;
  1291.     itmlst.buffer = unm_rel;
  1292.     itmlst.ret_len = &ret_len;
  1293.     itmlst.mbz = 0;
  1294.     x = sys$getsyiw (0, 0, 0, &itmlst, &iosb, 0, 0);
  1295.  
  1296.     if ((x & 1) && unm_rel[0]) {    /* Just the major version number */
  1297.     for (p = unm_rel; *p && !isdigit(*p); p++) ;
  1298.     ckstrncpy(unm_ver,p,CK_SYSNMLN);
  1299.     p = unm_ver;
  1300.     while (isdigit(*p++)) ;
  1301.     *(p-1) = '\0';
  1302.     }
  1303. #endif /* GETCKXSYS */
  1304.  
  1305. /*
  1306.  * Set up DCL Exit handler.  This allows us to reset terminal
  1307.  * and any other modifications we have done.
  1308.  */
  1309.     debug(F101,"sysinit ttychn","",ttychn);
  1310.     debug(F101,"sysinit conchn","",conchn);
  1311.     if (!CHECK_ERR("sysinit: sys$dclexh",
  1312.            sys$dclexh(&dclexh_))) {
  1313.         debug(F100,"sysinit failed to declare exit handler","",0);
  1314. #ifdef COMMENT
  1315.     return(0);
  1316. #endif /* COMMENT */
  1317.     }
  1318.  
  1319.     {                    /* Get username */
  1320.     struct itmlstdef {
  1321.         short int buflen;        /* Length of buffer */
  1322.         short int itmcod;        /* Function code */
  1323.         char *bufaddr;        /* Address of buffer */
  1324.         long int *retlen;        /* Address to return actual length */
  1325.     };
  1326.     int i;
  1327.     char userbuf[UIDBUFLEN];    /* The book says 12 is max */
  1328.     long buflen = 0;        /* And it's 12 in VMS 7.1  */
  1329.     unsigned long user_flags;
  1330.     struct itmlstdef gjiitm[] = {
  1331.         UIDBUFLEN-1, JPI$_USERNAME, NULL, 0, 0, 0, 0, 0
  1332.     };
  1333.     gjiitm[0].bufaddr = userbuf;
  1334.     gjiitm[0].retlen = &buflen;
  1335.     for (i = 0; i < UIDBUFLEN; i++) userbuf[i] = 0;
  1336.     vms_status = sys$getjpiw(0, 0, 0, &gjiitm, 0, 0, 0);
  1337.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  1338.     if (vms_status != SS$_NORMAL) {
  1339.         debug(F101,"sysinit sys$getjpiw error","",vms_status);
  1340.     } else {
  1341.         char c;
  1342.         debug(F111,"sysinit sys$getjpiw ok",userbuf,vms_status);
  1343.         for (i = 0; i < UIDBUFLEN; i++) {
  1344.         c = userbuf[i];
  1345.         if (c == ' ') c = '\0';
  1346.         uidbuf[i] = c;
  1347.         if (!c)
  1348.           break;
  1349.         }
  1350.         debug(F111,"sysinit sys$getjpiw uidbuf",uidbuf,buflen);
  1351.     }
  1352.     }
  1353.     if (ttychn)                         /* if comms line already opened */
  1354.       return(0);            /* (how could it be???) */
  1355.  
  1356.     if (!conchn) {            /* Get console channel */
  1357. #ifdef CMU_TCPIP
  1358.       /* need to open console using libcmu routine to enable `select' call on
  1359.        * file descriptor zero.
  1360.        */
  1361.       cmu_stdin_open(dftty);
  1362.       conchn = cmu_get_sdc(0);
  1363. #else
  1364.       struct dsc$descriptor_s devnam =
  1365.         {sizeof(dftty)-1,DSC$K_DTYPE_T,DSC$K_CLASS_S,NULL};
  1366.       devnam.dsc$a_pointer = dftty;
  1367.       conchn = vms_assign_channel(&devnam);
  1368. #endif /* CMU_TCPIP */
  1369.     }
  1370.     congm();                /* Get and save its modes */
  1371. /*
  1372.  * Parse console terminal device name.
  1373.  */
  1374.     itsatty = 0;
  1375.     if (isatty(0))
  1376.       itsatty = 1;
  1377.     debug(F101,"sysinit itsatty","",itsatty);
  1378.  
  1379.     if (
  1380. #ifdef COMMENT
  1381.     itsatty                /* Fails for batch and DCL */
  1382. #else
  1383.     !batch                /* Fails only for batch */
  1384. #endif /* COMMENT */
  1385.     ) {
  1386.     CHECK_ERR("sysinit: sys$getdviw",
  1387.           sys$getdviw(0, conchn, 0, &dviitm, &wrk_iosb, 0, 0, 0));
  1388.     debug(F111,"sysinit","cttnam",cttnam);
  1389.  
  1390.     if (!CHECK_ERR("sysinit: sys$qiow",
  1391.                sys$qiow(QIOW_EFN, conchn, IO$_SENSEMODE,
  1392.                 &wrk_iosb, 0, 0,
  1393.                 &ccold, sizeof(ccold), 0, 0, 0, 0)))
  1394.       return(-1);
  1395.     ttspeed = speed = ttispd((unsigned char) wrk_iosb.size);
  1396.     conspd = ttspeed;
  1397.     debug(F111,"sysinit speed",cttnam,speed);
  1398.     ckstrncpy(ttname,cttnam,TTNAMLEN);
  1399.     }
  1400.  
  1401.     /* Initialize descriptor */
  1402.     tt_fulldevnam_d.dsc$b_dtype = DSC$K_DTYPE_T;
  1403.     tt_fulldevnam_d.dsc$b_class = DSC$K_CLASS_S;
  1404.  
  1405.     strcpy(startupdir, zgtdir());    /* Default directory at startup */
  1406.  
  1407. #ifdef CK_VMSSETNAME            /* (Which is not defined) */
  1408. /*
  1409.   The problem with this is that it stays there even after Kermit exits.
  1410.   If anybody really cares to have "C-Kermit 6.1" show up in SHOW SYSTEM, etc,
  1411.   then we'll need to get the current name, save it, and then restore it in
  1412.   the exit handler.
  1413. */
  1414.     ckname[0] = '\0';            /* Process name */
  1415.     ckstrncpy(ckname,ck_s_name,22);    /* Copy program name from main() */
  1416.     strncat(ckname,"_",22);        /* Separator */
  1417.     strncat(ckname,ck_s_ver,22);    /* Version number */
  1418.     for (n = 0,i = 0; i < 24; i++) {    /* Chop off edit number */
  1419.     if (ckname[i] == '.')
  1420.       n++;
  1421.     if (n == 2) {
  1422.         ckname[i] = '\0';
  1423.         break;
  1424.     }
  1425.     }
  1426.     ckname[15] = '\0';            /* Max length is 15 */
  1427.     n = strlen(ckname);
  1428.     prcname.dsc$w_length = n;        /* Convert to descriptor */
  1429.     prcname.dsc$a_pointer = (char *)ckname;
  1430.     prcname.dsc$b_class = DSC$K_CLASS_S;
  1431.     prcname.dsc$b_dtype = DSC$K_DTYPE_T;
  1432.     for (i = 1; i <= 9; i++) {        /* Try to set it */
  1433.     x = sys$setprn(&prcname);
  1434.     if (!(x & 1)) vms_lasterr = x;
  1435.     debug(F111,"sysinit sys$setprn",ckname,x);
  1436.     if (x == SS$_NORMAL)        /* Worked, done */
  1437.       break;
  1438.     if (x != SS$_DUPLNAM)        /* Anything else but duplicate name */
  1439.       break;
  1440.     if (n > 13)            /* Add a suffix, 1..9, to name */
  1441.       break;
  1442.     ckname[n] = '_';
  1443.     ckname[n+1] = (char) (i + '0');
  1444.     ckname[n+2] = '\0';
  1445.     prcname.dsc$w_length = n+2;    /* Let VMS know the new length */
  1446.     }
  1447. #endif /* CK_VMSSETNAME */
  1448.     debug(F100,"sysinit done","",0);
  1449.     return(0);
  1450. }
  1451.  
  1452. /*
  1453.  * DCL Exit handler.  This is the cleanup handler for program.
  1454.  * Any final cleanup (closing channels etc) should be done at this
  1455.  * point.
  1456.  */
  1457. VOID
  1458. dcl_exit_h(sts) unsigned long int *sts; {
  1459.     syscleanup();
  1460.     return;
  1461. }
  1462.  
  1463. /*  S Y S C L E A N U P -- System-dependent program epilog.  */
  1464.  
  1465. int
  1466. syscleanup() {
  1467.     int x;
  1468.     extern int zclosf();
  1469.     extern void zrestoredir();
  1470.     void con_cancel();
  1471.     debug(F101,"syscleanup entry","",ttyfd);
  1472.     con_cancel();        /* Cancel pending console i/o. */
  1473.     ttclos(ttyfd);        /* Do the cleanup no matter what... */
  1474.     zclosf(ZSYSFN);        /* Close various files and kill child procs */
  1475. #ifdef COMMENT
  1476.     /* This is a bit extreme... */
  1477.     /*
  1478.        No it isn't.  Scenario: user closes DECwindow with Alt-F4 or whatever.
  1479.        The sys$qiow() call in conres() fails with %SYSTEM-F-DEVOFFLINE.
  1480.        If we don't exit now, this error will recur infinitely after we return,
  1481.        when we try to print any kind of message.  But we still keep this
  1482.        commented out because now (July 1997) we test for this in conres().
  1483.     */
  1484.     if ((x = conres()) < 0) exit(SS$_ABORT);
  1485. #else
  1486.     conres();
  1487. #endif /* COMMENT */
  1488.     zrestoredir();            /* Restore startup directory */
  1489.     printf("\r");
  1490.     return(0);
  1491. }
  1492.  
  1493. /*  T T O P E N  --  Open a tty for exclusive access.  */
  1494.  
  1495. /*  Returns 0 on success, -1 on failure.  */
  1496. /*
  1497.   If called with lcl < 0, sets value of lcl as follows:
  1498.   0: the terminal named by ttname is the job's controlling terminal.
  1499.   1: the terminal named by ttname is not the job's controlling terminal.
  1500.   But watch out: if a line is already open, or if requested line can't
  1501.   be opened, then lcl remains (and is returned as) -1.
  1502. */
  1503. static int ismodem = 0;
  1504. static int isremote = 0;
  1505. static int isdialup = 0;
  1506. static int isdisconnect = 0;
  1507. static int issecure = 0;
  1508. static int ishangup = 0;
  1509. static int ismodhangup = 0;
  1510.  
  1511. int
  1512. ttopen(ttname,lcl,modem,timo) char *ttname; int *lcl, modem, timo; {
  1513.     extern int speed;
  1514.     int s, x;
  1515.     unsigned long int no_share_priv[2], prev_privs[2];
  1516.     unsigned long int devchar, devclass, devsts;
  1517.  /* char dvibuf[65]; */
  1518.     struct dsc$descriptor_s devnam = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  1519.     struct itmlst dviitm[] = {{64,DVI$_FULLDEVNAM,(char *)&tt_fulldevnam,0},
  1520.             {sizeof(devchar),DVI$_DEVCHAR,NULL,0},
  1521.             {sizeof(devclass),DVI$_DEVCLASS,NULL,0},
  1522.             {sizeof(devsts),DVI$_STS,NULL,0},
  1523.             {0,0,0,0}};
  1524.  
  1525. #ifdef DEBUG
  1526.     if (deblog) {
  1527.     debug(F110,"ttopen ttname",ttname,0);
  1528.     debug(F110,"ttopen ttnmsv",ttnmsv,0);
  1529.     debug(F101,"ttopen modem","",modem);
  1530.     debug(F101,"ttopen network","",network);
  1531.     debug(F101,"ttopen ttychn","",ttychn);
  1532.     debug(F101,"ttopen ttyfd","",ttyfd);
  1533.     }
  1534. #endif /* DEBUG */
  1535.  
  1536.     dviitm[1].adr = (char *)&devchar;
  1537.     dviitm[2].adr = (char *)&devclass;
  1538.     dviitm[3].adr = (char *)&devsts;
  1539.  
  1540. #ifdef NETCONN
  1541.     if (network && ttyfd > -1) {    /* if device already opened */
  1542.         if (strncmp(ttname,ttnmsv,TTNAMLEN-1)) /* are new & old names equal? */
  1543.           ttclos(ttyfd);        /* no, close old ttname, open new */
  1544.         else                 /* else same, ignore this call, */
  1545.       return(0);            /* and return. */
  1546.     }
  1547.     if (modem < 0) {            /* modem < 0 = special code for net */
  1548.     int x;
  1549.     ttmdm = modem;
  1550.     modem = -modem;            /* Positive network type number */
  1551.     debug(F111,"ttopen net",ttname,modem);
  1552.     network = 1;            /* Because rlog_ini() uses ttoc() */
  1553.     x = netopen(ttname, lcl, modem);
  1554.     if (x > -1) {            /* Success... */
  1555.         ckstrncpy(ttnmsv,ttname,TTNAMLEN);
  1556.         ttnet = modem;
  1557.     } else                /* Failed, unset network flag */
  1558.       network = 0;
  1559.         return(x);
  1560.     }
  1561. #endif /* NETCONN */
  1562.  
  1563.     if (ttychn) return(0);        /* Close channel if open */
  1564.  
  1565. /* Now we know we're opening a serial device */
  1566.  
  1567.     network = 0;            /* So set this to zero */
  1568.  
  1569.     devnam.dsc$w_length  = strlen(ttname); /* Get real name of device */
  1570.     devnam.dsc$a_pointer = ttname;
  1571.     sys$getdviw(0, 0, &devnam, &dviitm, &wrk_iosb, 0, 0, 0);
  1572.     tt_fulldevnam[TTNAMLEN] = '\0';    /* Make sure name has an end... */
  1573.  
  1574.     if (devclass != DC$_TERM) {        /* Is it a terminal? */
  1575.     fprintf(stderr,
  1576.         "%%CKERMIT-W-NOTTERM, %s is not a terminal\n",ttname);
  1577.     return(-1);
  1578.     }
  1579.     if (!(devchar & DEV$M_AVL)) {    /* Is it available? */
  1580.     fprintf(stderr,
  1581.         "%%CKERMIT-W-NOTAVAL, %s is not available\n",tt_fulldevnam);
  1582.     return(-5);
  1583.     }
  1584.     if (!(devsts & UCB$M_ONLINE)) {    /* Is it online? */
  1585.     fprintf(stderr,
  1586.         "%%CKERMIT-W-OFFLINE, %s is not online\n",tt_fulldevnam);
  1587.     return(-5);
  1588.     }
  1589.     wasclosed = 1;
  1590.     ttmdm = modem;            /* Make this available to other fns */
  1591.     xlocal = *lcl;            /* Make this available to other fns */
  1592.     xhangup = 0;            /* New conneciton - no hangup yet */
  1593.     overruns = 0;            /* Reset overrun counter */
  1594. /*
  1595.  *  Set up the tt_fulldevnam_d descriptor for use by ttclos() later.
  1596.  */
  1597.     tt_fulldevnam_d.dsc$w_length  = strlen(tt_fulldevnam);
  1598.     tt_fulldevnam_d.dsc$a_pointer = tt_fulldevnam;
  1599. /*
  1600.   In the old days, we just tried to assign the device, but this could result
  1601.   in two people having the same serial device open, which does nobody any
  1602.   good.  Starting in 1993, we dropped SHARE privilege before allocating the
  1603.   device to prevent this.  But in 1997 we found that this prevented C-Kermit
  1604.   from being used in remote mode under DECIntact, which allocates the TT:
  1605.   device for itself first, so a /SHARE qualifier was added to SET LINE, which,
  1606.   if given, sets ok_to_share to 1, and if not given sets it to 0.  Now we set
  1607.   or unset SHARE privilege based on this variable.  -fdc, 31 Dec 1997.
  1608. */
  1609.     no_share_priv[0] = PRV$M_SHARE;
  1610.     debug(F101,"ttopen ok_to_share","",ok_to_share);
  1611.     vms_status = sys$setprv(ok_to_share ? 1 : 0, &no_share_priv,0,&prev_privs);
  1612.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  1613.     debug(F101,"ttopen sys$setprv 1","",vms_status);
  1614.     ttychn = vms_assign_channel(&devnam); /* Get a channel for it. */
  1615.     debug(F101,"ttopen ttychn","",ttychn);
  1616.     vms_status = sys$setprv (1, &prev_privs, 0, 0); /* Restore old privs */
  1617.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  1618.     debug(F101,"ttopen sys$setprv 2","",vms_status);
  1619.     if (!ttychn) return(-1);        /* Couldn't open, fail */
  1620.     wasclosed = 0;
  1621. /*
  1622.  * Check for maximum size of QIO, so as to not get the dreaded quota exceeded
  1623.  * status returned.  When doing a QIO that has a larger buffer than
  1624.  * MAXBUF, exceeded quota is returned.
  1625.  *
  1626.  * Example: MAXBUF = 2048, QIO = 1936, overhead is 112: will succeed.
  1627.  *        QIO of 1937 will fail.
  1628.  *
  1629.  * This can change for different versions of VMS.
  1630.  */
  1631.     qio_maxbuf_size = get_qio_maxbuf_size(ttychn);
  1632.  
  1633.     ckstrncpy(ttname,tt_fulldevnam,TTNAMLEN); /* Copy true name to main() */
  1634.     ckstrncpy(ttnmsv,ttname,TTNAMLEN);    /* Keep a copy of the name locally */
  1635.     ttxbn = ttxbp = 0;            /* Initialize input buffer */
  1636.  
  1637. /* Caller wants us to figure out if line is controlling tty */
  1638.  
  1639.     debug(F111,"ttopen ok",ttname,*lcl);
  1640.     if (*lcl < 0) {
  1641.     if (conclass == DC$_TERM)
  1642.       xlocal = (strncmp(ttname,cttnam,TTNAMLEN) == 0) ? 0 : 1;
  1643.     else
  1644.       xlocal = 1;             /* If not a term, then we must be local */
  1645.     debug(F111,"ttopen controlling terminal",cttnam,xlocal);
  1646.     }
  1647.     if (!CHECK_ERR("ttopen: sys$qiow",
  1648.     sys$qiow(QIOW_EFN, ttychn, IO$_SENSEMODE, &wrk_iosb, 0, 0,
  1649.          &ttold, sizeof(ttold), 0, 0, 0, 0))) return(-1);
  1650.  
  1651.     ttspeed = speed = ttispd((unsigned char) wrk_iosb.size);
  1652.     debug(F101,"ttopen speed","",speed);
  1653.  
  1654. /* Got the line, now set the desired value for local. */
  1655.  
  1656.     if (*lcl) *lcl = xlocal;
  1657.  
  1658.     tttvt = ttold;
  1659.     ttraw = ttold;
  1660.  
  1661. #ifdef TT$M_MODEM
  1662.     debug(F101,"ttopen TT$M_MODEM","",TT$M_MODEM);
  1663.     if (ttold.basic & TT$M_MODEM)
  1664.       ismodem = 1;
  1665. #endif /* TT$M_MODEM */
  1666.     debug(F101,"ttopen ismodem","",ismodem);
  1667. #ifdef TT$M_REMOTE
  1668.     debug(F101,"ttopen TT$M_REMOTE","",TT$M_REMOTE);
  1669.     if (ttold.basic & TT$M_REMOTE)    /* This really means "has carrier" */
  1670.       isremote = 1;            /* if TT$M_MODEM */
  1671. #endif /* TT$M_REMOTE */
  1672.     debug(F101,"ttopen isremote","",isremote);
  1673. #ifdef TT2$M_DIALUP
  1674.     debug(F101,"ttopen TT2$M_DIALUP","",TT2$M_DIALUP);
  1675.     if (ttold.basic & TT2$M_DIALUP)
  1676.       isdialup = 1;
  1677. #endif /* TT$2M_DIALUP */
  1678.     debug(F101,"ttopen isdialup","",isdialup);
  1679. #ifdef TT2$M_DISCONNECT
  1680.     debug(F101,"ttopen TT2$M_DISCONNECT","",TT2$M_DISCONNECT);
  1681.     if (ttold.basic & TT2$M_DISCONNECT)
  1682.       isdisconnect = 1;
  1683. #endif /* TT$2M_DISCONNECT */
  1684.     debug(F101,"ttopen isdisconnect","",isdisconnect);
  1685. #ifdef TT2$M_SECURE
  1686.     debug(F101,"ttopen TT2$M_SECURE","",TT2$M_SECURE);
  1687.     if (ttold.basic & TT2$M_SECURE)
  1688.       issecure = 1;
  1689. #endif /* TT$2M_SECURE */
  1690.     debug(F101,"ttopen issecure","",issecure);
  1691. #ifdef TT2$M_HANGUP
  1692.     debug(F101,"ttopen TT2$M_HANGUP","",TT2$M_HANGUP);
  1693.     if (ttold.basic & TT2$M_HANGUP)
  1694.       ishangup = 1;
  1695. #endif /* TT$2M_HANGUP */
  1696.     debug(F101,"ttopen ishangup","",ishangup);
  1697. #ifdef TT2$M_MODHANGUP
  1698.     debug(F101,"ttopen TT2$M_MODHANGUP","",TT2$M_MODHANGUP);
  1699.     if (ttold.basic & TT2$M_MODHANGUP)
  1700.       ismodhangup = 1;
  1701. #endif /* TT$2M_MODHANGUP */
  1702.     debug(F101,"ttopen ismodhangup","",ismodhangup);
  1703.  
  1704.     debug(F101,"ttopen lcl","",*lcl);
  1705.     ttpmsk = 0xff;
  1706.     return(0);
  1707. }
  1708.  
  1709. #ifdef COMMENT
  1710. /*
  1711.   Old version.
  1712. */
  1713. unsigned long int
  1714. vms_assign_channel(ttname) char *ttname;  {
  1715.     unsigned int channel = 0;
  1716.     struct dsc$descriptor_s d = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  1717.  
  1718.     d.dsc$w_length  = strlen(ttname);
  1719.     d.dsc$a_pointer = ttname;
  1720.     if (!CHECK_ERR("vms_assign_channel: sys$assign",
  1721.     sys$assign(&d, &channel, 0, 0))) return(0);
  1722.     return(channel);
  1723. }
  1724. #else
  1725. /*
  1726.   New version from Hunter Goatley.
  1727. */
  1728. unsigned short int
  1729. vms_assign_channel(ttname) struct dsc$descriptor_s *ttname; {
  1730.     unsigned short channel = 0;
  1731.  
  1732. #ifdef COMMENT
  1733. /* what's all this then ... */
  1734.     struct dsc$descriptor_s d = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  1735.     d.dsc$w_length  = strlen(ttname);
  1736.     d.dsc$a_pointer = ttname;
  1737. #endif /* COMMENT */
  1738.  
  1739.     if (!CHECK_ERR("vms_assign_channel: sys$assign",
  1740.     sys$assign(ttname, &channel, 0, 0))) return(0);
  1741.  
  1742. #ifdef NEW_STUFF_FROM_BRUCE_DAY
  1743. /*
  1744.   Which is not presently defined...
  1745.   This is supposed to allow C-Kermit to go into protocol mode on an LTA
  1746.   device without a prior CONNECT command.
  1747. */
  1748. #ifdef IO$M_LT_CONNECT
  1749.     vms_status = sys$qiow(QIOW_EFN, channel, IO$_TTY_PORT|IO$M_LT_CONNECT,
  1750.                           &wrk_iosb, 0, 0, 0, 0, 0, 0, 0, 0);
  1751.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  1752.     debug(F101, "vms_assign_channel LAT connect status","",vms_status);
  1753.     debug(F101, "vms_assign_channel LAT connect iosb","",wrk_iosb.status);
  1754. #endif /* IO$M_LT_CONNECT */
  1755. #endif /* NEW_STUFF_FROM_BRUCE_DAY */
  1756.  
  1757.     return(channel);
  1758. }
  1759. #endif /* COMMENT */
  1760.  
  1761. /*  T T C L O S  --  Close the communication device.  */
  1762.  
  1763. int
  1764. ttclos(dummy) int dummy; {
  1765.     int channel;
  1766. #ifdef DEBUG
  1767.     if (deblog) {
  1768.     debug(F101,"ttclos 1: ttyfd","",ttyfd);
  1769.     debug(F101,"ttclos 1: ttychn","",ttychn);
  1770.     debug(F101,"ttclos 1: network","",network);
  1771.     debug(F101,"ttclos 1: overruns","",overruns);
  1772.     }
  1773. #endif /* DEBUG */
  1774.  
  1775. #ifdef NETCONN
  1776.     if (network) {            /* Network connection. */
  1777.     netclos();            /* Close it. */
  1778.     debug(F101,"ttclos 2: network","",network);
  1779.     debug(F101,"ttclos 2: ttychn","",ttychn);
  1780.     debug(F101,"ttclos 2: ttyfd","",ttyfd);
  1781.     /* network = 0; */
  1782.     return(0);
  1783.     }
  1784. #endif /* NETCONN */
  1785.  
  1786. /* The rest is for serial connections, in which ttychn is nonzero. */
  1787.  
  1788.     if (!ttychn)
  1789.       return(0);
  1790. /*
  1791.   Observations indicate that it can take 20-30 seconds for DTR to drop
  1792.   after closing the device.  Perhaps a call to tthang() should go here.
  1793. */
  1794.     debug(F100,"ttclos calling ck_cancio","",0);
  1795.     ck_cancio();            /* Cancel any pending i/o. */
  1796.     debug(F100,"ttclos calling ttres","",0);
  1797.     ttres();                /* Reset modes. */
  1798. /*
  1799.   Assume it's a LAT device and try to do a LAT disconnect on it.
  1800.   If it fails, then it's not a LAT device and no harm is done.
  1801. */
  1802. #ifdef IO$M_LT_DISCON
  1803.     debug(F100,"ttclos calling sys$qiow for LAT disconnect","",0);
  1804.     vms_status = sys$qiow(QIOW_EFN, ttychn, IO$_TTY_PORT|IO$M_LT_DISCON,
  1805.               &wrk_iosb, 0, 0, 0, 0, 0, 0, 0, 0);
  1806.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  1807.     debug(F101, "ttclos LAT disconnect, status", "", vms_status);
  1808.     debug(F101, "ttclos LAT disconnect, iosb", "", wrk_iosb.status);
  1809. #else
  1810.     debug(F100, "ttclos LAT disconnect not supported", "", 0);
  1811. #endif /* IO$M_LT_DISCON */
  1812.     debug(F100,"ttclos calling sys$dassgn","",0);
  1813.     channel = ttychn;
  1814.     ttychn = 0;                /* Mark it as closed */
  1815.     wasclosed = 1;
  1816.     /* xhangup = 0; */
  1817.     if (!CHECK_ERR("ttclos: sys$dassgn", sys$dassgn(channel)))
  1818.       return(-1);
  1819.     debug(F101,"ttclos done - ttychn","",ttychn);
  1820.     return(0);
  1821. }
  1822.  
  1823. /*  T T R E S  --  Restore terminal to its original modes.  */
  1824.  
  1825. int
  1826. ttres() {                /* Restore the tty to normal. */
  1827.     ttpmsk = 0xff;
  1828. #ifdef NETCONN
  1829.     if (network) return (0);        /* Network connection, do nothing */
  1830. #endif /* NETCONN */
  1831.  
  1832.     if (!ttychn) return(-1);        /* Not open. */
  1833.  
  1834. #ifdef COMMENT
  1835. /* ck_cancio() does exactly the same thing */
  1836.     tt_cancel();            /* Cancel outstanding I/O */
  1837. #endif /* COMMENT */
  1838.     msleep(250);            /* Wait for pending i/o to finish. */
  1839.     debug(F101,"ttres, ttychn","",ttychn);
  1840.     if (!CHECK_ERR("ttres: sys$qiow",
  1841.     sys$qiow(QIOW_EFN, ttychn, IO$_SETMODE, &wrk_iosb, 0, 0,
  1842.              &ttold, sizeof(ttold), 0, 0, 0, 0))) return(-1);
  1843.     debug(F100,"ttres still going after sys$qiow","",0);
  1844.     return(0);
  1845. }
  1846.  
  1847. /*  T T B I N  --  Code shared by ttpkt() and ttvt()  */
  1848. /*
  1849.   Puts communication device in "binary" mode.  In VMS there's no distinction
  1850.   between device modes for terminal connection, packet operation, and dialing.
  1851.   BUT THERE SHOULD BE!  We need to get SET CARRIER-WATCH working...
  1852. */
  1853. static int
  1854. ttbin(speed, xflow, xparity) int speed, xflow, xparity; {
  1855.     int s;
  1856.     extern int flow;            /* Global flow control variable */
  1857.  
  1858.     debug(F101,"ttbin entry xparity","",xparity);
  1859.     debug(F101,"ttbin entry xflow","",xflow);
  1860.     debug(F101,"ttbin entry flow","",flow);
  1861.  
  1862.     if (xparity > -1) {
  1863.     ttprty  = xparity;
  1864.     ttpflg  = 0;            /* Parity not sensed yet */
  1865. #ifdef COMMENT
  1866. /*
  1867.   No.  We want the mask applied for file transfer, but not for CONNECT or
  1868.   scripts, because then we lose Telnet negotiations.
  1869. */
  1870.     ttpmsk  = ttprty ? 0177 : 0377;    /* Parity stripping mask */
  1871. #endif /* COMMENT */
  1872.     debug(F101,"ttbin ttprty","",ttprty);
  1873.     }
  1874.  
  1875. #ifdef NETCONN
  1876.     if (network) return(0);        /* Nothing to do on net connections */
  1877. #endif /* NETCONN */
  1878.  
  1879.     if (!ttychn) return(-1);        /* Not open. */
  1880.  
  1881. /* The following applies only to serial ports - causes errors on network */
  1882.  
  1883.     ttdialing = 0;            /* Not dialing */
  1884.     if (xflow != FLO_DIAL && ttflow != FLO_DIAX) {
  1885.     ttflow = xflow;            /* Save for other CKVTIO routines. */
  1886.     } else if (xflow == FLO_DIAL) {    /* We're dialing */
  1887.     ttdialing = 1;            /* Remember */
  1888.     }
  1889.     debug(F101,"ttbin ttdialing","",ttdialing);
  1890.     ttspeed = speed;            /* Keep local copies of arguments */
  1891.     if ((s = ttsspd(speed/10)) < 0)    /* Get internal speed code */
  1892.       s = 0;
  1893.  
  1894. /* Log original terminal settings for debugging purposes */
  1895.  
  1896.     ttraw = ttold;            /* Get a fresh copy of this */
  1897.     debug(F101, "ttbin ttraw.basic 1", "", ttraw.basic);
  1898.     debug(F101, "ttbin ttraw.extended 1", "", ttraw.extended);
  1899. /*
  1900.   Settings based on call parameters flow-control and parity...
  1901.   NOTE: we are using the GLOBAL copy of flow, not our parameter here.
  1902.   This is because the parameter might be FLO_DIAL or FLO_DIALX, which
  1903.   is not flow control at all.
  1904. */
  1905.     if (flow == FLO_XONX) {                /* FLOW = XON/XOFF */
  1906.     debug(F100,"ttbin FLO_XONX","",0);
  1907.     ttraw.basic |=  (TT$M_HOSTSYNC|TT$M_TTSYNC);
  1908.     } else if (flow == FLO_NONE) {            /* FLOW = NONE */
  1909.     debug(F100,"ttbin FLO_NONE","",0);
  1910.     ttraw.basic &= ~(TT$M_HOSTSYNC|TT$M_TTSYNC);
  1911.     } else if (flow == FLO_KEEP) {            /* FLOW = KEEP */
  1912. /*
  1913.  * Put flow-control parameters back the way we found them when
  1914.  * the device was first opened.
  1915.  */
  1916.     if (ttold.basic & TT$M_HOSTSYNC) {
  1917.         ttraw.basic |= TT$M_HOSTSYNC;
  1918.         debug(F100,"ttbin FLO_KEEP HOSTSYNC","",0);
  1919.     } else {
  1920.         ttraw.basic &= ~TT$M_HOSTSYNC;
  1921.         debug(F100,"ttbin FLO_KEEP no HOSTSYNC","",0);
  1922.     }
  1923.     if (ttold.basic & TT$M_TTSYNC) {
  1924.         ttraw.basic |= TT$M_TTSYNC;
  1925.         debug(F100,"ttbin FLO_KEEP TTSYNC","",0);
  1926.     } else {
  1927.         ttraw.basic &= ~TT$M_TTSYNC;
  1928.         debug(F100,"ttbin FLO_KEEP no TTSYNC","",0);
  1929.     }
  1930. /*
  1931.   NOTE: VMS 7.0 supports RTS/CTS.  But how can we include that here
  1932.   portably -- distinction between compile time & run time, etc...
  1933. */
  1934.     }
  1935.     if (parity == 0)
  1936.       ttraw.basic  |= TT$M_EIGHTBIT;    /* Allow 8-bit data if no parity */
  1937.     else                /* Otherwise */
  1938.       ttraw.basic  &= ~TT$M_EIGHTBIT;    /* 7-bit data. */
  1939.     ttraw.basic |= TT$M_NOBRDCST;    /* Turn on no-broadcasts */
  1940.     ttraw.basic |= TT$M_NOECHO;        /* Turn on no-echo */
  1941.     ttraw.basic &= ~TT$M_NOTYPEAHD;    /* Turn off no-type-ahead */
  1942.     ttraw.basic &= ~TT$M_ESCAPE;    /* Turn off escape-seq processing */
  1943.     ttraw.extended &= ~TT2$M_LOCALECHO;    /* Turn off local-echo */
  1944.     ttraw.extended |= TT2$M_PASTHRU;    /* Turn on pass-through mode */
  1945.     ttraw.extended |= TT2$M_ALTYPEAHD;    /* Turn on big type-ahead buffers */
  1946.  
  1947. /* Report what we did so we can check for problems */
  1948.  
  1949.     debug(F101, "ttbin ttraw.basic 2", "", ttraw.basic);
  1950.     debug(F101, "ttbin ttraw.extended 2", "", ttraw.extended);
  1951.  
  1952.     vms_status = sys$qiow(QIOW_EFN, ttychn, IO$_SETMODE, &wrk_iosb, 0, 0,
  1953.              &ttraw, sizeof(ttraw), s, 0, 0, 0);
  1954.     if (!(vms_status & 1))
  1955.       vms_lasterr = vms_status;
  1956. /*
  1957.   On some VMS systems, during remote-mode file transfer, C-Kermit still seems
  1958.   sensitive to Ctrl-C, Ctrl-X, Ctrl-Y, Ctrl-N, and Ctrl-O.  It has been
  1959.   suggested that maybe the following will get rid of at least the ^C and ^Y
  1960.   sensitivity (although it is not clear why the code above did not already
  1961.   do this):
  1962.  
  1963.     vms_status = sys$qiow(QIOW_EFN, ttychn, IO$_SETMODE+IO$M_CTRLCAST,
  1964.                   0, 0, 0, <handler:what-goes-here?>, 0, 0, 0, 0, 0);
  1965.     vms_status = sys$qiow(QIOW_EFN, ttychn, IO$_SETMODE+IO$M_CTRLYAST,
  1966.                   0, 0, 0, <handler:what-goes-here?>, 0, 0, 0, 0, 0);
  1967.  
  1968.   Actually, no -- this just sets up Ctrl-C and Ctrl-Y handlers; it doesn't
  1969.   prevent VMS from trapping them...  Anyway, it's not a problem most places;
  1970.   observed locally only when coming into VMS thru the UCX 2.0 Telnet server.
  1971. */
  1972.     debug(F101,"ttbin sy$qiow","",vms_status);
  1973.     if (vms_status != SS$_NORMAL) {    /* Error queuing request */
  1974.     print_msg("ttbin: sys$qiow");
  1975.     return(-1);
  1976.     }
  1977.     debug(F101,"ttbin iosb status","",wrk_iosb.status);
  1978.     if (wrk_iosb.status != SS$_NORMAL) { /* Error executing request */
  1979.     vms_status = wrk_iosb.status;
  1980.     print_msg("ttbin: sys$qiow(iosb)");
  1981.     return(-1);
  1982.     }
  1983.     debug(F100,"ttbin ok","",0);
  1984.     return(0);                /* All OK */
  1985. }
  1986.  
  1987. /*  T T P K T  --  Condition the communication device for packets. */
  1988.  
  1989. /*  Returns 0 on success, -1 on failure.  */
  1990.  
  1991. int
  1992. ttpkt(speed,flow,parity) long speed; int flow, parity; {
  1993.     int x;
  1994.     debug(F101,"ttpkt flow","",flow);
  1995.     x = ttbin(speed,flow,parity);    /* Put device in binary mode */
  1996.     debug(F101,"ttpkt ttbin","",x);
  1997.     ttpmsk = (ttprty) ? 0x7f : 0xff;    /* Parity stripping mask. */
  1998.     return(x);
  1999. }
  2000.  
  2001. /*  T T V T  --  Condition communication device terminal connection. */
  2002.  
  2003. int
  2004. ttvt(speed,flow) long speed; int flow; {
  2005.     int x;
  2006.     debug(F101,"ttvt flow","",flow);
  2007.     if ((x = ttbin(speed,flow,-1)) > -1) /* Put device in binary mode */
  2008.       tvtflg = 1;
  2009.     debug(F101,"ttvt ttbin","",x);
  2010.     ttpmsk = 0xff;            /* Let CONNECT module handle it */
  2011.     return(x);
  2012. }
  2013.  
  2014. /* T T I S P D  -- Return binary baud rate for internal coded speed */
  2015.  
  2016. int
  2017. ttispd(ispeed) unsigned char ispeed; {
  2018.     int s;
  2019.     long x;
  2020.  
  2021. #ifdef NETCONN
  2022.     if (network) return(-1);
  2023. #endif /* NETCONN */
  2024.  
  2025. /* When the line is set, grab the line speed  and save it */
  2026.  
  2027.     for (s = 0;  ttspeeds[s].dec &&
  2028.     (ttspeeds[s].dec != ispeed);  s++)
  2029.         ;
  2030.  
  2031. /* If speed is zero, then no match.  Set speed to -1 so it is undefined */
  2032.  
  2033.     x = ttspeeds[s].line ? (int) ttspeeds[s].line * 10 : -1;
  2034.     debug(F101,"ttispd","",x);
  2035.     return(x);
  2036. }
  2037.  
  2038. #define NSPDLIST 64
  2039. static long spdlist[NSPDLIST];
  2040.  
  2041. long *
  2042. ttspdlist() {
  2043.     int i; long n;
  2044.  
  2045.     for (i = 0; ttspeeds[i].dec; i++) {
  2046.     n  = ttspeeds[i].line * 10;
  2047.     if (n == 70L)
  2048.       n = 75L;
  2049.     else if (n == 130L)
  2050.       n = 134L;
  2051.     spdlist[i+1] = n;
  2052.     }
  2053.     spdlist[0] = i;
  2054.     return((long *)spdlist);
  2055. }
  2056.  
  2057. /*  T T S S P D  --  Set speed for serial device */
  2058. /*
  2059.   In VMS this routine doesn't actually change the speed; it just checks its
  2060.   argument and then if OK sets a global variable that is used by ttgspd() to
  2061.   say what the current speed is.  The speed is actually changed only by
  2062.   ttbin(), which has its own speed argument.  Note that in VMS there is no
  2063.   way to read the device's current speed (see ttgspd()).
  2064. */
  2065. int
  2066. ttsspd(cps) int cps; {
  2067.     int s;
  2068.     char msg[50];
  2069.  
  2070. #ifdef    NETCONN
  2071.     if (network) return(0);
  2072. #endif    /* NETCONN */
  2073.  
  2074.     if (cps <= 0)            /* 026 Unknown cps fails */
  2075.       return (-1);
  2076.     for (s = 0;  ttspeeds[s].line && (ttspeeds[s].line != cps);  s++) ;
  2077.     if (ttspeeds[s].line) {
  2078.         ttspeed = cps * 10L;        /* Make a copy global to this module */
  2079.     if (ttspeed == 70L)        /* ... in bits per second, not cps, */
  2080.       ttspeed = 75L;        /* because ttgspd() uses it! */
  2081.     debug(F101,"ttsspd","",ttspeed);
  2082.     return(ttspeeds[s].dec);
  2083.     } else {
  2084.     sprintf(msg,"Unsupported line speed - %d\n",cps*10);
  2085.     debug(F101,"ttsspd fails","",cps*10);
  2086.     ermsg(msg);
  2087.     ermsg("Current speed not changed\n");
  2088.     return(-1);
  2089.     }
  2090. }
  2091.  
  2092. /* Interrupt Functions */
  2093.  
  2094. /*  C O N I N T  --  Console Interrupt setter  */
  2095.  
  2096. static SIGTYP (*cctrap)();
  2097.  
  2098. VOID
  2099. #ifdef CK_ANSIC
  2100. conint(SIGTYP (*f)(int), SIGTYP (*s)(int))
  2101. #else
  2102. conint(f,s) int (*f)(int), (*s)(int);
  2103. #endif /* CK_ANSIC */
  2104. /* conint */ {                /* Set an interrupt trap. */
  2105.     /* debug(F101,"conint batch","",batch); */
  2106.  
  2107.     cctrap = f;                /* Make a global copy of handler. */
  2108.     if (batch)                /* Ignore signals in batch */
  2109.       return;                /* or if COMMAND INTERRUPT OFF. */
  2110.     signal(SIGINT,f);            /* Not batch - function to trap to. */
  2111.     conif = 1;                /* Flag console interrupts on. */
  2112. }
  2113.  
  2114. /*  C O N N O I  --  Reset console terminal interrupts */
  2115.  
  2116. VOID
  2117. connoi() {                /* Console-no-interrupts */
  2118.     signal(SIGINT,SIG_IGN);
  2119.     conif = 0;
  2120. }
  2121.  
  2122. /*  T T O L  --  Write string s, length n, to communication device.  */
  2123.  
  2124. #ifndef IO$M_BREAKTHRU
  2125. #define IO$M_BREAKTHRU    0x0200
  2126. #endif /* IO$M_BREAKTHRU */
  2127.  
  2128. #ifndef SS$_EXQUOTA
  2129. #define SS$_EXQUOTA 28
  2130. #endif /* SS$_EXQUOTA */
  2131.  
  2132. int
  2133. ttol(s,n) int n; CHAR *s; {
  2134.     int
  2135.       remaining,            /* Amount left to write */
  2136.       size;                /* How much to write this time */
  2137.     static int max = 0;            /* Chunk size for writing */
  2138.  
  2139. #ifdef NETCONN
  2140.     debug(F101,"ttol network","",network);
  2141.     if (network)            /* If SET HOST connection, */
  2142.     return(nettol(s,n));        /* call network package. */
  2143. #endif /* NETCONN */
  2144.  
  2145. /* It's not a SET HOST connection. */
  2146.  
  2147.     debug(F101,"ttol ttychn","",ttychn);
  2148.     if (!ttychn) return(-1);        /* Not open. */
  2149.     debug(F101,"ttol length","",n);
  2150.     debug(F101,"ttol max","",max);
  2151.  
  2152. /* Have we already calculated a chunk size? */
  2153.  
  2154.     if (max == 0) {            /* No, try to send whole packet. */
  2155.     vms_status = sys$qiow(QIOW_EFN, ttychn, IO$_WRITEVBLK|IO$M_BREAKTHRU,
  2156.                   &wrk_iosb, 0, 0, s, n, 0, 0, 0, 0);
  2157.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  2158.     if (vms_status == SS$_NORMAL) {
  2159.         debug(F101,"ttol 1 ok","",n);
  2160.         return(n);
  2161.     }
  2162. #ifdef DEBUG
  2163.     if (deblog) {            /* Failed. */
  2164.         debug(F101,"ttol 1 error, vms_status","",vms_status);
  2165.         debug(F101,"ttol 1 iosb size","",wrk_iosb.size);
  2166.         debug(F101,"ttol 1 iosb status","",wrk_iosb.status);
  2167.     }
  2168. #endif /* DEBUG */
  2169.     if (vms_status != SS$_EXQUOTA)    /* "Quota exceeded"? */
  2170.       return(-3);            /* No, something else, give up. */
  2171. /*
  2172.   Here we should find out what MAXBUF is (not to mention BYTLM, BIOLM, and
  2173.   friends), and chop up the packet into pieces accordingly.  But reportedly
  2174.   this information, and how to use it (percent overhead, etc), is highly
  2175.   VMS-version-dependent.  So instead we just try different numbers.  Our first
  2176.   attempt keeps dividing it in half until it works, down to about 70.
  2177. */
  2178.     do {
  2179.         max = (max == 0) ? n / 2 : max / 2;
  2180.         debug(F101,"ttol 2 max","",max);
  2181.         vms_status = sys$qiow(QIOW_EFN, ttychn,
  2182.                   IO$_WRITEVBLK|IO$M_BREAKTHRU,
  2183.                   &wrk_iosb, 0, 0, s, max, 0, 0, 0, 0);
  2184.         if (!(vms_status & 1)) vms_lasterr = vms_status;
  2185.           debug(F101,"ttol 2 vms_status","",vms_status);
  2186.         if (vms_status == SS$_NORMAL)
  2187.           break;
  2188.     } while (max > 70);        /* 70 is the minimum. */
  2189.  
  2190.     if (vms_status != SS$_NORMAL)    /* Loop exhausted, fail. */
  2191.       return(-3);
  2192.  
  2193.     } else {                /* We already calculated max. */
  2194.  
  2195.     size = (max > n) ? n : max;      /* Write 1st chunk, but not too much */
  2196.     vms_status = sys$qiow(QIOW_EFN, ttychn, IO$_WRITEVBLK|IO$M_BREAKTHRU,
  2197.                   &wrk_iosb, 0, 0, s, size, 0, 0, 0, 0);
  2198.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  2199.     debug(F101,"ttol 3 vms_status","",vms_status);
  2200.     if (vms_status != SS$_NORMAL)
  2201.       return(-3);
  2202.     if (size == n) {        /* (Not strictly necessary) */
  2203.         debug(F101,"ttol 3 done","",n);
  2204.           return(n);
  2205.     }
  2206.     }
  2207. /*
  2208.   We have written the first chunk successfully, now write the remaining
  2209.   max-sized chunks, plus the (usually) less-than-max-sized last chunk.
  2210. */
  2211.     remaining = n;
  2212.     while (1) {
  2213.     s += max;            /* Where to start */
  2214.     remaining -= max;        /* How much left to write */
  2215.     size = (remaining < max) ?    /* How much to write this time */
  2216.       remaining : max;
  2217.     if (size < 1)            /* Done? */
  2218.       break;
  2219.  
  2220.       debug(F101,"ttol 4 size","",size);
  2221.       vms_status = sys$qiow(QIOW_EFN, ttychn, IO$_WRITEVBLK|IO$M_BREAKTHRU,
  2222.                   &wrk_iosb, 0, 0, s, size, 0, 0, 0, 0);
  2223.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  2224.     debug(F101,"ttol 4 vms_status","",vms_status);
  2225.     if (vms_status != SS$_NORMAL)
  2226.       return(-3);
  2227.     }
  2228.     debug(F101,"ttol 5 done","",n);
  2229.     return(n);
  2230. }
  2231.  
  2232. /*  T T O C  --  Output a character to the communication line  */
  2233.  
  2234. int
  2235. #ifdef CK_ANSIC
  2236. ttoc(char c)
  2237. #else
  2238. ttoc(c) char c;
  2239. #endif    /* CK_ANSIC */
  2240. /* ttoc */ {
  2241. #ifdef NETCONN
  2242.     if (network) {
  2243.     return(nettoc(c));
  2244.     } else {
  2245. #endif /* NETCONN */
  2246.     debug(F101,"ttoc char","",c);
  2247.     if (!ttychn) {
  2248.         debug(F100,"ttoc ttychn not open","",0);
  2249.         return(-1);            /* Not open. */
  2250.     }
  2251.     if (CHECK_ERR("ttoc: sys$qiow",
  2252.               sys$qiow(QIOW_EFN, ttychn, IO$_WRITEVBLK|IO$M_BREAKTHRU,
  2253.                    &wrk_iosb, 0, 0, &c, 1, 0, 0, 0, 0)))
  2254.       return(0);
  2255. #ifdef NETCONN
  2256.     }
  2257. #endif /* NETCONN */
  2258.     return(-1);
  2259. }
  2260.  
  2261. /*  T T _ C A N C E L  --  Cancel i/o on tty channel if not complete  */
  2262.  
  2263. VOID
  2264. tt_cancel() {
  2265.     int mask;
  2266. #ifdef NETCONN
  2267.     if (network) return;
  2268. #endif /* NETCONN */
  2269.     CHECK_ERR("tt_cancel: sys$cancel",sys$cancel(ttychn));
  2270.     tt_queued = 0;
  2271. }
  2272.  
  2273. /*  C O N _ C A N C E L  --  Cancel i/o on console channel if not complete  */
  2274.  
  2275. VOID
  2276. con_cancel() {
  2277.     int mask;
  2278.  
  2279.     CHECK_ERR("con_cancel: sys$cancel",sys$cancel(conchn));
  2280.     con_queued = 0;
  2281. }
  2282.  
  2283. /*  S N D B R K  --  Send a BREAK signal of the given length.  */
  2284.  
  2285. int
  2286. sndbrk(msec) int msec; {
  2287.     int long x = 0;
  2288.     int brklen;
  2289.     struct iosb_struct  tmp_ttiosb;
  2290.     struct tt_mode ttchr;
  2291. #ifndef TT$M_BREAK            /* For old VMS with no BREAK... */
  2292. /*
  2293.   Note: 110 is used instead of 50, because 50 is not supported by all
  2294.   VAX serial port controllers.
  2295. */
  2296. #define BRKSPD = 110            /* Speed for simulating BREAK */
  2297. #define BRKSYM = TT$C_BAUD_110;        /* VMS symbol for this speed */
  2298. #endif /* TT$M_BREAK */
  2299.  
  2300. #ifdef NETCONN
  2301.     if (network)             /* Send network BREAK */
  2302.       return(netbreak());        /* Length doesn't matter */
  2303. #endif /* NETCONN */
  2304.  
  2305.     if (!ttychn) return(-1);        /* SET LINE not done. */
  2306.     debug(F101,"sndbrk msec","",msec);
  2307.  
  2308.     tt_cancel();            /* Cancel I/O */
  2309.  
  2310. #ifndef TT$M_BREAK            /* VMS doesn't have BREAK function */
  2311.  
  2312. /* Send the right number of NULs at BREAK-simulation speed... */
  2313.  
  2314.     brklen = ( BRKSPD * 1000 ) / ( msec * 10 ); /* Calculate number of chars */
  2315.     if (brklen > sizeof(brkarray)) brklen = sizeof(brkarray);
  2316.     debug(F101,"sndbrk speed","",BRKSPD);
  2317.     debug(F101,"sndbrk brklen","",brklen);
  2318.     /* Get current modes */
  2319.     if (!CHECK_ERR("ttsndb: SENSEMODE",
  2320.     sys$qiow(QIOW_EFN, ttychn, IO$_SENSEMODE, &wrk_iosb, 0, 0,
  2321.         &ttchr, sizeof(ttchr), 0, 0, 0, 0))) return(-1);
  2322.     /* Set speed */
  2323.     if (!CHECK_ERR("ttsndb: SETMODE(1)",
  2324.     sys$qiow(QIOW_EFN, ttychn, IO$_SETMODE, &tmp_ttiosb, 0, 0,
  2325.         &ttchr, sizeof(ttchr), BRKSYM, 0, 0, 0))) return(-1);
  2326.     /* Send NULs */
  2327.     if (!CHECK_ERR("ttsndb: writing nulls",
  2328.     sys$qiow(QIOW_EFN, ttychn, IO$_WRITEVBLK|IO$M_BREAKTHRU, &tmp_ttiosb,
  2329.          0, 0, (char *) brkarray, brklen, 0, 0, 0, 0))) return(-1);
  2330.     /* Restore modes */
  2331.     if (!CHECK_ERR("ttsndb: SETMODE(2)",
  2332.     sys$qiow(QIOW_EFN, ttychn, IO$_SETMODE, &tmp_ttiosb, 0, 0,
  2333.         &ttchr, sizeof(ttchr), wrk_iosb.size, 0, 0, 0))) return(-1);
  2334. #else
  2335.     if (!CHECK_ERR("ttsndb: SENSEMODE",
  2336.     sys$qiow(QIOW_EFN, ttychn, IO$_SENSEMODE, &wrk_iosb, 0, 0,
  2337.         &ttchr, sizeof(ttchr), 0, 0, 0, 0))) return(-1);
  2338.     x = TT$M_BREAK;            /* Break signal on */
  2339.     if (!CHECK_ERR("ttsndb: SETMODE(1)",
  2340.     sys$qiow(QIOW_EFN, ttychn, IO$_SETMODE, &wrk_iosb, 0, 0,
  2341.         &ttchr, sizeof(ttchr), 0, 0, x, 0))) return(-1);
  2342.     msleep(msec);            /* Sleep requested amount of time */
  2343.     x = 0;                /* Break signal off */
  2344.     if (!CHECK_ERR("ttsndb: SETMODE(2)",
  2345.     sys$qiow(QIOW_EFN, ttychn, IO$_SETMODE, &wrk_iosb, 0, 0,
  2346.         &ttchr, sizeof(ttchr), 0, 0, x, 0))) return(-1);
  2347. #endif /* TT$M_BREAK */
  2348.     return(0);
  2349. }
  2350.  
  2351. /*  T T S N D B  --  Send a BREAK signal  */
  2352.  
  2353. int
  2354. ttsndb() {
  2355.     return(sndbrk(275));
  2356. }
  2357.  
  2358. /*  T T S N D L B  --  Send a Long BREAK signal  */
  2359.  
  2360. int
  2361. ttsndlb() {
  2362.     return(sndbrk(1500));
  2363. }
  2364.  
  2365. /*  T T H A N G  --  Hang up the communications line  */
  2366. /*
  2367.   Warning: As written, this function DOES NOT WORK on terminal server
  2368.   ports.  This is a shortcoming of VMS, confirmed by the Digital Diagnostic
  2369.   Center (or whatever DDC stands for).  Someone should add code here to test
  2370.   if the ttychn device is not a real terminal, and if so to handle it some
  2371.   other way, like set the speed to zero for a sec, or close and reopen the
  2372.   device.
  2373. */
  2374. int
  2375. tthang() {
  2376.     if (!xlocal) return(0);        /* Only on local connections. */
  2377.  
  2378. #ifdef NETCONN
  2379.     if (network) {            /* Network connection. */
  2380.     return((netclos() < 0) ? -1 : 1); /* Just close it. */
  2381.     }
  2382. #endif /* NETCONN */
  2383.  
  2384.     if (!ttychn) return(0);        /* Not open. */
  2385.  
  2386.     tt_cancel();            /* Cancel pending i/o. */
  2387. /*
  2388.   This is NOT listed in the VMS Terminal Driver as one of the functions
  2389.   that does NOT work with LAT devices.
  2390. */
  2391.     debug(F101,"tthang 1","",gtimer());
  2392.     if (!CHECK_ERR("tthang: sys$qiow",
  2393.     sys$qiow(QIOW_EFN, ttychn, IO$_SETMODE|IO$M_HANGUP, &wrk_iosb, 0, 0,
  2394.         0, 0, 0, 0, 0, 0))) return(-1);
  2395. /*
  2396.   The following 3-second sleep is required because the sys$qiow() returns
  2397.   immediately, about 2.5 seconds before VMS brings DTR back up.  Without this
  2398.   sleep(), DIAL does not work at all if DIAL HANGUP is ON, and, worse,
  2399.   subsequent operations on the device can hang the Kermit process
  2400.   uninterruptibly.
  2401. */
  2402.     sleep(3);
  2403.     debug(F101,"tthang 2","",gtimer());
  2404.     return(1);
  2405. }
  2406.  
  2407. /*  M S L E E P  --  Millisecond version of sleep().  */
  2408.  
  2409. /*
  2410.  Handles intervals up to about 7 minutes (2**32 / 10**7 seconds)
  2411. */
  2412. int
  2413. msleep(m) int m; {
  2414.  
  2415.     struct time_struct {
  2416.     long int hi, lo;
  2417.     } t;
  2418.  
  2419.     if (m <= 0) return(0);
  2420.     t.hi = -10000 * m;  /*  Time in 100-nanosecond units  */
  2421.     t.lo = -1;
  2422.     if (!CHECK_ERR("msleep: sys$schdwk",
  2423.     sys$schdwk(0, 0, &t, 0))) return(-1);
  2424.     sys$hiber();
  2425.     debug(F101,"msleep ok","",m);
  2426.     return(0);
  2427. }
  2428.  
  2429. /*  R T I M E R --  Reset elapsed time counter  */
  2430.  
  2431. VOID
  2432. rtimer() {
  2433.     tcount = time( (TIME_T *) 0);
  2434. }
  2435.  
  2436. /*  G T I M E R --  Get current value of elapsed time counter in seconds  */
  2437.  
  2438. int
  2439. gtimer() {
  2440.     int x;
  2441.     x = (int) (time( (TIME_T *) 0 ) - tcount);
  2442.     return( (x < 0) ? 0 : x );
  2443. }
  2444.  
  2445. #ifdef GFTIMER
  2446. static QUAD tzero;
  2447.  
  2448. VOID
  2449. rftimer() {
  2450.     int status;
  2451.     status = sys$gettim(&tzero);
  2452.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  2453.     debug(F101,"rftimer status","",status);
  2454. }
  2455.  
  2456. /* Floating-point (fractional) timer -- granularity in VMS is ~0.00098 sec. */
  2457.  
  2458. CKFLOAT
  2459. gftimer() {
  2460.     float s;                /* gcc gawks at CKFLOAT */
  2461.     QUAD tnow, diff;            /* 64-bit times */
  2462.     int status;
  2463.     unsigned long lkd = LIB$K_DELTA_SECONDS_F;
  2464. #ifdef DEBUG
  2465.     char fpbuf[64];
  2466. #endif /* DEBUG */
  2467.     status = sys$gettim(&tnow);
  2468.     if (!(status & 1)) vms_lasterr = status;
  2469.     debug(F101,"gftimer status 1","",status);
  2470.     status = lib$sub_times(&tnow, &tzero, &diff );
  2471.     if (!(status & 1)) vms_lasterr = status;
  2472.     debug(F101,"gftimer status 2","",status);
  2473.     status = lib$cvtf_from_internal_time(&lkd,&s,&diff);
  2474.     if (!(status & 1)) vms_lasterr = status;
  2475.     debug(F101,"gftimer status 3","",status);
  2476. #ifdef DEBUG
  2477.     if (deblog) {
  2478.     sprintf(fpbuf,"%f",s);
  2479.     debug(F110,"gftimer s",fpbuf,0);
  2480.     }
  2481. #endif /* DEBUG */
  2482.     return(s > 0.0 ? (CKFLOAT) s : (CKFLOAT) 0.000001);
  2483. }
  2484. #endif /* GFTIMER */
  2485.  
  2486. /*  Z T I M E  --  Return date/time string  */
  2487.  
  2488. VOID
  2489. ztime(s) char **s; {
  2490.     static TIME_T clock;
  2491. #ifdef COMMENT
  2492. #ifdef bogus
  2493.     static char time_string[24];
  2494.     struct dsc$descriptor_s t =
  2495.     {sizeof(time_string)-1,DSC$K_DTYPE_T,DSC$K_CLASS_S,&time_string};
  2496.  
  2497.     if (!CHECK_ERR("ztime: sys$asctim",
  2498.     sys$asctim(0, &t, 0, 0))) return(-1);
  2499.     time_string[t.dsc$w_length] = '\0';
  2500.     *s = &time_string;
  2501. #else
  2502.     char *asctime();
  2503.     struct tm *tp;
  2504.  
  2505.     time(&clock);
  2506.     tp = localtime(&clock);
  2507.     *s = asctime(tp);
  2508. #endif /* bogus */
  2509. #else /* not COMMENT */
  2510. /*
  2511.  Apparently ctime() is available in old C libraries, even though asctime()
  2512.  is not.  Let's use the same method for all versions.
  2513. */
  2514.     time(&clock);
  2515.     *s = ctime(&clock);
  2516. #endif /* COMMENT */
  2517. }
  2518.  
  2519. /*  C O N G M  --  Get console terminal modes.  */
  2520.  
  2521. /*
  2522.  Saves current console mode, and establishes variables for switching between
  2523.  current (presumably normal) mode and other modes.
  2524. */
  2525. int
  2526. congm() {
  2527.     char s[] = CONDEV_COLON;
  2528.     struct itmlst dviitm[] = { {4,DVI$_DEVCLASS,NULL,0}, {0,0,0,0}};
  2529. #ifdef COMMENT /* old */
  2530.     struct dsc$descriptor_s
  2531.       r = {sizeof(s),DSC$K_DTYPE_T,DSC$K_CLASS_S,(char *)&s};
  2532. #else /* from ttj */
  2533.     struct dsc$descriptor_s
  2534.       devnam = {sizeof(s)-1,DSC$K_DTYPE_T,DSC$K_CLASS_S,NULL};
  2535.       devnam.dsc$a_pointer = s;
  2536. #endif /* COMMENT */
  2537.  
  2538.     dviitm[0].adr = (char *)&dviitm[0].adr;
  2539.  
  2540.     debug(F101,"congm cgmf","",cgmf);
  2541.     if (cgmf) return(-1);        /* If called already, then nop */
  2542.  
  2543. #ifndef CK_USEGETJPI
  2544. #ifdef JPI$_MODE            /* I don't know how far back */
  2545. #ifdef JPI$K_BATCH            /* these go... */
  2546. #define CK_USEGETJPI
  2547. #endif /* JPI$K_BATCH */
  2548. #endif /* JPI$_MODE */
  2549. #endif /* CK_USEGETJPI */
  2550.  
  2551. #ifdef CK_USEGETJPI            /* New way works better I think */
  2552.     {                    /* (fdc, Jan 98) */
  2553.     struct itmlstdef {
  2554.         short int buflen;
  2555.         short int itmcod;
  2556.         char *bufaddr;
  2557.         long int *retlen;
  2558.     };
  2559.     long mode_size;
  2560.     unsigned long mode_flags;
  2561.     struct itmlstdef gjiitm[] = { 4, JPI$_MODE, NULL, 0, 0, 0, 0, 0 };
  2562.  
  2563.     gjiitm[0].bufaddr = (char *)&mode_flags;
  2564.     gjiitm[0].retlen = &mode_size;
  2565.     vms_status = sys$getjpiw(0, 0, 0, &gjiitm, 0, 0, 0);
  2566.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  2567.     if (vms_status != SS$_NORMAL) {
  2568.         debug(F101,"congm sys$getjpiw error","",vms_status);
  2569.     } else {
  2570.         debug(F101,"congm sys$getjpiw mode_flags","",mode_flags);
  2571.         if (mode_flags == JPI$K_BATCH)
  2572.           batch = 1;
  2573.         debug(F101,"congm sys$getjpiw batch","",batch);
  2574.     }
  2575.     }
  2576. #else /* Old way */
  2577.     if (!CHECK_ERR("congm: sys$getdviw",
  2578.     sys$getdviw(0, 0, &devnam, &dviitm, &wrk_iosb, 0, 0, 0))) return(-1);
  2579.     debug(F101, "congm sys$getdviw: devclass", "",
  2580.       (unsigned long int) dviitm[0].adr);
  2581.     if ((unsigned long int) dviitm[0].adr != DC$_TERM)
  2582.       batch = 1;
  2583.     debug(F101,"congm sys$getdviw batch","",batch);
  2584. #endif /* COMMENT */
  2585.  
  2586. #ifdef COMMENT                /* Let's try it anyway... */
  2587.       else {
  2588. #endif /* COMMENT */
  2589.     /*
  2590.        NOTE: Reportedly, when C-Kermit is run from a .COM file
  2591.            it complains "Sorry, terminal type not supported: vt300-80".
  2592.            Reportedly, the cure is to execute the following code always,
  2593.            not just when the if condition above is false; i.e. just get
  2594.            rid of {, } else {, and }.  But some of the following system
  2595.            calls look like they might be dangerous on non-terminals,
  2596.            so widespread testing would be needed.  Better safe than sorry.
  2597.         */
  2598.         debug(F101, "congm: conchn", "", conchn);
  2599.     if (!conchn) {            /* Get console channel */
  2600.         $DESCRIPTOR(sys_input, CONDEV_COLON);
  2601.         conchn = vms_assign_channel(&devnam);
  2602.     }
  2603.     if (!conchn)
  2604.       return(-1);
  2605.         if (!CHECK_ERR("congm: sys$qiow",
  2606.         sys$qiow(QIOW_EFN, conchn, IO$_SENSEMODE, &wrk_iosb, 0, 0,
  2607.                  &ccold, sizeof(ccold), 0, 0, 0, 0))) return(-1);
  2608.         ccraw = cccbrk = ccold;
  2609. #ifdef COMMENT
  2610.     }
  2611. #endif /* COMMENT */
  2612.     cgmf = 1;                /* Flag that we got them. */
  2613.     return(0);
  2614. }
  2615.  
  2616. /*  C O N C B --  Put console in cbreak mode.  */
  2617.  
  2618. /*  Returns 0 if ok, -1 if not  */
  2619.  
  2620. int
  2621. #ifdef CK_ANSIC
  2622. concb(char esc)
  2623. #else
  2624. concb(esc) char esc;
  2625. #endif /* CK_ANSIC */
  2626. /* concb */ {
  2627.     int x;
  2628.  
  2629.     debug(F101,"concb batch","",batch);
  2630.     if (batch) {            /* If we're running in batch */
  2631.     extern int bgset;        /* but */
  2632.     if (bgset == 0)            /* the user said SET BACKROUND OFF */
  2633.       ckxech = 1;            /* allow echoing to batch log */
  2634.         debug(F101,"concb 1 ckxech","",ckxech);
  2635.     return(0);
  2636.     }
  2637.     if (!cgmf) congm();            /* Get modes if necessary. */
  2638.     escchr = esc;            /* Make this available to other fns */
  2639.     ckxech = 1;                /* Program can echo characters */
  2640.     debug(F101,"concb 2 ckxech","",ckxech);
  2641. /*
  2642.   Note: PASTHRU / PASSALL is what is preventing the Ctrl-C trap in the
  2643.   main program from working.  This business can be removed without any effect
  2644.   at all on the command parser -- everything still works: completion, ?-help,
  2645.   editing, etc.  The only problem is that Ctrl-Y is not trapped, so the
  2646.   program dies and leaves the terminal in no-echo mode.
  2647. */
  2648.     cccbrk.extended |= TT2$M_PASTHRU | TT2$M_ALTYPEAHD;
  2649.     if (parity)
  2650.       cccbrk.basic |= TT$M_NOECHO;
  2651.     else
  2652.       cccbrk.basic |= TT$M_NOECHO | TT$M_EIGHTBIT;
  2653.     cccbrk.basic &= ~TT$M_ESCAPE;    /* Disable escape-seq processing */
  2654.     cccbrk.extended &= ~TT2$M_LOCALECHO; /* and local echoing */
  2655.     if (!CHECK_ERR("concb: sys$qiow",
  2656.     sys$qiow(QIOW_EFN, conchn, IO$_SETMODE, &wrk_iosb, 0, 0,
  2657.                  &cccbrk, sizeof(cccbrk), 0, 0, 0, 0))) return(-1);
  2658.     debug(F100,"concb ok","",0);
  2659.     return(0);
  2660. }
  2661.  
  2662. /*  C O N B I N  --  Put console in binary mode  */
  2663.  
  2664. /*  Returns 0 if ok, -1 if not  */
  2665.  
  2666. int
  2667. #ifdef CK_ANSIC
  2668. conbin(char esc)
  2669. #else
  2670. conbin(esc) char esc;
  2671. #endif /* CK_ANSIC */
  2672. /* conbin */ {
  2673.  
  2674.     debug(F101,"conbin batch","",batch);
  2675.     if (batch) return(0);
  2676.     if (!cgmf) congm();            /* Get modes if necessary. */
  2677.     escchr = esc;            /* Make this available to other fns */
  2678.     ckxech = 1;                /* Program can echo characters */
  2679.     debug(F101,"conbin ckxech","",ckxech);
  2680.     ccraw.extended |= TT2$M_PASTHRU | TT2$M_ALTYPEAHD;
  2681.     ccraw.basic &= ~TT$M_ESCAPE;    /* Disable escape-seq processing */
  2682.     ccraw.extended &= ~TT2$M_LOCALECHO;    /* and local echoing */
  2683.     ccraw.basic |= TT$M_NOBRDCST;    /* Turn on no-broadcasts */
  2684.     if (parity)
  2685.       ccraw.basic |= TT$M_NOECHO;
  2686.     else
  2687.       ccraw.basic |= TT$M_NOECHO | TT$M_EIGHTBIT;
  2688. #ifdef COMMENT
  2689.     ccraw.basic &= ~(TT$M_HOSTSYNC | TT$M_TTSYNC);
  2690. #endif /* COMMENT */
  2691.     if (!CHECK_ERR("conbin: sys$qiow",
  2692.     sys$qiow(QIOW_EFN, conchn, IO$_SETMODE, &wrk_iosb, 0, 0,
  2693.                  &ccraw, sizeof(ccraw), 0, 0, 0, 0))) return(-1);
  2694.     return(0);
  2695. }
  2696.  
  2697. /*  C O N R E S  --  Restore the console terminal  */
  2698.  
  2699. int
  2700. conres() {
  2701.     debug(F101,"conres cgmf","",cgmf);
  2702.     if (!cgmf) return(0);        /* Do nothing if modes unknown */
  2703.     if (batch) return(0);
  2704.  
  2705.     msleep(250);
  2706.     ckxech = 0;                /* System should echo chars */
  2707.     debug(F101,"conres ckxech","",ckxech);
  2708.     debug(F101,"conres calling sys$qiow","",0);
  2709.     vms_status = sys$qiow(QIOW_EFN, conchn, IO$_SETMODE, &wrk_iosb, 0, 0,
  2710.     &ccold, sizeof(ccold), 0, 0, 0, 0);
  2711.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  2712.     debug(F101,"conres sys$qiow status","",vms_status);
  2713.     if (vms_status == SS$_NORMAL) {
  2714.     debug(F100,"conres ok","",0);
  2715.     return(0);
  2716.     } else if (vms_status == SS$_DEVOFFLINE) {
  2717.     debug(F100,"conres DEVOFFLINE","",0);
  2718.     exit(SS$_ABORT);
  2719.     } else
  2720.       return(-1);
  2721. }
  2722.  
  2723. /*  C O N R E S N E --  Restore the console terminal with No Echo */
  2724.  
  2725. int
  2726. conresne() {
  2727.     debug(F101,"conresne cgmf","",cgmf);
  2728.     if (!cgmf) return(0);        /* Don't do anything if modes unk */
  2729.     if (batch) return(0);
  2730.  
  2731.     msleep(250);
  2732.     ckxech = 1;                /* Program should echo chars */
  2733.     debug(F101,"conresne ckxech","",ckxech);
  2734.     cctmp = ccold;
  2735.     cctmp.basic |= TT$M_NOECHO;
  2736.     if (!CHECK_ERR("conres: sys$qiow",
  2737.     sys$qiow(QIOW_EFN, conchn, IO$_SETMODE, &wrk_iosb, 0, 0,
  2738.     &cctmp, sizeof(cctmp), 0, 0, 0, 0))) return(-1);
  2739.     debug(F100,"conresne ok","",0);
  2740.     return(0);
  2741. }
  2742.  
  2743. /*  C O N O C  --  Output a character to the console terminal  */
  2744.  
  2745. int
  2746. #ifdef CK_ANSIC
  2747. conoc(char c)
  2748. #else
  2749. conoc(c) char c;
  2750. #endif /* CK_ANSIC */
  2751. /* conoc */ {
  2752.     if (batch || !initflg)
  2753.       putchar(c);
  2754.     else if (!CHECK_ERR("conoc: sys$qiow",
  2755.         sys$qiow(QIOW_EFN, conchn, IO$_WRITEVBLK|IO$M_BREAKTHRU,
  2756.              &wrk_iosb, 0, 0, &c, 1, 0, 0, 0, 0)))
  2757.       return(-1);
  2758.     return(1);
  2759. }
  2760.  
  2761. /*  C O N X O  --  Write x characters to the console terminal  */
  2762.  
  2763. int
  2764. conxo(x,s) char *s; int x; {
  2765.     if (batch || !initflg) fprintf(stdout, "%.*s", x, s);
  2766.     else if (!CHECK_ERR("conxo: sys$qiow",
  2767.     sys$qiow(QIOW_EFN, conchn, IO$_WRITEVBLK|IO$M_BREAKTHRU,
  2768.          &wrk_iosb, 0, 0, s, x, 0, 0, 0, 0))) return(-1);
  2769.     return(0);
  2770. }
  2771.  
  2772. /*  C O N O L  --  Write a line to the console terminal  */
  2773.  
  2774. int
  2775. conol(s) char *s; {
  2776.     int len;
  2777.  
  2778.     if (batch || !initflg)
  2779.       fputs(s, stdout);
  2780.     else {
  2781.     len = strlen(s);
  2782.     if (!CHECK_ERR("conol: sys$qiow",
  2783.         sys$qiow(QIOW_EFN, conchn, IO$_WRITEVBLK|IO$M_BREAKTHRU, &wrk_iosb,
  2784.              0, 0, s, len, 0, 0, 0, 0))) return(-1);
  2785.     }
  2786.     return(1);
  2787. }
  2788.  
  2789. /*  C O N O L A  --  Write an array of lines to console, with CRLFs added */
  2790.  
  2791. int
  2792. conola(s) char *s[]; {
  2793.     int i;
  2794.     char t[100], *cp;
  2795.  
  2796.     for (i = 0; *s[i] ; i++) {
  2797.     ckstrncpy(t,s[i],100);
  2798.     for (cp = t + strlen(t); --cp >= t;) {
  2799.         if (*cp != '\n' && *cp != '\r') {
  2800.         cp++;
  2801.         *cp++ = '\r'; *cp++ = '\n'; *cp++ = '\0';
  2802.         break;
  2803.         }
  2804.     }
  2805.     if (conol(t) < 0) return(-1);
  2806.     }
  2807.     return(0);
  2808. }
  2809.  
  2810. /*  C O N O L L  --  Output a string followed by CRLF  */
  2811.  
  2812. int
  2813. conoll(s) char *s; {
  2814.     int x;
  2815.     x = conol(s);
  2816.     if (x > -1)
  2817.       x = conol("\r\n");
  2818.     return(x);
  2819. }
  2820.  
  2821. /*  C O N C H K  --  Check if characters available at console  */
  2822.  
  2823. int
  2824. conchk() {
  2825.     struct {
  2826.     unsigned short count;
  2827.     unsigned char first;
  2828.     unsigned char reserved1;
  2829.     long reserved2;
  2830.     } t;
  2831.  
  2832.     if (batch || !initflg)
  2833.       return(0);
  2834.     return(CHECK_ERR("conchk: sys$qiow",
  2835.     sys$qiow(QIOW_EFN, conchn, IO$_SENSEMODE|IO$M_TYPEAHDCNT, &wrk_iosb,
  2836.          0, 0, &t, sizeof(t), 0, 0, 0, 0)) ? t.count : 0);
  2837. }
  2838.  
  2839. /*  C O N I N C  --  Get a character from the console  */
  2840.  
  2841. int
  2842. coninc(timo) int timo; {        /* Timo > 0 = timeout in seconds. */
  2843.     int n = 0;
  2844.     unsigned char ch;
  2845.     int func, mask;
  2846.  
  2847.     debug(F101,"coninc timo","",timo);
  2848.     debug(F101,"coninc con_queued","",con_queued);
  2849.  
  2850.     if (!itsatty || !initflg)        /* (was "if (batch || !initflg)" */
  2851.       return(getchar());
  2852.  
  2853.     mask = 1 << CON_EFN;
  2854.  
  2855.     if (con_queued) {        /* If a console read was already posted... */
  2856.                 /* e.g. by contti() ... */
  2857.     if (timo > 0) {                /* If a timeout was specified... */
  2858.         struct { int hi, lo; } qtime;   /* Set a timer... */
  2859.         qtime.hi = -10*1000*1000*timo;  /* in VMS "delta-time" notation. */
  2860. /*
  2861.   If the timo value is big enough to make the delta-time overflow an integer,
  2862.   substitute something useful.
  2863. */
  2864.         if (qtime.hi > 0)        /* Did it go positive? */
  2865.           qtime.hi = -0x7fffffff;    /* Yes, so fudge it. */
  2866.         qtime.lo = -1;
  2867.         sys$setimr(TIM_EFN, &qtime, 0, 0, 0); /* Specify event flag. */
  2868.         mask |= TIM_EFN;        /* And add it to read mask. */
  2869.     }
  2870.     sys$wflor(CON_EFN, mask);    /* Wait for SETIMR to complete. */
  2871.     sys$readef(CON_EFN, &mask);    /* Read event flags. */
  2872.     if (mask & (1 << CON_EFN)) {    /* We got a console event? */
  2873.         ch = (unsigned char) conch;    /* (see contti() about this...) */
  2874.         CHECK_ERR("coninc: coniosb.status", coniosb.status);
  2875.         con_queued = 0;
  2876.     } else {            /* We didn't */
  2877.         ch = -1;            /* So indicate that coninc() ... */
  2878.         vms_status = SS$_TIMEOUT;    /*  timed out. */
  2879.     }
  2880.     } else {                /* Console read not already posted */
  2881.     func = IO$_READVBLK | IO$M_NOFILTR;
  2882.     if (timo > 0) func |= IO$M_TIMED;
  2883.     CHECK_ERR("coninc: sys$qiow",
  2884.       sys$qiow(QIOW_EFN, conchn, func, &wrk_iosb,0,0,&ch,1,timo,0,0,0));
  2885.     }
  2886.     if (vms_status & 1) {
  2887.     if (wrk_iosb.status == SS$_TIMEOUT)
  2888.       return(-1);
  2889.     else return((ch == '\r') ? '\n' : (unsigned)ch);
  2890.     } else return(-1);
  2891. }
  2892.  
  2893. /*  V M S _ G E T C H A R -- get a character from the console (no echo).
  2894.  *    Since we use raw reads, we must check for ctrl/c, ctrl/y and
  2895.  *    ctrl/z ourselves.  We probably should post a "mailbox" for
  2896.  *    ctrl/c and ctrl/y so the poor user can abort a runaway Kermit.
  2897.  *    Note: this routine intends for ctrl/z (eof) to be "permanent".
  2898.  *    Currently, no kermit routine calls "clearerror".  If this
  2899.  *    changes, the following code must be rewritten.
  2900.  */
  2901.  
  2902. int
  2903. vms_getchar() {
  2904.     register unsigned int ch;
  2905.     static int ateof = 0;
  2906.  
  2907.     if (ateof)
  2908.       return (EOF);
  2909.     ch = coninc(0);
  2910.     switch (ch) {
  2911.       case ('Y' - 64):
  2912.       case ('C' - 64):
  2913. #ifndef COMMENT
  2914. /*
  2915.   Just call the same handler that signal(SIGINT,xxx) would have invoked
  2916.   if Ctrl-C had been trapped.  The pointer to the handler was saved in
  2917.   cctrap by conint().
  2918. */
  2919.     if (cctrap)
  2920.       (*cctrap)(SIGINT,0);
  2921. #else
  2922.     ttclos(ttyfd);            /* Close down other terminal    */
  2923.     conres();            /* And cleanup console modes    */
  2924.     exit(SS$_ABORT);        /* Fatal exit.            */
  2925. #endif /* COMMENT */
  2926.       case ('Z' - 64):
  2927.     ateof = 1;
  2928.     return (EOF);
  2929.  
  2930.       default:
  2931.     return (ch);
  2932.     }
  2933. }
  2934.  
  2935. /*  C O N T T I  --  Get character from console then from tty  */
  2936. /*
  2937.   This is used in conect() when NO_FORK is defined.
  2938.   src is returned with 1 if the character came from the comm. line,
  2939.   0 if it was from the console, and with -1 if there was any error.
  2940. */
  2941. #ifdef TCPIPLIB
  2942. /*
  2943.  * Network/console read posted?
  2944.  */
  2945. static int    nettty_queued    = 0;
  2946. static int    netcon_queued    = 0;
  2947. #endif /* TCPIPLIB */
  2948.  
  2949. int
  2950. contti(c, src) int *c, *src; {
  2951.  
  2952. #ifndef TCPIPLIB
  2953.     int mask = 1<<CON_EFN | 1<<TTY_EFN;
  2954.     int x; unsigned char cc;
  2955.  
  2956. #else /* TCPIPLIB */
  2957.  
  2958. #ifdef CMU_TCPIP
  2959.     int s;                    /* select status */
  2960.     fd_set exceptfds;                /* select exceptions */
  2961.     fd_set readfds;                /* select read */
  2962.     static struct timeval timeout;        /* for non-blocking select */
  2963. #endif /* CMU_TCPIP */
  2964.  
  2965. #define NET_EFN 7                /* Network event flag */
  2966.  
  2967.     int                mask;        /* Event flag mask */
  2968.  
  2969.     static CHAR            concc;        /* Console and network data */
  2970.     static CHAR            netcc;
  2971.  
  2972.     static struct iosb_struct    net_iosb;
  2973.     static struct iosb_struct    con_iosb;    /* IO status blocks */
  2974.  
  2975. /* Buffered network data, count, next character.  Declared in CKCNET.C ... */
  2976.  
  2977.     extern CHAR            ttibuf[];
  2978.     extern int            ttibn;
  2979.     extern int            ttibp;
  2980. #ifdef NETLEBUF
  2981.     /* See NETLEBUF in ckcnet.h and ckcnet.c */
  2982.     extern int            ttpush, le_data;
  2983. #endif /* NETLEBUF */
  2984. #endif /* TCPIPLIB */
  2985.  
  2986. #ifdef DEBUG
  2987.     if (deblog) {
  2988.     debug(F101,"contti: network","",network);
  2989.     debug(F101,"contti: ttyfd","",ttyfd);
  2990.     debug(F101,"contti: ttychn","",ttychn);
  2991.     }
  2992. #endif /* DEBUG */
  2993.  
  2994.     *src = -1;                /* Assume there was an error */
  2995.  
  2996.     if (network && ttyfd < 0)        /* Make sure we're not called */
  2997.       return(-1);            /* out of context... */
  2998.     if (!network && !ttychn)
  2999.       return(-1);
  3000.  
  3001. #ifdef TCPIPLIB
  3002.     if (network) {            /* For active network connections */
  3003.     debug(F100,"contti: network","",0);
  3004.     debug(F101,"contti: ttibn","",ttibn);
  3005.  
  3006.     if (ttibn > 0 || ttpush > 0 || le_data > 0) {
  3007.         /*
  3008.          * Handle the case where data remains in our "internal" buffer.
  3009.          * We need to:
  3010.          *
  3011.          *    -- Handle the console keyboard (is a character ready?)
  3012.          *    -- Return one character from the network buffer if not
  3013.          *
  3014.          * Post a new console read if necessary
  3015.          */
  3016.             if (!netcon_queued) {
  3017. #ifdef CMU_TCPIP
  3018.             cmu_stdin_read(IO$_READVBLK, &concc, 1, 0, 0);
  3019. #else
  3020.             debug(F100,"contti: sys$qio conchn 1","",0);
  3021.                 if (!CHECK_ERR("contti: console sys$qio",
  3022.                        sys$qio(CON_EFN, conchn, IO$_READVBLK,
  3023.                            &con_iosb, 0, 0, &concc,
  3024.                            1, 0, 0, 0, 0))) {
  3025.                 debug(F100,"contti: sys$qio conchn 1 fails","",0);
  3026.                 return(*c = -1);
  3027.             }
  3028.             debug(F100,"contti: sys$qio conchn 2","",0);
  3029. #endif /* CMU_TCPIP */
  3030.                     netcon_queued = 1;
  3031.         }
  3032.         /* Console char ready? */
  3033. #ifdef CMU_TCPIP
  3034.         FD_ZERO(&exceptfds);
  3035.         FD_SET(0,&exceptfds);
  3036.          FD_ZERO(&readfds);
  3037.         FD_SET(0,&readfds);
  3038. #ifdef CMU_TCPIP_BOGUS_SELECT
  3039.         if (select(1, &readfds, 0, &exceptfds, &timeout) == 1)
  3040.           if (FD_ISSET(0,&exceptfds))
  3041.             return(*c = -1);
  3042.         netcon_queued = 0;
  3043.         *c   = (unsigned)(concc & 0xff);
  3044.         *src = 0;
  3045.         return(1);
  3046. #else    /* CMU_TCPIP_BOGUS_SELECT (wjm 02-feb-1997) */
  3047.         /* non-blocking select() - zero timeout */
  3048.         switch(select(1, &readfds, 0, &exceptfds, &timeout)) {
  3049.           case 1:    /* console ready */
  3050.             netcon_queued = 0;            /* QIO completed */
  3051.             *src = 0;
  3052.             if (!(FD_ISSET(0,&exceptfds))) {    /* o.k. */
  3053.             *c = (unsigned)(concc & 0xff);
  3054.             return(1);
  3055.             } else                /* I/O error */
  3056.             return(*c = -1);
  3057.  
  3058.           case 0:    /* timeout (no console input) */
  3059.             break;
  3060.  
  3061.           default:    /* select() error */
  3062.             return(*c = -1);        /* shouldn't get here */
  3063.         }
  3064. #endif /* CMU_TCPIP_BOGUS_SELECT (wjm 02-feb-1997) */
  3065. #else
  3066.                (void) sys$readef(CON_EFN, &mask);
  3067.             if (mask & (1 << CON_EFN)) {
  3068.             netcon_queued = 0;
  3069.             if (!CHECK_ERR("contti: con_iosb.status",
  3070.                    con_iosb.status)) {
  3071.             debug(F100,"contti: con_iosb.status fails","",0);
  3072.             return(*c = -1);
  3073.             }
  3074.             *c = (unsigned)(concc & 0xff);
  3075.             *src = 0;
  3076.             return(1);
  3077.         }
  3078. #endif /* CMU_TCPIP */
  3079.         /*
  3080.          * No console data; return buffered network character
  3081.          */
  3082. #ifdef NETLEBUF
  3083.         if (ttpush > 0 || le_data > 0) { /* See NETLEBUF in ckcnet.c */
  3084.             *c = netinc(0);    /* See ttbufr() in ckcnet.c */
  3085.         } else {
  3086. #endif /* NETLEBUF */
  3087.             ttibn--;
  3088.             *c   = (unsigned)(ttibuf[ttibp++]);
  3089. #ifdef NETLEBUF
  3090.         }
  3091. #endif /* NETLEBUF */
  3092.         *src = 1;
  3093.         return(1);
  3094.     }
  3095.     /*
  3096.      * No buffered data; post network and console reads
  3097.      */
  3098.     debug(F101,"contti: nettty_queued","",nettty_queued);
  3099.         if (!nettty_queued) {
  3100.  
  3101. /* Another attempt by fdc to catch a broken connection. */
  3102.  
  3103.         int x;
  3104.         x = nettchk();
  3105.         debug(F101,"contti: nettchk","",x);
  3106.         if (x < 0) {
  3107.         return(*src = x);
  3108.         }
  3109. #ifdef CMU_TCPIP
  3110.  
  3111. /* Network read is always posted */
  3112.  
  3113. #else
  3114.  
  3115. /*     -lt.  1992-09-14  begin */
  3116. /* All the event flag numbers should be obtained using lib$get_ef().
  3117.  * Using hard coded numbers, especially < 31 is tres dangereuse!!!
  3118.  * Be careful, one must also change the event flag cluster used by
  3119.  * sys$readef. It is *not* just a simple matter of changing a few #defines.
  3120.  *
  3121.  * At least for DEC TCP/IP Services, socket calls return a proper file
  3122.  * descriptor (fd).  OpenVMS system services require a channel
  3123.  * (from sys$assign).  The two are *not* the same.  The call vaxc$get_sdc()
  3124.  * maps from a DEC TCP/IP fd to a channel.
  3125.  *
  3126.  * This is "gag me with a spoon" code, but it gets thing up and running.
  3127.  *
  3128.  */
  3129. #ifdef DEC_TCPIP
  3130.  
  3131.         {
  3132.         static int last_ttyfd = -1;
  3133.         static short int net_chan = -1;
  3134.  
  3135.         if (ttyfd != last_ttyfd) {
  3136.             last_ttyfd = ttyfd;
  3137. #ifdef COMMENT
  3138. #ifdef __DECC
  3139.             net_chan = (short) decc$get_sdc(last_ttyfd);
  3140. #else
  3141. #ifdef VAXC
  3142.             net_chan = vaxc$get_sdc(last_ttyfd);
  3143. #else
  3144. # error CALL TO GET_SDC requires DECC or VAXC compiler!
  3145. #endif    /* VAXC */
  3146. #endif    /* DECC */
  3147. #else /* COMMENT */
  3148.             net_chan = GET_SDC(last_ttyfd);
  3149. #endif /* COMMENT */
  3150.             }
  3151.  
  3152.         if (!CHECK_ERR("contti: network sys$qio",
  3153.                    sys$qio(NET_EFN, net_chan, IO$_READVBLK,
  3154.                        &net_iosb, 0, 0,
  3155.                        &netcc, 1, 0, 0, 0, 0))) {
  3156.             debug(F100,"contti: network sys$qio net_chan fails","",0);
  3157.             return(*c = -1);
  3158.         }
  3159.         }
  3160.  
  3161. #else /* Not DEC_TCPIP */
  3162.  
  3163.             if (!CHECK_ERR("contti: network sys$qio",
  3164.                sys$qio(NET_EFN, ttyfd, IO$_READVBLK, &net_iosb,
  3165.                    0, 0, &netcc, 1, 0, 0, 0, 0))) {
  3166.         debug(F100,"contti: network sys$qio ttyfd fails","",0);
  3167.         return(*c = -1);
  3168.         }
  3169. #endif /* DEC_TCPIP */
  3170. #endif /* CMU_TCPIP */
  3171.         nettty_queued = 1;
  3172.     }
  3173.         if (!netcon_queued) {
  3174. #ifdef CMU_TCPIP
  3175.         cmu_stdin_read(IO$_READVBLK, &concc, 1, 0, 0);
  3176. #else
  3177.             if (!CHECK_ERR("contti: console sys$qio",
  3178.                sys$qio(CON_EFN, conchn, IO$_READVBLK, &con_iosb,
  3179.                    0, 0, &concc, 1, 0, 0, 0, 0))) {
  3180.         debug(F100,"contti: console sys$qio fails","",0);
  3181.         return(*c = -1);
  3182.         }
  3183. #endif /* CMU_TCPIP */
  3184.             netcon_queued = 1;
  3185.     }
  3186.     /*
  3187.      * Wait for a character
  3188.      */
  3189. #ifdef CMU_TCPIP
  3190.     FD_ZERO(&exceptfds);
  3191.     FD_SET(0,&exceptfds);
  3192.     FD_SET(ttyfd,&exceptfds);
  3193.     FD_ZERO(&readfds);
  3194.     FD_SET(0,&readfds);
  3195.     FD_SET(ttyfd,&readfds);
  3196.     s = select(ttyfd, &readfds, 0, &exceptfds, 0); /*a blocking select*/
  3197.  
  3198.     if (FD_ISSET(0,&exceptfds))
  3199.         return(-1);
  3200.  
  3201.     if (FD_ISSET(ttyfd,&exceptfds))
  3202.         return(-1);
  3203.  
  3204.     if (FD_ISSET(0,&readfds)) {
  3205.         *c         = (unsigned)(concc & 0xff);
  3206.         *src       = 0;
  3207.         netcon_queued = 0;
  3208.     } else {
  3209.         if (FD_ISSET(ttyfd,&readfds)) {
  3210.         s = cmu_read(ttyfd, &netcc, 1);
  3211.         if (s <= 0)
  3212.             return(-1);
  3213.         *c         = (unsigned)(netcc & 0xff);
  3214.         *src       = 1;
  3215.         nettty_queued = 0;
  3216.         }
  3217.     }
  3218. #else
  3219.         mask = (1 << CON_EFN) | (1 << NET_EFN);
  3220.  
  3221.         if (!CHECK_ERR("contti: sys$wflor", sys$wflor(CON_EFN, mask))) {
  3222.         debug(F100,"contti: sys$wflor fails", "", 0);
  3223.         return(*c = -1);
  3224.     }
  3225.         if (!CHECK_ERR("contti: sys$readef",sys$readef(CON_EFN, &mask))) {
  3226.         debug(F100,"contti: sys$readef fails", "", 0);
  3227.         return(*c = -1);
  3228.     }
  3229.         if (mask & (1 << CON_EFN)) {    /* Console */
  3230.             if (!CHECK_ERR("contti: con_iosb.status", con_iosb.status)) {
  3231.         debug(F100,"contti: con_iosb.status fails","",0);
  3232.         return(-1);
  3233.         }
  3234.             *c = (unsigned)(concc & 0xff);
  3235.         *src = 0;
  3236.             netcon_queued = 0;
  3237.  
  3238.         } else if (mask & (1 << NET_EFN)) { /* Network */
  3239.         if (!(net_iosb.status & 1)) { /* Read error */
  3240. #ifdef WINTCP
  3241. #ifdef OLD_TWG
  3242.         perror("contti: net_iosb.status");
  3243. #else
  3244.         _$set_vaxc_error(SS$_NORMAL, net_iosb.status);
  3245.         win$perror("contti: net_iosb.status");
  3246. #endif /* OLD_TWG */
  3247. #else
  3248. #ifdef MULTINET
  3249. #ifdef COMMENT
  3250. /*
  3251.   When user hangs up, this prints an unnecessary scary message,
  3252.   like "Operation would block."
  3253. */
  3254.         socket_perror("contti: net_iosb.status");
  3255. #endif /* COMMENT */
  3256. #endif /* MULTINET */
  3257. #endif /* WINTCP */
  3258.         debug(F100,"contti: network read error", "", 0);
  3259.         return(*c = -1);
  3260.         }
  3261.         debug(F101,"contti: net_iosb.size","",net_iosb.size);
  3262.         if (net_iosb.size == 0) {    /* Handle reset from remote */
  3263.         debug(F100,"contti: network reset from remote", "", 0);
  3264.         return(*c = -1);
  3265.         }
  3266.         *c = (unsigned)(netcc & 0xff);
  3267.         *src = 1;
  3268.             nettty_queued = 0;
  3269.         }
  3270. #endif /* CMU_TCPIP */
  3271.     } else                /* Not network */
  3272. #endif /* TCPIPLIB */
  3273.  
  3274. /*
  3275.   Should we worry about a network connection that's running under batch ?
  3276. */
  3277.     if (batch) {            /* Batch? */
  3278.     debug(F100,"contti batch","",0);
  3279.     if ((*c = getchar()) != EOF) {
  3280.         *src = 0;
  3281.     } else {
  3282.         *src = 1;
  3283.         *c = ttinc(0);
  3284.     }
  3285.     } else {                /* Interactive but not network */
  3286.  
  3287. #ifdef TTXBUF
  3288.     if (ttxbn > 0) {        /* Buffered port chars available */
  3289.  
  3290. /* Post a read on the console if one is not posted already */
  3291.  
  3292.         if (!con_queued) {
  3293.         if (!CHECK_ERR("contti: console sys$qio",
  3294.                  sys$qio(CON_EFN, conchn, IO$_READVBLK,
  3295.                      &coniosb, 0, 0,
  3296.                      &conch, 1, 0, 0, 0, 0)))
  3297.           return(-1);
  3298.         con_queued = 1;
  3299.         }
  3300.  
  3301. /* See if a console character has been read and if so, return it.  */
  3302.  
  3303.         (void) sys$readef(CON_EFN, &mask);
  3304.         if (mask & (1 << CON_EFN)) {
  3305.         con_queued = 0;
  3306.         if (!CHECK_ERR("contti: coniosb.status",
  3307.                    coniosb.status))
  3308.           return(-1);
  3309.         *c   = (unsigned)(conch & 0xff);
  3310.         *src = 0;
  3311.         return(1);
  3312.         }
  3313.  
  3314. /* No console character, so return buffered port character */
  3315.  
  3316.         *c = ttinc(0);
  3317.         *src = 1;
  3318.         return(1);
  3319.     }
  3320.  
  3321. /* No buffered port data; post both network and console reads... */
  3322.  
  3323. #endif /* TTXBUF */
  3324.  
  3325.         mask = 1<<CON_EFN | 1<<TTY_EFN;    /* Event mask */
  3326.  
  3327.     debug(F101,"contti interactive mask","",mask);
  3328.  
  3329.         if (!con_queued) {        /* Console read not queued... */
  3330.             if (!CHECK_ERR("contti: console sys$qio",
  3331.             sys$qio(CON_EFN, conchn, IO$_READVBLK, &coniosb, 0, 0,
  3332.                     &conch, 1, 0, 0, 0, 0))) return(-1);
  3333.         con_queued = 1;
  3334.         debug(F100,"contti con_queued","",0);
  3335.     }
  3336.         if (!tt_queued) {        /* Port read not queued */
  3337.         ttch = -9;            /* Impossible value */
  3338.             if (!CHECK_ERR("contti: tty sys$qio",
  3339.                sys$qio(TTY_EFN, ttychn, IO$_READVBLK,
  3340.                    &ttiosb, 0, 0, &ttch, 1, 0, 0, 0, 0)))
  3341.           return(-1);
  3342.         tt_queued = 1;
  3343.         debug(F100,"contti tt_queued","",0);
  3344.     }
  3345.  
  3346. /* Wait for one of the queued reads to complete */
  3347.  
  3348.         if (!CHECK_ERR("contti: sys$wflor",
  3349.             sys$wflor(CON_EFN, mask))) return(-1);
  3350.     debug(F100,"contti sys$wflor ok","",0);
  3351.  
  3352. /* Read the event flags to see which read was completed */
  3353.  
  3354.         if (!CHECK_ERR("contti: sys$readef",
  3355.             sys$readef(CON_EFN, &mask))) return(-1);
  3356.     debug(F100,"contti sys$readef ok","",0);
  3357.  
  3358. /* Return the character with the appropriate source (src) indicator */
  3359.  
  3360.         if (!(*src = ((mask & 1<<CON_EFN) ? 0 : 1))) {
  3361.             *c = (unsigned)(conch);
  3362.             CHECK_ERR("contti: coniosb.status", coniosb.status);
  3363.             con_queued = 0;
  3364.         } else {
  3365.             *c = (ttprty ? (unsigned)(ttch & 0177) : (unsigned)ttch);
  3366.         if (ttiosb.status == SS$_HANGUP) {
  3367.         debug(F101,"contti hangup ttch","",ttch);
  3368.         if (ttcarr != CAR_OFF) {
  3369.             xhangup = 1;
  3370.             fprintf(stderr,"\n%%CKERMIT-F-HANGUP, data set hang-up");
  3371.             *src = -1;
  3372.             return(1);
  3373.         } else if (ttch == -9) {
  3374.             tt_queued = 0;
  3375.             *src = -2;
  3376.             return(1);
  3377.         }
  3378.         } else if (ttiosb.status != SS$_HANGUP) {
  3379.         CHECK_ERR("contti: ttiosb.status", ttiosb.status);
  3380.         }
  3381.             tt_queued = 0;
  3382.         }
  3383.         if (!(vms_status & 1)) *src = -1;
  3384.     }
  3385.     debug(F101,"contti *src","",*src);
  3386.     return((*src > -1) ? 1 : 0);
  3387. }
  3388.  
  3389. /*
  3390.   C A N C I O
  3391.   Cancel pending I/O requests on console and communication device.
  3392. */
  3393.  
  3394. int
  3395. ck_cancio() {
  3396.  
  3397.     debug(F101,"ck_cancio: ttyfd","",ttyfd);
  3398.     if (network && ttyfd < 0)
  3399.       return(0);
  3400.     debug(F101,"ck_cancio: ttychn","",ttychn);
  3401.     if (!network && !ttychn)
  3402.       return(0);
  3403. #ifdef NETCONN
  3404.     if (network) {
  3405. #ifdef TCPIPLIB
  3406. #ifdef DEC_TCPIP
  3407.     short int net_chan = -1;
  3408.  
  3409.     net_chan = GET_SDC(ttyfd);
  3410.         if (nettty_queued) (void) sys$cancel(net_chan);
  3411.     nettty_queued = 0;        /*+wjm*/
  3412. #else  /* DEC_TCPIP */
  3413. #ifdef CMU_TCPIP
  3414.     /* not going to do this when CMU is the network transport.
  3415.      * the sys$cancel will cause the channel to shutdown
  3416.      *
  3417.      * if (nettty_queued) (void) sys$cancel(cmu_get_sdc(ttyfd));
  3418.      */
  3419. #else  /* !CMU_TCPIP */
  3420.        if (nettty_queued) (void) sys$cancel(ttyfd);
  3421.     nettty_queued = 0;        /*+wjm*/
  3422. #endif /* CMU_TCPIP */
  3423. #endif /* DEC_TCPIP */
  3424.         if (netcon_queued) (void) sys$cancel(conchn);
  3425.         netcon_queued = 0;
  3426.     return(0);
  3427. #else /* Not TCPIPLIB */
  3428.     return(0);
  3429. #endif /* TCPIPLIB */
  3430.     }
  3431. #endif /* NETCONN */
  3432.  
  3433.     if (!batch) {
  3434.         CHECK_ERR("ck_cancio: console sys$cancel",
  3435.             sys$cancel(conchn));
  3436.         CHECK_ERR("ck_cancio: tty sys$cancel",
  3437.             sys$cancel(ttychn));
  3438.         con_queued = 0;
  3439.         tt_queued = 0;
  3440.     }
  3441.     return(0);
  3442. }
  3443.  
  3444. /* get_qio_maxbuf_size()
  3445.  *
  3446.  * Get maximum size of QIO that can occur without getting the dreaded
  3447.  * exceeded quota status.
  3448.  */
  3449.  
  3450. #ifndef SYI$_MAXBUF
  3451. #define SYI$_MAXBUF 4175
  3452. #endif /* SYI$_MAXBUF */
  3453.  
  3454. int
  3455. get_qio_maxbuf_size(ttychn) unsigned long int ttychn; {
  3456.     unsigned char *tmpbuf;
  3457.     int unsigned long max=0;
  3458.     struct itmlst syiitm[] = { {2,SYI$_MAXBUF,NULL,0},
  3459.             {0,0,0,0}};
  3460.  
  3461.     syiitm[0].adr = (char *)&max;
  3462.  
  3463.     if (!ttychn) return(-1);
  3464.  
  3465.     if (!CHECK_ERR("get_qio_maxbuf_size: sys$getsyiw",
  3466.     sys$getsyiw(     0    /* efn */
  3467.             ,0    /* csidadr */
  3468.             ,0    /* nodename */
  3469.             ,&syiitm /* itmlst */
  3470.             ,&wrk_iosb /* iosb */
  3471.             ,0    /* astadr */
  3472.             ,0)))    /* astprm */
  3473.         exit(SS$_ABORT);        /* Fatal exit */
  3474.  
  3475.     if (!(tmpbuf = malloc(max)))
  3476.     return(0);
  3477.  
  3478.     for (; max > 0; max -= 16) {
  3479.     if (!test_qio(ttychn,max,tmpbuf)) /* (was &tmpbuf, caused crash) */
  3480.     {
  3481.         free(tmpbuf);
  3482.         return(max);
  3483.     }
  3484.     }
  3485.  
  3486.     free(tmpbuf);
  3487.     printf("\n%%CKERMIT-F-get_qio_maxbuf_size, Could not get maxbuf size\n");
  3488.     exit(SS$_ABORT);        /* Fatal exit */
  3489. }
  3490.  
  3491. int
  3492. test_qio(ttychn,max,dest)
  3493. unsigned long int ttychn;
  3494. long int max;
  3495. unsigned char *dest;
  3496. {
  3497.     static int trmmsk[2] = {0,0};
  3498.  
  3499. /*    trmmsk[1] = 1 << eol; */
  3500.  
  3501.     vms_status = sys$qiow(QIOW_EFN, ttychn, IO$_READVBLK|IO$M_TIMED,
  3502.               &wrk_iosb, 0, 0, dest, max, 0, &trmmsk, 0, 0);
  3503.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  3504.     return( !(vms_status & 1) ||
  3505.     (!(wrk_iosb.status & 1)) && wrk_iosb.status != SS$_TIMEOUT);
  3506. }
  3507.  
  3508.  
  3509. /* Flush communications device output buffer */
  3510.  
  3511. int
  3512. ttfluo() {
  3513.  
  3514.     long n=0;
  3515.  
  3516. #ifdef NETCONN
  3517.     if (network) return(0);
  3518. #endif /* NETCONN */
  3519.  
  3520.     if (!ttychn) return(-1);        /* Not open. */
  3521.  
  3522.     if (!CHECK_ERR("ttfluo: sys$qiow",
  3523.     sys$qiow(QIOW_EFN, ttychn, IO$_READVBLK|IO$M_TIMED|IO$M_PURGE,
  3524.          &wrk_iosb, 0, 0, &n, 0, 0, 0, 0, 0))) {
  3525.     perror("flush failed");
  3526.     return(-1);
  3527.     }
  3528.     return(0);
  3529. }
  3530.  
  3531. /*  T T G M D M  --  Get modem signals  */
  3532. /*
  3533.  Looks for the modem signals CTS, DSR, and CTS, and returns those that are
  3534.  on in as its return value, in a bit mask as described for ttwmdm.  Returns:
  3535.  -3 Not implemented
  3536.  -2 if the line does not have modem control
  3537.  -1 on error.
  3538.  >= 0 on success, with a bit mask containing the modem signals that are on.
  3539. */
  3540. int
  3541. ttgmdm() {
  3542.     struct {
  3543.     unsigned char type;
  3544.     unsigned char spare1;
  3545.     unsigned char modem;
  3546.     unsigned char spare2;
  3547.     unsigned long filler;
  3548.     } mdminfo;
  3549.     int retval;
  3550.  
  3551. #ifdef NETCONN
  3552.     if (network) return(-2);
  3553. #endif /* NETCONN */
  3554. /*
  3555.   According to the VMS I/O manual, devices that do not have the TT$M_MODEM
  3556.   characteristics can have their modem signals read using IO$M_MAINT.
  3557. */
  3558.     /* This does not work on inbound ports or on LAT devices */
  3559.     vms_status = sys$qiow(QIOW_EFN, ttychn, IO$_SENSEMODE|IO$M_RD_MODEM,
  3560.               &wrk_iosb, 0, 0, &mdminfo, 0, 0, 0, 0, 0);
  3561.     if (!(vms_status & 1)) vms_lasterr = vms_status;
  3562.     if (vms_status != SS$_NORMAL) {
  3563.     debug(F101,"ttgmdm serious error, status","",vms_status);
  3564.     debug(F101,"ttgmdm ttychn","",ttychn);
  3565.     return(-1);
  3566.     }
  3567.     debug(F101,"ttgmdm iosb","",wrk_iosb.status);
  3568.     debug(F101,"ttgmdm type","",mdminfo.type);
  3569.     debug(F101,"ttgmdm modem","",mdminfo.modem);
  3570.  
  3571.     if (wrk_iosb.status != SS$_NORMAL) {
  3572.     debug(F101,"ttgmdm iosb error, status","",wrk_iosb.status);
  3573.     return(-1);
  3574.     }
  3575.  
  3576. #ifdef DT$_LAT
  3577.     if (mdminfo.type == DT$_LAT) {
  3578.     debug(F101,"ttgmdm LAT port, no modem control","",0);
  3579.     return(-2);
  3580.     }
  3581. #endif /* DT$_LAT */
  3582.  
  3583. #ifndef __ALPHA                /* This works only on VAX */
  3584. /*
  3585.   It was suggested that we test for TT$M_MODEM here, but if it failed
  3586.   that does not necessarily mean we can't read modem signals.  As of
  3587.   now (12/1997) no reliable test is known that tells whether we can believe
  3588.   the mdminfo.modem bits.  But hopefully we won't get this far in that case.
  3589. */
  3590.     if (mdminfo.type == 0) {
  3591.     debug(F101,"ttgmdm unknown driver, modem","",mdminfo.modem);
  3592.     return(-2);
  3593.     }
  3594. #endif /* __ALPHA */
  3595.  
  3596.     retval = BM_DTR | BM_RTS;        /* Not visible, set by TTDRIVER */
  3597.     if (mdminfo.modem & TT$M_DS_CTS)
  3598.     retval |= BM_CTS;
  3599.     if (mdminfo.modem & TT$M_DS_DSR)
  3600.     retval |= BM_DSR;
  3601.     if (mdminfo.modem & TT$M_DS_CARRIER)
  3602.     retval |= BM_DCD;
  3603.     if (mdminfo.modem & TT$M_DS_RING)
  3604.     retval |= BM_RNG;
  3605.     return(retval);
  3606. }
  3607.  
  3608. long
  3609. congspd() {
  3610.     return(conspd);
  3611. }
  3612.  
  3613. /*
  3614.   Return serial communication device speed.  The speed returned is simply
  3615.   the last one set.  Why?  Because VMS apparently provides no method to
  3616.   query the speed.
  3617. */
  3618. long
  3619. ttgspd() {
  3620.     int i;
  3621.     extern int speed;
  3622.     unsigned int vms_speed, s1, s2;
  3623.     long x;
  3624.  
  3625. #ifdef COMMENT
  3626.     struct tt_mode ttmodes;
  3627. #endif /* COMMENT */
  3628.  
  3629.     debug(F101,"ttgspd ttspeed","",ttspeed);
  3630.     debug(F101,"ttgspd network","",network);
  3631.  
  3632. #ifdef NETCONN
  3633.     if (network) return(-1);        /* -1 if network connection */
  3634. #endif /* NETCONN */
  3635.  
  3636. #ifdef COMMENT
  3637. /*
  3638.   This looks like it should work and in fact it does -- but it returns
  3639.   the NOMINAL (permanent) speed of the device, not the current speed.
  3640.   So it's useless.  If there is a way to get the actual current speed,
  3641.   I'd like to know what it is.
  3642. */
  3643.     vms_status = sys$qiow(QIOW_EFN, ttychn, IO$_SENSEMODE, &wrk_iosb, 0, 0,
  3644.               &ttmodes, sizeof(ttmodes), 0, 0, 0, 0);
  3645.     if (vms_status != SS$_NORMAL) {
  3646.     debug(F101,"ttgspd: sys$qiow error","",vms_status);
  3647.     return(-1);
  3648.     }
  3649.     if (wrk_iosb.status != SS$_NORMAL) { /* Error executing request */
  3650.     vms_status = wrk_iosb.status;
  3651.     debug(F101,"ttgspd: sys$qiow iosb error","",wrk_iosb.status);
  3652.     return(-1);
  3653.     }
  3654.     x = ttispd((unsigned char)wrk_iosb.size);
  3655.     if (x > 0)
  3656.       ttspeed = x;
  3657.     else
  3658. #endif /* COMMENT */
  3659.       x = ttspeed;
  3660.     debug(F101,"ttgspd returns","",x);
  3661.     return(x);
  3662. }
  3663.  
  3664. /*  T T S C A R R  --  Set ttcarr variable, controlling carrier handling.
  3665.  *
  3666.  *  0 = Off: Always ignore carrier. E.g. you can connect without carrier.
  3667.  *  1 = On: Heed carrier, except during dialing. Carrier loss gives disconnect.
  3668.  *  2 = Auto: For "modem direct": The same as "Off".
  3669.  *            For real modem types: Heed carrier during connect, but ignore
  3670.  *                it anytime else.  Compatible with pre-5A C-Kermit versions.
  3671.  *
  3672.  * As you can see, this setting does not affect dialing, which always ignores
  3673.  * carrier (unless there is some special exception for some modem type).  It
  3674.  * does affect ttopen() if it is set before ttopen() is used.  This setting
  3675.  * takes effect on the next call to ttopen()/ttpkt()/ttvt().  And they are
  3676.  * (or should be) always called before any communications is tried, which
  3677.  * means that, practically speaking, the effect is immediate.
  3678.  * Of course, nothing of this applies to remote mode (xlocal = 0).
  3679.  */
  3680. int
  3681. ttscarr(carrier) int carrier; {
  3682.     ttcarr = carrier;
  3683.     debug(F101, "ttscarr","",ttcarr);
  3684.     return(ttcarr);
  3685. }
  3686.  
  3687. int
  3688. psuspend(x) int x; {
  3689.  
  3690.     return(-1);
  3691. }
  3692.  
  3693. int
  3694. vmsttyfd() {
  3695.     return(network ? ttyfd : ttychn);
  3696. }
  3697.  
  3698. #ifdef CK_CURSES
  3699. /*
  3700.   tgetent() support for VMS curses emulation.
  3701.   Used by all three VMS fullscreen methods.
  3702.   Called from "SET FILE DISPLAY FULLSCREEN" in ckuus7.c.
  3703. */
  3704. int isvt52 = 0;                /* VT52/VT1xx flag */
  3705.  
  3706. int
  3707. tgetent(lp, term) char *lp, *term; {
  3708.     debug(F101,"tgetent terminal type","",ccold.type);
  3709.     debug(F101,"tgetent terminal extended","",ccold.extended);
  3710.  
  3711.     if ((ccold.type == DT$_VT5X) || (ccold.type == DT$_VT55)) {
  3712.     debug(F100,"tgetent VT5x","",0);
  3713.     isvt52 = 1;
  3714.     return(1);
  3715.     }
  3716.     if ((ccold.extended & TT2$M_ANSICRT) == TT2$M_ANSICRT) {
  3717.     debug(F100,"tgetent ANSICRT","",0);
  3718.     isvt52 = 0;
  3719.     return(1);
  3720.     }
  3721.     if ((ccold.extended & TT2$M_DECCRT) == TT2$M_DECCRT) {
  3722.     debug(F100,"tgetent DECCRT","",0);
  3723.     isvt52 = 0;
  3724.     return(1);
  3725.     }
  3726.     return(0);                /* Not a supported terminal type */
  3727. }
  3728. #endif /* CK_CURSES */
  3729.