Example #1
0
/*
 * findHook - look for a hook routine
 */
static vars *findHook( char *which )
{
    char        foo[64];
    vars        *v;

    MySprintf( foo, "%shook", which );
    v = VarFind( foo, NULL );
    return( v );

} /* findHook */
Example #2
0
/*
 * srcHook - run a specified source hook
 */
static vi_rc srcHook( hooktype num, vi_rc lastrc )
{
    vars        *v;
    srcline     sline;
    vi_rc       rc;

    if( hookRun & num ) {
        return( lastrc );
    }

    /*
     * check script type
     */
    v = GetHookVar( num );
    /*
     * run script, if we have one
     */
    if( v != NULL ) {
        if( num == SRC_HOOK_COMMAND ) {
            VarAddGlobalStr( "Com", CommandBuffer );
        }
//        if( num == SRC_HOOK_MODIFIED ) {
//            lastrc = LastEvent;
//        }

        /*
         * set up for and run script
         */
        hookRun |= num;
        LastRetCode = lastrc;
        rc = Source( v->value, srcHookData, &sline );

        /*
         * if we had a command hook, look for replacement variable
         */
        if( num == SRC_HOOK_COMMAND ) {
            v = VarFind( "Com", NULL );
            if( v != NULL ) {
                strcpy( CommandBuffer, v->value );
            }
        }

        /*
         * we are done now, reset and go back
         */
        LastRetCode = ERR_NO_ERR;
        hookRun &= ~num;
        DCUpdateAll();
        return( rc );

    }
    return( lastrc );

} /* srcHook */
Example #3
0
/*
 * GetCurrentFilePath - build backup path from file name
 */
void GetCurrentFilePath( char *path )
{
    vars        *v;

    v = VarFind( "D", NULL );
    if( v ) {
        strcpy( path, v->value );
        v = VarFind( "P", NULL );
        if( v ) {
            strcat( path, v->value );
            v = VarFind( "N", NULL );
            if( v ) {
                strcat( path, v->value );
                v = VarFind( "E", NULL );
                if( v ) {
                    strcat( path, v->value );
                }
            }
        }
    }

} /* GetCurrentFilePath */
Example #4
0
/*
 * SrcAssign - assign a value to a variable
 */
vi_rc SrcAssign( const char *data, vlist *vl )
{
    int         i, j, k, l;
    long        val;
    bool        rxflag = false;
    bool        envflag = false;
    bool        setflag = false;
    bool        expflag = false;
    bool        timeflag = false;
    bool        lnumflag = false;
    char        tmp[MAX_SRC_LINE], tmp1[MAX_SRC_LINE], name[MAX_SRC_LINE];
    const char  *v1;
    char        *t;
    vars        *v;
    jmp_buf     jmpaddr;
    time_t      tod;
    bool        check_end;
    char        tmpstr[MAX_STR];

    /*
     * get assign syntax :
     * ASSIGN %v = /v1/(r)($)(@)(x)(l)(t)
     * possible v1:
     *  strlen %a
     *  strchr %a ch
     *  substr %a n1 n2
     */
    data = GetNextWord1( data, tmp );
    if( *tmp == '\0' ) {
        return( ERR_SRC_INVALID_ASSIGN );
    }
    if( !VarName( name, tmp, vl ) ) {
        return( ERR_SRC_INVALID_ASSIGN );
    }
    data = GetNextWord1( data, tmp );
    if( *tmp == '\0' ) {
        return( ERR_SRC_INVALID_ASSIGN );
    }
    if( stricmp( tmp, "=" ) != 0 ) {
        return( ERR_SRC_INVALID_ASSIGN );
    }
    data = SkipLeadingSpaces( data );

    if( data[0] == '/' || data[0] == '"' ) {
        check_end = false;
        if( data[0] == '"' ) {
            data = GetNextWord( data, tmp, SingleQuote );
            if( data[0] == '"' ) {
                check_end = true;
            }
        } else {
            data = GetNextWord( data, tmp, SingleSlash );
            if( data[0] == '/' ) {
                check_end = true;
            }
        }
        if( check_end ) {
            for( ++data; data[0] != '\0'; data++ ) {
                switch( data[0] ) {
                case 't':
                    timeflag = true;
                    break;
                case 'r':
                    rxflag = true;
                    break;
                case '$':
                    envflag = true;
                    break;
                case '@':
                    setflag = true;
                    break;
                case 'x':
                    expflag = true;
                    break;
                case 'l':
                    lnumflag = true;
                    break;
                }
            }
        }
        Expand( tmp1, tmp, vl );
    } else {
        data = GetNextWord1( data, tmp );
        if( *tmp == '\0' ) {
            return( ERR_SRC_INVALID_ASSIGN );
        }
        j = Tokenize( StrTokens, tmp, false );
        if( j != TOK_INVALID ) {
            data = GetNextWord1( data, tmp );
            if( *tmp == '\0' ) {
                return( ERR_SRC_INVALID_ASSIGN );
            }
            if( !VarName( tmp1, tmp, vl ) ) {
                return( ERR_SRC_INVALID_ASSIGN );
            }
            v = VarFind( tmp1, vl );
            switch( j ) {
            case STR_T_STRLEN:
                if( v != NULL ) {
                    sprintf( tmp1, "%d", v->len );
                } else {
                    strcpy( tmp1, "0" );
                }
                break;
            case STR_T_SUBSTR:
                data = GetNextWord1( data, tmp );
                if( *tmp == '\0' ) {
                    return( ERR_SRC_INVALID_ASSIGN );
                }
                Expand( tmp1, tmp, vl );
                i = atoi( tmp1 ) - 1;
                data = GetNextWord1( data, tmp );
                if( *tmp == '\0' ) {
                    return( ERR_SRC_INVALID_ASSIGN );
                }
                Expand( tmp1, tmp, vl );
                j = atoi( tmp1 ) - 1;
                if( v == NULL ) {
                    tmp1[0] = '\0';
                    break;
                }
                if( j >= v->len || i < 0 ) {
                    tmp1[0] = '\0';
                } else {
                    l = 0;
                    for( k = i; k <= j; k++ ) {
                        tmp1[l++] = v->value[k];
                    }
                    tmp1[l] = '\0';
                }
                break;
            case STR_T_STRCHR:
                data = GetNextWord1( data, tmp );
                if( *tmp == '\0' ) {
                    return( ERR_SRC_INVALID_ASSIGN );
                }
                Expand( tmp1, tmp, vl );
                if( v == NULL ) {
                    j = -1;
                } else {
                    t = strchr( v->value, tmp1[0] );
                    if( t != NULL ) {
                        j = t - v->value;
                    } else {
                        j = -1;
                    }
                    j++;
                }
                sprintf( tmp1, "%d", j );
                break;
            }
        } else {
            Expand( tmp1, tmp, vl );
        }

    }
    /*
     * regular expression subs.
     */
    if( rxflag && CurrentRegularExpression != NULL ) {
        RegSub( CurrentRegularExpression, tmp1, tmp, CurrentPos.line );
        strcpy( tmp1, tmp );
    }

    /*
     * special processing
     */
    if( envflag ) {
        v1 = getenv( tmp1 );
        if( v1 == NULL ) {
            v1 = "";
        }
    } else if( setflag ) {
        v1 = GetASetVal( tmp1, tmpstr );
    } else if( timeflag ) {
        tod = time( NULL );
        strftime( tmp, sizeof( tmp ), tmp1, localtime( &tod ) );
        v1 = tmp;
    } else if( expflag || lnumflag ) {
        i = setjmp( jmpaddr );
        if( i != 0 ) {
            return( (vi_rc)i );
        }
        StartExprParse( tmp1, jmpaddr );
        val = GetConstExpr();
        if( lnumflag ) {
            fcb         *cfcb;
            line        *cline;
            vi_rc       rc;
            rc = CGimmeLinePtr( val, &cfcb, &cline );
            v1 = cline->data;
            if( rc != ERR_NO_ERR ) {
                v1 = "";
            }
        } else {
            v1 = ltoa( val, tmp1, EditVars.Radix );
        }
    } else {
        v1 = tmp1;
    }

    VarAddStr( name, v1, vl );
    return( ERR_NO_ERR );

} /* SrcAssign */
Example #5
0
/*
 * Expand - expand local variables
 */
