Subversion Repositories HelenOS

Rev

Rev 3810 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3810 Rev 4687
1
#!/bin/sh
1
#!/bin/sh
2
# Copyright (C) 2008 Tim Post - All Rights Reserved
2
# Copyright (C) 2008 Tim Post - All Rights Reserved
3
# Redistribution and use in source and binary forms, with or without
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions are met:
4
# modification, are permitted provided that the following conditions are met:
5
#
5
#
6
# Redistributions of source code must retain the above copyright notice, this
6
# Redistributions of source code must retain the above copyright notice, this
7
# list of conditions and the following disclaimer.
7
# list of conditions and the following disclaimer.
8
#
8
#
9
# Redistributions in binary form must reproduce the above copyright notice,
9
# Redistributions in binary form must reproduce the above copyright notice,
10
# this list of conditions and the following disclaimer in the documentation
10
# this list of conditions and the following disclaimer in the documentation
11
# and/or other materials provided with the distribution.
11
# and/or other materials provided with the distribution.
12
#
12
#
13
# Neither the name of the original program's authors nor the names of its
13
# Neither the name of the original program's authors nor the names of its
14
# contributors may be used to endorse or promote products derived from this
14
# contributors may be used to endorse or promote products derived from this
15
# software without specific prior written permission.
15
# software without specific prior written permission.
16
#
16
#
17
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
27
# POSSIBILITY OF SUCH DAMAGE.
28
 
28
 
29
# Script to generate skeletal files for a new command
29
# Script to generate skeletal files for a new command
30
# Uses `getopt', not quite a bash-ism but might be
30
# Uses `getopt', not quite a bash-ism but might be
31
# lacking on some legacy systems.
31
# lacking on some legacy systems.
32
 
32
 
33
# If your shell does not support eval, shift (x) or
33
# If your shell does not support eval, shift (x) or
34
# here-now documents, sorry :) 
34
# here-now documents, sorry :) 
35
 
35
 
