124647Smckusick /* 224647Smckusick * Copyright (c) 1980 Regents of the University of California. 3*33767Sbostic * Copyright (c) 1976 Board of Trustees of the University of Illinois. 4*33767Sbostic * All rights reserved. 5*33767Sbostic * 6*33767Sbostic * Redistribution and use in source and binary forms are permitted 7*33767Sbostic * provided that this notice is preserved and that due credit is given 8*33767Sbostic * to the University of California at Berkeley and the University of 9*33767Sbostic * Illinois at Urbana. The name of either University may not be used 10*33767Sbostic * to endorse or promote products derived from this software without 11*33767Sbostic * specific prior written permission. This software is provided 12*33767Sbostic * ``as is'' without express or implied warranty. 1324647Smckusick */ 1424647Smckusick 1524647Smckusick #ifndef lint 16*33767Sbostic static char sccsid[] = "@(#)args.c 5.3 (Berkeley) 03/22/88"; 17*33767Sbostic #endif /* not lint */ 1824647Smckusick 1924647Smckusick /* 2024647Smckusick * Argument scanning and profile reading code. Default parameters 2124647Smckusick * are set here as well. 2224647Smckusick */ 2324647Smckusick 2424647Smckusick #include "indent_globs.h" 2524647Smckusick #include <sys/types.h> 2624647Smckusick #include <ctype.h> 2724647Smckusick 2824647Smckusick char *getenv(), *index(); 2924647Smckusick 3024647Smckusick /* profile types */ 3124647Smckusick #define PRO_SPECIAL 1 /* special case */ 3224647Smckusick #define PRO_BOOL 2 /* boolean */ 3324647Smckusick #define PRO_INT 3 /* integer */ 3424647Smckusick 3524647Smckusick /* profile specials for booleans */ 3624647Smckusick #define ON 1 /* turn it on */ 3724647Smckusick #define OFF 0 /* turn it off */ 3824647Smckusick 3924647Smckusick /* profile specials for specials */ 4024647Smckusick #define IGN 1 /* ignore it */ 4124647Smckusick #define CLI 2 /* case label indent (float) */ 4224647Smckusick #define STDIN 3 /* use stdin */ 4324647Smckusick #define KEY 4 /* type (keyword) */ 4424647Smckusick 4524647Smckusick /* 4624647Smckusick * N.B.: because of the way the table here is scanned, options 4724647Smckusick * whose names are substrings of other options must occur later; 4824647Smckusick * that is, with -lp vs -l, -lp must be first. Also, while (most) 4924647Smckusick * booleans occur more than once, the last default value is the 5024647Smckusick * one actually assigned. 5124647Smckusick */ 5224647Smckusick struct pro { 5324647Smckusick char *p_name; /* name, eg -bl, -cli */ 5424647Smckusick int p_type; /* type (int, bool, special) */ 5524647Smckusick int p_default; /* the default value (if int) */ 5624647Smckusick int p_special; /* depends on type */ 5724647Smckusick int *p_obj; /* the associated variable */ 5824647Smckusick } pro[] = { 5924647Smckusick "npro", PRO_SPECIAL, 0, IGN, 0, 6024647Smckusick "lc", PRO_INT, 0, 0, &block_comment_max_col, 6124647Smckusick "lp", PRO_BOOL, true, ON, &lineup_to_parens, 6224647Smckusick "nlp", PRO_BOOL, true, OFF, &lineup_to_parens, 6324647Smckusick "l", PRO_INT, 78, 0, &max_col, 6424647Smckusick "psl", PRO_BOOL, true, ON, &procnames_start_line, 6524647Smckusick "npsl", PRO_BOOL, true, OFF, &procnames_start_line, 6624647Smckusick "fc1", PRO_BOOL, true, ON, &format_col1_comments, 6724647Smckusick "nfc1", PRO_BOOL, true, OFF, &format_col1_comments, 6824647Smckusick "pcs", PRO_BOOL, false, ON, &proc_calls_space, 6924647Smckusick "npcs", PRO_BOOL, false, OFF, &proc_calls_space, 7024647Smckusick "ip", PRO_BOOL, true, ON, &ps.indent_parameters, 7124647Smckusick "nip", PRO_BOOL, true, OFF, &ps.indent_parameters, 7224647Smckusick /* see set_defaults for initialization of -cli */ 7324647Smckusick "cli", PRO_SPECIAL, 0, CLI, 0, 7424647Smckusick "ci", PRO_INT, 0, 0, &continuation_indent, 7524647Smckusick "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline, 7624647Smckusick "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline, 7724647Smckusick "i", PRO_INT, 8, 0, &ps.ind_size, 7824647Smckusick "cd", PRO_INT, 0, 0, &ps.decl_com_ind, 7924647Smckusick "ce", PRO_BOOL, true, ON, &cuddle_else, 8024647Smckusick "nce", PRO_BOOL, true, OFF, &cuddle_else, 8124647Smckusick "c", PRO_INT, 33, 0, &ps.com_ind, 8224647Smckusick "v", PRO_BOOL, false, ON, &verbose, 8324647Smckusick "nv", PRO_BOOL, false, OFF, &verbose, 8424647Smckusick "dj", PRO_BOOL, false, ON, &ps.ljust_decl, 8524647Smckusick "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl, 8624647Smckusick /* don't ask *me* why -bc/-nbc is backwards.... */ 8724647Smckusick "bc", PRO_BOOL, true, OFF, &ps.leave_comma, 8824647Smckusick "nbc", PRO_BOOL, true, ON, &ps.leave_comma, 8924647Smckusick "di", PRO_INT, 16, 0, &ps.decl_indent, 9024647Smckusick "d", PRO_INT, 0, 0, &ps.unindent_displace, 9124647Smckusick "br", PRO_BOOL, true, ON, &btype_2, 9224647Smckusick "bl", PRO_BOOL, true, OFF, &btype_2, 9324647Smckusick "st", PRO_SPECIAL, 0, STDIN, 0, 9424647Smckusick "ei", PRO_BOOL, true, ON, &ps.else_if, 9524647Smckusick "nei", PRO_BOOL, true, OFF, &ps.else_if, 9624647Smckusick "sc", PRO_BOOL, true, ON, &star_comment_cont, 9724647Smckusick "nsc", PRO_BOOL, true, OFF, &star_comment_cont, 9824647Smckusick "bap", PRO_BOOL, false, ON, &blanklines_after_procs, 9924647Smckusick "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs, 10024647Smckusick "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines, 10124647Smckusick "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines, 10224647Smckusick "bad", PRO_BOOL, false, ON, &blanklines_after_declarations, 10324647Smckusick "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations, 10424647Smckusick "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments, 10524647Smckusick "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments, 10624677Smckusick "ps", PRO_BOOL, false, ON, &pointer_as_binop, 10724677Smckusick "nps", PRO_BOOL, false, OFF, &pointer_as_binop, 10824647Smckusick "troff", PRO_BOOL, false, ON, &troff, 10924647Smckusick "T", PRO_SPECIAL, 0, KEY, 0, 11024647Smckusick /* whew! */ 11124647Smckusick 0, 0, 0, 0, 0 11224647Smckusick }; 11324647Smckusick 11424647Smckusick /* 11524647Smckusick * set_profile reads $HOME/.indent.pro and ./.indent.pro and 11624647Smckusick * handles arguments given in these files. 11724647Smckusick */ 11824647Smckusick set_profile() 11924647Smckusick { 12024647Smckusick register FILE *f; 12124647Smckusick char fname[BUFSIZ]; 12224647Smckusick static char pro[] = ".indent.pro"; 12324647Smckusick 12424647Smckusick sprintf(fname, "%s/%s", getenv("HOME"), pro); 12524647Smckusick if ((f = fopen(fname, "r")) != NULL) { 12624647Smckusick scan_profile(f); 12724647Smckusick (void) fclose(f); 12824647Smckusick } 12924647Smckusick if ((f = fopen(pro, "r")) != NULL) { 13024647Smckusick scan_profile(f); 13124647Smckusick (void) fclose(f); 13224647Smckusick } 13324647Smckusick } 13424647Smckusick 13524647Smckusick scan_profile(f) 13624647Smckusick register FILE *f; 13724647Smckusick { 13824647Smckusick register char *p, *arg; 13924647Smckusick char buf[BUFSIZ]; 14024647Smckusick 14124647Smckusick while (fgets(buf, sizeof buf, f)) { 14224647Smckusick if ((p = index(buf, '\n')) != NULL) 14324647Smckusick *p = 0; 14424647Smckusick if (verbose) 14524647Smckusick printf("profile: %s\n", buf); 14624647Smckusick p = buf; 14724647Smckusick for (;;) { 14824647Smckusick while (isspace(*p)) 14924647Smckusick p++; 15024647Smckusick if (*p == 0) 15124647Smckusick break; 15224647Smckusick arg = p; 15324647Smckusick while (*p) { 15424647Smckusick if (isspace(*p)) { 15524647Smckusick *p++ = 0; 15624647Smckusick break; 15724647Smckusick } 15824647Smckusick p++; 15924647Smckusick } 16024647Smckusick set_option(arg); 16124647Smckusick } 16224647Smckusick } 16324647Smckusick } 16424647Smckusick 16524647Smckusick char *param_start; 16624647Smckusick 16724647Smckusick eqin(s1, s2) 16824647Smckusick register char *s1; 16924647Smckusick register char *s2; 17024647Smckusick { 17124647Smckusick while (*s1) { 17224647Smckusick if (*s1++ != *s2++) 17324647Smckusick return (false); 17424647Smckusick } 17524647Smckusick param_start = s2; 17624647Smckusick return (true); 17724647Smckusick } 17824647Smckusick 17924647Smckusick /* 18024647Smckusick * Set the defaults. 18124647Smckusick */ 18224647Smckusick set_defaults() 18324647Smckusick { 18424647Smckusick register struct pro *p; 18524647Smckusick 18624647Smckusick /* 18724647Smckusick * Because ps.case_indent is a float, we can't initialize it 18824647Smckusick * from the table: 18924647Smckusick */ 19024647Smckusick ps.case_indent = 0.0; /* -cli0.0 */ 19124647Smckusick for (p = pro; p->p_name; p++) 19224647Smckusick if (p->p_type != PRO_SPECIAL) 19324647Smckusick *p->p_obj = p->p_default; 19424647Smckusick } 19524647Smckusick 19624647Smckusick set_option(arg) 19724647Smckusick register char *arg; 19824647Smckusick { 19924647Smckusick register struct pro *p; 20024647Smckusick extern double atof(); 20124647Smckusick 20224647Smckusick arg++; /* ignore leading "-" */ 20324647Smckusick for (p = pro; p->p_name; p++) 20424647Smckusick if (*p->p_name == *arg && eqin(p->p_name, arg)) 20524647Smckusick goto found; 20624647Smckusick fprintf(stderr, "indent: unknown parameter \"%s\"\n", arg - 1); 20724647Smckusick exit(1); 20824647Smckusick found: 20924647Smckusick switch (p->p_type) { 21024647Smckusick 21124647Smckusick case PRO_SPECIAL: 21224647Smckusick switch (p->p_special) { 21324647Smckusick 21424647Smckusick case IGN: 21524647Smckusick break; 21624647Smckusick 21724647Smckusick case CLI: 21824647Smckusick if (*param_start == 0) 21924647Smckusick goto need_param; 22024647Smckusick ps.case_indent = atof(param_start); 22124647Smckusick break; 22224647Smckusick 22324647Smckusick case STDIN: 22424647Smckusick if (input == 0) 22524647Smckusick input = stdin; 22624647Smckusick if (output == 0) 22724647Smckusick output = stdout; 22824647Smckusick break; 22924647Smckusick 23024647Smckusick case KEY: 23124647Smckusick if (*param_start == 0) 23224647Smckusick goto need_param; 23324647Smckusick addkey(param_start, 4); 23424647Smckusick break; 23524647Smckusick 23624647Smckusick default: 23724647Smckusick fprintf(stderr, "\ 23824647Smckusick indent: set_option: internal error: p_special %d\n", p->p_special); 23924647Smckusick exit(1); 24024647Smckusick } 24124647Smckusick break; 24224647Smckusick 24324647Smckusick case PRO_BOOL: 24424647Smckusick if (p->p_special == OFF) 24524647Smckusick *p->p_obj = false; 24624647Smckusick else 24724647Smckusick *p->p_obj = true; 24824647Smckusick break; 24924647Smckusick 25024647Smckusick case PRO_INT: 25124647Smckusick if (*param_start == 0) { 25224647Smckusick need_param: 25324647Smckusick fprintf(stderr, "indent: ``%s'' requires a parameter\n", 25424647Smckusick arg - 1); 25524647Smckusick exit(1); 25624647Smckusick } 25724647Smckusick *p->p_obj = atoi(param_start); 25824647Smckusick break; 25924647Smckusick 26024647Smckusick default: 26124647Smckusick fprintf(stderr, "indent: set_option: internal error: p_type %d\n", 26224647Smckusick p->p_type); 26324647Smckusick exit(1); 26424647Smckusick } 26524647Smckusick } 266