new version from James Gosling <jag@Sun.COM>
[unix-history] / usr / src / usr.bin / indent / args.c
index 5c6aa25..2aca4b8 100644 (file)
@@ -1,5 +1,6 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
+ * Copyright (c) 1985 Sun Microsystems, Inc.
+ * Copyright (c) 1980 The Regents of the University of California.
  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  * All rights reserved.
  *
  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  * All rights reserved.
  *
@@ -8,34 +9,35 @@
  * duplicated in all such forms and that any documentation,
  * advertising materials, and other materials related to such
  * distribution and use acknowledge that the software was developed
  * duplicated in all such forms and that any documentation,
  * advertising materials, and other materials related to such
  * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley and the University
- * of Illinois, Urbana.  The name of either
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
+ * by the University of California, Berkeley, the University of Illinois,
+ * Urbana, and Sun Microsystems, Inc.  The name of either University
+ * or Sun Microsystems may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)args.c     5.4 (Berkeley) %G%";
+static char sccsid[] = "@(#)args.c     5.5 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
- * Argument scanning and profile reading code.  Default parameters
- * are set here as well.
+ * Argument scanning and profile reading code.  Default parameters are set
+ * here as well.
  */
 
 #include "indent_globs.h"
 #include <sys/types.h>
 #include <ctype.h>
 
  */
 
 #include "indent_globs.h"
 #include <sys/types.h>
 #include <ctype.h>
 
-char *getenv(), *index();
+char       *getenv(), *index();
 
 /* profile types */
 #define        PRO_SPECIAL     1       /* special case */
 #define        PRO_BOOL        2       /* boolean */
 #define        PRO_INT         3       /* integer */
 
 /* profile types */
 #define        PRO_SPECIAL     1       /* special case */
 #define        PRO_BOOL        2       /* boolean */
 #define        PRO_INT         3       /* integer */
+#define PRO_FONT       4       /* troff font */
 
 /* profile specials for booleans */
 #define        ON              1       /* turn it on */
 
 /* profile specials for booleans */
 #define        ON              1       /* turn it on */
