124647Smckusick /* 235500Sbostic * Copyright (c) 1985 Sun Microsystems, Inc. 335500Sbostic * Copyright (c) 1980 The Regents of the University of California. 433767Sbostic * Copyright (c) 1976 Board of Trustees of the University of Illinois. 533767Sbostic * All rights reserved. 633767Sbostic * 733767Sbostic * Redistribution and use in source and binary forms are permitted 834885Sbostic * provided that the above copyright notice and this paragraph are 934885Sbostic * duplicated in all such forms and that any documentation, 1034885Sbostic * advertising materials, and other materials related to such 1134885Sbostic * distribution and use acknowledge that the software was developed 1235500Sbostic * by the University of California, Berkeley, the University of Illinois, 1335500Sbostic * Urbana, and Sun Microsystems, Inc. The name of either University 1435500Sbostic * or Sun Microsystems may not be used to endorse or promote products 1535500Sbostic * derived from this software without specific prior written permission. 1634885Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1734885Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1834885Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1924647Smckusick */ 2024647Smckusick 2124647Smckusick #ifndef lint 22*35508Sbostic static char sccsid[] = "@(#)args.c 5.6 (Berkeley) 09/15/88"; 2333767Sbostic #endif /* not lint */ 2424647Smckusick 2524647Smckusick /* 2635500Sbostic * Argument scanning and profile reading code. Default parameters are set 2735500Sbostic * here as well. 2824647Smckusick */ 2924647Smckusick 3024647Smckusick #include "indent_globs.h" 3124647Smckusick #include <sys/types.h> 3224647Smckusick #include <ctype.h> 3324647Smckusick 3435500Sbostic char *getenv(), *index(); 3524647Smckusick 3624647Smckusick /* profile types */ 3724647Smckusick #define PRO_SPECIAL 1 /* special case */ 3824647Smckusick #define PRO_BOOL 2 /* boolean */ 3924647Smckusick #define PRO_INT 3 /* integer */ 4035500Sbostic #define PRO_FONT 4 /* troff font */ 4124647Smckusick 4224647Smckusick /* profile specials for booleans */ 4324647Smckusick #define ON 1 /* turn it on */ 4424647Smckusick #define OFF 0 /* turn it off */ 4524647Smckusick 4624647Smckusick /* profile specials for specials */ 4724647Smckusick #define IGN 1 /* ignore it */ 4824647Smckusick #define CLI 2 /* case label indent (float) */ 4924647Smckusick #define STDIN 3 /* use stdin */ 5024647Smckusick #define KEY 4 /* type (keyword) */ 5124647Smckusick 5224647Smckusick /* 5335500Sbostic * N.B.: because of the way the table here is scanned, options whose names are 5435500Sbostic * substrings of other options must occur later; that is, with -lp vs -l, -lp 5535500Sbostic * must be first. Also, while (most) booleans occur more than once, the last 5635500Sbostic * default value is the one actually assigned. 5724647Smckusick */ 5824647Smckusick struct pro { 5935500Sbostic char *p_name; /* name, eg -bl, -cli */ 6035500Sbostic int p_type; /* type (int, bool, special) */ 6135500Sbostic int p_default; /* the default value (if int) */ 6235500Sbostic int p_special; /* depends on type */ 6335500Sbostic int *p_obj; /* the associated variable */ 6435500Sbostic } pro[] = { 6535500Sbostic 6635500Sbostic "T", PRO_SPECIAL, 0, KEY, 0, 6735500Sbostic "bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation, 6835500Sbostic "badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop, 6935500Sbostic "bad", PRO_BOOL, false, ON, &blanklines_after_declarations, 7035500Sbostic "bap", PRO_BOOL, false, ON, &blanklines_after_procs, 7135500Sbostic "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments, 7235500Sbostic "bc", PRO_BOOL, true, OFF, &ps.leave_comma, 7335500Sbostic "bl", PRO_BOOL, true, OFF, &btype_2, 7435500Sbostic "br", PRO_BOOL, true, ON, &btype_2, 7535500Sbostic "bs", PRO_BOOL, false, ON, &Bill_Shannon, 7635500Sbostic "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline, 7735500Sbostic "cd", PRO_INT, 0, 0, &ps.decl_com_ind, 7835500Sbostic "ce", PRO_BOOL, true, ON, &cuddle_else, 7935500Sbostic "ci", PRO_INT, 0, 0, &continuation_indent, 8035500Sbostic "cli", PRO_SPECIAL, 0, CLI, 0, 8135500Sbostic "c", PRO_INT, 33, 0, &ps.com_ind, 8235500Sbostic "di", PRO_INT, 16, 0, &ps.decl_indent, 8335500Sbostic "dj", PRO_BOOL, false, ON, &ps.ljust_decl, 8435500Sbostic "d", PRO_INT, 0, 0, &ps.unindent_displace, 8535500Sbostic "eei", PRO_BOOL, false, ON, &extra_expression_indent, 8635500Sbostic "ei", PRO_BOOL, true, ON, &ps.else_if, 8735500Sbostic "fbc", PRO_FONT, 0, 0, (int *) &blkcomf, 8835500Sbostic "fbx", PRO_FONT, 0, 0, (int *) &boxcomf, 8935500Sbostic "fb", PRO_FONT, 0, 0, (int *) &bodyf, 9035500Sbostic "fc1", PRO_BOOL, true, ON, &format_col1_comments, 9135500Sbostic "fc", PRO_FONT, 0, 0, (int *) &scomf, 9235500Sbostic "fk", PRO_FONT, 0, 0, (int *) &keywordf, 9335500Sbostic "fs", PRO_FONT, 0, 0, (int *) &stringf, 9435500Sbostic "ip", PRO_BOOL, true, ON, &ps.indent_parameters, 9535500Sbostic "i", PRO_INT, 8, 0, &ps.ind_size, 9635500Sbostic "lc", PRO_INT, 0, 0, &block_comment_max_col, 9735500Sbostic "lp", PRO_BOOL, true, ON, &lineup_to_parens, 9835500Sbostic "l", PRO_INT, 78, 0, &max_col, 9935500Sbostic "nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation, 10035500Sbostic "nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop, 10135500Sbostic "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations, 10235500Sbostic "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs, 10335500Sbostic "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments, 10435500Sbostic "nbc", PRO_BOOL, true, ON, &ps.leave_comma, 10535500Sbostic "nbs", PRO_BOOL, false, OFF, &Bill_Shannon, 10635500Sbostic "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline, 10735500Sbostic "nce", PRO_BOOL, true, OFF, &cuddle_else, 10835500Sbostic "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl, 10935500Sbostic "neei", PRO_BOOL, false, OFF, &extra_expression_indent, 11035500Sbostic "nei", PRO_BOOL, true, OFF, &ps.else_if, 11135500Sbostic "nfc1", PRO_BOOL, true, OFF, &format_col1_comments, 11235500Sbostic "nip", PRO_BOOL, true, OFF, &ps.indent_parameters, 11335500Sbostic "nlp", PRO_BOOL, true, OFF, &lineup_to_parens, 11435500Sbostic "npcs", PRO_BOOL, false, OFF, &proc_calls_space, 11535500Sbostic "npro", PRO_SPECIAL, 0, IGN, 0, 11635500Sbostic "npsl", PRO_BOOL, true, OFF, &procnames_start_line, 11735500Sbostic "nps", PRO_BOOL, false, OFF, &pointer_as_binop, 11835500Sbostic "nsc", PRO_BOOL, true, OFF, &star_comment_cont, 11935500Sbostic "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines, 12035500Sbostic "nv", PRO_BOOL, false, OFF, &verbose, 12135500Sbostic "pcs", PRO_BOOL, false, ON, &proc_calls_space, 12235500Sbostic "psl", PRO_BOOL, true, ON, &procnames_start_line, 12335500Sbostic "ps", PRO_BOOL, false, ON, &pointer_as_binop, 12435500Sbostic "sc", PRO_BOOL, true, ON, &star_comment_cont, 12535500Sbostic "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines, 12635500Sbostic "st", PRO_SPECIAL, 0, STDIN, 0, 12735500Sbostic "troff", PRO_BOOL, false, ON, &troff, 12835500Sbostic "v", PRO_BOOL, false, ON, &verbose, 12935500Sbostic /* whew! */ 13035500Sbostic 0, 0, 0, 0, 0 13124647Smckusick }; 13224647Smckusick 13324647Smckusick /* 13435500Sbostic * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments 13535500Sbostic * given in these files. 13624647Smckusick */ 13724647Smckusick set_profile() 13824647Smckusick { 13924647Smckusick register FILE *f; 14035500Sbostic char fname[BUFSIZ]; 141*35508Sbostic static char prof[] = ".indent.pro"; 14224647Smckusick 143*35508Sbostic sprintf(fname, "%s/%s", getenv("HOME"), prof); 14424647Smckusick if ((f = fopen(fname, "r")) != NULL) { 14524647Smckusick scan_profile(f); 14624647Smckusick (void) fclose(f); 14724647Smckusick } 148*35508Sbostic if ((f = fopen(prof, "r")) != NULL) { 14924647Smckusick scan_profile(f); 15024647Smckusick (void) fclose(f); 15124647Smckusick } 15224647Smckusick } 15324647Smckusick 15424647Smckusick scan_profile(f) 15524647Smckusick register FILE *f; 15624647Smckusick { 157*35508Sbostic register int i; 15835500Sbostic register char *p; 15935500Sbostic char buf[BUFSIZ]; 16024647Smckusick 16135500Sbostic while (1) { 162*35508Sbostic for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p); 16335500Sbostic if (p != buf) { 16435500Sbostic *p++ = 0; 16535500Sbostic if (verbose) 16635500Sbostic printf("profile: %s\n", buf); 16735500Sbostic set_option(buf); 16824647Smckusick } 169*35508Sbostic else if (i == EOF) 17035500Sbostic return; 17124647Smckusick } 17224647Smckusick } 17324647Smckusick 17424647Smckusick char *param_start; 17524647Smckusick 17624647Smckusick eqin(s1, s2) 17724647Smckusick register char *s1; 17824647Smckusick register char *s2; 17924647Smckusick { 18024647Smckusick while (*s1) { 18124647Smckusick if (*s1++ != *s2++) 18224647Smckusick return (false); 18324647Smckusick } 18424647Smckusick param_start = s2; 18524647Smckusick return (true); 18624647Smckusick } 18724647Smckusick 18824647Smckusick /* 18924647Smckusick * Set the defaults. 19024647Smckusick */ 19124647Smckusick set_defaults() 19224647Smckusick { 19324647Smckusick register struct pro *p; 19424647Smckusick 19524647Smckusick /* 19635500Sbostic * Because ps.case_indent is a float, we can't initialize it from the 19735500Sbostic * table: 19824647Smckusick */ 19924647Smckusick ps.case_indent = 0.0; /* -cli0.0 */ 20024647Smckusick for (p = pro; p->p_name; p++) 20135500Sbostic if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT) 20224647Smckusick *p->p_obj = p->p_default; 20324647Smckusick } 20424647Smckusick 20524647Smckusick set_option(arg) 20624647Smckusick register char *arg; 20724647Smckusick { 20824647Smckusick register struct pro *p; 20924647Smckusick extern double atof(); 21024647Smckusick 21124647Smckusick arg++; /* ignore leading "-" */ 21224647Smckusick for (p = pro; p->p_name; p++) 21324647Smckusick if (*p->p_name == *arg && eqin(p->p_name, arg)) 21424647Smckusick goto found; 21524647Smckusick fprintf(stderr, "indent: unknown parameter \"%s\"\n", arg - 1); 21624647Smckusick exit(1); 21724647Smckusick found: 21824647Smckusick switch (p->p_type) { 21924647Smckusick 22035500Sbostic case PRO_SPECIAL: 22135500Sbostic switch (p->p_special) { 22224647Smckusick 22335500Sbostic case IGN: 22435500Sbostic break; 22524647Smckusick 22635500Sbostic case CLI: 22735500Sbostic if (*param_start == 0) 22835500Sbostic goto need_param; 22935500Sbostic ps.case_indent = atof(param_start); 23024647Smckusick break; 23124647Smckusick 23235500Sbostic case STDIN: 23335500Sbostic if (input == 0) 23435500Sbostic input = stdin; 23535500Sbostic if (output == 0) 23635500Sbostic output = stdout; 23724647Smckusick break; 23824647Smckusick 23935500Sbostic case KEY: 24035500Sbostic if (*param_start == 0) 24135500Sbostic goto need_param; 24235500Sbostic { 24335500Sbostic register char *str = (char *) malloc(strlen(param_start) + 1); 24435500Sbostic strcpy(str, param_start); 24535500Sbostic addkey(str, 4); 24624647Smckusick } 24724647Smckusick break; 24824647Smckusick 24924647Smckusick default: 25035500Sbostic fprintf(stderr, "\ 25135500Sbostic indent: set_option: internal error: p_special %d\n", p->p_special); 25224647Smckusick exit(1); 25335500Sbostic } 25435500Sbostic break; 25535500Sbostic 25635500Sbostic case PRO_BOOL: 25735500Sbostic if (p->p_special == OFF) 25835500Sbostic *p->p_obj = false; 25935500Sbostic else 26035500Sbostic *p->p_obj = true; 26135500Sbostic break; 26235500Sbostic 26335500Sbostic case PRO_INT: 26435500Sbostic if (*param_start == 0) { 26535500Sbostic need_param: 26635500Sbostic fprintf(stderr, "indent: ``%s'' requires a parameter\n", 26735500Sbostic arg - 1); 26835500Sbostic exit(1); 26935500Sbostic } 27035500Sbostic *p->p_obj = atoi(param_start); 27135500Sbostic break; 27235500Sbostic 27335500Sbostic case PRO_FONT: 27435500Sbostic parsefont((struct fstate *) p->p_obj, param_start); 27535500Sbostic break; 27635500Sbostic 27735500Sbostic default: 27835500Sbostic fprintf(stderr, "indent: set_option: internal error: p_type %d\n", 27935500Sbostic p->p_type); 28035500Sbostic exit(1); 28124647Smckusick } 28224647Smckusick } 283