Beispiel #1
0
/*
 * FTSStart - start a new fts; EditFlags.FileTypeSource will suck in commands
 */
vi_rc FTSStart( char *data )
{
    char        template_data[MAX_STR];
    template_ll *templatePtr;
    ft_src      *fts, *ftsOld;

    fts = MemAlloc( sizeof( ft_src ) );
    fts->cmd_head = fts->cmd_tail = NULL;
    fts->template_head = fts->template_tail = NULL;
    while( NextWord1( data, template_data ) > 0 ) {
        templatePtr = MemAlloc( offsetof( template_ll, data ) + strlen( template_data ) + 1 );
        strcpy( templatePtr->data, template_data );
        AddLLItemAtEnd( (ss **)&fts->template_head, (ss **)&fts->template_tail, (ss *)templatePtr );
    }

    ftsOld = FTSMatchTemplate( fts->template_head );
    if( ftsOld != NULL ) {
        FTSKill( ftsOld );
    }
    AddLLItemAtEnd( (ss **)&ftsHead, (ss **)&ftsTail, (ss *)fts );

    EditFlags.FileTypeSource = TRUE;

    return( ERR_NO_ERR );

} /* FTSStart */
Beispiel #2
0
/*
 * CreateStringHandle - build a kept string handle
 */
bool CreateStringHandle( char *name, HSZ *hdl )
{
    hsz_list    *hlptr;
    int         len;
    HSZ         ohdl;

    if( hdl == NULL ) {
        hdl = &ohdl;
    }

    *hdl = DdeCreateStringHandle( DDEInstId, name, 0 );
    if( *hdl == 0 ) {
        return( FALSE );
    }
    if( !DdeKeepStringHandle( DDEInstId, *hdl ) ) {
        return( FALSE );
    }
    len = strlen( name );
    hlptr = MemAlloc( offsetof( hsz_list, string ) + len + 1 );

    hlptr->hsz = *hdl;
    memcpy( hlptr->string, name, len + 1 );
    AddLLItemAtEnd( (ss **)&hszHead, (ss **)&hszTail, (ss *)hlptr );
    return( TRUE );

} /* CreateStringHandle */
Beispiel #3
0
/*
 * FTSAddCmd - add a 1-line command to the current (tail) fts
 */
vi_rc FTSAddCmd( char *data, int tok )
{
    char    cmd_data[MAX_STR];
    cmd_ll  *cmd;

    assert( ftsTail );
    assert( EditFlags.FileTypeSource );

    cmd_data[0] = '\0';
    // Source gets cute & trashes "set "...
    if( tok >= SRC_T_NULL + 1 ) {
        switch( tok ) {
        case SRC_T_NULL + PCL_T_SET + 1:
            strcpy( cmd_data, "set " );
            if( EditFlags.ScriptIsCompiled ) {
                NextWord1( data, cmd_data + 4 );
                ExpandTokenSet( cmd_data + 4, cmd_data + 4 );
            }
            break;
        }
    }
    strcat( cmd_data, data );

    cmd = MemAlloc( offsetof( cmd_ll, data ) + strlen( cmd_data ) + 1 );
    strcpy( cmd->data, cmd_data );

    AddLLItemAtEnd( (ss **)&ftsTail->cmd_head, (ss **)&ftsTail->cmd_tail, (ss *)cmd );

    return( ERR_NO_ERR );

} /* FTSAddCmd */
Beispiel #4
0
vi_rc OpenFcbData( file *f )
{
    int         handle;
    vi_rc       rc;
    fcb         *cfcb;

    /*
     * open file handle if we need to
     */
    rc = ERR_NO_ERR;
    if( !f->is_stdio ) {
        handle = -1;
        ConditionalChangeDirectory( f->home );
        rc = FileOpen( f->name, false, O_BINARY | O_RDONLY, 0, &handle );
        if( rc != ERR_NO_ERR ) {
            return( ERR_FILE_OPEN );
        }
        if( handle == -1 ) {
            rc = ERR_FILE_NOT_FOUND;
        } else if( f->size == 0 ) {
            close( handle );
            rc = END_OF_FILE;
        } else {
            f->handle = handle;
        }
        if( rc != ERR_NO_ERR ) {
            cfcb = FcbAlloc( f );
            AddLLItemAtEnd( (ss **)&(f->fcbs.head), (ss **)&(f->fcbs.tail), (ss *)cfcb );
            CreateNullLine( cfcb );
        }
    }
    return( rc );
}
Beispiel #5
0
/*
 * CreateStringHandle - build a kept string handle
 */
