Exemple #1
0
int readLine(rpmSpec spec, int strip)
{
    char *s;
    int match;
    struct ReadLevelEntry *rl;
    OFI_t *ofi = spec->fileStack;
    int rc;
    int startLine = 0;

    if (!restoreFirstChar(spec)) {
retry:
        if ((rc = readLineFromOFI(spec, ofi)) != 0) {
            if (spec->readStack->next) {
                rpmlog(RPMLOG_ERR, _("line %d: Unclosed %%if\n"),
                       spec->readStack->lineNum);
                rc = PART_ERROR;
            } else if (startLine > 0) {
                rpmlog(RPMLOG_ERR,
                       _("line %d: unclosed macro or bad line continuation\n"),
                       startLine);
                rc = PART_ERROR;
            }
            return rc;
        }
        ofi = spec->fileStack;

        /* Copy next file line into the spec line buffer */
        rc = copyNextLineFromOFI(spec, ofi, strip);
        if (rc > 0) {
            if (startLine == 0)
                startLine = spec->lineNum;
            goto retry;
        } else if (rc < 0) {
            return PART_ERROR;
        }
    }

    copyNextLineFinish(spec, strip);

    s = spec->line;
    SKIPSPACE(s);

    match = -1;
    if (!spec->readStack->reading && ISMACROWITHARG(s, "%if")) {
        match = 0;
    } else if (ISMACROWITHARG(s, "%ifarch")) {
        ARGMATCH(s, "%{_target_cpu}", match);
    } else if (ISMACROWITHARG(s, "%ifnarch")) {
        ARGMATCH(s, "%{_target_cpu}", match);
        match = !match;
    } else if (ISMACROWITHARG(s, "%ifos")) {
        ARGMATCH(s, "%{_target_os}", match);
    } else if (ISMACROWITHARG(s, "%ifnos")) {
        ARGMATCH(s, "%{_target_os}", match);
        match = !match;
    } else if (ISMACROWITHARG(s, "%if")) {
        s += 3;
        match = parseExpressionBoolean(s);
        if (match < 0) {
            rpmlog(RPMLOG_ERR,
                   _("%s:%d: bad %%if condition\n"),
                   ofi->fileName, ofi->lineNum);
            return PART_ERROR;
        }
    } else if (ISMACRO(s, "%else")) {
        if (! spec->readStack->next) {
            /* Got an else with no %if ! */
            rpmlog(RPMLOG_ERR,
                   _("%s:%d: Got a %%else with no %%if\n"),
                   ofi->fileName, ofi->lineNum);
            return PART_ERROR;
        }
        spec->readStack->reading =
            spec->readStack->next->reading && ! spec->readStack->reading;
        spec->line[0] = '\0';
    } else if (ISMACRO(s, "%endif")) {
        if (! spec->readStack->next) {
            /* Got an end with no %if ! */
            rpmlog(RPMLOG_ERR,
                   _("%s:%d: Got a %%endif with no %%if\n"),
                   ofi->fileName, ofi->lineNum);
            return PART_ERROR;
        }
        rl = spec->readStack;
        spec->readStack = spec->readStack->next;
        free(rl);
        spec->line[0] = '\0';
    } else if (spec->readStack->reading && ISMACROWITHARG(s, "%include")) {
        char *fileName, *endFileName, *p;

        fileName = s+8;
        SKIPSPACE(fileName);
        endFileName = fileName;
        SKIPNONSPACE(endFileName);
        p = endFileName;
        SKIPSPACE(p);
        if (*fileName == '\0' || *p != '\0') {
            rpmlog(RPMLOG_ERR, _("%s:%d: malformed %%include statement\n"),
                   ofi->fileName, ofi->lineNum);
            return PART_ERROR;
        }
        *endFileName = '\0';

        ofi = pushOFI(spec, fileName);
        goto retry;
    }

    if (match != -1) {
        rl = xmalloc(sizeof(*rl));
        rl->reading = spec->readStack->reading && match;
        rl->next = spec->readStack;
        rl->lineNum = ofi->lineNum;
        spec->readStack = rl;
        spec->line[0] = '\0';
    }

    if (! spec->readStack->reading) {
        spec->line[0] = '\0';
    }

    /* Collect parsed line */
    if (spec->parsed == NULL)
        spec->parsed = newStringBuf();
    appendStringBufAux(spec->parsed, spec->line,(strip & STRIP_TRAILINGSPACE));

    /* FIX: spec->readStack->next should be dependent */
    return 0;
}
int
main (int argc, char *argv[])
{
    set_program_name (argv[0]);

    /* Not found.  */
    ASSERT (ARGMATCH ("klingon", backup_args, backup_vals) == -1);

    /* Exact match.  */
    ASSERT (ARGMATCH ("none", backup_args, backup_vals) == 1);
    ASSERT (ARGMATCH ("nil", backup_args, backup_vals) == 7);

    /* Too long.  */
    ASSERT (ARGMATCH ("nilpotent", backup_args, backup_vals) == -1);

    /* Abbreviated.  */
    ASSERT (ARGMATCH ("simpl", backup_args, backup_vals) == 3);
    ASSERT (ARGMATCH ("simp", backup_args, backup_vals) == 3);
    ASSERT (ARGMATCH ("sim", backup_args, backup_vals) == 3);

    /* Exact match and abbreviated.  */
    ASSERT (ARGMATCH ("numbered", backup_args, backup_vals) == 9);
    ASSERT (ARGMATCH ("numbere", backup_args, backup_vals) == -2);
    ASSERT (ARGMATCH ("number", backup_args, backup_vals) == -2);
    ASSERT (ARGMATCH ("numbe", backup_args, backup_vals) == -2);
    ASSERT (ARGMATCH ("numb", backup_args, backup_vals) == -2);
    ASSERT (ARGMATCH ("num", backup_args, backup_vals) == -2);
    ASSERT (ARGMATCH ("nu", backup_args, backup_vals) == -2);
    ASSERT (ARGMATCH ("n", backup_args, backup_vals) == -2);

    /* Ambiguous abbreviated.  */
    ASSERT (ARGMATCH ("ne", backup_args, backup_vals) == -2);

    /* Ambiguous abbreviated, but same value.  */
    ASSERT (ARGMATCH ("si", backup_args, backup_vals) == 3);
    ASSERT (ARGMATCH ("s", backup_args, backup_vals) == 3);

    return 0;
}