[BACK]Return to args.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / indent

Annotation of src/usr.bin/indent/args.c, Revision 1.3

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