bool CreateStringHandle( const char *name, HSZ *hdl )
{
    hsz_list    *hlptr;
    int         len;
    HSZ         ohdl;
    char        buff[256];

    if( hdl == NULL ) {
        hdl = &ohdl;
    }
    len = strlen( name );
    if( len > 255 )
        len = 255;
    memcpy( buff, name, len );
    buff[len] = '\0';
    *hdl = DdeCreateStringHandle( DDEInstId, buff, 0 );
    if( *hdl == 0 ) {
        return( false );
    }
    if( !DdeKeepStringHandle( DDEInstId, *hdl ) ) {
        return( false );
    }
    hlptr = MemAlloc( offsetof( hsz_list, string ) + len + 1 );

    hlptr->hsz = *hdl;
    memcpy( hlptr->string, buff, len + 1 );
    AddLLItemAtEnd( (ss **)&hszHead, (ss **)&hszTail, (ss *)hlptr );
    return( true );

} /* CreateStringHandle */
Beispiel #6
0
/*
 * CreateFcbData - create fcb with data from specified buffer
 */
void CreateFcbData( file *f, int cnt )
{
    int used, linecnt;
    fcb *cfcb;

    /*
     * get new fcb
     */
    cfcb = FcbAlloc( f );
    AddLLItemAtEnd( (ss **)&(f->fcbs.head), (ss **)&(f->fcbs.tail), (ss *)cfcb );

    /*
     * create lines from buffer info
     */
    CreateLinesFromBuffer( cnt, &cfcb->lines, &used, &linecnt, &(cfcb->byte_cnt) );

    /*
     * update position and line numbers
     */
    if( f->fcbs.tail->prev == NULL ) {
        cfcb->start_line = 1;
    } else {
        cfcb->start_line = f->fcbs.tail->prev->end_line + 1;
    }
    cfcb->end_line = cfcb->start_line + linecnt - 1;
    cfcb->non_swappable = false;

} /* CreateFcbData */
Beispiel #7
0
/*
 * duplicateFcb - do just that
 */
static void duplicateFcb( fcb *cfcb, fcb **dfcb )
{
    line        *cline, *nline;

    /*
     * get fcb and create a new one
     */
    FetchFcb( cfcb );
    cfcb->non_swappable = TRUE;
    *dfcb = FcbAlloc( NULL );
    (*dfcb)->start_line = cfcb->start_line;
    (*dfcb)->end_line = cfcb->end_line;
    (*dfcb)->byte_cnt = cfcb->byte_cnt;
    (*dfcb)->lines.head = (*dfcb)->lines.tail = NULL;

    /*
     * copy all lines
     */
    for( cline = cfcb->lines.head; cline != NULL; cline = cline->next ) {
        nline = LineAlloc( cline->data, cline->len );
        AddLLItemAtEnd( (ss **)&((*dfcb)->lines.head), (ss **)&((*dfcb)->lines.tail),
            (ss *)nline );
    }

    cfcb->non_swappable = FALSE;
    (*dfcb)->non_swappable = FALSE;

} /* duplicateFcb */
Beispiel #8
0
/*
 * CreateDuplicateFcbList - duplicate a list of fcb's
 */
void CreateDuplicateFcbList( fcb *cfcb, fcb_list *fcblist )
{
    fcb     *xfcb;

    for( ; cfcb != NULL; cfcb = cfcb->next ) {
        duplicateFcb( cfcb, &xfcb );
        AddLLItemAtEnd( (ss **)&fcblist->head, (ss **)&fcblist->tail, (ss *)xfcb );
    }

} /* CreateDuplicateFcbList */
Beispiel #9
0
/*
 * CreateNullLine - put a single null line in an fcb
 */
void CreateNullLine( fcb *cfcb )
{
    line        *cline;

    cline = LineAlloc( NULL, 0 );
    FetchFcb( cfcb );
    AddLLItemAtEnd( (ss **)&(cfcb->lines.head), (ss **)&(cfcb->lines.tail), (ss *)cline );
    cfcb->byte_cnt = 1;
    cfcb->start_line = cfcb->end_line = 1;
    cfcb->nullfcb = TRUE;

} /* CreateNullLine */
Beispiel #10
0
/*
 * AddBitmapToToolBar - add a toolbar item ([temp], bitmap, help & command)
 */
