Annotation of 43BSDReno/pgrm/indent/args.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1985 Sun Microsystems, Inc.
        !             3:  * Copyright (c) 1980 The Regents of the University of California.
        !             4:  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
        !             5:  * All rights reserved.
        !             6:  *
        !             7:  * Redistribution and use in source and binary forms are permitted
        !             8:  * provided that: (1) source distributions retain this entire copyright
        !             9:  * notice and comment, and (2) distributions including binaries display
        !            10:  * the following acknowledgement:  ``This product includes software
        !            11:  * developed by the University of California, Berkeley and its contributors''
        !            12:  * in the documentation or other materials provided with the distribution
        !            13:  * and in all advertising materials mentioning features or use of this
        !            14:  * software. Neither the name of the University nor the names of its
        !            15:  * contributors may be used to endorse or promote products derived
        !            16:  * from this software without specific prior written permission.
        !            17:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            18:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            19:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            20:  */
        !            21: 
        !            22: #ifndef lint
        !            23: static char sccsid[] = "@(#)args.c     5.9 (Berkeley) 6/1/90";
        !            24: #endif /* not lint */
        !            25: 
        !            26: /*
        !            27:  * Argument scanning and profile reading code.  Default parameters are set
        !            28:  * here as well.
        !            29:  */
        !            30: 
        !            31: #include "indent_globs.h"
        !            32: #include <ctype.h>
        !            33: 
        !            34: char       *getenv(), *index();
        !            35: 
        !            36: /* profile types */
        !            37: #define        PRO_SPECIAL     1       /* special case */
        !            38: #define        PRO_BOOL        2       /* boolean */
        !            39: #define        PRO_INT         3       /* integer */
        !            40: #define PRO_FONT       4       /* troff font */
        !            41: 
        !            42: /* profile specials for booleans */
        !            43: #define        ON              1       /* turn it on */
        !            44: #define        OFF             0       /* turn it off */
        !            45: 
        !            46: /* profile specials for specials */
        !            47: #define        IGN             1       /* ignore it */
        !            48: #define        CLI             2       /* case label indent (float) */
        !            49: #define        STDIN           3       /* use stdin */
        !            50: #define        KEY             4       /* type (keyword) */
        !            51: 
        !            52: char *option_source = "?";
        !            53: 
        !            54: /*
        !            55:  * N.B.: because of the way the table here is scanned, options whose names are
        !            56:  * substrings of other options must occur later; that is, with -lp vs -l, -lp
        !            57:  * must be first.  Also, while (most) booleans occur more than once, the last
        !            58:  * default value is the one actually assigned.
        !            59:  */
        !            60: struct pro {
        !            61:     char       *p_name;                /* name, eg -bl, -cli */
        !            62:     int         p_type;                /* type (int, bool, special) */
        !            63:     int         p_default;     /* the default value (if int) */
        !            64:     int         p_special;     /* depends on type */
        !            65:     int        *p_obj;         /* the associated variable */
        !            66: }           pro[] = {
        !            67: 
        !            68:     "T", PRO_SPECIAL, 0, KEY, 0,
        !            69:     "bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation,
        !            70:     "badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop,
        !            71:     "bad", PRO_BOOL, false, ON, &blanklines_after_declarations,
        !            72:     "bap", PRO_BOOL, false, ON, &blanklines_after_procs,
        !            73:     "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments,
        !            74:     "bc", PRO_BOOL, true, OFF, &ps.leave_comma,
        !            75:     "bl", PRO_BOOL, true, OFF, &btype_2,
        !            76:     "br", PRO_BOOL, true, ON, &btype_2,
        !            77:     "bs", PRO_BOOL, false, ON, &Bill_Shannon,
        !            78:     "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline,
        !            79:     "cd", PRO_INT, 0, 0, &ps.decl_com_ind,
        !            80:     "ce", PRO_BOOL, true, ON, &cuddle_else,
        !            81:     "ci", PRO_INT, 0, 0, &continuation_indent,
        !            82:     "cli", PRO_SPECIAL, 0, CLI, 0,
        !            83:     "c", PRO_INT, 33, 0, &ps.com_ind,
        !            84:     "di", PRO_INT, 16, 0, &ps.decl_indent,
        !            85:     "dj", PRO_BOOL, false, ON, &ps.ljust_decl,
        !            86:     "d", PRO_INT, 0, 0, &ps.unindent_displace,
        !            87:     "eei", PRO_BOOL, false, ON, &extra_expression_indent,
        !            88:     "ei", PRO_BOOL, true, ON, &ps.else_if,
        !            89:     "fbc", PRO_FONT, 0, 0, (int *) &blkcomf,
        !            90:     "fbx", PRO_FONT, 0, 0, (int *) &boxcomf,
        !            91:     "fb", PRO_FONT, 0, 0, (int *) &bodyf,
        !            92:     "fc1", PRO_BOOL, true, ON, &format_col1_comments,
        !            93:     "fc", PRO_FONT, 0, 0, (int *) &scomf,
        !            94:     "fk", PRO_FONT, 0, 0, (int *) &keywordf,
        !            95:     "fs", PRO_FONT, 0, 0, (int *) &stringf,
        !            96:     "ip", PRO_BOOL, true, ON, &ps.indent_parameters,
        !            97:     "i", PRO_INT, 8, 0, &ps.ind_size,
        !            98:     "lc", PRO_INT, 0, 0, &block_comment_max_col,
        !            99:     "lp", PRO_BOOL, true, ON, &lineup_to_parens,
        !           100:     "l", PRO_INT, 78, 0, &max_col,
        !           101:     "nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation,
        !           102:     "nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop,
        !           103:     "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations,
        !           104:     "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs,
        !           105:     "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments,
        !           106:     "nbc", PRO_BOOL, true, ON, &ps.leave_comma,
        !           107:     "nbs", PRO_BOOL, false, OFF, &Bill_Shannon,
        !           108:     "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline,
        !           109:     "nce", PRO_BOOL, true, OFF, &cuddle_else,
        !           110:     "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl,
        !           111:     "neei", PRO_BOOL, false, OFF, &extra_expression_indent,
        !           112:     "nei", PRO_BOOL, true, OFF, &ps.else_if,
        !           113:     "nfc1", PRO_BOOL, true, OFF, &format_col1_comments,
        !           114:     "nip", PRO_BOOL, true, OFF, &ps.indent_parameters,
        !           115:     "nlp", PRO_BOOL, true, OFF, &lineup_to_parens,
        !           116:     "npcs", PRO_BOOL, false, OFF, &proc_calls_space,
        !           117:     "npro", PRO_SPECIAL, 0, IGN, 0,
        !           118:     "npsl", PRO_BOOL, true, OFF, &procnames_start_line,
        !           119:     "nps", PRO_BOOL, false, OFF, &pointer_as_binop,
        !           120:     "nsc", PRO_BOOL, true, OFF, &star_comment_cont,
        !           121:     "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines,
        !           122:     "nv", PRO_BOOL, false, OFF, &verbose,
        !           123:     "pcs", PRO_BOOL, false, ON, &proc_calls_space,
        !           124:     "psl", PRO_BOOL, true, ON, &procnames_start_line,
        !           125:     "ps", PRO_BOOL, false, ON, &pointer_as_binop,
        !           126:     "sc", PRO_BOOL, true, ON, &star_comment_cont,
        !           127:     "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines,
        !           128:     "st", PRO_SPECIAL, 0, STDIN, 0,
        !           129:     "troff", PRO_BOOL, false, ON, &troff,
        !           130:     "v", PRO_BOOL, false, ON, &verbose,
        !           131:     /* whew! */
        !           132:     0, 0, 0, 0, 0
        !           133: };
        !           134: 
        !           135: /*
        !           136:  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
        !           137:  * given in these files.
        !           138:  */
        !           139: set_profile()
        !           140: {
        !           141:     register FILE *f;
        !           142:     char        fname[BUFSIZ];
        !           143:     static char prof[] = ".indent.pro";
        !           144: 
        !           145:     sprintf(fname, "%s/%s", getenv("HOME"), prof);
        !           146:     if ((f = fopen(option_source = fname, "r")) != NULL) {
        !           147:        scan_profile(f);
        !           148:        (void) fclose(f);
        !           149:     }
        !           150:     if ((f = fopen(option_source = prof, "r")) != NULL) {
        !           151:        scan_profile(f);
        !           152:        (void) fclose(f);
        !           153:     }
        !           154:     option_source = "Command line";
        !           155: }
        !           156: 
        !           157: scan_profile(f)
        !           158:     register FILE *f;
        !           159: {
        !           160:     register int i;
        !           161:     register char *p;
        !           162:     char        buf[BUFSIZ];
        !           163: 
        !           164:     while (1) {
        !           165:        for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p);
        !           166:        if (p != buf) {
        !           167:            *p++ = 0;
        !           168:            if (verbose)
        !           169:                printf("profile: %s\n", buf);
        !           170:            set_option(buf);
        !           171:        }
        !           172:        else if (i == EOF)
        !           173:            return;
        !           174:     }
        !           175: }
        !           176: 
        !           177: char       *param_start;
        !           178: 
        !           179: eqin(s1, s2)
        !           180:     register char *s1;
        !           181:     register char *s2;
        !           182: {
        !           183:     while (*s1) {
        !           184:        if (*s1++ != *s2++)
        !           185:            return (false);
        !           186:     }
        !           187:     param_start = s2;
        !           188:     return (true);
        !           189: }
        !           190: 
        !           191: /*
        !           192:  * Set the defaults.
        !           193:  */
        !           194: set_defaults()
        !           195: {
        !           196:     register struct pro *p;
        !           197: 
        !           198:     /*
        !           199:      * Because ps.case_indent is a float, we can't initialize it from the
        !           200:      * table:
        !           201:      */
        !           202:     ps.case_indent = 0.0;      /* -cli0.0 */
        !           203:     for (p = pro; p->p_name; p++)
        !           204:        if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
        !           205:            *p->p_obj = p->p_default;
        !           206: }
        !           207: 
        !           208: set_option(arg)
        !           209:     register char *arg;
        !           210: {
        !           211:     register struct pro *p;
        !           212:     extern double atof();
        !           213: 
        !           214:     arg++;                     /* ignore leading "-" */
        !           215:     for (p = pro; p->p_name; p++)
        !           216:        if (*p->p_name == *arg && eqin(p->p_name, arg))
        !           217:            goto found;
        !           218:     fprintf(stderr, "indent: %s: unknown parameter \"%s\"\n", option_source, arg - 1);
        !           219:     exit(1);
        !           220: found:
        !           221:     switch (p->p_type) {
        !           222: 
        !           223:     case PRO_SPECIAL:
        !           224:        switch (p->p_special) {
        !           225: 
        !           226:        case IGN:
        !           227:            break;
        !           228: 
        !           229:        case CLI:
        !           230:            if (*param_start == 0)
        !           231:                goto need_param;
        !           232:            ps.case_indent = atof(param_start);
        !           233:            break;
        !           234: 
        !           235:        case STDIN:
        !           236:            if (input == 0)
        !           237:                input = stdin;
        !           238:            if (output == 0)
        !           239:                output = stdout;
        !           240:            break;
        !           241: 
        !           242:        case KEY:
        !           243:            if (*param_start == 0)
        !           244:                goto need_param;
        !           245:            {
        !           246:                register char *str = (char *) malloc(strlen(param_start) + 1);
        !           247:                strcpy(str, param_start);
        !           248:                addkey(str, 4);
        !           249:            }
        !           250:            break;
        !           251: 
        !           252:        default:
        !           253:            fprintf(stderr, "\
        !           254: indent: set_option: internal error: p_special %d\n", p->p_special);
        !           255:            exit(1);
        !           256:        }
        !           257:        break;
        !           258: 
        !           259:     case PRO_BOOL:
        !           260:        if (p->p_special == OFF)
        !           261:            *p->p_obj = false;
        !           262:        else
        !           263:            *p->p_obj = true;
        !           264:        break;
        !           265: 
        !           266:     case PRO_INT:
        !           267:        if (!isdigit(*param_start)) {
        !           268:     need_param:
        !           269:            fprintf(stderr, "indent: %s: ``%s'' requires a parameter\n",
        !           270:                    option_source, arg - 1);
        !           271:            exit(1);
        !           272:        }
        !           273:        *p->p_obj = atoi(param_start);
        !           274:        break;
        !           275: 
        !           276:     case PRO_FONT:
        !           277:        parsefont((struct fstate *) p->p_obj, param_start);
        !           278:        break;
        !           279: 
        !           280:     default:
        !           281:        fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
        !           282:                p->p_type);
        !           283:        exit(1);
        !           284:     }
        !           285: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.