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