/*
This file is part of GNUnet
Copyright (C) 2006, 2011 GNUnet e.V.
GNUnet 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 3, or (at your
option) any later version.
GNUnet 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.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
/**
* @file src/util/getopt_helpers.c
* @brief implements command line that sets option
* @author Christian Grothoff
*/
#include "platform.h"
#include "gnunet_util_lib.h"
#define LOG(kind,...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__)
/**
* Print out program version (implements --version).
*
* @param ctx command line processing context
* @param scls additional closure (points to version string)
* @param option name of the option
* @param value not used (NULL)
* @return #GNUNET_NO (do not continue, not an error)
*/
static int
print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
void *scls,
const char *option,
const char *value)
{
const char *version = scls;
(void) option;
(void) value;
printf ("%s v%s\n",
ctx->binaryName,
version);
return GNUNET_NO;
}
/**
* Define the option to print the version of
* the application (-v option)
*
* @param version string with the version number
*/
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_version (const char *version)
{
struct GNUNET_GETOPT_CommandLineOption clo = {
.shortName = 'v',
.name = "version",
.description = gettext_noop("print the version number"),
.processor = &print_version,
.scls = (void *) version
};
return clo;
}
/**
* At what offset does the help text start?
*/
#define BORDER 29
/**
* Print out details on command line options (implements --help).
*
* @param ctx command line processing context
* @param scls additional closure (points to about text)
* @param option name of the option
* @param value not used (NULL)
* @return #GNUNET_NO (do not continue, not an error)
*/
static int
format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
void *scls,
const char *option,
const char *value)
{
const char *about = scls;
size_t slen;
unsigned int i;
int j;
size_t ml;
size_t p;
char *scp;
const char *trans;
const struct GNUNET_GETOPT_CommandLineOption *opt;
const struct GNUNET_OS_ProjectData *pd;
(void) option;
(void) value;
if (NULL != about)
{
printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
printf (_
("Arguments mandatory for long options are also mandatory for short options.\n"));
}
i = 0;
opt = ctx->allOptions;
while (NULL != opt[i].description)
{
if (opt[i].shortName == '\0')
printf (" ");
else
printf (" -%c, ", opt[i].shortName);
printf ("--%s", opt[i].name);
slen = 8 + strlen (opt[i].name);
if (NULL != opt[i].argumentHelp)
{
printf ("=%s", opt[i].argumentHelp);
slen += 1 + strlen (opt[i].argumentHelp);
}
if (slen > BORDER)
{
printf ("\n%*s", BORDER, "");
slen = BORDER;
}
if (slen < BORDER)
{
printf ("%*s", (int) (BORDER - slen), "");
slen = BORDER;
}
if (0 < strlen (opt[i].description))
trans = gettext (opt[i].description);
else
trans = "";
ml = strlen (trans);
p = 0;
OUTER:
while (ml - p > 78 - slen)
{
for (j = p + 78 - slen; j > (int) p; j--)
{
if (isspace ((unsigned char) trans[j]))
{
scp = GNUNET_malloc (j - p + 1);
GNUNET_memcpy (scp, &trans[p], j - p);
scp[j - p] = '\0';
printf ("%s\n%*s", scp, BORDER + 2, "");
GNUNET_free (scp);