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