[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.4

1.4     ! mickey      1: /*     $OpenBSD: args.c,v 1.3 1996/10/28 00:36:23 millert 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.4     ! mickey     40: static char rcsid[] = "$OpenBSD: args.c,v 1.3 1996/10/28 00:36:23 millert 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"
1.4     ! mickey     54: #include <err.h>
1.1       deraadt    55:
                     56: /* profile types */
                     57: #define        PRO_SPECIAL     1       /* special case */
                     58: #define        PRO_BOOL        2       /* boolean */
                     59: #define        PRO_INT         3       /* integer */
                     60: #define PRO_FONT       4       /* troff font */
                     61:
                     62: /* profile specials for booleans */
                     63: #define        ON              1       /* turn it on */
                     64: #define        OFF             0       /* turn it off */
                     65:
                     66: /* profile specials for specials */
                     67: #define        IGN             1       /* ignore it */
                     68: #define        CLI             2       /* case label indent (float) */
                     69: #define        STDIN           3       /* use stdin */
                     70: #define        KEY             4       /* type (keyword) */
                     71:
                     72: char *option_source = "?";
                     73:
                     74: /*
                     75:  * N.B.: because of the way the table here is scanned, options whose names are
                     76:  * substrings of other options must occur later; that is, with -lp vs -l, -lp
                     77:  * must be first.  Also, while (most) booleans occur more than once, the last
                     78:  * default value is the one actually assigned.
                     79:  */
                     80: struct pro {
                     81:     char       *p_name;                /* name, eg -bl, -cli */
                     82:     int         p_type;                /* type (int, bool, special) */
                     83:     int         p_default;     /* the default value (if int) */
                     84:     int         p_special;     /* depends on type */
                     85:     int        *p_obj;         /* the associated variable */
                     86: }           pro[] = {
                     87:
1.4     ! mickey     88:        { "T", PRO_SPECIAL, 0, KEY, 0 },
        !            89:        {"bacc", PRO_BOOL, false, ON,
        !            90:         &blanklines_around_conditional_compilation },
        !            91:        {"badp", PRO_BOOL, false, ON,
        !            92:         &blanklines_after_declarations_at_proctop },
        !            93:        {"bad", PRO_BOOL, false, ON, &blanklines_after_declarations },
        !            94:        {"bap", PRO_BOOL, false, ON, &blanklines_after_procs },
        !            95:        {"bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments },
        !            96:        {"bc", PRO_BOOL, true, OFF, &ps.leave_comma },
        !            97:        {"bl", PRO_BOOL, true, OFF, &btype_2 },
        !            98:        {"br", PRO_BOOL, true, ON, &btype_2 },
        !            99:        {"bs", PRO_BOOL, false, ON, &Bill_Shannon },
        !           100:        {"cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline },
        !           101:        {"cd", PRO_INT, 0, 0, &ps.decl_com_ind },
        !           102:        {"ce", PRO_BOOL, true, ON, &cuddle_else },
        !           103:        {"ci", PRO_INT, 0, 0, &continuation_indent },
        !           104:        {"cli", PRO_SPECIAL, 0, CLI, 0 },
        !           105:        {"c", PRO_INT, 33, 0, &ps.com_ind },
        !           106:        {"di", PRO_INT, 16, 0, &ps.decl_indent },
        !           107:        {"dj", PRO_BOOL, false, ON, &ps.ljust_decl },
        !           108:        {"d", PRO_INT, 0, 0, &ps.unindent_displace },
        !           109:        {"eei", PRO_BOOL, false, ON, &extra_expression_indent },
        !           110:        {"ei", PRO_BOOL, true, ON, &ps.else_if },
        !           111:        {"fbc", PRO_FONT, 0, 0, (int *) &blkcomf },
        !           112:        {"fbx", PRO_FONT, 0, 0, (int *) &boxcomf },
        !           113:        {"fb", PRO_FONT, 0, 0, (int *) &bodyf },
        !           114:        {"fc1", PRO_BOOL, true, ON, &format_col1_comments },
        !           115:        {"fc", PRO_FONT, 0, 0, (int *) &scomf },
        !           116:        {"fk", PRO_FONT, 0, 0, (int *) &keywordf },
        !           117:        {"fs", PRO_FONT, 0, 0, (int *) &stringf },
        !           118:        {"ip", PRO_BOOL, true, ON, &ps.indent_parameters },
        !           119:        {"i", PRO_INT, 8, 0, &ps.ind_size },
        !           120:        {"lc", PRO_INT, 0, 0, &block_comment_max_col },
        !           121:        {"lp", PRO_BOOL, true, ON, &lineup_to_parens },
        !           122:        {"l", PRO_INT, 78, 0, &max_col },
        !           123:        {"nbacc", PRO_BOOL, false, OFF,
        !           124:         &blanklines_around_conditional_compilation },
        !           125:        {"nbadp", PRO_BOOL, false, OFF,
        !           126:         &blanklines_after_declarations_at_proctop },
        !           127:        {"nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations },
        !           128:        {"nbap", PRO_BOOL, false, OFF, &blanklines_after_procs },
        !           129:        {"nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments },
        !           130:        {"nbc", PRO_BOOL, true, ON, &ps.leave_comma },
        !           131:        {"nbs", PRO_BOOL, false, OFF, &Bill_Shannon },
        !           132:        {"ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline },
        !           133:        {"nce", PRO_BOOL, true, OFF, &cuddle_else },
        !           134:        {"ndj", PRO_BOOL, false, OFF, &ps.ljust_decl },
        !           135:        {"neei", PRO_BOOL, false, OFF, &extra_expression_indent },
        !           136:        {"nei", PRO_BOOL, true, OFF, &ps.else_if },
        !           137:        {"nfc1", PRO_BOOL, true, OFF, &format_col1_comments },
        !           138:        {"nip", PRO_BOOL, true, OFF, &ps.indent_parameters },
        !           139:        {"nlp", PRO_BOOL, true, OFF, &lineup_to_parens },
        !           140:        {"npcs", PRO_BOOL, false, OFF, &proc_calls_space },
        !           141:        {"npro", PRO_SPECIAL, 0, IGN, 0 },
        !           142:        {"npsl", PRO_BOOL, true, OFF, &procnames_start_line },
        !           143:        {"nps", PRO_BOOL, false, OFF, &pointer_as_binop },
        !           144:        {"nsc", PRO_BOOL, true, OFF, &star_comment_cont },
        !           145:        {"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines },
        !           146:        {"nv", PRO_BOOL, false, OFF, &verbose },
        !           147:        {"pcs", PRO_BOOL, false, ON, &proc_calls_space },
        !           148:        {"psl", PRO_BOOL, true, ON, &procnames_start_line },
        !           149:        {"ps", PRO_BOOL, false, ON, &pointer_as_binop },
        !           150:        {"sc", PRO_BOOL, true, ON, &star_comment_cont },
        !           151:        {"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines },
        !           152:        {"st", PRO_SPECIAL, 0, STDIN, 0 },
        !           153:        {"troff", PRO_BOOL, false, ON, &troff },
        !           154:        {"v", PRO_BOOL, false, ON, &verbose },
        !           155:        /* whew! */
        !           156:        { 0, 0, 0, 0, 0 }
1.1       deraadt   157: };
                    158:
1.4     ! mickey    159: void scan_profile();
        !           160: void set_option();
        !           161:
1.1       deraadt   162: /*
                    163:  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
                    164:  * given in these files.
                    165:  */
1.4     ! mickey    166: void
1.1       deraadt   167: set_profile()
                    168: {
                    169:     register FILE *f;
                    170:     char        fname[BUFSIZ];
                    171:     static char prof[] = ".indent.pro";
                    172:
1.3       millert   173:     if (strlen(getenv("HOME")) + sizeof(prof) > sizeof(fname)) {
                    174:        warnx("%s/%s: %s", getenv("HOME"), prof, strerror(ENAMETOOLONG));
                    175:        return;
                    176:     }
1.1       deraadt   177:     sprintf(fname, "%s/%s", getenv("HOME"), prof);
                    178:     if ((f = fopen(option_source = fname, "r")) != NULL) {
                    179:        scan_profile(f);
                    180:        (void) fclose(f);
                    181:     }
                    182:     if ((f = fopen(option_source = prof, "r")) != NULL) {
                    183:        scan_profile(f);
                    184:        (void) fclose(f);
                    185:     }
                    186:     option_source = "Command line";
                    187: }
                    188:
1.4     ! mickey    189: void
1.1       deraadt   190: scan_profile(f)
                    191:     register FILE *f;
                    192: {
                    193:     register int i;
                    194:     register char *p;
                    195:     char        buf[BUFSIZ];
                    196:
                    197:     while (1) {
                    198:        for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p);
                    199:        if (p != buf) {
                    200:            *p++ = 0;
                    201:            if (verbose)
                    202:                printf("profile: %s\n", buf);
                    203:            set_option(buf);
                    204:        }
                    205:        else if (i == EOF)
                    206:            return;
                    207:     }
                    208: }
                    209:
                    210: char       *param_start;
                    211:
1.4     ! mickey    212: int
1.1       deraadt   213: eqin(s1, s2)
                    214:     register char *s1;
                    215:     register char *s2;
                    216: {
                    217:     while (*s1) {
                    218:        if (*s1++ != *s2++)
                    219:            return (false);
                    220:     }
                    221:     param_start = s2;
                    222:     return (true);
                    223: }
                    224:
                    225: /*
                    226:  * Set the defaults.
                    227:  */
1.4     ! mickey    228: void
1.1       deraadt   229: set_defaults()
                    230: {
                    231:     register struct pro *p;
                    232:
                    233:     /*
                    234:      * Because ps.case_indent is a float, we can't initialize it from the
                    235:      * table:
                    236:      */
                    237:     ps.case_indent = 0.0;      /* -cli0.0 */
                    238:     for (p = pro; p->p_name; p++)
                    239:        if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
                    240:            *p->p_obj = p->p_default;
                    241: }
                    242:
1.4     ! mickey    243: void
1.1       deraadt   244: set_option(arg)
                    245:     register char *arg;
                    246: {
                    247:     register struct pro *p;
                    248:     extern double atof();
                    249:
                    250:     arg++;                     /* ignore leading "-" */
                    251:     for (p = pro; p->p_name; p++)
                    252:        if (*p->p_name == *arg && eqin(p->p_name, arg))
                    253:            goto found;
                    254:     fprintf(stderr, "indent: %s: unknown parameter \"%s\"\n", option_source, arg - 1);
                    255:     exit(1);
                    256: found:
                    257:     switch (p->p_type) {
                    258:
                    259:     case PRO_SPECIAL:
                    260:        switch (p->p_special) {
                    261:
                    262:        case IGN:
                    263:            break;
                    264:
                    265:        case CLI:
                    266:            if (*param_start == 0)
                    267:                goto need_param;
                    268:            ps.case_indent = atof(param_start);
                    269:            break;
                    270:
                    271:        case STDIN:
                    272:            if (input == 0)
                    273:                input = stdin;
                    274:            if (output == 0)
                    275:                output = stdout;
                    276:            break;
                    277:
                    278:        case KEY:
                    279:            if (*param_start == 0)
                    280:                goto need_param;
                    281:            {
                    282:                register char *str = (char *) malloc(strlen(param_start) + 1);
                    283:                strcpy(str, param_start);
                    284:                addkey(str, 4);
                    285:            }
                    286:            break;
                    287:
                    288:        default:
                    289:            fprintf(stderr, "\
                    290: indent: set_option: internal error: p_special %d\n", p->p_special);
                    291:            exit(1);
                    292:        }
                    293:        break;
                    294:
                    295:     case PRO_BOOL:
                    296:        if (p->p_special == OFF)
                    297:            *p->p_obj = false;
                    298:        else
                    299:            *p->p_obj = true;
                    300:        break;
                    301:
                    302:     case PRO_INT:
                    303:        if (!isdigit(*param_start)) {
                    304:     need_param:
                    305:            fprintf(stderr, "indent: %s: ``%s'' requires a parameter\n",
                    306:                    option_source, arg - 1);
                    307:            exit(1);
                    308:        }
                    309:        *p->p_obj = atoi(param_start);
                    310:        break;
                    311:
                    312:     case PRO_FONT:
                    313:        parsefont((struct fstate *) p->p_obj, param_start);
                    314:        break;
                    315:
                    316:     default:
                    317:        fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
                    318:                p->p_type);
                    319:        exit(1);
                    320:     }
                    321: }