summaryrefslogtreecommitdiff
path: root/file.c
blob: 386149af76e0489dacbf3c7f435c9c4fa2bfb762 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * file.c - Open and read a file
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "util.h"
#include "main.h"
#include "git-file.h"
#include "file.h"


bool file_cat(const struct file *file, void *user, const char *line)
{
	printf("%s\n", line);
	return 1;
}


char *file_graft_relative(const char *base, const char *name)
{
	const char *slash;
	char *res;
	unsigned len;

	if (*name == '/')
		return NULL;

	slash = strrchr(base, '/');
	if (!slash)
		return NULL;

	len = slash + 1 - base;
	res = alloc_size(len + strlen(name) + 1);
	memcpy(res, base, len);
	strcpy(res + len, name);

	return res;
}


static bool try_related(struct file *file)
{
	char *tmp;

	if (!file->related)
		return 0;

	tmp = file_graft_relative(file->related->name, file->name);
	if (!tmp)
		return NULL;

	if (*file->name == '/')
		return 0;

	file->file = fopen(tmp, "r");
	if (!file->file) {
		free(tmp);
		return 0;
	}

	if (verbose)
		fprintf(stderr, "reading %s\n", tmp);

	free((char *) file->name);
	file->name = tmp;
	return 1;
}


/*
 * @@@ logic isn't quite complete yet. It should go something like this:
 *
 * - if there is no related item,
 *   - try file,
 *   - if there is a colon, try VCS,
 *   - give up
 * - if there is a related item,
 *   - if it is a VCS,
 *     - try the revision matching or predating (if different repo) that of the
 *       related repo,
 (     - fall through and try as if it was a file
 *   - try opening as file. If this fails,
 *     - if the name of the file to open is absolute, give up
 *     - try `dirname related`/file_ope_open
 *     - give up
 *
 * @@@ should we see if non-VCS file is in a VCS ? E.g.,
 * /home/my-libs/foo.lib 1234:/home/my-project/foo.sch
 * or maybe use : as indictor for VCS, i.e.,
 * :/home/my-libs/foo.lib ...
 *
 * @@@ explicit revision should always win over related.
 */

static void *open_vcs(struct file *file)
{
	char *colon;

	colon = strchr(file->name, ':');
	if (colon) {
		char *tmp;

		tmp = stralloc(file->name);
		tmp[colon - file->name] = 0;
		file->vcs = vcs_git_open(tmp, colon + 1,
		    file->related ? file->related->vcs : NULL);
		if (file->vcs) {
			free(tmp);
			return file->vcs;
		}
		if (verbose > 1)
			fprintf(stderr, "could not open %s:%s\n",
			    tmp, colon + 1);
		return NULL;
	} else {
		file->vcs = vcs_git_open(NULL, file->name,
		    file->related ? file->related->vcs : NULL);
		if (file->vcs)
			return file->vcs;
		if (verbose > 1)
			fprintf(stderr, "could not open %s\n", file->name);
		return 0;
	}
}


void file_open(struct file *file, const char *name, const struct file *related)
{
	file->name = stralloc(name);
	file->lineno = 0;
	file->related = related;
	file->file = NULL;
	file->vcs = NULL;

	if (related && related->vcs) {
		file->vcs = open_vcs(file);
		if (file->vcs)
			return;
	}

	file->file = fopen(name, "r");
	if (file->file) {
		if (verbose)
			fprintf(stderr, "reading %s\n", name);
		return;
	}

	if (try_related(file))
		return;

	if (verbose)
		perror(name);

	if (!strchr(name, ':')) {
		if (!verbose)
			perror(name);
		exit(1);
	}

	file->vcs = open_vcs(file);
	if (!file->vcs) {
		fprintf(stderr, "could not open %s\n", name);
		exit(1);
	}
}


void file_read(struct file *file,
    bool (*parse)(const struct file *file, void *user, const char *line),
    void *user)
{
	char buf[1000];
	char *nl;

	if (file->vcs) {
		vcs_read(file->vcs, file, parse, user);
		return;
	}
	while (fgets(buf, sizeof(buf), file->file)) {
		nl = strchr(buf, '\n');
		if (nl)
			*nl = 0;
		file->lineno++;
		if (!parse(file, user, buf))
			break;
	}
}


void file_close(struct file *file)
{
	if (file->file)
		fclose(file->file);
	if (file->vcs)
		vcs_close(file->vcs);
	free((char *) file->name);
}