Codebase list texinfo / debian/6.5.0.dfsg.1-4 info / makedoc.c
debian/6.5.0.dfsg.1-4

Tree @debian/6.5.0.dfsg.1-4 (Download .tar.gz)

makedoc.c @debian/6.5.0.dfsg.1-4raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* makedoc.c -- make doc.c and funs.h from input files.
   $Id: makedoc.c 5912 2014-11-07 10:49:13Z gavin $

   Copyright 1993, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2007,
   2008, 2012, 2013, 2014 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   Originally written by Brian Fox. */

/* This program grovels the contents of the source files passed as arguments
   and writes out a file of function pointers and documentation strings, and
   a header file which describes the contents.  This only does the functions
   declared with DECLARE_INFO_COMMAND. */

#include "info.h"
#include "doc.h"

char *program_name = "makedoc";

static void fatal_file_error (char *filename);

/* Name of the header file which receives the declarations of functions. */
static char *funs_filename = "funs.h";

/* Name of the documentation to function pointer file. */
static char *doc_filename = "doc.c";

static char *doc_header[] = {
  "/* doc.c -- Generated structure containing function names and doc strings.",
  "",
  "   This file was automatically made from various source files with the",
  "   command `%s'.",
  "   DO NOT EDIT THIS FILE, only `%s.c'.",
  NULL
};

static char *doc_header_1[] = {
  "   An entry in the array FUNCTION_DOC_ARRAY is made for each command",
  "   found in the above files; each entry consists of a function pointer,",
  "   a string which is the user-visible name of the function,",
  "   and a string which documents its purpose. */",
  "",
  "#include \"info.h\"",
  "#include \"window.h\"",
  "#include \"funs.h\"",
  "",
  "FUNCTION_DOC function_doc_array[] = {",
  "",
  NULL
};

/* How to remember the locations of the functions found so that Emacs
   can use the information in a tag table. */
typedef struct {
  char *name;                   /* Name of the tag. */
  int line;                     /* Line number at which it appears. */
  long char_offset;             /* Character offset at which it appears. */
} EMACS_TAG;

typedef struct {
  char *filename;               /* Name of the file containing entries. */
  long entrylen;                /* Total number of characters in tag block. */
  EMACS_TAG **entries;          /* Entries found in FILENAME. */
  size_t entries_index;
  size_t entries_slots;
} EMACS_TAG_BLOCK;

EMACS_TAG_BLOCK **emacs_tags = NULL;
size_t emacs_tags_index = 0;
size_t emacs_tags_slots = 0;

#define DECLARATION_STRING "\nDECLARE_INFO_COMMAND"

static void process_one_file (char *filename, FILE *doc_stream,
                              FILE *funs_stream);
static void maybe_dump_tags (FILE *stream);
static FILE *must_fopen (char *filename, char *mode);
static void init_func_key (unsigned int val);
static unsigned int next_func_key (void);

int
main (int argc, char **argv)
{
  register int i;
  int tags_only = 0;
  FILE *funs_stream, *doc_stream;

#if STRIP_DOT_EXE
  {
    char *dot = strrchr (argv[0], '.');

    if (dot && FILENAME_CMP (dot, ".exe") == 0)
      *dot = 0;
  }
#endif

  for (i = 1; i < argc; i++)
    if (strcmp (argv[i], "-tags") == 0)
      {
        tags_only++;
        break;
      }

  if (tags_only)
    {
      funs_filename = NULL_DEVICE;
      doc_filename = NULL_DEVICE;
    }

  /* The order of these calls depends exactly on the order in the
     Makefile.{in,am}, or they might fail on filesystems with
     high-precision times; see also the fclose calls below.  */
  funs_stream = must_fopen (funs_filename, "w");
  doc_stream = must_fopen (doc_filename, "w");

  fprintf (funs_stream,
      "/* %s -- Generated declarations for Info commands. */\n\n"
      "#include \"info.h\"\n"
      "#include \"window.h\"\n",
      funs_filename);

  for (i = 0; doc_header[i]; i++)
    {
      fprintf (doc_stream, doc_header[i], argv[0], argv[0]);
      fprintf (doc_stream, "\n");
    }

  fprintf (doc_stream,
           _("   Source files groveled to make this file include:\n\n"));

  for (i = 1; i < argc; i++)
    fprintf (doc_stream, "\t%s\n", argv[i]);

  fprintf (doc_stream, "\n");
  for (i = 0; doc_header_1[i]; i++)
    fprintf (doc_stream, "%s\n", doc_header_1[i]);

  init_func_key(0);

  for (i = 1; i < argc; i++)
    {
      char *curfile;
      curfile = argv[i];

      if (*curfile == '-')
        continue;

      fprintf (doc_stream, "/* Commands found in \"%s\". */\n", curfile);
      fprintf (funs_stream, "\n/* Functions declared in \"%s\". */\n",
               curfile);

      process_one_file (curfile, doc_stream, funs_stream);
    }

  fprintf (doc_stream, "   { NULL, NULL, NULL, NULL }\n};\n");
  fprintf (funs_stream, "\n#define A_NCOMMANDS %u\n", next_func_key());

  /* The order of these calls also depends exactly on the order in the
   * Makefile.{in,am}; see the must_fopen calls above.  */
  fclose (funs_stream);
  fclose (doc_stream);

  if (tags_only)
    maybe_dump_tags (stdout);
  return 0;
}