char *Expand( char *odata, const char *data, vlist *vl )
{
    char        result[MAX_SRC_LINE];
    char        varname[MAX_SRC_LINE];
    char        *ptr;
    int         paren_level;
    char        ch;
    bool        done;
    bool        has_var;
    vars        *v;
    char        *obptr;
    int         obpos;

    if( recurseDepth >= 3 ) {
        if( odata != data ) {
            strcpy( odata, data );
        }
    } else {
        obpos = bPos;
        obptr = bPtr;
        recurseDepth++;
        bPos = 0;
        bPtr = result;

        for( ; (ch = *data++) != '\0'; ) {
            if( ch == '%' ) {
                ch = *data++;
                if( ch == '\0' )
                    break;
                if( ch == '%' ) {
                    if( !addChar( '%' ) )
                        break;
                    continue;
                }
                has_var = false;
                if( ch != '(' ) {
                    varname[0] = ch;
                    varname[1] = '\0';
                } else {
                    paren_level = 1;
                    ptr = varname;
                    while( (ch = *data) != '\0' ) {
                        data++;
                        if( ch == '%' ) {
                            has_var = true;
                        } else if( ch == '(' ) {
                            paren_level++;
                        } else if( ch == ')' ) {
                            paren_level--;
                            if( paren_level == 0 ) {
                                break;
                            }
                        }
                        *ptr++ = ch;
                    }
                    *ptr = '\0';
                    if( has_var ) {
                        Expand( varname, varname, vl );
                    }
                }

                v = VarFind( varname, vl );
                done = false;
                if( v != NULL && ( !EditFlags.CompileScript || EditFlags.CompileAssignmentsDammit || varname[0] < 'A' || varname[0] > 'Z') ) {
                    // output variable value
                    ptr = v->value;
                    while( (ch = *ptr++) != '\0' ) {
                        if( !addChar( ch ) ) {
                            done = true;
                            break;
                        }
                    }
                    if( done ) {
                        break;
                    }
                } else {
                    // output variable name
                    if( !addChar( '%' ) )
                        break;
                    ptr = varname;
                    if( ptr[1] == '\0' ) {
                        // single character variable name
                        if( !addChar( *ptr ) ) {
                            break;
                        }
                    } else {
                        if( !addChar( '(' ) )
                            break;
                        while( (ch = *ptr++) != '\0' ) {
                            if( !addChar( ch ) ) {
                                done = true;
                                break;
                            }
                        }
                        if( done ) {
                            break;
                        }
                        if( !addChar( ')' ) ) {
                            break;
                        }
                    }
                }
            } else {
                if( !addChar( ch ) ) {
                    break;
                }
            }
        }
        bPtr[bPos++] = '\0';
        memcpy( odata, result, bPos );
        recurseDepth--;
        bPos = obpos;
        bPtr = obptr;
    }
    return( odata );

} /* Expand */