forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnvwindeps.c
294 lines (250 loc) · 7.08 KB
/
cnvwindeps.c
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/****************************************************************************
* tools/cnvwindeps.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#ifdef HOST_CYGWIN
#include <sys/cygwin.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MAX_LINE 1024
#define MAX_PATH 1024
/****************************************************************************
* Global Data
****************************************************************************/
static unsigned long g_lineno;
static char g_line[MAX_LINE];
static char g_dequoted[MAX_PATH];
static char g_posix[MAX_PATH];
/****************************************************************************
* Private Functions
****************************************************************************/
static char *skip_spaces(char *ptr)
{
while (*ptr && isspace((int)*ptr)) ptr++;
return ptr;
}
static char *find_spaces(char *ptr)
{
bool quoted = false;
while (*ptr)
{
if (ptr[0] == '\\' && isspace((int)ptr[1]))
{
quoted = true;
ptr++;
}
else if (!quoted && isspace((int)*ptr))
{
break;
}
else
{
quoted = false;
ptr++;
}
}
return ptr;
}
static bool scour_path(const char *path)
{
/* KLUDGE: GNU make cannot handle dependencies with spaces in them.
* There may be addition characters that cause problems too.
*/
return strchr(path, ' ') != NULL;
}
static bool dequote_path(const char *winpath)
{
char *dest = g_dequoted;
const char *src = winpath;
int len = 0;
bool quoted = false;
while (*src && len < MAX_PATH)
{
if (src[0] != '\\' || (src[1] != ' ' &&
src[1] != '(' && src[1] != ')'))
{
*dest++ = *src;
len++;
}
else
{
quoted = true;
}
src++;
}
if (*src || len >= MAX_PATH)
{
fprintf(stderr, "%lu: Line truncated\n", g_lineno);
exit(EXIT_FAILURE);
}
*dest = '\0';
return quoted;
}
static bool convert_path(const char *winpath)
{
ssize_t size;
ssize_t ret;
bool quoted;
quoted = dequote_path(winpath);
size = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
g_dequoted, NULL, 0);
if (size > MAX_PATH)
{
fprintf(stderr, "%lu: POSIX path too long: %lu\n",
g_lineno, (unsigned long)size);
exit(EXIT_FAILURE);
}
ret = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
g_dequoted, g_posix, MAX_PATH);
if (ret < 0)
{
fprintf(stderr, "%lu: cygwin_conv_path '%s' failed: %s\n",
g_lineno, g_dequoted, strerror(errno));
exit(EXIT_FAILURE);
}
return quoted;
}
static void show_usage(const char *progname)
{
fprintf(stderr, "USAGE: %s <path-to-deps-file>\n", progname);
exit(EXIT_FAILURE);
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, char **argv, char **envp)
{
char *path;
char *next;
FILE *stream;
bool begin;
bool quoted;
bool scouring;
if (argc != 2)
{
fprintf(stderr, "Unexpected number of arguments\n");
show_usage(argv[0]);
}
stream = fopen(argv[1], "r");
if (!stream)
{
fprintf(stderr, "open %s failed: %s\n", argv[1], strerror(errno));
exit(EXIT_FAILURE);
}
begin = true;
scouring = false;
g_lineno = 0;
while (fgets(g_line, MAX_LINE, stream) != NULL)
{
g_lineno++;
next = g_line;
for (; ; )
{
if (begin)
{
path = skip_spaces(next);
if (*path == '#')
{
/* The reset of the line is comment */
puts(path);
break;
}
next = strchr(path, ':');
if (!next)
{
fprintf(stderr, "%lu: Expected colon\n", g_lineno);
exit(EXIT_FAILURE);
}
if (*next != '\0')
{
*next++ = '\0';
}
scouring = scour_path(path);
if (!scouring)
{
quoted = convert_path(path);
if (quoted)
{
printf("\"%s\":", g_posix);
}
else
{
printf("%s:", g_posix);
}
}
begin = false;
}
else
{
path = skip_spaces(next);
next = find_spaces(path);
if (path[0] == '\\')
{
break;
}
else if (strcmp(path, "") == 0)
{
printf("\n\n");
begin = true;
scouring = false;
break;
}
else
{
if (*next != '\0')
{
*next++ = '\0';
}
if (!scouring && !scour_path(path))
{
quoted = convert_path(path);
if (quoted)
{
printf(" \\\n\t\"%s\"", g_posix);
}
else
{
printf(" \\\n\t%s", g_posix);
}
}
}
}
}
}
fclose(stream);
return 0;
}
#else /* HOST_CYGWIN */
int main(int argc, char **argv, char **envp)
{
fprintf(stderr, "ERROR: This tool is only available under Cygwin\n");
return EXIT_FAILURE;
}
#endif /* HOST_CYGWIN */