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

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