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