Deleted Added
full compact
io.c (93440) io.c (98771)
1/*
2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980, 1993
4 * The Regents of the University of California. All rights reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 24 unchanged lines hidden (view full) ---

33 * SUCH DAMAGE.
34 */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
39#endif
40static const char rcsid[] =
1/*
2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980, 1993
4 * The Regents of the University of California. All rights reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 24 unchanged lines hidden (view full) ---

33 * SUCH DAMAGE.
34 */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
39#endif
40static const char rcsid[] =
41 "$FreeBSD: head/usr.bin/indent/io.c 93440 2002-03-30 17:10:20Z dwmalone $";
41 "$FreeBSD: head/usr.bin/indent/io.c 98771 2002-06-24 17:40:27Z jmallett $";
42#endif /* not lint */
43
44#include <ctype.h>
45#include <err.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include "indent_globs.h"

--- 5 unchanged lines hidden (view full) ---

55
56void
57dump_line(void)
58{ /* dump_line is the routine that actually
59 * effects the printing of the new source. It
60 * prints the label section, followed by the
61 * code section with the appropriate nesting
62 * level, followed by any comments */
42#endif /* not lint */
43
44#include <ctype.h>
45#include <err.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include "indent_globs.h"

--- 5 unchanged lines hidden (view full) ---

55
56void
57dump_line(void)
58{ /* dump_line is the routine that actually
59 * effects the printing of the new source. It
60 * prints the label section, followed by the
61 * code section with the appropriate nesting
62 * level, followed by any comments */
63 register int cur_col,
63 int cur_col,
64 target_col = 1;
65 static int not_first_line;
66
67 if (ps.procname[0]) {
68 if (troff) {
69 if (comment_open) {
70 comment_open = 0;
71 fprintf(output, ".*/\n");

--- 41 unchanged lines hidden (view full) ---

113 comment_open = 0;
114 fprintf(output, ".*/\n");
115 }
116 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
117 e_lab--;
118 cur_col = pad_output(1, compute_label_target());
119 if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
120 || strncmp(s_lab, "#endif", 6) == 0)) {
64 target_col = 1;
65 static int not_first_line;
66
67 if (ps.procname[0]) {
68 if (troff) {
69 if (comment_open) {
70 comment_open = 0;
71 fprintf(output, ".*/\n");

--- 41 unchanged lines hidden (view full) ---

113 comment_open = 0;
114 fprintf(output, ".*/\n");
115 }
116 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
117 e_lab--;
118 cur_col = pad_output(1, compute_label_target());
119 if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
120 || strncmp(s_lab, "#endif", 6) == 0)) {
121 register char *s = s_lab;
121 char *s = s_lab;
122 if (e_lab[-1] == '\n') e_lab--;
123 do putc(*s++, output);
124 while (s < e_lab && 'a' <= *s && *s<='z');
125 while ((*s == ' ' || *s == '\t') && s < e_lab)
126 s++;
127 if (s < e_lab)
128 fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
129 (int)(e_lab - s), s);
130 }
131 else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
132 cur_col = count_spaces(cur_col, s_lab);
133 }
134 else
135 cur_col = 1; /* there is no label section */
136
137 ps.pcase = false;
138
139 if (s_code != e_code) { /* print code section, if any */
122 if (e_lab[-1] == '\n') e_lab--;
123 do putc(*s++, output);
124 while (s < e_lab && 'a' <= *s && *s<='z');
125 while ((*s == ' ' || *s == '\t') && s < e_lab)
126 s++;
127 if (s < e_lab)
128 fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
129 (int)(e_lab - s), s);
130 }
131 else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
132 cur_col = count_spaces(cur_col, s_lab);
133 }
134 else
135 cur_col = 1; /* there is no label section */
136
137 ps.pcase = false;
138
139 if (s_code != e_code) { /* print code section, if any */
140 register char *p;
140 char *p;
141
142 if (comment_open) {
143 comment_open = 0;
144 fprintf(output, ".*/\n");
145 }
146 target_col = compute_code_target();
147 {
141
142 if (comment_open) {
143 comment_open = 0;
144 fprintf(output, ".*/\n");
145 }
146 target_col = compute_code_target();
147 {
148 register int i;
148 int i;
149
150 for (i = 0; i < ps.p_l_follow; i++)
151 if (ps.paren_indents[i] >= 0)
152 ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
153 }
154 cur_col = pad_output(cur_col, target_col);
155 for (p = s_code; p < e_code; p++)
156 if (*p == (char) 0200)
157 fprintf(output, "%d", target_col * 7);
158 else
159 putc(*p, output);
160 cur_col = count_spaces(cur_col, s_code);
161 }
162 if (s_com != e_com) {
163 if (troff) {
164 int all_here = 0;
149
150 for (i = 0; i < ps.p_l_follow; i++)
151 if (ps.paren_indents[i] >= 0)
152 ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
153 }
154 cur_col = pad_output(cur_col, target_col);
155 for (p = s_code; p < e_code; p++)
156 if (*p == (char) 0200)
157 fprintf(output, "%d", target_col * 7);
158 else
159 putc(*p, output);
160 cur_col = count_spaces(cur_col, s_code);
161 }
162 if (s_com != e_com) {
163 if (troff) {
164 int all_here = 0;
165 register char *p;
165 char *p;
166
167 if (e_com[-1] == '/' && e_com[-2] == '*')
168 e_com -= 2, all_here++;
169 while (e_com > s_com && e_com[-1] == ' ')
170 e_com--;
171 *e_com = 0;
172 p = s_com;
173 while (*p == ' ')

--- 9 unchanged lines hidden (view full) ---

183 if (comment_open < 2 && ps.box_com) {
184 comment_open = 0;
185 fprintf(output, ".*/\n");
186 }
187 if (comment_open == 0) {
188 if ('a' <= *p && *p <= 'z')
189 *p = *p + 'A' - 'a';
190 if (e_com - p < 50 && all_here == 2) {
166
167 if (e_com[-1] == '/' && e_com[-2] == '*')
168 e_com -= 2, all_here++;
169 while (e_com > s_com && e_com[-1] == ' ')
170 e_com--;
171 *e_com = 0;
172 p = s_com;
173 while (*p == ' ')

--- 9 unchanged lines hidden (view full) ---

183 if (comment_open < 2 && ps.box_com) {
184 comment_open = 0;
185 fprintf(output, ".*/\n");
186 }
187 if (comment_open == 0) {
188 if ('a' <= *p && *p <= 'z')
189 *p = *p + 'A' - 'a';
190 if (e_com - p < 50 && all_here == 2) {
191 register char *follow = p;
191 char *follow = p;
192 fprintf(output, "\n.nr C! \\w\1");
193 while (follow < e_com) {
194 switch (*follow) {
195 case '\n':
196 putc(' ', output);
197 case 1:
198 break;
199 case '\\':

--- 13 unchanged lines hidden (view full) ---

213 comment_open = 1 + ps.box_com;
214 while (*p) {
215 if (*p == BACKSLASH)
216 putc(BACKSLASH, output);
217 putc(*p++, output);
218 }
219 }
220 else { /* print comment, if any */
192 fprintf(output, "\n.nr C! \\w\1");
193 while (follow < e_com) {
194 switch (*follow) {
195 case '\n':
196 putc(' ', output);
197 case 1:
198 break;
199 case '\\':

--- 13 unchanged lines hidden (view full) ---

213 comment_open = 1 + ps.box_com;
214 while (*p) {
215 if (*p == BACKSLASH)
216 putc(BACKSLASH, output);
217 putc(*p++, output);
218 }
219 }
220 else { /* print comment, if any */
221 register int target = ps.com_col;
222 register char *com_st = s_com;
221 int target = ps.com_col;
222 char *com_st = s_com;
223
224 target += ps.comment_delta;
225 while (*com_st == '\t')
226 com_st++, target += 8; /* ? */
227 while (target <= 0)
228 if (*com_st == ' ')
229 target++, com_st++;
230 else if (*com_st == '\t')

--- 54 unchanged lines hidden (view full) ---

285 ps.paren_level = ps.p_l_follow;
286 paren_target = -ps.paren_indents[ps.paren_level - 1];
287 not_first_line = 1;
288}
289
290int
291compute_code_target(void)
292{
223
224 target += ps.comment_delta;
225 while (*com_st == '\t')
226 com_st++, target += 8; /* ? */
227 while (target <= 0)
228 if (*com_st == ' ')
229 target++, com_st++;
230 else if (*com_st == '\t')

--- 54 unchanged lines hidden (view full) ---

285 ps.paren_level = ps.p_l_follow;
286 paren_target = -ps.paren_indents[ps.paren_level - 1];
287 not_first_line = 1;
288}
289
290int
291compute_code_target(void)
292{
293 register int target_col = ps.ind_size * ps.ind_level + 1;
293 int target_col = ps.ind_size * ps.ind_level + 1;
294
295 if (ps.paren_level)
296 if (!lineup_to_parens)
297 target_col += continuation_indent * ps.paren_level;
298 else {
294
295 if (ps.paren_level)
296 if (!lineup_to_parens)
297 target_col += continuation_indent * ps.paren_level;
298 else {
299 register int w;
300 register int t = paren_target;
299 int w;
300 int t = paren_target;
301
302 if ((w = count_spaces(t, s_code) - max_col) > 0
303 && count_spaces(target_col, s_code) <= max_col) {
304 t -= w + 1;
305 if (t > target_col)
306 target_col = t;
307 }
308 else

--- 27 unchanged lines hidden (view full) ---

336 * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A
337 * Willcox of CAC Added check for switch back to partly full input
338 * buffer from temporary buffer
339 *
340 */
341void
342fill_buffer(void)
343{ /* this routine reads stuff from the input */
301
302 if ((w = count_spaces(t, s_code) - max_col) > 0
303 && count_spaces(target_col, s_code) <= max_col) {
304 t -= w + 1;
305 if (t > target_col)
306 target_col = t;
307 }
308 else

--- 27 unchanged lines hidden (view full) ---

336 * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A
337 * Willcox of CAC Added check for switch back to partly full input
338 * buffer from temporary buffer
339 *
340 */
341void
342fill_buffer(void)
343{ /* this routine reads stuff from the input */
344 register char *p;
345 register int i;
346 register FILE *f = input;
344 char *p;
345 int i;
346 FILE *f = input;
347
348 if (bp_save != 0) { /* there is a partly filled input buffer left */
349 buf_ptr = bp_save; /* dont read anything, just switch buffers */
350 buf_end = be_save;
351 bp_save = be_save = 0;
352 if (buf_ptr < buf_end)
353 return; /* only return if there is really something in
354 * this buffer */
355 }
356 for (p = in_buffer;;) {
357 if (p >= in_buffer_limit) {
347
348 if (bp_save != 0) { /* there is a partly filled input buffer left */
349 buf_ptr = bp_save; /* dont read anything, just switch buffers */
350 buf_end = be_save;
351 bp_save = be_save = 0;
352 if (buf_ptr < buf_end)
353 return; /* only return if there is really something in
354 * this buffer */
355 }
356 for (p = in_buffer;;) {
357 if (p >= in_buffer_limit) {
358 register int size = (in_buffer_limit - in_buffer) * 2 + 10;
359 register int offset = p - in_buffer;
358 int size = (in_buffer_limit - in_buffer) * 2 + 10;
359 int offset = p - in_buffer;
360 in_buffer = realloc(in_buffer, size);
361 if (in_buffer == 0)
362 err(1, "input line too long");
363 p = in_buffer + offset;
364 in_buffer_limit = in_buffer + size - 2;
365 }
366 if ((i = getc(f)) == EOF) {
367 *p++ = ' ';

--- 88 unchanged lines hidden (view full) ---

456static int
457pad_output(int current, int target)
458 /* writes tabs and blanks (if necessary) to
459 * get the current output position up to the
460 * target column */
461 /* current: the current column value */
462 /* target: position we want it at */
463{
360 in_buffer = realloc(in_buffer, size);
361 if (in_buffer == 0)
362 err(1, "input line too long");
363 p = in_buffer + offset;
364 in_buffer_limit = in_buffer + size - 2;
365 }
366 if ((i = getc(f)) == EOF) {
367 *p++ = ' ';

--- 88 unchanged lines hidden (view full) ---

456static int
457pad_output(int current, int target)
458 /* writes tabs and blanks (if necessary) to
459 * get the current output position up to the
460 * target column */
461 /* current: the current column value */
462 /* target: position we want it at */
463{
464 register int curr; /* internal column pointer */
465 register int tcur;
464 int curr; /* internal column pointer */
465 int tcur;
466
467 if (troff)
468 fprintf(output, "\\h'|%dp'", (target - 1) * 7);
469 else {
470 if (current >= target)
471 return (current); /* line is already long enough */
472 curr = current;
473 while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {

--- 28 unchanged lines hidden (view full) ---

502 */
503int
504count_spaces(int current, char *buffer)
505/*
506 * this routine figures out where the character position will be after
507 * printing the text in buffer starting at column "current"
508 */
509{
466
467 if (troff)
468 fprintf(output, "\\h'|%dp'", (target - 1) * 7);
469 else {
470 if (current >= target)
471 return (current); /* line is already long enough */
472 curr = current;
473 while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {

--- 28 unchanged lines hidden (view full) ---

502 */
503int
504count_spaces(int current, char *buffer)
505/*
506 * this routine figures out where the character position will be after
507 * printing the text in buffer starting at column "current"
508 */
509{
510 register char *buf; /* used to look thru buffer */
511 register int cur; /* current character counter */
510 char *buf; /* used to look thru buffer */
511 int cur; /* current character counter */
512
513 cur = current;
514
515 for (buf = buffer; *buf != '\0'; ++buf) {
516 switch (*buf) {
517
518 case '\n':
519 case 014: /* form feed */

--- 146 unchanged lines hidden ---
512
513 cur = current;
514
515 for (buf = buffer; *buf != '\0'; ++buf) {
516 switch (*buf) {
517
518 case '\n':
519 case 014: /* form feed */

--- 146 unchanged lines hidden ---