-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_function
executable file
·334 lines (266 loc) · 7.07 KB
/
new_function
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
#!/bin/bash
###########################################################################
#
# Shell program to create shell function templates.
#
# Copyright © 1999-2002, William Shotts, Jr.
# <bshotts@users.sourceforge.net>
#
# 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 2 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.
#
# This software is part of the LinuxCommand.org project, a site for
# Linux education and advocacy devoted to helping users of legacy
# operating systems migrate into the future.
#
# You may contact the LinuxCommand.org project at:
#
# http://www.linuxcommand.org
#
# Description:
#
# This program creates shell function templates. It can output
# to a file or standard output. During template creation, the user
# is prompted to enter information about the function, including its
# name, purpose, and a list of arguments.
#
# Usage:
#
# new_function [ -h | --help ] [-f file]
#
# Options:
#
# -h, --help Display this help message and exit.
# -f file Write output to file, otherwise output
# is written to standard output.
#
#
# Revisions:
#
# 12/24/1999 File created
# 01/02/2000 Fixed typo
# 01/11/2000 Fixed typo
# 03/23/2000 Added CVS ID and LinuxCommand.org info
# 02/10/2002 "Restyled" output, improved user interface.
#
# $Id: new_function,v 1.2 2002/02/11 00:18:15 bshotts Exp $
###########################################################################
###########################################################################
# Constants
###########################################################################
PROGNAME=$(basename $0)
TEMP_FILE=/tmp/${PROGNAME}.$$.$RANDOM
###########################################################################
# Functions
###########################################################################
function clean_up
{
#####
# Function to remove temporary files and other housekeeping
# No arguments
#####
rm -f ${TEMP_FILE}
}
function graceful_exit
{
#####
# Function called for a graceful exit
# No arguments
#####
clean_up
exit
}
function error_exit
{
#####
# Function for exit due to fatal program error
# Accepts 1 argument
# string containing descriptive error message
#####
local err_msg
err_msg="${PROGNAME}: ${1}"
echo ${err_msg} >&2
clean_up
exit 1
}
function term_exit
{
#####
# Function to perform exit if termination signal is trapped
# No arguments
#####
echo "${PROGNAME}: Terminated"
clean_up
exit
}
function int_exit
{
#####
# Function to perform exit if interrupt signal is trapped
# No arguments
#####
echo "${PROGNAME}: Aborted by user"
clean_up
exit
}
function usage
{
#####
# Function to display usage message (does not exit)
# No arguments
#####
echo "Usage: ${PROGNAME} [-h | --help] [-f file]"
}
function helptext
{
#####
# Function to display help message for program
# No arguments
#####
local tab=$(echo -en "\t\t")
cat <<- -EOF-
This is a program to create function templates.
$(usage)
Options:
-h, --help Display this help message and exit.
-f file Write output to file, otherwise output
${tab}is written to standard output.
-EOF-
}
function ask_yes_no
{
#####
# Function to ask a yes/no question
# Arguments:
# 1 prompt string (optional)
#####
local yn=
while [ "$yn" = "" ]; do
echo -en "$1"
read yn
case $yn in
y|Y) yn=0 ;;
n|N) yn=1 ;;
*) yn=
echo "Invalid response - please answer y or n"
;;
esac
done
return $yn
}
function write_function
{
#####
# Constructs and outputs function template
# Arguments:
# none
#####
local function_name
local description
local arg_count=0
local arg_desc
local optional
local arg_test
echo -en "Name of function: "
read function_name
echo -e "\nfunction ${function_name}" > $TEMP_FILE
echo -e "{\n" >> $TEMP_FILE
echo "# ------------------------------------------------------------------------" >> $TEMP_FILE
echo -en "Description of function: "
read description
echo "# ${description}" >> $TEMP_FILE
echo "# Arguments:" >> $TEMP_FILE
if ask_yes_no "Does this function have arguments? [y/n]: "; then
arg_count=1
# While there are arguments let's ask the user about them
while [ $arg_count -gt 0 ]; do
echo -en "Description of argument ${arg_count} (return to quit): "
read arg_desc
# If user just hit return, that means we are done asking about arguments
if [ "$arg_desc" != "" ]; then
# Once we get an optional argument, all remaining args must be optional
if [ "$optional" != "(optional)" ]; then
if ask_yes_no "Is argument ${arg_count} optional? [y/n]: "; then
optional="(optional)"
else
# if argument is required, we construct a test for it in the code
optional="(required)"
arg_test="${arg_test}$(echo -en "\n\tif [ \"\$$arg_count\" = \"\" ]; then \n\t\terror_exit \"${function_name}: missing argument ${arg_count}\"\n\tfi")"
fi
fi
echo "# $arg_count $arg_desc ${optional}" >> $TEMP_FILE
arg_count=$((arg_count + 1))
else
arg_count=0
fi
done
else
echo "# none" >> $TEMP_FILE
fi
echo -e "# ------------------------------------------------------------------------\n" >> $TEMP_FILE
# If there are required arguments, put in the tests
if [ "$arg_test" != "" ]; then
echo -e "\t# Fatal error if required arguments are missing" >> $TEMP_FILE
echo -e "$arg_test\n" >> $TEMP_FILE
fi
echo -e " return\n" >> $TEMP_FILE
echo -e "} # end of $function_name\n" >> $TEMP_FILE
} # end of write_function
###########################################################################
# Program starts here
###########################################################################
# Trap TERM, HUP, and INT signals and properly exit
trap term_exit TERM HUP
trap int_exit INT
# Process command line arguments
if [ "$1" = "--help" ]; then
helptext
graceful_exit
fi
file_flag=0
while getopts ":hf:" opt; do
case $opt in
f ) file_flag=1
output_file=${OPTARG}
# See if output file already exists
if [ -e "${output_file}" ] ; then
# Make sure it's a regular file
if [ -f "${output_file}" ] ; then
# Is it writable?
if [ -w "${output_file}" ]; then
# Confirm overwrite
if ! ask_yes_no "Output file exists - Overwrite? [y/n] "; then
int_exit
fi
else
error_exit "Output file is not writable"
fi
else
error_exit "Output file is not a regular file"
fi
else
# Try and write it
touch "${output_file}" || error_exit "Cannot write output file"
fi ;;
h ) helptext
graceful_exit ;;
* ) usage
exit 1
esac
done
# Do all the real work here
write_function
# Are we writing output to a file?
if [ $file_flag = 1 ]; then
cp $TEMP_FILE $output_file
else
cat $TEMP_FILE
fi
graceful_exit