home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) 1980 Regents of the University of California. All rights
- * reserved. The Berkeley software License Agreement specifies the terms and
- * conditions for redistribution. */
-
- #ifndef lint
- static char sccsid[] = "@(#)args.c 5.2 (Berkeley) 9/10/85";
- #endif not lint
-
- /* Argument scanning and profile reading code. Default parameters are set
- * here as well. */
-
- #include "indent_globs.h"
- #include <sys/types.h>
- #include <ctype.h>
-
- /* profile types */
- typedef enum profile {
- PRO_SPECIAL = 1,
- PRO_BOOL,
- PRO_INT,
- PRO_SETTINGS,
- } profile;
-
- /* profile specials for specials and booleans */
- typedef enum special {
- OFF = 0,
- ON,
- IGN,
- CLI,
- STDIN,
- KEY,
- } special;
-
- /* 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 {
- char *p_name; /* name, eg -bl, -cli */
- profile p_type; /* type (int, bool, special) */
- int p_default; /* the default value (if int) */
- special p_special; /* depends on type */
- int *p_obj; /* the associated variable */
- char *p_data; /* data for the option */
- };
-
- struct pro pro[] = {
- "npro", PRO_SPECIAL, 0, IGN, 0, NULL,
- "lc", PRO_INT, 0, 0, &block_comment_max_col, NULL,
- "lp", PRO_BOOL, true, ON, &lineup_to_parens, NULL,
- "nlp", PRO_BOOL, true, OFF, &lineup_to_parens, NULL,
- "l", PRO_INT, 78, 0, &max_col, NULL,
- "psl", PRO_BOOL, true, ON, &procnames_start_line, NULL,
- "npsl", PRO_BOOL, true, OFF, &procnames_start_line, NULL,
- "fc1", PRO_BOOL, true, ON, &format_col1_comments, NULL,
- "nfc1", PRO_BOOL, true, OFF, &format_col1_comments, NULL,
- "fca", PRO_BOOL, true, ON, &format_comments, NULL,
- "nfca", PRO_BOOL, true, OFF, &format_comments, NULL,
- "pcs", PRO_BOOL, false, ON, &proc_calls_space, NULL,
- "npcs", PRO_BOOL, false, OFF, &proc_calls_space, NULL,
- "ip", PRO_BOOL, true, ON, &ps.indent_parameters, NULL,
- "nip", PRO_BOOL, true, OFF, &ps.indent_parameters, NULL,
- /* see set_defaults for initialization of -cli */
- "cli", PRO_SPECIAL, 0, CLI, 0, NULL,
- "ci", PRO_INT, 0, 0, &continuation_indent, NULL,
- "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline, NULL,
- "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline, NULL,
- "i", PRO_INT, 8, 0, &ps.ind_size, NULL,
- "cd", PRO_INT, 0, 0, &ps.decl_com_ind, NULL,
- "ce", PRO_BOOL, true, ON, &cuddle_else, NULL,
- "nce", PRO_BOOL, true, OFF, &cuddle_else, NULL,
- "c", PRO_INT, 33, 0, &ps.com_ind, NULL,
- "v", PRO_BOOL, false, ON, &verbose, NULL,
- "nv", PRO_BOOL, false, OFF, &verbose, NULL,
- "dj", PRO_BOOL, false, ON, &ps.ljust_decl, NULL,
- "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl, NULL,
- /* don't ask *me* why -bc/-nbc is backwards.... */
- "bc", PRO_BOOL, true, OFF, &ps.leave_comma, NULL,
- "nbc", PRO_BOOL, true, ON, &ps.leave_comma, NULL,
- "di", PRO_INT, 16, 0, &ps.decl_indent, NULL,
- "d", PRO_INT, 0, 0, &ps.unindent_displace, NULL,
- "br", PRO_BOOL, true, ON, &btype_2, NULL,
- "bl", PRO_BOOL, true, OFF, &btype_2, NULL,
- "st", PRO_SPECIAL, 0, STDIN, 0, NULL,
- "ei", PRO_BOOL, true, ON, &ps.else_if, NULL,
- "nei", PRO_BOOL, true, OFF, &ps.else_if, NULL,
- "sc", PRO_BOOL, true, ON, &star_comment_cont, NULL,
- "nsc", PRO_BOOL, true, OFF, &star_comment_cont, NULL,
- "bap", PRO_BOOL, false, ON, &blanklines_after_procs, NULL,
- "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs, NULL,
- "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines, NULL,
- "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines, NULL,
- "bad", PRO_BOOL, false, ON, &blanklines_after_declarations, NULL,
- "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations, NULL,
- "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments, NULL,
- "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments, NULL,
- "ps", PRO_BOOL, false, ON, &pointer_as_binop, NULL,
- "nps", PRO_BOOL, false, OFF, &pointer_as_binop, NULL,
- "troff", PRO_BOOL, false, ON, &troff, NULL,
- "T", PRO_SPECIAL, 0, KEY, 0, NULL,
- /* whew! */
- /* below two are borrowed from another version of indent, that is gnuindent,
- * but that wouldn't work on my machine and is bad if you don't have enough
- * memory (for those huge files */
- "kr", PRO_SETTINGS, 0, OFF, &kr_format, "-nbad\0-bap\0" \
- "-nbbb\0-nbc\0-br\0-c33\0-cd33\0-ncdb\0-ce\0-ci4\0-cli0\0-d0\0-di1\0-nfc1\0" \
- "-i4\0-ip0\0-l75\0-lp\0-npcs\0-npsl\0-cs\0-nsc\0-nsc\0-nsob\0-nfca\0\0",
- "gnu", PRO_SETTINGS, 0, OFF, &gnu_format, "-nbad\0-bap\0" \
- "-nbbb\0-nbc\0-bl\0-ncdb\0-cs\0-nce\0-di1\0-ndj\0-ei\0-nfc1\0-i2\0-ip5\0-lp\0" \
- "-pcs\0-nps\0-psl\0-nsc\0-nsob\0-cp1\0-nfca\0\0",
- /* below I made myself */
- "t", PRO_INT, 8, OFF, &tabsize, NULL,
- 0, 0, 0, 0, 0, 0,
- };
-
- /* set_profile reads $PATH/indent.pro and handles arguments given in this
- * file. */
- set_profile()
- {
- register FILE *f;
- char fname[BUFSIZ];
- char path[512]; /* hope this is a long enough path! */
- char *p;
- static char pro[] = "indent.pro";
-
- strcpy(path, getenv("PATH"));
-
- while (strlen(path) != 0) {
- /* get head element */
- if ((p = strchr(path, ';')) != NULL) {
- *p = NIL; /* delimit first string */
- p++; /* save positon of rest of string */
- }
- /* make path to look in */
- sprintf(fname, "%s\\%s", path, pro);
- sprintf(fname, "%s", path);
- /* check for trailing '\' by looking at the last element of the
- * string */
- if ((strncmp(&fname[strlen(fname) - 1], "\\", 1)) != 0)
- /* there is no trailing delimiter , add it */
- strcat(fname, "\\");
-
- /* add "profile.pro" */
- strcat(fname, pro);
-
- /* move path to next element */
- strcpy(path, p);
-
- /* look in directory */
- if ((f = fopen(fname, "r")) != NULL) {
- scan_profile(f);
- (void) fclose(f);
- return (0);
- }
- }
- fprintf(stderr, "indent: indent.pro not found on path; using defaults\n");
- }
-
- void
- scan_profile(f)
- register FILE *f;
- {
- register char *p, *arg;
- char buf[BUFSIZ];
-
- while (fgets(buf, sizeof buf, f)) {
- if ((p = strchr(buf, '\n')) != NULL)
- *p = 0;
- if (verbose)
- printf("profile: %s\n", 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);
- }
- }
- }
-
- char *param_start;
-
- eqin(s1, s2)
- register char *s1;
- register char *s2;
- {
- while (*s1) {
- if (*s1++ != *s2++)
- return (false);
- }
- param_start = s2;
- return (true);
- }
-
- /* Set the defaults. */
- void
- set_defaults()
- {
- register struct pro *p;
-
- /* 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++)
- if (p->p_type != PRO_SPECIAL)
- *p->p_obj = p->p_default;
- }
-
- void
- set_option(arg)
- register char *arg;
- {
- register struct pro *p;
-
- arg++; /* ignore leading "-" */
- for (p = pro; p->p_name; p++)
- if (*p->p_name == *arg && eqin(p->p_name, arg))
- goto found;
- fprintf(stderr, "indent: unknown parameter \"%s\"\n", arg - 1);
- exit(1);
- 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 STDIN:
- if (input == 0)
- input = stdin;
- if (output == 0)
- output = stdout;
- break;
-
- case KEY:
- if (*param_start == 0)
- goto need_param;
- addkey(param_start, rw_decl);
- break;
-
- default:
- 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);
- }
- *p->p_obj = atoi(param_start);
- break;
-
- case PRO_SETTINGS:
- {
- char *t; /* current position */
-
- set_defaults();
-
- t = p->p_data;
- do {
- set_option(t);
- /* advance to character following next NUL */
- while (*t++);
- }
- while (*t);
- }
- break;
-
- default:
- fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
- p->p_type);
- exit(1);
- }
- }
-