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