split-include.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * split-include.c
  3. *
  4. * Copyright abandoned, Michael Chastain, <mailto:mec@shout.net>.
  5. * This is a C version of syncdep.pl by Werner Almesberger.
  6. *
  7. * This program takes autoconf.h as input and outputs a directory full
  8. * of one-line include files, merging onto the old values.
  9. *
  10. * Think of the configuration options as key-value pairs. Then there
  11. * are five cases:
  12. *
  13. * key old value new value action
  14. *
  15. * KEY-1 VALUE-1 VALUE-1 leave file alone
  16. * KEY-2 VALUE-2A VALUE-2B write VALUE-2B into file
  17. * KEY-3 - VALUE-3 write VALUE-3 into file
  18. * KEY-4 VALUE-4 - write an empty file
  19. * KEY-5 (empty) - leave old empty file alone
  20. */
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. #include <ctype.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #define ERROR_EXIT(strExit) \
  31. { \
  32. const int errnoSave = errno; \
  33. fprintf(stderr, "%s: ", str_my_name); \
  34. errno = errnoSave; \
  35. perror((strExit)); \
  36. exit(1); \
  37. }
  38. int main(int argc, const char * argv [])
  39. {
  40. const char * str_my_name;
  41. const char * str_file_autoconf;
  42. const char * str_dir_config;
  43. FILE * fp_config;
  44. FILE * fp_target;
  45. FILE * fp_find;
  46. int buffer_size;
  47. char * line;
  48. char * old_line;
  49. char * list_target;
  50. char * ptarget;
  51. struct stat stat_buf;
  52. /* Check arg count. */
  53. if (argc != 3)
  54. {
  55. fprintf(stderr, "%s: wrong number of arguments.\n", argv[0]);
  56. exit(1);
  57. }
  58. str_my_name = argv[0];
  59. str_file_autoconf = argv[1];
  60. str_dir_config = argv[2];
  61. /* Find a buffer size. */
  62. if (stat(str_file_autoconf, &stat_buf) != 0)
  63. ERROR_EXIT(str_file_autoconf);
  64. buffer_size = 2 * stat_buf.st_size + 4096;
  65. /* Allocate buffers. */
  66. if ( (line = malloc(buffer_size)) == NULL
  67. || (old_line = malloc(buffer_size)) == NULL
  68. || (list_target = malloc(buffer_size)) == NULL )
  69. ERROR_EXIT(str_file_autoconf);
  70. /* Open autoconfig file. */
  71. if ((fp_config = fopen(str_file_autoconf, "r")) == NULL)
  72. ERROR_EXIT(str_file_autoconf);
  73. /* Make output directory if needed. */
  74. if (stat(str_dir_config, &stat_buf) != 0)
  75. {
  76. if (mkdir(str_dir_config, 0755) != 0)
  77. ERROR_EXIT(str_dir_config);
  78. }
  79. /* Change to output directory. */
  80. if (chdir(str_dir_config) != 0)
  81. ERROR_EXIT(str_dir_config);
  82. /* Put initial separator into target list. */
  83. ptarget = list_target;
  84. *ptarget++ = '\n';
  85. /* Read config lines. */
  86. while (fgets(line, buffer_size, fp_config))
  87. {
  88. const char * str_config;
  89. int is_same;
  90. int itarget;
  91. if (line[0] != '#')
  92. continue;
  93. if ((str_config = strstr(line, " CONFIG_")) == NULL)
  94. continue;
  95. /* We found #define CONFIG_foo or #undef CONFIG_foo.
  96. * Make the output file name. */
  97. str_config += sizeof(" CONFIG_") - 1;
  98. for (itarget = 0; !isspace(str_config[itarget]); itarget++)
  99. {
  100. int c = (unsigned char) str_config[itarget];
  101. if (isupper(c)) c = tolower(c);
  102. if (c == '_') c = '/';
  103. ptarget[itarget] = c;
  104. }
  105. ptarget[itarget++] = '.';
  106. ptarget[itarget++] = 'h';
  107. ptarget[itarget++] = '\0';
  108. /* Check for existing file. */
  109. is_same = 0;
  110. if ((fp_target = fopen(ptarget, "r")) != NULL)
  111. {
  112. fgets(old_line, buffer_size, fp_target);
  113. if (fclose(fp_target) != 0)
  114. ERROR_EXIT(ptarget);
  115. if (!strcmp(line, old_line))
  116. is_same = 1;
  117. }
  118. if (!is_same)
  119. {
  120. /* Auto-create directories. */
  121. int islash;
  122. for (islash = 0; islash < itarget; islash++)
  123. {
  124. if (ptarget[islash] == '/')
  125. {
  126. ptarget[islash] = '\0';
  127. if (stat(ptarget, &stat_buf) != 0
  128. && mkdir(ptarget, 0755) != 0)
  129. ERROR_EXIT( ptarget );
  130. ptarget[islash] = '/';
  131. }
  132. }
  133. /* Write the file. */
  134. if ((fp_target = fopen(ptarget, "w")) == NULL)
  135. ERROR_EXIT(ptarget);
  136. fputs(line, fp_target);
  137. if (ferror(fp_target) || fclose(fp_target) != 0)
  138. ERROR_EXIT(ptarget);
  139. }
  140. /* Update target list */
  141. ptarget += itarget;
  142. *(ptarget-1) = '\n';
  143. }
  144. /*
  145. * Close autoconfig file.
  146. * Terminate the target list.
  147. */
  148. if (fclose(fp_config) != 0)
  149. ERROR_EXIT(str_file_autoconf);
  150. *ptarget = '\0';
  151. /*
  152. * Fix up existing files which have no new value.
  153. * This is Case 4 and Case 5.
  154. *
  155. * I re-read the tree and filter it against list_target.
  156. * This is crude. But it avoids data copies. Also, list_target
  157. * is compact and contiguous, so it easily fits into cache.
  158. *
  159. * Notice that list_target contains strings separated by \n,
  160. * with a \n before the first string and after the last.
  161. * fgets gives the incoming names a terminating \n.
  162. * So by having an initial \n, strstr will find exact matches.
  163. */
  164. fp_find = popen("find * -type f -name \"*.h\" -print", "r");
  165. if (fp_find == 0)
  166. ERROR_EXIT( "find" );
  167. line[0] = '\n';
  168. while (fgets(line+1, buffer_size, fp_find))
  169. {
  170. if (strstr(list_target, line) == NULL)
  171. {
  172. /*
  173. * This is an old file with no CONFIG_* flag in autoconf.h.
  174. */
  175. /* First strip the \n. */
  176. line[strlen(line)-1] = '\0';
  177. /* Grab size. */
  178. if (stat(line+1, &stat_buf) != 0)
  179. ERROR_EXIT(line);
  180. /* If file is not empty, make it empty and give it a fresh date. */
  181. if (stat_buf.st_size != 0)
  182. {
  183. if ((fp_target = fopen(line+1, "w")) == NULL)
  184. ERROR_EXIT(line);
  185. if (fclose(fp_target) != 0)
  186. ERROR_EXIT(line);
  187. }
  188. }
  189. }
  190. if (pclose(fp_find) != 0)
  191. ERROR_EXIT("find");
  192. return 0;
  193. }