124647Smckusick /* 2*35500Sbostic * Copyright (c) 1985 Sun Microsystems, Inc. 3*35500Sbostic * 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 12*35500Sbostic * by the University of California, Berkeley, the University of Illinois, 13*35500Sbostic * Urbana, and Sun Microsystems, Inc. The name of either University 14*35500Sbostic * or Sun Microsystems may not be used to endorse or promote products 15*35500Sbostic * 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*35500Sbostic static char sccsid[] = "@(#)args.c 5.5 (Berkeley) 09/15/88"; 2333767Sbostic #endif /* not lint */ 2424647Smckusick 2524647Smckusick /* 26*35500Sbostic * Argument scanning and profile reading code. Default parameters are set 27*35500Sbostic * here as well. 2824647Smckusick */ 2924647Smckusick 3024647Smckusick #include "indent_globs.h" 3124647Smckusick #include <sys/types.h> 3224647Smckusick #include <ctype.h> 3324647Smckusick 34*35500Sbostic 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 */ 40*35500Sbostic #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 /* 53*35500Sbostic * N.B.: because of the way the table here is scanned, options whose names are 54*35500Sbostic * substrings of other options must occur later; that is, with -lp vs -l, -lp 55*35500Sbostic * must be first. Also, while (most) booleans occur more than once, the last 56*35500Sbostic * default value is the one actually assigned. 5724647Smckusick */ 5824647Smckusick struct pro { 59*35500Sbostic char *p_name; /* name, eg -bl, -cli */ 60*35500Sbostic int p_type; /* type (int, bool, special) */ 61*35500Sbostic int p_default; /* the default value (if int) */ 62*35500Sbostic int p_special; /* depends on type */ 63*35500Sbostic int *p_obj; /* the associated variable */ 64*35500Sbostic } pro[] = { 65*35500Sbostic 66*35500Sbostic "T", PRO_SPECIAL, 0, KEY, 0, 67*35500Sbostic "bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation, 68*35500Sbostic "badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop, 69*35500Sbostic "bad", PRO_BOOL, false, ON, &blanklines_after_declarations, 70*35500Sbostic "bap", PRO_BOOL, false, ON, &blanklines_after_procs, 71*35500Sbostic "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments, 72*35500Sbostic "bc", PRO_BOOL, true, OFF, &ps.leave_comma, 73*35500Sbostic "bl", PRO_BOOL, true, OFF, &btype_2, 74*35500Sbostic "br", PRO_BOOL, true, ON, &btype_2, 75*35500Sbostic "bs", PRO_BOOL, false, ON, &Bill_Shannon, 76*35500Sbostic "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline, 77*35500Sbostic "cd", PRO_INT, 0, 0, &ps.decl_com_ind, 78*35500Sbostic "ce", PRO_BOOL, true, ON, &cuddle_else, 79*35500Sbostic "ci", PRO_INT, 0, 0, &continuation_indent, 80*35500Sbostic "cli", PRO_SPECIAL, 0, CLI, 0, 81*35500Sbostic "c", PRO_INT, 33, 0, &ps.com_ind, 82*35500Sbostic "di", PRO_INT, 16, 0, &ps.decl_indent, 83*35500Sbostic "dj", PRO_BOOL, false, ON, &ps.ljust_decl, 84*35500Sbostic "d", PRO_INT, 0, 0, &ps.unindent_displace, 85*35500Sbostic "eei", PRO_BOOL, false, ON, &extra_expression_indent, 86*35500Sbostic "ei", PRO_BOOL, true, ON, &ps.else_if, 87*35500Sbostic "fbc", PRO_FONT, 0, 0, (int *) &blkcomf, 88*35500Sbostic "fbx", PRO_FONT, 0, 0, (int *) &boxcomf, 89*35500Sbostic "fb", PRO_FONT, 0, 0, (int *) &bodyf, 90*35500Sbostic "fc1", PRO_BOOL, true, ON, &format_col1_comments, 91*35500Sbostic "fc", PRO_FONT, 0, 0, (int *) &scomf, 92*35500Sbostic "fk", PRO_FONT, 0, 0, (int *) &keywordf, 93*35500Sbostic "fs", PRO_FONT, 0, 0, (int *) &stringf, 94*35500Sbostic "ip", PRO_BOOL, true, ON, &ps.indent_parameters, 95*35500Sbostic "i", PRO_INT, 8, 0, &ps.ind_size, 96*35500Sbostic "lc", PRO_INT, 0, 0, &block_comment_max_col, 97*35500Sbostic "lp", PRO_BOOL, true, ON, &lineup_to_parens, 98*35500Sbostic "l", PRO_INT, 78, 0, &max_col, 99*35500Sbostic "nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation, 100*35500Sbostic "nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop, 101*35500Sbostic "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations, 102*35500Sbostic "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs, 103*35500Sbostic "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments, 104*35500Sbostic "nbc", PRO_BOOL, true, ON, &ps.leave_comma, 105*35500Sbostic "nbs", PRO_BOOL, false, OFF, &Bill_Shannon, 106*35500Sbostic "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline, 107*35500Sbostic "nce", PRO_BOOL, true, OFF, &cuddle_else, 108*35500Sbostic "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl, 109*35500Sbostic "neei", PRO_BOOL, false, OFF, &extra_expression_indent, 110*35500Sbostic "nei", PRO_BOOL, true, OFF, &ps.else_if, 111*35500Sbostic "nfc1", PRO_BOOL, true, OFF, &format_col1_comments, 112*35500Sbostic "nip", PRO_BOOL, true, OFF, &ps.indent_parameters, 113*35500Sbostic "nlp", PRO_BOOL, true, OFF, &lineup_to_parens, 114*35500Sbostic "npcs", PRO_BOOL, false, OFF, &proc_calls_space, 115*35500Sbostic "npro", PRO_SPECIAL, 0, IGN, 0, 116*35500Sbostic "npsl", PRO_BOOL, true, OFF, &procnames_start_line, 117*35500Sbostic "nps", PRO_BOOL, false, OFF, &pointer_as_binop, 118*35500Sbostic "nsc", PRO_BOOL, true, OFF, &star_comment_cont, 119*35500Sbostic "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines, 120*35500Sbostic "nv", PRO_BOOL, false, OFF, &verbose, 121*35500Sbostic "pcs", PRO_BOOL, false, ON, &proc_calls_space, 122*35500Sbostic "psl", PRO_BOOL, true, ON, &procnames_start_line, 123*35500Sbostic "ps", PRO_BOOL, false, ON, &pointer_as_binop, 124*35500Sbostic "sc", PRO_BOOL, true, ON, &star_comment_cont, 125*35500Sbostic "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines, 126*35500Sbostic "st", PRO_SPECIAL, 0, STDIN, 0, 127*35500Sbostic "troff", PRO_BOOL, false, ON, &troff, 128*35500Sbostic "v", PRO_BOOL, false, ON, &verbose, 129*35500Sbostic /* whew! */ 130*35500Sbostic 0, 0, 0, 0, 0 13124647Smckusick }; 13224647Smckusick 13324647Smckusick /* 134*35500Sbostic * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments 135*35500Sbostic * given in these files. 13624647Smckusick */ 13724647Smckusick set_profile() 13824647Smckusick { 13924647Smckusick register FILE *f; 140*35500Sbostic char fname[BUFSIZ]; 14124647Smckusick static char pro[] = ".indent.pro"; 14224647Smckusick 14324647Smckusick sprintf(fname, "%s/%s", getenv("HOME"), pro); 14424647Smckusick if ((f = fopen(fname, "r")) != NULL) { 14524647Smckusick scan_profile(f); 14624647Smckusick (void) fclose(f); 14724647Smckusick } 14824647Smckusick if ((f = fopen(pro, "r")) != NULL) { 14924647Smckusick scan_profile(f); 15024647Smckusick (void) fclose(f); 15124647Smckusick } 15224647Smckusick } 15324647Smckusick 15424647Smckusick scan_profile(f) 15524647Smckusick register FILE *f; 15624647Smckusick { 157*35500Sbostic register char *p; 158*35500Sbostic char buf[BUFSIZ]; 15924647Smckusick 160*35500Sbostic while (1) { 16124647Smckusick p = buf; 162*35500Sbostic while ((*p = getc(f)) != EOF && *p > ' ') 163*35500Sbostic p++; 164*35500Sbostic if (p != buf) { 165*35500Sbostic *p++ = 0; 166*35500Sbostic if (verbose) 167*35500Sbostic printf("profile: %s\n", buf); 168*35500Sbostic set_option(buf); 16924647Smckusick } 170*35500Sbostic else if (*p == EOF) 171*35500Sbostic return; 17224647Smckusick } 17324647Smckusick } 17424647Smckusick 17524647Smckusick char *param_start; 17624647Smckusick 17724647Smckusick eqin(s1, s2) 17824647Smckusick register char *s1; 17924647Smckusick register char *s2; 18024647Smckusick { 18124647Smckusick while (*s1) { 18224647Smckusick if (*s1++ != *s2++) 18324647Smckusick return (false); 18424647Smckusick } 18524647Smckusick param_start = s2; 18624647Smckusick return (true); 18724647Smckusick } 18824647Smckusick 18924647Smckusick /* 19024647Smckusick * Set the defaults. 19124647Smckusick */ 19224647Smckusick set_defaults() 19324647Smckusick { 19424647Smckusick register struct pro *p; 19524647Smckusick 19624647Smckusick /* 197*35500Sbostic * Because ps.case_indent is a float, we can't initialize it from the 198*35500Sbostic * table: 19924647Smckusick */ 20024647Smckusick ps.case_indent = 0.0; /* -cli0.0 */ 20124647Smckusick for (p = pro; p->p_name; p++) 202*35500Sbostic if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT) 20324647Smckusick *p->p_obj = p->p_default; 20424647Smckusick } 20524647Smckusick 20624647Smckusick set_option(arg) 20724647Smckusick register char *arg; 20824647Smckusick { 20924647Smckusick register struct pro *p; 21024647Smckusick extern double atof(); 21124647Smckusick 21224647Smckusick arg++; /* ignore leading "-" */ 21324647Smckusick for (p = pro; p->p_name; p++) 21424647Smckusick if (*p->p_name == *arg && eqin(p->p_name, arg)) 21524647Smckusick goto found; 21624647Smckusick fprintf(stderr, "indent: unknown parameter \"%s\"\n", arg - 1); 21724647Smckusick exit(1); 21824647Smckusick found: 21924647Smckusick switch (p->p_type) { 22024647Smckusick 221*35500Sbostic case PRO_SPECIAL: 222*35500Sbostic switch (p->p_special) { 22324647Smckusick 224*35500Sbostic case IGN: 225*35500Sbostic break; 22624647Smckusick 227*35500Sbostic case CLI: 228*35500Sbostic if (*param_start == 0) 229*35500Sbostic goto need_param; 230*35500Sbostic ps.case_indent = atof(param_start); 23124647Smckusick break; 23224647Smckusick 233*35500Sbostic case STDIN: 234*35500Sbostic if (input == 0) 235*35500Sbostic input = stdin; 236*35500Sbostic if (output == 0) 237*35500Sbostic output = stdout; 23824647Smckusick break; 23924647Smckusick 240*35500Sbostic case KEY: 241*35500Sbostic if (*param_start == 0) 242*35500Sbostic goto need_param; 243*35500Sbostic { 244*35500Sbostic register char *str = (char *) malloc(strlen(param_start) + 1); 245*35500Sbostic strcpy(str, param_start); 246*35500Sbostic addkey(str, 4); 24724647Smckusick } 24824647Smckusick break; 24924647Smckusick 25024647Smckusick default: 251*35500Sbostic fprintf(stderr, "\ 252*35500Sbostic indent: set_option: internal error: p_special %d\n", p->p_special); 25324647Smckusick exit(1); 254*35500Sbostic } 255*35500Sbostic break; 256*35500Sbostic 257*35500Sbostic case PRO_BOOL: 258*35500Sbostic if (p->p_special == OFF) 259*35500Sbostic *p->p_obj = false; 260*35500Sbostic else 261*35500Sbostic *p->p_obj = true; 262*35500Sbostic break; 263*35500Sbostic 264*35500Sbostic case PRO_INT: 265*35500Sbostic if (*param_start == 0) { 266*35500Sbostic need_param: 267*35500Sbostic fprintf(stderr, "indent: ``%s'' requires a parameter\n", 268*35500Sbostic arg - 1); 269*35500Sbostic exit(1); 270*35500Sbostic } 271*35500Sbostic *p->p_obj = atoi(param_start); 272*35500Sbostic break; 273*35500Sbostic 274*35500Sbostic case PRO_FONT: 275*35500Sbostic parsefont((struct fstate *) p->p_obj, param_start); 276*35500Sbostic break; 277*35500Sbostic 278*35500Sbostic default: 279*35500Sbostic fprintf(stderr, "indent: set_option: internal error: p_type %d\n", 280*35500Sbostic p->p_type); 281*35500Sbostic exit(1); 28224647Smckusick } 28324647Smckusick } 284