-
Notifications
You must be signed in to change notification settings - Fork 21
/
info.c
176 lines (158 loc) · 4.52 KB
/
info.c
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
/* $Copyright: $
* Copyright (c) 1996 - 2022 by Steve Baker ([email protected])
* All Rights reserved
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tree.h"
/**
* TODO: Make a "filenote" command for info comments.
* maybe TODO: Support language extensions (i.e. .info.en, .info.gr, etc)
* # comments
* pattern
* pattern
* info messages
* more info
*/
extern FILE *outfile;
extern const struct linedraw *linedraw;
struct infofile *infostack = NULL;
struct comment *new_comment(struct pattern *phead, char **line, int lines)
{
struct comment *com = xmalloc(sizeof(struct comment));
com->pattern = phead;
com->desc = xmalloc(sizeof(char *) * (lines+1));
int i;
for(i=0; i < lines; i++) com->desc[i] = line[i];
com->desc[i] = NULL;
com->next = NULL;
return com;
}
struct infofile *new_infofile(char *path)
{
char buf[PATH_MAX];
struct infofile *inf;
struct comment *chead = NULL, *cend = NULL, *com;
struct pattern *phead = NULL, *pend = NULL, *p;
char *line[PATH_MAX];
FILE *fp;
int lines = 0;
if (strcmp(path,INFO_PATH) == 0) fp = fopen(path, "r");
else {
snprintf(buf, PATH_MAX, "%s/.info", path);
fp = fopen(buf, "r");
}
if (fp == NULL) return NULL;
while (fgets(buf, PATH_MAX, fp) != NULL) {
if (buf[0] == '#') continue;
gittrim(buf);
if (strlen(buf) < 1) continue;
if (buf[0] == '\t') {
line[lines++] = scopy(buf+1);
} else {
if (lines) {
// Save previous pattern/message:
if (phead) {
com = new_comment(phead, line, lines);
if (!chead) chead = cend = com;
else cend = cend->next = com;
} else {
// Accumulated info message lines w/ no associated pattern?
for(int i=0; i < lines; i++) free(line[i]);
}
// Reset for next pattern/message:
phead = pend = NULL;
lines = 0;
}
p = new_pattern(buf);
if (phead == NULL) phead = pend = p;
else pend = pend->next = p;
}
}
if (phead) {
com = new_comment(phead, line, lines);
if (!chead) chead = cend = com;
else cend = cend->next = com;
} else {
for(int i=0; i < lines; i++) free(line[i]);
}
fclose(fp);
inf = xmalloc(sizeof(struct infofile));
inf->comments = chead;
inf->path = scopy(path);
inf->next = NULL;
return inf;
}
void push_infostack(struct infofile *inf)
{
if (inf == NULL) return;
inf->next = infostack;
infostack = inf;
}
struct infofile *pop_infostack(void)
{
struct infofile *inf = infostack;
struct comment *cn, *cc;
struct pattern *p, *c;
infostack = infostack->next;
if (inf == NULL) return NULL;
for(cn = cc = inf->comments; cn != NULL; cc = cn) {
cn = cn->next;
for(p=c=cc->pattern; p != NULL; c = p) {
p=p->next;
free(c->pattern);
}
for(int i=0; cc->desc[i] != NULL; i++) free(cc->desc[i]);
free(cc->desc);
free(cc);
}
free(inf->path);
free(inf);
return NULL;
}
/**
* Returns an info pointer if a path matches a pattern.
* top == 1 if called in a directory with a .info file.
*/
struct comment *infocheck(char *path, char *name, int top, int isdir)
{
struct infofile *inf = infostack;
struct comment *com;
struct pattern *p;
if (inf == NULL) return NULL;
for(inf = infostack; inf != NULL; inf = inf->next) {
for(com = inf->comments; com != NULL; com = com->next) {
for(p = com->pattern; p != NULL; p = p->next) {
if (patmatch(path, p->pattern, isdir) == 1) return com;
if (top && patmatch(name, p->pattern, isdir) == 1) return com;
}
}
top = 0;
}
return NULL;
}
void printcomment(int line, int lines, char *s)
{
if (lines == 1) fprintf(outfile, "%s ", linedraw->csingle);
else {
if (line == 0) fprintf(outfile, "%s ", linedraw->ctop);
else if (line < 2) {
fprintf(outfile, "%s ", (lines==2)? linedraw->cbot : linedraw->cmid);
} else {
fprintf(outfile, "%s ", (line == lines-1)? linedraw->cbot : linedraw->cext);
}
}
fprintf(outfile, "%s\n", s);
}