home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume6 / copt / d_optable.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-07  |  4.0 KB  |  146 lines

  1. static char rcsid[] = "$Header: d_optable.c,v 1.1 89/03/03 12:53:23 np Exp $";
  2. /* $Log:    d_optable.c,v $
  3.  * Revision 1.1  89/03/03  12:53:23  np
  4.  * Initial revision
  5.  * 
  6.  */
  7.  
  8. /*
  9.  * Copyright (C) 1985-1989 Nigel Perry
  10.  *
  11.  * This program is distributed without any warranty.
  12.  * The author and and distributors accept no resposibility
  13.  * to anyone for the consequence of using this program or
  14.  * whether it serves any particular purpose or works at all,
  15.  * unless they say so in writing.
  16.  *
  17.  * Everyone is granted permission to copy, modify and
  18.  * redistribute this program, but only provided that:
  19.  *
  20.  * 1) They do so without financial or material gain.
  21.  *
  22.  * 2) The copyright notice and this notice are preserved on
  23.  *    all copies.
  24.  *
  25.  * 3) The original source is preserved in any redistribution.
  26.  *
  27.  * I hope you find this program useful!
  28.  *
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include "cset.h"
  33. #include "cset.d"
  34.  
  35. #define next continue
  36.  
  37. /* Pretty print an _optable - or try to, boring ol' stdio!
  38.  
  39.    Adapted from d.optab - written in the REAL language B Plus!
  40.  
  41.    args:
  42.       withelp - true is want long form with short desc
  43.       table   - the table to be printed, 0 => _optable
  44.       hidden  - if true, the 'hidden' options are also printed
  45.  */
  46.  
  47. d_optable(withelp, table, hidden) int withelp, hidden; _opt_entry *table;
  48. {  extern _opt_entry _optable[];
  49.    _opt_entry *p;
  50.    int c, n, flip, column;
  51.    char *left, *right;
  52.  
  53.    /* set default table */
  54.    if( table == (_opt_entry *)0 ) table = _optable;
  55.  
  56.    p = table;
  57.    flip = c = n = 0;
  58.    /* count command and option names */
  59.    while(p->_opt_pat) if((p++)->_opt_types == COMM_KWD) c++; else n++;
  60.    if(c > 1) /* only list command names if > 1 */
  61.    {  fputs("Command Names:", stderr);
  62.       p = table;
  63.       while(p->_opt_pat)
  64.       {  if(p->_opt_types == COMM_KWD)
  65.      {  if(withelp)
  66.         {  fprintf(stderr, "\n%*s%s%*s%s", TAB1, "", p->_opt_pat,
  67.           TAB3 - (strlen(p->_opt_pat) + TAB1), "",
  68.           p->_opt_help == NOHELP ? "(see manual)" : p->_opt_help);
  69.         }
  70.         else
  71.        {  fprintf(stderr, "%sc%*s%s", flip ? (char *)0 : "\n",
  72.           flip ? (TAB2 - column) : TAB1, "", p->_opt_pat);
  73.            if(flip = !flip) column = TAB1 + strlen(p->_opt_pat);
  74.         }
  75.      }
  76.      p++;
  77.       }
  78.       fputc('\n', stderr);
  79.       if(!n) return;
  80.    }
  81.    fputs("Options:", stderr);
  82.    flip = 0;
  83.    p = table; p--;
  84.    while( (++p)->_opt_pat )
  85.    {  if(p->_opt_types == COMM_KWD) next;
  86.       /* if HIDE and not hidden don't print it! */
  87.       if( (p->_opt_types & HIDE_KWD) && !hidden) next;
  88.       _opt_adorn(p->_opt_types, &left, &right);
  89.       if(withelp)
  90.       {  fprintf( stderr, "\n%*s%s%s%s%*s%s", TAB1, "", left, p->_opt_pat,
  91.           right,
  92.           TAB3 - (TAB1+strlen(left)+strlen(p->_opt_pat)+strlen(right)),
  93.           "", p->_opt_help == NOHELP ? "(see manual)" : p->_opt_help);
  94.       }
  95.       else
  96.       {  fprintf( stderr, "%s%*s", flip ? "" : "\n",
  97.           flip ? (TAB2 - column) : TAB1, "");
  98.      flip = !flip;
  99.      fprintf(stderr, "%s%s%s", left, p->_opt_pat, right);
  100.      if(flip)
  101.         column = TAB1 + strlen(left) + strlen(p->_opt_pat) + strlen(right);
  102.       }
  103.    }
  104.    fputc('\n', stderr);
  105. }
  106.  
  107. /* return strings to 'adorn' options */
  108. _opt_adorn(p, left, right) char **left, **right;
  109. {  static char *dpb[] = { (char *)0, "", "+", "[+]", "-", "[-]",
  110.               "(+|-)", "[+|-]" };
  111.    static char *s_val = { "=X" };
  112.    static char *m_val = { "=X,...,X" };
  113.    int c;
  114.    if(p == COMM_KWD)
  115.       *left = *right = "";
  116.    else if(c = p & (DASH_KWD | PLUS_KWD | BLNK_KWD))
  117.    {  *left = dpb[c >> 8];
  118.       *right = "";
  119.    }
  120.    else if(p  == SVAL_KWD)
  121.    {  *left = "";
  122.       s_val[1] = 's'; /* construct '=s' */
  123.       *right = s_val;
  124.    }
  125.    else /* numeric */
  126.    {  switch(p & 0xFF) /* find base */
  127.       {  case 0: c = 'n'; break;
  128.      case 8: c = 'o'; break;
  129.      case 10: c = 'd'; break;
  130.      case 16: c = 'x'; break;
  131.      default: /* somebody is being ackward... */
  132.         c = p & 0xFF;
  133.         c += c < 10 ? '0' : ('A' - 10);
  134.       }
  135.       *left = "";
  136.       if(p & MVAL_BIT)
  137.       {  m_val[1] = m_val[7] = c;
  138.      *right = m_val;
  139.       }
  140.       else
  141.       {  s_val[1] = c;
  142.      *right = s_val;
  143.       }
  144.    }
  145. }
  146.