home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume18 / indent / part02 < prev    next >
Text File  |  1989-03-20  |  44KB  |  1,595 lines

  1. Subject:  v18i050:  Indent, C reformatting program, Part02/03
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: Ozan Yigit <oz@nexus.yorku.ca>
  7. Posting-number: Volume 18, Issue 50
  8. Archive-name: indent/part02
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then unpack
  12. # it by saving it into a file and typing "sh file".  To overwrite existing
  13. # files, type "sh file -c".  You can also feed this as standard input via
  14. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  15. # will see the following message at the end:
  16. #        "End of archive 2 (of 3)."
  17. # Contents:  indent/io.c indent/lexi.c indent/pr_comment.c
  18. # Wrapped by oz@yunexus on Thu Mar  9 18:11:05 1989
  19. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  20. if test -f 'indent/io.c' -a "${1}" != "-c" ; then 
  21.   echo shar: Will not clobber existing file \"'indent/io.c'\"
  22. else
  23. echo shar: Extracting \"'indent/io.c'\" \(14572 characters\)
  24. sed "s/^X//" >'indent/io.c' <<'END_OF_FILE'
  25. X/*
  26. X * Copyright (c) 1985 Sun Microsystems, Inc.
  27. X * Copyright (c) 1980 The Regents of the University of California.
  28. X * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  29. X * All rights reserved.
  30. X *
  31. X * Redistribution and use in source and binary forms are permitted
  32. X * provided that the above copyright notice and this paragraph are
  33. X * duplicated in all such forms and that any documentation,
  34. X * advertising materials, and other materials related to such
  35. X * distribution and use acknowledge that the software was developed
  36. X * by the University of California, Berkeley, the University of Illinois,
  37. X * Urbana, and Sun Microsystems, Inc.  The name of either University
  38. X * or Sun Microsystems may not be used to endorse or promote products
  39. X * derived from this software without specific prior written permission.
  40. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  41. X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  42. X * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  43. X */
  44. X
  45. X#ifndef lint
  46. Xstatic char sccsid[] = "@(#)io.c    5.10 (Berkeley) 9/15/88";
  47. X#endif /* not lint */
  48. X
  49. X#include "indent_globs.h"
  50. X#include <ctype.h>
  51. X
  52. X
  53. Xint         comment_open;
  54. Xstatic      paren_target;
  55. X
  56. Xdump_line()
  57. X{                /* dump_line is the routine that actually
  58. X                 * effects the printing of the new source. It
  59. X                 * prints the label section, followed by the
  60. X                 * code section with the appropriate nesting
  61. X                 * level, followed by any comments */
  62. X    register int cur_col,
  63. X                target_col;
  64. X    static      not_first_line;
  65. X
  66. X    if (ps.procname[0]) {
  67. X    if (troff) {
  68. X        if (comment_open) {
  69. X        comment_open = 0;
  70. X        fprintf(output, ".*/\n");
  71. X        }
  72. X        fprintf(output, ".Pr \"%s\"\n", ps.procname);
  73. X    }
  74. X    ps.ind_level = 0;
  75. X    ps.procname[0] = 0;
  76. X    }
  77. X    if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
  78. X    if (suppress_blanklines > 0)
  79. X        suppress_blanklines--;
  80. X    else {
  81. X        ps.bl_line = true;
  82. X        n_real_blanklines++;
  83. X    }
  84. X    }
  85. X    else if (!inhibit_formatting) {
  86. X    suppress_blanklines = 0;
  87. X    ps.bl_line = false;
  88. X    if (prefix_blankline_requested && not_first_line)
  89. X        if (swallow_optional_blanklines) {
  90. X        if (n_real_blanklines == 1)
  91. X            n_real_blanklines = 0;
  92. X        }
  93. X        else {
  94. X        if (n_real_blanklines == 0)
  95. X            n_real_blanklines = 1;
  96. X        }
  97. X    while (--n_real_blanklines >= 0)
  98. X        putc('\n', output);
  99. X    n_real_blanklines = 0;
  100. X    if (ps.ind_level == 0)
  101. X        ps.ind_stmt = 0;    /* this is a class A kludge. dont do
  102. X                 * additional statement indentation if we are
  103. X                 * at bracket level 0 */
  104. X
  105. X    if (e_lab != s_lab || e_code != s_code)
  106. X        ++code_lines;    /* keep count of lines with code */
  107. X
  108. X
  109. X    if (e_lab != s_lab) {    /* print lab, if any */
  110. X        if (comment_open) {
  111. X        comment_open = 0;
  112. X        fprintf(output, ".*/\n");
  113. X        }
  114. X        while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
  115. X        e_lab--;
  116. X        cur_col = pad_output(1, compute_label_target());
  117. X        fprintf(output, "%.*s", e_lab - s_lab, s_lab);
  118. X        cur_col = count_spaces(cur_col, s_lab);
  119. X    }
  120. X    else
  121. X        cur_col = 1;    /* there is no label section */
  122. X
  123. X    ps.pcase = false;
  124. X
  125. X    if (s_code != e_code) {    /* print code section, if any */
  126. X        register char *p;
  127. X
  128. X        if (comment_open) {
  129. X        comment_open = 0;
  130. X        fprintf(output, ".*/\n");
  131. X        }
  132. X        target_col = compute_code_target();
  133. X        {
  134. X        register    i;
  135. X
  136. X        for (i = 0; i < ps.p_l_follow; i++)
  137. X            if (ps.paren_indents[i] >= 0)
  138. X            ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
  139. X        }
  140. X        cur_col = pad_output(cur_col, target_col);
  141. X        for (p = s_code; p < e_code; p++)
  142. X        if (*p == (char) 0200)
  143. X            fprintf(output, "%d", target_col * 7);
  144. X        else
  145. X            putc(*p, output);
  146. X        cur_col = count_spaces(cur_col, s_code);
  147. X    }
  148. X    if (s_com != e_com)
  149. X        if (troff) {
  150. X        int         all_here = 0;
  151. X        register char *p;
  152. X
  153. X        if (e_com[-1] == '/' && e_com[-2] == '*')
  154. X            e_com -= 2, all_here++;
  155. X        while (e_com > s_com && e_com[-1] == ' ')
  156. X            e_com--;
  157. X        *e_com = 0;
  158. X        p = s_com;
  159. X        while (*p == ' ')
  160. X            p++;
  161. X        if (p[0] == '/' && p[1] == '*')
  162. X            p += 2, all_here++;
  163. X        else if (p[0] == '*')
  164. X            p += p[1] == '/' ? 2 : 1;
  165. X        while (*p == ' ')
  166. X            p++;
  167. X        if (*p == 0)
  168. X            goto inhibit_newline;
  169. X        if (comment_open < 2 && ps.box_com) {
  170. X            comment_open = 0;
  171. X            fprintf(output, ".*/\n");
  172. X        }
  173. X        if (comment_open == 0) {
  174. X            if ('a' <= *p && *p <= 'z')
  175. X            *p = *p + 'A' - 'a';
  176. X            if (e_com - p < 50 && all_here == 2) {
  177. X            register char *follow = p;
  178. X            fprintf(output, "\n.nr C! \\w\1");
  179. X            while (follow < e_com) {
  180. X                switch (*follow) {
  181. X                case '\n':
  182. X                putc(' ', output);
  183. X                case 1:
  184. X                break;
  185. X                case '\\':
  186. X                putc('\\', output);
  187. X                default:
  188. X                putc(*follow, output);
  189. X                }
  190. X                follow++;
  191. X            }
  192. X            putc(1, output);
  193. X            }
  194. X            fprintf(output, "\n./* %dp %d %dp\n",
  195. X                ps.com_col * 7,
  196. X                (s_code != e_code || s_lab != e_lab) - ps.box_com,
  197. X                target_col * 7);
  198. X        }
  199. X        comment_open = 1 + ps.box_com;
  200. X        while (*p) {
  201. X            if (*p == BACKSLASH)
  202. X            putc(BACKSLASH, output);
  203. X            putc(*p++, output);
  204. X        }
  205. X        }
  206. X        else {        /* print comment, if any */
  207. X        register    target = ps.com_col;
  208. X        register char *com_st = s_com;
  209. X
  210. X        target += ps.comment_delta;
  211. X        while (*com_st == '\t')
  212. X            com_st++, target += 8;    /* ? */
  213. X        while (target <= 0)
  214. X            if (*com_st == ' ')
  215. X            target++, com_st++;
  216. X            else if (*com_st == '\t')
  217. X            target = ((target - 1) & ~7) + 9, com_st++;
  218. X            else
  219. X            target = 1;
  220. X        if (cur_col > target) {    /* if comment cant fit on this line,
  221. X                     * put it on next line */
  222. X            putc('\n', output);
  223. X            cur_col = 1;
  224. X            ++ps.out_lines;
  225. X        }
  226. X        while (e_com > com_st && isspace(e_com[-1]))
  227. X            e_com--;
  228. X        cur_col = pad_output(cur_col, target);
  229. X        if (!ps.box_com) {
  230. X            if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1))
  231. X            if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
  232. X                com_st[1] = '*';
  233. X            else
  234. X                fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
  235. X        }
  236. X        fwrite(com_st, e_com - com_st, 1, output);
  237. X        ps.comment_delta = ps.n_comment_delta;
  238. X        cur_col = count_spaces(cur_col, com_st);
  239. X        ++ps.com_lines;    /* count lines with comments */
  240. X        }
  241. X    if (ps.use_ff)
  242. X        putc('\014', output);
  243. X    else
  244. X        putc('\n', output);
  245. Xinhibit_newline:
  246. X    ++ps.out_lines;
  247. X    if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
  248. X        prefix_blankline_requested = 1;
  249. X        ps.just_saw_decl = 0;
  250. X    }
  251. X    else
  252. X        prefix_blankline_requested = postfix_blankline_requested;
  253. X    postfix_blankline_requested = 0;
  254. X    }
  255. X    ps.decl_on_line = ps.in_decl;    /* if we are in the middle of a
  256. X                     * declaration, remember that fact for
  257. X                     * proper comment indentation */
  258. X    ps.ind_stmt = ps.in_stmt & ~ps.in_decl;    /* next line should be
  259. X                         * indented if we have not
  260. X                         * completed this stmt and if
  261. X                         * we are not in the middle of
  262. X                         * a declaration */
  263. X    ps.use_ff = false;
  264. X    ps.dumped_decl_indent = 0;
  265. X    *(e_lab = s_lab) = '\0';    /* reset buffers */
  266. X    *(e_code = s_code) = '\0';
  267. X    *(e_com = s_com) = '\0';
  268. X    ps.ind_level = ps.i_l_follow;
  269. X    ps.paren_level = ps.p_l_follow;
  270. X    paren_target = -ps.paren_indents[ps.paren_level - 1];
  271. X    not_first_line = 1;
  272. X    return;
  273. X};
  274. X
  275. Xcompute_code_target()
  276. X{
  277. X    register    target_col = ps.ind_size * ps.ind_level + 1;
  278. X
  279. X    if (ps.paren_level)
  280. X    if (!lineup_to_parens)
  281. X        target_col += continuation_indent * ps.paren_level;
  282. X    else {
  283. X        register    w;
  284. X        register    t = paren_target;
  285. X
  286. X        if ((w = count_spaces(t, s_code) - max_col) > 0
  287. X            && count_spaces(target_col, s_code) <= max_col) {
  288. X        t -= w + 1;
  289. X        if (t > target_col)
  290. X            target_col = t;
  291. X        }
  292. X        else
  293. X        target_col = t;
  294. X    }
  295. X    else if (ps.ind_stmt)
  296. X    target_col += continuation_indent;
  297. X    return target_col;
  298. X}
  299. X
  300. Xcompute_label_target()
  301. X{
  302. X    return
  303. X    ps.pcase ? (int) (case_ind * ps.ind_size) + 1
  304. X    : *s_lab == '#' ? 1
  305. X    : ps.ind_size * (ps.ind_level - label_offset) + 1;
  306. X}
  307. X
  308. X
  309. X/*
  310. X * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
  311. X * 
  312. X * All rights reserved
  313. X * 
  314. X * 
  315. X * NAME: fill_buffer
  316. X * 
  317. X * FUNCTION: Reads one block of input into input_buffer
  318. X * 
  319. X * HISTORY: initial coding     November 1976    D A Willcox of CAC 1/7/77 A
  320. X * Willcox of CAC    Added check for switch back to partly full input
  321. X * buffer from temporary buffer
  322. X * 
  323. X */
  324. Xint
  325. Xfill_buffer()
  326. X{                /* this routine reads stuff from the input */
  327. X    register char *p;
  328. X    register int i;
  329. X    register FILE *f = input;
  330. X
  331. X    if (bp_save != 0) {        /* there is a partly filled input buffer left */
  332. X    buf_ptr = bp_save;    /* dont read anything, just switch buffers */
  333. X    buf_end = be_save;
  334. X    bp_save = be_save = 0;
  335. X    if (buf_ptr < buf_end)
  336. X        return;        /* only return if there is really something in
  337. X                 * this buffer */
  338. X    }
  339. X    for (p = buf_ptr = in_buffer;;) {
  340. X    if ((i = getc(f)) == EOF) {
  341. X        *p++ = ' ';
  342. X        *p++ = '\n';
  343. X        had_eof = true;
  344. X        break;
  345. X    }
  346. X    *p++ = i;
  347. X    if (i == '\n')
  348. X        break;
  349. X    }
  350. X    buf_end = p;
  351. X    if (p[-2] == '/' && p[-3] == '*') {
  352. X    if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
  353. X        fill_buffer();    /* flush indent error message */
  354. X    else {
  355. X        int         com = 0;
  356. X
  357. X        p = in_buffer;
  358. X        while (*p == ' ' || *p == '\t')
  359. X        p++;
  360. X        if (*p == '/' && p[1] == '*') {
  361. X        p += 2;
  362. X        while (*p == ' ' || *p == '\t')
  363. X            p++;
  364. X        if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
  365. X            && p[4] == 'N' && p[5] == 'T') {
  366. X            p += 6;
  367. X            while (*p == ' ' || *p == '\t')
  368. X            p++;
  369. X            if (*p == '*')
  370. X            com = 1;
  371. X            else if (*p == 'O')
  372. X            if (*++p == 'N')
  373. X                p++, com = 1;
  374. X            else if (*p == 'F' && *++p == 'F')
  375. X                p++, com = 2;
  376. X            while (*p == ' ' || *p == '\t')
  377. X            p++;
  378. X            if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
  379. X            if (s_com != e_com || s_lab != e_lab || s_code != e_code)
  380. X                dump_line();
  381. X            if (!(inhibit_formatting = com - 1)) {
  382. X                n_real_blanklines = 0;
  383. X                postfix_blankline_requested = 0;
  384. X                prefix_blankline_requested = 0;
  385. X                suppress_blanklines = 1;
  386. X            }
  387. X            }
  388. X        }
  389. X        }
  390. X    }
  391. X    }
  392. X    if (inhibit_formatting) {
  393. X    p = in_buffer;
  394. X    do
  395. X        putc(*p, output);
  396. X    while (*p++ != '\n');
  397. X    }
  398. X    return;
  399. X};
  400. X
  401. X/*
  402. X * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
  403. X * 
  404. X * All rights reserved
  405. X * 
  406. X * 
  407. X * NAME: pad_output
  408. X * 
  409. X * FUNCTION: Writes tabs and spaces to move the current column up to the desired
  410. X * position.
  411. X * 
  412. X * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
  413. X * 
  414. X * PARAMETERS: current        integer        The current column target
  415. X * nteger        The desired column
  416. X * 
  417. X * RETURNS: Integer value of the new column.  (If current >= target, no action is
  418. X * taken, and current is returned.
  419. X * 
  420. X * GLOBALS: None
  421. X * 
  422. X * CALLS: write (sys)
  423. X * 
  424. X * CALLED BY: dump_line
  425. X * 
  426. X * HISTORY: initial coding     November 1976    D A Willcox of CAC
  427. X * 
  428. X */
  429. Xpad_output(current, target)    /* writes tabs and blanks (if necessary) to
  430. X                 * get the current output position up to the
  431. X                 * target column */
  432. X    int         current;    /* the current column value */
  433. X    int         target;        /* position we want it at */
  434. X{
  435. X    register int curr;        /* internal column pointer */
  436. X    register int tcur;
  437. X
  438. X    if (troff)
  439. X    fprintf(output, "\\h'|%dp'", (target - 1) * 7);
  440. X    else {
  441. X    if (current >= target)
  442. X        return (current);    /* line is already long enough */
  443. X    curr = current;
  444. X    while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
  445. X        putc('\t', output);
  446. X        curr = tcur;
  447. X    }
  448. X    while (curr++ < target)
  449. X        putc(' ', output);    /* pad with final blanks */
  450. X    }
  451. X    return (target);
  452. X};
  453. X
  454. X/*
  455. X * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
  456. X * 
  457. X * All rights reserved
  458. X * 
  459. X * 
  460. X * NAME: count_spaces
  461. X * 
  462. X * FUNCTION: Find out where printing of a given string will leave the current
  463. X * character position on output.
  464. X * 
  465. X * ALGORITHM: Run thru input string and add appropriate values to current
  466. X * position.
  467. X * 
  468. X * RETURNS: Integer value of position after printing "buffer" starting in column
  469. X * "current".
  470. X * 
  471. X * HISTORY: initial coding     November 1976    D A Willcox of CAC
  472. X * 
  473. X */
  474. Xint
  475. Xcount_spaces(current, buffer)
  476. X/*
  477. X * this routine figures out where the character position will be after
  478. X * printing the text in buffer starting at column "current"
  479. X */
  480. X    int         current;
  481. X    char       *buffer;
  482. X{
  483. X    register char *buf;        /* used to look thru buffer */
  484. X    register int cur;        /* current character counter */
  485. X
  486. X    cur = current;
  487. X
  488. X    for (buf = buffer; *buf != '\0'; ++buf) {
  489. X    switch (*buf) {
  490. X
  491. X    case '\n':
  492. X    case 014:        /* form feed */
  493. X        cur = 1;
  494. X        break;
  495. X
  496. X    case '\t':
  497. X        cur = ((cur - 1) & tabmask) + tabsize + 1;
  498. X        break;
  499. X
  500. X    case '':        /* this is a backspace */
  501. X        --cur;
  502. X        break;
  503. X
  504. X    default:
  505. X        ++cur;
  506. X        break;
  507. X    }            /* end of switch */
  508. X    }                /* end of for loop */
  509. X    return (cur);
  510. X};
  511. X
  512. Xint    found_err;
  513. Xdiag(level, msg, a, b)
  514. X{
  515. X    if (level)
  516. X    found_err = 1;
  517. X    if (output == stdout) {
  518. X    fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
  519. X    fprintf(stdout, msg, a, b);
  520. X    fprintf(stdout, " */\n");
  521. X    }
  522. X    else {
  523. X    fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
  524. X    fprintf(stderr, msg, a, b);
  525. X    fprintf(stderr, "\n");
  526. X    }
  527. X}
  528. X
  529. Xwritefdef(f, nm)
  530. X    register struct fstate *f;
  531. X{
  532. X    fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
  533. X        nm, f->font, nm, f->size);
  534. X}
  535. X
  536. Xchar       *
  537. Xchfont(of, nf, s)
  538. X    register struct fstate *of,
  539. X               *nf;
  540. X    char       *s;
  541. X{
  542. X    if (of->font[0] != nf->font[0]
  543. X        || of->font[1] != nf->font[1]) {
  544. X    *s++ = '\\';
  545. X    *s++ = 'f';
  546. X    if (nf->font[1]) {
  547. X        *s++ = '(';
  548. X        *s++ = nf->font[0];
  549. X        *s++ = nf->font[1];
  550. X    }
  551. X    else
  552. X        *s++ = nf->font[0];
  553. X    }
  554. X    if (nf->size != of->size) {
  555. X    *s++ = '\\';
  556. X    *s++ = 's';
  557. X    if (nf->size < of->size) {
  558. X        *s++ = '-';
  559. X        *s++ = '0' + of->size - nf->size;
  560. X    }
  561. X    else {
  562. X        *s++ = '+';
  563. X        *s++ = '0' + nf->size - of->size;
  564. X    }
  565. X    }
  566. X    return s;
  567. X}
  568. X
  569. X
  570. Xparsefont(f, s0)
  571. X    register struct fstate *f;
  572. X    char       *s0;
  573. X{
  574. X    register char *s = s0;
  575. X    int         sizedelta = 0;
  576. X    bzero(f, sizeof *f);
  577. X    while (*s) {
  578. X    if (isdigit(*s))
  579. X        f->size = f->size * 10 + *s - '0';
  580. X    else if (isupper(*s))
  581. X        if (f->font[0])
  582. X        f->font[1] = *s;
  583. X        else
  584. X        f->font[0] = *s;
  585. X    else if (*s == 'c')
  586. X        f->allcaps = 1;
  587. X    else if (*s == '+')
  588. X        sizedelta++;
  589. X    else if (*s == '-')
  590. X        sizedelta--;
  591. X    else {
  592. X        fprintf(stderr, "indent: bad font specification: %s\n", s0);
  593. X        exit(1);
  594. X    }
  595. X    s++;
  596. X    }
  597. X    if (f->font[0] == 0)
  598. X    f->font[0] = 'R';
  599. X    if (bodyf.size == 0)
  600. X    bodyf.size = 11;
  601. X    if (f->size == 0)
  602. X    f->size = bodyf.size + sizedelta;
  603. X    else if (sizedelta > 0)
  604. X    f->size += bodyf.size;
  605. X    else
  606. X    f->size = bodyf.size - f->size;
  607. X}
  608. END_OF_FILE
  609. echo shar: 1 control character may be missing from \"'indent/io.c'\"
  610. if test 14572 -ne `wc -c <'indent/io.c'`; then
  611.     echo shar: \"'indent/io.c'\" unpacked with wrong size!
  612. fi
  613. # end of 'indent/io.c'
  614. fi
  615. if test -f 'indent/lexi.c' -a "${1}" != "-c" ; then 
  616.   echo shar: Will not clobber existing file \"'indent/lexi.c'\"
  617. else
  618. echo shar: Extracting \"'indent/lexi.c'\" \(13568 characters\)
  619. sed "s/^X//" >'indent/lexi.c' <<'END_OF_FILE'
  620. X/*
  621. X * Copyright (c) 1985 Sun Microsystems, Inc.
  622. X * Copyright (c) 1980 The Regents of the University of California.
  623. X * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  624. X * All rights reserved.
  625. X *
  626. X * Redistribution and use in source and binary forms are permitted
  627. X * provided that the above copyright notice and this paragraph are
  628. X * duplicated in all such forms and that any documentation,
  629. X * advertising materials, and other materials related to such
  630. X * distribution and use acknowledge that the software was developed
  631. X * by the University of California, Berkeley, the University of Illinois,
  632. X * Urbana, and Sun Microsystems, Inc.  The name of either University
  633. X * or Sun Microsystems may not be used to endorse or promote products
  634. X * derived from this software without specific prior written permission.
  635. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  636. X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  637. X * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  638. X */
  639. X
  640. X#ifndef lint
  641. Xstatic char sccsid[] = "@(#)lexi.c    5.11 (Berkeley) 9/15/88";
  642. X#endif /* not lint */
  643. X
  644. X/*
  645. X * Here we have the token scanner for indent.  It scans off one token and puts
  646. X * it in the global variable "token".  It returns a code, indicating the type
  647. X * of token scanned.
  648. X */
  649. X
  650. X#include "indent_globs.h"
  651. X#include "indent_codes.h"
  652. X#include "ctype.h"
  653. X
  654. X#define alphanum 1
  655. X#define opchar 3
  656. X
  657. Xstruct templ {
  658. X    char       *rwd;
  659. X    int         rwcode;
  660. X};
  661. X
  662. Xstruct templ specials[100] =
  663. X{
  664. X    "switch", 1,
  665. X    "case", 2,
  666. X    "break", 0,
  667. X    "struct", 3,
  668. X    "union", 3,
  669. X    "enum", 3,
  670. X    "default", 2,
  671. X    "int", 4,
  672. X    "char", 4,
  673. X    "float", 4,
  674. X    "double", 4,
  675. X    "long", 4,
  676. X    "short", 4,
  677. X    "typdef", 4,
  678. X    "unsigned", 4,
  679. X    "register", 4,
  680. X    "static", 4,
  681. X    "global", 4,
  682. X    "extern", 4,
  683. X    "void", 4,
  684. X    "goto", 0,
  685. X    "return", 0,
  686. X    "if", 5,
  687. X    "while", 5,
  688. X    "for", 5,
  689. X    "else", 6,
  690. X    "do", 6,
  691. X    "sizeof", 7,
  692. X    0, 0
  693. X};
  694. X
  695. Xchar        chartype[128] =
  696. X{                /* this is used to facilitate the decision of
  697. X                 * what type (alphanumeric, operator) each
  698. X                 * character is */
  699. X    0, 0, 0, 0, 0, 0, 0, 0,
  700. X    0, 0, 0, 0, 0, 0, 0, 0,
  701. X    0, 0, 0, 0, 0, 0, 0, 0,
  702. X    0, 0, 0, 0, 0, 0, 0, 0,
  703. X    0, 3, 0, 0, 1, 3, 3, 0,
  704. X    0, 0, 3, 3, 0, 3, 0, 3,
  705. X    1, 1, 1, 1, 1, 1, 1, 1,
  706. X    1, 1, 0, 0, 3, 3, 3, 3,
  707. X    0, 1, 1, 1, 1, 1, 1, 1,
  708. X    1, 1, 1, 1, 1, 1, 1, 1,
  709. X    1, 1, 1, 1, 1, 1, 1, 1,
  710. X    1, 1, 1, 0, 0, 0, 3, 1,
  711. X    0, 1, 1, 1, 1, 1, 1, 1,
  712. X    1, 1, 1, 1, 1, 1, 1, 1,
  713. X    1, 1, 1, 1, 1, 1, 1, 1,
  714. X    1, 1, 1, 0, 3, 0, 3, 0
  715. X};
  716. X
  717. X
  718. X
  719. X
  720. Xint
  721. Xlexi()
  722. X{
  723. X    register char *tok;        /* local pointer to next char in token */
  724. X    int         unary_delim;    /* this is set to 1 if the current token
  725. X                 * 
  726. X                 * forces a following operator to be unary */
  727. X    static int  last_code;    /* the last token type returned */
  728. X    static int  l_struct;    /* set to 1 if the last token was 'struct' */
  729. X    int         code;        /* internal code to be returned */
  730. X    char        qchar;        /* the delimiter character for a string */
  731. X
  732. X    tok = token;        /* point to start of place to save token */
  733. X    unary_delim = false;
  734. X    ps.col_1 = ps.last_nl;    /* tell world that this token started in
  735. X                 * column 1 iff the last thing scanned was nl */
  736. X    ps.last_nl = false;
  737. X
  738. X    while (*buf_ptr == ' ' || *buf_ptr == '\t') {    /* get rid of blanks */
  739. X    ps.col_1 = false;    /* leading blanks imply token is not in column
  740. X                 * 1 */
  741. X    if (++buf_ptr >= buf_end)
  742. X        fill_buffer();
  743. X    }
  744. X
  745. X    /* Scan an alphanumeric token */
  746. X    if (chartype[*buf_ptr] == alphanum || buf_ptr[0] == '.' && isdigit(buf_ptr[1])) {
  747. X    /*
  748. X     * we have a character or number
  749. X     */
  750. X    register char *j;    /* used for searching thru list of
  751. X                 * 
  752. X                 * reserved words */
  753. X    register struct templ *p;
  754. X
  755. X    if (isdigit(*buf_ptr) || buf_ptr[0] == '.' && isdigit(buf_ptr[1])) {
  756. X        int         seendot = 0,
  757. X                    seenexp = 0;
  758. X        if (*buf_ptr == '0' &&
  759. X            (buf_ptr[1] == 'x' || buf_ptr[1] == 'X')) {
  760. X        *tok++ = *buf_ptr++;
  761. X        *tok++ = *buf_ptr++;
  762. X        while (isxdigit(*buf_ptr))
  763. X            *tok++ = *buf_ptr++;
  764. X        }
  765. X        else
  766. X        while (1) {
  767. X            if (*buf_ptr == '.')
  768. X            if (seendot)
  769. X                break;
  770. X            else
  771. X                seendot++;
  772. X            *tok++ = *buf_ptr++;
  773. X            if (!isdigit(*buf_ptr) && *buf_ptr != '.')
  774. X            if ((*buf_ptr != 'E' && *buf_ptr != 'e') || seenexp)
  775. X                break;
  776. X            else {
  777. X                seenexp++;
  778. X                seendot++;
  779. X                *tok++ = *buf_ptr++;
  780. X                if (*buf_ptr == '+' || *buf_ptr == '-')
  781. X                *tok++ = *buf_ptr++;
  782. X            }
  783. X        }
  784. X        if (*buf_ptr == 'L' || *buf_ptr == 'l')
  785. X        *tok++ = *buf_ptr++;
  786. X    }
  787. X    else
  788. X        while (chartype[*buf_ptr] == alphanum) {    /* copy it over */
  789. X        *tok++ = *buf_ptr++;
  790. X        if (buf_ptr >= buf_end)
  791. X            fill_buffer();
  792. X        }
  793. X    *tok++ = '\0';
  794. X    while (*buf_ptr == ' ' || *buf_ptr == '\t') {    /* get rid of blanks */
  795. X        if (++buf_ptr >= buf_end)
  796. X        fill_buffer();
  797. X    }
  798. X    ps.its_a_keyword = false;
  799. X    ps.sizeof_keyword = false;
  800. X    if (l_struct) {        /* if last token was 'struct', then this token
  801. X                 * should be treated as a declaration */
  802. X        l_struct = false;
  803. X        last_code = ident;
  804. X        ps.last_u_d = true;
  805. X        return (decl);
  806. X    }
  807. X    ps.last_u_d = false;    /* Operator after indentifier is binary */
  808. X    last_code = ident;    /* Remember that this is the code we will
  809. X                 * return */
  810. X
  811. X    /*
  812. X     * This loop will check if the token is a keyword.
  813. X     */
  814. X    for (p = specials; (j = p->rwd) != 0; p++) {
  815. X        tok = token;    /* point at scanned token */
  816. X        if (*j++ != *tok++ || *j++ != *tok++)
  817. X        continue;    /* This test depends on the fact that
  818. X                 * identifiers are always at least 1 character
  819. X                 * long (ie. the first two bytes of the
  820. X                 * identifier are always meaningful) */
  821. X        if (tok[-1] == 0)
  822. X        break;        /* If its a one-character identifier */
  823. X        while (*tok++ == *j)
  824. X        if (*j++ == 0)
  825. X            goto found_keyword;    /* I wish that C had a multi-level
  826. X                     * break... */
  827. X    }
  828. X    if (p->rwd) {        /* we have a keyword */
  829. X    found_keyword:
  830. X        ps.its_a_keyword = true;
  831. X        ps.last_u_d = true;
  832. X        switch (p->rwcode) {
  833. X        case 1:        /* it is a switch */
  834. X        return (swstmt);
  835. X        case 2:        /* a case or default */
  836. X        return (casestmt);
  837. X
  838. X        case 3:        /* a "struct" */
  839. X        if (ps.p_l_follow)
  840. X            break;    /* inside parens: cast */
  841. X        l_struct = true;
  842. X
  843. X        /*
  844. X         * Next time around, we will want to know that we have had a
  845. X         * 'struct'
  846. X         */
  847. X        case 4:        /* one of the declaration keywords */
  848. X        if (ps.p_l_follow) {
  849. X            ps.cast_mask |= 1 << ps.p_l_follow;
  850. X            break;    /* inside parens: cast */
  851. X        }
  852. X        last_code = decl;
  853. X        return (decl);
  854. X
  855. X        case 5:        /* if, while, for */
  856. X        return (sp_paren);
  857. X
  858. X        case 6:        /* do, else */
  859. X        return (sp_nparen);
  860. X
  861. X        case 7:
  862. X        ps.sizeof_keyword = true;
  863. X        default:        /* all others are treated like any other
  864. X                 * identifier */
  865. X        return (ident);
  866. X        }            /* end of switch */
  867. X    }            /* end of if (found_it) */
  868. X    if (*buf_ptr == '(' && ps.tos <= 1 && ps.ind_level == 0) {
  869. X        register char *tp = buf_ptr;
  870. X        while (tp < buf_end)
  871. X        if (*tp++ == ')' && *tp == ';')
  872. X            goto not_proc;
  873. X        strncpy(ps.procname, token, sizeof ps.procname - 1);
  874. X        ps.in_parameter_declaration = 1;
  875. X    not_proc:;
  876. X    }
  877. X    /*
  878. X     * The following hack attempts to guess whether or not the current
  879. X     * token is in fact a declaration keyword -- one that has been
  880. X     * typedefd
  881. X     */
  882. X    if (((*buf_ptr == '*' && buf_ptr[1] != '=') || isalpha(*buf_ptr) || *buf_ptr == '_')
  883. X        && !ps.p_l_follow
  884. X            && !ps.block_init
  885. X        && (ps.last_token == rparen || ps.last_token == semicolon ||
  886. X            ps.last_token == decl ||
  887. X            ps.last_token == lbrace || ps.last_token == rbrace)) {
  888. X        ps.its_a_keyword = true;
  889. X        ps.last_u_d = true;
  890. X        last_code = decl;
  891. X        return decl;
  892. X    }
  893. X    if (last_code == decl)    /* if this is a declared variable, then
  894. X                 * following sign is unary */
  895. X        ps.last_u_d = true;    /* will make "int a -1" work */
  896. X    last_code = ident;
  897. X    return (ident);        /* the ident is not in the list */
  898. X    }                /* end of procesing for alpanum character */
  899. X    /* l l l Scan a non-alphanumeric token */
  900. X
  901. X    *tok++ = *buf_ptr;        /* if it is only a one-character token, it is
  902. X                 * moved here */
  903. X    *tok = '\0';
  904. X    if (++buf_ptr >= buf_end)
  905. X    fill_buffer();
  906. X
  907. X    switch (*token) {
  908. X    case '\n':
  909. X    unary_delim = ps.last_u_d;
  910. X    ps.last_nl = true;    /* remember that we just had a newline */
  911. X    code = (had_eof ? 0 : newline);
  912. X
  913. X    /*
  914. X     * if data has been exausted, the newline is a dummy, and we should
  915. X     * return code to stop
  916. X     */
  917. X    break;
  918. X
  919. X    case '\'':            /* start of quoted character */
  920. X    case '"':            /* start of string */
  921. X    qchar = *token;
  922. X    if (troff) {
  923. X        tok[-1] = '`';
  924. X        if (qchar == '"')
  925. X        *tok++ = '`';
  926. X        tok = chfont(&bodyf, &stringf, tok);
  927. X    }
  928. X    do {            /* copy the string */
  929. X        while (1) {        /* move one character or [/<char>]<char> */
  930. X        if (*buf_ptr == '\n') {
  931. X            printf("%d: Unterminated literal\n", line_no);
  932. X            goto stop_lit;
  933. X        }
  934. X        *tok = *buf_ptr++;
  935. X        if (buf_ptr >= buf_end)
  936. X            fill_buffer();
  937. X        if (had_eof || ((tok - token) > (bufsize - 2))) {
  938. X            printf("Unterminated literal\n");
  939. X            ++tok;
  940. X            goto stop_lit;
  941. X            /* get outof literal copying loop */
  942. X        }
  943. X        if (*tok == BACKSLASH) {    /* if escape, copy extra char */
  944. X            if (*buf_ptr == '\n')    /* check for escaped newline */
  945. X            ++line_no;
  946. X            if (troff) {
  947. X            *++tok = BACKSLASH;
  948. X            if (*buf_ptr == BACKSLASH)
  949. X                *++tok = BACKSLASH;
  950. X            }
  951. X            *++tok = *buf_ptr++;
  952. X            ++tok;    /* we must increment this again because we
  953. X                 * copied two chars */
  954. X            if (buf_ptr >= buf_end)
  955. X            fill_buffer();
  956. X        }
  957. X        else
  958. X            break;    /* we copied one character */
  959. X        }            /* end of while (1) */
  960. X    } while (*tok++ != qchar);
  961. X    if (troff) {
  962. X        tok = chfont(&stringf, &bodyf, tok - 1);
  963. X        if (qchar == '"')
  964. X        *tok++ = '\'';
  965. X    }
  966. Xstop_lit:
  967. X    code = ident;
  968. X    break;
  969. X
  970. X    case ('('):
  971. X    case ('['):
  972. X    unary_delim = true;
  973. X    code = lparen;
  974. X    break;
  975. X
  976. X    case (')'):
  977. X    case (']'):
  978. X    code = rparen;
  979. X    break;
  980. X
  981. X    case '#':
  982. X    unary_delim = ps.last_u_d;
  983. X    code = preesc;
  984. X    break;
  985. X
  986. X    case '?':
  987. X    unary_delim = true;
  988. X    code = question;
  989. X    break;
  990. X
  991. X    case (':'):
  992. X    code = colon;
  993. X    unary_delim = true;
  994. X    break;
  995. X
  996. X    case (';'):
  997. X    unary_delim = true;
  998. X    code = semicolon;
  999. X    break;
  1000. X
  1001. X    case ('{'):
  1002. X    unary_delim = true;
  1003. X
  1004. X    /*
  1005. X     * if (ps.in_or_st) ps.block_init = 1;
  1006. X     */
  1007. X    /* ?    code = ps.block_init ? lparen : lbrace; */
  1008. X    code = lbrace;
  1009. X    break;
  1010. X
  1011. X    case ('}'):
  1012. X    unary_delim = true;
  1013. X    /* ?    code = ps.block_init ? rparen : rbrace; */
  1014. X    code = rbrace;
  1015. X    break;
  1016. X
  1017. X    case 014:            /* a form feed */
  1018. X    unary_delim = ps.last_u_d;
  1019. X    ps.last_nl = true;    /* remember this so we can set 'ps.col_1'
  1020. X                 * right */
  1021. X    code = form_feed;
  1022. X    break;
  1023. X
  1024. X    case (','):
  1025. X    unary_delim = true;
  1026. X    code = comma;
  1027. X    break;
  1028. X
  1029. X    case '.':
  1030. X    unary_delim = false;
  1031. X    code = period;
  1032. X    break;
  1033. X
  1034. X    case '-':
  1035. X    case '+':            /* check for -, +, --, ++ */
  1036. X    code = (ps.last_u_d ? unary_op : binary_op);
  1037. X    unary_delim = true;
  1038. X
  1039. X    if (*buf_ptr == token[0]) {
  1040. X        /* check for doubled character */
  1041. X        *tok++ = *buf_ptr++;
  1042. X        /* buffer overflow will be checked at end of loop */
  1043. X        if (last_code == ident || last_code == rparen) {
  1044. X        code = (ps.last_u_d ? unary_op : postop);
  1045. X        /* check for following ++ or -- */
  1046. X        unary_delim = false;
  1047. X        }
  1048. X    }
  1049. X    else if (*buf_ptr == '=')
  1050. X        /* check for operator += */
  1051. X        *tok++ = *buf_ptr++;
  1052. X    else if (*buf_ptr == '>') {
  1053. X        /* check for operator -> */
  1054. X        *tok++ = *buf_ptr++;
  1055. X        if (!pointer_as_binop) {
  1056. X        unary_delim = false;
  1057. X        code = unary_op;
  1058. X        ps.want_blank = false;
  1059. X        }
  1060. X    }
  1061. X    break;            /* buffer overflow will be checked at end of
  1062. X                 * switch */
  1063. X
  1064. X    case '=':
  1065. X    if (ps.in_or_st)
  1066. X        ps.block_init = 1;
  1067. X#ifdef undef
  1068. X    if (chartype[*buf_ptr] == opchar) {    /* we have two char assignment */
  1069. X        tok[-1] = *buf_ptr++;
  1070. X        if ((tok[-1] == '<' || tok[-1] == '>') && tok[-1] == *buf_ptr)
  1071. X        *tok++ = *buf_ptr++;
  1072. X        *tok++ = '=';    /* Flip =+ to += */
  1073. X        *tok = 0;
  1074. X    }
  1075. X#else
  1076. X    if (*buf_ptr == '=') {/* == */
  1077. X        *tok++ = '=';    /* Flip =+ to += */
  1078. X        buf_ptr++;
  1079. X        *tok = 0;
  1080. X    }
  1081. X#endif
  1082. X    code = binary_op;
  1083. X    unary_delim = true;
  1084. X    break;
  1085. X    /* can drop thru!!! */
  1086. X
  1087. X    case '>':
  1088. X    case '<':
  1089. X    case '!':            /* ops like <, <<, <=, !=, etc */
  1090. X    if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
  1091. X        *tok++ = *buf_ptr;
  1092. X        if (++buf_ptr >= buf_end)
  1093. X        fill_buffer();
  1094. X    }
  1095. X    if (*buf_ptr == '=')
  1096. X        *tok++ = *buf_ptr++;
  1097. X    code = (ps.last_u_d ? unary_op : binary_op);
  1098. X    unary_delim = true;
  1099. X    break;
  1100. X
  1101. X    default:
  1102. X    if (token[0] == '/' && *buf_ptr == '*') {
  1103. X        /* it is start of comment */
  1104. X        *tok++ = '*';
  1105. X
  1106. X        if (++buf_ptr >= buf_end)
  1107. X        fill_buffer();
  1108. X
  1109. X        code = comment;
  1110. X        unary_delim = ps.last_u_d;
  1111. X        break;
  1112. X    }
  1113. X    while (*(tok - 1) == *buf_ptr || *buf_ptr == '=') {
  1114. X        /*
  1115. X         * handle ||, &&, etc, and also things as in int *****i
  1116. X         */
  1117. X        *tok++ = *buf_ptr;
  1118. X        if (++buf_ptr >= buf_end)
  1119. X        fill_buffer();
  1120. X    }
  1121. X    code = (ps.last_u_d ? unary_op : binary_op);
  1122. X    unary_delim = true;
  1123. X
  1124. X
  1125. X    }                /* end of switch */
  1126. X    if (code != newline) {
  1127. X    l_struct = false;
  1128. X    last_code = code;
  1129. X    }
  1130. X    if (buf_ptr >= buf_end)    /* check for input buffer empty */
  1131. X    fill_buffer();
  1132. X    ps.last_u_d = unary_delim;
  1133. X    *tok = '\0';        /* null terminate the token */
  1134. X    return (code);
  1135. X};
  1136. X
  1137. X/*
  1138. X * Add the given keyword to the keyword table, using val as the keyword type
  1139. X */
  1140. Xaddkey(key, val)
  1141. X    char       *key;
  1142. X{
  1143. X    register struct templ *p = specials;
  1144. X    while (p->rwd)
  1145. X    if (p->rwd[0] == key[0] && strcmp(p->rwd, key) == 0)
  1146. X        return;
  1147. X    else
  1148. X        p++;
  1149. X    if (p >= specials + sizeof specials / sizeof specials[0])
  1150. X    return;            /* For now, table overflows are silently
  1151. X                 * ignored */
  1152. X    p->rwd = key;
  1153. X    p->rwcode = val;
  1154. X    p[1].rwd = 0;
  1155. X    p[1].rwcode = 0;
  1156. X    return;
  1157. X}
  1158. END_OF_FILE
  1159. if test 13568 -ne `wc -c <'indent/lexi.c'`; then
  1160.     echo shar: \"'indent/lexi.c'\" unpacked with wrong size!
  1161. fi
  1162. # end of 'indent/lexi.c'
  1163. fi
  1164. if test -f 'indent/pr_comment.c' -a "${1}" != "-c" ; then 
  1165.   echo shar: Will not clobber existing file \"'indent/pr_comment.c'\"
  1166. else
  1167. echo shar: Extracting \"'indent/pr_comment.c'\" \(11737 characters\)
  1168. sed "s/^X//" >'indent/pr_comment.c' <<'END_OF_FILE'
  1169. X/*
  1170. X * Copyright (c) 1985 Sun Microsystems, Inc.
  1171. X * Copyright (c) 1980 The Regents of the University of California.
  1172. X * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  1173. X * All rights reserved.
  1174. X *
  1175. X * Redistribution and use in source and binary forms are permitted
  1176. X * provided that the above copyright notice and this paragraph are
  1177. X * duplicated in all such forms and that any documentation,
  1178. X * advertising materials, and other materials related to such
  1179. X * distribution and use acknowledge that the software was developed
  1180. X * by the University of California, Berkeley, the University of Illinois,
  1181. X * Urbana, and Sun Microsystems, Inc.  The name of either University
  1182. X * or Sun Microsystems may not be used to endorse or promote products
  1183. X * derived from this software without specific prior written permission.
  1184. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  1185. X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  1186. X * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  1187. X */
  1188. X
  1189. X#ifndef lint
  1190. Xstatic char sccsid[] = "@(#)pr_comment.c    5.9 (Berkeley) 9/15/88";
  1191. X#endif /* not lint */
  1192. X
  1193. X/*
  1194. X * NAME:
  1195. X *    pr_comment
  1196. X *
  1197. X * FUNCTION:
  1198. X *    This routine takes care of scanning and printing comments.
  1199. X *
  1200. X * ALGORITHM:
  1201. X *    1) Decide where the comment should be aligned, and if lines should
  1202. X *       be broken.
  1203. X *    2) If lines should not be broken and filled, just copy up to end of
  1204. X *       comment.
  1205. X *    3) If lines should be filled, then scan thru input_buffer copying
  1206. X *       characters to com_buf.  Remember where the last blank, tab, or
  1207. X *       newline was.  When line is filled, print up to last blank and
  1208. X *       continue copying.
  1209. X *
  1210. X * HISTORY:
  1211. X *    November 1976    D A Willcox of CAC    Initial coding
  1212. X *    12/6/76        D A Willcox of CAC    Modification to handle
  1213. X *                        UNIX-style comments
  1214. X *
  1215. X */
  1216. X
  1217. X/*
  1218. X * this routine processes comments.  It makes an attempt to keep comments from
  1219. X * going over the max line length.  If a line is too long, it moves everything
  1220. X * from the last blank to the next comment line.  Blanks and tabs from the
  1221. X * beginning of the input line are removed
  1222. X */
  1223. X
  1224. X
  1225. X#include "indent_globs.h"
  1226. X
  1227. X
  1228. Xpr_comment()
  1229. X{
  1230. X    int         now_col;    /* column we are in now */
  1231. X    int         adj_max_col;    /* Adjusted max_col for when we decide to
  1232. X                 * spill comments over the right margin */
  1233. X    char       *last_bl;    /* points to the last blank in the output
  1234. X                 * buffer */
  1235. X    char       *t_ptr;        /* used for moving string */
  1236. X    int         unix_comment;    /* tri-state variable used to decide if it is
  1237. X                 * a unix-style comment. 0 means only blanks
  1238. X                 * since /*, 1 means regular style comment, 2
  1239. X                 * means unix style comment */
  1240. X    int         break_delim = comment_delimiter_on_blankline;
  1241. X    int         l_just_saw_decl = ps.just_saw_decl;
  1242. X    /*
  1243. X     * int         ps.last_nl = 0;    /* true iff the last significant thing
  1244. X     * weve seen is a newline
  1245. X     */
  1246. X    int         one_liner = 1;    /* true iff this comment is a one-liner */
  1247. X    adj_max_col = max_col;
  1248. X    ps.just_saw_decl = 0;
  1249. X    last_bl = 0;        /* no blanks found so far */
  1250. X    ps.box_com = false;        /* at first, assume that we are not in
  1251. X                     * a boxed comment or some other
  1252. X                     * comment that should not be touched */
  1253. X    ++ps.out_coms;        /* keep track of number of comments */
  1254. X    unix_comment = 1;        /* set flag to let us figure out if there is a
  1255. X                 * unix-style comment ** DISABLED: use 0 to
  1256. X                 * reenable this hack! */
  1257. X
  1258. X    /* Figure where to align and how to treat the comment */
  1259. X
  1260. X    if (ps.col_1 && !format_col1_comments) {    /* if comment starts in column
  1261. X                         * 1 it should not be touched */
  1262. X    ps.box_com = true;
  1263. X    ps.com_col = 1;
  1264. X    }
  1265. X    else {
  1266. X    if (*buf_ptr == '-' || *buf_ptr == '*') {
  1267. X        ps.box_com = true;    /* a comment with a '-' or '*' immediately
  1268. X                 * after the /* is assumed to be a boxed
  1269. X                 * comment */
  1270. X        break_delim = 0;
  1271. X    }
  1272. X    if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
  1273. X        /* klg: check only if this line is blank */
  1274. X        /*
  1275. X         * If this (*and previous lines are*) blank, dont put comment way
  1276. X         * out at left
  1277. X         */
  1278. X        ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
  1279. X        adj_max_col = block_comment_max_col;
  1280. X        if (ps.com_col <= 1)
  1281. X        ps.com_col = 1 + !format_col1_comments;
  1282. X    }
  1283. X    else {
  1284. X        register    target_col;
  1285. X        break_delim = 0;
  1286. X        if (s_code != e_code)
  1287. X        target_col = count_spaces(compute_code_target(), s_code);
  1288. X        else {
  1289. X        target_col = 1;
  1290. X        if (s_lab != e_lab)
  1291. X            target_col = count_spaces(compute_label_target(), s_lab);
  1292. X        }
  1293. X        ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
  1294. X        if (ps.com_col < target_col)
  1295. X        ps.com_col = ((target_col + 7) & ~7) + 1;
  1296. X        if (ps.com_col + 24 > adj_max_col)
  1297. X        adj_max_col = ps.com_col + 24;
  1298. X    }
  1299. X    }
  1300. X    if (ps.box_com) {
  1301. X    buf_ptr[-2] = 0;
  1302. X    ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
  1303. X    buf_ptr[-2] = '/';
  1304. X    }
  1305. X    else {
  1306. X    ps.n_comment_delta = 0;
  1307. X    while (*buf_ptr == ' ' || *buf_ptr == '\t')
  1308. X        buf_ptr++;
  1309. X    }
  1310. X    ps.comment_delta = 0;
  1311. X    *e_com++ = '/';        /* put '/*' into buffer */
  1312. X    *e_com++ = '*';
  1313. X    if (*buf_ptr != ' ' && !ps.box_com)
  1314. X    *e_com++ = ' ';
  1315. X
  1316. X    *e_com = '\0';
  1317. X    if (troff) {
  1318. X    now_col = 1;
  1319. X    adj_max_col = 80;
  1320. X    }
  1321. X    else
  1322. X    now_col = count_spaces(ps.com_col, s_com);    /* figure what column we
  1323. X                             * would be in if we
  1324. X                             * printed the comment
  1325. X                             * now */
  1326. X
  1327. X    /* Start to copy the comment */
  1328. X
  1329. X    while (1) {            /* this loop will go until the comment is
  1330. X                 * copied */
  1331. X    if (*buf_ptr > 040 && *buf_ptr != '*')
  1332. X        ps.last_nl = 0;
  1333. X    check_size(com);
  1334. X    switch (*buf_ptr) {    /* this checks for various spcl cases */
  1335. X    case 014:        /* check for a form feed */
  1336. X        if (!ps.box_com) {    /* in a text comment, break the line here */
  1337. X        ps.use_ff = true;
  1338. X        /* fix so dump_line uses a form feed */
  1339. X        dump_line();
  1340. X        last_bl = 0;
  1341. X        *e_com++ = ' ';
  1342. X        *e_com++ = '*';
  1343. X        *e_com++ = ' ';
  1344. X        while (*++buf_ptr == ' ' || *buf_ptr == '\t');
  1345. X        }
  1346. X        else {
  1347. X        if (++buf_ptr >= buf_end)
  1348. X            fill_buffer();
  1349. X        *e_com++ = 014;
  1350. X        }
  1351. X        break;
  1352. X
  1353. X    case '\n':
  1354. X        if (had_eof) {    /* check for unexpected eof */
  1355. X        printf("Unterminated comment\n");
  1356. X        *e_com = '\0';
  1357. X        dump_line();
  1358. X        return;
  1359. X        }
  1360. X        one_liner = 0;
  1361. X        if (ps.box_com || ps.last_nl) {    /* if this is a boxed comment,
  1362. X                         * we dont ignore the newline */
  1363. X        if (s_com == e_com) {
  1364. X            *e_com++ = ' ';
  1365. X            *e_com++ = ' ';
  1366. X        }
  1367. X        *e_com = '\0';
  1368. X        if (!ps.box_com && e_com - s_com > 3) {
  1369. X            if (break_delim == 1 && s_com[0] == '/'
  1370. X                && s_com[1] == '*' && s_com[2] == ' ') {
  1371. X            char       *t = e_com;
  1372. X            break_delim = 2;
  1373. X            e_com = s_com + 2;
  1374. X            *e_com = 0;
  1375. X            if (blanklines_before_blockcomments)
  1376. X                prefix_blankline_requested = 1;
  1377. X            dump_line();
  1378. X            e_com = t;
  1379. X            s_com[0] = s_com[1] = s_com[2] = ' ';
  1380. X            }
  1381. X            dump_line();
  1382. X            check_size(com);
  1383. X            *e_com++ = ' ';
  1384. X            *e_com++ = ' ';
  1385. X        }
  1386. X        dump_line();
  1387. X        now_col = ps.com_col;
  1388. X        }
  1389. X        else {
  1390. X        ps.last_nl = 1;
  1391. X        if (unix_comment != 1) {    /* we not are in unix_style
  1392. X                         * comment */
  1393. X            if (unix_comment == 0 && s_code == e_code) {
  1394. X            /*
  1395. X             * if it is a UNIX-style comment, ignore the
  1396. X             * requirement that previous line be blank for
  1397. X             * unindention
  1398. X             */
  1399. X            ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
  1400. X            if (ps.com_col <= 1)
  1401. X                ps.com_col = 2;
  1402. X            }
  1403. X            unix_comment = 2;    /* permanently remember that we are in
  1404. X                     * this type of comment */
  1405. X            dump_line();
  1406. X            ++line_no;
  1407. X            now_col = ps.com_col;
  1408. X            *e_com++ = ' ';
  1409. X            /*
  1410. X             * fix so that the star at the start of the line will line
  1411. X             * up
  1412. X             */
  1413. X            do        /* flush leading white space */
  1414. X            if (++buf_ptr >= buf_end)
  1415. X                fill_buffer();
  1416. X            while (*buf_ptr == ' ' || *buf_ptr == '\t');
  1417. X            break;
  1418. X        }
  1419. X        if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
  1420. X            last_bl = e_com - 1;
  1421. X        /*
  1422. X         * if there was a space at the end of the last line, remember
  1423. X         * where it was
  1424. X         */
  1425. X        else {        /* otherwise, insert one */
  1426. X            last_bl = e_com;
  1427. X            check_size(com);
  1428. X            *e_com++ = ' ';
  1429. X            ++now_col;
  1430. X        }
  1431. X        }
  1432. X        ++line_no;        /* keep track of input line number */
  1433. X        if (!ps.box_com) {
  1434. X        int         nstar = 1;
  1435. X        do {        /* flush any blanks and/or tabs at start of
  1436. X                 * next line */
  1437. X            if (++buf_ptr >= buf_end)
  1438. X            fill_buffer();
  1439. X            if (*buf_ptr == '*' && --nstar >= 0) {
  1440. X            if (++buf_ptr >= buf_end)
  1441. X                fill_buffer();
  1442. X            if (*buf_ptr == '/')
  1443. X                goto end_of_comment;
  1444. X            }
  1445. X        } while (*buf_ptr == ' ' || *buf_ptr == '\t');
  1446. X        }
  1447. X        else if (++buf_ptr >= buf_end)
  1448. X        fill_buffer();
  1449. X        break;        /* end of case for newline */
  1450. X
  1451. X    case '*':        /* must check for possibility of being at end
  1452. X                 * of comment */
  1453. X        if (++buf_ptr >= buf_end)    /* get to next char after * */
  1454. X        fill_buffer();
  1455. X
  1456. X        if (unix_comment == 0)    /* set flag to show we are not in
  1457. X                     * unix-style comment */
  1458. X        unix_comment = 1;
  1459. X
  1460. X        if (*buf_ptr == '/') {    /* it is the end!!! */
  1461. X    end_of_comment:
  1462. X        if (++buf_ptr >= buf_end)
  1463. X            fill_buffer();
  1464. X
  1465. X        if (*(e_com - 1) != ' ' && !ps.box_com) {    /* insure blank before
  1466. X                                 * end */
  1467. X            *e_com++ = ' ';
  1468. X            ++now_col;
  1469. X        }
  1470. X        if (break_delim == 1 && !one_liner && s_com[0] == '/'
  1471. X            && s_com[1] == '*' && s_com[2] == ' ') {
  1472. X            char       *t = e_com;
  1473. X            break_delim = 2;
  1474. X            e_com = s_com + 2;
  1475. X            *e_com = 0;
  1476. X            if (blanklines_before_blockcomments)
  1477. X            prefix_blankline_requested = 1;
  1478. X            dump_line();
  1479. X            e_com = t;
  1480. X            s_com[0] = s_com[1] = s_com[2] = ' ';
  1481. X        }
  1482. X        if (break_delim == 2 && e_com > s_com + 3
  1483. X             /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
  1484. X            *e_com = '\0';
  1485. X            dump_line();
  1486. X            now_col = ps.com_col;
  1487. X        }
  1488. X        check_size(com);
  1489. X        *e_com++ = '*';
  1490. X        *e_com++ = '/';
  1491. X        *e_com = '\0';
  1492. X        ps.just_saw_decl = l_just_saw_decl;
  1493. X        return;
  1494. X        }
  1495. X        else {        /* handle isolated '*' */
  1496. X        *e_com++ = '*';
  1497. X        ++now_col;
  1498. X        }
  1499. X        break;
  1500. X    default:        /* we have a random char */
  1501. X        if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
  1502. X        unix_comment = 1;    /* we are not in unix-style comment */
  1503. X
  1504. X        *e_com = *buf_ptr++;
  1505. X        if (buf_ptr >= buf_end)
  1506. X        fill_buffer();
  1507. X
  1508. X        if (*e_com == '\t')    /* keep track of column */
  1509. X        now_col = ((now_col - 1) & tabmask) + tabsize + 1;
  1510. X        else if (*e_com == '\b')    /* this is a backspace */
  1511. X        --now_col;
  1512. X        else
  1513. X        ++now_col;
  1514. X
  1515. X        if (*e_com == ' ' || *e_com == '\t')
  1516. X        last_bl = e_com;
  1517. X        /* remember we saw a blank */
  1518. X
  1519. X        ++e_com;
  1520. X        if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') {
  1521. X        /*
  1522. X         * the comment is too long, it must be broken up
  1523. X         */
  1524. X        if (break_delim == 1 && s_com[0] == '/'
  1525. X            && s_com[1] == '*' && s_com[2] == ' ') {
  1526. X            char       *t = e_com;
  1527. X            break_delim = 2;
  1528. X            e_com = s_com + 2;
  1529. X            *e_com = 0;
  1530. X            if (blanklines_before_blockcomments)
  1531. X            prefix_blankline_requested = 1;
  1532. X            dump_line();
  1533. X            e_com = t;
  1534. X            s_com[0] = s_com[1] = s_com[2] = ' ';
  1535. X        }
  1536. X        if (last_bl == 0) {    /* we have seen no blanks */
  1537. X            last_bl = e_com;    /* fake it */
  1538. X            *e_com++ = ' ';
  1539. X        }
  1540. X        *e_com = '\0';    /* print what we have */
  1541. X        *last_bl = '\0';
  1542. X        while (last_bl > s_com && last_bl[-1] < 040)
  1543. X            *--last_bl = 0;
  1544. X        e_com = last_bl;
  1545. X        dump_line();
  1546. X
  1547. X        *e_com++ = ' ';    /* add blanks for continuation */
  1548. X        *e_com++ = ' ';
  1549. X        *e_com++ = ' ';
  1550. X
  1551. X        t_ptr = last_bl + 1;
  1552. X        last_bl = 0;
  1553. X        if (t_ptr >= e_com) {
  1554. X            while (*t_ptr == ' ' || *t_ptr == '\t')
  1555. X            t_ptr++;
  1556. X            while (*t_ptr != '\0') {    /* move unprinted part of
  1557. X                         * comment down in buffer */
  1558. X            if (*t_ptr == ' ' || *t_ptr == '\t')
  1559. X                last_bl = e_com;
  1560. X            *e_com++ = *t_ptr++;
  1561. X            }
  1562. X        }
  1563. X        *e_com = '\0';
  1564. X        now_col = count_spaces(ps.com_col, s_com);    /* recompute current
  1565. X                                 * position */
  1566. X        }
  1567. X        break;
  1568. X    }
  1569. X    }
  1570. X}
  1571. END_OF_FILE
  1572. if test 11737 -ne `wc -c <'indent/pr_comment.c'`; then
  1573.     echo shar: \"'indent/pr_comment.c'\" unpacked with wrong size!
  1574. fi
  1575. # end of 'indent/pr_comment.c'
  1576. fi
  1577. echo shar: End of archive 2 \(of 3\).
  1578. cp /dev/null ark2isdone
  1579. MISSING=""
  1580. for I in 1 2 3 ; do
  1581.     if test ! -f ark${I}isdone ; then
  1582.     MISSING="${MISSING} ${I}"
  1583.     fi
  1584. done
  1585. if test "${MISSING}" = "" ; then
  1586.     echo You have unpacked all 3 archives.
  1587.     rm -f ark[1-9]isdone
  1588. else
  1589.     echo You still need to unpack the following archives:
  1590.     echo "        " ${MISSING}
  1591. fi
  1592. ##  End of shell archive.
  1593. exit 0
  1594.  
  1595.