epmlink.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 515 行 · 第 1/2 页

C
515
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  Interface DLL for communicating to EPM (OS/2 system editor)
*
****************************************************************************/

#include <process.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

#define INCL_WINDDE
#define INCL_WINWINDOWMGR
#define INCL_WININPUT
#include <os2.h>

typedef struct errstr {
    ULONG       errorline;
    ULONG       offset;
    ULONG       length;
    ULONG       magic;
} errstr;

typedef struct initiate_data {
    ULONG       errorcount;
    errstr      errors[1];
    ULONG       liblength;
    char        libname[1];
} initiate_data;

typedef struct goto_data {
    ULONG       errorline;
    ULONG       offset;
    ULONG       resourceid;
    ULONG       magic;
    ULONG       textlength;
    char        errortext[1];
} goto_data;

static CONVCONTEXT      ConvContext = { sizeof( CONVCONTEXT ), 0 };

static PFNWP            prevClientProc;
static HWND             hwndDDE;        // window handle for DDE session

#define _Editor         "EPM.EXE"

typedef struct message {
    int         row;
    int         col;
    int         len;
    int         resourceid;
    char        error[1];
} message;

typedef struct session {
    struct session      *link;
    HWND                hwnd;
    message             *msg;
    char                connected;
    char                *file_name;
    char                *help_library;
    char                data[2];        // room for 2 NULLCHAR's
} session;

static session          *SessionList;
static session          *CurrSession;
static unsigned         AllocatedBlocks;
static char             StartingSessionInProgress;

#define RESOURCE_ID_BASE        19999


static void *MemAlloc( unsigned size )
/************************************/
{
    void        *p;

    p = malloc( size );
    if( p != NULL ) {
        ++AllocatedBlocks;
    }
    return( p );
}


static void MemFree( void *p )
/****************************/
{
    free( p );
    --AllocatedBlocks;
}


static session *FindSession( char *fn )
/*************************************/
{
    session     *sess;

    for( sess = SessionList; sess != NULL; sess = sess->link ) {
        if( strcmp( sess->file_name, fn ) == 0 ) return( sess );
    }
    return( NULL );
}


static session *FindSessionByHWND( HWND hwnd )
/********************************************/
{
    session     *sess;

    for( sess = SessionList; sess != NULL; sess = sess->link ) {
        if( sess->hwnd == hwnd ) return( sess );
    }
    return( NULL );
}


static session *NewSession( char *fn, char *hlib )
/************************************************/
{
    int         len_fn;
    int         len_hlib;
    session     *new_session;

    len_fn = strlen( fn );
    if( hlib == NULL ) {
        hlib = "";
    }
    len_hlib = strlen( hlib );
    new_session = MemAlloc( sizeof( session ) + len_fn + len_hlib );
    if( new_session != NULL ) {
        // WM_DDE_INITIATE handler assumes that the new
        // session is inserted at the head of the list
        new_session->file_name = &new_session->data[0];
        new_session->help_library = &new_session->data[len_fn+1];
        memcpy( new_session->file_name, fn, len_fn + 1 );
        memcpy( new_session->help_library, hlib, len_hlib + 1 );
        new_session->hwnd = NULLHANDLE;
        new_session->connected = FALSE;
        new_session->msg = NULL;
        new_session->link = NULL;
    }
    return( new_session );
}


static void FreeSession( session *sess )
/**************************************/
{
    if( sess->msg != NULL ) {
        MemFree( sess->msg );
    }
    MemFree( sess );
}


static void DeleteSession( HWND hwnd )
/************************************/
{
    session     **owner;
    session     *curr;

    owner = &SessionList;
    for(;;) {
        curr = *owner;
        if( curr->hwnd == hwnd ) break;
        owner = &curr->link;
    }
    *owner = curr->link;
    FreeSession( curr );
}


static void LinkSession( session *sess )
/**************************************/
{
    sess->link = SessionList;
    SessionList = sess;
}


static void FreeSessions( void )
/******************************/
{
    session     *link;

    while( SessionList != NULL ) {
        link = SessionList->link;
        FreeSession( SessionList );
        SessionList = link;
    }
}


static void InitSessions( void )
/******************************/
{
    SessionList = NULL;
}


static void Connect( void )
/*************************/
{
    WinDdeInitiate( hwndDDE, "WB Editor", CurrSession->file_name, &ConvContext );
}


static message *BuildMsg( long lRow, int nCol, int nLen,
                          int resourceid, char *error_msg )
/*********************************************************/
{
    message     *msg;

    if( error_msg == NULL ) {
        msg = MemAlloc( sizeof( message ) );
    } else {
        msg = MemAlloc( sizeof( message ) + strlen( error_msg ) );
    }
    if( msg != NULL ) {
        msg->row = lRow;
        msg->col = nCol - 1;
        msg->len = nLen;
        msg->resourceid = RESOURCE_ID_BASE + resourceid;
        if( error_msg == NULL ) {
            msg->error[0] = '\0';
        } else {
            strcpy( msg->error, error_msg );
        }
    }
    return( msg );
}


⌨️ 快捷键说明

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