File: mkdoc.c

package info (click to toggle)
zile 2.2.59-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,772 kB
  • ctags: 1,124
  • sloc: ansic: 13,670; sh: 1,140; makefile: 168
file content (202 lines) | stat: -rw-r--r-- 4,455 bytes parent folder | download
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
/* A Quick & Dirty tool to produce the AUTODOC file.

   Copyright (c) 2008 Free Software Foundation, Inc.
   Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Sandro Sigala.
   Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008 Reuben Thomas.

   This file is part of GNU Zile.

   GNU Zile 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, or (at your option)
   any later version.

   GNU Zile 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 GNU Zile; see the file COPYING.  If not, write to the
   Free Software Foundation, Fifth Floor, 51 Franklin Street, Boston,
   MA 02111-1301, USA.  */

#include "config.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* #include other sources so this program can be easily built on the
   build host when cross-compiling */
#include "strrstr.c"
#include "vasprintf.c"
#include "zmalloc.c"
#include "astr.c"

struct fentry
{
  char *name;
  char *key1;
  char *key2;
  char *key3;
  char *key4;
  astr doc;
} fentry_table[] =
{
#define X0(zile_name, c_name) \
  { zile_name, NULL, NULL, NULL, NULL, NULL },
#define X1(zile_name, c_name, key1) \
  { zile_name, key1, NULL, NULL, NULL, NULL },
#define X2(zile_name, c_name, key1, key2) \
  { zile_name, key1, key2, NULL, NULL, NULL },
#define X3(zile_name, c_name, key1, key2, key3) \
  { zile_name, key1, key2, key3, NULL, NULL },
#include "tbl_funcs.h"
#undef X0
#undef X1
#undef X2
#undef X3
};

#define fentry_table_size (sizeof fentry_table  / sizeof fentry_table[0])

struct ventry
{
  char *name;
  char *defvalue;
  int local;
  char *doc;
} ventry_table[] =
{
#define X(name, defvalue, local, doc) \
	{ name, defvalue, local, doc },
#include "tbl_vars.h"
#undef X
};

#define ventry_table_size (sizeof ventry_table / sizeof ventry_table[0])

FILE *input_file;
FILE *output_file;

static void
fdecl (const char *name)
{
  astr doc = astr_new ();
  astr buf;
  unsigned s = 0, i;

  while ((buf = astr_fgets (input_file)) != NULL)
    {
      if (s == 1)
	{
	  if (!strncmp (astr_cstr (buf), "+*/", (size_t) 3))
	    break;
	  astr_cat (doc, buf);
	  astr_cat_char (doc, '\n');
	}
      if (!strncmp (astr_cstr (buf), "/*+", (size_t) 3))
	s = 1;
      else if (astr_cstr (buf)[0] == '{')
	break;
      astr_delete (buf);
    }

  for (i = 0; i < fentry_table_size; ++i)
    if (!strcmp (name, fentry_table[i].name))
      fentry_table[i].doc = doc;
}

static void
parse (void)
{
  astr buf;

  while ((buf = astr_fgets (input_file)) != NULL)
    {
      if (!strncmp (astr_cstr (buf), "DEFUN (", (size_t) 6))
	{
	  int i, j;
	  astr sub;
	  i = astr_find_cstr (buf, "\"");
	  j = astr_rfind_cstr (buf, "\"");
	  if (i < 0 || j < 0 || i == j)
	    {
	      fprintf (stderr, "mkdoc: invalid DEFUN () syntax\n");
	      exit (1);
	    }
	  sub = astr_substr (buf, i + 1, (size_t) (j - i - 1));
	  astr_cpy (buf, sub);
	  astr_delete (sub);
	  fdecl (astr_cstr (buf));
	}
      astr_delete (buf);
    }
}

static void
dump_help (void)
{
  unsigned int i;
  for (i = 0; i < fentry_table_size; ++i)
    {
      astr doc = fentry_table[i].doc;
      if (doc != NULL)
	fprintf (output_file, "\fF_%s\n%s",
		 fentry_table[i].name, astr_cstr (doc));
    }
  for (i = 0; i < ventry_table_size; ++i)
    fprintf (output_file, "\fV_%s\n%s\n%s\n",
	     ventry_table[i].name, ventry_table[i].defvalue,
	     ventry_table[i].doc);
}

static void
process_file (char *filename)
{
  if (filename != NULL && strcmp (filename, "-") != 0)
    {
      input_file = fopen (filename, "r");
      if (input_file == NULL)
	{
	  fprintf (stderr, "mkdoc:%s: %s\n", filename, strerror (errno));
	  exit (1);
	}
    }
  else
    input_file = stdin;

  parse ();

  if (input_file != stdin)
    fclose (input_file);
}

/*
 * Stub to make zmalloc &c. happy.
 */
void
zile_exit (int doabort GCC_UNUSED)
{
  exit (2);
}

int
main (int argc, char **argv)
{
  input_file = stdin;
  output_file = stdout;

  if (argc < 1)
    process_file (NULL);
  else
    while (*argv)
      process_file (*argv++);

  dump_help ();

  return 0;
}