vi_rc AddBitmapToToolBar( const char *data )
{
    char                file[FILENAME_MAX];
    char                help[MAX_STR];
    char                dont_save[MAX_STR];
    tool_item           *item;
    int                 cmd_len;
    int                 name_len;

    dont_save[0] = 0;

    data = SkipLeadingSpaces( data );
    if( strnicmp( data, "temp", 4 ) == 0 ) {
        /* get to the command */
        GetStringWithPossibleQuote( &data, dont_save );
    }

    GetStringWithPossibleQuote( &data, file );
    GetStringWithPossibleQuote( &data, help );

    data = SkipLeadingSpaces( data );
    cmd_len = strlen( data );
    name_len = strlen( file );
    item = MemAlloc( sizeof( tool_item ) + cmd_len + name_len + strlen( help ) + 2 );
    strcpy( item->cmd, data );
    if( name_len != 0 ) {
        item->id = NextMenuId();
    } else {
        item->is_blank = true;
    }
    item->dont_save = ( strlen( dont_save ) != 0 );

    if( file[0] && item->cmd[0] ) {
        item->bmp = LoadBitmap( InstanceHandle, file );
        if( item->bmp == HNULL ) {
            item->bmp = ReadBitmapFile( ToolBarWindow( toolBar ), file, NULL );
        }
        item->name = &item->cmd[cmd_len + 1];
        strcpy( item->name, file );
        item->help = &item->name[name_len + 1];
        strcpy( item->help, help );
    } else {
        item->bmp = HNULL;
    }
    if( toolBar ) {
        addToolBarItem( item );
    }
    AddLLItemAtEnd( &toolBarHead, &toolBarTail, &item->tool_head );
    return( ERR_NO_ERR );

} /* AddBitmapToToolBar */
Beispiel #11
0
/*
 * addAnFcb - add a new fcb and its data
 */
static int addAnFcb( fcb_list *fcblist, int numbytes )
{
    fcb         *cfcb;
    int         linecnt;
    int         used;

    cfcb = FcbAlloc( NULL );
    AddLLItemAtEnd( (ss **)&fcblist->head, (ss **)&fcblist->tail, (ss *)cfcb );
    CreateLinesFromBuffer( numbytes, &cfcb->lines, &used, &linecnt, &(cfcb->byte_cnt) );
    if( fcblist->tail->prev == NULL ) {
        cfcb->start_line = 1;
    } else {
        cfcb->start_line = fcblist->tail->prev->end_line + 1;
    }
    cfcb->end_line = cfcb->start_line + linecnt - 1;
    return( used );

} /* addAnFcb */
Beispiel #12
0
/*
 * InsertNewLine - do just that
 */
void InsertNewLine( line *who, line_list *linelist, char *data, int copylen,
                    insert_dir dir )
{
    line        *cl;

    if( copylen <= 0 ) {
        data = NULL;
        copylen = 0;
    }
    cl = LineAlloc( data, copylen );
    if( linelist->head == NULL ) {
        AddLLItemAtEnd( (ss **)&linelist->head, (ss **)&linelist->tail, (ss *)cl );
    } else {
        if( dir == INSERT_AFTER ) {
            InsertLLItemAfter( (ss **)&linelist->tail, (ss *)who, (ss *)cl );
        } else {
            InsertLLItemBefore( (ss **)&linelist->head, (ss *)who, (ss *)cl );
        }
    }

} /* InsertNewLine */
Beispiel #13
0
/*
 * setGenericAlias - define an alias/abbreviation
 */
static vi_rc setGenericAlias( const char *what, alias_list **head, alias_list **tail )
{
    alias_list  *curr;
    char        str[MAX_STR];

    what = GetNextWord1( what, str );
    if( *str == '\0' ) {
        return( ERR_INVALID_ALIAS );
    }
    what = SkipLeadingSpaces( what );

    /*
     * see if alias is already in list: if so, and there is expansion data,
     * then replace the data, else delete the item
     */
    for( curr = *head; curr != NULL; curr = curr->next ) {
        if( strcmp( str, curr->alias ) == 0 ) {
            MemFree( curr->expand );
            if( *what == '\0' ) {
                MemFree( curr->alias );
                MemFree( DeleteLLItem( (ss **)head, (ss **)tail, (ss *)curr ) );
            } else {
                curr->expand = DupString( what );
            }
        }
    }

    /*
     * add the new item
     */
    curr = MemAlloc( sizeof( alias_list ) );
    curr->alias = DupString( str );
    curr->expand = DupString( what );

    AddLLItemAtEnd( (ss **)head, (ss **)tail, (ss *)curr );
    Message1( "%s set to \"%s\"", str, what );
    return( DO_NOT_CLEAR_MESSAGE_WINDOW );

} /* setGenericAlias */
Beispiel #14
0
/*
 * FTSAddCmd - add a 1-line command to the current (tail) fts
 */