36
usage()
36
usage()
37
{
37
{
38
	def="$DEFAULT_COMMAND"
38
	def="$DEFAULT_COMMAND"
39
	cat << EOF
39
	cat << EOF
40
\`$PROGNAME' generates skeletal command files to simplify adding commands
40
\`$PROGNAME' generates skeletal command files to simplify adding commands
41
Usage: $PROGNAME [options] <location>
41
Usage: $PROGNAME [options] <location>
42
Options:
42
Options:
43
  -n, --name         Name of the command (default: ${def})
43
  -n, --name         Name of the command (default: ${def})
44
  -d, --desc         Short (20 30 chars) description of the command
44
  -d, --desc         Short (20 30 chars) description of the command
45
                     (def: "The $def command")
45
                     (def: "The $def command")
46
  -e, --entry        Entry function of the command (def: cmd_${def})
46
  -e, --entry        Entry function of the command (def: cmd_${def})
47
  -h, --help-entry   Entry function for command help (def: help_cmd_${def})
47
  -h, --help-entry   Entry function for command help (def: help_cmd_${def})
48
  -a, --alias        Alias (nickname) for this command (def: none)
48
  -a, --alias        Alias (nickname) for this command (def: none)
49
  -t, --type         Type of command (module or builtin) (def: module)
49
  -t, --type         Type of command (module or builtin) (def: module)
50
  -H, --help         This help summary
50
  -H, --help         This help summary
51
  -V, --version      Print $PROGNAME version and exit normally
51
  -V, --version      Print $PROGNAME version and exit normally
52
 
52
 
53
Notes:
53
Notes:
54
  You must supply at least the name of the command.
54
  You must supply at least the name of the command.
55
 
55
 
56
  If you do not specify a location (i.e. modules/foo), the command will be
56
  If you do not specify a location (i.e. modules/foo), the command will be
57
  created in modules/command_name or builtins/command_name depending on your
57
  created in modules/command_name or builtins/command_name depending on your
58
  selection.
58
  selection.
59
 
59
 
60
  This script will only create skeletal files and inform you what headers
60
  This script will only create skeletal files and inform you what headers
61
  need to be modified to incorporate the command. You will also have to
61
  need to be modified to incorporate the command. You will also have to
62
  manually update the main Makefile.
62
  manually update the main Makefile.
63
 
63
 
64
  This script is intended only to be a convenience for developers. Example use:
64
  This script is intended only to be a convenience for developers. Example use:
65
    $PROGNAME -n foo -d "Foo power" -a bar -r both -t module modules/foo
65
    $PROGNAME -n foo -d "Foo power" -a bar -r both -t module modules/foo
66
 
66
 
67
  The example would generate a modular command named 'foo', which is also
67
  The example would generate a modular command named 'foo', which is also
68
  reached by typing 'bar'.
68
  reached by typing 'bar'.
69
 
69
 
70
  Skeletal files do *not* depend on the autoconf generated "config.h" unless you
70
  Skeletal files do *not* depend on the autoconf generated "config.h" unless you
71
  include it. This may or may not be desirable depending on your use.
71
  include it. This may or may not be desirable depending on your use.
72
 
72
 
73
Report bugs to $PROGMAINT
73
Report bugs to $PROGMAINT
74
 
74
 
75
EOF
75
EOF
76
}
76
}
77
 
77
 
78
# Convert a string to all uppercase
78
# Convert a string to all uppercase
79
toupper()
79
toupper()
80
{
80
{
81
	local str="$1"
81
	local str="$1"
82
 
82
 
83
	echo "${str}" | tr 'a-z' 'A-Z'
83
	echo "${str}" | tr 'a-z' 'A-Z'
84
}
84
}
85
 
85
 
86
# Template stored `here-now' style, this generates all files needed
86
# Template stored `here-now' style, this generates all files needed
87
# for a new command according to arguments passed.
87
# for a new command according to arguments passed.
88
generate_code()
88
generate_code()
89
{
89
{
90
	echo "Creating ${OUTDIR}/${CMDNAME}_def.h ..."
90
	echo "Creating ${OUTDIR}/${CMDNAME}_def.h ..."
91
	cat << EOF > ${OUTDIR}/${CMDNAME}_def.h
91
	cat << EOF > ${OUTDIR}/${CMDNAME}_def.h
92
{
92
{
93
	"${CMDNAME}",
93
	"${CMDNAME}",
94
	"${CMDDESC}",
94
	"${CMDDESC}",
95
	&${CMDENTRY},
95
	&${CMDENTRY},
96
	&${HELPENTRY},
96
	&${HELPENTRY},
97
},
97
},
98
 
98
 
99
EOF
99
EOF
100
	[ -n "${CMDALIAS}" ] && cat << EOF >> ${OUTDIR}/${CMDNAME}_def.h
100
	[ -n "${CMDALIAS}" ] && cat << EOF >> ${OUTDIR}/${CMDNAME}_def.h
101
{
101
{
102
	"${CMDALIAS}",
102
	"${CMDALIAS}",
103
	NULL,
103
	NULL,
104
	&${CMDENTRY},
104
	&${CMDENTRY},
105
	&${HELPENTRY},
105
	&${HELPENTRY},
106
},
106
},
107
 
107
 
108
EOF
108
EOF
109
	local defname=$(toupper "${CMDNAME}")
109
	local defname=$(toupper "${CMDNAME}")
110
	echo "Creating ${OUTDIR}/entry.h ..."
110
	echo "Creating ${OUTDIR}/entry.h ..."
111
	cat << EOF > ${OUTDIR}/entry.h
111
	cat << EOF > ${OUTDIR}/entry.h
112
#ifndef ${defname}_ENTRY_H
112
#ifndef ${defname}_ENTRY_H
113
#define ${defname}_ENTRY_H
113
#define ${defname}_ENTRY_H
114
 
114
 
115
EOF
115
EOF
116
	[ "${CMDTYPE}" = "module" ] && cat << EOF >> ${OUTDIR}/entry.h
116
	[ "${CMDTYPE}" = "module" ] && cat << EOF >> ${OUTDIR}/entry.h
117
/* Entry points for the ${CMDNAME} command */
117
/* Entry points for the ${CMDNAME} command */
118
extern int ${CMDENTRY}(char **);
118
extern int ${CMDENTRY}(char **);
119
extern void ${HELPENTRY}(unsigned int);
119
extern void ${HELPENTRY}(unsigned int);
120
 
120
 
121
#endif /* ${defname}_ENTRY_H */
121
#endif /* ${defname}_ENTRY_H */
122
 
122
 
123
EOF
123
EOF
124
	[ "${CMDTYPE}" = "builtin" ] && cat << EOF >> ${OUTDIR}/entry.h
124
	[ "${CMDTYPE}" = "builtin" ] && cat << EOF >> ${OUTDIR}/entry.h
125
/* Pick up cliuser_t */
125
/* Pick up cliuser_t */
126
#include "scli.h"
126
#include "scli.h"
127
 
127
 
128
/* Entry points for the ${CMDNAME} command */
128
/* Entry points for the ${CMDNAME} command */
129
extern int * ${CMDENTRY}(char **, cliuser_t *);
129
extern int * ${CMDENTRY}(char **, cliuser_t *);
130
extern void * ${HELPENTRY}(unsigned int);
130
extern void * ${HELPENTRY}(unsigned int);
131
 
131
 
132
#endif /* ${defname}_ENTRY_H */
132
#endif /* ${defname}_ENTRY_H */
133
 
133
 
134
EOF
134
EOF
135
	echo "Creating ${OUTDIR}/${CMDNAME}.h ..."
135
	echo "Creating ${OUTDIR}/${CMDNAME}.h ..."
136
	cat << EOF > ${OUTDIR}/${CMDNAME}.h
136
	cat << EOF > ${OUTDIR}/${CMDNAME}.h
137
#ifndef ${defname}_H
137
#ifndef ${defname}_H
138
#define ${defname}_H
138
#define ${defname}_H
139
 
139
 
140
/* Prototypes for the ${CMDNAME} command, excluding entry points */
140
/* Prototypes for the ${CMDNAME} command, excluding entry points */
141
 
141
 
142
 
142
 
143
#endif /* ${defname}_H */
143
#endif /* ${defname}_H */
144
 
144
 
145
EOF
145
EOF
146
	echo "Creating ${OUTDIR}/${CMDNAME}.c ..."
146
	echo "Creating ${OUTDIR}/${CMDNAME}.c ..."
147
	cat << EOF > ${OUTDIR}/${CMDNAME}.c
147
	cat << EOF > ${OUTDIR}/${CMDNAME}.c
148
/* Automatically generated by ${PROGNAME} on ${TIMESTAMP}
148
/* Automatically generated by ${PROGNAME} on ${TIMESTAMP}
149
 * This is machine generated output. The author of ${PROGNAME} claims no
149
 * This is machine generated output. The author of ${PROGNAME} claims no
150
 * copyright over the contents of this file. Where legally permitted, the
150
 * copyright over the contents of this file. Where legally permitted, the
151
 * contents herein are donated to the public domain.
151
 * contents herein are donated to the public domain.
152
 *
152
 *
153
 * You should apply any license and copyright that you wish to this file,
153
 * You should apply any license and copyright that you wish to this file,
154
 * replacing this header in its entirety. */
154
 * replacing this header in its entirety. */
155
 
155
 
156
#include <stdio.h>
156
#include <stdio.h>
157
#include <stdlib.h>
157
#include <stdlib.h>
158
#include "config.h"
158
#include "config.h"
159
#include "util.h"
159
#include "util.h"
160
#include "errors.h"
160
#include "errors.h"
161
#include "entry.h"
161
#include "entry.h"
162
#include "${CMDNAME}.h"
162
#include "${CMDNAME}.h"
163
#include "cmds.h"
163
#include "cmds.h"
164
 
164
 
165
static const char *cmdname = "${CMDNAME}";
165
static const char *cmdname = "${CMDNAME}";
166
 
166
 
167
/* Dispays help for ${CMDNAME} in various levels */
167
/* Dispays help for ${CMDNAME} in various levels */
168
void ${HELPENTRY}(unsigned int level)
168
void ${HELPENTRY}(unsigned int level)
169
{
169
{
170
	printf("This is the %s help for '%s'.\n",
170
	printf("This is the %s help for '%s'.\n",
171
		level ? EXT_HELP : SHORT_HELP, cmdname);
171
		level ? EXT_HELP : SHORT_HELP, cmdname);
172
	return;
172
	return;
173
}
173
}
174
 
174
 
175
EOF
175
EOF
176
	[ "${CMDTYPE}" = "module" ] && cat << EOF >> ${OUTDIR}/${CMDNAME}.c
176
	[ "${CMDTYPE}" = "module" ] && cat << EOF >> ${OUTDIR}/${CMDNAME}.c
177
/* Main entry point for ${CMDNAME}, accepts an array of arguments */
177
/* Main entry point for ${CMDNAME}, accepts an array of arguments */
178
int ${CMDENTRY}(char **argv)
178
int ${CMDENTRY}(char **argv)
179
EOF
179
EOF
180
	[ "${CMDTYPE}" = "builtin" ] && cat << EOF >> ${OUTDIR}/${CMDNAME}.c
180
	[ "${CMDTYPE}" = "builtin" ] && cat << EOF >> ${OUTDIR}/${CMDNAME}.c
181
/* Main entry point for ${CMDNAME}, accepts an array of arguments and a
181
/* Main entry point for ${CMDNAME}, accepts an array of arguments and a
182
 * pointer to the cliuser_t structure */
182
 * pointer to the cliuser_t structure */
183
int ${CMDENTRY}(char **argv, cliuser_t *usr)
183
int ${CMDENTRY}(char **argv, cliuser_t *usr)
184
EOF
184
EOF
185
	cat << EOF >> ${OUTDIR}/${CMDNAME}.c
185
	cat << EOF >> ${OUTDIR}/${CMDNAME}.c
186
{
186
{
187
	unsigned int argc;
187
	unsigned int argc;
188
	unsigned int i;
188
	unsigned int i;
189
 
189
 
190
	/* Count the arguments */
190
	/* Count the arguments */
191
	for (argc = 0; argv[argc] != NULL; argc ++);
191
	for (argc = 0; argv[argc] != NULL; argc ++);
192
 
192
 
193
	printf("%s %s\n", TEST_ANNOUNCE, cmdname);
193
	printf("%s %s\n", TEST_ANNOUNCE, cmdname);
194
	printf("%d arguments passed to %s", argc - 1, cmdname);
194
	printf("%d arguments passed to %s", argc - 1, cmdname);
195
 
195
 
196
	if (argc < 2) {
196
	if (argc < 2) {
197
		printf("\n");
197
		printf("\n");
198
		return CMD_SUCCESS;
198
		return CMD_SUCCESS;
199
	}
199
	}
200
 
200
 
201
	printf(":\n");
201
	printf(":\n");
202
	for (i = 1; i < argc; i++)
202
	for (i = 1; i < argc; i++)
203
		printf("[%d] -> %s\n", i, argv[i]);
203
		printf("[%d] -> %s\n", i, argv[i]);
204
 
204
 
205
	return CMD_SUCCESS;
205
	return CMD_SUCCESS;
206
}
206
}
207
 
207
 
208
EOF
208
EOF
209
	printf "Done.\n\nYou should now modify %ss/%ss.h and ../Makefile" \
209
	printf "Done.\n\nYou should now modify %ss/%ss.h and ../Makefile" \
210
		"${CMDTYPE}" "${CMDTYPE}"
210
		"${CMDTYPE}" "${CMDTYPE}"
211
	printf " to include your new command.\n"
211
	printf " to include your new command.\n"
212
	[ -n "$CMDALIAS" ] &&  {
212
	[ -n "$CMDALIAS" ] &&  {
213
		printf "\nYou should also modify %ss/%s_aliases.h and " \
213
		printf "\nYou should also modify %ss/%s_aliases.h and " \
214
			"${CMDTYPE}" "${CMDTYPE}"
214
			"${CMDTYPE}" "${CMDTYPE}"
215
		printf "add %s as an alias for %s\n" \
215
		printf "add %s as an alias for %s\n" \
216
			"${CMDALIAS}" "${CMDNAME}"
216
			"${CMDALIAS}" "${CMDNAME}"
217
	}
217
	}
218
	printf "\nOnce completed, re-run make\n\n"
218
	printf "\nOnce completed, re-run make\n\n"
219
}
219
}
220
 
220
 
221
# Main program
221
# Main program
222
 
222
 
223
TIMESTAMP="$(date)"
223
TIMESTAMP="$(date)"
224
PROGNAME=$(basename $0)
224
PROGNAME=$(basename $0)
225
PROGVER="0.0.1"
225
PROGVER="0.0.1"
226
PROGMAINT="Tim Post <echo@echoreply.us>"
226
PROGMAINT="Tim Post <echo@echoreply.us>"
227
DEFAULT_COMMAND="cmdname"
227
DEFAULT_COMMAND="cmdname"
228
 
228
 
229
# We need at least one
229
# We need at least one
230
[ $# = 0 ] && usage && exit 1;
230
[ $# = 0 ] && usage && exit 1;
231
 
231
 
232
TEMP=$(getopt -o n:d:e:h:a:t:HV \
232
TEMP=$(getopt -o n:d:e:h:a:t:HV \
233
--long name:,desc:,entry:,help-entry:,alias:,type:,help,version \
233
--long name:,desc:,entry:,help-entry:,alias:,type:,help,version \
234
-- "$@") || {
234
-- "$@") || {
235
	echo "Try $PROGNAME --help for help"
235
	echo "Try $PROGNAME --help for help"
236
}
236
}
237
 
237
 
238
eval set -- "$TEMP"
238
eval set -- "$TEMP"
239
 
239
 
240
while true; do
240
while true; do
241
	case "$1" in
241
	case "$1" in
242
	-n | --name)
242
	-n | --name)
243
		CMDNAME="$2"
243
		CMDNAME="$2"
244
		shift 2
244
		shift 2
245
		continue
245
		continue
246
	;;
246
	;;
247
	-d | --desc)
247
	-d | --desc)
