summaryrefslogtreecommitdiff
path: root/lib/command_graph.c
blob: 323bd3e95358c09107fab687fe32d25a8e2c13ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Graph data structure and companion routines for CLI backend.
 *
 * --
 * Copyright (C) 2016 Cumulus Networks, Inc.
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */
#include <zebra.h>
#include "command_graph.h"
#include "memory.h"

struct graph_node *
graphnode_add_child (struct graph_node *parent, struct graph_node *child)
{
  vector_set (parent->children, child);
  child->refs++;
  return child;
}

struct graph_node *
graphnode_new (enum graph_node_type type)
{
  struct graph_node *node =
     XCALLOC(MTYPE_CMD_TOKENS, sizeof(struct graph_node));

  node->type = type;
  node->children = vector_init(VECTOR_MIN_SIZE);

  return node;
}

void
graphnode_delete (struct graph_node *node)
{
  if (!node) return;
  if (node->children) vector_free (node->children);
  if (node->element) free_cmd_element (node->element);
  free (node->text);
  free (node->arg);
  free (node);
}

void
graphnode_delete_graph (struct graph_node *start)
{
  if (start && start->children && vector_active (start->children) > 0)
    {
      for (unsigned int i = 0; i < vector_active (start->children); i++)
        {
          graphnode_delete (vector_slot(start->children, i));
          vector_unset (start->children, i);
        }
    }

  if (--(start->refs) == 0)
    graphnode_delete (start);
}