args.c

Go to the documentation of this file.
00001 
00021 /*
00022  * Argument scanning and profile reading code.  Default parameters are set
00023  * here as well.
00024  */
00025 
00026 #define PUBLIC extern
00027 #include <sys/types.h>
00028 #include <ctype.h>
00029 #include <string.h>
00030 #include <stdlib.h>
00031 #include "globs.h"
00032 #include "proto.h"
00033 
00034 /* profile types */
00035 #define PRO_SPECIAL     1               /* special case */
00036 #define PRO_BOOL        2               /* boolean */
00037 #define PRO_INT         3               /* integer */
00038 #define PRO_FONT        4               /* troff font */
00039 
00040 /* profile specials for booleans */
00041 #define ON              1               /* turn it on */
00042 #define OFF             0               /* turn it off */
00043 
00044 /* profile specials for specials */
00045 #define IGN             1               /* ignore it */
00046 #define CLI             2               /* case label indent (float) */
00047 #define STDIN           3               /* use stdin */
00048 #define KEY             4               /* type (keyword) */
00049 
00050 /*
00051  * N.B.: because of the way the table here is scanned, options whose names
00052  * are substrings of other options must occur later; that is, with -lp vs -l,
00053  * -lp must be first.  Also, while (most) booleans occur more than once, the
00054  * last default value is the one actually assigned.
00055  */
00056 struct pro
00057 {
00058    char           *p_name;              /* name, eg -bl, -cli */
00059    int             p_type;              /* type (int, bool, special) */
00060    int             p_default;           /* the default value (if int) */
00061    int             p_special;           /* depends on type */
00062    int            *p_obj;               /* the associated variable */
00063 }               pro[] =
00064 
00065 {
00066 
00067    "T", PRO_SPECIAL, 0, KEY, 0,
00068    "bacc", PRO_BOOL, false, ON, &bl_around,
00069    "badp", PRO_BOOL, false, ON, &bl_at_proctop,
00070    "bad", PRO_BOOL, false, ON, &bl_aft_decl,
00071    "bap", PRO_BOOL, false, ON, &bl_a_procs,
00072    "bbb", PRO_BOOL, false, ON, &bl_bef_bk,
00073    "bc", PRO_BOOL, true, OFF, &ps.leave_comma,
00074    "bl", PRO_BOOL, true, OFF, &btype_2,
00075    "br", PRO_BOOL, true, ON, &btype_2,
00076    "bs", PRO_BOOL, false, ON, &Bill_Shannon,
00077    "cdb", PRO_BOOL, true, ON, &del_on_bl,
00078    "cd", PRO_INT, 0, 0, &ps.decl_com_ind,
00079    "ce", PRO_BOOL, true, ON, &cuddle_else,
00080    "ci", PRO_INT, 0, 0, &continuation_indent,
00081    "cli", PRO_SPECIAL, 0, CLI, 0,
00082    "c", PRO_INT, 33, 0, &ps.com_ind,
00083    "di", PRO_INT, 16, 0, &ps.decl_indent,
00084    "dj", PRO_BOOL, false, ON, &ps.ljust_decl,
00085    "d", PRO_INT, 0, 0, &ps.unindent_displace,
00086    "eei", PRO_BOOL, false, ON, &ex_expr_indent,
00087    "ei", PRO_BOOL, true, ON, &ps.else_if,
00088    "fbc", PRO_FONT, 0, 0, (int *) &blkcomf,
00089    "fbx", PRO_FONT, 0, 0, (int *) &boxcomf,
00090    "fb", PRO_FONT, 0, 0, (int *) &bodyf,
00091    "fc1", PRO_BOOL, true, ON, &format_col1_comments,
00092    "fc", PRO_FONT, 0, 0, (int *) &scomf,
00093    "fk", PRO_FONT, 0, 0, (int *) &keywordf,
00094    "fs", PRO_FONT, 0, 0, (int *) &stringf,
00095    "ip", PRO_BOOL, true, ON, &ps.indent_parameters,
00096    "i", PRO_INT, 8, 0, &ps.ind_size,
00097    "lc", PRO_INT, 0, 0, &bk_max_col,
00098    "lp", PRO_BOOL, true, ON, &lineup_to_parens,
00099    "l", PRO_INT, 78, 0, &max_col,
00100    "nbacc", PRO_BOOL, false, OFF, &bl_around,
00101    "nbadp", PRO_BOOL, false, OFF, &bl_at_proctop,
00102    "nbad", PRO_BOOL, false, OFF, &bl_aft_decl,
00103    "nbap", PRO_BOOL, false, OFF, &bl_a_procs,
00104    "nbbb", PRO_BOOL, false, OFF, &bl_bef_bk,
00105    "nbc", PRO_BOOL, true, ON, &ps.leave_comma,
00106    "nbs", PRO_BOOL, false, OFF, &Bill_Shannon,
00107    "ncdb", PRO_BOOL, true, OFF, &del_on_bl,
00108    "nce", PRO_BOOL, true, OFF, &cuddle_else,
00109    "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl,
00110    "neei", PRO_BOOL, false, OFF, &ex_expr_indent,
00111    "nei", PRO_BOOL, true, OFF, &ps.else_if,
00112    "nfc1", PRO_BOOL, true, OFF, &format_col1_comments,
00113    "nip", PRO_BOOL, true, OFF, &ps.indent_parameters,
00114    "nlp", PRO_BOOL, true, OFF, &lineup_to_parens,
00115    "npcs", PRO_BOOL, false, OFF, &proc_calls_space,
00116    "npro", PRO_SPECIAL, 0, IGN, 0,
00117    "npsl", PRO_BOOL, true, OFF, &proc_str_line,
00118    "nps", PRO_BOOL, false, OFF, &ptr_binop,
00119    "nsc", PRO_BOOL, true, OFF, &star_comment_cont,
00120    "nsob", PRO_BOOL, false, OFF, &swallow_opt_bl,
00121    "nv", PRO_BOOL, false, OFF, &verbose,
00122    "pcs", PRO_BOOL, false, ON, &proc_calls_space,
00123    "psl", PRO_BOOL, true, ON, &proc_str_line,
00124    "ps", PRO_BOOL, false, ON, &ptr_binop,
00125    "sc", PRO_BOOL, true, ON, &star_comment_cont,
00126    "sob", PRO_BOOL, false, ON, &swallow_opt_bl,
00127    "st", PRO_SPECIAL, 0, STDIN, 0,
00128    "troff", PRO_BOOL, false, ON, &troff,
00129    "v", PRO_BOOL, false, ON, &verbose,
00130    /* whew! */
00131    0, 0, 0, 0, 0
00132 };
00133 
00134 /*
00135  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles
00136  * arguments given in these files.
00137  */
00138 void set_profile()
00139 {
00140    register FILE  *f;
00141    char            fname[BUFSIZ];
00142    static char     prof[] = ".indent.pro";
00143 
00144    sprintf(fname, "%s/%s", getenv("HOME"), prof);
00145    if ((f = fopen(fname, "r")) != NULL)
00146    {
00147       scan_profile(f);
00148       (void) fclose(f);
00149    }
00150    if ((f = fopen(prof, "r")) != NULL)
00151    {
00152       scan_profile(f);
00153       (void) fclose(f);
00154    }
00155 }
00156 
00157 void scan_profile(f)
00158    register FILE  *f;
00159 {
00160    register int    i;
00161    register char  *p;
00162    char            buf[BUFSIZ];
00163 
00164    while (1)
00165    {
00166       for (p = buf; (i = getc(f)) != EOF && (*p = (char)i) > ' '; ++p);
00167       if (p != buf)
00168       {
00169          *p++ = 0;
00170          if (verbose)
00171             printf("profile: %s\n", buf);
00172          set_option(buf);
00173       } else if (i == EOF)
00174          return;
00175    }
00176 }
00177 
00178 char           *param_start;
00179 
00180 int eqin(s1, s2)
00181    register char  *s1;
00182    register char  *s2;
00183 {
00184    while (*s1)
00185    {
00186       if (*s1++ != *s2++)
00187          return (false);
00188    }
00189    param_start = s2;
00190    return (true);
00191 }
00192 
00193 /*
00194  * Set the defaults.
00195  */
00196 void set_defaults()
00197 {
00198    register struct pro *p;
00199 
00200    /* Because ps.case_indent is a float, we can't initialize it from
00201       the table: */
00202    ps.case_indent = 0;                  /* -cli0.0 */
00203    for (p = pro; p->p_name; p++)
00204       if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
00205          *p->p_obj = p->p_default;
00206 }
00207 
00208 void set_option(arg)
00209    register char  *arg;
00210 {
00211    register struct pro *p;
00212 
00213    arg++;                               /* ignore leading "-" */
00214    for (p = pro; p->p_name; p++)
00215       if (*p->p_name == *arg && eqin(p->p_name, arg))
00216          goto found;
00217    fprintf(stderr, "indent: unknown parameter \"%s\"\n", arg - 1);
00218    exit(1);
00219 found:
00220    switch (p->p_type)
00221    {
00222 
00223    case PRO_SPECIAL:
00224       switch (p->p_special)
00225       {
00226 
00227       case IGN:
00228          break;
00229 
00230       case CLI:
00231          if (*param_start == 0)
00232             goto need_param;
00233          ps.case_indent = atoi(param_start);
00234          break;
00235 
00236       case STDIN:
00237          if (input == 0)
00238             input = stdin;
00239          if (output == 0)
00240             output = stdout;
00241          break;
00242 
00243       case KEY:
00244          if (*param_start == 0)
00245             goto need_param;
00246          {
00247             register char  *str = (char *) malloc(strlen(param_start) + 1);
00248             strcpy(str, param_start);
00249             addkey(str, 4);
00250          }
00251          break;
00252 
00253       default:
00254          fprintf(stderr, "\
00255 indent: set_option: internal error: p_special %d\n", p->p_special);
00256          exit(1);
00257       }
00258       break;
00259 
00260    case PRO_BOOL:
00261       if (p->p_special == OFF)
00262          *p->p_obj = false;
00263       else
00264          *p->p_obj = true;
00265       break;
00266 
00267    case PRO_INT:
00268       if (*param_start == 0)
00269       {
00270    need_param:
00271          fprintf(stderr, "indent: ``%s'' requires a parameter\n",
00272                  arg - 1);
00273          exit(1);
00274       }
00275       *p->p_obj = atoi(param_start);
00276       break;
00277 
00278    case PRO_FONT:
00279       parsefont((struct fstate *) p->p_obj, param_start);
00280       break;
00281 
00282    default:
00283       fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
00284               p->p_type);
00285       exit(1);
00286    }
00287 }

Generated on Fri Apr 14 22:56:58 2006 for minix by  doxygen 1.4.6