home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume21 / indent / part03 < prev    next >
Text File  |  1991-08-07  |  33KB  |  1,104 lines

  1. Newsgroups: comp.sources.misc,comp.lang.c++
  2. From: Benson I. Margulies <benson@odi.com>
  3. Subject:  v21i100:  indent - BSD Indent with C++ support, Part03/03
  4. Message-ID: <1991Aug8.015940.3376@sparky.IMD.Sterling.COM>
  5. X-Md4-Signature: 64faffc46f51124e951fdad0a2b3ee85
  6. Date: Thu, 8 Aug 1991 01:59:40 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: Benson I. Margulies <benson@odi.com>
  10. Posting-number: Volume 21, Issue 100
  11. Archive-name: indent/part03
  12. Environment: UNIX, C++
  13.  
  14. #! /bin/sh
  15. # into a shell via "sh file" or similar.  To overwrite existing files,
  16. # type "sh file -c".
  17. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  18. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  19. # Contents:  indent/args.c indent/parse.c indent/pr_comment.c
  20. # Wrapped by kent@sparky on Wed Aug  7 19:44:13 1991
  21. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  22. echo If this archive is complete, you will see the following message:
  23. echo '          "shar: End of archive 3 (of 3)."'
  24. if test -f 'indent/args.c' -a "${1}" != "-c" ; then 
  25.   echo shar: Will not clobber existing file \"'indent/args.c'\"
  26. else
  27.   echo shar: Extracting \"'indent/args.c'\" \(8848 characters\)
  28.   sed "s/^X//" >'indent/args.c' <<'END_OF_FILE'
  29. X/*
  30. X * Copyright (c) 1985 Sun Microsystems, Inc.
  31. X * Copyright (c) 1980 The Regents of the University of California.
  32. X * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  33. X * All rights reserved.
  34. X *
  35. X * Redistribution and use in source and binary forms are permitted
  36. X * provided that the above copyright notice and this paragraph are
  37. X * duplicated in all such forms and that any documentation,
  38. X * advertising materials, and other materials related to such
  39. X * distribution and use acknowledge that the software was developed
  40. X * by the University of California, Berkeley, the University of Illinois,
  41. X * Urbana, and Sun Microsystems, Inc.  The name of either University
  42. X * or Sun Microsystems may not be used to endorse or promote products
  43. X * derived from this software without specific prior written permission.
  44. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  45. X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  46. X * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  47. X */
  48. X
  49. X#ifndef lint
  50. Xstatic char sccsid[] = "@(#)args.c    5.6 (Berkeley) 9/15/88";
  51. X#endif /* not lint */
  52. X
  53. X/*
  54. X * Argument scanning and profile reading code.  Default parameters are set
  55. X * here as well.
  56. X */
  57. X
  58. X#include "indent_globs.h"
  59. X#include <sys/types.h>
  60. X#include <ctype.h>
  61. X
  62. Xchar       *getenv(), *index();
  63. X
  64. X/* profile types */
  65. X#define    PRO_SPECIAL    1    /* special case */
  66. X#define    PRO_BOOL    2    /* boolean */
  67. X#define    PRO_INT        3    /* integer */
  68. X#define PRO_FONT    4    /* troff font */
  69. X
  70. X/* profile specials for booleans */
  71. X#define    ON        1    /* turn it on */
  72. X#define    OFF        0    /* turn it off */
  73. X
  74. X/* profile specials for specials */
  75. X#define    IGN        1    /* ignore it */
  76. X#define    CLI        2    /* case label indent (float) */
  77. X#define    STDIN        3    /* use stdin */
  78. X#define    KEY        4    /* type (keyword) */
  79. X#define    CCI        5    /* case code indent (float) */
  80. X
  81. X/*
  82. X * N.B.: because of the way the table here is scanned, options whose names are
  83. X * substrings of other options must occur later; that is, with -lp vs -l, -lp
  84. X * must be first.  Also, while (most) booleans occur more than once, the last
  85. X * default value is the one actually assigned.
  86. X */
  87. Xstruct pro {
  88. X    char       *p_name;        /* name, eg -bl, -cli */
  89. X    int         p_type;        /* type (int, bool, special) */
  90. X    int         p_default;    /* the default value (if int) */
  91. X    int         p_special;    /* depends on type */
  92. X    int        *p_obj;        /* the associated variable */
  93. X}           pro[] = {
  94. X
  95. X    "T", PRO_SPECIAL, 0, KEY, 0,
  96. X    "bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation,
  97. X    "badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop,
  98. X    "bad", PRO_BOOL, false, ON, &blanklines_after_declarations,
  99. X    "bap", PRO_BOOL, false, ON, &blanklines_after_procs,
  100. X    "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments,
  101. X    "bc", PRO_BOOL, true, OFF, &ps.leave_comma,
  102. X    "bl", PRO_BOOL, false, OFF, &btype_2,
  103. X    "brr", PRO_BOOL, false, ON, &btype_3,
  104. X    "br", PRO_BOOL, true, ON, &btype_2,
  105. X    "bs", PRO_BOOL, false, ON, &Bill_Shannon,
  106. X    "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline,
  107. X    "cd", PRO_INT, 0, 0, &ps.decl_com_ind,
  108. X    "ce", PRO_BOOL, true, ON, &cuddle_else,
  109. X    "ci", PRO_INT, 0, 0, &continuation_indent,
  110. X    "cli", PRO_SPECIAL, 0, CLI, 0,
  111. X    "cci", PRO_SPECIAL, 0, CCI, 0,
  112. X    "c", PRO_INT, 33, 0, &ps.com_ind,
  113. X    "di", PRO_INT, 16, 0, &ps.decl_indent,
  114. X    "dj", PRO_BOOL, false, ON, &ps.ljust_decl,
  115. X    "d", PRO_INT, 0, 0, &ps.unindent_displace,
  116. X    "eei", PRO_BOOL, false, ON, &extra_expression_indent,
  117. X    "ei", PRO_BOOL, true, ON, &ps.else_if,
  118. X    "fbc", PRO_FONT, 0, 0, (int *) &blkcomf,
  119. X    "fbx", PRO_FONT, 0, 0, (int *) &boxcomf,
  120. X    "fb", PRO_FONT, 0, 0, (int *) &bodyf,
  121. X    "fc1", PRO_BOOL, true, ON, &format_col1_comments,
  122. X    "fc", PRO_FONT, 0, 0, (int *) &scomf,
  123. X    "fk", PRO_FONT, 0, 0, (int *) &keywordf,
  124. X    "fs", PRO_FONT, 0, 0, (int *) &stringf,
  125. X    "ip", PRO_BOOL, true, ON, &ps.indent_parameters,
  126. X    "i", PRO_INT, 8, 0, &ps.ind_size,
  127. X    "lc", PRO_INT, 0, 0, &block_comment_max_col,
  128. X    "lp", PRO_BOOL, true, ON, &lineup_to_parens,
  129. X    "l", PRO_INT, 78, 0, &max_col,
  130. X    "nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation,
  131. X    "nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop,
  132. X    "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations,
  133. X    "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs,
  134. X    "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments,
  135. X    "nbc", PRO_BOOL, true, ON, &ps.leave_comma,
  136. X    "nbs", PRO_BOOL, false, OFF, &Bill_Shannon,
  137. X    "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline,
  138. X    "nce", PRO_BOOL, true, OFF, &cuddle_else,
  139. X    "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl,
  140. X    "neei", PRO_BOOL, false, OFF, &extra_expression_indent,
  141. X    "nei", PRO_BOOL, true, OFF, &ps.else_if,
  142. X    "nfc1", PRO_BOOL, true, OFF, &format_col1_comments,
  143. X    "nip", PRO_BOOL, true, OFF, &ps.indent_parameters,
  144. X    "nlp", PRO_BOOL, true, OFF, &lineup_to_parens,
  145. X    "npcs", PRO_BOOL, false, OFF, &proc_calls_space,
  146. X    "npro", PRO_SPECIAL, 0, IGN, 0,
  147. X    "nprs", PRO_BOOL, false, OFF, &parens_space,
  148. X    "npsl", PRO_BOOL, true, OFF, &procnames_start_line,
  149. X    "nps", PRO_BOOL, false, OFF, &pointer_as_binop,
  150. X    "nsc", PRO_BOOL, true, OFF, &star_comment_cont,
  151. X    "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines,
  152. X    "nv", PRO_BOOL, false, OFF, &verbose,
  153. X    "pcs", PRO_BOOL, false, ON, &proc_calls_space,
  154. X    "prs", PRO_BOOL, false, ON, &parens_space,
  155. X    "psl", PRO_BOOL, true, ON, &procnames_start_line,
  156. X    "ps", PRO_BOOL, false, ON, &pointer_as_binop,
  157. X    "sc", PRO_BOOL, true, ON, &star_comment_cont,
  158. X    "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines,
  159. X    "st", PRO_SPECIAL, 0, STDIN, 0,
  160. X    "troff", PRO_BOOL, false, ON, &troff,
  161. X    "v", PRO_BOOL, false, ON, &verbose,
  162. X     "+", PRO_BOOL, false, ON, &cplus,
  163. X /* whew! */
  164. X    0, 0, 0, 0, 0
  165. X};
  166. X
  167. X/*
  168. X * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
  169. X * given in these files.
  170. X */
  171. Xset_profile()
  172. X{
  173. X    register FILE *f;
  174. X    char        fname[BUFSIZ];
  175. X    static char prof[] = ".indent.pro";
  176. X
  177. X    sprintf(fname, "%s/%s", getenv("HOME"), prof);
  178. X    if ((f = fopen(fname, "r")) != NULL) {
  179. X    scan_profile(f);
  180. X    (void) fclose(f);
  181. X    }
  182. X    if ((f = fopen(prof, "r")) != NULL) {
  183. X    scan_profile(f);
  184. X    (void) fclose(f);
  185. X    }
  186. X}
  187. X
  188. Xscan_profile(f)
  189. X    register FILE *f;
  190. X{
  191. X    register int i;
  192. X    register char *p;
  193. X    char        buf[BUFSIZ];
  194. X
  195. X    while (1) {
  196. X    for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p);
  197. X    if (p != buf) {
  198. X        *p++ = 0;
  199. X        if (verbose)
  200. X        printf("profile: %s\n", buf);
  201. X        set_option(buf);
  202. X    }
  203. X    else if (i == EOF)
  204. X        return;
  205. X    }
  206. X}
  207. X
  208. Xchar       *param_start;
  209. X
  210. Xeqin(s1, s2)
  211. X    register char *s1;
  212. X    register char *s2;
  213. X{
  214. X    while (*s1) {
  215. X    if (*s1++ != *s2++)
  216. X        return (false);
  217. X    }
  218. X    param_start = s2;
  219. X    return (true);
  220. X}
  221. X
  222. X/*
  223. X * Set the defaults.
  224. X */
  225. Xset_defaults()
  226. X{
  227. X    register struct pro *p;
  228. X
  229. X    /*
  230. X     * Because ps.case_indent and ps.case_code_indent are floats, we can't
  231. X     * initialize them from the table:
  232. X     */
  233. X    ps.case_indent = 0.0;    /* -cli0.0 */
  234. X    ps.case_code_indent = 1.0;    /* -cci1.0 */
  235. X    for (p = pro; p->p_name; p++)
  236. X    if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
  237. X        *p->p_obj = p->p_default;
  238. X}
  239. X
  240. Xset_option(arg)
  241. X    register char *arg;
  242. X{
  243. X    register struct pro *p;
  244. X    extern double atof();
  245. X
  246. X    arg++;            /* ignore leading "-" */
  247. X    for (p = pro; p->p_name; p++)
  248. X    if (*p->p_name == *arg && eqin(p->p_name, arg))
  249. X        goto found;
  250. X    fprintf(stderr, "indent: unknown parameter \"%s\"\n", arg - 1);
  251. X    exit(1);
  252. Xfound:
  253. X    switch (p->p_type) {
  254. X
  255. X    case PRO_SPECIAL:
  256. X    switch (p->p_special) {
  257. X
  258. X    case IGN:
  259. X        break;
  260. X
  261. X    case CLI:
  262. X        if (*param_start == 0)
  263. X        goto need_param;
  264. X        ps.case_indent = atof(param_start);
  265. X        break;
  266. X
  267. X    case CCI:
  268. X        if (*param_start == 0)
  269. X        goto need_param;
  270. X        ps.case_code_indent = atof(param_start);
  271. X        break;
  272. X
  273. X    case STDIN:
  274. X        if (input == 0)
  275. X        input = stdin;
  276. X        if (output == 0)
  277. X        output = stdout;
  278. X        break;
  279. X
  280. X    case KEY:
  281. X        if (*param_start == 0)
  282. X        goto need_param;
  283. X        {
  284. X        register char *str = (char *) malloc(strlen(param_start) + 1);
  285. X        strcpy(str, param_start);
  286. X        addkey(str, 4);
  287. X        }
  288. X        break;
  289. X
  290. X    default:
  291. X        fprintf(stderr, "\
  292. Xindent: set_option: internal error: p_special %d\n", p->p_special);
  293. X        exit(1);
  294. X    }
  295. X    break;
  296. X
  297. X    case PRO_BOOL:
  298. X    if (p->p_special == OFF)
  299. X        *p->p_obj = false;
  300. X    else
  301. X        *p->p_obj = true;
  302. X    break;
  303. X
  304. X    case PRO_INT:
  305. X    if (*param_start == 0) {
  306. X    need_param:
  307. X        fprintf(stderr, "indent: ``%s'' requires a parameter\n",
  308. X            arg - 1);
  309. X        exit(1);
  310. X    }
  311. X    *p->p_obj = atoi(param_start);
  312. X    break;
  313. X
  314. X    case PRO_FONT:
  315. X    parsefont((struct fstate *) p->p_obj, param_start);
  316. X    break;
  317. X
  318. X    default:
  319. X    fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
  320. X        p->p_type);
  321. X    exit(1);
  322. X    }
  323. X}
  324. END_OF_FILE
  325.   if test 8848 -ne `wc -c <'indent/args.c'`; then
  326.     echo shar: \"'indent/args.c'\" unpacked with wrong size!
  327.   fi
  328.   # end of 'indent/args.c'
  329. fi
  330. if test -f 'indent/parse.c' -a "${1}" != "-c" ; then 
  331.   echo shar: Will not clobber existing file \"'indent/parse.c'\"
  332. else
  333.   echo shar: Extracting \"'indent/parse.c'\" \(8434 characters\)
  334.   sed "s/^X//" >'indent/parse.c' <<'END_OF_FILE'
  335. X/*
  336. X * Copyright (c) 1985 Sun Microsystems, Inc.
  337. X * Copyright (c) 1980 The Regents of the University of California.
  338. X * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  339. X * All rights reserved.
  340. X *
  341. X * Redistribution and use in source and binary forms are permitted
  342. X * provided that the above copyright notice and this paragraph are
  343. X * duplicated in all such forms and that any documentation,
  344. X * advertising materials, and other materials related to such
  345. X * distribution and use acknowledge that the software was developed
  346. X * by the University of California, Berkeley, the University of Illinois,
  347. X * Urbana, and Sun Microsystems, Inc.  The name of either University
  348. X * or Sun Microsystems may not be used to endorse or promote products
  349. X * derived from this software without specific prior written permission.
  350. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  351. X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  352. X * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  353. X */
  354. X
  355. X#ifndef lint
  356. Xstatic char sccsid[] = "@(#)parse.c    5.8 (Berkeley) 9/15/88";
  357. X#endif /* not lint */
  358. X
  359. X#include "./indent_globs.h"
  360. X#include "./indent_codes.h"
  361. X
  362. X
  363. X
  364. X
  365. Xparse(tk)
  366. X    int         tk;        /* the code for the construct scanned */
  367. X{
  368. X    int         i;
  369. X
  370. X#ifdef debug
  371. X    printf("%2d - %s\n", tk, token);
  372. X#endif
  373. X
  374. X    while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
  375. X    /* true if we have an if without an else */
  376. X    ps.p_stack[ps.tos] = stmt;    /* apply the if(..) stmt ::= stmt
  377. X                     * reduction */
  378. X    reduce();        /* see if this allows any reduction */
  379. X    }
  380. X
  381. X
  382. X    switch (tk) {        /* go on and figure out what to do with the
  383. X                 * input */
  384. X
  385. X    case decl:            /* scanned a declaration word */
  386. X    ps.search_brace = btype_2 && !btype_3;
  387. X    /* indicate that following brace should be on same line */
  388. X    if (ps.p_stack[ps.tos] != decl) {    /* only put one declaration
  389. X                         * onto stack */
  390. X        break_comma = true;    /* while in declaration, newline should be
  391. X                 * forced after comma */
  392. X        ps.p_stack[++ps.tos] = decl;
  393. X        ps.il[ps.tos] = ps.i_l_follow;
  394. X
  395. X        if (ps.ljust_decl) {/* only do if we want left justified
  396. X                 * declarations */
  397. X        ps.ind_level = 0;
  398. X        for (i = ps.tos - 1; i > 0; --i)
  399. X            if (ps.p_stack[i] == decl)
  400. X            ++ps.ind_level;    /* indentation is number of
  401. X                     * declaration levels deep we are */
  402. X        ps.i_l_follow = ps.ind_level;
  403. X        }
  404. X    }
  405. X    break;
  406. X
  407. X    case ifstmt:        /* scanned if (...) */
  408. X    if (ps.p_stack[ps.tos] == elsehead && ps.else_if)    /* "else if ..." */
  409. X        ps.i_l_follow = ps.il[ps.tos];
  410. X    case dolit:        /* 'do' */
  411. X    case forstmt:        /* for (...) */
  412. X    ps.p_stack[++ps.tos] = tk;
  413. X    ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
  414. X    ++ps.i_l_follow;    /* subsequent statements should be indented 1 */
  415. X    ps.search_brace = btype_2 && !btype_3;
  416. X    break;
  417. X
  418. X    case lbrace:        /* scanned { */
  419. X    break_comma = false;    /* don't break comma in an initial list */
  420. X    if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
  421. X        || ps.p_stack[ps.tos] == stmtl)
  422. X        ++ps.i_l_follow;    /* it is a random, isolated stmt group or a
  423. X                 * declaration */
  424. X    else {
  425. X        if (s_code == e_code) {
  426. X        /*
  427. X         * only do this if there is nothing on the line
  428. X         */
  429. X        --ps.ind_level;
  430. X        /*
  431. X         * it is a group as part of a while, for, etc.
  432. X         */
  433. X        if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1 &&
  434. X            ps.case_code_indent >= 1)
  435. X            --ps.ind_level;
  436. X        /*
  437. X         * for a switch, brace should be two levels out from the code
  438. X         */
  439. X        }
  440. X    }
  441. X
  442. X    ps.p_stack[++ps.tos] = lbrace;
  443. X    ps.il[ps.tos] = ps.ind_level;
  444. X    ps.p_stack[++ps.tos] = stmt;
  445. X    /* allow null stmt between braces */
  446. X    ps.il[ps.tos] = ps.i_l_follow;
  447. X    break;
  448. X
  449. X    case whilestmt:        /* scanned while (...) */
  450. X    if (ps.p_stack[ps.tos] == dohead) {
  451. X        /* it is matched with do stmt */
  452. X        ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
  453. X        ps.p_stack[++ps.tos] = whilestmt;
  454. X        ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
  455. X    }
  456. X    else {            /* it is a while loop */
  457. X        ps.p_stack[++ps.tos] = whilestmt;
  458. X        ps.il[ps.tos] = ps.i_l_follow;
  459. X        ++ps.i_l_follow;
  460. X        ps.search_brace = btype_2 && !btype_3;
  461. X    }
  462. X
  463. X    break;
  464. X
  465. X    case elselit:        /* scanned an else */
  466. X
  467. X    if (ps.p_stack[ps.tos] != ifhead)
  468. X        diag(1, "Unmatched 'else'");
  469. X    else {
  470. X        ps.ind_level = ps.il[ps.tos];    /* indentation for else should
  471. X                         * be same as for if */
  472. X        ps.i_l_follow = ps.ind_level + 1;    /* everything following should
  473. X                         * be in 1 level */
  474. X        ps.p_stack[ps.tos] = elsehead;
  475. X        /* remember if with else */
  476. X        ps.search_brace = (btype_2 && !btype_3) | ps.else_if;
  477. X    }
  478. X    break;
  479. X
  480. X    case rbrace:        /* scanned a } */
  481. X    /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
  482. X    if (ps.p_stack[ps.tos - 1] == lbrace) {
  483. X        if (btype_3)
  484. X        ps.i_l_follow = ps.il[--ps.tos];
  485. X        else
  486. X        ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
  487. X        ps.p_stack[ps.tos] = stmt;
  488. X    }
  489. X    else
  490. X        diag(1, "Stmt nesting error.");
  491. X    break;
  492. X
  493. X    case swstmt:        /* had switch (...) */
  494. X    ps.p_stack[++ps.tos] = swstmt;
  495. X    ps.cstk[ps.tos] = case_ind;
  496. X    /* save current case indent level */
  497. X    ps.il[ps.tos] = ps.i_l_follow;
  498. X    case_ind = ps.i_l_follow + ps.case_indent;
  499. X    /*
  500. X     * cases should be one level down from switch
  501. X     */
  502. X    ps.i_l_follow += ps.case_indent + ps.case_code_indent;
  503. X    /*
  504. X     * statements should be two levels in
  505. X     */
  506. X    ps.search_brace = btype_2 && !btype_3;
  507. X    break;
  508. X
  509. X    case semicolon:        /* this indicates a simple stmt */
  510. X    break_comma = false;    /* turn off flag to break after commas in a
  511. X                 * declaration */
  512. X    ps.p_stack[++ps.tos] = stmt;
  513. X    ps.il[ps.tos] = ps.ind_level;
  514. X    break;
  515. X
  516. X    default:            /* this is an error */
  517. X    diag(1, "Unknown code to parser");
  518. X    return;
  519. X
  520. X
  521. X    }                /* end of switch */
  522. X
  523. X    reduce();            /* see if any reduction can be done */
  524. X
  525. X#ifdef debug
  526. X    for (i = 1; i <= ps.tos; ++i)
  527. X    printf("(%d %d)", ps.p_stack[i], ps.il[i]);
  528. X    printf("\n");
  529. X#endif
  530. X
  531. X    return;
  532. X}
  533. X
  534. X/*
  535. X * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
  536. X * 
  537. X * All rights reserved
  538. X * 
  539. X * 
  540. X * NAME: reduce
  541. X * 
  542. X * FUNCTION: Implements the reduce part of the parsing algorithm
  543. X * 
  544. X * ALGORITHM: The following reductions are done.  Reductions are repeated until
  545. X * no more are possible.
  546. X * 
  547. X * Old TOS        New TOS <stmt> <stmt>    <stmtl> <stmtl> <stmt>    <stmtl> do
  548. X * <stmt>    "dostmt" if <stmt>    "ifstmt" switch <stmt>    <stmt> decl
  549. X * <stmt>    <stmt> "ifelse" <stmt>    <stmt> for <stmt>    <stmt> while
  550. X * <stmt>    <stmt> "dostmt" while    <stmt>
  551. X * 
  552. X * On each reduction, ps.i_l_follow (the indentation for the following line) is
  553. X * set to the indentation level associated with the old TOS.
  554. X * 
  555. X * PARAMETERS: None
  556. X * 
  557. X * RETURNS: Nothing
  558. X * 
  559. X * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
  560. X * 
  561. X * CALLS: None
  562. X * 
  563. X * CALLED BY: parse 
  564. X * 
  565. X * HISTORY: initial coding     November 1976    D A Willcox of CAC
  566. X * 
  567. X */
  568. X/*----------------------------------------------*\
  569. X|   REDUCTION PHASE                    |
  570. X\*----------------------------------------------*/
  571. Xreduce()
  572. X{
  573. X
  574. X    register int i;
  575. X
  576. X    for (;;) {            /* keep looping until there is nothing left to
  577. X                 * reduce */
  578. X
  579. X    switch (ps.p_stack[ps.tos]) {
  580. X
  581. X    case stmt:
  582. X        switch (ps.p_stack[ps.tos - 1]) {
  583. X
  584. X        case stmt:
  585. X        case stmtl:
  586. X        /* stmtl stmt or stmt stmt */
  587. X        ps.p_stack[--ps.tos] = stmtl;
  588. X        break;
  589. X
  590. X        case dolit:    /* <do> <stmt> */
  591. X        ps.p_stack[--ps.tos] = dohead;
  592. X        ps.i_l_follow = ps.il[ps.tos];
  593. X        break;
  594. X
  595. X        case ifstmt:
  596. X        /* <if> <stmt> */
  597. X        ps.p_stack[--ps.tos] = ifhead;
  598. X        for (i = ps.tos - 1;
  599. X            (
  600. X             ps.p_stack[i] != stmt
  601. X             &&
  602. X             ps.p_stack[i] != stmtl
  603. X             &&
  604. X             ps.p_stack[i] != lbrace
  605. X             );
  606. X            --i);
  607. X        ps.i_l_follow = ps.il[i];
  608. X        /*
  609. X         * for the time being, we will assume that there is no else on
  610. X         * this if, and set the indentation level accordingly. If an
  611. X         * else is scanned, it will be fixed up later
  612. X         */
  613. X        break;
  614. X
  615. X        case swstmt:
  616. X        /* <switch> <stmt> */
  617. X        case_ind = ps.cstk[ps.tos - 1];
  618. X
  619. X        case decl:        /* finish of a declaration */
  620. X        case elsehead:
  621. X        /* <<if> <stmt> else> <stmt> */
  622. X        case forstmt:
  623. X        /* <for> <stmt> */
  624. X        case whilestmt:
  625. X        /* <while> <stmt> */
  626. X        ps.p_stack[--ps.tos] = stmt;
  627. X        ps.i_l_follow = ps.il[ps.tos];
  628. X        break;
  629. X
  630. X        default:        /* <anything else> <stmt> */
  631. X        return;
  632. X
  633. X        }            /* end of section for <stmt> on top of stack */
  634. X        break;
  635. X
  636. X    case whilestmt:    /* while (...) on top */
  637. X        if (ps.p_stack[ps.tos - 1] == dohead) {
  638. X        /* it is termination of a do while */
  639. X        ps.p_stack[--ps.tos] = stmt;
  640. X        break;
  641. X        }
  642. X        else
  643. X        return;
  644. X
  645. X    default:        /* anything else on top */
  646. X        return;
  647. X
  648. X    }
  649. X    }
  650. X}
  651. END_OF_FILE
  652.   if test 8434 -ne `wc -c <'indent/parse.c'`; then
  653.     echo shar: \"'indent/parse.c'\" unpacked with wrong size!
  654.   fi
  655.   # end of 'indent/parse.c'
  656. fi
  657. if test -f 'indent/pr_comment.c' -a "${1}" != "-c" ; then 
  658.   echo shar: Will not clobber existing file \"'indent/pr_comment.c'\"
  659. else
  660.   echo shar: Extracting \"'indent/pr_comment.c'\" \(12009 characters\)
  661.   sed "s/^X//" >'indent/pr_comment.c' <<'END_OF_FILE'
  662. X/*
  663. X * Copyright (c) 1985 Sun Microsystems, Inc.
  664. X * Copyright (c) 1980 The Regents of the University of California.
  665. X * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  666. X * All rights reserved.
  667. X *
  668. X * Redistribution and use in source and binary forms are permitted
  669. X * provided that the above copyright notice and this paragraph are
  670. X * duplicated in all such forms and that any documentation,
  671. X * advertising materials, and other materials related to such
  672. X * distribution and use acknowledge that the software was developed
  673. X * by the University of California, Berkeley, the University of Illinois,
  674. X * Urbana, and Sun Microsystems, Inc.  The name of either University
  675. X * or Sun Microsystems may not be used to endorse or promote products
  676. X * derived from this software without specific prior written permission.
  677. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  678. X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  679. X * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  680. X */
  681. X
  682. X#ifndef lint
  683. Xstatic char sccsid[] = "@(#)pr_comment.c    5.9 (Berkeley) 9/15/88";
  684. X#endif /* not lint */
  685. X
  686. X/*
  687. X * NAME:
  688. X *    pr_comment
  689. X *
  690. X * FUNCTION:
  691. X *    This routine takes care of scanning and printing comments.
  692. X *
  693. X * ALGORITHM:
  694. X *    1) Decide where the comment should be aligned, and if lines should
  695. X *       be broken.
  696. X *    2) If lines should not be broken and filled, just copy up to end of
  697. X *       comment.
  698. X *    3) If lines should be filled, then scan thru input_buffer copying
  699. X *       characters to com_buf.  Remember where the last blank, tab, or
  700. X *       newline was.  When line is filled, print up to last blank and
  701. X *       continue copying.
  702. X *
  703. X * HISTORY:
  704. X *    November 1976    D A Willcox of CAC    Initial coding
  705. X *    12/6/76        D A Willcox of CAC    Modification to handle
  706. X *                        UNIX-style comments
  707. X *
  708. X */
  709. X
  710. X/*
  711. X * this routine processes comments.  It makes an attempt to keep comments from
  712. X * going over the max line length.  If a line is too long, it moves everything
  713. X * from the last blank to the next comment line.  Blanks and tabs from the
  714. X * beginning of the input line are removed
  715. X */
  716. X
  717. X
  718. X#include "indent_globs.h"
  719. X
  720. X
  721. Xpr_comment()
  722. X{
  723. X    int         now_col;    /* column we are in now */
  724. X    int         adj_max_col;    /* Adjusted max_col for when we decide to
  725. X                 * spill comments over the right margin */
  726. X    char       *last_bl;    /* points to the last blank in the output
  727. X                 * buffer */
  728. X    char       *t_ptr;        /* used for moving string */
  729. X    int         unix_comment;    /* tri-state variable used to decide if it is
  730. X                 * a unix-style comment. 0 means only blanks
  731. X                 * since /*, 1 means regular style comment, 2
  732. X                 * means unix style comment */
  733. X    int         break_delim = comment_delimiter_on_blankline;
  734. X    int         l_just_saw_decl = ps.just_saw_decl;
  735. X    /*
  736. X     * int         ps.last_nl = 0;    /* true iff the last significant thing
  737. X     * weve seen is a newline
  738. X     */
  739. X    int         one_liner = 1;    /* true iff this comment is a one-liner */
  740. X    adj_max_col = max_col;
  741. X    ps.just_saw_decl = 0;
  742. X    last_bl = 0;        /* no blanks found so far */
  743. X    ps.box_com = false;        /* at first, assume that we are not in
  744. X                     * a boxed comment or some other
  745. X                     * comment that should not be touched */
  746. X    ++ps.out_coms;        /* keep track of number of comments */
  747. X    unix_comment = 1;        /* set flag to let us figure out if there is a
  748. X                 * unix-style comment ** DISABLED: use 0 to
  749. X                 * reenable this hack! */
  750. X
  751. X    /* Figure where to align and how to treat the comment */
  752. X
  753. X    if (ps.col_1 && !format_col1_comments) {    /* if comment starts in column
  754. X                         * 1 it should not be touched */
  755. X    ps.box_com = true;
  756. X    ps.com_col = 1;
  757. X    }
  758. X    else {
  759. X    if (*buf_ptr == '-' || *buf_ptr == '*') {
  760. X        ps.box_com = true;    /* a comment with a '-' or '*' immediately
  761. X                 * after the /* is assumed to be a boxed
  762. X                 * comment */
  763. X        break_delim = 0;
  764. X    }
  765. X    if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
  766. X        /* klg: check only if this line is blank */
  767. X        /*
  768. X         * If this (*and previous lines are*) blank, dont put comment way
  769. X         * out at left
  770. X         */
  771. X        ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
  772. X        adj_max_col = block_comment_max_col;
  773. X        if (ps.com_col <= 1)
  774. X        ps.com_col = 1 + !format_col1_comments;
  775. X    }
  776. X    else {
  777. X        register    target_col;
  778. X        break_delim = 0;
  779. X        if (s_code != e_code)
  780. X        target_col = count_spaces(compute_code_target(), s_code);
  781. X        else {
  782. X        target_col = 1;
  783. X        if (s_lab != e_lab)
  784. X            target_col = count_spaces(compute_label_target(), s_lab);
  785. X        }
  786. X        ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
  787. X        if (ps.com_col < target_col)
  788. X        ps.com_col = ((target_col + 7) & ~7) + 1;
  789. X        if (ps.com_col + 24 > adj_max_col)
  790. X        adj_max_col = ps.com_col + 24;
  791. X    }
  792. X    }
  793. X    if (ps.box_com) {
  794. X    buf_ptr[-2] = 0;
  795. X    ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
  796. X    buf_ptr[-2] = '/';
  797. X    }
  798. X    else {
  799. X    ps.n_comment_delta = 0;
  800. X    while (*buf_ptr == ' ' || *buf_ptr == '\t')
  801. X        buf_ptr++;
  802. X    }
  803. X    ps.comment_delta = 0;
  804. X    *e_com++ = '/';        /* put '/*' into buffer */
  805. X    if (ps.cc_comment)        /*  (or '//') */
  806. X    *e_com++ = '/';
  807. X    else
  808. X    *e_com++ = '*';
  809. X    if (*buf_ptr != ' ' && !ps.box_com)
  810. X    *e_com++ = ' ';
  811. X
  812. X    *e_com = '\0';
  813. X    if (troff) {
  814. X    now_col = 1;
  815. X    adj_max_col = 80;
  816. X    }
  817. X    else
  818. X    now_col = count_spaces(ps.com_col, s_com);    /* figure what column we
  819. X                             * would be in if we
  820. X                             * printed the comment
  821. X                             * now */
  822. X
  823. X    /* Start to copy the comment */
  824. X
  825. X    while (1) {            /* this loop will go until the comment is
  826. X                 * copied */
  827. X    if (*buf_ptr >= 040 && *buf_ptr != '*')
  828. X        ps.last_nl = 0;
  829. X    check_size(com);
  830. X    switch (*buf_ptr) {    /* this checks for various spcl cases */
  831. X    case 014:        /* check for a form feed */
  832. X        if (!ps.box_com) {    /* in a text comment, break the line here */
  833. X        ps.use_ff = true;
  834. X        /* fix so dump_line uses a form feed */
  835. X        dump_line();
  836. X        last_bl = 0;
  837. X        *e_com++ = ' ';
  838. X        *e_com++ = '*';
  839. X        *e_com++ = ' ';
  840. X        while (*++buf_ptr == ' ' || *buf_ptr == '\t');
  841. X        }
  842. X        else {
  843. X        if (++buf_ptr >= buf_end)
  844. X            fill_buffer();
  845. X        *e_com++ = 014;
  846. X        }
  847. X        break;
  848. X
  849. X    case '\n':
  850. X        if (ps.cc_comment) {
  851. X        *e_com = '\0';
  852. X        dump_line();
  853. X        ps.cc_comment = 0;
  854. X        ps.just_saw_decl = l_just_saw_decl; /* ?? */
  855. X        if (++buf_ptr >= buf_end)    /* eat '\n' */
  856. X            fill_buffer();
  857. X        return;
  858. X        }
  859. X        if (had_eof) {    /* check for unexpected eof */
  860. X        printf("Unterminated comment\n");
  861. X        *e_com = '\0';
  862. X        dump_line();
  863. X        return;
  864. X        }
  865. X        one_liner = 0;
  866. X        if (ps.box_com || ps.last_nl) {    /* if this is a boxed comment,
  867. X                         * we dont ignore the newline */
  868. X        if (s_com == e_com) {
  869. X            *e_com++ = ' ';
  870. X            *e_com++ = ' ';
  871. X        }
  872. X        *e_com = '\0';
  873. X        if (!ps.box_com && e_com - s_com > 3) {
  874. X            if (break_delim == 1 && s_com[0] == '/'
  875. X                && s_com[1] == '*' && s_com[2] == ' ') {
  876. X            char       *t = e_com;
  877. X            break_delim = 2;
  878. X            e_com = s_com + 2;
  879. X            *e_com = 0;
  880. X            if (blanklines_before_blockcomments)
  881. X                prefix_blankline_requested = 1;
  882. X            dump_line();
  883. X            e_com = t;
  884. X            s_com[0] = s_com[1] = s_com[2] = ' ';
  885. X            }
  886. X            dump_line();
  887. X            check_size(com);
  888. X            *e_com++ = ' ';
  889. X            *e_com++ = ' ';
  890. X        }
  891. X        dump_line();
  892. X        now_col = ps.com_col;
  893. X        }
  894. X        else {
  895. X        ps.last_nl = 1;
  896. X        if (unix_comment != 1) {    /* we not are in unix_style
  897. X                         * comment */
  898. X            if (unix_comment == 0 && s_code == e_code) {
  899. X            /*
  900. X             * if it is a UNIX-style comment, ignore the
  901. X             * requirement that previous line be blank for
  902. X             * unindention
  903. X             */
  904. X            ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
  905. X            if (ps.com_col <= 1)
  906. X                ps.com_col = 2;
  907. X            }
  908. X            unix_comment = 2;    /* permanently remember that we are in
  909. X                     * this type of comment */
  910. X            dump_line();
  911. X            ++line_no;
  912. X            now_col = ps.com_col;
  913. X            *e_com++ = ' ';
  914. X            /*
  915. X             * fix so that the star at the start of the line will line
  916. X             * up
  917. X             */
  918. X            do        /* flush leading white space */
  919. X            if (++buf_ptr >= buf_end)
  920. X                fill_buffer();
  921. X            while (*buf_ptr == ' ' || *buf_ptr == '\t');
  922. X            break;
  923. X        }
  924. X        if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
  925. X            last_bl = e_com - 1;
  926. X        /*
  927. X         * if there was a space at the end of the last line, remember
  928. X         * where it was
  929. X         */
  930. X        else {        /* otherwise, insert one */
  931. X            last_bl = e_com;
  932. X            check_size(com);
  933. X            *e_com++ = ' ';
  934. X            ++now_col;
  935. X        }
  936. X        }
  937. X        ++line_no;        /* keep track of input line number */
  938. X        if (!ps.box_com) {
  939. X        int         nstar = 1;
  940. X        do {        /* flush any blanks and/or tabs at start of
  941. X                 * next line */
  942. X            if (++buf_ptr >= buf_end)
  943. X            fill_buffer();
  944. X            if (*buf_ptr == '*' && --nstar >= 0) {
  945. X            if (++buf_ptr >= buf_end)
  946. X                fill_buffer();
  947. X            if (*buf_ptr == '/')
  948. X                goto end_of_comment;
  949. X            }
  950. X        } while (*buf_ptr == ' ' || *buf_ptr == '\t');
  951. X        }
  952. X        else if (++buf_ptr >= buf_end)
  953. X        fill_buffer();
  954. X        break;        /* end of case for newline */
  955. X
  956. X    case '*':        /* must check for possibility of being at end
  957. X                 * of comment */
  958. X        if (++buf_ptr >= buf_end)    /* get to next char after * */
  959. X        fill_buffer();
  960. X
  961. X        if (unix_comment == 0)    /* set flag to show we are not in
  962. X                     * unix-style comment */
  963. X        unix_comment = 1;
  964. X
  965. X        if (*buf_ptr == '/') {    /* it is the end!!! */
  966. X    end_of_comment:
  967. X        if (++buf_ptr >= buf_end)
  968. X            fill_buffer();
  969. X
  970. X        if (*(e_com - 1) != ' ' && !ps.box_com) {    /* insure blank before
  971. X                                 * end */
  972. X            *e_com++ = ' ';
  973. X            ++now_col;
  974. X        }
  975. X        if (break_delim == 1 && !one_liner && s_com[0] == '/'
  976. X            && s_com[1] == '*' && s_com[2] == ' ') {
  977. X            char       *t = e_com;
  978. X            break_delim = 2;
  979. X            e_com = s_com + 2;
  980. X            *e_com = 0;
  981. X            if (blanklines_before_blockcomments)
  982. X            prefix_blankline_requested = 1;
  983. X            dump_line();
  984. X            e_com = t;
  985. X            s_com[0] = s_com[1] = s_com[2] = ' ';
  986. X        }
  987. X        if (break_delim == 2 && e_com > s_com + 3
  988. X             /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
  989. X            *e_com = '\0';
  990. X            dump_line();
  991. X            now_col = ps.com_col;
  992. X        }
  993. X        check_size(com);
  994. X        *e_com++ = '*';
  995. X        *e_com++ = '/';
  996. X        *e_com = '\0';
  997. X        ps.just_saw_decl = l_just_saw_decl;
  998. X        return;
  999. X        }
  1000. X        else {        /* handle isolated '*' */
  1001. X        *e_com++ = '*';
  1002. X        ++now_col;
  1003. X        }
  1004. X        break;
  1005. X    default:        /* we have a random char */
  1006. X        if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
  1007. X        unix_comment = 1;    /* we are not in unix-style comment */
  1008. X
  1009. X        *e_com = *buf_ptr++;
  1010. X        if (buf_ptr >= buf_end)
  1011. X        fill_buffer();
  1012. X
  1013. X        if (*e_com == '\t')    /* keep track of column */
  1014. X        now_col = ((now_col - 1) & tabmask) + tabsize + 1;
  1015. X        else if (*e_com == '\b')    /* this is a backspace */
  1016. X        --now_col;
  1017. X        else
  1018. X        ++now_col;
  1019. X
  1020. X        if (*e_com == ' ' || *e_com == '\t')
  1021. X        last_bl = e_com;
  1022. X        /* remember we saw a blank */
  1023. X
  1024. X        ++e_com;
  1025. X        if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') {
  1026. X        /*
  1027. X         * the comment is too long, it must be broken up
  1028. X         */
  1029. X        if (break_delim == 1 && s_com[0] == '/'
  1030. X            && s_com[1] == '*' && s_com[2] == ' ') {
  1031. X            char       *t = e_com;
  1032. X            break_delim = 2;
  1033. X            e_com = s_com + 2;
  1034. X            *e_com = 0;
  1035. X            if (blanklines_before_blockcomments)
  1036. X            prefix_blankline_requested = 1;
  1037. X            dump_line();
  1038. X            e_com = t;
  1039. X            s_com[0] = s_com[1] = s_com[2] = ' ';
  1040. X        }
  1041. X        if (last_bl == 0) {    /* we have seen no blanks */
  1042. X            last_bl = e_com;    /* fake it */
  1043. X            *e_com++ = ' ';
  1044. X        }
  1045. X        *e_com = '\0';    /* print what we have */
  1046. X        *last_bl = '\0';
  1047. X        while (last_bl > s_com && last_bl[-1] < 040)
  1048. X            *--last_bl = 0;
  1049. X        e_com = last_bl;
  1050. X        dump_line();
  1051. X
  1052. X        *e_com++ = ' ';    /* add blanks for continuation */
  1053. X        *e_com++ = ' ';
  1054. X        *e_com++ = ' ';
  1055. X
  1056. X        t_ptr = last_bl + 1;
  1057. X        last_bl = 0;
  1058. X        if (t_ptr >= e_com) {
  1059. X            while (*t_ptr == ' ' || *t_ptr == '\t')
  1060. X            t_ptr++;
  1061. X            while (*t_ptr != '\0') {    /* move unprinted part of
  1062. X                         * comment down in buffer */
  1063. X            if (*t_ptr == ' ' || *t_ptr == '\t')
  1064. X                last_bl = e_com;
  1065. X            *e_com++ = *t_ptr++;
  1066. X            }
  1067. X        }
  1068. X        *e_com = '\0';
  1069. X        now_col = count_spaces(ps.com_col, s_com);    /* recompute current
  1070. X                                 * position */
  1071. X        }
  1072. X        break;
  1073. X    }
  1074. X    }
  1075. X}
  1076. END_OF_FILE
  1077.   if test 12009 -ne `wc -c <'indent/pr_comment.c'`; then
  1078.     echo shar: \"'indent/pr_comment.c'\" unpacked with wrong size!
  1079.   fi
  1080.   # end of 'indent/pr_comment.c'
  1081. fi
  1082. echo shar: End of archive 3 \(of 3\).
  1083. cp /dev/null ark3isdone
  1084. MISSING=""
  1085. for I in 1 2 3 ; do
  1086.     if test ! -f ark${I}isdone ; then
  1087.     MISSING="${MISSING} ${I}"
  1088.     fi
  1089. done
  1090. if test "${MISSING}" = "" ; then
  1091.     echo You have unpacked all 3 archives.
  1092.     rm -f ark[1-9]isdone
  1093. else
  1094.     echo You still must unpack the following archives:
  1095.     echo "        " ${MISSING}
  1096. fi
  1097. exit 0
  1098. exit 0 # Just in case...
  1099. -- 
  1100. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1101. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1102. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1103. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1104.