Imported Upstream version 2.4.3
[platform/upstream/audit.git] / auparse / nvlist.c
1 /*
2 * nvlist.c - Minimal linked list library for name-value pairs
3 * Copyright (c) 2006-07 Red Hat Inc., Durham, North Carolina.
4 * All Rights Reserved. 
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 *
20 * Authors:
21 *   Steve Grubb <sgrubb@redhat.com>
22 */
23
24 #include "config.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include "nvlist.h"
28 #include "interpret.h"
29 #include "auparse-idata.h"
30
31
32 void nvlist_create(nvlist *l)
33 {
34         l->head = NULL;
35         l->cur = NULL;
36         l->cnt = 0;
37 }
38
39 static void nvlist_last(nvlist *l)
40 {
41         register nvnode* window;
42         
43         if (l->head == NULL)
44                 return;
45
46         window = l->head;
47         while (window->next)
48                 window = window->next;
49         l->cur = window;
50 }
51
52 nvnode *nvlist_next(nvlist *l)
53 {
54         if (l->cur)
55                 l->cur = l->cur->next;
56         return l->cur;
57 }
58
59 void nvlist_append(nvlist *l, nvnode *node)
60 {
61         nvnode* newnode = malloc(sizeof(nvnode));
62
63         newnode->name = node->name;
64         newnode->val = node->val;
65         newnode->interp_val = NULL;
66         newnode->item = l->cnt; 
67         newnode->next = NULL;
68
69         // if we are at top, fix this up
70         if (l->head == NULL)
71                 l->head = newnode;
72         else {  // Otherwise add pointer to newnode
73                 if (l->cnt == (l->cur->item+1)) {
74                         l->cur->next = newnode;
75                 }
76                 else {
77                         nvlist_last(l);
78                         l->cur->next = newnode;
79                 }
80         }
81
82         // make newnode current
83         l->cur = newnode;
84         l->cnt++;
85 }
86
87 /*
88  * This function will start at current index and scan for a name
89  */
90 int nvlist_find_name(nvlist *l, const char *name)
91 {
92         register nvnode* window = l->cur;
93
94         while (window) {
95                 if (strcmp(window->name, name) == 0) {
96                         l->cur = window;
97                         return 1;
98                 }
99                 else
100                         window = window->next;
101         }
102         return 0;
103 }
104
105 extern int interp_adjust_type(int rtype, const char *name, const char *val);
106 int nvlist_get_cur_type(const rnode *r)
107 {
108         const nvlist *l = &r->nv;
109         return auparse_interp_adjust_type(r->type, l->cur->name, l->cur->val);
110 }
111
112 const char *nvlist_interp_cur_val(const rnode *r)
113 {
114         const nvlist *l = &r->nv;
115         if (l->cur->interp_val)
116                 return l->cur->interp_val;
117         return interpret(r);
118 }
119
120 void nvlist_clear(nvlist* l)
121 {
122         nvnode* nextnode;
123         register nvnode* current;
124
125         current = l->head;
126         while (current) {
127                 nextnode=current->next;
128                 free(current->name);
129                 free(current->val);
130                 free(current->interp_val);
131                 free(current);
132                 current=nextnode;
133         }
134         l->head = NULL;
135         l->cur = NULL;
136         l->cnt = 0;
137 }