errsrc.c

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

C
929
字号
/****************************************************************************
*
*                            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:  make C file for errors messages
*
****************************************************************************/


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

#define NULLCHAR        '\0'

typedef struct word_list {
    char                *word;
    int                 word_num;
    int                 ref_count;
    struct word_list    *link;
    struct word_list    *sortlink;
} word_list;

typedef struct msg_word {
    struct word_list    *word;
    struct msg_word     *link;
} msg_word;

typedef struct msg_list {
    struct msg_word     *msg;
    int                 caret;
    int                 count;
    struct msg_list     *link;
} msg_list;

typedef struct group_list {
    struct group_list   *link;
    char                name[3];
    int                 start_msg_num;
    int                 end_msg_num;
} group_list;

static  msg_list        *AddWords( char *msg );
static  msg_list        *InitMsg( void );
static  word_list       *ProcessWord( char *word );

static  word_list       *HeadWord;
static  msg_list        *HeadMsg;
static  group_list      *HeadGroup;
static  FILE            *MsgFile;
static  FILE            *ErrMsg;
static  FILE            *ErrGrp;
static  FILE            *ErrCod;
static  FILE            *ErrFile;
static  FILE            *RCFile;
static  int             MaxRefCount;
static  word_list       *SortPtr;
static  word_list       *SortHead;
static  int             RecNum;

#define BUFF_LEN        133

// Error messages contain a 5-character field with the following information:
//      1. language of the message
//              english                         'e' or ' '
//              japanese                        'j'
//      2. message used by WATFOR-77            'w'
//         message used by WFC                  'o'
//         message used by WFL                  'l'
//         message used by WFL, WFC             'c'
//         message used by WATFOR-77, WFL, WFC  '.'
//         message used by WATFOR-77, WFC       ' '
//      3. message is 80386-specific            '3'
//         message is 8086-specific             'i'
//         message is 80x86-specific            '8'
//         message is RISC-specific             'r'
//         message is target-independant        ' '
//      4. message is used at compile-time      'c'
//         message is used at run-time          'r'
//         message is used during both          ' '
//      5. caret information                    '0', '1' or '2'

static  char    sw_language = { ' ' };
static  char    sw_compiler = { ' ' };
static  char    sw_target = { ' ' };
static  char    sw_used_at = { ' ' };


int     main() {
//==============

    char        cmd[128+1];

    ProcArgs( getcmd( cmd ) );
    if( Initialize() != 0 ) {
        return( 1 );
    }
    printf( "Building Lists...\n" );
    BuildLists();
    printf( "Finding Phrases...\n" );
    FindPhrases();
    printf( "Sorting...\n" );
    SortByRef();
    printf( "ReNumbering...\n" );
    ReNumber();
    printf( "Dumping Header...\n" );
    DumpHeader();
    printf( "Dumping Msgs...\n" );
    DumpMsg();
    printf( "Dumping GroupTable...\n" );
    DumpGroupTable();
    printf( "Dumping ErrWords...\n" );
    DumpErrWord();
    Finalize();
    return( 0 );
}


static  void    ProcArgs( char *args ) {
//======================================

    while( *args == ' ' ) {
        ++args;
    }
    if( *args == '\0' ) return;
    sw_language = *args;
    if( sw_language == 'e' ) {
        sw_language = ' ';
    }
    ++args;
    if( *args == '\0' ) return;
    sw_compiler = *args;
    ++args;
    if( *args == '\0' ) return;
    sw_target = *args;
    ++args;
    if( *args == '\0' ) return;
    sw_used_at = *args;
}


