home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / indent / args.c next >
C/C++ Source or Header  |  1999-05-21  |  26KB  |  828 lines

  1. /* Copyright (c) 1992, Free Software Foundation, Inc.  All rights reserved.
  2.  
  3.    Copyright (c) 1985 Sun Microsystems, Inc. Copyright (c) 1980 The Regents
  4.    of the University of California. Copyright (c) 1976 Board of Trustees of
  5.    the University of Illinois. All rights reserved.
  6.  
  7.    Redistribution and use in source and binary forms are permitted
  8.    provided that
  9.    the above copyright notice and this paragraph are duplicated in all such
  10.    forms and that any documentation, advertising materials, and other
  11.    materials related to such distribution and use acknowledge that the
  12.    software was developed by the University of California, Berkeley, the
  13.    University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
  14.    either University or Sun Microsystems may not be used to endorse or
  15.    promote products derived from this software without specific prior written
  16.    permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  17.    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
  18.    OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
  19.  
  20.  
  21. /* Argument scanning and profile reading code.  Default parameters are set
  22.    here as well. */
  23.  
  24. #include "sys.h"
  25. #include "indent.h"
  26. #include <ctype.h>
  27. #include "version.h"
  28.  
  29. int else_endif_col;
  30.  
  31. extern char *in_name;
  32.  
  33. char *getenv ();
  34.  
  35. /* profile types */
  36. enum profile
  37. {
  38.   PRO_BOOL,            /* boolean */
  39.   PRO_INT,            /* integer */
  40.   PRO_FONT,            /* troff font */
  41.   PRO_IGN,            /* ignore it */
  42.   PRO_KEY,            /* -T switch */
  43.   PRO_SETTINGS,            /* bundled set of settings */
  44.   PRO_PRSTRING            /* Print string and exit */
  45. };
  46.  
  47. /* profile specials for booleans */
  48. enum on_or_off
  49. {
  50.   ONOFF_NA,            /* Not applicable.  Used in table for
  51.                    non-booleans.  */
  52.   OFF,                /* This option turns on the boolean variable
  53.                    in question.  */
  54.   ON                /* it turns it off */
  55. };
  56.  
  57. /* Explicit flags for each option.  */
  58. static int exp_T = 0;
  59. static int exp_bacc = 0;
  60. static int exp_badp = 0;
  61. static int exp_bad = 0;
  62. static int exp_bap = 0;
  63. static int exp_bbb = 0;
  64. static int exp_bc = 0;
  65. static int exp_bli = 0;
  66. static int exp_bl = 0;
  67. static int exp_bs = 0;
  68. static int exp_cdb = 0;
  69. static int exp_cd = 0;
  70. static int exp_ce = 0;
  71. static int exp_ci = 0;
  72. static int exp_cli = 0;
  73. static int exp_cp = 0;
  74. static int exp_cs = 0;
  75. static int exp_c = 0;
  76. static int exp_di = 0;
  77. static int exp_dj = 0;
  78. static int exp_d = 0;
  79. static int exp_eei = 0;
  80. static int exp_ei = 0;
  81. static int exp_fbc = 0;
  82. static int exp_fbx = 0;
  83. static int exp_fb = 0;
  84. static int exp_fc1 = 0;
  85. static int exp_fca = 0;
  86. static int exp_fc = 0;
  87. static int exp_fk = 0;
  88. static int exp_fs = 0;
  89. static int exp_gnu = 0;
  90. static int exp_orig = 0;
  91. static int exp_ip = 0;
  92. static int exp_nip = 0;
  93. static int exp_i = 0;
  94. static int exp_lc = 0;
  95. static int exp_lp = 0;
  96. static int exp_l = 0;
  97. static int exp_lps = 0;
  98. static int exp_pcs = 0;
  99. static int exp_psl = 0;
  100. static int exp_pro = 0;
  101. static int exp_ps = 0;
  102. static int exp_kr = 0;
  103. static int exp_sc = 0;
  104. static int exp_sob = 0;
  105. static int exp_ss = 0;
  106. static int exp_st = 0;
  107. static int exp_troff = 0;
  108. static int exp_ts = 0;
  109. static int exp_v = 0;
  110. static int exp_version = 0;
  111.  
  112. /* The following variables are controlled by command line parameters and
  113.    their meaning is explained in indent.h.  */
  114. int leave_comma;
  115. int decl_com_ind;
  116. int case_indent;
  117. int com_ind;
  118. int decl_indent;
  119. int ljust_decl;
  120. int unindent_displace;
  121. int else_if;
  122. int indent_parameters;
  123. int ind_size;
  124. int tabsize;
  125. int blanklines_after_procs;
  126. int use_stdout;
  127. int pointer_as_binop;
  128. int blanklines_after_declarations;
  129. int blanklines_before_blockcomments;
  130. int blanklines_around_conditional_compilation;
  131. int swallow_optional_blanklines;
  132. int n_real_blanklines;
  133. int prefix_blankline_requested;
  134. int postfix_blankline_requested;
  135. int brace_indent;
  136. int btype_2;
  137. int space_sp_semicolon;
  138. int case_ind;
  139. int max_col;
  140. int verbose;
  141. int cuddle_else;
  142. int star_comment_cont;
  143. int comment_delimiter_on_blankline;
  144. int troff;
  145. int procnames_start_line;
  146. int proc_calls_space;
  147. int cast_space;
  148. int format_col1_comments;
  149. int format_comments;
  150. int continuation_indent;
  151. int lineup_to_parens;
  152. int leave_preproc_space;
  153. int blank_after_sizeof;
  154. int blanklines_after_declarations_at_proctop;
  155. int block_comment_max_col;
  156. int extra_expression_indent;
  157.  
  158. int expect_output_file;
  159.  
  160. /* N.B.: because of the way the table here is scanned, options whose names
  161.    are substrings of other options must occur later; that is, with -lp vs -l,
  162.    -lp must be first.  Also, while (most) booleans occur more than once, the
  163.    last default value is the one actually assigned. */
  164. struct pro
  165. {
  166.   char *p_name;            /* name, eg -bl, -cli */
  167.   enum profile p_type;
  168.   int p_default;        /* the default value (if int) */
  169.  
  170.   /* If p_type == PRO_BOOL, ON or OFF to tell how this switch affects the
  171.      variable. Not used for other p_type's.  */
  172.   enum on_or_off p_special;
  173.  
  174.   /* if p_type == PRO_SETTINGS, a (char *) pointing to a list of the switches
  175.      to set, separated by NULs, terminated by 2 NULs. if p_type == PRO_BOOL,
  176.      PRO_INT, or PRO_FONT, address of the variable that gets set by the
  177.      option. if p_type == PRO_PRSTRING, a (char *) pointing to the string.  */
  178.   int *p_obj;
  179.  
  180.   /* Points to a nonzero value (allocated statically for all options) if the
  181.      option has been specified explicitly.  This is necessary because for
  182.      boolean options, the options to set and reset the variable must share
  183.      the explicit flag.  */
  184.   int *p_explicit;
  185. };
  186.  
  187. #ifdef BERKELEY_DEFAULTS
  188. /* Settings for original defaults */
  189. struct pro pro[] =
  190. {
  191.   {"T", PRO_KEY, 0, ONOFF_NA, 0, &exp_T},
  192.   {"bacc", PRO_BOOL, false, ON,
  193.    &blanklines_around_conditional_compilation, &exp_bacc},
  194.   {"badp", PRO_BOOL, false, ON,
  195.    &blanklines_after_declarations_at_proctop, &exp_badp},
  196.   {"bad", PRO_BOOL, false, ON, &blanklines_after_declarations, &exp_bad},
  197.   {"bap", PRO_BOOL, false, ON, &blanklines_after_procs, &exp_bap},
  198.   {"bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments, &exp_bbb},
  199.   {"bc", PRO_BOOL, true, OFF, &leave_comma, &exp_bc},
  200.   {"bli", PRO_INT, 0, ONOFF_NA, &brace_indent, &exp_bli},
  201.   {"bl", PRO_BOOL, true, OFF, &btype_2, &exp_bl},
  202.   {"br", PRO_BOOL, true, ON, &btype_2, &exp_bl},
  203.   {"bs", PRO_BOOL, false, ON, &blank_after_sizeof, &exp_bs},
  204.   {"cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline, &exp_cdb},
  205.   {"cd", PRO_INT, 33, ONOFF_NA, &decl_com_ind, &exp_cd},
  206.   {"ce", PRO_BOOL, true, ON, &cuddle_else, &exp_ce},
  207.   {"ci", PRO_INT, 4, ONOFF_NA, &continuation_indent, &exp_ci},
  208.   {"cli", PRO_INT, 0, ONOFF_NA, &case_indent, &exp_cli},
  209.   {"cp", PRO_INT, 33, ONOFF_NA, &else_endif_col, &exp_cp},
  210.   {"cs", PRO_BOOL, true, ON, &cast_space, &exp_cs},
  211.   {"c", PRO_INT, 33, ONOFF_NA, &com_ind, &exp_c},
  212.   {"di", PRO_INT, 16, ONOFF_NA, &decl_indent, &exp_di},
  213.   {"dj", PRO_BOOL, false, ON, &ljust_decl, &exp_dj},
  214.   {"d", PRO_INT, 0, ONOFF_NA, &unindent_displace, &exp_d},
  215.   {"eei", PRO_BOOL, false, ON, &extra_expression_indent, &exp_eei},
  216.   {"ei", PRO_BOOL, true, ON, &else_if, &exp_ei},
  217.   {"fbc", PRO_FONT, 0, ONOFF_NA, (int *) &blkcomf, &exp_fbc},
  218.   {"fbx", PRO_FONT, 0, ONOFF_NA, (int *) &boxcomf, &exp_fbx},
  219.   {"fb", PRO_FONT, 0, ONOFF_NA, (int *) &bodyf, &exp_fb},
  220.   {"fc1", PRO_BOOL, true, ON, &format_col1_comments, &exp_fc1},
  221.   {"fca", PRO_BOOL, true, ON, &format_comments, &exp_fca},
  222.   {"fc", PRO_FONT, 0, ONOFF_NA, (int *) &scomf, &exp_fc},
  223.   {"fk", PRO_FONT, 0, ONOFF_NA, (int *) &keywordf, &exp_fk},
  224.   {"fs", PRO_FONT, 0, ONOFF_NA, (int *) &stringf, &exp_fs},
  225.   {"gnu", PRO_SETTINGS, 0, ONOFF_NA,
  226.    (int *) "-nbad\0-bap\0-nbbb\0-nbc\0-bl\0-ncdb\0-cs\0-nce\0-di2\0-ndj\0\
  227. -ei\0-nfc1\0-i2\0-ip5\0-lp\0-pcs\0-nps\0-psl\0-nsc\0-nsob\0-bli2\0\
  228. -cp1\0-nfca\0", &exp_gnu},
  229.   {"ip", PRO_INT, 4, ON, &indent_parameters, &exp_ip},
  230.   {"i", PRO_INT, 4, ONOFF_NA, &ind_size, &exp_i},
  231.   {"kr", PRO_SETTINGS, 0, ONOFF_NA,
  232.    (int *) "-nbad\0-bap\0-nbbb\0-nbc\0-br\0-c33\0-cd33\0-ncdb\0-ce\0\
  233. -ci4\0-cli0\0-d0\0-di1\0-nfc1\0-i4\0-ip0\0-l75\0-lp\0-npcs\0-npsl\0-cs\0\
  234. -nsc\0-nsc\0-nsob\0-nfca\0-cp33\0-nss\0", &exp_kr},
  235.   {"lc", PRO_INT, 0, ONOFF_NA, &block_comment_max_col, &exp_lc},
  236.   {"lps", PRO_BOOL, false, ON, &leave_preproc_space, &exp_lps},
  237.   {"lp", PRO_BOOL, true, ON, &lineup_to_parens, &exp_lp},
  238.   {"l", PRO_INT, 78, ONOFF_NA, &max_col, &exp_l},
  239.   {"nbacc", PRO_BOOL, false, OFF,
  240.    &blanklines_around_conditional_compilation, &exp_bacc},
  241.   {"nbadp", PRO_BOOL, false, OFF,
  242.    &blanklines_after_declarations_at_proctop, &exp_badp},
  243.   {"nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations, &exp_bad},
  244.   {"nbap", PRO_BOOL, false, OFF, &blanklines_after_procs, &exp_bap},
  245.   {"nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments, &exp_bbb},
  246.   {"nbc", PRO_BOOL, true, ON, &leave_comma, &exp_bc},
  247.   {"nbs", PRO_BOOL, false, OFF, &blank_after_sizeof, &exp_bs},
  248.   {"ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline, &exp_cdb},
  249.   {"nce", PRO_BOOL, true, OFF, &cuddle_else, &exp_ce},
  250.   {"ncs", PRO_BOOL, true, OFF, &cast_space, &exp_cs},
  251.   {"ndj", PRO_BOOL, false, OFF, &ljust_decl, &exp_dj},
  252.   {"neei", PRO_BOOL, false, OFF, &extra_expression_indent, &exp_eei},
  253.   {"nei", PRO_BOOL, true, OFF, &else_if, &exp_ei},
  254.   {"nfc1", PRO_BOOL, true, OFF, &format_col1_comments, &exp_fc1},
  255.   {"nfca", PRO_BOOL, true, OFF, &format_comments, &exp_fca},
  256.   {"nip", PRO_SETTINGS, 0, ONOFF_NA, (int *) "-ip0", &exp_nip},
  257.   {"nlp", PRO_BOOL, true, OFF, &lineup_to_parens, &exp_lp},
  258.   {"nlps", PRO_BOOL, false, OFF, &leave_preproc_space, &exp_lps},
  259.   {"npcs", PRO_BOOL, false, OFF, &proc_calls_space, &exp_pcs},
  260.   {"npro", PRO_IGN, 0, ONOFF_NA, 0, &exp_pro},
  261.   {"npsl", PRO_BOOL, true, OFF, &procnames_start_line, &exp_psl},
  262.   {"nps", PRO_BOOL, false, OFF, &pointer_as_binop, &exp_ps},
  263.   {"nsc", PRO_BOOL, true, OFF, &star_comment_cont, &exp_sc},
  264.   {"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines, &exp_sob},
  265.   {"nss", PRO_BOOL, false, OFF, &space_sp_semicolon, &exp_ss},
  266.   {"nv", PRO_BOOL, false, OFF, &verbose, &exp_v},
  267.  
  268.   {"o", PRO_BOOL, false, ON, &expect_output_file, &expect_output_file},
  269.  
  270.   {"pcs", PRO_BOOL, false, ON, &proc_calls_space, &exp_pcs},
  271.   {"psl", PRO_BOOL, true, ON, &procnames_start_line, &exp_psl},
  272.   {"ps", PRO_BOOL, false, ON, &pointer_as_binop, &exp_ps},
  273.   {"sc", PRO_BOOL, true, ON, &star_comment_cont, &exp_sc},
  274.   {"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines, &exp_sob},
  275.   {"ss", PRO_BOOL, false, ON, &space_sp_semicolon, &exp_ss},
  276.   {"st", PRO_BOOL, false, ON, &use_stdout, &exp_st},
  277.   {"troff", PRO_BOOL, false, ON, &troff, &exp_troff},
  278.   {"ts", PRO_INT, 8, ONOFF_NA, &tabsize, &exp_ts},
  279.   {"version", PRO_PRSTRING, 0, ONOFF_NA,
  280.    (int *) VERSION_STRING, &exp_version},
  281.   {"v", PRO_BOOL, false, ON, &verbose, &exp_v},
  282.  
  283. /* Signify end of structure.  */
  284.   {0, PRO_IGN, 0, ONOFF_NA, 0, 0}
  285. };
  286. #else /* Default to GNU style */
  287.  
  288. /* Changed to make GNU style the default. */
  289. struct pro pro[] =
  290. {
  291.   {"T", PRO_KEY, 0, ONOFF_NA, 0, &exp_T},
  292.   {"bacc", PRO_BOOL, false, ON,
  293.    &blanklines_around_conditional_compilation, &exp_bacc},
  294.   {"badp", PRO_BOOL, false, ON,
  295.    &blanklines_after_declarations_at_proctop, &exp_badp},
  296.   {"bad", PRO_BOOL, false, ON, &blanklines_after_declarations, &exp_bad},
  297.   {"bap", PRO_BOOL, true, ON, &blanklines_after_procs, &exp_bap},
  298.   {"bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments, &exp_bbb},
  299.   {"bc", PRO_BOOL, true, OFF, &leave_comma, &exp_bc},
  300.   {"bli", PRO_INT, 2, ONOFF_NA, &brace_indent, &exp_bli},
  301.   {"bl", PRO_BOOL, true, OFF, &btype_2, &exp_bl},
  302.   {"br", PRO_BOOL, false, ON, &btype_2, &exp_bl},
  303.   {"bs", PRO_BOOL, false, ON, &blank_after_sizeof, &exp_bs},
  304.   {"cdb", PRO_BOOL, false, ON, &comment_delimiter_on_blankline, &exp_cdb},
  305.   {"cd", PRO_INT, 33, ONOFF_NA, &decl_com_ind, &exp_cd},
  306.   {"ce", PRO_BOOL, false, ON, &cuddle_else, &exp_ce},
  307.   {"ci", PRO_INT, 0, ONOFF_NA, &continuation_indent, &exp_ci},
  308.   {"cli", PRO_INT, 0, ONOFF_NA, &case_indent, &exp_cli},
  309.   {"cp", PRO_INT, 1, ONOFF_NA, &else_endif_col, &exp_cp},
  310.   {"cs", PRO_BOOL, true, ON, &cast_space, &exp_cs},
  311.   {"c", PRO_INT, 33, ONOFF_NA, &com_ind, &exp_c},
  312.   {"di", PRO_INT, 2, ONOFF_NA, &decl_indent, &exp_di},
  313.   {"dj", PRO_BOOL, false, ON, &ljust_decl, &exp_dj},
  314.   {"d", PRO_INT, 0, ONOFF_NA, &unindent_displace, &exp_d},
  315.   {"eei", PRO_BOOL, false, ON, &extra_expression_indent, &exp_eei},
  316.   {"ei", PRO_BOOL, true, ON, &else_if, &exp_ei},
  317.   {"fbc", PRO_FONT, 0, ONOFF_NA, (int *) &blkcomf, &exp_fbc},
  318.   {"fbx", PRO_FONT, 0, ONOFF_NA, (int *) &boxcomf, &exp_fbx},
  319.   {"fb", PRO_FONT, 0, ONOFF_NA, (int *) &bodyf, &exp_fb},
  320.   {"fc1", PRO_BOOL, false, ON, &format_col1_comments, &exp_fc1},
  321.   {"fca", PRO_BOOL, false, ON, &format_comments, &exp_fca},
  322.   {"fc", PRO_FONT, 0, ONOFF_NA, (int *) &scomf, &exp_fc},
  323.   {"fk", PRO_FONT, 0, ONOFF_NA, (int *) &keywordf, &exp_fk},
  324.   {"fs", PRO_FONT, 0, ONOFF_NA, (int *) &stringf, &exp_fs},
  325.  
  326. /* This is now the default. */
  327.   {"gnu", PRO_SETTINGS, 0, ONOFF_NA,
  328.    (int *) "-nbad\0-bap\0-nbbb\0-nbc\0-bl\0-ncdb\0-cs\0-nce\0-di2\0-ndj\0\
  329. -ei\0-nfc1\0-i2\0-ip5\0-lp\0-pcs\0-nps\0-psl\0-nsc\0-nsob\0-bli2\0\
  330. -cp1\0-nfca\0", &exp_gnu},
  331.  
  332.   {"ip", PRO_INT, 5, ON, &indent_parameters, &exp_ip},
  333.   {"i", PRO_INT, 2, ONOFF_NA, &ind_size, &exp_i},
  334.   {"kr", PRO_SETTINGS, 0, ONOFF_NA,
  335.    (int *) "-nbad\0-bap\0-nbbb\0-nbc\0-br\0-c33\0-cd33\0-ncdb\0-ce\0\
  336. -ci4\0-cli0\0-d0\0-di1\0-nfc1\0-i4\0-ip0\0-l75\0-lp\0-npcs\0-npsl\0-cs\0\
  337. -nsc\0-nsc\0-nsob\0-nfca\0-cp33\0-nss\0", &exp_kr},
  338.   {"lc", PRO_INT, 0, ONOFF_NA, &block_comment_max_col, &exp_lc},
  339.   {"lps", PRO_BOOL, false, ON, &leave_preproc_space, &exp_lps},
  340.   {"lp", PRO_BOOL, true, ON, &lineup_to_parens, &exp_lp},
  341.   {"l", PRO_INT, 78, ONOFF_NA, &max_col, &exp_l},
  342.   {"nbacc", PRO_BOOL, false, OFF,
  343.    &blanklines_around_conditional_compilation, &exp_bacc},
  344.   {"nbadp", PRO_BOOL, false, OFF,
  345.    &blanklines_after_declarations_at_proctop, &exp_badp},
  346.   {"nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations, &exp_bad},
  347.   {"nbap", PRO_BOOL, true, OFF, &blanklines_after_procs, &exp_bap},
  348.   {"nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments, &exp_bbb},
  349.   {"nbc", PRO_BOOL, true, ON, &leave_comma, &exp_bc},
  350.   {"nbs", PRO_BOOL, false, OFF, &blank_after_sizeof, &exp_bs},
  351.   {"ncdb", PRO_BOOL, false, OFF, &comment_delimiter_on_blankline, &exp_cdb},
  352.   {"nce", PRO_BOOL, false, OFF, &cuddle_else, &exp_ce},
  353.   {"ncs", PRO_BOOL, true, OFF, &cast_space, &exp_cs},
  354.   {"ndj", PRO_BOOL, false, OFF, &ljust_decl, &exp_dj},
  355.   {"neei", PRO_BOOL, false, OFF, &extra_expression_indent, &exp_eei},
  356.   {"nei", PRO_BOOL, true, OFF, &else_if, &exp_ei},
  357.   {"nfc1", PRO_BOOL, false, OFF, &format_col1_comments, &exp_fc1},
  358.   {"nfca", PRO_BOOL, false, OFF, &format_comments, &exp_fca},
  359.   {"nip", PRO_SETTINGS, 0, ONOFF_NA, (int *) "-ip0\0", &exp_nip},
  360.   {"nlp", PRO_BOOL, true, OFF, &lineup_to_parens, &exp_lp},
  361.   {"nlps", PRO_BOOL, false, OFF, &leave_preproc_space, &exp_lps},
  362.   {"npcs", PRO_BOOL, true, OFF, &proc_calls_space, &exp_pcs},
  363.   {"npro", PRO_IGN, 0, ONOFF_NA, 0, &exp_pro},
  364.   {"npsl", PRO_BOOL, true, OFF, &procnames_start_line, &exp_psl},
  365.   {"nps", PRO_BOOL, false, OFF, &pointer_as_binop, &exp_ps},
  366.   {"nsc", PRO_BOOL, false, OFF, &star_comment_cont, &exp_sc},
  367.   {"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines, &exp_sob},
  368.   {"nss", PRO_BOOL, false, OFF, &space_sp_semicolon, &exp_ss},
  369.   {"nv", PRO_BOOL, false, OFF, &verbose, &exp_v},
  370.   {"orig", PRO_SETTINGS, 0, ONOFF_NA,
  371.    (int *) "-nbap\0-nbad\0-nbbb\0-bc\0-br\0-c33\0-cd33\0-cdb\0-ce\0-ci4\0\
  372. -cli0\0-cp33\0-di16\0-fc1\0-fca\0-i4\0-ip4\0-l75\0-lp\0\
  373. -npcs\0-psl\0-sc\0-nsob\0-nss\0-ts8\0", &exp_orig},
  374.  
  375.   {"o", PRO_BOOL, false, ON, &expect_output_file, &expect_output_file},
  376.  
  377.   {"pcs", PRO_BOOL, true, ON, &proc_calls_space, &exp_pcs},
  378.   {"psl", PRO_BOOL, true, ON, &procnames_start_line, &exp_psl},
  379.   {"ps", PRO_BOOL, false, ON, &pointer_as_binop, &exp_ps},
  380.   {"sc", PRO_BOOL, false, ON, &star_comment_cont, &exp_sc},
  381.   {"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines, &exp_sob},
  382.   {"ss", PRO_BOOL, false, ON, &space_sp_semicolon, &exp_ss},
  383.   {"st", PRO_BOOL, false, ON, &use_stdout, &exp_st},
  384.   {"troff", PRO_BOOL, false, ON, &troff, &exp_troff},
  385.   {"ts", PRO_INT, 8, ONOFF_NA, &tabsize, &exp_ts},
  386.   {"version", PRO_PRSTRING, 0, ONOFF_NA,
  387.    (int *) VERSION_STRING, &exp_version},
  388.   {"v", PRO_BOOL, false, ON, &verbose, &exp_v},
  389.  
  390. /* Signify end of structure.  */
  391.   {0, PRO_IGN, 0, ONOFF_NA, 0, 0}
  392. };
  393. #endif /* GNU defaults */
  394.  
  395. struct long_option_conversion
  396. {
  397.   char *long_name;
  398.   char *short_name;
  399. };
  400.  
  401. struct long_option_conversion option_conversions[] =
  402. {
  403.   {"blank-lines-after-ifdefs", "bacc"},
  404.   {"blank-lines-after-procedure-declarations", "badp"},
  405.   {"blank-lines-after-declarations", "bad"},
  406.   {"blank-lines-after-procedures", "bap"},
  407.   {"blank-lines-after-block-comments", "bbb"},
  408.   {"blank-lines-after-commas", "bc"},
  409.   {"brace-indent", "bli"},
  410.   {"braces-after-if-line", "bl"},
  411.   {"braces-on-if-line", "br"},
  412.   {"Bill-Shannon", "bs"},
  413.   {"blank-before-sizeof", "bs"},
  414.   {"comment-delimiters-on-blank-lines", "cdb"},
  415.   {"declaration-comment-column", "cd"},
  416.   {"cuddle-else", "ce"},
  417.   {"continuation-indentation", "ci"},
  418.   {"case-indentation", "cli"},
  419.   {"else-endif-column", "cp"},
  420.   {"space-after-cast", "cs"},
  421.   {"comment-indentation", "c"},
  422.   {"declaration-indentation", "di"},
  423.   {"left-justify-declarations", "dj"},
  424.   {"line-comments-indentation", "d"},
  425.   {"extra-expression-indentation", "eei"},
  426.   {"else-if", "ei"},
  427.   {"*", "fbc"},
  428.   {"*", "fbx"},
  429.   {"*", "fb"},
  430.   {"format-first-column-comments", "fc1"},
  431.   {"format-all-comments", "fca"},
  432.   {"*", "fc"},
  433.   {"*", "fk"},
  434.   {"*", "fs"},
  435.   {"gnu-style", "gnu"},
  436.   {"parameter-indentation", "ip"},
  437.   {"indentation-level", "i"},
  438.   {"indent-level", "i"},
  439.   {"k-and-r-style", "kr"},
  440.   {"kernighan-and-ritchie-style", "kr"},
  441.   {"kernighan-and-ritchie", "kr"},
  442.   {"*", "lc"},
  443.   {"continue-at-parentheses", "lp"},
  444.   {"leave-preprocessor-space", "lps"},
  445.   {"remove-preprocessor-space", "nlps"},
  446.   {"line-length", "l"},
  447.   {"no-blank-lines-after-ifdefs", "nbacc"},
  448.   {"no-blank-lines-after-procedure-declarations", "nbadp"},
  449.   {"no-blank-lines-after-declarations", "nbad"},
  450.   {"no-blank-lines-after-procedures", "nbap"},
  451.   {"no-blank-lines-after-block-comments", "nbbb"},
  452.   {"no-blank-lines-after-commas", "nbc"},
  453.   {"no-Bill-Shannon", "nbs"},
  454.   {"no-blank-before-sizeof", "nbs"},
  455.   {"no-comment-delimiters-on-blank-lines", "ncdb"},
  456.   {"dont-cuddle-else", "nce"},
  457.   {"no-space-after-casts", "ncs"},
  458.   {"dont-left-justify-declarations", "ndj"},
  459.   {"no-extra-expression-indentation", "neei"},
  460.   {"no-else-if", "nei"},
  461.   {"dont-format-first-column-comments", "nfc1"},
  462.   {"dont-format-comments", "nfca"},
  463.   {"no-parameter-indentation", "nip"},
  464.   {"dont-indent-parameters", "nip"},
  465.   {"dont-line-up-parentheses", "nlp"},
  466.   {"no-space-after-function-call-names", "npcs"},
  467.   {"ignore-profile", "npro"},
  468.   {"dont-break-procedure-type", "npsl"},
  469.   {"*", "nps"},
  470.   {"dont-star-comments", "nsc"},
  471.   {"leave-optional-blank-lines", "nsob"},
  472.   {"dont-space-special-semicolon", "nss"},
  473.   {"no-verbosity", "nv"},
  474.   {"output", "o"},
  475.   {"output-file", "o"},
  476.   {"original", "orig"},
  477.   {"original-style", "orig"},
  478.   {"berkeley-style", "orig"},
  479.   {"berkeley", "orig"},
  480.   {"space-after-procedure-calls", "pcs"},
  481.   {"procnames-start-lines", "psl"},
  482.   {"pointer-as-binary-op", "ps"},
  483.   {"start-left-side-of-comments", "sc"},
  484.   {"swallow-optional-blank-lines", "sob"},
  485.   {"space-special-semicolon", "ss"},
  486.   {"standard-output", "st"},
  487.   {"troff-formatting", "troff"},
  488.   {"tab-size", "ts"},
  489.   {"version", "version"},
  490.   {"verbose", "v"},
  491.   {0, 0},
  492. };
  493.  
  494. /* S1 should be a string.  S2 should be a string, perhaps followed by an
  495.    argument.  Compare the two, returning true if they are equal, and if they
  496.    are equal set *START_PARAM to point to the argument in S2.  */
  497.  
  498. static int
  499. eqin (s1, s2, start_param)
  500.      register char *s1;
  501.      register char *s2;
  502.      char **start_param;
  503. {
  504.   while (*s1)
  505.     {
  506.       if (*s1++ != *s2++)
  507.     return (false);
  508.     }
  509.   *start_param = s2;
  510.   return (true);
  511. }
  512.  
  513. /* Set the defaults. */
  514. void
  515. set_defaults ()
  516. {
  517.   register struct pro *p;
  518.  
  519.   for (p = pro; p->p_name; p++)
  520.     if (p->p_type == PRO_BOOL || p->p_type == PRO_INT)
  521.       *p->p_obj = p->p_default;
  522. }
  523.  
  524. /* Stings which can prefix an option, longest first. */
  525. static char *option_prefixes[] =
  526. {
  527.   "--",
  528.   "-",
  529.   "+",
  530.   0
  531. };
  532.  
  533. static int
  534. option_prefix (arg)
  535.      char *arg;
  536. {
  537.   register char **prefixes = option_prefixes;
  538.   register char *this_prefix, *argp;
  539.  
  540.   do
  541.     {
  542.       this_prefix = *prefixes;
  543.       argp = arg;
  544.       while (*this_prefix == *argp)
  545.     {
  546.       this_prefix++;
  547.       argp++;
  548.     }
  549.       if (*this_prefix == '\0')
  550.     return this_prefix - *prefixes;
  551.     }
  552.   while (*++prefixes);
  553.  
  554.   return 0;
  555. }
  556.  
  557. /* Process an option ARG (e.g. "-l60").  PARAM is a possible value
  558.    for ARG, if PARAM is nonzero.  EXPLICT should be nonzero iff the
  559.    argument is being explicitly specified (as opposed to being taken from a
  560.    PRO_SETTINGS group of settings).  */
  561.  
  562. int
  563. set_option (option, param, explicit)
  564.      char *option, *param;
  565.      int explicit;
  566. {
  567.   struct pro *p = pro;
  568.   char *param_start;
  569.   register int option_length, val;
  570.  
  571.   val = 0;
  572.   option_length = option_prefix (option);
  573.   if (option_length > 0)
  574.     {
  575.       if (option_length == 1 && *option == '-')
  576.     /* Short option prefix */
  577.     {
  578.       option++;
  579.       for (p = pro; p->p_name; p++)
  580.         if (*p->p_name == *option
  581.         && eqin (p->p_name, option, ¶m_start))
  582.           goto found;
  583.     }
  584.       else
  585.     /* Long prefix */
  586.     {
  587.       register struct long_option_conversion *o = option_conversions;
  588.       option += option_length;
  589.  
  590.       while (o->short_name)
  591.         {
  592.           if (eqin (o->long_name, option, ¶m_start))
  593.         break;
  594.           o++;
  595.         }
  596.  
  597.       /* Searching twice means we don't have to keep the two tables in
  598.          sync. */
  599.       if (o->short_name)
  600.         for (p = pro; p->p_name; p++)
  601.           if (!strcmp (p->p_name, o->short_name))
  602.         goto found;
  603.     }
  604.     }
  605.  
  606.   fprintf (stderr, "indent: unknown option \"%s\"\n", option - 1);
  607.   exit (1);
  608.  
  609. arg_missing:
  610.   fprintf (stderr, "indent: missing argument to parameter %s\n", option);
  611.   exit (1);
  612.  
  613. found:
  614.   /* If the parameter has been explicitly specified, we don't
  615.      want a group of bundled settings to override the explicit
  616.      setting.  */
  617.   if (verbose)
  618.     fprintf (stderr, "option: %s\n", p->p_name);
  619.   if (explicit || !*(p->p_explicit))
  620.     {
  621.       if (explicit)
  622.     *(p->p_explicit) = 1;
  623.  
  624.       switch (p->p_type)
  625.     {
  626.  
  627.     case PRO_PRSTRING:
  628.       puts ((char *) p->p_obj);
  629.       exit (0);
  630.  
  631.     case PRO_SETTINGS:
  632.       {
  633.         char *t;        /* current position */
  634.  
  635.         t = (char *) p->p_obj;
  636.         do
  637.           {
  638.         set_option (t, 0, 0);
  639.         /* advance to character following next NUL */
  640.         while (*t++);
  641.           }
  642.         while (*t);
  643.       }
  644.  
  645.     case PRO_IGN:
  646.       break;
  647.  
  648.     case PRO_KEY:
  649.       {
  650.         register char *str;
  651.         if (*param_start == 0)
  652.           if (!(param_start = param))
  653.         goto arg_missing;
  654.           else
  655.         val = 1;
  656.  
  657.         str = (char *) xmalloc (strlen (param_start) + 1);
  658.         strcpy (str, param_start);
  659.         addkey (str, rw_decl);
  660.       }
  661.       break;
  662.  
  663.     case PRO_BOOL:
  664.       if (p->p_special == OFF)
  665.         *p->p_obj = false;
  666.       else
  667.         *p->p_obj = true;
  668.       break;
  669.  
  670.     case PRO_INT:
  671.       if (*param_start == 0)
  672.         if (!(param_start = param))
  673.           goto arg_missing;
  674.         else
  675.           val = 1;
  676.       if (!isdigit (*param_start))
  677.         {
  678.           fprintf (stderr,
  679.              "indent: option ``%s'' requires a numeric parameter\n",
  680.                option - 1);
  681.           exit (1);
  682.         }
  683.       *p->p_obj = atoi (param_start);
  684.       break;
  685.  
  686.     case PRO_FONT:
  687.       if (*param_start == 0)
  688.         if (!(param_start = param))
  689.           goto arg_missing;
  690.         else
  691.           val = 1;
  692.       parsefont ((struct fstate *) p->p_obj, param_start);
  693.       break;
  694.  
  695.     default:
  696.       fprintf (stderr, "indent: set_option: internal error: p_type %d\n",
  697.            (int) p->p_type);
  698.       exit (1);
  699.     }
  700.     }
  701.  
  702.   return val;
  703. }
  704.  
  705.  
  706. /* Scan the options in the file F. */
  707.  
  708. static void
  709. scan_profile (f)
  710.      register FILE *f;
  711. {
  712.   register int i;
  713.   register char *p, *this, *next, *temp;
  714.   char b0[BUFSIZ];
  715.   char b1[BUFSIZ];
  716.  
  717.   next = b0;
  718.   this = 0;
  719.   while (1)
  720.     {
  721.       for (p = next; (i = getc (f)) != EOF && (*p = i) > ' '; ++p);
  722.  
  723.       if (p != next)
  724.     {
  725.       *p++ = 0;
  726.       if (!this)
  727.         {
  728.           this = b0;
  729.           next = b1;
  730.           continue;
  731.         }
  732.  
  733.       if (set_option (this, next, 1))
  734.         {
  735.           this = 0;
  736.           next = b0;
  737.           continue;
  738.         }
  739.  
  740.       temp = this;
  741.       this = next;
  742.       next = temp;
  743.     }
  744.       else if (i == EOF)
  745.     {
  746.       if (this)
  747.         set_option (this, 0, 1);
  748.       return;
  749.     }
  750.     }
  751. }
  752.  
  753.  
  754. /* Some operating systems don't allow more than one dot in a filename.  */
  755. #if defined (ONE_DOT_PER_FILENAME)
  756. #define INDENT_PROFILE "indent.pro"
  757. #else
  758. #define INDENT_PROFILE ".indent.pro"
  759. #endif
  760.  
  761. /* set_profile looks for ./.indent.pro or $HOME/.indent.pro, in
  762.    that order, and reads the options given in that file.  Return the
  763.    path of the file read.
  764.  
  765.    Note that as of version 1.3, indent only reads one file. */
  766.  
  767. char *
  768. set_profile ()
  769. {
  770.   register FILE *f;
  771.   char *fname;
  772.   static char prof[] = INDENT_PROFILE;
  773.   char *homedir;
  774.  
  775. #ifdef AMIGA
  776.   if ((f = fopen (prof, "r")) != NULL)
  777.     {
  778.       scan_profile (f);
  779.       (void) fclose (f);
  780.       fname = xmalloc (sizeof prof);
  781.       strcpy (fname, prof);
  782.       return fname;
  783.     }
  784.  
  785.   fname = xmalloc (sizeof prof + 5);
  786.   sprintf (fname, "S:%s", prof);
  787.  
  788.   if ((f = fopen (fname, "r")) != NULL)
  789.     {
  790.       scan_profile (f);
  791.       (void) fclose (f);
  792.       return fname;
  793.     }
  794. #else /* !AMIGA */
  795.   if ((f = fopen (INDENT_PROFILE, "r")) != NULL)
  796.     {
  797.       int len = strlen (INDENT_PROFILE) + 3;
  798.  
  799.       scan_profile (f);
  800.       (void) fclose (f);
  801.       fname = xmalloc (len);
  802.       fname[0] = '.';
  803.       fname[1] = '/';
  804.       memcpy (&fname[2], INDENT_PROFILE, len - 3);
  805.       fname[len - 1] = '\0';
  806.       return fname;
  807.     }
  808.  
  809.   homedir = getenv ("HOME");
  810.   if (homedir)
  811.     {
  812.       fname = xmalloc (strlen (homedir) + 10 + sizeof prof);
  813.       sprintf (fname, "%s/%s", homedir, prof);
  814.  
  815.       if ((f = fopen (fname, "r")) != NULL)
  816.     {
  817.       scan_profile (f);
  818.       (void) fclose (f);
  819.       return fname;
  820.     }
  821.  
  822.       free (fname);
  823.     }
  824. #endif /* !AMIGA */
  825.  
  826.   return 0;
  827. }
  828.