gwenhywfar  5.10.1
p_checkprogs.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Mon Feb 08 2021
3  copyright : (C) 2021 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * Please see toplevel file COPYING for license details *
8  ***************************************************************************/
9 
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 
17 
18 #include <gwenhywfar/debug.h>
19 #include <gwenhywfar/directory.h>
20 
21 
22 
23 static int _parseChildNodes(GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWEN_XMLNODE *xmlNode);
24 static int _parseProg(GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWEN_XMLNODE *xmlNode);
25 static int _determineProgPath(GWEN_DB_NODE *db, const char *sCmd, const char *sId,
26  const GWEN_STRINGLIST *slAltNames, const GWEN_STRINGLIST *slPaths);
27 static int _determineNamePath(GWEN_DB_NODE *db, const char *sCmd, const char *sId, const GWEN_STRINGLIST *slPaths);
28 
29 
30 
31 
32 
33 
34 int GWB_ParseCheckProgs(GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWEN_XMLNODE *xmlNode)
35 {
36  int rv;
37 
38  rv=_parseChildNodes(project, currentContext, xmlNode);
39  if (rv<0) {
40  DBG_INFO(NULL, "here (%d)", rv);
41  return rv;
42  }
43 
44  return 0;
45 }
46 
47 
48 
49 int _parseChildNodes(GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWEN_XMLNODE *xmlNode)
50 {
51  GWEN_XMLNODE *n;
52 
53  n=GWEN_XMLNode_GetFirstTag(xmlNode);
54  while (n) {
55  const char *name;
56 
57  name=GWEN_XMLNode_GetData(n);
58  if (name && *name) {
59  int rv;
60 
61  DBG_DEBUG(NULL, "Handling element \"%s\"", name);
62 
63  if (strcasecmp(name, "prog")==0)
64  rv=_parseProg(project, currentContext, n);
65  else {
66  DBG_INFO(NULL, "Element not handled");
67  rv=0;
68  }
69  if (rv<0) {
70  DBG_ERROR(GWEN_LOGDOMAIN, "Error in element \"%s\", aborting", name);
71  return rv;
72  }
73  }
74 
76  }
77 
78  return 0;
79 }
80 
81 
82 
83 int _parseProg(GWEN_UNUSED GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWEN_XMLNODE *xmlNode)
84 {
85  int rv;
86  const char *sCmd;
87  const char *sId;
88  GWEN_XMLNODE *n;
89  GWEN_DB_NODE *db;
90  GWEN_STRINGLIST *slAltNames=NULL;
91  GWEN_STRINGLIST *slPaths=NULL;
92 
93 
94  db=GWB_Context_GetVars(currentContext);
95 
96  rv=GWEN_XMLNode_ExpandProperties(xmlNode, db);
97  if (rv<0) {
98  DBG_INFO(NULL, "here (%d)", rv);
99  return rv;
100  }
101 
102  sCmd=GWEN_XMLNode_GetProperty(xmlNode, "cmd", NULL);
103  if (!(sCmd && *sCmd)) {
104  DBG_ERROR(NULL, "No cmd in <prog>");
105  return GWEN_ERROR_GENERIC;
106  }
107 
108  sId=GWEN_XMLNode_GetProperty(xmlNode, "id", NULL);
109  if (!(sId && *sId)) {
110  DBG_ERROR(NULL, "No id in <prog>");
111  return GWEN_ERROR_GENERIC;
112  }
113 
114  n=GWEN_XMLNode_FindFirstTag(xmlNode, "alternativeNames", NULL, NULL);
115  if (n)
116  slAltNames=GWB_Parser_ReadXmlDataIntoStringList(GWB_Context_GetVars(currentContext), n);
117 
118  n=GWEN_XMLNode_FindFirstTag(xmlNode, "paths", NULL, NULL);
119  if (n)
120  slPaths=GWB_Parser_ReadXmlDataIntoStringList(GWB_Context_GetVars(currentContext), n);
121  else {
123  if (slPaths==NULL)
124  slPaths=GWEN_StringList_fromString("/usr/local/bin:/usr/bin:/bin", ": ", 0);
125  }
126 
127  rv=_determineProgPath(GWB_Context_GetVars(currentContext), sCmd, sId, slAltNames, slPaths);
128  if (rv==1) {
129  GWB_Parser_SetItemValue(db, sId, "_EXISTS", "TRUE");
130  fprintf(stdout, " prog %s: found\n", sCmd);
131  }
132  else {
133  GWB_Parser_SetItemValue(GWB_Context_GetVars(currentContext), sId, "_EXISTS", "FALSE");
134  fprintf(stdout, " prog %s: not found\n", sCmd);
135  }
136  return 0;
137 }
138 
139 
140 
141 /* Result <0: ERROR, ==0: Not found, >0; Found */
142 int _determineProgPath(GWEN_DB_NODE *db, const char *sCmd, const char *sId,
143  const GWEN_STRINGLIST *slAltNames, const GWEN_STRINGLIST *slPaths)
144 {
145  int rv;
146 
147  rv=_determineNamePath(db, sCmd, sId, slPaths);
148  if (rv<0) {
149  DBG_INFO(NULL, "here (%d)", rv);
150  return rv;
151  }
152  else if (rv==1) {
153  DBG_DEBUG(NULL, "Found path for prog \"%s\"", sCmd);
154  return 1;
155  }
156  else {
157  if (slAltNames) {
158  const GWEN_STRINGLISTENTRY *se;
159 
160  se=GWEN_StringList_FirstEntry(slAltNames);
161  while(se) {
162  const char *s;
163 
165  if (s && *s) {
166  rv=_determineNamePath(db, sCmd, sId, slPaths);
167  if (rv<0) {
168  DBG_INFO(NULL, "here (%d)", rv);
169  return rv;
170  }
171  else if (rv>0) {
172  DBG_DEBUG(NULL, "Found path for prog \"%s\"", sCmd);
173  return 1;
174  }
175  }
177  }
178  }
179  }
180 
181  return 0;
182 }
183 
184 
185 
186 /* Result <0: ERROR, ==0: Not found, >0; Found */
187 int _determineNamePath(GWEN_DB_NODE *db, const char *sCmd, const char *sId, const GWEN_STRINGLIST *slPaths)
188 {
189  GWEN_BUFFER *dbuf;
190  int rv;
191 
192  dbuf=GWEN_Buffer_new(0, 256, 0, 1);
193  rv=GWEN_Directory_FindFileInPaths(slPaths, sCmd, dbuf);
194  if (rv<0) {
195  if (rv==GWEN_ERROR_NOT_FOUND) {
196  DBG_DEBUG(NULL, "Command \"%s\" not found", sCmd);
197  GWEN_Buffer_free(dbuf);
198  return 0;
199  }
200  else {
201  DBG_DEBUG(NULL, "ERROR on GWEN_Directory_FindFileInPaths(\"%s\"): %d", sCmd, rv);
202  GWEN_Buffer_free(dbuf);
203  return rv;
204  }
205  }
206 
208  GWEN_Buffer_free(dbuf);
209  return 1;
210 }
211 
212 
213 
214 
215 
216 
static int _determineNamePath(GWEN_DB_NODE *db, const char *sCmd, const char *sId, const GWEN_STRINGLIST *slPaths)
Definition: p_checkprogs.c:187
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
struct GWEN_STRINGLISTENTRYSTRUCT GWEN_STRINGLISTENTRY
Definition: stringlist.h:53
struct GWB_CONTEXT GWB_CONTEXT
Definition: context.h:17
#define GWEN_DB_FLAGS_OVERWRITE_VARS
Definition: db.h:121
GWEN_STRINGLIST * GWB_Parser_ReadXmlDataIntoStringList(GWEN_DB_NODE *db, GWEN_XMLNODE *xmlNode)
Definition: parser.c:283
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
const char * GWEN_XMLNode_GetProperty(const GWEN_XMLNODE *n, const char *name, const char *defaultValue)
Definition: xml.c:239
static int _parseProg(GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWEN_XMLNODE *xmlNode)
#define NULL
Definition: binreloc.c:300
struct GWB_PROJECT GWB_PROJECT
Definition: project.h:14
#define GWEN_LOGDOMAIN
Definition: logger.h:35
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
GWEN_STRINGLIST * GWBUILD_GetPathFromEnvironment()
Definition: gwenbuild.c:154
GWEN_STRINGLISTENTRY * GWEN_StringList_FirstEntry(const GWEN_STRINGLIST *sl)
Definition: stringlist.c:390
const char * GWEN_StringListEntry_Data(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:406
GWEN_XMLNODE * GWEN_XMLNode_FindFirstTag(const GWEN_XMLNODE *n, const char *tname, const char *pname, const char *pvalue)
Definition: xml.c:776
GWEN_DB_NODE * GWB_Context_GetVars(const GWB_CONTEXT *ctx)
Definition: context.c:427
#define DBG_DEBUG(dbg_logger, format, args...)
Definition: debug.h:214
GWEN_XMLNODE * GWEN_XMLNode_GetNextTag(const GWEN_XMLNODE *n)
Definition: xml.c:712
GWENHYWFAR_API int GWEN_Directory_FindFileInPaths(const GWEN_STRINGLIST *paths, const char *filePath, GWEN_BUFFER *fbuf)
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:56
#define GWEN_ERROR_GENERIC
Definition: error.h:62
GWEN_STRINGLIST * GWEN_StringList_fromString(const char *str, const char *delimiters, int checkDouble)
Definition: stringlist.c:746
static int _parseChildNodes(GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWEN_XMLNODE *xmlNode)
Definition: p_checkprogs.c:49
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
GWEN_XMLNODE * GWEN_XMLNode_GetFirstTag(const GWEN_XMLNODE *n)
Definition: xml.c:705
const char * GWEN_XMLNode_GetData(const GWEN_XMLNODE *n)
Definition: xml.c:370
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
Definition: db.c:997
GWEN_STRINGLISTENTRY * GWEN_StringListEntry_Next(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:398
#define GWEN_ERROR_NOT_FOUND
Definition: error.h:89
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:181
void GWB_Parser_SetItemValue(GWEN_DB_NODE *db, const char *sId, const char *suffix, const char *value)
Definition: parser.c:856
int GWEN_XMLNode_ExpandProperties(const GWEN_XMLNODE *n, GWEN_DB_NODE *dbVars)
Definition: xml.c:634
int GWB_ParseCheckProgs(GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWEN_XMLNODE *xmlNode)
Definition: p_checkprogs.c:34
struct GWEN__XMLNODE GWEN_XMLNODE
Definition: xml.h:156
#define GWEN_UNUSED
static int _determineProgPath(GWEN_DB_NODE *db, const char *sCmd, const char *sId, const GWEN_STRINGLIST *slAltNames, const GWEN_STRINGLIST *slPaths)
Definition: p_checkprogs.c:142