static  int     Initialize() {
//============================

    MsgFile = fopen( "error.msg", "rt" );
    if( MsgFile == NULL ) {
        return( 1 );
    }
    ErrMsg = fopen( "errmsg.c", "wt" );
    if( ErrMsg == NULL ) {
        fclose( MsgFile );
        return( 1 );
    }
    ErrGrp = fopen( "errgrp.c", "wt" );
    if( ErrGrp == NULL ) {
        fclose( MsgFile );
        fclose( ErrMsg );
        return( 1 );
    }
    ErrCod = fopen( "errcod.h", "wt" );
    if( ErrCod == NULL ) {
        fclose( MsgFile );
        fclose( ErrMsg );
        fclose( ErrGrp );
        return( 1 );
    }
    ErrFile = fopen( "errmsg.msg", "wt" );
    if( ErrFile == NULL ) {
        fclose( MsgFile );
        fclose( ErrMsg );
        fclose( ErrGrp );
        fclose( ErrCod );
        return( 1 );
    }
    RCFile = fopen( "errmsg.rc", "wt" );
    if( RCFile == NULL ) {
        fclose( MsgFile );
        fclose( ErrMsg );
        fclose( ErrGrp );
        fclose( ErrCod );
        fclose( ErrFile );
        return( 1 );
    }
    HeadWord = NULL;
    HeadMsg = NULL;
    HeadGroup = NULL;
    MaxRefCount = 0;
    RecNum = 1 ;
    return( 0 );
}


static  void    Finalize() {
//==========================

    fclose( MsgFile );
    fclose( ErrMsg );
    fclose( ErrGrp );
    fclose( ErrCod );
    fclose( ErrFile );
    fclose( RCFile );
}

static int      IsTarget( char target ) {
//=======================================

    if( (sw_target == target) || (target == ' ') ) {
        return( 1 );
    } else if( (sw_target == 'i') || (sw_target =='3') ) {
        return( target == '8' );
    }
    return( 0 );
}

static  char    UseMessage( char cmp, char target, char used_at ) {
//=================================================================

    if( sw_compiler == 'o' ) {
        if( (cmp != 'o') && (cmp != ' ') && (cmp != '.') && (cmp != 'c') ) {
            return( 0 );
        }
    } else if( sw_compiler == 'w' ) {
        if( (cmp != 'w') && (cmp != ' ') && (cmp != '.') ) {
            return( 0 );
        }
    } else if( sw_compiler == 'l' ) {
        if( (cmp != 'l') && (cmp != 'c') && (cmp != '.') ) {
            return( 0 );
        }
    }
    if( IsTarget( target ) ) {
        if( sw_compiler == 'w' ) {
            if( sw_used_at == ' ' ) {
                // load'n go compiler doesn't care whether message is
                // used at compile-time or run-time
                return( 1 );
            } else if( (sw_used_at == used_at) || (used_at == ' ') ) {
                return( 1 );
            }
        } else if( (sw_used_at == used_at) || (used_at == ' ') ) {
            return( 1 );
        }
    }
    return( 0 );
}


