Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6561c73825ceaf13bceaccb2aef94f4e0b506fac (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2000, 2010 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.make.internal.core.makefile;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;

/**
 */
public class MakefileReader extends LineNumberReader {


	public MakefileReader(Reader reader) {
		super(reader);
	}

	public MakefileReader(Reader reader, int sz) {
		super(reader, sz);
	}


	@Override
	public String readLine() throws IOException {
		boolean done = false;
		StringBuffer buffer = new StringBuffer();
		boolean escapedLine = false;
		boolean escapedCommand = false;
		while (!done) {
			String line = super.readLine();
			if (line == null) {
				return null;
			}

			if (escapedLine && line.length() > 0) {
				// Eat the spaces at the beginning.
				int i = 0;
				while (i < line.length() && (Util.isSpace(line.charAt(i)))) {
					i++ ;
				}
				line = line.substring(i);
			} else if (escapedCommand && line.length() > 0) {
				// Only eat the first tab
				if (line.charAt(0) == '\t') {
					line.substring(1);
				}
			}

			// According to POSIX rules:
			// When an escaped <newline>(one preceded by a backslash) is found
			// anywhere in the makefile except in a command line, it shall be replaced,
			// along with any leading white space on the following line, with a single <space>
			//
			// When an escaped <newline> is found in a command line in a makefile,
			// the command line shall contain the backslash, the <newline>, and  the next line,
			// except that the first character of the next line shall not be included if it is a <tab>
			if (Util.isEscapedLine(line)) {
				int index = line.lastIndexOf('\\');
				if (index > 0) {
					if (!escapedLine && Util.isCommand(line)) {
						escapedCommand = true;
						buffer.append(line);
					} else {
						escapedLine = true;
						buffer.append(line.substring(0, index));
						buffer.append(' ');
					}
				}
			} else {
				done = true;
				escapedLine = false;
				escapedCommand = false;
				buffer.append(line);
			}
		}
		return buffer.toString();
	}

}

Back to the top