248
		CMDDESC="$2"
248
		CMDDESC="$2"
249
		shift 2
249
		shift 2
250
		continue
250
		continue
251
	;;
251
	;;
252
	-e | --entry)
252
	-e | --entry)
253
		CMDENTRY="$2"
253
		CMDENTRY="$2"
254
		shift 2
254
		shift 2
255
		continue
255
		continue
256
	;;
256
	;;
257
	-h | --help-entry)
257
	-h | --help-entry)
258
		HELPENTRY="$2"
258
		HELPENTRY="$2"
259
		shift 2
259
		shift 2
260
		continue
260
		continue
261
	;;
261
	;;
262
	-a | --alias)
262
	-a | --alias)
263
		CMDALIAS="$2"
263
		CMDALIAS="$2"
264
		shift 2
264
		shift 2
265
		continue
265
		continue
266
	;;
266
	;;
267
	-t | --type)
267
	-t | --type)
268
		CMDTYPE="$2"
268
		CMDTYPE="$2"
269
		shift 2
269
		shift 2
270
		continue
270
		continue
271
	;;
271
	;;
272
	-H | --help)
272
	-H | --help)
273
		usage
273
		usage
274
		exit 0
274
		exit 0
275
	;;
275
	;;
276
	-V | --version)
276
	-V | --version)