static  void    BuildLists() {
//============================

    int         index;
    group_list  *curr_group;
    int         group;
    msg_list    *curr_msg;
    msg_list    *msg_ptr;
    msg_list    *last_non_null_msg;
    msg_list    **p_null_msg;
    msg_word    *word;
    int         caret;
    char        rec[BUFF_LEN+1];
    char        msg_used_at;
    char        msg_compiler;
    char        msg_target;
    char        delim;

    fprintf( ErrCod, "#define    NO_CARROT  0\n" );
    fprintf( ErrCod, "#define    OPR_CARROT 1\n" );
    fprintf( ErrCod, "#define    OPN_CARROT 2\n" );
    fprintf( RCFile, "#include \"errcod.h\"\n\n" );
    fprintf( RCFile, "stringtable begin\n\n" );
    group = 0;
    ReadInFile( &rec );
    last_non_null_msg = NULL;
    for(;;) {
        if( HeadGroup == NULL ) {
            HeadGroup = malloc( sizeof( group_list ) );
            curr_group = HeadGroup;
        } else {
            curr_group->link = malloc( sizeof( group_list ) );
            curr_group = curr_group->link;
        }
        curr_group->link = NULL;
        curr_group->start_msg_num = group * 256;
        curr_group->end_msg_num = curr_group->start_msg_num;
        curr_group->name[ 0 ] = rec[ 0 ];
        curr_group->name[ 1 ] = rec[ 1 ];
        curr_group->name[ 2 ] = NULLCHAR;
        for(;;) {
            if( ReadInFile( &rec ) != 0 ) {
                fprintf( RCFile, "\nend\n" );
                return;
            }
            ++RecNum;
            if( ( strlen( rec ) > 2 ) && ( rec[ 2 ] == ' ' ) ) break;
            index = 3;
            while( rec[ index ] != ' ' ) {
                ++index;
            }
            rec[ index ] = '\0';
            ++index;
            ++index;    // skip [
            if( rec[ index ] != sw_language ) continue;
            ++index;
            msg_compiler = rec[ index ];
            ++index;
            msg_target = rec[ index ];
            ++index;
            msg_used_at = rec[ index ];
            ++index;
            caret = rec[ index ] - '0';
            ++index;
            ++index;    // skip ]
            if( UseMessage( msg_compiler, msg_target, msg_used_at ) ) {
                msg_ptr = AddWords( &rec[ index ] );
            } else {
                msg_ptr = InitMsg();
            }
            if( msg_ptr->msg != NULL ) {
                fprintf( ErrCod, "#define    %s %d\n", rec,
                         curr_group->end_msg_num );
                fprintf( RCFile, "    %s+MSG_LANG_BASE \"", rec );
                word = msg_ptr->msg;
                delim = ' ';
                while( word != NULL ) {
                    if( word->link == NULL ) {
                        delim = '"';
                    }
                    fprintf( RCFile, "%s%c", word->word->word, delim );
                    word = word->link;
                }
                fprintf( RCFile, "\n" );
                last_non_null_msg = msg_ptr;
            }
            curr_group->end_msg_num++;
            if( HeadMsg == NULL ) {
                HeadMsg = msg_ptr;
            } else {
                curr_msg->link = msg_ptr;
            }
            curr_msg = msg_ptr;
            curr_msg->caret = caret;
        }
        curr_msg = last_non_null_msg;
        if( last_non_null_msg == NULL ) {
            p_null_msg = &HeadMsg;
        } else {
            p_null_msg = &last_non_null_msg->link;
        }
        while( *p_null_msg != NULL ) {
            msg_ptr = (*p_null_msg)->link;
            free( *p_null_msg );
            curr_group->end_msg_num--;
            *p_null_msg = msg_ptr;
        }
        ++group;
    }
}


static  int     ReadInFile( char *buff ) {
//========================================

    int         len;

    for(;;) {
        if( fgets( buff, BUFF_LEN, MsgFile ) == NULL ) {
            return( 1 );
        }
        len = strlen( buff );
        buff[ len - 1 ] = NULLCHAR;
        buff[ len ] = NULLCHAR;
        if( *buff != ' ' ) break;
    }
    return( 0 );
}


static  msg_list        *InitMsg() {
//==================================

    msg_list    *curr_msg;

    curr_msg = malloc( sizeof( msg_list ) );
    curr_msg->count = 0;
    curr_msg->msg = NULL;
    curr_msg->link = NULL;
    curr_msg->caret = 0;
    return( curr_msg );
}


static  msg_list        *AddWords( char *msg ) {
//==============================================

    int         index;
    char        *word_str;
    msg_list    *curr_msg;
    msg_word    *curr_word;
    int         word_size;

    curr_msg = InitMsg();
    curr_word = NULL;
    for(;;) {
        while( *msg == ' ' ) {
            ++msg;
        }
        if( *msg == NULLCHAR ) break;
        index = 0;
        for(;;) {
            if( msg[ index ] == NULLCHAR ) break;
            ++index;
            if( msg[ index ] == ' ' ) break;
        }
        word_size = index + 1;
        if( msg[ index ] == ' ' ) {
            msg[ index++ ] = NULLCHAR;
        }
        word_str = malloc( word_size );
        strcpy( word_str, msg );
        if( curr_word == NULL ) {
            curr_word = malloc( sizeof( msg_word ) );
            curr_msg->msg = curr_word;
        } else {
            curr_word->link = malloc( sizeof( msg_word ) );
            curr_word = curr_word->link;
        }
        curr_word->link = NULL;
        curr_word->word = ProcessWord( word_str );
        curr_msg->count++;
        msg += index;
    }
    return( curr_msg );
}


static  word_list       *ProcessWord( char *word ) {
//==================================================

    word_list   *curr_word;
    word_list   *prev_word;
    word_list   *new_word;
    char        *w1;

⌨️ 快捷键说明

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