예제 #1
0
static void list_top(char *fn)
{
  int status,done;
#define BUFLEN 256
  char buf[BUFLEN];
  gmx_cpp_t handle;
  char *cppopts[] = { NULL };

  status = cpp_open_file(fn,&handle,cppopts);
  if (status != 0) 
    gmx_fatal(FARGS,cpp_error(&handle,status));
  do {
    status = cpp_read_line(&handle,BUFLEN,buf);
    done = (status == eCPP_EOF);
    if (!done) {
      if (status != eCPP_OK)
	gmx_fatal(FARGS,cpp_error(&handle,status));
      else 
	printf("%s\n",buf);
    }
  } while (!done);
  status = cpp_close_file(&handle);
  if (status != eCPP_OK) 
    gmx_fatal(FARGS,cpp_error(&handle,status));
}
예제 #2
0
static int
process_directive(gmx_cpp_t *handlep, const char *dname, const char *dval)
{
    gmx_cpp_t    handle = (gmx_cpp_t)*handlep;
    int          i, i0, len, status;
    unsigned int i1;
    char        *inc_fn, *name;
    const char  *ptr;
    int          bIfdef, bIfndef;

    /* #ifdef or ifndef statement */
    bIfdef  = (strcmp(dname, "ifdef") == 0);
    bIfndef = (strcmp(dname, "ifndef") == 0);
    if (bIfdef || bIfndef)
    {
        if ((handle->nifdef > 0) && (handle->ifdefs[handle->nifdef-1] != eifTRUE))
        {
            handle->nifdef++;
            srenew(handle->ifdefs, handle->nifdef);
            handle->ifdefs[handle->nifdef-1] = eifIGNORE;
        }
        else
        {
            snew(name, strlen(dval)+1);
            sscanf(dval, "%s", name);
            for (i = 0; (i < ndef); i++)
            {
                if (strcmp(defs[i].name, name) == 0)
                {
                    break;
                }
            }
            handle->nifdef++;
            srenew(handle->ifdefs, handle->nifdef);
            if ((bIfdef && (i < ndef)) || (bIfndef && (i == ndef)))
            {
                handle->ifdefs[handle->nifdef-1] = eifTRUE;
            }
            else
            {
                handle->ifdefs[handle->nifdef-1] = eifFALSE;
            }
            sfree(name);
        }
        return eCPP_OK;
    }

    /* #else statement */
    if (strcmp(dname, "else") == 0)
    {
        if (handle->nifdef <= 0)
        {
            return eCPP_SYNTAX;
        }
        if (handle->ifdefs[handle->nifdef-1] == eifTRUE)
        {
            handle->ifdefs[handle->nifdef-1] = eifFALSE;
        }
        else if (handle->ifdefs[handle->nifdef-1] == eifFALSE)
        {
            handle->ifdefs[handle->nifdef-1] = eifTRUE;
        }
        return eCPP_OK;
    }

    /* #endif statement */
    if (strcmp(dname, "endif") == 0)
    {
        if (handle->nifdef <= 0)
        {
            return eCPP_SYNTAX;
        }
        handle->nifdef--;
        return eCPP_OK;
    }

    /* Check whether we're not ifdeffed out. The order of this statement
       is important. It has to come after #ifdef, #else and #endif, but
       anything else should be ignored. */
    if (is_ifdeffed_out(handle))
    {
        return eCPP_OK;
    }

    /* Check for include statements */
    if (strcmp(dname, "include") == 0)
    {
        len = -1;
        i0  = 0;
        for (i1 = 0; (i1 < strlen(dval)); i1++)
        {
            if ((dval[i1] == '"') || (dval[i1] == '<') || (dval[i1] == '>'))
            {
                if (len == -1)
                {
                    i0  = i1+1;
                    len = 0;
                }
                else
                {
                    break;
                }
            }
            else if (len >= 0)
            {
                len++;
            }
        }
        if (len == -1)
        {
            return eCPP_SYNTAX;
        }
        snew(inc_fn, len+1);
        strncpy(inc_fn, dval+i0, len);
        inc_fn[len] = '\0';

        if (debug)
        {
            fprintf(debug, "Going to open include file '%s' i0 = %d, strlen = %d\n",
                    inc_fn, i0, len);
        }
        /* Open include file and store it as a child in the handle structure */
        status = cpp_open_file(inc_fn, &(handle->child), NULL);
        sfree(inc_fn);
        if (status != eCPP_OK)
        {
            handle->child = NULL;
            return status;
        }
        /* Make a linked list of open files and move on to the include file */
        handle->child->parent = handle;
        *handlep              = handle->child;
        handle                = *handlep;
        return eCPP_OK;
    }

    /* #define statement */
    if (strcmp(dname, "define") == 0)
    {
        /* Split it into name and value. */
        ptr = dval;
        while ((*ptr != '\0') && !isspace(*ptr))
        {
            ptr++;
        }
        name = gmx_strndup(dval, ptr - dval);

        while ((*ptr != '\0') && isspace(*ptr))
        {
            ptr++;
        }

        add_define(name, ptr);
        sfree(name);
        return eCPP_OK;
    }

    /* #undef statement */
    if (strcmp(dname, "undef") == 0)
    {
        snew(name, strlen(dval)+1);
        sscanf(dval, "%s", name);
        for (i = 0; (i < ndef); i++)
        {
            if (strcmp(defs[i].name, name) == 0)
            {
                sfree(defs[i].name);
                sfree(defs[i].def);
                break;
            }
        }
        sfree(name);
        for (; (i < ndef-1); i++)
        {
            defs[i].name = defs[i+1].name;
            defs[i].def  = defs[i+1].def;
        }
        ndef--;

        return eCPP_OK;
    }

    /* If we haven't matched anything, this is an unknown directive */
    return eCPP_SYNTAX;
}