home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / INDENTSR.ZIP / ARGS.C next >
C/C++ Source or Header  |  1992-08-27  |  10KB  |  351 lines

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