⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connect.c

📁 dtelent是开源的开发项目
💻 C
📖 第 1 页 / 共 2 页
字号:
/* connect.c
 * Copyright (C) 1998 David Cole
 *
 * Manage connect dialog, connection history, and connect parameters.
 */
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "platform.h"
#include "resource.h"
#include "utils.h"
#include "emul.h"
#include "connect.h"
#include "argv.h"
#include "dtelnet.h"
#include "socket.h"
#include "term.h"
#include "raw.h"
#include "dialog.h"
#include "status.h"
#include "dtchars.h"

extern EmulNames emuls;

static BOOL haveDialog;		/* have we created the Connect dialog? */
/* static connectDlgExitProc DlgOnExit;    exit */

static Connect* history;	/* connection history */
static int historySize;		/* number of entries in the history */
static int activeHistoryIdx = -1; /* current history entry in use */

static const Connect defVars = {
    "",				/* default host (none) */
    "telnet",			/* default port */
    "telnet",			/* default protocol */
    "linux",                    /* default terminal emulation */
    "",				/* default user (none) */
    FALSE,			/* default bs2del state */
    "ANSI",			/* default server charset */
    "dtelnet from Dave Cool",   /* answerback */
    22                          /* length of answerback */
};				/* default variables */
static Connect dlgVars;		/* current dialog variables */
static BOOL exitOnDisconnect;	/* Exit when the socket disconnects? */

extern struct Protocols protocols[];
extern unsigned int numProtocols;

/* Identifiers used in the .INI file that are related to connections.
 */
static char connectStr[] = "Connect";
static char hostStr[] = "Host";
static char portStr[] = "Port";
static char protocolStr[] = "Protocol";
static char termStr[] = "Term";
static char userStr[] = "User";
static char numStr[] = "History Len";
static char historyStr[] = "History %d";
static char exitStr[] = "Exit On Disconnect";
static char abackStr [] = "AnswerBack";
/* Fred */
static char bs2DelStr[] = "BackspaceToDelete";
static char charsetStr[] = "ServerCharSet";

/* Add the dlgVars session to the connection history.  If the session
 * is already in the history, do nothing.  When this is a new session,
 * add it to the top.
 */
static void addConnectHistory(void)
{
    int idx;			/* count sessions */
    Connect* connect;		/* session iterator */

    /* If already in the history - refresh it
     */
    for (idx = 0, connect = history; idx < historySize; ++idx, ++connect) {
	if (stricmp(connect->host, dlgVars.host) == 0
	    && stricmp(connect->port, dlgVars.port) == 0) {
	    memcpy(connect, &dlgVars, sizeof(*connect));
	    return;
	}
    }
    /* Add to head of the history
     */
    if (history == NULL) {
	history = (Connect *)xmalloc(MAX_HISTORY * sizeof(*history));
	historySize = 1;
    } else {
	if (historySize != MAX_HISTORY)
	    ++historySize;
	memmove(&history[1], &history[0],
		(historySize - 1) * sizeof(*history));
    }

    memcpy(&history[0], &dlgVars, sizeof(*history));
}

/* Format the specified session and return result as string
 *
 * Args:
 * idx -     index of session to be formatted
 * forMenu - format as menu item?
 *
 * Return pointer to static formatted string.
 */
char *connectGetHistory(char format[MAX_HISTORY_SIZE], int idx, BOOL forMenu)
{
    char *p;
    Connect* connect;		/* session to be formatted */

    if (idx >= historySize)
	return NULL;
    connect = &history[idx];

    if (forMenu)
	/* Format for menu entry, eg. "&1 term1/telnet"
	 */
	sprintf(format, "&%d %s/%s",
		idx + 1,
		connect->host, connect->port);
    else {
	/* Format for .INI file, eg. "/Hterm1 /Ptelnet /Stelnet /Tlinux /Exterm"
	 */
	p = format;
	p += sprintf(p, "/H%s /P%s /S%s /T%s",
		connect->host, connect->port,
		connect->protocol, connect->term);
	if (connect->user[0] != '\0')
	    p += sprintf(p, " /U%s", connect->user);
	/* Fred */
	if (connect->bs2Del) {
	    strcpy(p, " /D");
	    p += 3;
	}
	p += sprintf (p, " /C%s", connect->charset);
	p += sprintf (p, " /%s=", abackStr);
	*p++ = '"';
	if (connect->abacklen) {
	    p += escape (connect->answerback, connect->abacklen,
			 p, 128);
	}
	*p++ = '"';
	*p = '\0';
    }
    return format;
}

