source: trunk/diffutils/src/side.c@ 2603

Last change on this file since 2603 was 2556, checked in by bird, 20 years ago

diffutils 2.8.1

File size: 6.8 KB
Line 
1/* sdiff-format output routines for GNU DIFF.
2
3 Copyright (C) 1991, 1992, 1993, 1998, 2001, 2002 Free Software
4 Foundation, Inc.
5
6 This file is part of GNU DIFF.
7
8 GNU DIFF is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY. No author or distributor
10 accepts responsibility to anyone for the consequences of using it
11 or for whether it serves any particular purpose or works at all,
12 unless he says so in writing. Refer to the GNU DIFF General Public
13 License for full details.
14
15 Everyone is granted permission to copy, modify and redistribute
16 GNU DIFF, but only under the conditions described in the
17 GNU DIFF General Public License. A copy of this license is
18 supposed to have been given to you along with GNU DIFF so you
19 can know your rights and responsibilities. It should be in a
20 file named COPYING. Among other things, the copyright notice
21 and this notice must be preserved on all copies. */
22
23#include "diff.h"
24
25static void print_sdiff_common_lines (lin, lin);
26static void print_sdiff_hunk (struct change *);
27
28/* Next line number to be printed in the two input files. */
29static lin next0, next1;
30
31/* Print the edit-script SCRIPT as a sdiff style output. */
32
33void
34print_sdiff_script (struct change *script)
35{
36 begin_output ();
37
38 next0 = next1 = - files[0].prefix_lines;
39 print_script (script, find_change, print_sdiff_hunk);
40
41 print_sdiff_common_lines (files[0].valid_lines, files[1].valid_lines);
42}
43
44/* Tab from column FROM to column TO, where FROM <= TO. Yield TO. */
45
46static unsigned int
47tab_from_to (unsigned int from, unsigned int to)
48{
49 FILE *out = outfile;
50 unsigned int tab;
51
52 if (!expand_tabs)
53 for (tab = from + TAB_WIDTH - from % TAB_WIDTH; tab <= to; tab += TAB_WIDTH)
54 {
55 putc ('\t', out);
56 from = tab;
57 }
58 while (from++ < to)
59 putc (' ', out);
60 return to;
61}
62
63/*
64 * Print the text for half an sdiff line. This means truncate to width
65 * observing tabs, and trim a trailing newline. Returns the last column
66 * written (not the number of chars).
67 */
68static unsigned int
69print_half_line (char const *const *line, unsigned int indent,
70 unsigned int out_bound)
71{
72 FILE *out = outfile;
73 register unsigned int in_position = 0;
74 register unsigned int out_position = 0;
75 register char const *text_pointer = line[0];
76 register char const *text_limit = line[1];
77
78 while (text_pointer < text_limit)
79 {
80 register unsigned char c = *text_pointer++;
81
82 switch (c)
83 {
84 case '\t':
85 {
86 unsigned int spaces = TAB_WIDTH - in_position % TAB_WIDTH;
87 if (in_position == out_position)
88 {
89 unsigned int tabstop = out_position + spaces;
90 if (expand_tabs)
91 {
92 if (out_bound < tabstop)
93 tabstop = out_bound;
94 for (; out_position < tabstop; out_position++)
95 putc (' ', out);
96 }
97 else
98 if (tabstop < out_bound)
99 {
100 out_position = tabstop;
101 putc (c, out);
102 }
103 }
104 in_position += spaces;
105 }
106 break;
107
108 case '\r':
109 {
110 putc (c, out);
111 tab_from_to (0, indent);
112 in_position = out_position = 0;
113 }
114 break;
115
116 case '\b':
117 if (in_position != 0 && --in_position < out_bound)
118 {
119 if (out_position <= in_position)
120 /* Add spaces to make up for suppressed tab past out_bound. */
121 for (; out_position < in_position; out_position++)
122 putc (' ', out);
123 else
124 {
125 out_position = in_position;
126 putc (c, out);
127 }
128 }
129 break;
130
131 case '\f':
132 case '\v':
133 control_char:
134 if (in_position < out_bound)
135 putc (c, out);
136 break;
137
138 default:
139 if (! ISPRINT (c))
140 goto control_char;
141 /* falls through */
142 case ' ':
143 if (in_position++ < out_bound)
144 {
145 out_position = in_position;
146 putc (c, out);
147 }
148 break;
149
150 case '\n':
151 return out_position;
152 }
153 }
154
155 return out_position;
156}
157
158/*
159 * Print side by side lines with a separator in the middle.
160 * 0 parameters are taken to indicate white space text.