1// Copyright 2021 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package response
16
17import (
18	"io"
19	"io/ioutil"
20	"strings"
21	"unicode"
22)
23
24const noQuote = '\x00'
25
26// ReadRspFile reads a file in Ninja's response file format and returns its contents.
27func ReadRspFile(r io.Reader) ([]string, error) {
28	var files []string
29	var file []byte
30
31	buf, err := ioutil.ReadAll(r)
32	if err != nil {
33		return nil, err
34	}
35
36	isEscaping := false
37	quotingStart := byte(noQuote)
38	for _, c := range buf {
39		switch {
40		case isEscaping:
41			if quotingStart == '"' {
42				if !(c == '"' || c == '\\') {
43					// '\"' or '\\' will be escaped under double quoting.
44					file = append(file, '\\')
45				}
46			}
47			file = append(file, c)
48			isEscaping = false
49		case c == '\\' && quotingStart != '\'':
50			isEscaping = true
51		case quotingStart == noQuote && (c == '\'' || c == '"'):
52			quotingStart = c
53		case quotingStart != noQuote && c == quotingStart:
54			quotingStart = noQuote
55		case quotingStart == noQuote && unicode.IsSpace(rune(c)):
56			// Current character is a space outside quotes
57			if len(file) != 0 {
58				files = append(files, string(file))
59			}
60			file = file[:0]
61		default:
62			file = append(file, c)
63		}
64	}
65
66	if len(file) != 0 {
67		files = append(files, string(file))
68	}
69
70	return files, nil
71}
72
73func rspUnsafeChar(r rune) bool {
74	switch {
75	case 'A' <= r && r <= 'Z',
76		'a' <= r && r <= 'z',
77		'0' <= r && r <= '9',
78		r == '_',
79		r == '+',
80		r == '-',
81		r == '.',
82		r == '/':
83		return false
84	default:
85		return true
86	}
87}
88
89var rspEscaper = strings.NewReplacer(`'`, `'\''`)
90
91// WriteRspFile writes a list of files to a file in Ninja's response file format.
92func WriteRspFile(w io.Writer, files []string) error {
93	for i, f := range files {
94		if i != 0 {
95			_, err := io.WriteString(w, " ")
96			if err != nil {
97				return err
98			}
99		}
100
101		if strings.IndexFunc(f, rspUnsafeChar) != -1 {
102			f = `'` + rspEscaper.Replace(f) + `'`
103		}
104
105		_, err := io.WriteString(w, f)
106		if err != nil {
107			return err
108		}
109	}
110
111	return nil
112}
113