cnvwindeps.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /****************************************************************************
  2. * tools/cnvwindeps.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <stdio.h>
  24. #include <stdbool.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <errno.h>
  29. #ifdef HOST_CYGWIN
  30. #include <sys/cygwin.h>
  31. /****************************************************************************
  32. * Pre-processor Definitions
  33. ****************************************************************************/
  34. #define MAX_LINE 1024
  35. #define MAX_PATH 1024
  36. /****************************************************************************
  37. * Global Data
  38. ****************************************************************************/
  39. static unsigned long g_lineno;
  40. static char g_line[MAX_LINE];
  41. static char g_dequoted[MAX_PATH];
  42. static char g_posix[MAX_PATH];
  43. /****************************************************************************
  44. * Private Functions
  45. ****************************************************************************/
  46. static char *skip_spaces(char *ptr)
  47. {
  48. while (*ptr && isspace((int)*ptr)) ptr++;
  49. return ptr;
  50. }
  51. static char *find_spaces(char *ptr)
  52. {
  53. bool quoted = false;
  54. while (*ptr)
  55. {
  56. if (ptr[0] == '\\' && isspace((int)ptr[1]))
  57. {
  58. quoted = true;
  59. ptr++;
  60. }
  61. else if (!quoted && isspace((int)*ptr))
  62. {
  63. break;
  64. }
  65. else
  66. {
  67. quoted = false;
  68. ptr++;
  69. }
  70. }
  71. return ptr;
  72. }
  73. static bool scour_path(const char *path)
  74. {
  75. /* KLUDGE: GNU make cannot handle dependencies with spaces in them.
  76. * There may be addition characters that cause problems too.
  77. */
  78. return strchr(path, ' ') != NULL;
  79. }
  80. static bool dequote_path(const char *winpath)
  81. {
  82. char *dest = g_dequoted;
  83. const char *src = winpath;
  84. int len = 0;
  85. bool quoted = false;
  86. while (*src && len < MAX_PATH)
  87. {
  88. if (src[0] != '\\' || (src[1] != ' ' &&
  89. src[1] != '(' && src[1] != ')'))
  90. {
  91. *dest++ = *src;
  92. len++;
  93. }
  94. else
  95. {
  96. quoted = true;
  97. }
  98. src++;
  99. }
  100. if (*src || len >= MAX_PATH)
  101. {
  102. fprintf(stderr, "%lu: Line truncated\n", g_lineno);
  103. exit(EXIT_FAILURE);
  104. }
  105. *dest = '\0';
  106. return quoted;
  107. }
  108. static bool convert_path(const char *winpath)
  109. {
  110. ssize_t size;
  111. ssize_t ret;
  112. bool quoted;
  113. quoted = dequote_path(winpath);
  114. size = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
  115. g_dequoted, NULL, 0);
  116. if (size > MAX_PATH)
  117. {
  118. fprintf(stderr, "%lu: POSIX path too long: %lu\n",
  119. g_lineno, (unsigned long)size);
  120. exit(EXIT_FAILURE);
  121. }
  122. ret = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
  123. g_dequoted, g_posix, MAX_PATH);
  124. if (ret < 0)
  125. {
  126. fprintf(stderr, "%lu: cygwin_conv_path '%s' failed: %s\n",
  127. g_lineno, g_dequoted, strerror(errno));
  128. exit(EXIT_FAILURE);
  129. }
  130. return quoted;
  131. }
  132. static void show_usage(const char *progname)
  133. {
  134. fprintf(stderr, "USAGE: %s <path-to-deps-file>\n", progname);
  135. exit(EXIT_FAILURE);
  136. }
  137. /****************************************************************************
  138. * Public Functions
  139. ****************************************************************************/
  140. int main(int argc, char **argv, char **envp)
  141. {
  142. char *path;
  143. char *next;
  144. FILE *stream;
  145. bool begin;
  146. bool quoted;
  147. bool scouring;
  148. if (argc != 2)
  149. {
  150. fprintf(stderr, "Unexpected number of arguments\n");
  151. show_usage(argv[0]);
  152. }
  153. stream = fopen(argv[1], "r");
  154. if (!stream)
  155. {
  156. fprintf(stderr, "open %s failed: %s\n", argv[1], strerror(errno));
  157. exit(EXIT_FAILURE);
  158. }
  159. begin = true;
  160. scouring = false;
  161. g_lineno = 0;
  162. while (fgets(g_line, MAX_LINE, stream) != NULL)
  163. {
  164. g_lineno++;
  165. next = g_line;
  166. for (; ; )
  167. {
  168. if (begin)
  169. {
  170. path = skip_spaces(next);
  171. if (*path == '#')
  172. {
  173. /* The reset of the line is comment */
  174. puts(path);
  175. break;
  176. }
  177. next = strchr(path, ':');
  178. if (!next)
  179. {
  180. fprintf(stderr, "%lu: Expected colon\n", g_lineno);
  181. exit(EXIT_FAILURE);
  182. }
  183. if (*next != '\0')
  184. {
  185. *next++ = '\0';
  186. }
  187. scouring = scour_path(path);
  188. if (!scouring)
  189. {
  190. quoted = convert_path(path);
  191. if (quoted)
  192. {
  193. printf("\"%s\":", g_posix);
  194. }
  195. else
  196. {
  197. printf("%s:", g_posix);
  198. }
  199. }
  200. begin = false;
  201. }
  202. else
  203. {
  204. path = skip_spaces(next);
  205. next = find_spaces(path);
  206. if (path[0] == '\\')
  207. {
  208. break;
  209. }
  210. else if (strcmp(path, "") == 0)
  211. {
  212. printf("\n\n");
  213. begin = true;
  214. scouring = false;
  215. break;
  216. }
  217. else
  218. {
  219. if (*next != '\0')
  220. {
  221. *next++ = '\0';
  222. }
  223. if (!scouring && !scour_path(path))
  224. {
  225. quoted = convert_path(path);
  226. if (quoted)
  227. {
  228. printf(" \\\n\t\"%s\"", g_posix);
  229. }
  230. else
  231. {
  232. printf(" \\\n\t%s", g_posix);
  233. }
  234. }
  235. }
  236. }
  237. }
  238. }
  239. fclose(stream);
  240. return 0;
  241. }
  242. #else /* HOST_CYGWIN */
  243. int main(int argc, char **argv, char **envp)
  244. {
  245. fprintf(stderr, "ERROR: This tool is only available under Cygwin\n");
  246. return EXIT_FAILURE;
  247. }
  248. #endif /* HOST_CYGWIN */