help.c 12.9 KB
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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
/* help.c -- general-purpose command line option parser
   Copyright (C) 2016 Free Software Foundation, Inc.

   GNU Mailutils 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.

   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <unistd.h>
#include <mailutils/alloc.h>
#include <mailutils/opt.h>
#include <mailutils/cctype.h>
#include <mailutils/nls.h>
#include <mailutils/wordsplit.h>
#include <mailutils/stream.h>

unsigned short_opt_col = 2;
unsigned long_opt_col = 6;
/*FIXME: doc_opt_col? */
unsigned header_col = 1;
unsigned opt_doc_col = 29;
unsigned usage_indent = 12;
unsigned rmargin = 79;

unsigned dup_args = 0;
unsigned dup_args_note = 1;

enum usage_var_type
  {
    usage_var_column,
    usage_var_bool
  };

static struct usage_var
{
  char *name;
  unsigned *valptr;
  enum usage_var_type type;
} usage_var[] = {
  { "short-opt-col", &short_opt_col, usage_var_column },
  { "header-col",    &header_col,    usage_var_column },
  { "opt-doc-col",   &opt_doc_col,   usage_var_column },
  { "usage-indent",  &usage_indent,  usage_var_column },
  { "rmargin",       &rmargin,       usage_var_column },
  { "dup-args",      &dup_args,      usage_var_bool },
  { "dup-args-note", &dup_args_note, usage_var_bool },
  { "long-opt-col",  &long_opt_col,  usage_var_column },
  { "doc_opt_col",   NULL,           usage_var_column },
  { NULL }
};

unsigned
mu_parseopt_getcolumn (const char *name)
{
  struct usage_var *p;
  unsigned retval = 0;
  for (p = usage_var; p->name; p++)
    {
      if (strcmp (p->name, name) == 0)
	{
	  if (p->valptr)
	    retval = *p->valptr;
	  break;
	}
    }
  return retval;
}

static void
set_usage_var (struct mu_parseopt *po, char const *id)
{
  struct usage_var *p;
  size_t len;
  int boolval = 1;
  
  if (strlen (id) > 3 && memcmp (id, "no-", 3) == 0)
    {
      id += 3;
      boolval = 0;
    }
  len = strcspn (id, "=");

  for (p = usage_var; p->name; p++)
    {
      if (strlen (p->name) == len && memcmp (p->name, id, len) == 0)
	{
	  if (!p->valptr)
	    return;

	  if (p->type == usage_var_bool)
	    {
	      if (id[len])
		{
		  if (po->po_prog_name)
		    fprintf (stderr, "%s: ", po->po_prog_name);
		  fprintf (stderr,
			   "error in ARGP_HELP_FMT: improper usage of [no-]%s\n",
			   id);
		  return;
		}
	      *p->valptr = boolval;
	      return;
	    }
	  
	  if (id[len])
	    {
	      char *endp;
	      unsigned long val;
	      
	      errno = 0;
	      val = strtoul (id + len + 1, &endp, 10);
	      if (errno || *endp)
		{
		  if (po->po_prog_name)
		    fprintf (stderr, "%s: ", po->po_prog_name);
		  fprintf (stderr,
			   "error in ARGP_HELP_FMT: bad value for %s\n",
			   id);
		}
	      else if (val > UINT_MAX)
		{
		  if (po->po_prog_name)
		    fprintf (stderr, "%s: ", po->po_prog_name);
		  fprintf (stderr,
			   "error in ARGP_HELP_FMT: %s value is out of range\n",
			   id);
		}
	      else
		*p->valptr = val;
	    }
	  else
	    {
	      if (po->po_prog_name)
		fprintf (stderr, "%s: ", po->po_prog_name);
	      fprintf (stderr,
		       "%s: ARGP_HELP_FMT parameter requires a value\n",
		       id);
	      return;
	    }
	  return;
	}
    }

  if (po->po_prog_name)
    fprintf (stderr, "%s: ", po->po_prog_name);
  fprintf (stderr,
	   "%s: Unknown ARGP_HELP_FMT parameter\n",
	   id);
}

