split-include.c 5.4 KB

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