vi_rc FTSAddCmd( char *data, int setkilled )
{
    char    cmd_data[MAX_STR];
    cmd_ll  *cmd;

    assert( ftsTail );
    assert( EditFlags.FileTypeSource );

    // Source gets cute & trashes "set "...
    if( setkilled >= SRC_T_NULL + 1 ) {
        strcpy( cmd_data, "set" );
        strcat( cmd_data, data );
    } else {
        strcpy( cmd_data, data );
    }

    cmd = MemAlloc( sizeof( cmd_ll ) + strlen( cmd_data ) );
    strcpy( cmd->data, cmd_data );

    AddLLItemAtEnd( (ss **)&ftsTail->cmd_head, (ss **)&ftsTail->cmd_tail, (ss *)cmd );

    return( ERR_NO_ERR );

} /* FTSAddCmd */
Beispiel #15
0
/*
 * createNewFile - create new file entry
 */
static vi_rc createNewFile( char *name, bool same_file )
{
    int         height;
    window_id   cw;
    info        *tmp;
    vi_rc       rc;

    /*
     * test that we can create this file
     */
    tmp = NULL;
    if( !same_file ) {
        rc = FileExists( name );
        if( !(rc == ERR_READ_ONLY_FILE || rc == ERR_NO_ERR || rc == ERR_FILE_EXISTS) ) {
            return( rc );
        }
    } else {
        if( name != NULL ) {
            for( tmp = InfoHead; tmp != NULL; tmp = tmp->next ) {
                if( !strcmp( tmp->CurrentFile->name, name ) ) {
                    break;
                }
            }
        }
        if( tmp == NULL )  {
            return( ERR_FILE_NOT_FOUND );
        }
        if( tmp->CurrentFile->dup_count > MAX_DUPLICATE_FILES ) {
            return( ERR_WIND_NO_MORE_WINDOWS );
        }
    }

    /*
     * get new window
     */
    rc = NewWindow2( &cw, &editw_info );
    if( rc != ERR_NO_ERR ) {
        return( rc );
    }
#ifdef __WIN__
    if( !strncmp( name, "untitled", 8 ) ) {
        // better yet, pass normal/maximize flag to NewWindow2...
        ShowWindow( cw, SW_SHOWMAXIMIZED );
    }
#endif
    SetBorderGadgets( cw, EditFlags.WindowGadgets );

    /*
     * get new file entry, and read the data
     */

    if( same_file ) {
        CurrentFile = tmp->CurrentFile;
        CurrentFile->dup_count++;
        SetFileWindowTitle( CurrentWindow, CurrentInfo, true );
        tmp = CurrentInfo;
        CurrentInfo = MemAlloc( sizeof( *CurrentInfo ) );
        FTSRunCmds( name );

        rc = ERR_NO_ERR;
    } else {
        bool crlf_reached;

        crlf_reached = false;
        tmp = CurrentInfo;
        CurrentInfo = MemAlloc( sizeof( *CurrentInfo ) );
        FTSRunCmds( name );
        height = editw_info.y2 - editw_info.y1 + 1;

        CurrentFile = FileAlloc( name );
        rc = OpenFcbData( CurrentFile );
        for( ; rc == ERR_NO_ERR; ) {
            rc = ReadFcbData( CurrentFile, &crlf_reached );
            if( rc == ERR_NO_ERR && !CurrentFile->is_stdio ) {
                if( EditFlags.BreakPressed || !EditFlags.ReadEntireFile ) {
                    if( CurrentFile->fcbs.tail->end_line > height ) {
                        break;
                    }
                }
            }
        }
        if( rc != ERR_NO_ERR && rc != ERR_FILE_NOT_FOUND && rc != END_OF_FILE ) {
            MemFree( CurrentInfo );
            CurrentInfo = tmp;
            FileFree( CurrentFile );
            CloseAWindow( cw );
            return( rc );
        }
        if( rc == ERR_FILE_NOT_FOUND ) {
            rc = NEW_FILE;
            EditFlags.NewFile = true;
            CurrentFile->write_crlf = FileSysNeedsCR( CurrentFile->handle );
#ifdef __UNIX__
            CurrentFile->attr = PMODE_RW;
#endif
        } else {
            rc = ERR_NO_ERR;
            CurrentFile->write_crlf = crlf_reached;
            EditFlags.NewFile = false;
#ifdef __UNIX__
            {
                struct stat     sb;
                stat( name, &sb );
                CurrentFile->attr = sb.st_mode & ~S_IFMT;
            }
#endif
        }
        CurrentFile->check_readonly = true;
    }

    /*
     * create info entry
     */
    CurrentPos.line = 0;
    CurrentPos.column = 1;
    VirtualColumnDesired = 1;
    LeftTopPos.line = 1;
    LeftTopPos.column = 0;
    if( !same_file ) {
        AllocateUndoStacks();
    }
    AllocateMarkList();
    CurrentWindow = cw;
    CurrentInfo->DuplicateID = CurrentFile->dup_count;
    CurrentInfo->CurrentWindow = cw;
    LangInit( CurrentInfo->fsi.Language );
#ifdef __WIN__
    {
        window_data     *wd;
        wd = DATA_FROM_ID( cw );
        wd->info = CurrentInfo;
    }
#endif
    DCCreate();
    SetFileWindowTitle( CurrentWindow, CurrentInfo, true );

    /*
     * set current file info
     */
    CurrentFcb = CurrentFile->fcbs.head;
    CurrentLine = CurrentFcb->lines.head;

    if( EditFlags.LineNumbers ) {
        LineNumbersSetup();
    }

    if( tmp != NULL ) {
        InsertLLItemAfter( (ss **)&InfoTail, (ss *)tmp, (ss *)CurrentInfo );
    } else {
        AddLLItemAtEnd( (ss **)&InfoHead, (ss **)&InfoTail, (ss *)CurrentInfo );
    }

    return( rc );

} /* createNewFile */
Beispiel #16
0
/*
 * ReadFcbData - read fcb data
 */