static void
init_usage_vars (struct mu_parseopt *po)
{
  char *fmt;
  struct mu_wordsplit ws;
  size_t i;
  
  fmt = getenv ("ARGP_HELP_FMT");
  if (!fmt)
    return;
  ws.ws_delim = ",";
  if (mu_wordsplit (fmt, &ws, 
		    MU_WRDSF_DELIM | MU_WRDSF_NOVAR | MU_WRDSF_NOCMD
		    | MU_WRDSF_WS | MU_WRDSF_SHOWERR))
    return;
  for (i = 0; i < ws.ws_wordc; i++)
    {
      set_usage_var (po, ws.ws_wordv[i]);
    }

  mu_wordsplit_free (&ws);
}

static void
set_margin (mu_stream_t str, unsigned margin)
{
  mu_stream_ioctl (str, MU_IOCTL_WORDWRAPSTREAM,
		   MU_IOCTL_WORDWRAP_SET_MARGIN,
		   &margin);
}
static void
set_next_margin (mu_stream_t str, unsigned margin)
{
  mu_stream_ioctl (str, MU_IOCTL_WORDWRAPSTREAM,
		   MU_IOCTL_WORDWRAP_SET_NEXT_MARGIN,
		   &margin);
}
static void
get_offset (mu_stream_t str, unsigned *offset)
{
  mu_stream_ioctl (str, MU_IOCTL_WORDWRAPSTREAM,
		   MU_IOCTL_WORDWRAP_GET_COLUMN,
		   offset);
}

static void
print_opt_arg (mu_stream_t str, struct mu_option *opt, int delim)
{
  if (opt->opt_flags & MU_OPTION_ARG_OPTIONAL)
    {
      if (delim == '=')
	mu_stream_printf (str, "[=%s]", gettext (opt->opt_arg));
      else
	mu_stream_printf (str, "[%s]", gettext (opt->opt_arg));
    }
  else
    mu_stream_printf (str, "%c%s", delim, gettext (opt->opt_arg));
}

static size_t
print_option (mu_stream_t str,
	      struct mu_option **optbuf, size_t optcnt, size_t num,
	      int *argsused)
{
  struct mu_option *opt = optbuf[num];
  size_t next, i;
  int delim;
  int first_option = 1;
  int first_long_option = 1;
  
  if (MU_OPTION_IS_GROUP_HEADER (opt))
    {
      if (num)
	mu_stream_printf (str, "\n");
      if (opt->opt_doc[0])
	{
	  set_margin (str, header_col);
	  mu_stream_printf (str, "%s", gettext (opt->opt_doc));
	}
      return num + 1;
    }

  /* count aliases */
  for (next = num + 1;
       next < optcnt && optbuf[next]->opt_flags & MU_OPTION_ALIAS;
       next++);

  if (opt->opt_flags & MU_OPTION_HIDDEN)
    return next;

  set_margin (str, short_opt_col); 
  for (i = num; i < next; i++)
    {
      if (MU_OPTION_IS_VALID_SHORT_OPTION (optbuf[i]))
	{
	  if (first_option)
	    first_option = 0;
	  else
	    mu_stream_printf (str, ", ");
	  mu_stream_printf (str, "-%c", optbuf[i]->opt_short);
	  delim = ' ';
	  if (opt->opt_arg && dup_args)
	    print_opt_arg (str, opt, delim);
	}
    }

  for (i = num; i < next; i++)
    {
      if (MU_OPTION_IS_VALID_LONG_OPTION (optbuf[i]))
	{
	  if (first_option)
	    first_option = 0;
	  else
	    mu_stream_printf (str, ", ");

	  if (first_long_option)
	    {
	      unsigned off;
	      get_offset (str, &off);
	      if (off < long_opt_col)
		set_margin (str, long_opt_col);
	      first_long_option = 0;
	    }
	  
  	  mu_stream_printf (str, "--%s", optbuf[i]->opt_long);
	  delim = '=';
	  if (opt->opt_arg && dup_args)
	    print_opt_arg (str, opt, delim);
	}
    }
  
  if (opt->opt_arg)
    {
      *argsused = 1;
      if (!dup_args)
	print_opt_arg (str, opt, delim);
    }

  set_margin (str, opt_doc_col);
  mu_stream_printf (str, "%s\n", gettext (opt->opt_doc));

  return next;
}

