_nlist.c 3.77 KB
Newer Older
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
Michael Meeks's avatar
Michael Meeks committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

20 21 22 23 24 25 26 27 28 29
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpp.h"

extern int Cplusplus;
Nlist *kwdefined;
char wd[128];

/*
30 31 32 33
    ER: Table was made extra large, because there seems to be a problem with the
    chaining. An nlist->next is sometimes overwritten somewhere, which
    results in a SIGSEGV. I canceled the GDB with watchpoint after 2 days, though..
    It works this way for now..
34 35 36 37 38 39 40
 */
#define NLSIZE 15000

static Nlist *nlist[NLSIZE];

struct kwtab
{
41 42 43
    char * const kw;
    int const val;
    int const flag;
Stephan Bergmann's avatar
Stephan Bergmann committed
44
};
45

Stephan Bergmann's avatar
Stephan Bergmann committed
46
static struct kwtab kwtab[] =
47
{
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        {"if", KIF, ISKW},
        {"ifdef", KIFDEF, ISKW},
        {"ifndef", KIFNDEF, ISKW},
        {"elif", KELIF, ISKW},
        {"else", KELSE, ISKW},
        {"endif", KENDIF, ISKW},
        {"include", KINCLUDE, ISKW},
        {"include_next", KINCLUDENEXT, ISKW},
        {"import", KIMPORT, ISKW},
        {"define", KDEFINE, ISKW},
        {"undef", KUNDEF, ISKW},
        {"line", KLINE, ISKW},
        {"error", KERROR, ISKW},
        {"pragma", KPRAGMA, ISKW},
        {"ident", KIDENT, ISKW},
        {"eval", KEVAL, ISKW},
        {"defined", KDEFINED, ISDEFINED + ISUNCHANGE},
        {"machine", KMACHINE, ISDEFINED + ISUNCHANGE},
        {"__LINE__", KLINENO, ISMAC + ISUNCHANGE},
        {"__FILE__", KFILE, ISMAC + ISUNCHANGE},
        {"__DATE__", KDATE, ISMAC + ISUNCHANGE},
        {"__TIME__", KTIME, ISMAC + ISUNCHANGE},
        {"__STDC__", KSTDC, ISUNCHANGE},
71
        {NULL, 0, 0}
72 73 74 75 76 77 78 79 80 81
};

unsigned long namebit[077 + 1];

void
    setup_kwtab(void)
{
    struct kwtab *kp;
    Nlist *np;
    Token t;
82
    static Token deftoken[1] = {{NAME, 0, 7, (uchar *) "defined", 0}};
83 84 85 86 87 88 89
    static Tokenrow deftr = {deftoken, deftoken, deftoken + 1, 1};

    for (kp = kwtab; kp->kw; kp++)
    {
        t.t = (uchar *) kp->kw;
        t.len = strlen(kp->kw);
        np = lookup(&t, 1);
90 91
        np->flag = (char) kp->flag;
        np->val = (char) kp->val;
92 93 94 95 96
        if (np->val == KDEFINED)
        {
            kwdefined = np;
            np->val = NAME;
            np->vp = &deftr;
97
            np->ap = NULL;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        }
    }
}

Nlist *
    lookup(Token * tp, int install)
{
    unsigned int h;
    Nlist *np;
    uchar *cp, *cpe;

    h = 0;
    for (cp = tp->t, cpe = cp + tp->len; cp < cpe;)
        h += *cp++;
    h %= NLSIZE;
    np = nlist[h];
    while (np)
    {
        if (*tp->t == *np->name && tp->len == (unsigned int)np->len
            && strncmp((char *)tp->t, (char *)np->name, tp->len) == 0)
            return np;
        np = np->next;
    }
    if (install)
    {
        np = new(Nlist);
        np->vp = NULL;
        np->ap = NULL;
        np->flag = 0;
        np->val = 0;
        np->len = tp->len;
        np->name = newstring(tp->t, tp->len, 0);
        np->next = nlist[h];
        nlist[h] = np;
        quickset(tp->t[0], tp->len > 1 ? tp->t[1] : 0);
        return np;
    }
    return NULL;
}
137 138

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */