home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / INDENT1.ZIP / ARGS.C next >
C/C++ Source or Header  |  1991-07-04  |  9KB  |  324 lines

  1. /*
  2.  * Copyright (c) 1985 Sun Microsystems, Inc. Copyright (c) 1980 The Regents
  3.  * of the University of California. Copyright (c) 1976 Board of Trustees of
  4.  * the University of Illinois. All rights reserved.
  5.  * 
  6.  * Redistribution and use in source and binary forms are permitted provided that
  7.  * the above copyright notice and this paragraph are duplicated in all such
  8.  * forms and that any documentation, advertising materials, and other
  9.  * materials related to such distribution and use acknowledge that the
  10.  * software was developed by the University of California, Berkeley, the
  11.  * University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
  12.  * either University or Sun Microsystems may not be used to endorse or
  13.  * promote products derived from this software without specific prior written
  14.  * permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  15.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
  16.  * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17.  */
  18.  
  19. #ifndef lint
  20. static char     sccsid[] = "@(#)args.c    5.6 (Berkeley) 9/15/88";
  21.  
  22. #endif                                    /* not lint */
  23.  
  24. /*
  25.  * Argument scanning and profile reading code.  Default parameters are set
  26.  * here as well.
  27.  */
  28.  
  29. #include "globs.h"
  30.  
  31. #if        CC_MSC
  32. #include "msdos.h"
  33. #include "malloc.h"
  34. #else
  35. #include <sys/param.h>
  36. #endif
  37.  
  38. #include <sys/types.h>
  39. #include <ctype.h>
  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.  
  59. /*
  60.  * N.B.: because of the way the table here is scanned, options whose names
  61.  * are substrings of other options must occur later; that is, with -lp vs -l,
  62.  * -lp must be first.  Also, while (most) booleans occur more than once, the
  63.  * last default value is the one actually assigned.
  64.  */
  65. struct pro
  66. {
  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.  
  75.     "T", PRO_SPECIAL, 0, KEY, 0,
  76.     "bacc", PRO_BOOL, false, ON, &bl_around_cc,
  77.     "badp", PRO_BOOL, false, ON, &bl_after_dcl_at_proctop,
  78.     "bad", PRO_BOOL, false, ON, &bl_after_dcl,
  79.     "bap", PRO_BOOL, false, ON, &bl_after_procs,
  80.     "bbb", PRO_BOOL, false, ON, &bl_before_blockcomments,
  81.     "bc", PRO_BOOL, true, OFF, &ps.leave_comma,
  82.     "bl", PRO_BOOL, true, OFF, &btype_2,
  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_bl,
  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.     "c", PRO_INT, 33, 0, &ps.com_ind,
  91.     "di", PRO_INT, 16, 0, &ps.decl_indent,
  92.     "dj", PRO_BOOL, false, ON, &ps.ljust_decl,
  93.     "d", PRO_INT, 0, 0, &ps.unindent_displace,
  94.     "eei", PRO_BOOL, false, ON, &extra_expression_indent,
  95.     "ei", PRO_BOOL, true, ON, &ps.else_if,
  96.     "fbc", PRO_FONT, 0, 0, (int *) &blkcomf,
  97.     "fbx", PRO_FONT, 0, 0, (int *) &boxcomf,
  98.     "fb", PRO_FONT, 0, 0, (int *) &bodyf,
  99.     "fc1", PRO_BOOL, true, ON, &format_col1_comments,
  100.     "fc", PRO_FONT, 0, 0, (int *) &scomf,
  101.     "fk", PRO_FONT, 0, 0, (int *) &keywordf,
  102.     "fs", PRO_FONT, 0, 0, (int *) &stringf,
  103.     "ip", PRO_BOOL, true, ON, &ps.indent_parameters,
  104.     "i", PRO_INT, 8, 0, &ps.ind_size,
  105.     "lc", PRO_INT, 0, 0, &block_comment_max_col,
  106.     "lp", PRO_BOOL, true, ON, &lineup_to_parens,
  107.     "l", PRO_INT, 78, 0, &max_col,
  108.     "nbacc", PRO_BOOL, false, OFF, &bl_around_cc,
  109.     "nbadp", PRO_BOOL, false, OFF, &bl_after_dcl_at_proctop,
  110.     "nbad", PRO_BOOL, false, OFF, &bl_after_dcl,
  111.     "nbap", PRO_BOOL, false, OFF, &bl_after_procs,
  112.     "nbbb", PRO_BOOL, false, OFF, &bl_before_blockcomments,
  113.     "nbc", PRO_BOOL, true, ON, &ps.leave_comma,
  114.     "nbs", PRO_BOOL, false, OFF, &Bill_Shannon,
  115.     "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_bl,
  116.     "nce", PRO_BOOL, true, OFF, &cuddle_else,
  117.     "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl,
  118.     "neei", PRO_BOOL, false, OFF, &extra_expression_indent,
  119.     "nei", PRO_BOOL, true, OFF, &ps.else_if,
  120.     "nfc1", PRO_BOOL, true, OFF, &format_col1_comments,
  121.     "nip", PRO_BOOL, true, OFF, &ps.indent_parameters,
  122.     "nlp", PRO_BOOL, true, OFF, &lineup_to_parens,
  123.     "npcs", PRO_BOOL, false, OFF, &proc_calls_space,
  124.     "npro", PRO_SPECIAL, 0, IGN, 0,
  125.     "npsl", PRO_BOOL, true, OFF, &procnames_start_line,
  126.     "nps", PRO_BOOL, false, OFF, &pointer_as_binop,
  127.     "nsc", PRO_BOOL, true, OFF, &star_comment_cont,
  128.     "nsob", PRO_BOOL, false, OFF, &swallow_optional_bl,
  129.     "nv", PRO_BOOL, false, OFF, &verbose,
  130.     "pcs", PRO_BOOL, false, ON, &proc_calls_space,
  131.     "psl", PRO_BOOL, true, ON, &procnames_start_line,
  132.     "ps", PRO_BOOL, false, ON, &pointer_as_binop,
  133.     "sc", PRO_BOOL, true, ON, &star_comment_cont,
  134.     "sob", PRO_BOOL, false, ON, &swallow_optional_bl,
  135.     "st", PRO_SPECIAL, 0, STDIN, 0,
  136.     "tabs", PRO_INT, 8, 0, &tabsize,
  137.     "troff", PRO_BOOL, false, ON, &troff,
  138.     "v", PRO_BOOL, false, ON, &verbose,
  139.     /* whew! */
  140.     0, 0, 0, 0, 0
  141. };
  142.  
  143. /*
  144.  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles
  145.  * arguments given in these files.
  146.  */
  147. set_profile()
  148. {
  149.     register FILE  *f;
  150.     char            home[MAXPATHLEN];
  151.     char           *env;
  152.     char            fname[BUFSIZ];
  153.     static char     prof[] = PROFILE_NAME;
  154.  
  155.     if (!(env = getenv("HOME")))
  156.         env = "";
  157.  
  158.     strcpy(home, env);                    /* Make a local copy */
  159.  
  160. #if    OS_MSDOS
  161.     /***********************************************************************\
  162.     *                                        *
  163.     * MS-Dos is sensitive about extra directory name separators, so lets    *
  164.     * try to remove them:                            *
  165.     *                                        *
  166.     \***********************************************************************/
  167.  
  168.     env = &home[strlen(home)];
  169.  
  170.     while ((env-- != home) && ((*env == '/') || (*env == '\\')));
  171.  
  172.     *(++env) = '\0';                    /* Terminate the thing */
  173. #endif                                    /* OS_MSDOS */
  174.  
  175.     sprintf(fname, "%s/%s", home, prof);
  176.     if ((f = fopen(fname, "r")) != NULL)
  177.     {
  178.         scan_profile(f);
  179.         (void) fclose(f);
  180.     }
  181.     if ((f = fopen(prof, "r")) != NULL)
  182.     {
  183.         scan_profile(f);
  184.         (void) fclose(f);
  185.     }
  186. }
  187.  
  188. scan_profile(f)
  189. register FILE  *f;
  190. {
  191.     register int    i;
  192.     register char  *p;
  193.     char            buf[BUFSIZ];
  194.  
  195.     while (1)
  196.     {
  197.         for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p);
  198.         if (p != buf)
  199.         {
  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.  
  212. eqin(s1, s2)
  213. register char  *s1;
  214. register char  *s2;
  215. {
  216.     while (*s1)
  217.     {
  218.         if (*s1++ != *s2++)
  219.             return (false);
  220.     }
  221.     param_start = s2;
  222.     return (true);
  223. }
  224.  
  225. /*
  226.  * Set the defaults.
  227.  */
  228. set_defaults()
  229. {
  230.     register struct pro *p;
  231.  
  232.     /*
  233.      * Because ps.case_indent is a float, we can't initialize it from the
  234.      * table:
  235.      */
  236.     ps.case_indent = 0.0;                /* -cli0.0 */
  237.     for (p = pro; p->p_name; p++)
  238.         if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
  239.             *p->p_obj = p->p_default;
  240. }
  241.  
  242. set_option(arg)
  243. register char  *arg;
  244. {
  245.     register struct pro *p;
  246.     extern double   atof();
  247.  
  248.     arg++;                                /* ignore leading "-" */
  249.     for (p = pro; p->p_name; p++)
  250.         if (*p->p_name == *arg && eqin(p->p_name, arg))
  251.             goto found;
  252.     fprintf(stderr, "indent: unknown parameter \"%s\"\n", arg - 1);
  253.     exit(1);
  254. found:
  255.     switch (p->p_type)
  256.     {
  257.  
  258.     case PRO_SPECIAL:
  259.         switch (p->p_special)
  260.         {
  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.  
  284.                 strcpy(str, param_start);
  285.                 addkey(str, 4);
  286.             }
  287.             break;
  288.  
  289.         default:
  290.             fprintf(stderr, "\
  291. indent: set_option: internal error: p_special %d\n", p->p_special);
  292.             exit(1);
  293.         }
  294.         break;
  295.  
  296.     case PRO_BOOL:
  297.         if (p->p_special == OFF)
  298.             *p->p_obj = false;
  299.         else
  300.             *p->p_obj = true;
  301.         break;
  302.  
  303.     case PRO_INT:
  304.         if (*param_start == 0)
  305.         {
  306.     need_param:
  307.             fprintf(stderr, "indent: ``%s'' requires a parameter\n",
  308.                     arg - 1);
  309.             exit(1);
  310.         }
  311.         *p->p_obj = atoi(param_start);
  312.         break;
  313.  
  314.     case PRO_FONT:
  315.         parsefont((struct fstate *) p->p_obj, param_start);
  316.         break;
  317.  
  318.     default:
  319.         fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
  320.                 p->p_type);
  321.         exit(1);
  322.     }
  323. }
  324.