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
|
/*
* cwgen - simple morse code practice character generator
*/
/* trap for when no valid platform selected */
#if !defined(LINUX) && !defined(SCO) && !defined(UNIXWARE)
#include <-DLINUX,-DSCO,_or_-DUNIXWARE_must_be_defined>
#endif
/* include files */
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* conditional include files */
#if defined(LINUX)
# include <strings.h>
# include <getopt.h>
#endif
#if defined(SCO)
# include <string.h>
# include <limits.h>
# define MAXPATHLEN _POSIX_PATH_MAX
#endif
#if defined(UNIXWARE)
# include <string.h>
#endif
/* definitions */
#define VERSION "cwgen version 1.1"
#define bool int /* boolean type */
#define FALSE 0
#define TRUE (!FALSE)
#define EOS '\0' /* end of string */
#define ASC_FNS '/' /* ASCII filename sep char */
#define MIN_GROUPS 1 /* lowest no of groups allowed */
#define MAX_GROUPS 20000 /* highest no of groups allowed */
#define MIN_GROUPSIZE 1 /* lowest group size allowed */
#define MAX_GROUPSIZE 80 /* highest WPM allowed */
#define DEF_CHARSET "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
/* default character set */
#define MAXARGS 128 /* args to exec */
#define MAXOPTSTR 1024 /* longest _OPTIONS env variable */
#define ARGS_WHITESPACE " \t" /* argument separators */
#define ENV_OPTIONS "CWGEN_OPTIONS" /* env string holding options */
/* initial defaults for parameters */
#define INIT_GROUPS 128
#define INIT_GROUPSIZE 5
/* global variables */
static int groups = INIT_GROUPS; /* initially 128 groups */
static int groupsize = INIT_GROUPSIZE;/* initially 5-character groups */
static char *charset = DEF_CHARSET; /* initially send mixed letters nums */
/*
* print_help - give help information
*/
static void print_help( char *pname ) {
printf(
#if defined(LINUX)
"Usage: %s [options...]\n\n\
-g, --groups=GROUPS send GROUPS groups of chars (default %d)\n\
valid GROUPS values are between %d and %d\n\
-n, --groupsize=GS make groups GS chars (default %d)\n\
valid GS values are between %d and %d\n\
-c, --charset=CHARSET select chars to send from this set\n\
(default %s)\n\
-h, --help print this message\n\
-v, --version output version information and exit\n\n",
#endif
#if defined(SCO) || defined(UNIXWARE)
"Usage: %s [options...]\n\n\
-g GROUPS send GROUPS groups of chars (default %d)\n\
valid GROUPS values are between %d and %d\n\
-n GS make groups GS chars (default %d)\n\
valid GS values are between %d and %d\n\
-c CHARSET select chars to send from this set\n\
(default %s)\n\
-h print this message\n\
-v output version information and exit\n\n",
#endif
pname,
INIT_GROUPS, MIN_GROUPS, MAX_GROUPS,
INIT_GROUPSIZE, MIN_GROUPSIZE, MAX_GROUPSIZE, DEF_CHARSET );
}
/*
* parse_cmdline - parse command line options for initial values
*/
static void parse_cmdline( int argc, char **argv ) {
int c; /* option character */
char *argv0; /* program name */
int argind; /* loop index */
char env_options[MAXOPTSTR]; /* env options string */
char *sptr; /* string pointer */
char *local_argv[MAXARGS]; /* local argv array */
int local_argc = 0; /* local argc */
#if defined(LINUX)
int option_index; /* option index */
static struct option long_options[] = { /* options table */
{ "groups", 1, 0, 0 },
{ "groupsize", 1, 0, 0 },
{ "charset", 1, 0, 0 },
{ "help", 0, 0, 0 },
{ "version", 0, 0, 0 },
{ 0, 0, 0, 0 }};
#endif
/* set argv0 to be the basename part of the program name */
argv0 = argv[0] + strlen( argv[0] );
while ( *argv0 != ASC_FNS && argv0 > argv[0] )
argv0--;
if ( *argv0 == ASC_FNS ) argv0++;
/* build a new view of argc and argv by first prepending
the strings from ..._OPTIONS, if defined, then putting the
command line args on (so they take precedence) */
local_argv[ local_argc++ ] = argv[0];
if ( getenv( ENV_OPTIONS ) != NULL ) {
strcpy( env_options, getenv( ENV_OPTIONS ));
sptr = env_options;
while ( local_argc < MAXARGS - 1 ) {
for ( ; strchr( ARGS_WHITESPACE, *sptr ) != (char *)NULL
&& *sptr != EOS;
sptr++ );
if ( *sptr == EOS )
break;
else {
local_argv[ local_argc++ ] = sptr;
for ( ; strchr( ARGS_WHITESPACE, *sptr )
== (char *)NULL && *sptr != EOS;
sptr++ );
if ( strchr( ARGS_WHITESPACE, *sptr )
!= (char *)NULL && *sptr != EOS ) {
*sptr = EOS; sptr++;
}
}
}
}
for ( argind = 1; argind < argc; argind++ ) {
local_argv[ local_argc++ ] = argv[ argind ];
}
/* process every option */
#if defined(LINUX)
while ( (c=getopt_long( local_argc, local_argv, "g:n:c:hv",
long_options, &option_index )) != -1 ) {
#endif
#if defined(SCO) || defined(UNIXWARE)
while ( (c=getopt( local_argc, local_argv, "g:n:c:hv" )) != -1 ) {
#endif
/* check for -g or --groups argument */
if ( c == 'g'
#if defined(LINUX)
|| c == 0 && !strcmp( long_options[option_index].name,
"groups" )) {
#endif
#if defined(SCO) || defined(UNIXWARE)
) {
#endif
if ( sscanf( optarg, "%d", &groups ) != 1 ||
groups < MIN_GROUPS ||
groups > MAX_GROUPS ) {
fprintf( stderr, "%s: invalid groups value\n",
argv0 );
exit( 1 );
}
}
/* check for -n or --groupsize argument */
else if ( c == 'n'
#if defined(LINUX)
|| c == 0 && !strcmp( long_options[option_index].name,
"groupsize" )) {
#endif
#if defined(SCO) || defined(UNIXWARE)
) {
#endif
if ( sscanf( optarg, "%d", &groupsize ) != 1 ||
groupsize < MIN_GROUPSIZE ||
groupsize > MAX_GROUPSIZE ) {
fprintf( stderr, "%s: invalid groupsize value\n",
argv0 );
exit( 1 );
}
}
/* check for -c or --charset argument */
else if ( c == 'c'
#if defined(LINUX)
|| c == 0 && !strcmp( long_options[option_index].name,
"charset" )) {
#endif
#if defined(SCO) || defined(UNIXWARE)
) {
#endif
charset = optarg;
}
/* check for -h or --help argument */
else if ( c == 'h'
#if defined(LINUX)
|| c == 0 && !strcmp( long_options[option_index].name,
"help" )) {
#endif
#if defined(SCO) || defined(UNIXWARE)
) {
#endif
print_help( argv0 );
exit( 0 );
}
/* check for -v or --version argument */
else if ( c == 'v'
#if defined(LINUX)
|| c == 0 && !strcmp( long_options[option_index].name,
"version" )) {
#endif
#if defined(SCO) || defined(UNIXWARE)
) {
#endif
printf( "%s\n", VERSION );
exit( 0 );
}
/* check for illegal option */
else if ( c == '?' ) {
fprintf( stderr,
#if defined(LINUX)
"Try '%s --help' for more information.\n",
#endif
#if defined(SCO) || defined(UNIXWARE)
"Try '%s -h' for more information.\n",
#endif
argv0 );
exit( 1 );
}
/* nothing else left to do */
else {
fprintf( stderr, "%s: getopts returned %c\n",
argv0, c );
exit( 1 );
}
}
if ( optind != local_argc ) {
fprintf( stderr,
#if defined(LINUX)
"Try '%s --help' for more information.\n",
#endif
#if defined(SCO) || defined(UNIXWARE)
"Try '%s -h' for more information.\n",
#endif
argv0 );
exit( 1 );
}
}
/*
* generate_chars - generate random characters on stdout
*/
static void generate_chars( int num_groups, int groupchars, char *set ) {
int gcount; /* group counter */
int ccount; /* chars in a group counter */
/* randomize the rand() generator */
srand( (int)time( NULL ));
/* generate groups */
for ( gcount = 0; gcount < num_groups; gcount++ ) {
for ( ccount = 0; ccount < groupchars; ccount++ ) {
/* pick a 'random' character from the set */
printf( "%c", set[rand() % strlen( set )] );
fflush( stdout );
}
/* send a group separator space */
printf( " " );
fflush( stdout );
}
}
/*
* main - parse the command line options, then generate the characters
*/
int main( int argc, char **argv ) {
char *argv0; /* program name */
/* parse the command line arguments */
parse_cmdline( argc, argv );
/* set argv0 to be the basename part of the program name */
argv0 = argv[0] + strlen( argv[0] );
while ( *argv0 != ASC_FNS && argv0 > argv[0] )
argv0--;
if ( *argv0 == ASC_FNS ) argv0++;
/* randomize the rand() generator */
srand( (int)time( NULL ));
/* generate groups */
generate_chars( groups, groupsize, charset );
/* all done */
printf( "\n" );
exit( 0 );
return( 0 ); /* for LINT */
}
|