/* Remember the terminal type used with the current session, if any
 *
 * Args:
 * termName - terminal type used
 */
void connectRememberTerm(const char* termName)
{
    Connect* active;

    if (activeHistoryIdx < 0
	|| activeHistoryIdx >= historySize)
	return;

    active = history + activeHistoryIdx;
    strncpy(active->term, termName, sizeof(active->term));
    active->term[sizeof(active->term) - 1] = '\0';
}

/* Open a connection to the host/port currently specified in the dialog
 */
void connectOpenHost()
{
    socketConnect(dlgVars.host, dlgVars.port);
    /* Reset the application window title
     */
    termSetTitle("");
    /* Set connection options
     */
    emulSetTerm(dlgVars.term);
    emulResetTerminal();
    emulSetCharset(dlgVars.charset);
    emulSetAnswerBack (dlgVars.abacklen, dlgVars.answerback);

    dtcEC_CheckMenuItem(dlgVars.charset);
    rawEnableProtocol(findPortProtocol(dlgVars.protocol));
    rawSetTerm();
    /* Indicate the terminal type emulated
     */
    statusSetTerm();
}

/* Connect to the specified historic session
 *
 * Args:
 * idx - index of historic session to connect to
 */
void connectHistory(int idx)
{
    if (idx >= historySize)
	return;
    memcpy(&dlgVars, &history[idx], sizeof(dlgVars));
    activeHistoryIdx = idx;
    connectOpenHost();
}

char* connectGetHost()
{
    return dlgVars.host;
}
char* connectGetPort()
{
    return dlgVars.port;
}
char* connectGetProtocol()
{
    return dlgVars.protocol;
}
/* Return the terminal type of the current session
 */
char* connectGetTerm()
{
    return dlgVars.term;
}

/* Return the user name in the current session
 */
char* connectGetUser()
{
    return dlgVars.user;
}

/* Fred. Return the bs2Del option in the current session
 */
BOOL connectGetBs2DelOption()
{
    return dlgVars.bs2Del;
}

/* Return server-character-set */
char *connectGetServerCharSet(void)
{
    return dlgVars.charset;
}

/* Set Server CharacterSet */
void connectSetServerCharSet(char *set)
{
    strcpy (dlgVars.charset, set);
}

/* Return the state of exit-on-disconnect
 */
BOOL connectGetExitOnDisconnect()
{
    return exitOnDisconnect;
}

/* Initialize connect dialog controls from a Connect structure
 */
void connectDlgSetVars(HWND dlg, Connect *conn)
{
    LRESULT res;
    unsigned int idx;

    SetDlgItemText(dlg, IDC_HOSTNAME, conn->host);
    SetDlgItemText(dlg, IDC_PORT, conn->port);
    for (idx = 0; idx < numProtocols; idx++) {
	SendDlgItemMessage(dlg, IDC_PROTOCOL, CB_ADDSTRING, 0, (LPARAM)protocols[idx].name);
    }
    if (conn->protocol[0] == '\0') {
	strcpy(conn->protocol, protocols[protoNone].name);
    }
    res = SendDlgItemMessage(dlg, IDC_PROTOCOL, CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)(LPSTR) conn->protocol);
    if (res != CB_ERR) {
	SendDlgItemMessage(dlg, IDC_PROTOCOL, CB_SETCURSEL, (WPARAM) res, 0);
    }
    EnableWindow(GetDlgItem(dlg, IDC_USER), stricmp(conn->protocol, protocols[protoRlogin].name) == 0);
    telnetEnumTermProfiles(&emuls);
    for (idx = 0; idx < emuls.num; idx++) {
	SendDlgItemMessage(dlg, IDC_TERM, CB_ADDSTRING, 0, (LPARAM)emuls.names[idx]);
    }
    res = SendDlgItemMessage(dlg, IDC_TERM, CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)(LPSTR)conn->term);
    if (res != CB_ERR) {
	SendDlgItemMessage(dlg, IDC_TERM, CB_SETCURSEL, (WPARAM)res, 0);
    }
    SetDlgItemText(dlg, IDC_USER, conn->user);
    /* Fred */
    SendDlgItemMessage(dlg, IDC_BS2DEL, BM_SETCHECK, conn->bs2Del, 0);
    dtcEC_NamesToCombo(GetDlgItem(dlg, IDC_CHARSET));
    SendDlgItemMessage(dlg, IDC_CHARSET, CB_SELECTSTRING, 0, (LPARAM)(LPSTR)conn->charset);
}

