1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /*                                                                           */
3 /*   File....: rdefpar.c                                                     */
4 /*   Name....: Read Definition / Parameter                                   */
5 /*   Author..: Thorsten Koch                                                 */
6 /*   Copyright by Author, All rights reserved                                */
7 /*                                                                           */
8 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
9 /*
10  * Copyright (C) 2001-2018 by Thorsten Koch <koch@zib.de>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 3
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 
32 #include <stdbool.h>
33 #include "zimpl/mshell.h"
34 #include "zimpl/mme.h"
35 #include "zimpl/strstore.h"
36 #include "zimpl/rdefpar.h"
37 
38 #define RDEF_SID     0x52446566
39 #define RPAR_SID     0x52506172
40 
41 enum read_param_type { RPAR_ERR = 0, RPAR_SKIP, RPAR_USE, RPAR_CMNT, RPAR_MTCH };
42 
43 typedef enum read_param_type   RParType;
44 typedef union read_param_value RParVal;
45 
46 union read_param_value
47 {
48    int         i;
49    const char* s;
50 };
51 
52 struct read_param
53 {
54    SID
55    RParType type;
56    RParVal  val;
57 };
58 
59 struct read_definition
60 {
61    SID
62    const char* filename;
63    const char* pattern;  /* this was named "template", but template */
64    const char* comment;  /* is a C++ reserved word */
65    const char* match;
66    int         use;
67    int         skip;
68    int         refc;
69 };
70 
rdef_new(const char * filename,const char * pattern)71 RDef* rdef_new(const char* filename, const char* pattern)
72 {
73    RDef* rdef = calloc(1, sizeof(*rdef));
74 
75    assert(filename != NULL);
76    assert(pattern  != NULL);
77    assert(rdef     != NULL);
78 
79    rdef->filename = filename;
80    rdef->pattern  = pattern;
81    rdef->comment  = str_new("");
82    rdef->match    = NULL;
83    rdef->skip     = 0;
84    rdef->use      = -1;
85    rdef->refc     = 1;
86 
87    SID_set(rdef, RDEF_SID);
88 
89    assert(rdef_is_valid(rdef));
90 
91    return rdef;
92 }
93 
rdef_free(RDef * rdef)94 void rdef_free(RDef* rdef)
95 {
96    assert(rdef_is_valid(rdef));
97 
98    rdef->refc--;
99 
100    if (rdef->refc == 0)
101    {
102       SID_del(rdef);
103 
104       free(rdef);
105    }
106 }
107 
rdef_is_valid(const RDef * rdef)108 bool rdef_is_valid(const RDef* rdef)
109 {
110    return ((rdef != NULL)
111       && SID_ok(rdef, RDEF_SID)
112       && (rdef->filename != NULL)
113       && (rdef->pattern  != NULL)
114       && (rdef->comment  != NULL));
115 }
116 
rdef_copy(const RDef * source)117 RDef* rdef_copy(const RDef* source)
118 {
119    RDef* rdef = (RDef*)source;
120 
121    assert(rdef_is_valid(rdef));
122 
123    rdef->refc++;
124 
125    return rdef;
126 }
127 
rdef_set_param(RDef * rdef,const RPar * rpar)128 void rdef_set_param(RDef* rdef, const RPar* rpar)
129 {
130    assert(rdef_is_valid(rdef));
131    assert(rpar_is_valid(rpar));
132 
133    switch(rpar->type)
134    {
135    case RPAR_SKIP :
136       rdef->skip = rpar->val.i;
137       break;
138    case RPAR_USE :
139       rdef->use  = rpar->val.i;
140       break;
141    case RPAR_CMNT :
142       rdef->comment = rpar->val.s;
143       break;
144    case RPAR_MTCH :
145       rdef->match = rpar->val.s;
146       break;
147    case RPAR_ERR :
148    default :
149       abort();
150    }
151 }
152 
rdef_get_filename(const RDef * rdef)153 const char* rdef_get_filename(const RDef* rdef)
154 {
155    assert(rdef_is_valid(rdef));
156 
157    return rdef->filename;
158 }
159 
rdef_get_pattern(const RDef * rdef)160 const char* rdef_get_pattern(const RDef* rdef)
161 {
162    assert(rdef_is_valid(rdef));
163 
164    return rdef->pattern;
165 }
166 
rdef_get_comment(const RDef * rdef)167 const char* rdef_get_comment(const RDef* rdef)
168 {
169    assert(rdef_is_valid(rdef));
170 
171    return rdef->comment;
172 }
173 
rdef_get_match(const RDef * rdef)174 const char* rdef_get_match(const RDef* rdef)
175 {
176    assert(rdef_is_valid(rdef));
177 
178    return rdef->match;
179 }
180 
rdef_get_use(const RDef * rdef)181 int rdef_get_use(const RDef* rdef)
182 {
183    assert(rdef_is_valid(rdef));
184 
185    return rdef->use;
186 }
187 
rdef_get_skip(const RDef * rdef)188 int rdef_get_skip(const RDef* rdef)
189 {
190    assert(rdef_is_valid(rdef));
191 
192    return rdef->skip;
193 }
194 
195 /* ----------------------------------------------------------------------------
196  * Read Parameter
197  * ----------------------------------------------------------------------------
198  */
rpar_new_skip(int skip)199 RPar* rpar_new_skip(int skip)
200 {
201    RPar* rpar = calloc(1, sizeof(*rpar));
202 
203    assert(rpar != NULL);
204 
205    rpar->type  = RPAR_SKIP;
206    rpar->val.i = skip;
207 
208    SID_set(rpar, RPAR_SID);
209 
210    assert(rpar_is_valid(rpar));
211 
212    return rpar;
213 }
214 
rpar_new_use(int use)215 RPar* rpar_new_use(int use)
216 {
217    RPar* rpar = calloc(1, sizeof(*rpar));
218 
219    assert(rpar != NULL);
220 
221    rpar->type  = RPAR_USE;
222    rpar->val.i = use;
223 
224    SID_set(rpar, RPAR_SID);
225 
226    assert(rpar_is_valid(rpar));
227 
228    return rpar;
229 }
230 
rpar_new_comment(const char * comment)231 RPar* rpar_new_comment(const char* comment)
232 {
233    RPar* rpar = calloc(1, sizeof(*rpar));
234 
235    assert(rpar    != NULL);
236    assert(comment != NULL);
237 
238    rpar->type  = RPAR_CMNT;
239    rpar->val.s = comment;
240 
241    SID_set(rpar, RPAR_SID);
242 
243    assert(rpar_is_valid(rpar));
244 
245    return rpar;
246 }
247 
rpar_new_match(const char * match)248 RPar* rpar_new_match(const char* match)
249 {
250    RPar* rpar = calloc(1, sizeof(*rpar));
251 
252    assert(rpar  != NULL);
253    assert(match != NULL);
254 
255    rpar->type  = RPAR_MTCH;
256    rpar->val.s = match;
257 
258    SID_set(rpar, RPAR_SID);
259 
260    assert(rpar_is_valid(rpar));
261 
262    return rpar;
263 }
264 
rpar_free(RPar * rpar)265 void rpar_free(RPar* rpar)
266 {
267    assert(rpar_is_valid(rpar));
268 
269    SID_del(rpar);
270 
271    free(rpar);
272 }
273 
rpar_is_valid(const RPar * rpar)274 bool rpar_is_valid(const RPar* rpar)
275 {
276    return ((rpar != NULL) && SID_ok(rpar, RPAR_SID)
277       && (rpar->type != RPAR_ERR));
278 }
279 
rpar_copy(const RPar * rpar)280 RPar* rpar_copy(const RPar* rpar)
281 {
282    RPar* rpnew = calloc(1, sizeof(*rpar));
283 
284    assert(rpar  != NULL);
285    assert(rpnew != NULL);
286 
287    rpnew->type = rpar->type;
288    rpnew->val  = rpar->val;
289 
290    SID_set(rpnew, RPAR_SID);
291 
292    assert(rpar_is_valid(rpnew));
293 
294    return rpnew;
295 }
296 
297