makedoc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* makedoc.c -- make doc.c and funs.h from input files.
  2. $Id$
  3. Copyright 1993, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2007,
  4. 2008, 2012, 2013, 2014 Free Software Foundation, Inc.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. Originally written by Brian Fox. */
  16. /* This program grovels the contents of the source files passed as arguments
  17. and writes out a file of function pointers and documentation strings, and
  18. a header file which describes the contents. This only does the functions
  19. declared with DECLARE_INFO_COMMAND. */
  20. #include "info.h"
  21. #include "doc.h"
  22. char *program_name = "makedoc";
  23. static void fatal_file_error (char *filename);
  24. /* Name of the header file which receives the declarations of functions. */
  25. static char *funs_filename = "funs.h";
  26. /* Name of the documentation to function pointer file. */
  27. static char *doc_filename = "doc.c";
  28. static char *doc_header[] = {
  29. "/* doc.c -- Generated structure containing function names and doc strings.",
  30. "",
  31. " This file was automatically made from various source files with the",
  32. " command `%s'.",
  33. " DO NOT EDIT THIS FILE, only `%s.c'.",
  34. NULL
  35. };
  36. static char *doc_header_1[] = {
  37. " An entry in the array FUNCTION_DOC_ARRAY is made for each command",
  38. " found in the above files; each entry consists of a function pointer,",
  39. " a string which is the user-visible name of the function,",
  40. " and a string which documents its purpose. */",
  41. "",
  42. "#include \"info.h\"",
  43. "#include \"window.h\"",
  44. "#include \"funs.h\"",
  45. "",
  46. "FUNCTION_DOC function_doc_array[] = {",
  47. "",
  48. NULL
  49. };
  50. /* How to remember the locations of the functions found so that Emacs
  51. can use the information in a tag table. */
  52. typedef struct {
  53. char *name; /* Name of the tag. */
  54. int line; /* Line number at which it appears. */
  55. long char_offset; /* Character offset at which it appears. */
  56. } EMACS_TAG;
  57. typedef struct {
  58. char *filename; /* Name of the file containing entries. */
  59. long entrylen; /* Total number of characters in tag block. */
  60. EMACS_TAG **entries; /* Entries found in FILENAME. */
  61. size_t entries_index;
  62. size_t entries_slots;
  63. } EMACS_TAG_BLOCK;
  64. EMACS_TAG_BLOCK **emacs_tags = NULL;
  65. size_t emacs_tags_index = 0;
  66. size_t emacs_tags_slots = 0;
  67. #define DECLARATION_STRING "\nDECLARE_INFO_COMMAND"
  68. static void process_one_file (char *filename, FILE *doc_stream,
  69. FILE *funs_stream);
  70. static void maybe_dump_tags (FILE *stream);
  71. static FILE *must_fopen (char *filename, char *mode);
  72. static void init_func_key (unsigned int val);
  73. static unsigned int next_func_key (void);
  74. int
  75. main (int argc, char **argv)
  76. {
  77. register int i;
  78. int tags_only = 0;
  79. FILE *funs_stream, *doc_stream;
  80. #if STRIP_DOT_EXE
  81. {
  82. char *dot = strrchr (argv[0], '.');
  83. if (dot && FILENAME_CMP (dot, ".exe") == 0)
  84. *dot = 0;
  85. }
  86. #endif
  87. for (i = 1; i < argc; i++)
  88. if (strcmp (argv[i], "-tags") == 0)
  89. {
  90. tags_only++;
  91. break;
  92. }
  93. if (tags_only)
  94. {
  95. funs_filename = NULL_DEVICE;
  96. doc_filename = NULL_DEVICE;
  97. }
  98. /* The order of these calls depends exactly on the order in the
  99. Makefile.{in,am}, or they might fail on filesystems with
  100. high-precision times; see also the fclose calls below. */
  101. funs_stream = must_fopen (funs_filename, "w");
  102. doc_stream = must_fopen (doc_filename, "w");
  103. fprintf (funs_stream,
  104. "/* %s -- Generated declarations for Info commands. */\n\n"
  105. "#include \"info.h\"\n"
  106. "#include \"window.h\"\n",
  107. funs_filename);
  108. for (i = 0; doc_header[i]; i++)
  109. {
  110. fprintf (doc_stream, doc_header[i], argv[0], argv[0]);
  111. fprintf (doc_stream, "\n");
  112. }
  113. fprintf (doc_stream,
  114. _(" Source files groveled to make this file include:\n\n"));
  115. for (i = 1; i < argc; i++)
  116. fprintf (doc_stream, "\t%s\n", argv[i]);
  117. fprintf (doc_stream, "\n");
  118. for (i = 0; doc_header_1[i]; i++)
  119. fprintf (doc_stream, "%s\n", doc_header_1[i]);
  120. init_func_key(0);
  121. for (i = 1; i < argc; i++)
  122. {
  123. char *curfile;
  124. curfile = argv[i];
  125. if (*curfile == '-')
  126. continue;
  127. fprintf (doc_stream, "/* Commands found in \"%s\". */\n", curfile);
  128. fprintf (funs_stream, "\n/* Functions declared in \"%s\". */\n",
  129. curfile);
  130. process_one_file (curfile, doc_stream, funs_stream);
  131. }
  132. fprintf (doc_stream, " { NULL, NULL, NULL, NULL }\n};\n");
  133. fprintf (funs_stream, "\n#define A_NCOMMANDS %u\n", next_func_key());
  134. /* The order of these calls also depends exactly on the order in the
  135. * Makefile.{in,am}; see the must_fopen calls above. */
  136. fclose (funs_stream);
  137. fclose (doc_stream);
  138. if (tags_only)
  139. maybe_dump_tags (stdout);
  140. return 0;
  141. }
  142. /* Dumping out the contents of an Emacs tags table. */
  143. static void
  144. maybe_dump_tags (FILE *stream)
  145. {
  146. size_t i;
  147. /* Emacs needs its TAGS file to be in Unix text format (i.e., only
  148. newline at end of every line, no CR), so when we generate a
  149. TAGS table, we must switch the output stream to binary mode.
  150. (If the table is written to a terminal, this is obviously not needed.) */
  151. SET_BINARY (fileno (stream));
  152. /* Print out the information for each block. */
  153. for (i = 0; i < emacs_tags_index; i++)
  154. {
  155. size_t j;
  156. register EMACS_TAG_BLOCK *block;
  157. register EMACS_TAG *etag;
  158. long block_len;
  159. block_len = 0;
  160. block = emacs_tags[i];
  161. /* Calculate the length of the dumped block first. */
  162. for (j = 0; j < block->entries_index; j++)
  163. {
  164. char digits[30];
  165. etag = block->entries[j];
  166. block_len += 3 + strlen (etag->name);
  167. sprintf (digits, "%d,%ld", etag->line, etag->char_offset);
  168. block_len += strlen (digits);
  169. }
  170. /* Print out the defining line. */
  171. fprintf (stream, "\f\n%s,%ld\n", block->filename, block_len);
  172. /* Print out the individual tags. */
  173. for (j = 0; j < block->entries_index; j++)
  174. {
  175. etag = block->entries[j];
  176. fprintf (stream, "%s,\177%d,%ld\n",
  177. etag->name, etag->line, etag->char_offset);
  178. }
  179. }
  180. }
  181. /* Keeping track of names, line numbers and character offsets of functions
  182. found in source files. */
  183. static EMACS_TAG_BLOCK *
  184. make_emacs_tag_block (char *filename)
  185. {
  186. EMACS_TAG_BLOCK *block;
  187. block = xmalloc (sizeof (EMACS_TAG_BLOCK));
  188. block->filename = xstrdup (filename);
  189. block->entrylen = 0;
  190. block->entries = NULL;
  191. block->entries_index = 0;
  192. block->entries_slots = 0;
  193. return block;
  194. }
  195. static void
  196. add_tag_to_block (EMACS_TAG_BLOCK *block,
  197. char *name, int line, long int char_offset)
  198. {
  199. EMACS_TAG *tag;
  200. tag = xmalloc (sizeof (EMACS_TAG));
  201. tag->name = name;
  202. tag->line = line;
  203. tag->char_offset = char_offset;
  204. add_pointer_to_array (tag, block->entries_index, block->entries,
  205. block->entries_slots, 50);
  206. }
  207. /* Read the file represented by FILENAME into core, and search it for Info
  208. function declarations. Output the declarations in various forms to the
  209. DOC_STREAM and FUNS_STREAM. */
  210. static void
  211. process_one_file (char *filename, FILE *doc_stream, FILE *funs_stream)
  212. {
  213. int descriptor, decl_len;
  214. char *buffer, *decl_str;
  215. struct stat finfo;
  216. long offset;
  217. long file_size;
  218. EMACS_TAG_BLOCK *block;
  219. if (stat (filename, &finfo) == -1)
  220. fatal_file_error (filename);
  221. descriptor = open (filename, O_RDONLY, 0666);
  222. if (descriptor == -1)
  223. fatal_file_error (filename);
  224. file_size = (long) finfo.st_size;
  225. buffer = xmalloc (1 + file_size);
  226. /* On some systems, the buffer will actually contain
  227. less characters than the full file's size, because
  228. the CR characters are removed from line endings. */
  229. file_size = read (descriptor, buffer, file_size);
  230. close (descriptor);
  231. offset = 0;
  232. decl_str = DECLARATION_STRING;
  233. decl_len = strlen (decl_str);
  234. block = make_emacs_tag_block (filename);
  235. while (1)
  236. {
  237. long point = 0;
  238. long line_start = 0;
  239. int line_number = 0;
  240. char *func, *doc;
  241. char *func_name;
  242. for (; offset < (file_size - decl_len); offset++)
  243. {
  244. if (buffer[offset] == '\n')
  245. {
  246. line_number++;
  247. line_start = offset + 1;
  248. }
  249. if (strncmp (buffer + offset, decl_str, decl_len) == 0)
  250. {
  251. offset += decl_len;
  252. point = offset;
  253. break;
  254. }
  255. }
  256. if (!point)
  257. break;
  258. /* Skip forward until we find the open paren. */
  259. while (point < file_size)
  260. {
  261. if (buffer[point] == '\n')
  262. {
  263. line_number++;
  264. line_start = point + 1;
  265. }
  266. else if (buffer[point] == '(')
  267. break;
  268. point++;
  269. }
  270. while (point++ < file_size)
  271. {
  272. if (!whitespace_or_newline (buffer[point]))
  273. break;
  274. else if (buffer[point] == '\n')
  275. {
  276. line_number++;
  277. line_start = point + 1;
  278. }
  279. }
  280. if (point >= file_size)
  281. break;
  282. /* Now looking at name of function. Get it. */
  283. for (offset = point; buffer[offset] != ','; offset++);
  284. func = xmalloc (1 + (offset - point));
  285. strncpy (func, buffer + point, offset - point);
  286. func[offset - point] = '\0';
  287. /* Remember this tag in the current block. */
  288. {
  289. char *tag_name;
  290. tag_name = xmalloc (1 + (offset - line_start));
  291. strncpy (tag_name, buffer + line_start, offset - line_start);
  292. tag_name[offset - line_start] = '\0';
  293. add_tag_to_block (block, tag_name, line_number, point);
  294. }
  295. /* Generate the user-visible function name from the function's name. */
  296. {
  297. register int i;
  298. char *name_start;
  299. name_start = func;
  300. if (strncmp (name_start, "info_", 5) == 0)
  301. name_start += 5;
  302. func_name = xstrdup (name_start);
  303. /* Fix up "ea" commands. */
  304. if (strncmp (func_name, "ea_", 3) == 0)
  305. {
  306. char *temp_func_name;
  307. temp_func_name = xmalloc (10 + strlen (func_name));
  308. strcpy (temp_func_name, "echo_area_");
  309. strcat (temp_func_name, func_name + 3);
  310. free (func_name);
  311. func_name = temp_func_name;
  312. }
  313. for (i = 0; func_name[i]; i++)
  314. if (func_name[i] == '_')
  315. func_name[i] = '-';
  316. }
  317. /* Find doc string. */
  318. point = offset + 1;
  319. while (point < file_size)
  320. {
  321. if (buffer[point] == '\n')
  322. {
  323. line_number++;
  324. line_start = point + 1;
  325. }
  326. if (buffer[point] == '"')
  327. break;
  328. else
  329. point++;
  330. }
  331. offset = point + 1;
  332. while (offset < file_size)
  333. {
  334. if (buffer[offset] == '\n')
  335. {
  336. line_number++;
  337. line_start = offset + 1;
  338. }
  339. if (buffer[offset] == '\\')
  340. offset += 2;
  341. else if (buffer[offset] == '"')
  342. break;
  343. else
  344. offset++;
  345. }
  346. offset++;
  347. if (offset >= file_size)
  348. break;
  349. doc = xmalloc (1 + (offset - point));
  350. strncpy (doc, buffer + point, offset - point);
  351. doc[offset - point] = '\0';
  352. fprintf (doc_stream,
  353. " { (VFunction *)%s, \"%s\", (FUNCTION_KEYSEQ *)0, %s },\n",
  354. func, func_name, doc);
  355. free (func_name);
  356. fprintf (funs_stream, "#define A_%s %u\n", func, next_func_key());
  357. fprintf (funs_stream,
  358. "extern void %s (WINDOW *window, int count);\n",
  359. func);
  360. free (func);
  361. free (doc);
  362. }
  363. free (buffer);
  364. /* If we created any tags, remember this file on our global list. Otherwise,
  365. free the memory already allocated to it. */
  366. if (block->entries)
  367. add_pointer_to_array (block, emacs_tags_index, emacs_tags,
  368. emacs_tags_slots, 10);
  369. else
  370. {
  371. free (block->filename);
  372. free (block);
  373. }
  374. }
  375. static void
  376. fatal_file_error (char *filename)
  377. {
  378. fprintf (stderr, _("Couldn't manipulate the file %s.\n"), filename);
  379. exit (EXIT_FAILURE);
  380. }
  381. static FILE *
  382. must_fopen (char *filename, char *mode)
  383. {
  384. FILE *stream;
  385. stream = fopen (filename, mode);
  386. if (!stream)
  387. fatal_file_error (filename);
  388. return stream;
  389. }
  390. static unsigned int func_key;
  391. static void
  392. init_func_key(unsigned int val)
  393. {
  394. func_key = val;
  395. }
  396. static unsigned int
  397. next_func_key(void)
  398. {
  399. return func_key++;
  400. }