/* Save dialog controls values into a Connect structure
 */
void connectDlgGetVars(HWND dlg, Connect *conn)
{
    LRESULT res;

    GetDlgItemText(dlg, IDC_HOSTNAME, conn->host, HOST_NAME_LEN);
    GetDlgItemText(dlg, IDC_PORT, conn->port, PORT_NAME_LEN);
    res = SendDlgItemMessage(dlg, IDC_PROTOCOL, CB_GETCURSEL, 0, 0);
    if (res != CB_ERR) {
	SendDlgItemMessage(dlg, IDC_PROTOCOL, CB_GETLBTEXT, (WPARAM)res, (LPARAM)(LPSTR)conn->protocol);
    }
    res = SendDlgItemMessage(dlg, IDC_TERM, CB_GETCURSEL, 0, 0);
    if (res != CB_ERR) {
	SendDlgItemMessage(dlg, IDC_TERM, CB_GETLBTEXT, (WPARAM)res, (LPARAM)(LPSTR)conn->term);
    }
    GetDlgItemText(dlg, IDC_USER, conn->user, USER_NAME_LEN);
    /* Fred */
    conn->bs2Del = (BOOL)SendDlgItemMessage(dlg, IDC_BS2DEL, BM_GETCHECK, 0, 0);
    res = SendDlgItemMessage(dlg, IDC_CHARSET, CB_GETCURSEL, 0, 0);
    if (res != CB_ERR) {
	SendDlgItemMessage(dlg, IDC_CHARSET, CB_GETLBTEXT, (WPARAM)res, (LPARAM)(LPSTR)conn->charset);
    }
}

/* hostname has been changed
 */
void connectDlgOnHostChange(HWND dlg)
{
    BOOL enable = FALSE;
    char host[HOST_NAME_LEN];
    char port[PORT_NAME_LEN];

    GetDlgItemText(dlg, IDC_HOSTNAME, host, sizeof(host));
    GetDlgItemText(dlg, IDC_PORT, port, sizeof(port));
    if (isNonBlank(host) && isNonBlank(port)) {
	enable = TRUE;
    }
    EnableWindow(GetDlgItem(dlg, IDOK), enable);
}

/* port has been changed
 */
void connectDlgOnPortChange(HWND dlg)
{
    char port[PORT_NAME_LEN];

    GetDlgItemText(dlg, IDC_PORT, port, sizeof(port));
    switch (findPortProtocol(port)) 
    {
    case protoRlogin:
	SendDlgItemMessage(dlg, IDC_PROTOCOL, CB_SETCURSEL, (WPARAM)protoRlogin, 0);
	EnableWindow(GetDlgItem(dlg, IDC_USER), TRUE);
	break;
    case protoTelnet:
	SendDlgItemMessage(dlg, IDC_PROTOCOL, CB_SETCURSEL, (WPARAM)protoTelnet, 0);
	EnableWindow(GetDlgItem(dlg, IDC_USER), FALSE);
	break;
    default:
	SendDlgItemMessage(dlg, IDC_PROTOCOL, CB_SETCURSEL, (WPARAM)protoNone, 0);
	EnableWindow(GetDlgItem(dlg, IDC_USER), FALSE);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -