gwenhywfar  5.10.1
buildctx_depfile.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 #define DISABLE_DEBUGLOG
15 
16 
17 #include "gwenbuild/buildctx/buildctx_p.h"
20 
21 #include <gwenhywfar/debug.h>
22 #include <gwenhywfar/text.h>
23 #include <gwenhywfar/directory.h>
24 
25 #include <unistd.h>
26 #include <ctype.h>
27 
28 
29 
30 static GWEN_STRINGLIST *_readDepFile(const char *fileName);
31 static GWEN_STRINGLIST *_makeAbsolutePaths(GWEN_STRINGLIST *slInput, const char *folder);
33 
34 
35 
36 GWEN_STRINGLIST *GWB_BuildCtx_ReadAndTranslateDepfile(const char *folder, const char *fileName)
37 {
38  GWEN_STRINGLIST *slInput;
39 
40  slInput=_readDepFile(fileName);
41  if (slInput) {
42  GWEN_STRINGLIST *slOutput;
43 
44  slOutput=_makeAbsolutePaths(slInput, folder);
45  if (slOutput) {
46  GWEN_StringList_free(slInput);
47  return slOutput;
48  }
49  GWEN_StringList_free(slInput);
50  }
51 
52  return NULL;
53 }
54 
55 
56 
57 GWEN_STRINGLIST *_readDepFile(const char *fileName)
58 {
59  GWEN_BUFFER *fileBuffer;
60  int rv;
61  char *s;
62 
63  fileBuffer=GWEN_Buffer_new(0, 256, 0, 1);
64  rv=GWEN_SyncIo_Helper_ReadFile(fileName, fileBuffer);
65  if (rv<0) {
66  DBG_ERROR(NULL, "here (%d)", rv);
67  GWEN_Buffer_free(fileBuffer);
68  return NULL;
69  }
71  GWEN_Text_CondenseBuffer(fileBuffer);
72 
73  s=strchr(GWEN_Buffer_GetStart(fileBuffer), ':');
74  if (s) {
75  GWEN_STRINGLIST *slDependencies;
76 
77  slDependencies=GWEN_StringList_fromString2(s+1, " ",
78  1,
84  if (slDependencies) {
85  GWEN_Buffer_free(fileBuffer);
86  return slDependencies;
87  }
88  }
89  GWEN_Buffer_free(fileBuffer);
90  return NULL;
91 }
92 
93 
94 
96 {
97  char *ptr;
98 
99  ptr=GWEN_Buffer_GetStart(buffer);
100  while(*ptr) {
101  char *ptrNextEscape;
102 
103  ptrNextEscape=strchr(ptr, '\\');
104  if (ptrNextEscape) {
105  if (iscntrl(ptrNextEscape[1])) {
106  /* overwrite backslash */
107  *(ptrNextEscape++)=' ';
108  /* overwrite every immediately following control character (such as LF, CR, TAB etc) */
109  while(*ptrNextEscape && iscntrl(*ptrNextEscape))
110  *(ptrNextEscape++)=' ';
111  }
112  }
113  else
114  break;
115  }
116 }
117 
118 
119 
120 GWEN_STRINGLIST *_makeAbsolutePaths(GWEN_STRINGLIST *slInput, const char *folder)
121 {
123 
124  se=GWEN_StringList_FirstEntry(slInput);
125  if (se) {
126  GWEN_STRINGLIST *slOutput;
127 
128  slOutput=GWEN_StringList_new();
129  while(se) {
130  const char *s;
131 
133  if (s) {
134  while(*s && *s<33)
135  s++;
136  if (*s) {
137  if (*s=='/') {
138  GWEN_BUFFER *buf;
139 
140  buf=GWEN_Buffer_new(0, 256, 0, 1);
141  while(*s && *s>31)
142  GWEN_Buffer_AppendByte(buf, *(s++));
144  GWEN_Buffer_free(buf);
145  }
146  else {
147  const char *ptrToSlash;
148 
149  ptrToSlash=strrchr(s, '/');
150  if (ptrToSlash) {
151  GWEN_BUFFER *buf;
152  GWEN_BUFFER *absBuf;
153 
154  /* get absolute path for folder */
155  buf=GWEN_Buffer_new(0, 256, 0, 1);
156  if (folder) {
157  GWEN_Buffer_AppendString(buf, folder);
159  }
160  GWEN_Buffer_AppendBytes(buf, s, ptrToSlash-s); /* deliberately not "+1": excluding '/' */
161  absBuf=GWEN_Buffer_new(0, 256, 0, 1);
163  if (GWEN_Buffer_GetUsedBytes(absBuf))
165 
166  /* add file name */
167  ptrToSlash++; /* skip '/', ptrToSlash now points to file name */
168  GWEN_Buffer_AppendString(absBuf, ptrToSlash);
169 
170  /* add complete absolute path to stringlist */
171  GWEN_StringList_AppendString(slOutput, GWEN_Buffer_GetStart(absBuf), 0, 1);
172  GWEN_Buffer_free(absBuf);
173  GWEN_Buffer_free(buf);
174  }
175  }
176  }
177  }
178 
180  }
181  if (GWEN_StringList_Count(slOutput)==0) {
182  GWEN_StringList_free(slOutput);
183  return NULL;
184  }
185 
186  return slOutput;
187  }
188 
189  return NULL;
190 }
191 
192 
193 
GWEN_STRINGLIST * GWEN_StringList_fromString2(const char *str, const char *delimiters, int checkDouble, uint32_t flags)
Definition: stringlist.c:801
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
GWENHYWFAR_API int GWEN_Directory_GetAbsoluteFolderPath(const char *folder, GWEN_BUFFER *tbuf)
struct GWEN_STRINGLISTENTRYSTRUCT GWEN_STRINGLISTENTRY
Definition: stringlist.h:53
void GWEN_Text_CondenseBuffer(GWEN_BUFFER *buf)
Definition: text.c:1620
#define GWEN_TEXT_FLAGS_DEL_TRAILING_BLANKS
Definition: text.h:45
#define GWEN_DIR_SEPARATOR_S
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
Definition: buffer.c:277
#define NULL
Definition: binreloc.c:300
int GWEN_SyncIo_Helper_ReadFile(const char *fName, GWEN_BUFFER *dbuf)
Definition: syncio.c:524
static void _overwriteEscapedLineFeedsWithSpace(GWEN_BUFFER *buffer)
static GWEN_STRINGLIST * _makeAbsolutePaths(GWEN_STRINGLIST *slInput, const char *folder)
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
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_STRINGLIST * GWB_BuildCtx_ReadAndTranslateDepfile(const char *folder, const char *fileName)
void GWEN_StringList_free(GWEN_STRINGLIST *sl)
Definition: stringlist.c:62
int GWEN_StringList_AppendString(GWEN_STRINGLIST *sl, const char *s, int take, int checkDouble)
Definition: stringlist.c:245
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:56
#define GWEN_TEXT_FLAGS_DEL_MULTIPLE_BLANKS
Definition: text.h:46
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
Definition: buffer.c:394
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
unsigned int GWEN_StringList_Count(const GWEN_STRINGLIST *sl)
Definition: stringlist.c:427
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
GWEN_STRINGLISTENTRY * GWEN_StringListEntry_Next(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:398
#define GWEN_TEXT_FLAGS_DEL_QUOTES
Definition: text.h:49
#define GWEN_TEXT_FLAGS_DEL_LEADING_BLANKS
Definition: text.h:44
int GWEN_Buffer_AppendBytes(GWEN_BUFFER *bf, const char *buffer, uint32_t size)
Definition: buffer.c:361
#define GWEN_TEXT_FLAGS_CHECK_BACKSLASH
Definition: text.h:50
static GWEN_STRINGLIST * _readDepFile(const char *fileName)
GWEN_STRINGLIST * GWEN_StringList_new(void)
Definition: stringlist.c:50
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:989