3
0

split-include.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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((unsigned char)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. if (!fgets(old_line, buffer_size, fp_target) && ferror(fp_target))
  113. ERROR_EXIT(ptarget);
  114. if (fclose(fp_target) != 0)
  115. ERROR_EXIT(ptarget);
  116. if (!strcmp(line, old_line))
  117. is_same = 1;
  118. }
  119. if (!is_same)
  120. {
  121. /* Auto-create directories. */
  122. int islash;
  123. for (islash = 0; islash < itarget; islash++)
  124. {
  125. if (ptarget[islash] == '/')
  126. {
  127. ptarget[islash] = '\0';
  128. if (stat(ptarget, &stat_buf) != 0
  129. && mkdir(ptarget, 0755) != 0)
  130. ERROR_EXIT( ptarget );
  131. ptarget[islash] = '/';
  132. }
  133. }
  134. /* Write the file. */
  135. if ((fp_target = fopen(ptarget, "w")) == NULL)
  136. ERROR_EXIT(ptarget);
  137. fputs(line, fp_target);
  138. if (ferror(fp_target) || fclose(fp_target) != 0)
  139. ERROR_EXIT(ptarget);
  140. }
  141. /* Update target list */
  142. ptarget += itarget;
  143. *(ptarget-1) = '\n';
  144. }
  145. /*
  146. * Close autoconfig file.
  147. * Terminate the target list.
  148. */
  149. if (fclose(fp_config) != 0)
  150. ERROR_EXIT(str_file_autoconf);
  151. *ptarget = '\0';
  152. /*
  153. * Fix up existing files which have no new value.
  154. * This is Case 4 and Case 5.
  155. *
  156. * I re-read the tree and filter it against list_target.
  157. * This is crude. But it avoids data copies. Also, list_target
  158. * is compact and contiguous, so it easily fits into cache.
  159. *
  160. * Notice that list_target contains strings separated by \n,
  161. * with a \n before the first string and after the last.
  162. * fgets gives the incoming names a terminating \n.
  163. * So by having an initial \n, strstr will find exact matches.
  164. */
  165. fp_find = popen("find * -type f -name \"*.h\" -print", "r");
  166. if (fp_find == 0)
  167. ERROR_EXIT( "find" );
  168. line[0] = '\n';
  169. while (fgets(line+1, buffer_size, fp_find))
  170. {
  171. if (strstr(list_target, line) == NULL)
  172. {
  173. /*
  174. * This is an old file with no CONFIG_* flag in autoconf.h.
  175. */
  176. /* First strip the \n. */
  177. line[strlen(line)-1] = '\0';
  178. /* Grab size. */
  179. if (stat(line+1, &stat_buf) != 0)
  180. ERROR_EXIT(line);
  181. /* If file is not empty, make it empty and give it a fresh date. */
  182. if (stat_buf.st_size != 0)
  183. {
  184. if ((fp_target = fopen(line+1, "w")) == NULL)
  185. ERROR_EXIT(line);
  186. if (fclose(fp_target) != 0)
  187. ERROR_EXIT(line);
  188. }
  189. }
  190. }
  191. if (pclose(fp_find) != 0)
  192. ERROR_EXIT("find");
  193. return 0;
  194. }