void
mu_option_describe_options (mu_stream_t str,
			    struct mu_option **optbuf, size_t optcnt)
{
  unsigned i;
  int argsused = 0;

  for (i = 0; i < optcnt; )
    i = print_option (str, optbuf, optcnt, i, &argsused);
  mu_stream_printf (str, "\n");

  if (argsused && dup_args_note)
    {
      set_margin (str, 0);
      mu_stream_printf (str, "%s\n\n",
			_("Mandatory or optional arguments to long options are also mandatory or optional for any corresponding short options."));
    }
}

static void print_program_usage (struct mu_parseopt *po, int optsum,
				 mu_stream_t str);

void
mu_program_help (struct mu_parseopt *po, mu_stream_t outstr)
{
  mu_stream_t str;
  int rc;
  
  init_usage_vars (po);

  rc = mu_wordwrap_stream_create (&str, outstr, 0, rmargin);
  if (rc)
    {
      abort ();//FIXME
    }

  print_program_usage (po, 0, str);
  
  if (po->po_prog_doc)
    {
      set_margin (str, 0);
      mu_stream_printf (str, "%s\n", gettext (po->po_prog_doc));
    }
  mu_stream_printf (str, "\n");
  
  mu_option_describe_options (str, po->po_optv, po->po_optc);

  if (po->po_help_hook)
    {
      po->po_help_hook (po, str);
      mu_stream_printf (str, "\n");
    }
  
  set_margin (str, 0);
  if (po->po_bug_address)
    /* TRANSLATORS: The placeholder indicates the bug-reporting address
       for this package.  Please add _another line_ saying
       "Report translation bugs to <...>\n" with the address for translation
       bugs (typically your translation team's web or email address).  */
    mu_stream_printf (str, _("Report bugs to <%s>.\n"), po->po_bug_address);

  if (po->po_package_name && po->po_package_url)
    mu_stream_printf (str, _("%s home page: <%s>\n"),
		      po->po_package_name, po->po_package_url);
  if (po->po_flags & MU_PARSEOPT_EXTRA_INFO)
    mu_stream_printf (str, "%s\n", _(po->po_extra_info));

  mu_stream_destroy (&str);
}

static struct mu_option **option_tab;

static int
cmpidx_short (const void *a, const void *b)
{
  unsigned const *ai = (unsigned const *)a;
  unsigned const *bi = (unsigned const *)b;
  int ac = option_tab[*ai]->opt_short;
  int bc = option_tab[*bi]->opt_short;
  int d;
  
  if (mu_isalpha (ac))
    {
      if (!mu_isalpha (bc))
	return -1;
    }
  else if (mu_isalpha (bc))
    return 1;

  d = mu_tolower (ac) - mu_tolower (bc);
  if (d == 0)
    d =  mu_isupper (ac) ? 1 : -1;
  return d;
}
  
static int
cmpidx_long (const void *a, const void *b)
{
  unsigned const *ai = (unsigned const *)a;
  unsigned const *bi = (unsigned const *)b;
  struct mu_option const *ap = option_tab[*ai];
  struct mu_option const *bp = option_tab[*bi];
  return strcmp (ap->opt_long, bp->opt_long);
}

