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
|
/*
* util - Utility function library
*
* Print standard program messages
*
* Copyright IBM Corp. 2016, 2017
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <errno.h>
#include <stdio.h>
#include "lib/util_base.h"
#include "lib/util_prg.h"
#include "lib/zt_common.h"
/*
* Private data
*/
static struct util_prg_l {
const struct util_prg *prg;
/* Command used for parsing */
const char *command;
} l;
struct util_prg_l *util_prg_l = &l;
/**
* Set the current command for command line option processing
*
* @param[in] command The current command or NULL for no command
*/
void util_prg_set_command(const char *command)
{
l.command = command;
}
/**
* Print program usage information for the --help option
*/
void util_prg_print_help(void)
{
/* Print usage */
printf("Usage: %s", program_invocation_short_name);
if (l.prg->command_args)
printf(" %s", l.prg->command_args);
printf(" [OPTIONS]");
if (l.prg->args)
printf(" %s", l.prg->args);
/* Print usage description */
printf("\n\n");
util_print_indented(l.prg->desc, 0);
printf("\n");
}
/**
* Print program version information for the --version option
*/
void util_prg_print_version(void)
{
const struct util_prg_copyright *copyright;
printf("%s version %s\n", program_invocation_short_name,
RELEASE_STRING);
copyright = l.prg->copyright_vec;
while (copyright->owner) {
if (copyright->pub_first == copyright->pub_last)
printf("Copyright %s %d\n", copyright->owner,
copyright->pub_first);
else
printf("Copyright %s %d, %d\n", copyright->owner,
copyright->pub_first, copyright->pub_last);
copyright++;
}
}
/*
* Ask user to use the --help option
*/
void util_prg_print_parse_error(void)
{
if (l.command)
fprintf(stderr, "Try '%s %s --help' for more information.\n",
program_invocation_short_name, l.command);
else
fprintf(stderr, "Try '%s --help' for more information.\n",
program_invocation_short_name);
}
/**
* An option has been specified that is not supported
*
* @param[in] option Option string (short or long)
*/
void util_prg_print_invalid_option(const char *opt_name)
{
fprintf(stderr, "%s: Invalid option '%s'\n",
program_invocation_short_name, opt_name);
util_prg_print_parse_error();
}
/**
* A required argument for an option is missing
*
* @param[in] option Option string
*/
void util_prg_print_required_arg(const char *opt_name)
{
fprintf(stderr, "%s: Option '%s' requires an argument\n",
program_invocation_short_name, opt_name);
util_prg_print_parse_error();
}
/**
* A superfluous invalid positional argument has been specified
*
* @param[in] arg_name Name of the invalid argument
*/
void util_prg_print_arg_error(const char *arg_name)
{
fprintf(stderr, "%s: Invalid argument '%s'\n",
program_invocation_short_name, arg_name);
util_prg_print_parse_error();
}
/**
* Initialize the program module
*
* @param[in] prg Program description
*/
void util_prg_init(const struct util_prg *prg)
{
l.prg = prg;
}
|