/* Dumping out the contents of an Emacs tags table. */
static void
maybe_dump_tags (FILE *stream)
{
  size_t i;

  /* Emacs needs its TAGS file to be in Unix text format (i.e., only
     newline at end of every line, no CR), so when we generate a
     TAGS table, we must switch the output stream to binary mode.
     (If the table is written to a terminal, this is obviously not needed.) */
  SET_BINARY (fileno (stream));

  /* Print out the information for each block. */
  for (i = 0; i < emacs_tags_index; i++)
    {
      size_t j;
      register EMACS_TAG_BLOCK *block;
      register EMACS_TAG *etag;
      long block_len;

      block_len = 0;
      block = emacs_tags[i];

      /* Calculate the length of the dumped block first. */
      for (j = 0; j < block->entries_index; j++)
        {
          char digits[30];
          etag = block->entries[j];
          block_len += 3 + strlen (etag->name);
          sprintf (digits, "%d,%ld", etag->line, etag->char_offset);
          block_len += strlen (digits);
        }

      /* Print out the defining line. */
      fprintf (stream, "\f\n%s,%ld\n", block->filename, block_len);

      /* Print out the individual tags. */
      for (j = 0; j < block->entries_index; j++)
        {
          etag = block->entries[j];

          fprintf (stream, "%s,\177%d,%ld\n",
                   etag->name, etag->line, etag->char_offset);
        }
    }
}

/* Keeping track of names, line numbers and character offsets of functions
   found in source files. */
static EMACS_TAG_BLOCK *
make_emacs_tag_block (char *filename)
{
  EMACS_TAG_BLOCK *block;

  block = xmalloc (sizeof (EMACS_TAG_BLOCK));
  block->filename = xstrdup (filename);
  block->entrylen = 0;
  block->entries = NULL;
  block->entries_index = 0;
  block->entries_slots = 0;
  return block;
}

static void
add_tag_to_block (EMACS_TAG_BLOCK *block,
    char *name, int line, long int char_offset)
{
  EMACS_TAG *tag;

  tag = xmalloc (sizeof (EMACS_TAG));
  tag->name = name;
  tag->line = line;
  tag->char_offset = char_offset;
  add_pointer_to_array (tag, block->entries_index, block->entries,
                        block->entries_slots, 50);
}

/* Read the file represented by FILENAME into core, and search it for Info
   function declarations.  Output the declarations in various forms to the
   DOC_STREAM and FUNS_STREAM. */
static void
process_one_file (char *filename, FILE *doc_stream, FILE *funs_stream)
{
  int descriptor, decl_len;
  char *buffer, *decl_str;
  struct stat finfo;
  long offset;
  long file_size;
  EMACS_TAG_BLOCK *block;

  if (stat (filename, &finfo) == -1)
    fatal_file_error (filename);

  descriptor = open (filename, O_RDONLY, 0666);

  if (descriptor == -1)
    fatal_file_error (filename);

  file_size = (long) finfo.st_size;
  buffer = xmalloc (1 + file_size);
  /* On some systems, the buffer will actually contain
     less characters than the full file's size, because
     the CR characters are removed from line endings.  */
  file_size = read (descriptor, buffer, file_size);
  close (descriptor);

  offset = 0;
  decl_str = DECLARATION_STRING;
  decl_len = strlen (decl_str);

  block = make_emacs_tag_block (filename);

  while (1)
    {
      long point = 0;
      long line_start = 0;
      int line_number = 0;

      char *func, *doc;
      char *func_name;

      for (; offset < (file_size - decl_len); offset++)
        {
          if (buffer[offset] == '\n')
            {
              line_number++;
              line_start = offset + 1;
            }

          if (strncmp (buffer + offset, decl_str, decl_len) == 0)
            {
              offset += decl_len;
              point = offset;
              break;
            }
        }

      if (!point)
        break;

      /* Skip forward until we find the open paren. */
      while (point < file_size)
        {
          if (buffer[point] == '\n')
            {
              line_number++;
              line_start = point + 1;
            }
          else if (buffer[point] == '(')
            break;

          point++;
        }

      while (point++ < file_size)
        {
          if (!whitespace_or_newline (buffer[point]))
            break;
          else if (buffer[point] == '\n')
            {
              line_number++;
              line_start = point + 1;
            }
        }

      if (point >= file_size)
        break;

      /* Now looking at name of function.  Get it. */
      for (offset = point; buffer[offset] != ','; offset++);
      func = xmalloc (1 + (offset - point));
      strncpy (func, buffer + point, offset - point);
      func[offset - point] = '\0';

      /* Remember this tag in the current block. */
      {
        char *tag_name;

        tag_name = xmalloc (1 + (offset - line_start));
        strncpy (tag_name, buffer + line_start, offset - line_start);
        tag_name[offset - line_start] = '\0';
        add_tag_to_block (block, tag_name, line_number, point);
      }

      /* Generate the user-visible function name from the function's name. */
      {
        register int i;
        char *name_start;

        name_start = func;

        if (strncmp (name_start, "info_", 5) == 0)
          name_start += 5;

        func_name = xstrdup (name_start);

        /* Fix up "ea" commands. */
        if (strncmp (func_name, "ea_", 3) == 0)
          {
            char *temp_func_name;

            temp_func_name = xmalloc (10 + strlen (func_name));
            strcpy (temp_func_name, "echo_area_");
            strcat (temp_func_name, func_name + 3);
            free (func_name);
            func_name = temp_func_name;
          }

        for (i = 0; func_name[i]; i++)
          if (func_name[i] == '_')
            func_name[i] = '-';
      }

      /* Find doc string. */
      point = offset + 1;

      while (point < file_size)
        {
          if (buffer[point] == '\n')
            {
              line_number++;
              line_start = point + 1;
            }

          if (buffer[point] == '"')
            break;
          else
            point++;
        }

      offset = point + 1;

      while (offset < file_size)
        {
          if (buffer[offset] == '\n')
            {
              line_number++;
              line_start = offset + 1;
            }

          if (buffer[offset] == '\\')
            offset += 2;
          else if (buffer[offset] == '"')
            break;
          else
            offset++;
        }

      offset++;
      if (offset >= file_size)
        break;

      doc = xmalloc (1 + (offset - point));
      strncpy (doc, buffer + point, offset - point);
      doc[offset - point] = '\0';

      fprintf (doc_stream,
          "   { (VFunction *)%s, \"%s\", (FUNCTION_KEYSEQ *)0, %s },\n",
          func, func_name, doc);

      free (func_name);

      fprintf (funs_stream, "#define A_%s %u\n", func, next_func_key());
      fprintf (funs_stream,
          "extern void %s (WINDOW *window, int count);\n",
          func);
      free (func);
      free (doc);
    }
  free (buffer);

  /* If we created any tags, remember this file on our global list.  Otherwise,
     free the memory already allocated to it. */
  if (block->entries)
    add_pointer_to_array (block, emacs_tags_index, emacs_tags,
                          emacs_tags_slots, 10);
  else
    {
      free (block->filename);
      free (block);
    }
}

static void
fatal_file_error (char *filename)
{
  fprintf (stderr, _("Couldn't manipulate the file %s.\n"), filename);
  exit (EXIT_FAILURE);
}

static FILE *
must_fopen (char *filename, char *mode)
{
  FILE *stream;

  stream = fopen (filename, mode);
  if (!stream)
    fatal_file_error (filename);

  return stream;
}

static unsigned int func_key;

static void
init_func_key(unsigned int val)
{
	func_key = val;
}

static unsigned int
next_func_key(void)
{
	return func_key++;
}