vi_rc ReadFcbData( file *f, bool *crlf_reached )
{
    int         cnt, used, linecnt;
    bool        eofflag;
    fcb         *cfcb;
    vi_rc       rc;

    f->bytes_pending = false;
    /*
     * go to appropriate location in file
     */
    if( f->is_stdio ) {
        if( extraData != NULL ) {
            memcpy( ReadBuffer, extraData, extraDataSize );
            MemFreePtr( &extraData );
        }
    } else {
        rc = FileSeek( f->handle, f->curr_pos );
        if( rc != ERR_NO_ERR ) {
            return( rc );
        }
    }

    /*
     * read file data
     */
    if( f->is_stdio ) {
        cnt = fread( ReadBuffer + extraDataSize, 1, MAX_IO_BUFFER - extraDataSize, stdin );
        cnt += extraDataSize;
        extraDataSize = 0;
        if( ferror( stdin ) ) {
            return( ERR_READ );
        }
    } else {
        cnt = read( f->handle, ReadBuffer, MAX_IO_BUFFER );
        if( cnt == -1 ) {
            return( ERR_READ );
        }
    }

    /*
     * get new fcb
     */
    cfcb = FcbAlloc( f );
    AddLLItemAtEnd( (ss **)&(f->fcbs.head), (ss **)&(f->fcbs.tail), (ss *)cfcb );

    /*
     * create lines from buffer info
     */
    eofflag = CreateLinesFromFileBuffer( cnt, &cfcb->lines, &used, &linecnt, &(cfcb->byte_cnt), crlf_reached );

    if( used == 0 ) {
        CreateNullLine( cfcb );
        eofflag = true;
    } else {

        /*
         * update position
         */
        f->curr_pos += used;
        if( f->fcbs.tail->prev == NULL ) {
            cfcb->start_line = 1;
        } else {
            cfcb->start_line = f->fcbs.tail->prev->end_line + 1;
        }
        if( f->is_stdio && feof( stdin ) && used >= cnt || !f->is_stdio && f->curr_pos >= f->size ) {
            eofflag = true;
            if( !EditFlags.LastEOL && ReadBuffer[used - 1] == LF ) {
                ++linecnt;
                AddLLItemAtEnd( (ss **)&(cfcb->lines.head), (ss **)&(cfcb->lines.tail), (ss *)LineAlloc( NULL, 0 ) );
            }
        }
        /*
         * update line numbers
         */
        cfcb->end_line = cfcb->start_line + linecnt - 1;
        cfcb->non_swappable = false;
    }

    if( eofflag ) {
        if( !f->is_stdio ) {
            close( f->handle );
            f->handle = -1;
        }
        return( END_OF_FILE );
    }

    f->bytes_pending = true;
    if( f->is_stdio ) {
        extraDataSize = cnt - used;
        extraData = MemAlloc( extraDataSize );
        memcpy( extraData, ReadBuffer + used, extraDataSize );
    }
    return( ERR_NO_ERR );

} /* ReadFcbData */