]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/gen-helpfiles.c
Bash-5.2 patch 17: fix for optimizing forks when using the . builtin in a subshell
[thirdparty/bash.git] / builtins / gen-helpfiles.c
CommitLineData
ac50fbac
CR
1/* gen-helpfiles - create files containing builtin help text */
2
74091dd4 3/* Copyright (C) 2012-2021 Free Software Foundation, Inc.
ac50fbac
CR
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21/* This links with a specially-generated version of builtins.c and takes
22 the long_doc members of each struct builtin element and writes those to
23 the file named by the `handle' member of the struct builtin element. */
24
25#if !defined (CROSS_COMPILING)
26# include <config.h>
27#else /* CROSS_COMPILING */
28/* A conservative set of defines based on POSIX/SUS3/XPG6 */
29# define HAVE_UNISTD_H
30# define HAVE_STRING_H
31# define HAVE_STDLIB_H
32
33# define HAVE_RENAME
34#endif /* CROSS_COMPILING */
35
36#if defined (HAVE_UNISTD_H)
37# ifdef _MINIX
38# include <sys/types.h>
39# endif
40# include <unistd.h>
41#endif
42
43#ifndef _MINIX
44# include "../bashtypes.h"
45# if defined (HAVE_SYS_FILE_H)
46# include <sys/file.h>
47# endif
48#endif
49
50#include "posixstat.h"
51#include "filecntl.h"
52
53#include "../bashansi.h"
54#include <stdio.h>
55#include <errno.h>
56
57#include "stdc.h"
58
59#include "../builtins.h"
60#include "tmpbuiltins.h"
61
62#if defined (USING_BASH_MALLOC)
63#undef xmalloc
64#undef xrealloc
65#undef xfree
66
74091dd4 67#undef malloc
ac50fbac
CR
68#undef free /* defined in xmalloc.h */
69#endif
70
71#ifndef errno
72extern int errno;
73#endif
74
75#if !defined (__STDC__) && !defined (strcpy)
76extern char *strcpy ();
77#endif /* !__STDC__ && !strcpy */
78
79#define whitespace(c) (((c) == ' ') || ((c) == '\t'))
80
81/* Flag values that builtins can have. */
82#define BUILTIN_FLAG_SPECIAL 0x01
83#define BUILTIN_FLAG_ASSIGNMENT 0x02
84#define BUILTIN_FLAG_POSIX_BUILTIN 0x04
85
86#define BASE_INDENT 4
87
88/* Non-zero means to produce separate help files for each builtin, named by
89 the builtin name, in `./helpfiles'. */
90int separate_helpfiles = 0;
91
92/* Non-zero means to create single C strings for each `longdoc', with
93 embedded newlines, for ease of translation. */
94int single_longdoc_strings = 1;
95
96/* The name of a directory into which the separate external help files will
97 eventually be installed. */
98char *helpfile_directory;
99
100/* Forward declarations. */
101
8868edaf 102int write_helpfiles PARAMS((struct builtin *));
ac50fbac
CR
103
104/* For each file mentioned on the command line, process it and
105 write the information to STRUCTFILE and EXTERNFILE, while
8868edaf 106 creating the production file if necessary. */
ac50fbac
CR
107int
108main (argc, argv)
109 int argc;
110 char **argv;
111{
112 int arg_index = 1;
113
114 while (arg_index < argc && argv[arg_index][0] == '-')
115 {
116 char *arg = argv[arg_index++];
117
118 if (strcmp (arg, "-noproduction") == 0)
119 ;
120 else if (strcmp (arg, "-H") == 0)
121 helpfile_directory = argv[arg_index++];
122 else if (strcmp (arg, "-S") == 0)
123 single_longdoc_strings = 0;
124 else
125 {
126 fprintf (stderr, "%s: Unknown flag %s.\n", argv[0], arg);
127 exit (2);
128 }
129 }
130
131 write_helpfiles(shell_builtins);
132
133 exit (0);
134}
135
136/* Write DOCUMENTATION to STREAM, perhaps surrounding it with double-quotes
137 and quoting special characters in the string. Handle special things for
138 internationalization (gettext) and the single-string vs. multiple-strings
139 issues. */
140void
141write_documentation (stream, documentation, indentation)
142 FILE *stream;
143 char *documentation;
144 int indentation;
145{
146 if (stream == 0)
147 return;
148
149 if (documentation)
150 fprintf (stream, "%*s%s\n", indentation, " ", documentation);
151}
152
153int
154write_helpfiles (builtins)
155 struct builtin *builtins;
156{
157 char *helpfile, *bname, *fname;
158 FILE *helpfp;
159 int i, hdlen;
160 struct builtin b;
161
162 i = mkdir ("helpfiles", 0777);
163 if (i < 0 && errno != EEXIST)
164 {
165 fprintf (stderr, "write_helpfiles: helpfiles: cannot create directory\n");
166 return -1;
167 }
168
169 hdlen = strlen ("helpfiles/");
170 for (i = 0; i < num_shell_builtins; i++)
171 {
172 b = builtins[i];
173
174 fname = (char *)b.handle;
175 helpfile = (char *)malloc (hdlen + strlen (fname) + 1);
176 if (helpfile == 0)
177 {
178 fprintf (stderr, "gen-helpfiles: cannot allocate memory\n");
179 exit (1);
180 }
181 sprintf (helpfile, "helpfiles/%s", fname);
182
183 helpfp = fopen (helpfile, "w");
184 if (helpfp == 0)
185 {
186 fprintf (stderr, "write_helpfiles: cannot open %s\n", helpfile);
187 free (helpfile);
188 continue;
189 }
190
191 write_documentation (helpfp, b.long_doc[0], 4);
192
193 fflush (helpfp);
194 fclose (helpfp);
195 free (helpfile);
196 }
197 return 0;
198}