static void
option_summary (struct mu_parseopt *po, mu_stream_t str)
{
  unsigned i;
  unsigned *idxbuf;
  unsigned nidx;

  struct mu_option **optbuf = po->po_optv;
  size_t optcnt = po->po_optc;
  
  option_tab = optbuf;
  
  idxbuf = mu_calloc (optcnt, sizeof (idxbuf[0]));

  /* Print a list of short options without arguments. */
  for (i = nidx = 0; i < optcnt; i++)
    if (MU_OPTION_IS_VALID_SHORT_OPTION (optbuf[i]) && !optbuf[i]->opt_arg)
      idxbuf[nidx++] = i;

  if (nidx)
    {
      qsort (idxbuf, nidx, sizeof (idxbuf[0]), cmpidx_short);
      mu_stream_printf (str, "[-");
      for (i = 0; i < nidx; i++)
	{
	  mu_stream_printf (str, "%c", optbuf[idxbuf[i]]->opt_short);
	}
      mu_stream_printf (str, "%c", ']');
    }

  /* Print a list of short options with arguments. */
  for (i = nidx = 0; i < optcnt; i++)
    {
      if (MU_OPTION_IS_VALID_SHORT_OPTION (optbuf[i]) && optbuf[i]->opt_arg)
	idxbuf[nidx++] = i;
    }

  if (nidx)
    {
      qsort (idxbuf, nidx, sizeof (idxbuf[0]), cmpidx_short);
    
      for (i = 0; i < nidx; i++)
	{
	  struct mu_option *opt = optbuf[idxbuf[i]];
	  const char *arg = gettext (opt->opt_arg);
	  if (opt->opt_flags & MU_OPTION_ARG_OPTIONAL)
	    mu_stream_printf (str, " [-%c[%s]]", opt->opt_short, arg);
	  else
	    mu_stream_printf (str, " [-%c %s]", opt->opt_short, arg);
	}
    }
  
  /* Print a list of long options */
  for (i = nidx = 0; i < optcnt; i++)
    {
      if (MU_OPTION_IS_VALID_LONG_OPTION (optbuf[i]))
	idxbuf[nidx++] = i;
    }

  if (nidx)
    {
      qsort (idxbuf, nidx, sizeof (idxbuf[0]), cmpidx_long);
	
      for (i = 0; i < nidx; i++)
	{
	  struct mu_option *opt = optbuf[idxbuf[i]];
	  const char *arg = opt->opt_arg ? gettext (opt->opt_arg) : NULL;

	  mu_stream_printf (str, " [--%s", opt->opt_long);
	  if (opt->opt_arg)
	    {
	      if (opt->opt_flags & MU_OPTION_ARG_OPTIONAL)
		mu_stream_printf (str, "[=%s]", arg);
	      else
		mu_stream_printf (str, "=%s", arg);
	    }
	  mu_stream_printf (str, "%c", ']');
	}
    }

  free (idxbuf);
}  

static void
print_program_usage (struct mu_parseopt *po, int optsum, mu_stream_t str)
{
  char const *usage_text;
  char const **arg_text;
  size_t i;
  
  usage_text = _("Usage:");

  if (po->po_flags & MU_PARSEOPT_PROG_ARGS)
    arg_text = po->po_prog_args;
  else
    arg_text = NULL;
  i = 0;
  
  do
    {
      mu_stream_printf (str, "%s %s ", usage_text, po->po_prog_name);
      set_next_margin (str, usage_indent);

      if (optsum)
	{
	  option_summary (po, str);
	  optsum = 0;
	}
      else
	mu_stream_printf (str, "[%s...]", _("OPTION"));

      if (arg_text)
	{
	  mu_stream_printf (str, " %s\n", gettext (arg_text[i]));
	  if (i == 0)
	    usage_text = _("or: ");
	  set_margin (str, 2);
	  i++;
	}
      else
	mu_stream_flush (str);
    }
  while (arg_text && arg_text[i]);
}

void
mu_program_usage (struct mu_parseopt *po, int optsum, mu_stream_t outstr)
{
  int rc;
  mu_stream_t str;
  
  init_usage_vars (po);

  rc = mu_wordwrap_stream_create (&str, outstr, 0, rmargin);
  if (rc)
    {
      abort ();//FIXME
    }  
  print_program_usage (po, optsum, str);
  mu_stream_destroy (&str);
}

void
mu_program_version (struct mu_parseopt *po, mu_stream_t outstr)
{
  int rc;
  mu_stream_t str;
  
  init_usage_vars (po);

  rc = mu_wordwrap_stream_create (&str, outstr, 0, rmargin);
  if (rc)
    {
      abort ();//FIXME
    }  
  po->po_version_hook (po, str);

  mu_stream_destroy (&str);
}