Back to index

texmacs  1.0.7.15
Classes | Defines | Functions
otl_opt.c File Reference
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "system.h"
#include "error.h"
#include "mem.h"
#include "mfileio.h"
#include "otl_opt.h"

Go to the source code of this file.

Classes

struct  bt_node
struct  otl_opt

Defines

#define FLAG_NOT   (1 << 0)
#define FLAG_AND   (1 << 1)

Functions

static int match_expr (struct bt_node *expr, const char *key)
static struct bt_nodebt_new_tree (void)
static void bt_release_tree (struct bt_node *tree)
static struct bt_nodeparse_expr (char **pp, char *endptr)
otl_optotl_new_opt (void)
void otl_release_opt (otl_opt *opt)
int otl_parse_optstring (otl_opt *opt, const char *optstr)
int otl_match_optrule (otl_opt *opt, const char *tag)

Class Documentation

struct bt_node

Definition at line 38 of file otl_opt.c.

Collaboration diagram for bt_node:
Class Members
char data
int flag
struct bt_node * left
struct bt_node * right
struct otl_opt

Definition at line 213 of file otl_opt.c.

Collaboration diagram for otl_opt:
Class Members
struct bt_node * rule

Define Documentation

#define FLAG_AND   (1 << 1)

Definition at line 48 of file otl_opt.c.

#define FLAG_NOT   (1 << 0)

Definition at line 47 of file otl_opt.c.


Function Documentation

static struct bt_node* bt_new_tree ( void  ) [static, read]

Definition at line 86 of file otl_opt.c.

{
  struct bt_node *expr;

  expr = NEW(1, struct bt_node);
  expr->flag  = 0;
  expr->left  = NULL;
  expr->right = NULL;
  memset(expr->data, 0, 4);

  return expr;
}

Here is the caller graph for this function:

static void bt_release_tree ( struct bt_node tree) [static]

Definition at line 102 of file otl_opt.c.

{
  if (tree) {
    if (tree->left)
      bt_release_tree(tree->left);
    if (tree->right)
      bt_release_tree(tree->right);
    RELEASE(tree);
  }
}

Here is the caller graph for this function:

static int match_expr ( struct bt_node expr,
const char *  key 
) [static]

Definition at line 52 of file otl_opt.c.

{
  int retval = 1;
  int i;

  if (expr) {
    if (!expr->left && !expr->right) {
      for (i = 0; i < 4; i++) {
       if (expr->data[i] != '?' &&
           expr->data[i] != key[i]) {
         retval = 0;
         break;
       }
      }
    } else {
      if (expr->left) {
       retval  = match_expr(expr->left, key);
      }
      if (expr->right) {
       if (retval && (expr->flag & FLAG_AND)) /* and */
         retval &= match_expr(expr->right, key);
       else if (!retval && !(expr->flag & FLAG_AND)) /* or */
         retval  = match_expr(expr->right, key);
      }
    }
    if (expr->flag & FLAG_NOT) /* not */
      retval = retval ? 0 : 1;

  }

  return retval;
}

Here is the caller graph for this function:

int otl_match_optrule ( otl_opt opt,
const char *  tag 
)

Definition at line 335 of file otl_opt.c.

{
  ASSERT(tag);

  if (!opt || !opt->rule)
    return 1;

  return match_expr(opt->rule, tag);
}

Here is the call graph for this function:

Here is the caller graph for this function:

otl_opt* otl_new_opt ( void  )

Definition at line 219 of file otl_opt.c.

{
  struct otl_opt *opt;

  opt = NEW(1, struct otl_opt);
  opt->rule = NULL;

  return (otl_opt *) opt;
}

Here is the caller graph for this function:

int otl_parse_optstring ( otl_opt opt,
const char *  optstr 
)

Definition at line 319 of file otl_opt.c.

{
  char  *p, *endptr;

  ASSERT(opt);

  if (optstr) {
    p      = (char *) optstr;
    endptr = p + strlen(optstr);
    opt->rule = parse_expr(&p, endptr);
  }

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void otl_release_opt ( otl_opt opt)

Definition at line 231 of file otl_opt.c.

{
  if (opt->rule) {
    bt_release_tree(opt->rule);
  }
  opt->rule = NULL;
  RELEASE(opt);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static struct bt_node* parse_expr ( char **  pp,
char *  endptr 
) [static, read]

Definition at line 114 of file otl_opt.c.

{
  struct bt_node *root, *curr;
  
  if (*pp >= endptr)
    return NULL;

  root = curr = bt_new_tree();
  while (*pp < endptr) {
    switch (**pp) {
    case '!':
      if (curr->flag & 2)
        curr->flag &= ~FLAG_NOT;
      else
        curr->flag |=  FLAG_NOT;
      (*pp)++;
      break;
    case '(':
      (*pp)++;
      if (*pp < endptr) {
        struct bt_node *expr;

        expr = parse_expr(pp, endptr);
        if (!expr) {
            WARN("Syntax error: %s\n", *pp);
            return NULL;
        }
        if (**pp != ')') {
            WARN("Syntax error: Unbalanced ()\n");
            return NULL;
           }
        curr->left  = expr->left;
        curr->right = expr->right;
        memcpy(curr->data, expr->data, 4);

        RELEASE(expr);
      } else {
        WARN("Syntax error: Unbalanced ()\n");
        bt_release_tree(root);
        return NULL;
      }
      (*pp)++;
      break;
    case ')':
      return root;
      break;
    case '|': case '&':
      if (*pp >= endptr) {
        WARN("Syntax error: %s\n", *pp);
        bt_release_tree(root);
        return NULL;
      } else {
        struct bt_node *tmp;

        tmp        = bt_new_tree();
        tmp->left  = root;
        tmp->right = curr = bt_new_tree();
        if (**pp == '&')
          tmp->flag = 1;
        else
          tmp->flag = 0;
        root = tmp;
      }
      (*pp)++;
      break;
    case '*':
      memset(curr->data, '?', 4);
      (*pp)++;
      break;
    default:
      if (*pp + 4 <= endptr) {
        int i;

        for (i = 0; i < 4; i++) {
            if (**pp == ' '   || **pp == '?' ||
                isalpha(**pp) || isdigit(**pp))
                curr->data[i] = **pp;
            else if (**pp == '_')
                curr->data[i] = ' ';
            else {
                WARN("Invalid char in tag: %c\n", **pp);
                bt_release_tree(root);
                return NULL;
            }
            (*pp)++;
        }
      } else {
        WARN("Syntax error: %s\n", *pp);
        bt_release_tree(root);
        return NULL;
      }
      break;
    }
  }

  return root;
}

Here is the call graph for this function:

Here is the caller graph for this function: