cnvwindeps.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /****************************************************************************
  2. * tools/cnvwindeps.c
  3. *
  4. * Copyright (C) 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <stdio.h>
  39. #include <stdbool.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <ctype.h>
  43. #include <errno.h>
  44. #ifdef HOST_CYGWIN
  45. #include <sys/cygwin.h>
  46. /****************************************************************************
  47. * Pre-processor Definitions
  48. ****************************************************************************/
  49. #define MAX_LINE 1024
  50. #define MAX_PATH 1024
  51. /****************************************************************************
  52. * Global Data
  53. ****************************************************************************/
  54. static unsigned long g_lineno;
  55. static char g_line[MAX_LINE];
  56. static char g_dequoted[MAX_PATH];
  57. static char g_posix[MAX_PATH];
  58. /****************************************************************************
  59. * Private Functions
  60. ****************************************************************************/
  61. static char *skip_spaces(char *ptr)
  62. {
  63. while (*ptr && isspace((int)*ptr)) ptr++;
  64. return ptr;
  65. }
  66. static char *find_spaces(char *ptr)
  67. {
  68. bool quoted = false;
  69. while (*ptr)
  70. {
  71. if (ptr[0] == '\\' && isspace((int)ptr[1]))
  72. {
  73. quoted = true;
  74. ptr++;
  75. }
  76. else if (!quoted && isspace((int)*ptr))
  77. {
  78. break;
  79. }
  80. else
  81. {
  82. quoted = false;
  83. ptr++;
  84. }
  85. }
  86. return ptr;
  87. }
  88. static bool scour_path(const char *path)
  89. {
  90. /* KLUDGE: GNU make cannot handle dependencies with spaces in them.
  91. * There may be addition characters that cause problems too.
  92. */
  93. return strchr(path, ' ') != NULL;
  94. }
  95. static bool dequote_path(const char *winpath)
  96. {
  97. char *dest = g_dequoted;
  98. const char *src = winpath;
  99. int len = 0;
  100. bool quoted = false;
  101. while (*src && len < MAX_PATH)
  102. {
  103. if (src[0] != '\\' || (src[1] != ' ' && src[1] != '(' && src[1] != ')'))
  104. {
  105. *dest++ = *src;
  106. len++;
  107. }
  108. else
  109. {
  110. quoted = true;
  111. }
  112. src++;
  113. }
  114. if (*src || len >= MAX_PATH)
  115. {
  116. fprintf(stderr, "%lu: Line truncated\n", g_lineno);
  117. exit(EXIT_FAILURE);
  118. }
  119. *dest = '\0';
  120. return quoted;
  121. }
  122. static bool convert_path(const char *winpath)
  123. {
  124. ssize_t size;
  125. ssize_t ret;
  126. bool quoted;
  127. quoted = dequote_path(winpath);
  128. size = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE, g_dequoted, NULL, 0);
  129. if (size > MAX_PATH)
  130. {
  131. fprintf(stderr, "%lu: POSIX path too long: %lu\n",
  132. g_lineno, (unsigned long)size);
  133. exit(EXIT_FAILURE);
  134. }
  135. ret = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE, g_dequoted, g_posix, MAX_PATH);
  136. if (ret < 0)
  137. {
  138. fprintf(stderr, "%lu: cygwin_conv_path '%s' failed: %s\n",
  139. g_lineno, g_dequoted, strerror(errno));
  140. exit(EXIT_FAILURE);
  141. }
  142. return quoted;
  143. }
  144. static void show_usage(const char *progname)
  145. {
  146. fprintf(stderr, "USAGE: %s <path-to-deps-file>\n", progname);
  147. exit(EXIT_FAILURE);
  148. }
  149. /****************************************************************************
  150. * Public Functions
  151. ****************************************************************************/
  152. int main(int argc, char **argv, char **envp)
  153. {
  154. char *path;
  155. char *next;
  156. FILE *stream;
  157. bool begin;
  158. bool quoted;
  159. bool scouring;
  160. if (argc != 2)
  161. {
  162. fprintf(stderr, "Unexpected number of arguments\n");
  163. show_usage(argv[0]);
  164. }
  165. stream = fopen(argv[1], "r");
  166. if (!stream)
  167. {
  168. fprintf(stderr, "open %s failed: %s\n", argv[1], strerror(errno));
  169. exit(EXIT_FAILURE);
  170. }
  171. begin = true;
  172. scouring = false;
  173. g_lineno = 0;
  174. while (fgets(g_line, MAX_LINE, stream) != NULL)
  175. {
  176. g_lineno++;
  177. next = g_line;
  178. for (; ; )
  179. {
  180. if (begin)
  181. {
  182. path = skip_spaces(next);
  183. if (*path == '#')
  184. {
  185. /* The reset of the line is comment */
  186. puts(path);
  187. break;
  188. }
  189. next = strchr(path, ':');
  190. if (!next)
  191. {
  192. fprintf(stderr, "%lu: Expected colon\n", g_lineno);
  193. exit(EXIT_FAILURE);
  194. }
  195. if (*next != '\0')
  196. {
  197. *next++ = '\0';
  198. }
  199. scouring = scour_path(path);
  200. if (!scouring)
  201. {
  202. quoted = convert_path(path);
  203. if (quoted)
  204. {
  205. printf("\"%s\":", g_posix);
  206. }
  207. else
  208. {
  209. printf("%s:", g_posix);
  210. }
  211. }
  212. begin = false;
  213. }
  214. else
  215. {
  216. path = skip_spaces(next);
  217. next = find_spaces(path);
  218. if (path[0] == '\\')
  219. {
  220. break;
  221. }
  222. else if (strcmp(path, "") == 0)
  223. {
  224. printf("\n\n");
  225. begin = true;
  226. scouring = false;
  227. break;
  228. }
  229. else
  230. {
  231. if (*next != '\0')
  232. {
  233. *next++ = '\0';
  234. }
  235. if (!scouring && !scour_path(path))
  236. {
  237. quoted = convert_path(path);
  238. if (quoted)
  239. {
  240. printf(" \\\n\t\"%s\"", g_posix);
  241. }
  242. else
  243. {
  244. printf(" \\\n\t%s", g_posix);
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. fclose(stream);
  252. return 0;
  253. }
  254. #else /* HOST_CYGWIN */
  255. int main(int argc, char **argv, char **envp)
  256. {
  257. fprintf(stderr, "ERROR: This tool is only available under Cygwin\n");
  258. return EXIT_FAILURE;
  259. }
  260. #endif /* HOST_CYGWIN */