@@ -48,82 +50,94 @@ char *getenv(), *index();
 #define        KEY             4       /* type (keyword) */
 
 /*
 #define        KEY             4       /* type (keyword) */
 
 /*
- * N.B.: because of the way the table here is scanned, options
- * whose names are substrings of other options must occur later;
- * that is, with -lp vs -l, -lp must be first.  Also, while (most)
- * booleans occur more than once, the last default value is the
- * one actually assigned.
+ * N.B.: because of the way the table here is scanned, options whose names are
+ * substrings of other options must occur later; that is, with -lp vs -l, -lp
+ * must be first.  Also, while (most) booleans occur more than once, the last
+ * default value is the one actually assigned.
  */
 struct pro {
  */
 struct pro {
-    char *p_name;              /* name, eg -bl, -cli */
-    int   p_type;              /* type (int, bool, special) */
-    int   p_default;           /* the default value (if int) */
-    int   p_special;           /* depends on type */
-    int  *p_obj;               /* the associated variable */
-} pro[] = {
-    "npro",    PRO_SPECIAL,    0,      IGN,    0,
-    "lc",      PRO_INT,        0,      0,      &block_comment_max_col,
-    "lp",      PRO_BOOL,       true,   ON,     &lineup_to_parens,
-    "nlp",     PRO_BOOL,       true,   OFF,    &lineup_to_parens,
-    "l",       PRO_INT,        78,     0,      &max_col,
-    "psl",     PRO_BOOL,       true,   ON,     &procnames_start_line,
-    "npsl",    PRO_BOOL,       true,   OFF,    &procnames_start_line,
-    "fc1",     PRO_BOOL,       true,   ON,     &format_col1_comments,
-    "nfc1",    PRO_BOOL,       true,   OFF,    &format_col1_comments,
-    "pcs",     PRO_BOOL,       false,  ON,     &proc_calls_space,
-    "npcs",    PRO_BOOL,       false,  OFF,    &proc_calls_space,
-    "ip",      PRO_BOOL,       true,   ON,     &ps.indent_parameters,
-    "nip",     PRO_BOOL,       true,   OFF,    &ps.indent_parameters,
- /* see set_defaults for initialization of -cli */
-    "cli",     PRO_SPECIAL,    0,      CLI,    0,
-    "ci",      PRO_INT,        0,      0,      &continuation_indent,
-    "cdb",     PRO_BOOL,       true,   ON,  &comment_delimiter_on_blankline,
-    "ncdb",    PRO_BOOL,       true,   OFF, &comment_delimiter_on_blankline,
-    "i",       PRO_INT,        8,      0,      &ps.ind_size,
-    "cd",      PRO_INT,        0,      0,      &ps.decl_com_ind,
-    "ce",      PRO_BOOL,       true,   ON,     &cuddle_else,
-    "nce",     PRO_BOOL,       true,   OFF,    &cuddle_else,
-    "c",       PRO_INT,        33,     0,      &ps.com_ind,
-    "v",       PRO_BOOL,       false,  ON,     &verbose,
-    "nv",      PRO_BOOL,       false,  OFF,    &verbose,
-    "dj",      PRO_BOOL,       false,  ON,     &ps.ljust_decl,
-    "ndj",     PRO_BOOL,       false,  OFF,    &ps.ljust_decl,
- /* don't ask *me* why -bc/-nbc is backwards.... */
-    "bc",      PRO_BOOL,       true,   OFF,    &ps.leave_comma,
-    "nbc",     PRO_BOOL,       true,   ON,     &ps.leave_comma,
-    "di",      PRO_INT,        16,     0,      &ps.decl_indent,
-    "d",       PRO_INT,        0,      0,      &ps.unindent_displace,
-    "br",      PRO_BOOL,       true,   ON,     &btype_2,
-    "bl",      PRO_BOOL,       true,   OFF,    &btype_2,
-    "st",      PRO_SPECIAL,    0,      STDIN,  0,
-    "ei",      PRO_BOOL,       true,   ON,     &ps.else_if,
-    "nei",     PRO_BOOL,       true,   OFF,    &ps.else_if,
-    "sc",      PRO_BOOL,       true,   ON,     &star_comment_cont,
-    "nsc",     PRO_BOOL,       true,   OFF,    &star_comment_cont,
-    "bap",     PRO_BOOL,       false,  ON,     &blanklines_after_procs,
-    "nbap",    PRO_BOOL,       false,  OFF,    &blanklines_after_procs,
-    "sob",     PRO_BOOL,       false,  ON,     &swallow_optional_blanklines,
-    "nsob",    PRO_BOOL,       false,  OFF,    &swallow_optional_blanklines,
-    "bad",     PRO_BOOL,       false,  ON,  &blanklines_after_declarations,
-    "nbad",    PRO_BOOL,       false,  OFF, &blanklines_after_declarations,
-    "bbb",     PRO_BOOL,       false,  ON,  &blanklines_before_blockcomments,
-    "nbbb",    PRO_BOOL,       false,  OFF, &blanklines_before_blockcomments,
-    "ps",      PRO_BOOL,       false,  ON,     &pointer_as_binop,
-    "nps",     PRO_BOOL,       false,  OFF,    &pointer_as_binop,
-    "troff",   PRO_BOOL,       false,  ON,     &troff,
-    "T",       PRO_SPECIAL,    0,      KEY,    0,
- /* whew! */
-    0,         0,              0,      0,      0
+    char       *p_name;                /* name, eg -bl, -cli */
+    int         p_type;                /* type (int, bool, special) */
+    int         p_default;     /* the default value (if int) */
+    int         p_special;     /* depends on type */
+    int        *p_obj;         /* the associated variable */
+}           pro[] = {
+
+    "T", PRO_SPECIAL, 0, KEY, 0,
+    "bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation,
+    "badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop,
+    "bad", PRO_BOOL, false, ON, &blanklines_after_declarations,
+    "bap", PRO_BOOL, false, ON, &blanklines_after_procs,
+    "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments,
+    "bc", PRO_BOOL, true, OFF, &ps.leave_comma,
+    "bl", PRO_BOOL, true, OFF, &btype_2,
+    "br", PRO_BOOL, true, ON, &btype_2,
+    "bs", PRO_BOOL, false, ON, &Bill_Shannon,
+    "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline,
+    "cd", PRO_INT, 0, 0, &ps.decl_com_ind,
+    "ce", PRO_BOOL, true, ON, &cuddle_else,
+    "ci", PRO_INT, 0, 0, &continuation_indent,
+    "cli", PRO_SPECIAL, 0, CLI, 0,
+    "c", PRO_INT, 33, 0, &ps.com_ind,
+    "di", PRO_INT, 16, 0, &ps.decl_indent,
+    "dj", PRO_BOOL, false, ON, &ps.ljust_decl,
+    "d", PRO_INT, 0, 0, &ps.unindent_displace,
+    "eei", PRO_BOOL, false, ON, &extra_expression_indent,
+    "ei", PRO_BOOL, true, ON, &ps.else_if,
+    "fbc", PRO_FONT, 0, 0, (int *) &blkcomf,
+    "fbx", PRO_FONT, 0, 0, (int *) &boxcomf,
+    "fb", PRO_FONT, 0, 0, (int *) &bodyf,
+    "fc1", PRO_BOOL, true, ON, &format_col1_comments,
+    "fc", PRO_FONT, 0, 0, (int *) &scomf,
+    "fk", PRO_FONT, 0, 0, (int *) &keywordf,
+    "fs", PRO_FONT, 0, 0, (int *) &stringf,
+    "ip", PRO_BOOL, true, ON, &ps.indent_parameters,
+    "i", PRO_INT, 8, 0, &ps.ind_size,
+    "lc", PRO_INT, 0, 0, &block_comment_max_col,
+    "lp", PRO_BOOL, true, ON, &lineup_to_parens,
+    "l", PRO_INT, 78, 0, &max_col,
+    "nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation,
+    "nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop,
+    "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations,
+    "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs,
+    "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments,
+    "nbc", PRO_BOOL, true, ON, &ps.leave_comma,
+    "nbs", PRO_BOOL, false, OFF, &Bill_Shannon,
+    "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline,
+    "nce", PRO_BOOL, true, OFF, &cuddle_else,
+    "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl,
+    "neei", PRO_BOOL, false, OFF, &extra_expression_indent,
+    "nei", PRO_BOOL, true, OFF, &ps.else_if,
+    "nfc1", PRO_BOOL, true, OFF, &format_col1_comments,
+    "nip", PRO_BOOL, true, OFF, &ps.indent_parameters,
+    "nlp", PRO_BOOL, true, OFF, &lineup_to_parens,
+    "npcs", PRO_BOOL, false, OFF, &proc_calls_space,
+    "npro", PRO_SPECIAL, 0, IGN, 0,
+    "npsl", PRO_BOOL, true, OFF, &procnames_start_line,
+    "nps", PRO_BOOL, false, OFF, &pointer_as_binop,
+    "nsc", PRO_BOOL, true, OFF, &star_comment_cont,
+    "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines,
+    "nv", PRO_BOOL, false, OFF, &verbose,
+    "pcs", PRO_BOOL, false, ON, &proc_calls_space,
+    "psl", PRO_BOOL, true, ON, &procnames_start_line,
+    "ps", PRO_BOOL, false, ON, &pointer_as_binop,
+    "sc", PRO_BOOL, true, ON, &star_comment_cont,
+    "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines,
+    "st", PRO_SPECIAL, 0, STDIN, 0,
+    "troff", PRO_BOOL, false, ON, &troff,
+    "v", PRO_BOOL, false, ON, &verbose,
+    /* whew! */
+    0, 0, 0, 0, 0
 };
 
 /*
 };
 
 /*
- * set_profile reads $HOME/.indent.pro and ./.indent.pro and
- * handles arguments given in these files.
+ * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
+ * given in these files.
  */
 set_profile()
 {
     register FILE *f;
  */
 set_profile()
 {
     register FILE *f;
-    char fname[BUFSIZ];
+    char        fname[BUFSIZ];
     static char pro[] = ".indent.pro";
 
     sprintf(fname, "%s/%s", getenv("HOME"), pro);
     static char pro[] = ".indent.pro";
 
     sprintf(fname, "%s/%s", getenv("HOME"), pro);
@@ -140,30 +154,21 @@ set_profile()
 scan_profile(f)
     register FILE *f;
 {
 scan_profile(f)
     register FILE *f;
 {
-    register char *p, *arg;
-    char buf[BUFSIZ];
+    register char *p;
+    char        buf[BUFSIZ];
 
 
-    while (fgets(buf, sizeof buf, f)) {
-       if ((p = index(buf, '\n')) != NULL)
-           *p = 0;
-       if (verbose)
-           printf("profile: %s\n", buf);
+    while (1) {
        p = buf;
        p = buf;
-       for (;;) {
-           while (isspace(*p))
-               p++;
-           if (*p == 0)
-               break;
-           arg = p;
-           while (*p) {
-               if (isspace(*p)) {
-                   *p++ = 0;
-                   break;
-               }
-               p++;
-           }
-           set_option(arg);
+       while ((*p = getc(f)) != EOF && *p > ' ')
+           p++;
+       if (p != buf) {
+           *p++ = 0;
+           if (verbose)
+               printf("profile: %s\n", buf);
+           set_option(buf);
        }
        }
+       else if (*p == EOF)
+           return;
     }
 }
 
     }
 }
 
@@ -189,12 +194,12 @@ set_defaults()
     register struct pro *p;
 
     /*
     register struct pro *p;
 
     /*
-     * Because ps.case_indent is a float, we can't initialize it
-     * from the table:
+     * Because ps.case_indent is a float, we can't initialize it from the
+     * table:
      */
     ps.case_indent = 0.0;      /* -cli0.0 */
     for (p = pro; p->p_name; p++)
      */
     ps.case_indent = 0.0;      /* -cli0.0 */
     for (p = pro; p->p_name; p++)
-       if (p->p_type != PRO_SPECIAL)
+       if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
            *p->p_obj = p->p_default;
 }
 
            *p->p_obj = p->p_default;
 }
 
@@ -213,58 +218,66 @@ set_option(arg)
 found:
     switch (p->p_type) {
 
 found:
     switch (p->p_type) {
 
-       case PRO_SPECIAL:
-           switch (p->p_special) {
-
-               case IGN:
-                   break;
-
-               case CLI:
-                   if (*param_start == 0)
-                       goto need_param;
-                   ps.case_indent = atof(param_start);
-                   break;
+    case PRO_SPECIAL:
+       switch (p->p_special) {
 
 
-               case STDIN:
-                   if (input == 0)
-                       input = stdin;
-                   if (output == 0)
-                       output = stdout;
-                   break;
-
-               case KEY:
-                   if (*param_start == 0)
-                       goto need_param;
-                   addkey(param_start, 4);
-                   break;
+       case IGN:
+           break;
 
 
-               default:
-                   fprintf(stderr, "\
-indent: set_option: internal error: p_special %d\n", p->p_special);
-                   exit(1);
-           }
+       case CLI:
+           if (*param_start == 0)
+               goto need_param;
+           ps.case_indent = atof(param_start);
            break;
 
            break;
 
-       case PRO_BOOL:
-           if (p->p_special == OFF)
-               *p->p_obj = false;
-           else
-               *p->p_obj = true;
+       case STDIN:
+           if (input == 0)
+               input = stdin;
+           if (output == 0)
+               output = stdout;
            break;
 
            break;
 
-       case PRO_INT:
-           if (*param_start == 0) {
-need_param:
-               fprintf(stderr, "indent: ``%s'' requires a parameter\n",
-                       arg - 1);
-               exit(1);
+       case KEY:
+           if (*param_start == 0)
+               goto need_param;
+           {
+               register char *str = (char *) malloc(strlen(param_start) + 1);
+               strcpy(str, param_start);
+               addkey(str, 4);
            }
            }
-           *p->p_obj = atoi(param_start);
            break;
 
        default:
            break;
 
        default:
-           fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
-                   p->p_type);
+           fprintf(stderr, "\
+indent: set_option: internal error: p_special %d\n", p->p_special);
+           exit(1);
+       }
+       break;
+
+    case PRO_BOOL:
+       if (p->p_special == OFF)
+           *p->p_obj = false;
+       else
+           *p->p_obj = true;
+       break;
+
+    case PRO_INT:
+       if (*param_start == 0) {
+    need_param:
+           fprintf(stderr, "indent: ``%s'' requires a parameter\n",
+                   arg - 1);
            exit(1);
            exit(1);
+       }
+       *p->p_obj = atoi(param_start);
+       break;
+
+    case PRO_FONT:
+       parsefont((struct fstate *) p->p_obj, param_start);
+       break;
+
+    default:
+       fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
+               p->p_type);
+       exit(1);
     }
 }
     }
 }