PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/gprolog-1.4.0/src/EnginePl/cpp_headers.c

#
C | 253 lines | 155 code | 41 blank | 57 comment | 34 complexity | e91051c149f98de13e96caff6a03bbc2 MD5 | raw file
Possible License(s): LGPL-3.0
  1. /*-------------------------------------------------------------------------*
  2. * GNU Prolog *
  3. * *
  4. * Part : installation *
  5. * File : cpp_headers.c *
  6. * Descr.: General GNU Prolog header file maker *
  7. * Author: Daniel Diaz *
  8. * *
  9. * Copyright (C) 1999-2011 Daniel Diaz *
  10. * *
  11. * This file is part of GNU Prolog *
  12. * *
  13. * GNU Prolog is free software: you can redistribute it and/or *
  14. * modify it under the terms of either: *
  15. * *
  16. * - the GNU Lesser General Public License as published by the Free *
  17. * Software Foundation; either version 3 of the License, or (at your *
  18. * option) any later version. *
  19. * *
  20. * or *
  21. * *
  22. * - the GNU General Public License as published by the Free *
  23. * Software Foundation; either version 2 of the License, or (at your *
  24. * option) any later version. *
  25. * *
  26. * or both in parallel, as here. *
  27. * *
  28. * GNU Prolog is distributed in the hope that it will be useful, *
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  31. * General Public License for more details. *
  32. * *
  33. * You should have received copies of the GNU General Public License and *
  34. * the GNU Lesser General Public License along with this program. If *
  35. * not, see http://www.gnu.org/licenses/. *
  36. *-------------------------------------------------------------------------*/
  37. /* $Id: cpp_headers.c,v 1.16 2011/03/28 16:20:05 diaz Exp $ */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <ctype.h>
  42. #include "gp_config.h"
  43. #if 1
  44. #define DO_NOT_ADD_COMMENTS
  45. #endif
  46. #if 0
  47. #define REMOVE_COMMENTS
  48. #endif
  49. #if 0
  50. #define REMOVE_BLANK_LINES
  51. #endif
  52. /*---------------------------------*
  53. * Constants *
  54. *---------------------------------*/
  55. /*---------------------------------*
  56. * Type Definitions *
  57. *---------------------------------*/
  58. typedef struct
  59. {
  60. char *name;
  61. char *parent;
  62. int line;
  63. }
  64. UsedFile;
  65. /*---------------------------------*
  66. * Global Variables *
  67. *---------------------------------*/
  68. char **dir;
  69. FILE *fout;
  70. char buff[4096];
  71. UsedFile used[1024];
  72. int nb_used = 0;
  73. /*---------------------------------*
  74. * Function Prototypes *
  75. *---------------------------------*/
  76. void Cpp_File(char *name, int skip_comment);
  77. /*-------------------------------------------------------------------------*
  78. * MAIN *
  79. * *
  80. *-------------------------------------------------------------------------*/
  81. int
  82. main(int argc, char *argv[])
  83. {
  84. if (argc < 4)
  85. {
  86. fprintf(stderr, "Usage cpp_headers in_file.h out_file.h search_dir...\n");
  87. return 1;
  88. }
  89. dir = argv + 3;
  90. if ((fout = fopen(argv[2], "w")) == NULL)
  91. {
  92. perror(argv[2]);
  93. return 1;
  94. }
  95. #ifndef DO_NOT_ADD_COMMENTS
  96. fprintf(fout, "/* %s generated from %s using cpp_headers */\n",
  97. argv[2], argv[1]);
  98. #endif
  99. Cpp_File(argv[1], 0);
  100. fclose(fout);
  101. return 0;
  102. }
  103. #define SKIP_SPACE(p) while(isspace(*p)) p++;
  104. /*-------------------------------------------------------------------------*
  105. * CPP_FILE *
  106. * *
  107. *-------------------------------------------------------------------------*/
  108. void
  109. Cpp_File(char *name, int skip_comment)
  110. {
  111. char **d;
  112. int i;
  113. FILE *fin;
  114. char *p, *q;
  115. int line = 0;
  116. char name1[1024];
  117. #ifdef REMOVE_COMMENTS
  118. int inside_comment = 0;
  119. #endif
  120. #ifdef REMOVE_BLANK_LINES
  121. int can_be_removed = 1;
  122. #endif
  123. for (d = dir; *d; d++)
  124. {
  125. sprintf(buff, "%s/%s", *d, name);
  126. if ((fin = fopen(buff, "r")) != NULL)
  127. break;
  128. }
  129. if (*d == NULL)
  130. {
  131. fprintf(stderr, "Cannot find the location of %s\n", name);
  132. exit(1);
  133. }
  134. while (fgets(buff, sizeof(buff), fin))
  135. {
  136. line++;
  137. #ifdef REMOVE_COMMENTS
  138. if (skip_comment)
  139. {
  140. if (!inside_comment)
  141. {
  142. for(p = buff; isspace(*p); p++)
  143. ;
  144. if (*p == '/' && p[1] == '*')
  145. {
  146. if (strstr(p, "*/") == NULL)
  147. inside_comment = 1;
  148. continue;
  149. }
  150. }
  151. else
  152. {
  153. if (strstr(p, "*/") != NULL)
  154. inside_comment = 0;
  155. continue;
  156. }
  157. }
  158. #endif
  159. if (*buff != '#')
  160. goto reflect_line;
  161. p = buff + 1;
  162. SKIP_SPACE(p);
  163. if (strncmp(p, "include", 7))
  164. goto reflect_line;
  165. p += 7;
  166. SKIP_SPACE(p);
  167. if (*p != '"')
  168. goto reflect_line;
  169. p++;
  170. q = p + strlen(p);
  171. while (*q != '"')
  172. q--;
  173. *q = '\0';
  174. strcpy(name1, p);
  175. #ifndef DO_NOT_ADD_COMMENTS
  176. fprintf(fout, "/* %s:%d includes %s */\n", name, line, name1);
  177. #endif
  178. for (i = 0; i < nb_used; i++)
  179. if (strcmp(used[i].name, name1) == 0)
  180. break;
  181. if (i >= nb_used)
  182. {
  183. used[nb_used].name = strdup(name1);
  184. used[nb_used].parent = strdup(name);
  185. used[nb_used].line = line;
  186. nb_used++;
  187. Cpp_File(name1, 1);
  188. }
  189. #ifndef DO_NOT_ADD_COMMENTS
  190. else
  191. fprintf(fout, "/* already included by %s:%d */\n",
  192. used[i].parent, used[i].line);
  193. #endif
  194. continue;
  195. reflect_line:
  196. #ifdef REMOVE_BLANK_LINES
  197. if (can_be_removed)
  198. {
  199. p = buff;
  200. SKIP_SPACE(p);
  201. if (*p == '\0')
  202. continue;
  203. }
  204. p = buff + strlen(buff) - 1;
  205. while (isspace(*p))
  206. p--;
  207. can_be_removed = (*p != '\\');
  208. #endif
  209. fputs(buff, fout);
  210. }
  211. fclose(fin);
  212. }