aboutsummaryrefslogtreecommitdiffstats
path: root/gpp.1
blob: 31af553d738ce97f0e47f1b039ed220b9544f6c5 (plain) (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
.TH GPP 1 gpp
.SH NAME
gpp - Bash-based preprocessor for anything

.SH SYNOPSIS
.B gpp
[-D
.IR name [= value ]]
[-f
.I file
| [-i
.IR input-file ]
[-o
.IR output-file ]]
[-n
.IR count ]
[-R
.IR macroline-replacement-text ]
[-s
.IR symbol ]
[-u [-u]]
.RI [ shell
.RI [ argument ]\ ...]

.SH ETYMOLOGY
gpp stands for General Preprocessor.

.SH DESCRIPTION
.B gpp
lets a developer embed directives written in GNU Bash
(this can be changed) into any text document. These
directives are used to automate the writting of parts
of the document.
.PP
The preprocessing directives start with a symbol (or
text string) specified by the developer. By default
this symbol is
.B @
(at).
.PP
Any line starting with 
.B @<
(where
.B @
is the selected symbol for preprocessing directives) or
.BR @> ,
or is between a line starting with
.B @
and a line starting with
.BR @> ,
is parsed as a line, written in
.BR bash (1),
that is executed during preprocessing. A
.B @<
line must have an associated
.B @>
line somewhere after it, all lines between them are
parsed as preprocessing directives. A
.B @>
does however not need an associated
.B @<
line somewhere before it, making
.B @>
suitable for single line directives.
.PP
Preprocessing directives can also be inline. For this, use
.BI @( COMMAND )
where
.I COMMAND
is the
.BR bash (1)
code to run. Additionally,
.B gpp
supports variable substitution.
.BI @{ VARIABLE }
will be replaces by the value if the variable
(possibility environment variable)
.IR VARIABLE .
.B gpp
supports all modifiers that
.BR bash (1)
(or which ever
.I shell
is selected) supports. For example, if you want the
value to be included but uppercase you can write
.BR @{ \fIVARIABLE\fP ^^} ,
or
.BI @{ VARIABLE ,,}
for lowercase.
.B gpp
also supports mathematical expressions that the via
the shells
.BI $(( EXPRESSION ))
syntax, by using
.BR @(( \fIEXPRESSION\fP )) .
.PP
Everything that is not a preprocessing directive is
echo verbatim, except all
.B @@
are replaced by
.BR @ .

.SH OPTIONS
The
.B gpp
utility conforms to the Base Definitions volume of POSIX.1-2017,
.IR "Section 12.2" ,
.IR "Utility Syntax Guidelines" .
.PP
The following option is supported:
.TP
.BR \-D\  \fIname\fP\fB=\fP\fIvalue\fP
Set the environment variable \fIname\fP to hold
the value \fIvalue\fP.
.TP
.BR \-D\  \fIname\fP
Set the environment variable \fIname\fP to hold
the value 1.
.TP
.BI \-f\  file
Equivalent to \-i
.I file
\-o
.IR file .
.TP
.BI \-i\  input-file
Select file to process. If
.B -
is specified, /dev/stdin will be used.
Default value is /dev/stdin.
.TP
.BI \-n\  n
Process the file recursively
.I n
times. Default value is 1.
.TP
.BI \-o\  output-file
Select output file. If
.B -
is specified, /dev/stdout will be used.
Default value is /dev/stdout.
.TP
.BI \-R\  macroline-replacement-text
Text to replace macrolines with in the output.
You may for example want to use
.B %
for TeX files.
Default value is the empty string.
.TP
.BI \-s\  symbol
Set the prefix symbol for preprocessor directives.
Default value is
.BR @ .
.TP
.B \-u
Clear the shebang line, remove it if this flag
is used twice. If used twice, an empty line
will be inserted after the new first line.

.SH OPERANDS
The following operands are supported:
.TP
.I shell
The shell to run instead of
.BR bash .
The
.I shell
must be compatible with POSIX shell.
.TP
.IR argument \ ...
Command line arguments for the shell.

.SH STDIN
The
.B gpp
utility does not use the standard input.

.SH INPUT FILES
The input file may be of any type, except it may not be a directory.

.SH ENVIRONMENT VARIABLES
The following environment variables affects the execution of
.BR gpp :
.TP
.B PATH
Default. See to the Base Definitions volume of POSIX.1-2017, Section 8.3, Other Environment Variables.
.PP
.B gpp
will set the environment variable
.B _GPP
to the zeroth argument
.B gpp
was execute with (the name of the command or path to the command).

.SH ASYNCHRONOUS EVENTS
Default.

.SH STDOUT
The
.B gpp
utility does not use the standard output.

.SH STDERR
The standard error is only used for diagnostic messages.

.SH OUTPUT FILES
The output file may be of any type, except it may not be a directory.

.SH EXTENDED DESCRIPTION
None.

.SH EXIT STATUS
.TP
0
Successful completion.
.TP
1
An error occurred.

.SH CONSEQUENCES OF ERRORS
Default.

.SH APPLICATION USAGE
None.

.SH EXAMPLES
.SS Conditional hello world
This example only includes the
.RB \(dq "Hello world" \(dq
line if the environment variable
.B HELLO
is defined and is not empty.
.PP
.nf
@>if [ -z "$HELLO" ]; the
Hello world
@>fi
.fi

.SS Mutliline preprocessor directive
This example creates the function
.BR uppercase ()
that convert lower case ASCII leters to uper case.
.PP
.nf
@<uppercase () {
	lower=qwertyuiopasdfghjklzxcvbnm
	upper=QWERTYUIOPASDFGHJKLZXCVBNM
	sed y/$lower/$upper/ <<<"$*"
@>}
.fi

.SS Inline directives
This example uses the
.BR uppercase ()
function above to convert the user's username
to upper case. If the user's username is
.BR john ,
the code will expand to
.B You are logged in as JOHN.
.PP
.nf
You are logged in as @(uppercase $USER).
.fi

.SS Variable expansions
In this example, if the user's username
.BR john ,
the code will expand to
.B You are logged in as john.
.PP
.nf
You are logged in as @{USER}.
.fi

.SS Variable expansion with substitution
This example uses a substitution mechanism in Bash to
convert the first letter in a variable to upper case.
In this example, if the user's username
.BR john ,
the code will expand to
.B You are logged in as John.
.PP
.nf
You are logged in as @{USER^}.
.fi

.SS Include paths
This example lets the user define a colon-separated
list of paths, in the
.B INCLUDEPATH
environment variable, in which to look for files to
either include directly into the source that is being
preprocessed, using the
.BR include_verbatim ()
function, directly into the preprocessor, using the
.BR include ()
function, or into the source that is being processed
but after preprocessing it with
.BR gpp ,
using the
.BR include_verbatim ()
and piping it into
.BR gpp .
.PP
.nf
locate () (
	IFS=:
	for d in $INCLUDEPATH; do
		if [ -f \(dq$d/$1\(dq ]; then
			printf \(aq%s\en\(aq \(dq$d/$1\(dq
			exit 0
		fi
	done
	printf \(aqCannot locate %s\en\(aq \(dq$1\(dq >&2
	exit 1
)

locatex () {
	local method
	local file
	set -e
	method=\(dq$1\(dq
	file=\(dq$2\(dq
	test -n \(dq$method\(dq
	test -n \(dq$file\(dq
	shift 2
	$method -- \(dq$(locate \(dq$file\(dq)\(dq \(dq$@\(dq
}

include () {
	locatex . \(dq$@\(dq
}

include_verbatim () {
	locatex cat \(dq$@\(dq
}
.fi

.SH RATIONALE
Programmers need more automation when we write software
and documentation. An unrestricted preprocessor lets
you automate just about anything. Of course, it can be
used for anything, must just writing software and
documentation. Preprocessing can be used for more than
automation, it can also be used to increase the flexibility
of the work.
.PP
C is one of the few languages that includes a preprocessor,
some times it is not enough; and all languages need
preprocessors.

.SH NOTES
None.

.SH BUGS
None.

.SH FUTURE DIRECTIONS
None.

.SH SEE ALSO
.BR bash (1),
.BR jpp (1),
.BR cpp (1),
.BR env (1)