summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/diff/ifdef.c
blob: c5dde5cc9ce345f5cbe4d2dbdea528703f20b898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* #ifdef-format output routines for GNU DIFF.
   Copyright (C) 1989, 91, 92 Free Software Foundation, Inc.

This file is part of GNU DIFF.

GNU DIFF is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.  No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.  Refer to the GNU DIFF General Public
License for full details.

Everyone is granted permission to copy, modify and redistribute
GNU DIFF, but only under the conditions described in the
GNU DIFF General Public License.   A copy of this license is
supposed to have been given to you along with GNU DIFF so you
can know your rights and responsibilities.  It should be in a
file named COPYING.  Among other things, the copyright notice
and this notice must be preserved on all copies.  */


#include "diff.h"

static void format_ifdef ();
static void print_ifdef_hunk ();
static void print_ifdef_lines ();
struct change *find_change ();

static int next_line;

/* Print the edit-script SCRIPT as a merged #ifdef file.  */

void
print_ifdef_script (script)
     struct change *script;
{
  next_line = - files[0].prefix_lines;
  print_script (script, find_change, print_ifdef_hunk);
  if (next_line < files[0].valid_lines)
    {
      begin_output ();
      format_ifdef (group_format[UNCHANGED], next_line, files[0].valid_lines,
		    0, -1);
    }
}

/* Print a hunk of an ifdef diff.
   This is a contiguous portion of a complete edit script,
   describing changes in consecutive lines.  */

static void
print_ifdef_hunk (hunk)
     struct change *hunk;
{
  int first0, last0, first1, last1, deletes, inserts;
  const char *format;

  /* Determine range of line numbers involved in each file.  */
  analyze_hunk (hunk, &first0, &last0, &first1, &last1, &deletes, &inserts);
  if (inserts)
    format = deletes ? group_format[CHANGED] : group_format[NEW];
  else if (deletes)
    format = group_format[OLD];
  else
    return;

  begin_output ();

  /* Print lines up to this change.  */
  if (next_line < first0)
    format_ifdef (group_format[UNCHANGED], next_line, first0, 0, -1);

  /* Print this change.  */
  next_line = last0 + 1;
  format_ifdef (format, first0, next_line, first1, last1 + 1);
}

/* Print a set of lines according to FORMAT.
   Lines BEG0 up to END0 are from the first file.
   If END1 is -1, then the second file's lines are identical to the first;
   otherwise, lines BEG1 up to END1 are from the second file.  */

static void
format_ifdef (format, beg0, end0, beg1, end1)
     const char *format;
     int beg0, end0, beg1, end1;
{
  register FILE *out = outfile;
  register char c;
  register const char *f = format;

  while ((c = *f++) != 0)
    {
      if (c == '%')
	switch ((c = *f++))
	  {
	  case 0:
	    return;

	  case '<':
	    /* Print lines deleted from first file.  */
	    print_ifdef_lines (line_format[OLD], &files[0], beg0, end0);
	    continue;

	  case '=':
	    /* Print common lines.  */
	    print_ifdef_lines (line_format[UNCHANGED], &files[0], beg0, end0);
	    continue;

	  case '>':
	    /* Print lines inserted from second file.  */
	    if (end1 == -1)
	      print_ifdef_lines (line_format[NEW], &files[0], beg0, end0);
	    else
	      print_ifdef_lines (line_format[NEW], &files[1], beg1, end1);
	    continue;

	  case '0':
	    c = 0;
	    break;

	  default:
	    break;
	  }
      putc (c, out);
  }
}

/* Use FORMAT to print each line of CURRENT starting with FROM
   and continuing up to UPTO.  */
static void
print_ifdef_lines (format, current, from, upto)
     const char *format;
     const struct file_data *current;
     int from, upto;
{
  const char * const *linbuf = current->linbuf;

  /* If possible, use a single fwrite; it's faster.  */
  if (!tab_expand_flag && strcmp (format, "%l\n") == 0)
    fwrite (linbuf[from], sizeof (char),
	    linbuf[upto] + (linbuf[upto][-1] != '\n') -  linbuf[from],
	    outfile);
  else if (!tab_expand_flag && strcmp (format, "%L") == 0)
    fwrite (linbuf[from], sizeof (char), linbuf[upto] -  linbuf[from], outfile);
  else
    for (;  from < upto;  from++)
      {
	register FILE *out = outfile;
	register char c;
	register const char *f = format;

	while ((c = *f++) != 0)
	  {
	    if (c == '%')
	      switch ((c = *f++))
		{
		case 0:
		  goto format_done;

		case 'l':
		  output_1_line (linbuf[from],
				 linbuf[from + 1]
				   - (linbuf[from + 1][-1] == '\n'), 0, 0);
		  continue;

		case 'L':
		  output_1_line (linbuf[from], linbuf[from + 1], 0, 0);
		  continue;

		case '0':
		  c = 0;
		  break;

		default:
		  break;
		}
	    putc (c, out);
	  }

      format_done:;
      }
}
OpenPOWER on IntegriCloud