277
		echo "$PROGVER"
277
		echo "$PROGVER"
278
		exit 0
278
		exit 0
279
	;;
279
	;;
280
	--)
280
	--)
281
		break
281
		break
282
	;;
282
	;;
283
	esac
283
	esac
284
done
284
done
285
 
285
 
286
# Pick up a location if one was specified
286
# Pick up a location if one was specified
287
eval set -- "$*"
287
eval set -- "$*"
288
[ -n "$2" ] && OUTDIR="$2"
288
[ -n "$2" ] && OUTDIR="$2"
289
 
289
 
290
# Fill in defaults for whatever was not specified
290
# Fill in defaults for whatever was not specified
291
[ -n "$CMDNAME" ] || CMDNAME="$DEFAULT_COMMAND"
291
[ -n "$CMDNAME" ] || CMDNAME="$DEFAULT_COMMAND"
292
[ -n "$CMDDESC" ] || CMDDESC="The $CMDNAME command"
292
[ -n "$CMDDESC" ] || CMDDESC="The $CMDNAME command"
293
[ -n "$CMDENTRY" ] || CMDENTRY="cmd_${CMDNAME}"
293
[ -n "$CMDENTRY" ] || CMDENTRY="cmd_${CMDNAME}"
294
[ -n "$HELPENTRY" ] || HELPENTRY="help_cmd_${CMDNAME}"
294
[ -n "$HELPENTRY" ] || HELPENTRY="help_cmd_${CMDNAME}"
295
[ -n "$CMDTYPE" ] || CMDTYPE="module"
295
[ -n "$CMDTYPE" ] || CMDTYPE="module"
296
[ -n "$OUTDIR" ] || OUTDIR="${CMDTYPE}s/${CMDNAME}"
296
[ -n "$OUTDIR" ] || OUTDIR="${CMDTYPE}s/${CMDNAME}"
297
 
297
 
298
 
298
 
299
# Do a little sanity
299
# Do a little sanity
300
[ -d $OUTDIR ] && {
300
[ -d $OUTDIR ] && {
301
	echo "$OUTDIR already exists, remove it to proceed."
301
	echo "$OUTDIR already exists, remove it to proceed."
302
	exit 1
302
	exit 1
303
}
303
}
304
 
304
 
305
mkdir -p ${OUTDIR} >/dev/null 2>&1 || {
305
mkdir -p ${OUTDIR} >/dev/null 2>&1 || {
306
	echo "Could not create ${OUTDIR}, aborting!"
306
	echo "Could not create ${OUTDIR}, aborting!"
307
	exit 1
307
	exit 1
308
}
308
}
309
 
309
 
310
# Generate the files and inform on how to include them based on options
310
# Generate the files and inform on how to include them based on options
311
generate_code
311
generate_code
312
 
312
 
313
exit 0
313
exit 0
314
 
314
 
315
 
315