-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcs-keyword.c
625 lines (542 loc) · 17.4 KB
/
rcs-keyword.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
* Copyright (c) 2017, 2019-2020 Tuxera US Inc
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Expand RCS keywords in text files.
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "interfaces.h"
/* unescape double-@@ characters to single-@ */
void
rcs_data_unescape_ats(struct rcs_line *dlines)
{
struct rcs_line *dl;
char *lp, *at;
for (dl = dlines; dl; dl = dl->next) {
/*
* If this line contains an "@@", make sure its line buffer is
* writable.
*/
if (line_findstr(dl->line, "@@"))
line_allocate(dl);
/*
* Every time we see an "@@", shift the line contents to the
* left to squash the 2nd '@'.
*/
lp = dl->line;
while ((at = line_findstr(lp, "@@"))) {
memmove(at + 1, at + 2, line_length(at + 2) + 1);
lp = at + 1;
dl->len--;
}
}
}
/* re-escape single-@ characters to double-@@ */
static void
rcs_data_reescape_ats(struct rcs_line *dlines)
{
struct rcs_line *dl;
unsigned int at_count;
const char *lp;
char *lnbuf, *pos;
for (dl = dlines; dl; dl = dl->next) {
at_count = 0;
for (lp = dl->line; lp < &dl->line[dl->len]; ++lp)
if (*lp == '@')
++at_count;
if (!at_count)
continue;
lnbuf = pos = xmalloc(dl->len + at_count + 1, __func__);
for (lp = dl->line; lp < &dl->line[dl->len]; ++lp) {
*pos++ = *lp;
if (*lp == '@')
*pos++ = '@';
}
*pos++ = '\0';
if (dl->line_allocated)
free(dl->line);
dl->line = lnbuf;
dl->len += at_count;
dl->line_allocated = true;
}
}
/* search for an RCS lock for a version of a file */
static const struct rcs_lock *
lock_find(const struct rcs_file *file, const struct rcs_version *ver)
{
const struct rcs_lock *l;
for (l = file->locks; l; l = l->next)
if (rcs_number_equal(&l->number, &ver->number))
return l;
return NULL;
}
/* mark version as having an RCS keyword that expands to a file name */
static void
name_keyword(const struct rcs_file *file, struct rcs_version *ver)
{
ver->kw_name = true;
/*
* If the canonical capitalization of the file name changes over time,
* then this file revision will need to be exported just-in-time, in the
* context of the project revision, in order for the keyword to expand
* to the canonical capitalization which is correct for that project
* revision.
*/
if (file->name_changes > 1)
ver->jit = true;
}
/* mark version as having an RCS keyword that expands to a file path */
static void
path_keyword(const struct rcs_file *file, struct rcs_version *ver)
{
ver->kw_path = true;
/* See comment in name_keyword(): the same applies for the path. */
if (file->path_changes > 1)
ver->jit = true;
}
/* signature for a keyword expander function */
typedef char *(keyword_expander_t)(const struct rcs_file *file,
struct rcs_version *ver);
/* generate an expanded $Author$ keyword string */
static char *
expanded_author_str(const struct rcs_file *file, struct rcs_version *ver)
{
return sprintf_alloc("$Author: %s $", ver->author);
}
/* generate an expanded $Date$ keyword string */
static char *
expanded_date_str(const struct rcs_file *file, struct rcs_version *ver)
{
return sprintf_alloc("$Date: %s $", ver->date.string);
}
/* generate an expanded $Header$ keyword string */
static char *
expanded_header_str(const struct rcs_file *file, struct rcs_version *ver)
{
const char *path;
path_keyword(file, ver);
/* See comment about the source dirpath in expanded_source_str(). */
if (source_dir_path)
path = source_dir_path;
else {
fprintf(stderr, "warning: $Header$ in %s rev. %s is being "
"incorrectly expanded, because --source-dir was not "
"provided\n", file->name,
rcs_number_string_sb(&ver->number));
path = mkssi_rcs_dir_path;
}
return sprintf_alloc("$Header: %s/%s %s %s %s %s $", path, file->name,
rcs_number_string_sb(&ver->number), ver->date.string,
ver->author, ver->state);
}
/* generate an expanded $Id$ keyword string */
static char *
expanded_id_str(const struct rcs_file *file, struct rcs_version *ver)
{
const struct rcs_lock *lock;
name_keyword(file, ver);
/* If the file is locked, $Id$ includes the locker username. */
lock = lock_find(file, ver);
return sprintf_alloc("$Id: %s %s %s %s %s%s%s $", path_to_name(file->name),
rcs_number_string_sb(&ver->number), ver->date.string,
ver->author, ver->state,
lock ? " " : "",
lock ? lock->locker : "");
}
/* generate an expanded $Locker$ keyword string */
static char *
expanded_locker_str(const struct rcs_file *file, struct rcs_version *ver)
{
const struct rcs_lock *lock;
lock = lock_find(file, ver);
if (!lock)
return xstrdup("$Locker: $", __func__);
return sprintf_alloc("$Locker: %s $", lock->locker);
}
/* generate an expanded $ProjectName$ keyword string */
static char *
expanded_projectname_str(const struct rcs_file *file, struct rcs_version *ver)
{
const char *path, *name;
/*
* The correct expansion of this keyword uses the project.pj name in the
* project directory. However, if there is no project directory,
* fallback on the project.pj name in the RCS directory.
*/
if (proj_projectpj_name)
name = proj_projectpj_name;
else
name = rcs_projectpj_name;
/*
* The correct expansion of this keyword will use a quasi-Windows style
* path with a drive letter (but forward slashes for the path
* separators) pointing to the project directory. This cannot be
* derived; it must be provided via the --pname-dir argument. If that
* argument wasn't provided, fallback on other directories.
*/
if (pname_dir_path)
path = pname_dir_path;
else if (mkssi_proj_dir_path)
path = mkssi_proj_dir_path;
else
path = mkssi_rcs_dir_path;
if (!proj_projectpj_name || !pname_dir_path)
fprintf(stderr, "warning: $ProjectName$ in %s rev. %s is being "
"incorrectly expanded, because --proj-dir or "
"--pname-dir was not provided\n", file->name,
rcs_number_string_sb(&ver->number));
return sprintf_alloc("$ProjectName: %s/%s $", path, name);
}
/* generate an expanded $ProjectRevision$ keyword string */
static char *
expanded_projectrevision_str(const struct rcs_file *file,
struct rcs_version *ver)
{
const struct rcs_number *revnum;
/*
* $ProjectRevision$ is an unfortunate keyword. It expands to the
* project.pj file revision which is being used to check-out the file.
* Thus, if a given file revision is referenced from multiple branches
* or checkpoints, and if that file revision has a $ProjectRevision$
* keyword, the contents of the file revision will be different for the
* various branches and checkpoints that reference it, depending on the
* respective project.pj revision. This means that we cannot export a
* single blob for the file revision and reuse it everywhere.
*/
ver->kw_projrev = true; /* Version has $ProjectRevision$ */
ver->jit = true; /* Version will need to be just-in-time exported */
/*
* pj_revnum_cur is the project.pj revision number that is currently
* being exported. It is the correct number to use for this keyword.
*
* If we aren't yet exporting project revision, pj_revnum_cur will be
* unpopulated. The revision number in that case is unimportant, since
* the blob we are exporting will never be used.
*/
if (pj_revnum_cur.c)
revnum = &pj_revnum_cur;
else
revnum = &project->head;
return sprintf_alloc("$ProjectRevision: %s $",
rcs_number_string_sb(revnum));
}
/* generate an expanded $RCSfile$ keyword string */
static char *
expanded_rcsfile_str(const struct rcs_file *file, struct rcs_version *ver)
{
name_keyword(file, ver);
return sprintf_alloc("$RCSfile: %s $", path_to_name(file->name));
}
/* generate an expanded $Log$ keyword string */
static char *
expanded_log_str(const struct rcs_file *file, struct rcs_version *ver)
{
name_keyword(file, ver);
return sprintf_alloc("$Log: %s $", path_to_name(file->name));
}
/* generate an expanded $Revision$ keyword string */
static char *
expanded_revision_str(const struct rcs_file *file, struct rcs_version *ver)
{
return sprintf_alloc("$Revision: %s $",
rcs_number_string_sb(&ver->number));
}
/* generate an expanded $Source$ keyword string */
static char *
expanded_source_str(const struct rcs_file *file, struct rcs_version *ver)
{
const char *path;
path_keyword(file, ver);
/*
* The correct expansion of the $Source$ (and $Header$) keyword will use
* a quasi-Windows style path with a drive letter (but forward slashes
* for path separators) pointing to the RCS directory. This cannot be
* derived, so it needs to be specified with --source-dir.
*/
if (source_dir_path)
path = source_dir_path;
else {
fprintf(stderr, "warning: $Source$ in %s rev. %s is being "
"incorrectly expanded, because --source-dir was not "
"provided\n", file->name,
rcs_number_string_sb(&ver->number));
path = mkssi_rcs_dir_path;
}
return sprintf_alloc("$Source: %s/%s $", path, file->name);
}
/* generate an expanded $State$ keyword string */
static char *
expanded_state_str(const struct rcs_file *file, struct rcs_version *ver)
{
return sprintf_alloc("$State: %s $", ver->state);
}
/* replace a keyword on a line with its expanded revision */
static void
expand_keyword(struct rcs_line *line, size_t kw_start, size_t kw_end,
const char *expanded_str)
{
char *lnbuf, *pos;
pos = lnbuf = xmalloc(kw_start + strlen(expanded_str) + (line->len -
kw_end) + 1, __func__);
memcpy(pos, line->line, kw_start);
pos += kw_start;
strcpy(pos, expanded_str);
pos += strlen(expanded_str);
memcpy(pos, line->line + kw_end, line->len - kw_end);
pos += line->len - kw_end;
*pos++ = '\0';
if (line->line_allocated)
free(line->line);
line->line = lnbuf;
line->len = pos - lnbuf - 1;
line->line_allocated = true;
}
/* expand a generic RCS keyword */
static void
rcs_data_expand_generic_keyword(const struct rcs_file *file,
struct rcs_version *ver, struct rcs_line *dlines,
const char *keyword, keyword_expander_t expander)
{
struct rcs_line *dl;
char *lp, *kw, *expanded;
for (dl = dlines; dl; dl = dl->next) {
/* Look for the keyword on this line */
kw = line_findstr(dl->line, keyword);
if (!kw)
continue;
/*
* Keyword must be "$Keyword$" or "$Keyword: blah $", where
* "blah" is most likely a previous expansion of the keyword.
*/
lp = kw + strlen(keyword);
if (*lp == ':')
for (++lp; *lp && *lp != '\n' && *lp != '$'; ++lp)
;
/* If no closing '$' was found, then not a keyword */
if (*lp != '$')
continue;
++lp; /* Move past the '$' */
expanded = expander(file, ver);
expand_keyword(dl, kw - dl->line, lp - dl->line, expanded);
free(expanded);
}
}
/* generate header for log text */
static struct rcs_line *
log_header(const struct rcs_version *ver, const char *template_line,
size_t prefix_len, size_t postfix_start, size_t postfix_len)
{
struct rcs_line *loghdr;
char *pos, *hdr;
/*
* Generate the first line of the log message from the metadata. An
* example of what this might look like:
*
* Revision 1.8 2012/12/11 23:45:55Z daniel.lewis
*/
loghdr = xcalloc(1, sizeof *loghdr, __func__);
hdr = sprintf_alloc("Revision %s %s %s",
rcs_number_string_sb(&ver->number),
ver->date.string, ver->author);
loghdr->len = prefix_len + strlen(hdr) + postfix_len;
loghdr->line = pos = xmalloc(loghdr->len + 1, __func__);
memcpy(pos, template_line, prefix_len);
pos += prefix_len;
strcpy(pos, hdr);
pos += strlen(hdr);
memcpy(pos, &template_line[postfix_start], postfix_len);
pos += postfix_len;
*pos++ = '\0';
free(hdr);
return loghdr;
}
/* convert log text into a sequence of lines to insert into the source file */
static struct rcs_line *
log_text_to_lines(char *log, const char *template_line, size_t prefix_len,
size_t postfix_start, size_t postfix_len)
{
struct rcs_line *loglines, *ll;
char *lnbuf, *pos;
/* Break the log (check-in comment) text into lines */
loglines = string_to_lines(log);
/*
* Duplicate an MKSSI bug: any '@' character in a revision history
* comment will show up as "@@" when the log keyword is expanded, due to
* a failure to unescape the "@@" character sequence in this context.
* (Remarkably, in other contexts, such as the member archive GUI, the
* log message is correctly displayed with just a single '@'.)
*/
rcs_data_reescape_ats(loglines);
/* Add the prefix/postfix onto each log line */
for (ll = loglines; ll; ll = ll->next) {
/* lnbuf = prefix + ll->line */
lnbuf = pos = xmalloc(prefix_len + ll->len + postfix_len + 1,
__func__);
memcpy(pos, template_line, prefix_len);
pos += prefix_len;
memcpy(pos, ll->line, ll->len);
pos += ll->len;
memcpy(pos, &template_line[postfix_start], postfix_len);
pos += postfix_len;
*pos++ = '\0';
/*
* Replace original log line with the version containing the
* prefix.
*/
if (ll->line_allocated)
free(ll->line);
ll->line = lnbuf;
ll->line_allocated = true;
ll->len = pos - lnbuf - 1;
ll->no_newline = false;
}
return loglines;
}
/* expand "$Log$" keyword and insert revision history comment after it */
static void
rcs_data_expand_log_keyword(const struct rcs_file *file,
struct rcs_version *ver, const struct rcs_patch *patch,
struct rcs_line *dlines)
{
struct rcs_line *dl, *loghdr, *loglines, *ll;
char *lp, *kw, *log, *template_line;
size_t prefix_len, postfix_len, postfix_start;
struct rcs_number num;
const struct rcs_version *pver;
const struct rcs_patch *ppatch;
for (dl = dlines; dl; dl = dl->next) {
/* Look for the $Log$ keyword on this line */
kw = line_findstr(dl->line, "$Log");
if (!kw)
continue;
/*
* Keyword must be "$Log$" or "$Log: blah $", where "blah" is
* most likely a previous expansion of the keyword.
*/
lp = kw + strlen("$Log");
if (*lp == ':')
for (++lp; *lp && *lp != '\n' && *lp != '$'; ++lp)
;
/* If no closing '$' was found, then not a log keyword */
if (*lp != '$')
continue;
++lp; /* Move past '$' */
/*
* Any characters (usually white space or comment delimiters)
* preceding and following the log keyword need to be included
* on the log lines.
*/
prefix_len = kw - dl->line;
postfix_start = lp - dl->line;
postfix_len = dl->len - postfix_start;
/*
* As mentioned just above, the original $Log$ line is the
* template for leading/trailing characters. However, dl->line
* is about to be modified to expand the $Log$ keyword, which --
* if there are trailing characters -- can alter the portions of
* the line needed for the template. Avoid this by making a
* temporary copy of the template line.
*/
template_line = xmalloc(dl->len + 1, __func__);
memcpy(template_line, dl->line, dl->len);
template_line[dl->len] = '\0';
/*
* Whatever "$Log...$" string we find needs to be replaced by
* "$Log: filename $", where filename is the basename of the
* file.
*/
log = expanded_log_str(file, ver);
expand_keyword(dl, prefix_len, postfix_start, log);
free(log);
/*
* Generate the first line of the log message from the metadata.
*/
loghdr = log_header(ver, template_line, prefix_len,
postfix_start, postfix_len);
/*
* Add lines for the check-in comment. The prefix must be added
* to each of them.
*/
if (*patch->log) {
/* Break the log (check-in comment) text into lines */
loglines = log_text_to_lines(patch->log, template_line,
prefix_len, postfix_start, postfix_len);
/* Make ll point at the last of the log lines */
for (ll = loglines; ll->next; ll = ll->next)
;
/*
* If this is a duplicate revision, the log text of the
* revision from which it was duplicated must be
* included. This is done only once, even if the
* revision is a duplicate of a duplicate.
*/
num = ver->number;
if (!strcmp(patch->log, "Duplicate revision\n")
&& num.c >= 4 && num.n[num.c - 1] == 1) {
rcs_number_decrement(&num);
pver = rcs_file_find_version(file, &num, false);
ppatch = rcs_file_find_patch(file, &num, false);
if (!pver || !ppatch)
break;
ll->next = log_header(pver, template_line,
prefix_len, postfix_start, postfix_len);
ll = ll->next;
ll->next = log_text_to_lines(ppatch->log,
template_line, prefix_len,
postfix_start, postfix_len);
for (; ll->next; ll = ll->next)
;
}
/* Link the log message into the list of lines */
ll->next = dl->next;
loghdr->next = loglines;
dl->next = loghdr;
dl = ll;
} else {
/*
* Empty check-in comment, so just link the log header
* into the list of lines.
*/
loghdr->next = dl->next;
dl->next = loghdr;
dl = loghdr;
}
free(template_line);
}
}
/* expand RCS escapes and keywords */
void
rcs_data_keyword_expansion(const struct rcs_file *file, struct rcs_version *ver,
const struct rcs_patch *patch, struct rcs_line *dlines)
{
struct {
const char *keyword;
keyword_expander_t *expandfn;
} keywords[] = {
{"$Author", expanded_author_str},
{"$Date", expanded_date_str},
{"$Header", expanded_header_str},
{"$Id", expanded_id_str},
{"$Locker", expanded_locker_str},
{"$ProjectName", expanded_projectname_str},
{"$ProjectRevision", expanded_projectrevision_str},
{"$RCSfile", expanded_rcsfile_str},
{"$Revision", expanded_revision_str},
{"$Source", expanded_source_str},
{"$State", expanded_state_str},
};
unsigned int i;
/* Replace "@@" escape sequences with "@" */
rcs_data_unescape_ats(dlines);
/* Expand RCS keywords */
for (i = 0; i < ARRAY_SIZE(keywords); ++i)
rcs_data_expand_generic_keyword(file, ver, dlines,
keywords[i].keyword, keywords[i].expandfn);
/*
* Expand the "$Log$" RCS keyword -- this is somewhat more complicated,
* so it needs its own function, rather than using the "generic" code.
*/
rcs_data_expand_log_keyword(file, ver, patch, dlines);
}