Commit c66162b4 authored by jani@hynda.(none)'s avatar jani@hynda.(none)

my_getopt:

- some minor bugfixes
- made code more readable
- support for printing GET_BOOL type options in my_print_variables(),
  value is either TRUE or FALSE
parent 475bc6c3
...@@ -125,7 +125,7 @@ static void die(const char* fmt, ...) ...@@ -125,7 +125,7 @@ static void die(const char* fmt, ...)
static void print_version() static void print_version()
{ {
printf("%s Ver 2.0 for %s at %s\n", my_progname, SYSTEM_TYPE, MACHINE_TYPE); printf("%s Ver 2.1 for %s at %s\n", my_progname, SYSTEM_TYPE, MACHINE_TYPE);
} }
...@@ -141,7 +141,6 @@ Dumps a MySQL binary log in a format usable for viewing or for pipeing to\n\ ...@@ -141,7 +141,6 @@ Dumps a MySQL binary log in a format usable for viewing or for pipeing to\n\
the mysql command line client\n\n"); the mysql command line client\n\n");
printf("Usage: %s [options] log-files\n", my_progname); printf("Usage: %s [options] log-files\n", my_progname);
my_print_help(my_long_options); my_print_help(my_long_options);
putchar('\n');
my_print_variables(my_long_options); my_print_variables(my_long_options);
} }
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#define MANAGER_PWGEN_VERSION "1.1" #define MANAGER_PWGEN_VERSION "1.2"
#include <my_global.h> #include <my_global.h>
#include <m_ctype.h> #include <m_ctype.h>
...@@ -72,7 +72,6 @@ void usage() ...@@ -72,7 +72,6 @@ void usage()
printf("Generates a password file to be used by mysqltest.\n\n"); printf("Generates a password file to be used by mysqltest.\n\n");
printf("Usage: %s [OPTIONS]\n", my_progname); printf("Usage: %s [OPTIONS]\n", my_progname);
my_print_help(my_long_options); my_print_help(my_long_options);
putchar('\n');
my_print_variables(my_long_options); my_print_variables(my_long_options);
} }
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#define MANAGER_CLIENT_VERSION "1.2" #define MANAGER_CLIENT_VERSION "1.3"
#include <my_global.h> #include <my_global.h>
#include <mysql.h> #include <mysql.h>
...@@ -93,7 +93,7 @@ void usage() ...@@ -93,7 +93,7 @@ void usage()
printf("Command-line client for MySQL manager daemon.\n\n"); printf("Command-line client for MySQL manager daemon.\n\n");
printf("Usage: %s [OPTIONS] < command_file\n", my_progname); printf("Usage: %s [OPTIONS] < command_file\n", my_progname);
my_print_help(my_long_options); my_print_help(my_long_options);
printf(" --no-defaults Don't read default options from any options file.\n\n"); printf(" --no-defaults Don't read default options from any options file.\n");
my_print_variables(my_long_options); my_print_variables(my_long_options);
} }
......
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
**********************************************************************/ **********************************************************************/
#define MTEST_VERSION "1.20" #define MTEST_VERSION "1.21"
#include <my_global.h> #include <my_global.h>
#include <mysql_embed.h> #include <mysql_embed.h>
...@@ -1873,7 +1873,7 @@ void usage() ...@@ -1873,7 +1873,7 @@ void usage()
printf("Runs a test against the mysql server and compares output with a results file.\n\n"); printf("Runs a test against the mysql server and compares output with a results file.\n\n");
printf("Usage: %s [OPTIONS] [database] < test_file\n", my_progname); printf("Usage: %s [OPTIONS] [database] < test_file\n", my_progname);
my_print_help(my_long_options); my_print_help(my_long_options);
printf(" --no-defaults Don't read default options from any options file.\n\n"); printf(" --no-defaults Don't read default options from any options file.\n");
my_print_variables(my_long_options); my_print_variables(my_long_options);
} }
......
...@@ -292,7 +292,7 @@ static struct my_option my_long_options[] = ...@@ -292,7 +292,7 @@ static struct my_option my_long_options[] =
static void print_version(void) static void print_version(void)
{ {
printf("%s Ver 2.4 for %s at %s\n", my_progname, SYSTEM_TYPE, printf("%s Ver 2.5 for %s at %s\n", my_progname, SYSTEM_TYPE,
MACHINE_TYPE); MACHINE_TYPE);
} }
...@@ -380,7 +380,6 @@ static void usage(void) ...@@ -380,7 +380,6 @@ static void usage(void)
(It may be VERY slow to do a sort the first time!)"); (It may be VERY slow to do a sort the first time!)");
print_defaults("my", load_default_groups); print_defaults("my", load_default_groups);
putchar('\n');
my_print_variables(my_long_options); my_print_variables(my_long_options);
} }
......
...@@ -283,7 +283,7 @@ int handle_options(int *argc, char ***argv, ...@@ -283,7 +283,7 @@ int handle_options(int *argc, char ***argv,
*/ */
*((my_bool*) optp->value)= (my_bool) (!optend || *optend == '1'); *((my_bool*) optp->value)= (my_bool) (!optend || *optend == '1');
(*argc)--; (*argc)--;
continue; continue; // For GET_BOOL get_one_option() shouldn't be called
} }
argument= optend; argument= optend;
} }
...@@ -330,7 +330,7 @@ int handle_options(int *argc, char ***argv, ...@@ -330,7 +330,7 @@ int handle_options(int *argc, char ***argv,
{ {
*((my_bool*) optp->value)= (my_bool) 1; *((my_bool*) optp->value)= (my_bool) 1;
(*argc)--; (*argc)--;
continue; continue; // For GET_BOOL get_one_option() shouldn't be called
} }
else if (optp->arg_type == REQUIRED_ARG || else if (optp->arg_type == REQUIRED_ARG ||
optp->arg_type == OPT_ARG) optp->arg_type == OPT_ARG)
...@@ -414,23 +414,33 @@ static int setval (const struct my_option *opts, char *argument, ...@@ -414,23 +414,33 @@ static int setval (const struct my_option *opts, char *argument,
if (!result_pos) if (!result_pos)
return ERR_NO_PTR_TO_VARIABLE; return ERR_NO_PTR_TO_VARIABLE;
if (opts->var_type == GET_INT || opts->var_type == GET_UINT) switch (opts->var_type) {
case GET_INT:
case GET_UINT: /* fall through */
*((int*) result_pos)= (int) getopt_ll(argument, opts, &err); *((int*) result_pos)= (int) getopt_ll(argument, opts, &err);
else if (opts->var_type == GET_LONG || opts->var_type == GET_ULONG) break;
case GET_LONG:
case GET_ULONG: /* fall through */
*((long*) result_pos)= (long) getopt_ll(argument, opts, &err); *((long*) result_pos)= (long) getopt_ll(argument, opts, &err);
else if (opts->var_type == GET_LL) break;
case GET_LL:
*((longlong*) result_pos)= getopt_ll(argument, opts, &err); *((longlong*) result_pos)= getopt_ll(argument, opts, &err);
else if (opts->var_type == GET_ULL) break;
case GET_ULL:
*((ulonglong*) result_pos)= getopt_ull(argument, opts, &err); *((ulonglong*) result_pos)= getopt_ull(argument, opts, &err);
else if (opts->var_type == GET_STR) break;
case GET_STR:
*((char**) result_pos)= argument; *((char**) result_pos)= argument;
else if (opts->var_type == GET_STR_ALLOC) break;
{ case GET_STR_ALLOC:
if ((*((char**) result_pos))) if ((*((char**) result_pos)))
my_free((*(char**) result_pos), my_free((*(char**) result_pos),
MYF(MY_WME | MY_FAE)); MYF(MY_WME | MY_FAE));
if (!(*((char**) result_pos)= my_strdup(argument, MYF(MY_WME)))) if (!(*((char**) result_pos)= my_strdup(argument, MYF(MY_WME))))
return ERR_OUT_OF_MEMORY; return ERR_OUT_OF_MEMORY;
break;
default: /* dummy default to avoid compiler warnings */
break;
} }
if (err) if (err)
return ERR_UNKNOWN_SUFFIX; return ERR_UNKNOWN_SUFFIX;
...@@ -587,27 +597,38 @@ static void init_variables(const struct my_option *options) ...@@ -587,27 +597,38 @@ static void init_variables(const struct my_option *options)
{ {
if (options->value) if (options->value)
{ {
if (options->var_type == GET_INT) switch (options->var_type) {
case GET_BOOL:
*((my_bool*) options->u_max_value)= *((my_bool*) options->value)=
(my_bool) options->def_value;
break;
case GET_INT:
*((int*) options->u_max_value)= *((int*) options->value)= *((int*) options->u_max_value)= *((int*) options->value)=
(int) options->def_value; (int) options->def_value;
else if (options->var_type == GET_UINT) break;
case GET_UINT:
*((uint*) options->u_max_value)= *((uint*) options->value)= *((uint*) options->u_max_value)= *((uint*) options->value)=
(uint) options->def_value; (uint) options->def_value;
else if (options->var_type == GET_BOOL) break;
*((my_bool*) options->u_max_value)= *((my_bool*) options->value)= case GET_LONG:
(my_bool) options->def_value;
else if (options->var_type == GET_LONG)
*((long*) options->u_max_value)= *((long*) options->value)= *((long*) options->u_max_value)= *((long*) options->value)=
(long) options->def_value; (long) options->def_value;
else if (options->var_type == GET_ULONG) break;
case GET_ULONG:
*((ulong*) options->u_max_value)= *((ulong*) options->value)= *((ulong*) options->u_max_value)= *((ulong*) options->value)=
(ulong) options->def_value; (ulong) options->def_value;
else if (options->var_type == GET_LL) break;
case GET_LL:
*((longlong*) options->u_max_value)= *((longlong*) options->value)= *((longlong*) options->u_max_value)= *((longlong*) options->value)=
(longlong) options->def_value; (longlong) options->def_value;
else if (options->var_type == GET_ULL) break;
case GET_ULL:
*((ulonglong*) options->u_max_value)= *((ulonglong*) options->value)= *((ulonglong*) options->u_max_value)= *((ulonglong*) options->value)=
(ulonglong) options->def_value; (ulonglong) options->def_value;
break;
default: /* dummy default to avoid compiler warnings */
break;
}
} }
} }
} }
...@@ -696,8 +717,9 @@ void my_print_variables(const struct my_option *options) ...@@ -696,8 +717,9 @@ void my_print_variables(const struct my_option *options)
char buff[255]; char buff[255];
const struct my_option *optp; const struct my_option *optp;
printf("Variables (--variable-name=value) Default value\n"); printf("\nVariables (--variable-name=value)\n");
printf("--------------------------------- -------------\n"); printf("and boolean options {FALSE|TRUE} Value (after reading options)\n");
printf("--------------------------------- -----------------------------\n");
for (optp= options; optp->id; optp++) for (optp= options; optp->id; optp++)
{ {
if (optp->value) if (optp->value)
...@@ -706,64 +728,36 @@ void my_print_variables(const struct my_option *options) ...@@ -706,64 +728,36 @@ void my_print_variables(const struct my_option *options)
length= strlen(optp->name); length= strlen(optp->name);
for (; length < name_space; length++) for (; length < name_space; length++)
putchar(' '); putchar(' ');
if (optp->var_type == GET_STR || optp->var_type == GET_STR_ALLOC) switch (optp->var_type) {
{ case GET_STR:
if (*((char**) optp->value)) case GET_STR_ALLOC: /* fall through */
printf("%s\n", *((char**) optp->value)); printf("%s\n", *((char**) optp->value) ? *((char**) optp->value) :
else "(No default value)");
printf("(No default value)\n"); break;
} case GET_BOOL:
else if (optp->var_type == GET_BOOL) printf("%s\n", *((my_bool*) optp->value) ? "TRUE" : "FALSE");
{ break;
if (!optp->def_value && !*((my_bool*) optp->value)) case GET_INT:
printf("(No default value)\n"); printf("%d\n", *((int*) optp->value));
else break;
printf("%d\n", *((my_bool*) optp->value)); case GET_UINT:
} printf("%d\n", *((uint*) optp->value));
else if (optp->var_type == GET_INT) break;
{ case GET_LONG:
if (!optp->def_value && !*((int*) optp->value)) printf("%lu\n", *((long*) optp->value));
printf("(No default value)\n"); break;
else case GET_ULONG:
printf("%d\n", *((int*) optp->value)); printf("%lu\n", *((ulong*) optp->value));
} break;
else if (optp->var_type == GET_UINT) case GET_LL:
{ printf("%s\n", llstr(*((longlong*) optp->value), buff));
if (!optp->def_value && !*((uint*) optp->value)) break;
printf("(No default value)\n"); case GET_ULL:
else longlong2str(*((ulonglong*) optp->value), buff, 10);
printf("%d\n", *((uint*) optp->value)); printf("%s\n", buff);
} break;
else if (optp->var_type == GET_LONG) default: /* dummy default to avoid compiler warnings */
{ break;
if (!optp->def_value && !*((long*) optp->value))
printf("(No default value)\n");
else
printf("%lu\n", *((long*) optp->value));
}
else if (optp->var_type == GET_ULONG)
{
if (!optp->def_value && !*((ulong*) optp->value))
printf("(No default value)\n");
else
printf("%lu\n", *((ulong*) optp->value));
}
else if (optp->var_type == GET_LL)
{
if (!optp->def_value && !*((longlong*) optp->value))
printf("(No default value)\n");
else
printf("%s\n", llstr(*((longlong*) optp->value), buff));
}
else if (optp->var_type == GET_ULL)
{
if (!optp->def_value && !*((ulonglong*) optp->value))
printf("(No default value)\n");
else
{
longlong2str(*((ulonglong*) optp->value), buff, 10);
printf("%s\n", buff);
}
} }
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment