/[CvsGraph]/cvsgraph/cvsgraph.c
ViewVC logotype

Annotate of /cvsgraph/cvsgraph.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.24 - (show annotations)
Thu Jul 18 12:07:11 2002 UTC (15 years, 3 months ago) by bertho
Branch: MAIN
Changes since 1.23: +1 -1 lines
File MIME type: text/plain
Fix possible exploitable buffer overflow in string expansion
1 /*
2 * CvsGraph graphical representation generator of brances and revisions
3 * of a file in cvs/rcs.
4 *
5 * Copyright (C) 2001,2002 B. Stultiens
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
32 #include <fcntl.h>
33 #include <regex.h>
34 #include <errno.h>
35 #include <ctype.h>
36 #include <time.h>
37 #include <limits.h>
38
39 #ifdef HAVE_GETOPT_H
40 # include <getopt.h>
41 #endif
42
43 #include <gd.h>
44 #include <gdfontt.h>
45
46 #include "cvsgraph.h"
47 #include "utils.h"
48 #include "readconf.h"
49 #include "rcs.h"
50
51 #if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG) && !defined(HAVE_IMAGE_JPEG)
52 # error No image output format available. Check libgd
53 #endif
54
55
56 /*#define DEBUG 1*/
57 /*#define NOGDFILL 1*/
58
59 #define LOOPSAFEGUARD 100 /* Max itterations in possible infinite loops */
60
61 #ifndef MAX
62 # define MAX(a,b) ((a) > (b) ? (a) : (b))
63 #endif
64
65 #ifndef MIN
66 # define MIN(a,b) ((a) < (b) ? (a) : (b))
67 #endif
68
69 #define ALIGN_HL 0x00
70 #define ALIGN_HC 0x01
71 #define ALIGN_HR 0x02
72 #define ALIGN_HX 0x0f
73 #define ALIGN_VT 0x00
74 #define ALIGN_VC 0x10
75 #define ALIGN_VB 0x20
76 #define ALIGN_VX 0xf0
77
78 /*
79 **************************************************************************
80 * Globals
81 **************************************************************************
82 */
83
84 config_t conf;
85 int debuglevel;
86 color_t white_color = {255, 255, 255, 0};
87 color_t black_color = {0, 0, 0, 0};
88
89
90 /*
91 **************************************************************************
92 * Debug routines
93 **************************************************************************
94 */
95 static void dump_rev(char *p, rev_t *r)
96 {
97 printf("%s", p);
98 if(r)
99 printf("'%s', '%s', %d\n", r->rev, r->branch, r->isbranch);
100 else
101 printf("<null>\n");
102 }
103
104 static void dump_id(char *p, char *d)
105 {
106 printf("%s", p);
107 if(d)
108 printf("'%s'\n", d);
109 else
110 printf("<null>\n");
111 }
112
113 static void dump_idrev(char *p, idrev_t *t)
114 {
115 printf("%s", p);
116 if(t)
117 {
118 printf("'%s' -> ", t->id);
119 dump_rev("", t->rev);
120 }
121 else
122 printf("<null>\n");
123 }
124
125 static void dump_tag(char *p, tag_t *t)
126 {
127 printf("%s", p);
128 if(t)
129 {
130 printf("'%s' -> ", t->tag);
131 dump_rev("", t->rev);
132 }
133 else
134 printf("<null>\n");
135 }
136
137 static void dump_delta(char *p, delta_t *d)
138 {
139 int i;
140 printf("%sdelta.rev : ", p);
141 dump_rev("", d->rev);
142 printf("%sdelta.date : %s\n", p, d->date);
143 printf("%sdelta.author: %s\n", p, d->author);
144 printf("%sdelta.state : %s\n", p, d->state);
145 for(i = 0; d->branches && i < d->branches->nrevs; i++)
146 {
147 printf("%sdelta.branch: ", p);
148 dump_rev("", d->branches->revs[i]);
149 }
150 printf("%sdelta.next : ", p);
151 dump_rev("", d->next);
152 printf("\n");
153 }
154
155 static void dump_dtext(char *p, dtext_t *d)
156 {
157 printf("%sdtext.rev : ", p);
158 dump_rev("", d->rev);
159 printf("%sdtext.log : %d bytes\n", p, d->log ? strlen(d->log) : -1);
160 printf("%sdtext.text : %d bytes\n", p, d->text ? strlen(d->text) : -1);
161 printf("\n");
162 }
163
164 static void dump_rcsfile(rcsfile_t *rcs)
165 {
166 int i;
167 printf("root : '%s'\n", rcs->root);
168 printf("module : '%s'\n", rcs->module);
169 printf("file : '%s'\n", rcs->file);
170 dump_rev("head : ", rcs->head);
171 dump_rev("branch : ", rcs->branch);
172 printf("access :\n");
173 for(i = 0; rcs->access && i < rcs->access->nids; i++)
174 dump_id("\t", rcs->access->ids[i]);
175 printf("tags :\n");
176 for(i = 0; rcs->tags && i < rcs->tags->ntags; i++)
177 dump_tag("\t", rcs->tags->tags[i]);
178 printf("locks :%s\n", rcs->strict ? " (strict)" : "");
179 for(i = 0; rcs->locks && i < rcs->locks->nidrevs; i++)
180 dump_idrev("\t", rcs->locks->idrevs[i]);
181 printf("comment: '%s'\n", rcs->comment);
182 printf("expand : '%s'\n", rcs->expand ? rcs->expand : "(default -kv)");
183 printf("deltas :\n");
184 for(i = 0; rcs->deltas && i < rcs->deltas->ndeltas; i++)
185 dump_delta("\t", rcs->deltas->deltas[i]);
186 printf("desc : '%s'\n", rcs->desc);
187 printf("dtexts :\n");
188 for(i = 0; rcs->dtexts && i < rcs->dtexts->ndtexts; i++)
189 dump_dtext("\t", rcs->dtexts->dtexts[i]);
190
191 fflush(stdout);
192 }
193
194 /*
195 **************************************************************************
196 * Read the rcs file
197 **************************************************************************
198 */
199 rcsfile_t *get_rcsfile(const char *cvsroot, const char *module, const char *file)
200 {
201 char *cmd = NULL;
202 int rv;
203
204 if(file)
205 {
206 cmd = xmalloc(strlen(cvsroot) + strlen(module) + strlen(file) + 1);
207 sprintf(cmd, "%s%s%s", cvsroot, module, file);
208 if(!(rcsin = fopen(cmd, "rb")))
209 {
210 perror(cmd);
211 return NULL;
212 }
213 input_file = cmd;
214 }
215 else
216 {
217 rcsin = stdin;
218 input_file = "<stdin>";
219 }
220 line_number = 1;
221 rv = rcsparse();
222 if(file)
223 {
224 fclose(rcsin);
225 xfree(cmd);
226 }
227 if(rv)
228 return NULL;
229 input_file = NULL;
230 if(file)
231 {
232 rcsfile->root = xstrdup(cvsroot);
233 rcsfile->module = xstrdup(module);
234 rcsfile->file = xstrdup(file);
235 }
236 else
237 {
238 rcsfile->root = xstrdup("");
239 rcsfile->module = xstrdup("");
240 rcsfile->file = xstrdup("<stdin>");
241 }
242 return rcsfile;
243 }
244
245 /*
246 **************************************************************************
247 * Sort and find helpers
248 **************************************************************************
249 */
250 int count_dots(const char *s)
251 {
252 int i;
253 for(i = 0; *s; s++)
254 {
255 if(*s == '.')
256 i++;
257 }
258 return i;
259 }
260
261 int compare_rev(int bcmp, const rev_t *r1, const rev_t *r2)
262 {
263 int d1, d2;
264 char *c1, *c2;
265 char *v1, *v2;
266 char *s1, *s2;
267 int retval = 0;
268 assert(r1 != NULL);
269 assert(r2 != NULL);
270 if(bcmp)
271 {
272 assert(r1->branch != NULL);
273 assert(r2->branch != NULL);
274 c1 = r1->branch;
275 c2 = r2->branch;
276 }
277 else
278 {
279 assert(r1->rev != NULL);
280 assert(r2->rev != NULL);
281 c1 = r1->rev;
282 c2 = r2->rev;
283 }
284
285 d1 = count_dots(c1);
286 d2 = count_dots(c2);
287 if(d1 != d2)
288 {
289 return d1 - d2;
290 }
291
292 s1 = v1 = xstrdup(c1);
293 s2 = v2 = xstrdup(c2);
294 while(1)
295 {
296 char *vc1 = strchr(s1, '.');
297 char *vc2 = strchr(s2, '.');
298 if(vc1 && vc2)
299 *vc1 = *vc2 = '\0';
300 if(*s1 && *s2)
301 {
302 d1 = atoi(s1);
303 d2 = atoi(s2);
304 if(d1 != d2)
305 {
306 retval = d1 - d2;
307 break;
308 }
309 }
310 if(!vc1 || !vc2)
311 break;
312 s1 = vc1 + 1;
313 s2 = vc2 + 1;
314 }
315 xfree(v1);
316 xfree(v2);
317 return retval;
318 }
319
320 /*
321 **************************************************************************
322 * Reorganise the rcsfile for the branches
323 *
324 * Basically, we have a list of deltas (i.e. administrative info on
325 * revisions) and a list of delta text (the actual logs and diffs).
326 * The deltas are linked through the 'next' and the 'branches' fields
327 * which describe the tree-structure of revisions.
328 * The reorganisation means that we put each delta and corresponding
329 * delta text in a revision structure and assign it to a specific
330 * branch. This is required because we want to be able to calculate
331 * the bounding boxes of each branch. The revisions expand vertically
332 * and the branches expand horizontally.
333 * The reorganisation is performed in these steps:
334 * 1 - sort deltas and delta text on revision number for quick lookup
335 * 2 - start at the denoted head revision:
336 * * create a branch structure and add this revision
337 * * for each 'branches' in the delta do:
338 * - walk all 'branches' of the delta and recursively goto 2
339 * with the denoted branch delta as new head
340 * - backlink the newly create sub-branch to the head revision
341 * so that we can draw them recursively
342 * * set head to the 'next' field and goto 2 until no next is
343 * available
344 * 3 - update the administration
345 **************************************************************************
346 */
347 static int sort_delta(const void *d1, const void *d2)
348 {
349 return compare_rev(0, (*(delta_t **)d1)->rev, (*(delta_t **)d2)->rev);
350 }
351
352 static int search_delta(const void *r, const void *d)
353 {
354 return compare_rev(0, (rev_t *)r, (*(delta_t **)d)->rev);
355 }
356
357 static delta_t *find_delta(delta_t **dl, int n, rev_t *r)
358 {
359 delta_t **d;
360 if(!n)
361 return NULL;
362 d = bsearch(r, dl, n, sizeof(*dl), search_delta);
363 if(!d)
364 return NULL;
365 return *d;
366 }
367
368 static int sort_dtext(const void *d1, const void *d2)
369 {
370 return compare_rev(0, (*(dtext_t **)d1)->rev, (*(dtext_t **)d2)->rev);
371 }
372
373 static int search_dtext(const void *r, const void *d)
374 {
375 return compare_rev(0, (rev_t *)r, (*(dtext_t **)d)->rev);
376 }
377
378 static dtext_t *find_dtext(dtext_t **dl, int n, rev_t *r)
379 {
380 dtext_t **d;
381 if(!n)
382 return NULL;
383 d = bsearch(r, dl, n, sizeof(*dl), search_dtext);
384 if(!d)
385 return NULL;
386 return *d;
387 }
388
389 static rev_t *dup_rev(const rev_t *r)
390 {
391 rev_t *t = xmalloc(sizeof(*t));
392 t->rev = xstrdup(r->rev);
393 t->branch = xstrdup(r->branch);
394 t->isbranch = r->isbranch;
395 return t;
396 }
397
398 static branch_t *new_branch(delta_t *d, dtext_t *t)
399 {
400 branch_t *b = xmalloc(sizeof(*b));
401 revision_t *r = xmalloc(sizeof(*r));
402 r->delta = d;
403 r->dtext = t;
404 r->rev = d->rev;
405 r->branch = b;
406 b->branch = dup_rev(d->rev);
407 b->branch->isbranch = 1;
408 b->nrevs = 1;
409 b->revs = xmalloc(sizeof(b->revs[0]));
410 b->revs[0] = r;
411 return b;
412 }
413
414 static revision_t *add_to_branch(branch_t *b, delta_t *d, dtext_t *t)
415 {
416 revision_t *r = xmalloc(sizeof(*r));
417 r->delta = d;
418 r->dtext = t;
419 r->rev = d->rev;
420 r->branch = b;
421 b->revs = xrealloc(b->revs, (b->nrevs+1) * sizeof(b->revs[0]));
422 b->revs[b->nrevs] = r;
423 b->nrevs++;
424 return r;
425 }
426
427 void build_branch(branch_t ***bl, int *nbl, delta_t **sdl, int nsdl, dtext_t **sdt, int nsdt, delta_t *head)
428 {
429 branch_t *b;
430 dtext_t *text;
431 revision_t *currev;
432
433 assert(head != NULL);
434
435 if(head->flag)
436 {
437 fprintf(stderr, "Circular reference on '%s' in branchpoint\n", head->rev->rev);
438 return;
439 }
440 head->flag++;
441 text = find_dtext(sdt, nsdt, head->rev);
442
443 /* Create a new branch for this head */
444 b = new_branch(head, text);
445 *bl = xrealloc(*bl, (*nbl+1)*sizeof((*bl)[0]));
446 (*bl)[*nbl] = b;
447 (*nbl)++;
448 currev = b->revs[0];
449 while(1)
450 {
451 /* Process all sub-branches */
452 if(head->branches)
453 {
454 int i;
455 for(i = 0; i < head->branches->nrevs; i++)
456 {
457 delta_t *d = find_delta(sdl, nsdl, head->branches->revs[i]);
458 int btag = *nbl;
459 if(!d)
460 continue;
461 build_branch(bl, nbl, sdl, nsdl, sdt, nsdt, d);
462
463 /* Set the new branch's origin */
464 (*bl)[btag]->branchpoint = currev;
465
466 /* Add branch to this revision */
467 currev->branches = xrealloc(currev->branches, (currev->nbranches+1)*sizeof(currev->branches[0]));
468 currev->branches[currev->nbranches] = (*bl)[btag];
469 currev->nbranches++;
470 }
471 }
472
473 /* Walk through the next list */
474 if(!head->next)
475 return;
476
477 head = find_delta(sdl, nsdl, head->next);
478 if(!head)
479 {
480 fprintf(stderr, "Next revision (%s) not found in deltalist\n", head->next->rev);
481 return;
482 }
483 if(head->flag)
484 {
485 fprintf(stderr, "Circular reference on '%s'\n", head->rev->rev);
486 return;
487 }
488 head->flag++;
489 text = find_dtext(sdt, nsdt, head->rev);
490 currev = add_to_branch(b, head, text);
491 }
492 }
493
494 int reorganise_branches(rcsfile_t *rcs)
495 {
496 delta_t **sdelta;
497 int nsdelta;
498 dtext_t **sdtext;
499 int nsdtext;
500 delta_t *head;
501 branch_t **bl;
502 int nbl;
503 int i;
504
505 assert(rcs->deltas != NULL);
506 assert(rcs->head != NULL);
507
508 /* Make a new list for quick lookup */
509 nsdelta = rcs->deltas->ndeltas;
510 sdelta = xmalloc(nsdelta * sizeof(sdelta[0]));
511 memcpy(sdelta, rcs->deltas->deltas, nsdelta * sizeof(sdelta[0]));
512 qsort(sdelta, nsdelta, sizeof(sdelta[0]), sort_delta);
513
514 /* Do the same for the delta text */
515 if(rcs->dtexts)
516 {
517 nsdtext = rcs->dtexts->ndtexts;
518 sdtext = xmalloc(nsdtext * sizeof(sdtext[0]));
519 memcpy(sdtext, rcs->dtexts->dtexts, nsdtext * sizeof(sdtext[0]));
520 qsort(sdtext, nsdtext, sizeof(sdtext[0]), sort_dtext);
521 }
522 else
523 {
524 nsdtext = 0;
525 sdtext = NULL;
526 }
527
528 /* Start from the head of the trunk */
529 head = find_delta(sdelta, nsdelta, rcs->head);
530 if(!head)
531 {
532 fprintf(stderr, "Head revision (%s) not found in deltalist\n", rcs->head->rev);
533 return 0;
534 }
535 bl = NULL;
536 nbl = 0;
537 build_branch(&bl, &nbl, sdelta, nsdelta, sdtext, nsdtext, head);
538
539 /* Reverse the head branch */
540 for(i = 0; i < bl[0]->nrevs/2; i++)
541 {
542 revision_t *r;
543 r = bl[0]->revs[i];
544 bl[0]->revs[i] = bl[0]->revs[bl[0]->nrevs-i-1];
545 bl[0]->revs[bl[0]->nrevs-i-1] = r;
546 }
547
548 /* Update the branch-number of the head because it was reversed */
549 xfree(bl[0]->branch->branch);
550 bl[0]->branch->branch = xstrdup(bl[0]->revs[0]->rev->branch);
551
552 /* Keep the admin */
553 rcs->branches = bl;
554 rcs->nbranches = nbl;
555 rcs->sdelta = sdelta;
556 rcs->nsdelta = nsdelta;
557 rcs->sdtext = sdtext;
558 rcs->nsdtext = nsdtext;
559 rcs->active = bl[0];
560 return 1;
561 }
562
563 /*
564 **************************************************************************
565 * Assign the symbolic tags to the revisions and branches
566 *
567 * The tags point to revision numbers somewhere in the tree structure
568 * of branches and revisions. First we make a sorted list of all
569 * revisions and then we assign each tag to the proper revision.
570 **************************************************************************
571 */
572 static int sort_revision(const void *r1, const void *r2)
573 {
574 return compare_rev(0, (*(revision_t **)r1)->delta->rev, (*(revision_t **)r2)->delta->rev);
575 }
576
577 static int search_revision(const void *t, const void *r)
578 {
579 return compare_rev(0, (rev_t *)t, (*(revision_t **)r)->delta->rev);
580 }
581
582 static int sort_branch(const void *b1, const void *b2)
583 {
584 return compare_rev(1, (*(branch_t **)b1)->branch, (*(branch_t **)b2)->branch);
585 }
586
587 static int search_branch(const void *t, const void *b)
588 {
589 return compare_rev(1, (rev_t *)t, (*(branch_t **)b)->branch);
590 }
591
592 static char *previous_rev(const char *c)
593 {
594 int dots = count_dots(c);
595 char *cptr;
596 char *r;
597 if(!dots)
598 {
599 fprintf(stderr, "FIXME: previous_rev(\"%s\"): Cannot determine parent branch revision\n", c);
600 return xstrdup("1.0"); /* FIXME: don't know what the parent is */
601 }
602 if(dots & 1)
603 {
604 /* Is is a revision we want the parent of */
605 r = xstrdup(c);
606 cptr = strrchr(r, '.');
607 assert(cptr != NULL);
608 if(dots == 1)
609 {
610 fprintf(stderr, "FIXME: previous_rev(\"%s\"): Going beyond top-level?\n", c);
611 /* FIXME: What is the parent of 1.1? */
612 cptr[1] = '\0';
613 strcat(r, "0");
614 return r;
615 }
616 /* Here we have a "x.x[.x.x]+" case */
617 *cptr = '\0';
618 cptr = strrchr(r, '.');
619 assert(cptr != NULL);
620 *cptr = '\0';
621 return r;
622 }
623 /* It is a branch we want the parent of */
624 r = xstrdup(c);
625 cptr = strrchr(r, '.');
626 assert(cptr != NULL);
627 *cptr = '\0';
628 return r;
629 }
630
631 int assign_tags(rcsfile_t *rcs)
632 {
633 int i;
634 int nr;
635
636 for(i = nr = 0; i < rcs->nbranches; i++)
637 nr += rcs->branches[i]->nrevs;
638
639 rcs->srev = xmalloc(nr * sizeof(rcs->srev[0]));
640 rcs->nsrev = nr;
641 for(i = nr = 0; i < rcs->nbranches; i++)
642 {
643 memcpy(&rcs->srev[nr], rcs->branches[i]->revs, rcs->branches[i]->nrevs * sizeof(rcs->branches[i]->revs[0]));
644 nr += rcs->branches[i]->nrevs;
645 }
646
647 qsort(rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), sort_revision);
648 qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
649
650 if(!rcs->branch)
651 {
652 /* The main trunk is the active trunk */
653 rcs->tags->tags = xrealloc(rcs->tags->tags, (rcs->tags->ntags+1)*sizeof(rcs->tags->tags[0]));
654 rcs->tags->tags[rcs->tags->ntags] = xmalloc(sizeof(tag_t));
655 rcs->tags->tags[rcs->tags->ntags]->tag = xstrdup("MAIN");
656 rcs->tags->tags[rcs->tags->ntags]->rev = xmalloc(sizeof(rev_t));
657 rcs->tags->tags[rcs->tags->ntags]->rev->rev = xstrdup(rcs->active->branch->rev);
658 rcs->tags->tags[rcs->tags->ntags]->rev->branch = xstrdup(rcs->active->branch->branch);
659 rcs->tags->tags[rcs->tags->ntags]->rev->isbranch = 1;
660 rcs->tags->ntags++;
661 }
662
663 /* We should have at least two tags (HEAD and MAIN) */
664 assert(rcs->tags != 0);
665
666 for(i = 0; i < rcs->tags->ntags; i++)
667 {
668 tag_t *t = rcs->tags->tags[i];
669 if(t->rev->isbranch)
670 {
671 branch_t **b;
672 add_btag:
673 b = bsearch(t->rev, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
674 if(!b)
675 {
676 rev_t rev;
677 revision_t **r;
678 /* This happens for the magic branch numbers if there are
679 * no commits within the new branch yet. So, we add the
680 * branch and try to continue.
681 */
682 rev.rev = previous_rev(t->rev->branch);
683 rev.branch = NULL;
684 rev.isbranch = 0;
685 r = bsearch(&rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
686 xfree(rev.rev);
687 if(!r)
688 {
689 if(!quiet)
690 fprintf(stderr, "No branch found for tag '%s:%s'\n", t->tag, t->rev->branch);
691 }
692 else
693 {
694 rcs->branches = xrealloc(rcs->branches, (rcs->nbranches+1)*sizeof(rcs->branches[0]));
695 rcs->branches[rcs->nbranches] = xmalloc(sizeof(branch_t));
696 rcs->branches[rcs->nbranches]->branch = dup_rev(t->rev);
697 rcs->branches[rcs->nbranches]->branchpoint = *r;
698 (*r)->branches = xrealloc((*r)->branches, ((*r)->nbranches+1)*sizeof((*r)->branches[0]));
699 (*r)->branches[(*r)->nbranches] = rcs->branches[rcs->nbranches];
700 (*r)->nbranches++;
701 rcs->nbranches++;
702 /* Resort the branches */
703 qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
704 goto add_btag;
705 }
706 }
707 else
708 {
709 branch_t *bb = *b;
710 bb->tags = xrealloc(bb->tags, (bb->ntags+1)*sizeof(bb->tags[0]));
711 bb->tags[bb->ntags] = t;
712 bb->ntags++;
713 }
714 }
715 else
716 {
717 revision_t **r = bsearch(t->rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
718 if(!r)
719 {
720 if(!quiet)
721 fprintf(stderr, "No revision found for tag '%s:%s'\n", t->tag, t->rev->rev);
722 }
723 else
724 {
725 revision_t *rr = *r;
726 rr->tags = xrealloc(rr->tags, (rr->ntags+1)*sizeof(rr->tags[0]));
727 rr->tags[rr->ntags] = t;
728 rr->ntags++;
729 }
730 }
731 }
732
733 /* We need to reset the first in the list of branches to the
734 * active branch to ensure the drawing of all
735 */
736 if(rcs->active != rcs->branches[0])
737 {
738 branch_t **b = bsearch(rcs->active->branch, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
739 branch_t *t;
740 assert(b != NULL);
741 t = *b;
742 *b = rcs->branches[0];
743 rcs->branches[0] = t;
744 }
745 return 1;
746 }
747
748 /*
749 **************************************************************************
750 * String expansion
751 **************************************************************************
752 */
753 static char *_string;
754 static int _nstring;
755 static int _nastring;
756
757 static void add_string_str(const char *s)
758 {
759 int l = strlen(s) + 1;
760 if(_nstring + l > _nastring)
761 {
762 _nastring += MAX(128, l);
763 _string = xrealloc(_string, _nastring * sizeof(_string[0]));
764 }
765 memcpy(_string+_nstring, s, l);
766 _nstring += l-1;
767 }
768
769 static void add_string_ch(int ch)
770 {
771 char buf[2];
772 buf[0] = ch;
773 buf[1] = '\0';
774 add_string_str(buf);
775 }
776
777 static void add_string_date(const char *d)
778 {
779 struct tm tm, *tmp;
780 int n;
781 time_t t;
782 char *buf;
783 int nbuf;
784
785 memset(&tm, 0, sizeof(tm));
786 n = sscanf(d, "%d.%d.%d.%d.%d.%d",
787 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
788 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
789 tm.tm_mon--;
790 if(tm.tm_year > 1900)
791 tm.tm_year -= 1900;
792 t = mktime(&tm);
793 if(n != 6 || t == (time_t)(-1))
794 {
795 add_string_str("<invalid date>");
796 return;
797 }
798
799 tmp = localtime(&t);
800 nbuf = strlen(conf.date_format) * 16; /* Should be enough to hold all types of expansions */
801 buf = xmalloc(nbuf);
802 strftime(buf, nbuf, conf.date_format, tmp);
803 add_string_str(buf);
804 xfree(buf);
805 }
806
807 static void add_string_str_html(const char *s, int maxlen)
808 {
809 int l = 0;
810 char *str = xmalloc(6 * strlen(s) + 1); /* Should hold all char entity-expand */
811 char *cptr = str;
812 for(; *s; s++)
813 {
814 if(maxlen && l > abs(maxlen))
815 {
816 cptr += sprintf(cptr, "...");
817 break;
818 }
819 if(*s < 0x20)
820 {
821 if(*s == '\n')
822 {
823 if(maxlen < 0)
824 *cptr++ = ' ';
825 else
826 cptr += sprintf(cptr, "<br>");
827 }
828 }
829 else if(*s >= 0x7f)
830 cptr += sprintf(cptr, "&#%d;", (int)(unsigned char)*s);
831 else if(*s == '<')
832 cptr += sprintf(cptr, "&lt;");
833 else if(*s == '>')
834 cptr += sprintf(cptr, "&gt;");
835 else
836 *cptr++ = *s;
837 l++;
838 }
839 *cptr = '\0';
840 add_string_str(str);
841 xfree(str);
842 }
843
844 static void add_string_str_len(const char *s, int maxlen)
845 {
846 int l = strlen(s);
847 char *str = xmalloc(l + 1 + 3);
848 strcpy(str, s);
849 if(maxlen < l)
850 sprintf(&str[maxlen], "...");
851 add_string_str(str);
852 xfree(str);
853 }
854
855 char *expand_string(const char *s, rcsfile_t *rcs, revision_t *r, rev_t *rev, rev_t *prev, tag_t *tag)
856 {
857 char nb[32];
858 char nr[32];
859 char *base;
860 int l;
861 char ch;
862
863 if(!s)
864 return xstrdup("");
865
866 _nstring = 0;
867 if(_string)
868 _string[0] = '\0';
869
870 sprintf(nb, "%d", rcs->nbranches);
871 sprintf(nr, "%d", rcs->nsrev);
872 for(; *s; s++)
873 {
874 if(*s == '%')
875 {
876 switch(*++s)
877 {
878 case 'c':
879 case 'C':
880 add_string_str(conf.cvsroot);
881 if(*s == 'C' && conf.cvsroot[0] && conf.cvsroot[strlen(conf.cvsroot)-1] == '/')
882 {
883 /* Strip the trailing '/' */
884 _nstring--;
885 _string[_nstring] = '\0';
886 }
887 break;
888 case 'f':
889 case 'F':
890 base = strrchr(rcs->file, '/');
891 if(!base)
892 add_string_str(rcs->file);
893 else
894 add_string_str(base+1);
895 if(*s == 'F' && _string[_nstring-1] == 'v' && _string[_nstring-2] == ',')
896 {
897 _nstring -= 2;
898 _string[_nstring] = '\0';
899 }
900 break;
901 case 'p':
902 base = strrchr(rcs->file, '/');
903 if(base)
904 {
905 char *c = xstrdup(rcs->file);
906 base = strrchr(c, '/');
907 assert(base != NULL);
908 base[1] = '\0';
909 add_string_str(c);
910 xfree(c);
911 }
912 /*
913 * We should not add anything here because we can encounter
914 * a completely empty path, in which case we do not want
915 * to add any slash. This prevents an inadvertent root redirect.
916 *
917 * else
918 * add_string_str("/");
919 */
920 break;
921 case 'm':
922 case 'M':
923 add_string_str(conf.cvsmodule);
924 if(*s == 'M' && conf.cvsmodule[0] && conf.cvsmodule[strlen(conf.cvsmodule)-1] == '/')
925 {
926 /* Strip the trailing '/' */
927 _nstring--;
928 _string[_nstring] = '\0';
929 }
930 break;
931 case 'r': add_string_str(nr); break;
932 case 'b': add_string_str(nb); break;
933 case '%': add_string_ch('%'); break;
934 case '0': if(conf.expand[0]) add_string_str(conf.expand[0]); break;
935 case '1': if(conf.expand[1]) add_string_str(conf.expand[1]); break;
936 case '2': if(conf.expand[2]) add_string_str(conf.expand[2]); break;
937 case '3': if(conf.expand[3]) add_string_str(conf.expand[3]); break;
938 case '4': if(conf.expand[4]) add_string_str(conf.expand[4]); break;
939 case '5': if(conf.expand[5]) add_string_str(conf.expand[5]); break;
940 case '6': if(conf.expand[6]) add_string_str(conf.expand[6]); break;
941 case '7': if(conf.expand[7]) add_string_str(conf.expand[7]); break;
942 case '8': if(conf.expand[8]) add_string_str(conf.expand[8]); break;
943 case '9': if(conf.expand[9]) add_string_str(conf.expand[9]); break;
944 case 'R': if(rev && rev->rev) add_string_str(rev->rev); break;
945 case 'P': if(prev && prev->rev) add_string_str(prev->rev); break;
946 case 'B': if(rev && rev->branch) add_string_str(rev->branch); break;
947 case 't': if(tag && tag->tag) add_string_str(tag->tag); break;
948 case 'd': if(r && r->delta && r->delta->date) add_string_date(r->delta->date); break;
949 case 's': if(r && r->delta && r->delta->state) add_string_str(r->delta->state); break;
950 case 'a': if(r && r->delta && r->delta->author) add_string_str(r->delta->author); break;
951 case 'L':
952 case 'l':
953 ch = *s;
954 l = 0;
955 if(s[1] == '[')
956 {
957 char *cptr = strchr(s, ']');
958 char *eptr;
959 if(cptr)
960 {
961 l = strtol(&s[2], &eptr, 10);
962 if(eptr != cptr)
963 l = 0;
964 else
965 s = cptr;
966 }
967 }
968 if(!conf.parse_logs)
969 add_string_str("N/A");
970 else if(r && r->dtext && r->dtext->log)
971 {
972 if(ch == 'l')
973 add_string_str_html(r->dtext->log, l);
974 else
975 add_string_str_len(r->dtext->log, abs(l));
976 }
977 break;
978 default:
979 add_string_ch('%');
980 add_string_ch(*s);
981 break;
982 }
983 }
984 else
985 add_string_ch(*s);
986 }
987 return xstrdup(_string);
988 }
989
990 /*
991 **************************************************************************
992 * Drawing routines
993 **************************************************************************
994 */
995 static int get_swidth(const char *s, font_t *f)
996 {
997 int n;
998 int m;
999 if(!s || !*s)
1000 return 0;
1001
1002 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1003 if(conf.use_ttf && f->ttfont)
1004 {
1005 int bb[8];
1006 char *e;
1007 #ifdef HAVE_GDIMAGESTRINGFT
1008 e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1009 #else
1010 e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1011 #endif
1012 if(!e)
1013 return bb[2] - bb[6];
1014 }
1015 #endif
1016 for(n = m = 0; *s; n++, s++)
1017 {
1018 if(*s == '\n')
1019 {
1020 if(n > m)
1021 m = n;
1022 n = 0;
1023 }
1024 }
1025 if(n > m)
1026 m = n;
1027 return f->gdfont ? m * f->gdfont->w : m;
1028 }
1029
1030 static int get_sheight(const char *s, font_t *f)
1031 {
1032 int nl;
1033 if(!s || !*s)
1034 return 0;
1035
1036 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1037 if(conf.use_ttf && f->ttfont)
1038 {
1039 int bb[8];
1040 char *e;
1041 #ifdef HAVE_GDIMAGESTRINGFT
1042 e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1043 #else
1044 e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1045 #endif
1046 if(!e)
1047 return bb[3] - bb[7] + 4;
1048 }
1049 #endif
1050 for(nl = 1; *s; s++)
1051 {
1052 if(*s == '\n' && s[1])
1053 nl++;
1054 }
1055 return nl * f->gdfont->h;
1056 }
1057
1058 static void draw_rbox(gdImagePtr im, int x1, int y1, int x2, int y2, int r, color_t *color, color_t *bgcolor)
1059 {
1060 int r2 = 2*r;
1061 gdImageLine(im, x1+r, y1, x2-r, y1, color->id);
1062 gdImageLine(im, x1+r, y2, x2-r, y2, color->id);
1063 gdImageLine(im, x1, y1+r, x1, y2-r, color->id);
1064 gdImageLine(im, x2, y1+r, x2, y2-r, color->id);
1065 if(conf.box_shadow)
1066 {
1067 gdImageLine(im, x1+r+1, y2+1, x2-r, y2+1, black_color.id);
1068 gdImageLine(im, x2+1, y1+r+1, x2+1, y2-r, black_color.id);
1069 }
1070 if(r)
1071 {
1072 /* FIXME: Pixelization is not perfect */
1073 gdImageArc(im, x1+r, y1+r, r2, r2, 180, 270, color->id);
1074 gdImageArc(im, x2-r, y1+r, r2, r2, 270, 360, color->id);
1075 gdImageArc(im, x1+r, y2-r, r2, r2, 90, 180, color->id);
1076 if(conf.box_shadow)
1077 {
1078 gdImageArc(im, x2-r+1, y2-r+1, r2, r2, 0, 90, black_color.id);
1079 gdImageArc(im, x2-r+1, y2-r, r2, r2, 0, 90, black_color.id);
1080 gdImageArc(im, x2-r, y2-r+1, r2, r2, 0, 90, black_color.id);
1081 }
1082 gdImageArc(im, x2-r, y2-r, r2, r2, 0, 90, color->id);
1083 }
1084 #ifndef NOGDFILL
1085 gdImageFillToBorder(im, (x1+x2)/2, (y1+y2)/2, color->id, bgcolor->id);
1086 #endif
1087 }
1088
1089 static void draw_string(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1090 {
1091 int h = get_sheight(s, f);
1092 int xx, yy;
1093 switch(align & ALIGN_HX)
1094 {
1095 default:
1096 case ALIGN_HL: xx = 0; break;
1097 case ALIGN_HC: xx = -get_swidth(s, f)/2; break;
1098 case ALIGN_HR: xx = -get_swidth(s, f); break;
1099 }
1100 switch(align & ALIGN_VX)
1101 {
1102 default:
1103 case ALIGN_VT: yy = 0; break;
1104 case ALIGN_VC: yy = h/2; break;
1105 case ALIGN_VB: yy = h; break;
1106 }
1107 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1108 if(conf.use_ttf && f->ttfont)
1109 {
1110 int bb[8];
1111 char *e;
1112 int cid = conf.anti_alias ? c->id : -c->id;
1113 #ifdef HAVE_GDIMAGESTRINGFT
1114 e = gdImageStringFT(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1115 #else
1116 e = gdImageStringTTF(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1117 #endif
1118 if(!e)
1119 return;
1120 }
1121 #endif
1122 yy = -yy;
1123 gdImageString(im, f->gdfont, x+xx+1, y+yy, s, c->id);
1124 }
1125
1126 static void draw_stringnl(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1127 {
1128 char *t;
1129 char *d;
1130 d = s = xstrdup(s);
1131 do
1132 {
1133 t = strchr(s, '\n');
1134 if(t)
1135 *t = '\0';
1136 draw_string(im, s, f, x, y, align, c);
1137 y += get_sheight(s, f);
1138 s = t+1;
1139 } while(t);
1140 xfree(d);
1141 }
1142
1143 static void draw_rev(gdImagePtr im, revision_t *r)
1144 {
1145 int lx = r->cx - r->w/2;
1146 int rx = lx + r->w;
1147 int i;
1148 int ty = r->y;
1149 draw_rbox(im, lx, ty, rx, ty+r->h, 0, &conf.rev_color, &conf.rev_bgcolor);
1150 ty += conf.rev_tspace;
1151 draw_string(im, r->rev->rev, &conf.rev_font, r->cx, ty, ALIGN_HC, &conf.rev_color);
1152 ty += get_sheight(r->rev->rev, &conf.rev_font);
1153 draw_stringnl(im, r->revtext, &conf.rev_text_font, r->cx, ty, ALIGN_HC, &conf.rev_text_color);
1154 ty += get_sheight(r->revtext, &conf.rev_text_font);
1155 for(i = 0; i < r->ntags; i++)
1156 {
1157 draw_string(im, r->tags[i]->tag, &conf.tag_font, r->cx, ty, ALIGN_HC, &conf.tag_color);
1158 ty += get_sheight(r->tags[i]->tag, &conf.tag_font) + conf.rev_separator;
1159 }
1160 }
1161
1162 static void draw_branch_box(gdImagePtr im, branch_t *b, int ypos)
1163 {
1164 int lx = b->cx - b->w/2;
1165 int rx = lx + b->w;
1166 int i;
1167 int yy;
1168
1169 draw_rbox(im, lx, ypos, rx, ypos+b->h, 5, &conf.branch_color, &conf.branch_bgcolor);
1170 yy = conf.branch_tspace;
1171 draw_string(im, b->branch->branch, &conf.branch_font, b->cx, ypos+yy, ALIGN_HC, &conf.branch_color);
1172 yy += get_sheight(b->branch->branch, &conf.branch_font);
1173 for(i = 0; i < b->ntags; i++)
1174 {
1175 draw_string(im, b->tags[i]->tag, &conf.branch_tag_font, b->cx, ypos+yy, ALIGN_HC, &conf.branch_tag_color);
1176 yy += get_sheight(b->tags[i]->tag, &conf.branch_font);
1177 }
1178 }
1179
1180 static void draw_branch(gdImagePtr im, branch_t *b)
1181 {
1182 int yy;
1183 int i;
1184 int line[4];
1185 int l;
1186 int sign;
1187
1188 line[0] = conf.rev_color.id;
1189 line[1] = gdTransparent;
1190 line[1] = gdTransparent;
1191 line[3] = conf.rev_color.id;
1192
1193 draw_branch_box(im, b, b->y);
1194
1195 if(conf.upside_down)
1196 {
1197 yy = b->y;
1198 for(i = 0; i < b->nrevs; i++)
1199 {
1200 revision_t *r = b->revs[i];
1201 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1202 gdImageLine(im, r->cx, yy, r->cx, r->y+r->h, gdStyled);
1203 for(sign = l = 1; l < conf.thick_lines; l++)
1204 {
1205 int pp = (l+1)/2*sign;
1206 gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y+r->h, gdStyled);
1207 sign *= -1;
1208 }
1209 draw_rev(im, r);
1210 yy = r->y;
1211 }
1212 if(conf.branch_dupbox)
1213 {
1214 i = b->y - b->th + b->h;
1215 gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1216 for(sign = l = 1; l < conf.thick_lines; l++)
1217 {
1218 int pp = (l+1)/2*sign;
1219 gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, conf.rev_color.id);
1220 sign *= -1;
1221 }
1222 draw_branch_box(im, b, i);
1223 }
1224 }
1225 else
1226 {
1227 yy = b->y + b->h;
1228 for(i = 0; i < b->nrevs; i++)
1229 {
1230 revision_t *r = b->revs[i];
1231 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1232 gdImageLine(im, r->cx, yy, r->cx, r->y, gdStyled);
1233 for(sign = l = 1; l < conf.thick_lines; l++)
1234 {
1235 int pp = (l+1)/2*sign;
1236 gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y, gdStyled);
1237 sign *= -1;
1238 }
1239 draw_rev(im, r);
1240 yy = r->y + r->h;
1241 }
1242 if(conf.branch_dupbox)
1243 {
1244 i = b->y + b->th - b->h;
1245 gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1246 for(sign = l = 1; l < conf.thick_lines; l++)
1247 {
1248 int pp = (l+1)/2*sign;
1249 gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, conf.rev_color.id);
1250 sign *= -1;
1251 }
1252 draw_branch_box(im, b, i);
1253 }
1254 }
1255 }
1256
1257 static void draw_connector(gdImagePtr im, branch_t *b)
1258 {
1259 int l;
1260 int sign;
1261 revision_t *r = b->branchpoint;
1262 int x1 = r->cx + r->w/2 + 2;
1263 int y1 = r->y + r->h/2;
1264 int x2 = b->cx;
1265 int y2 = b->y;
1266 if(conf.upside_down)
1267 y2 += b->h;
1268 gdImageLine(im, x1, y1, x2, y1, conf.branch_color.id);
1269 gdImageLine(im, x2, y1, x2, y2, conf.branch_color.id);
1270 for(sign = l = 1; l < conf.thick_lines; l++)
1271 {
1272 int pp = (l+1)/2*sign;
1273 gdImageLine(im, x1, y1+pp, x2-pp, y1+pp, conf.branch_color.id);
1274 gdImageLine(im, x2+pp, y1-pp, x2+pp, y2, conf.branch_color.id);
1275 sign *= -1;
1276 }
1277 }
1278
1279 static void alloc_color(gdImagePtr im, color_t *c)
1280 {
1281 c->id = gdImageColorAllocate(im, c->r, c->g, c->b);
1282 }
1283
1284 gdImagePtr make_image(rcsfile_t *rcs)
1285 {
1286 gdImagePtr im;
1287 int i;
1288 char *cptr;
1289
1290 cptr = expand_string(conf.title, rcs, NULL, NULL, NULL, NULL);
1291 i = get_swidth(cptr, &conf.title_font);
1292 if(rcs->tw+conf.margin_left+conf.margin_right > i)
1293 i = rcs->tw+conf.margin_left+conf.margin_right;
1294 im = gdImageCreate(i, rcs->th+conf.margin_top+conf.margin_bottom);
1295 alloc_color(im, &conf.color_bg);
1296 alloc_color(im, &conf.tag_color);
1297 alloc_color(im, &conf.rev_color);
1298 alloc_color(im, &conf.rev_bgcolor);
1299 alloc_color(im, &conf.rev_text_color);
1300 alloc_color(im, &conf.branch_color);
1301 alloc_color(im, &conf.branch_tag_color);
1302 alloc_color(im, &conf.branch_bgcolor);
1303 alloc_color(im, &conf.title_color);
1304 alloc_color(im, &black_color);
1305 alloc_color(im, &white_color);
1306
1307 if(conf.transparent_bg)
1308 gdImageColorTransparent(im, conf.color_bg.id);
1309
1310 for(i = 0; i < rcs->nbranches; i++)
1311 draw_branch(im, rcs->branches[i]);
1312 for(i = 0; i < rcs->nbranches; i++)
1313 {
1314 if(rcs->branches[i]->branchpoint)
1315 draw_connector(im, rcs->branches[i]);
1316 }
1317 draw_stringnl(im, cptr, &conf.title_font, conf.title_x, conf.title_y, conf.title_align, &conf.title_color);
1318 xfree(cptr);
1319
1320 return im;
1321 }
1322
1323 /*
1324 **************************************************************************
1325 * Layout routines
1326 *
1327 * Branch BBox:
1328 * left = center_x - total_width / 2 (cx-tw)/2
1329 * right = center_x + total_width / 2 (cx+tw)/2
1330 * top = y_pos (y)
1331 * bottom = y_pos + total_height (y+th)
1332 *
1333 * Margins of branches:
1334 *
1335 * . .
1336 * . .
1337 * +--------------+
1338 * ^
1339 * | branch_margin .
1340 * v .
1341 * ----------------+ .
1342 * | ^ |
1343 * | | branch_connect |
1344 * | v |
1345 *..-+ +t-----+------+ +------+------+
1346 * | l | | |
1347 * | <--> | branch bbox | <--> | branch bbox |
1348 * | | | r | | |
1349 *..-+ | +------------b+ | +-------------+
1350 * | ^ branch_margin
1351 * | | branch_margin
1352 * | v
1353 * | +-------------+
1354 * | . .
1355 * | . .
1356 * |
1357 * branch_margin
1358 *
1359 * FIXME: There are probable som +/-1 errors in the code...
1360 * (notably shadows are not calculated in the margins)
1361 **************************************************************************
1362 */
1363 static void move_branch(branch_t *b, int x, int y)
1364 {
1365 int i;
1366 b->cx += x;
1367 b->y += y;
1368 for(i = 0; i < b->nrevs; i++)
1369 {
1370 b->revs[i]->cx += x;
1371 b->revs[i]->y += y;
1372 }
1373 }
1374
1375 static void initial_reposition_branch(revision_t *r, int *x, int *w)
1376 {
1377 int i, j;
1378 for(j = 0; j < r->nbranches; j++)
1379 {
1380 branch_t *b = r->branches[j];
1381 *x += *w + conf.rev_minline + b->tw/2 - b->cx;
1382 *w = b->tw/2;
1383 move_branch(b, *x, r->y + r->h/2 + conf.branch_connect);
1384 *x = b->cx;
1385 /* Recurse to move branches of branched revisions */
1386 for(i = b->nrevs-1; i >= 0; i--)
1387 {
1388 initial_reposition_branch(b->revs[i], x, w);
1389 }
1390 }
1391 }
1392
1393 static void rect_union(int *x, int *y, int *w, int *h, branch_t *b)
1394 {
1395 int x1 = *x;
1396 int x2 = x1 + *w;
1397 int y1 = *y;
1398 int y2 = y1 + *h;
1399 int xx1 = b->cx - b->tw/2;
1400 int xx2 = xx1 + b->tw;
1401 int yy1 = b->y;
1402 int yy2 = yy1 + b->th;
1403 x1 = MIN(x1, xx1);
1404 x2 = MAX(x2, xx2);
1405 y1 = MIN(y1, yy1);
1406 y2 = MAX(y2, yy2);
1407 *x = x1;
1408 *y = y1;
1409 *w = x2 - x1;
1410 *h = y2 - y1;
1411 }
1412
1413 static int branch_intersects(int top, int bottom, int left, branch_t *b)
1414 {
1415 int br = b->cx + b->tw/2;
1416 int bt = b->y - conf.branch_connect - conf.branch_margin/2;
1417 int bb = b->y + b->th + conf.branch_margin/2;
1418 return !(bt > bottom || bb < top || br >= left);
1419 }
1420
1421 static int kern_branch(rcsfile_t *rcs, branch_t *b)
1422 {
1423 int left = b->cx - b->tw/2;
1424 int top = b->y - conf.branch_connect - conf.branch_margin/2;
1425 int bottom = b->y + b->th + conf.branch_margin/2;
1426 int i;
1427 int xpos = 0;
1428
1429 for(i = 0; i < rcs->nbranches; i++)
1430 {
1431 branch_t *bp = rcs->branches[i];
1432 if(bp == b)
1433 continue;
1434 if(branch_intersects(top, bottom, left, bp))
1435 {
1436 int m = bp->cx + bp->tw/2 + conf.branch_margin;
1437 if(m > xpos)
1438 xpos = m;
1439 }
1440 }
1441 if(xpos && (b->cx - b->tw/2) - xpos > 0)
1442 {
1443 move_branch(b, xpos - (b->cx - b->tw/2), 0);
1444 return 1;
1445 }
1446 return 0;
1447 }
1448
1449 static int kern_tree(rcsfile_t *rcs)
1450 {
1451 int i;
1452 int moved;
1453 int safeguard;
1454 int totalmoved = 0;
1455 for(moved = 1, safeguard = LOOPSAFEGUARD; moved && safeguard; safeguard--)
1456 {
1457 moved = 0;
1458 for(i = 1; i < rcs->nbranches; i++)
1459 {
1460 moved += kern_branch(rcs, rcs->branches[i]);
1461 }
1462 totalmoved += moved;
1463 #ifdef DEBUG
1464 fprintf(stderr, "kern_tree: moved=%d\n", moved);
1465 #endif
1466 }
1467 if(!quiet && !safeguard)
1468 fprintf(stderr, "kern_tree: safeguard terminated possible infinite loop; please report.\n");
1469 return totalmoved;
1470 }
1471
1472 static int index_of_revision(revision_t *r)
1473 {
1474 branch_t *b = r->branch;
1475 int i;
1476 for(i = 0; i < b->nrevs; i++)
1477 {
1478 if(r == b->revs[i])
1479 return i;
1480 }
1481 fprintf(stderr, "index_of_revision: Cannot find revision in branch\n");
1482 return 0;
1483 }
1484
1485 static void branch_bbox(branch_t *br, int *l, int *r, int *t, int *b)
1486 {
1487 if(l) *l = br->cx - br->tw/2;
1488 if(r) *r = br->cx + br->tw/2;
1489 if(t) *t = br->y;
1490 if(b) *b = br->y + br->th + (conf.branch_dupbox ? conf.rev_minline + br->h : 0);
1491 }
1492
1493 static void branch_ext_bbox(branch_t *br, int *l, int *r, int *t, int *b)
1494 {
1495 int extra = conf.branch_margin & 1; /* Correct +/-1 error on div 2 */
1496 branch_bbox(br, l, r, t, b);
1497 if(l) *l -= conf.branch_margin/2;
1498 if(r) *r += conf.branch_margin/2 + extra;
1499 if(t) *t -= conf.branch_connect + conf.branch_margin/2;
1500 if(b) *b += conf.branch_margin/2 + extra;
1501 }
1502
1503 static int branch_distance(branch_t *br1, branch_t *br2)
1504 {
1505 int l1, r1, t1, b1;
1506 int l2, r2, t2, b2;
1507 assert(br1 != NULL);
1508 assert(br2 != NULL);
1509 branch_bbox(br1, &l1, &r1, NULL, NULL);
1510 branch_bbox(br2, &l2, &r2, NULL, NULL);
1511 branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
1512 branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
1513 /* Return:
1514 * - 0 if branches have no horizontal overlap
1515 * - positive if b1 is left of b2
1516 * - negative if b2 is left of b1
1517 */
1518 if((t1 > t2 && t1 < b2) || (b1 > t2 && b1 < b2))
1519 return l1 < l2 ? l2 - r1 : -(l1 - r2);
1520 else
1521 return 0;
1522 }
1523
1524 static int space_needed(branch_t *br1, branch_t *br2)
1525 {
1526 int t1, b1;
1527 int t2, b2;
1528 assert(br1 != NULL);
1529 assert(br2 != NULL);
1530 assert(br1->cx < br2->cx); /* br1 must be left of br2 */
1531 branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
1532 branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
1533 /* Return:
1534 * - positive if top br1 is located lower than br2
1535 * - negatve is top br2 is located lower than br1
1536 */
1537 if(t1 > t2)
1538 return -(t1 - b2);
1539 else
1540 return t2 - b1;
1541 }
1542
1543 static void move_yr_branch(branch_t *b, int dy)
1544 {
1545 int i, j;
1546 #ifdef DEBUG
1547 /* fprintf(stderr, "move_yr_branch: b=%s, dy=%d\n", b->branch->branch, dy);*/
1548 #endif
1549 b->y += dy;
1550 for(i = 0; i < b->nrevs; i++)
1551 {
1552 b->revs[i]->y += dy;
1553 for(j = 0; j < b->revs[i]->nbranches; j++)
1554 {
1555 #ifdef DEBUG
1556 /* fprintf(stderr, ".");*/
1557 #endif
1558 move_yr_branch(b->revs[i]->branches[j], dy);
1559 }
1560 }
1561 }
1562
1563 static void move_trunk(revision_t *r, int dy)
1564 {
1565 int i, j;
1566 branch_t *b = r->branch;
1567 b->th += dy;
1568 for(i = index_of_revision(r); i < b->nrevs; i++)
1569 {
1570 #ifdef DEBUG
1571 fprintf(stderr, "move_trunk: start %s, moving %s by %d (b's %d)\n", r->rev->rev, b->revs[i]->rev->rev, dy, b->revs[i]->nbranches);
1572 #endif
1573 b->revs[i]->y += dy;
1574 for(j = 0; j < b->revs[i]->nbranches; j++)
1575 {
1576 move_yr_branch(b->revs[i]->branches[j], dy);
1577 }
1578 }
1579 }
1580
1581 static int space_below(rcsfile_t *rcs, revision_t *r)
1582 {
1583 int i, j;
1584 int bl, br, bb;
1585 int space = INT_MAX;
1586 branch_t *b = r->branch;
1587 branch_t *minb = NULL;
1588
1589 branch_ext_bbox(b, &bl, &br, NULL, &bb);
1590 for(i = 0; i < rcs->nbranches; i++)
1591 {
1592 int tbl, tbr, tbt;
1593 branch_t *tb = rcs->branches[i];
1594 branch_ext_bbox(tb, &tbl, &tbr, &tbt, NULL);
1595 if(tb == b)
1596 continue;
1597 if(tbt > bb) /* Must be below our branch */
1598 {
1599 if(tb->branchpoint) /* Take account for the horiz connector */
1600 tbl = tb->branchpoint->cx + tb->branchpoint->branch->tw/2;
1601 if((bl >= tbl && bl <= tbr) || (br <= tbr && br >= tbl))
1602 {
1603 int s = tbt - bb - conf.branch_connect;
1604 if(s < space)
1605 {
1606 space = s;
1607 minb = tb;
1608 }
1609 }
1610 }
1611 }
1612 if(b->branchpoint)
1613 {
1614 for(i = index_of_revision(r); i < b->nrevs; i++)
1615 {
1616 for(j = 0; j < b->revs[i]->nbranches; j++)
1617 {
1618 int s = space_below(rcs, b->revs[i]->branches[j]->revs[0]);
1619 if(s < space)
1620 space = s;
1621 }
1622 }
1623 }
1624 #ifdef DEBUG
1625 fprintf(stderr, "space_below: from %s have %d to %s\n", b->branch->branch, space, minb ? minb->branch->branch : "<recursed>");
1626 #endif
1627 return space;
1628 }
1629
1630 static int space_available(rcsfile_t *rcs, branch_t *colbr, branch_t *tagbr, int *nl, revision_t **bpcommon)
1631 {
1632 int i;
1633 int space = 0;
1634 int nlinks = 0;
1635 revision_t *r;
1636 branch_t *b;
1637 branch_t *ancestor;
1638 revision_t *branchpoint;
1639
1640 if(!tagbr->branchpoint || !colbr->branchpoint)
1641 {
1642 if(!quiet)
1643 fprintf(stderr, "space_available: Trying to stretch the top?\n");
1644 return 0;
1645 }
1646
1647 r = colbr->branchpoint;
1648 b = r->branch;
1649 branchpoint = tagbr->branchpoint;
1650 ancestor = branchpoint->branch;
1651 assert(b != NULL);
1652 assert(ancestor != NULL);
1653
1654 while(1)
1655 {
1656 int s;
1657 int rtag = b == ancestor ? index_of_revision(branchpoint)+1 : 0;
1658 for(i = index_of_revision(r); i >= rtag; i--)
1659 {
1660 if(i > 0)
1661 s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
1662 else
1663 s = b->revs[i]->y - (b->y + b->h);
1664 if(s < conf.rev_maxline)
1665 {
1666 space += conf.rev_maxline - s;
1667 nlinks++;
1668 }
1669 }
1670 s = space_below(rcs, r);
1671 if(s < space)
1672 space = s;
1673 #ifdef DEBUG
1674 if(space < 0)
1675 return -1;
1676 #endif
1677 if(b == ancestor)
1678 break;
1679 r = b->branchpoint;
1680 if(!r)
1681 {
1682 /* Not a common ancestor */
1683 r = colbr->branchpoint;
1684 b = r->branch;
1685 branchpoint = ancestor->branchpoint;
1686 if(!branchpoint)
1687 {
1688 if(!quiet)
1689 fprintf(stderr, "space_available: No common ancestor?\n");
1690 return 0;
1691 }
1692 ancestor = branchpoint->branch;
1693 assert(ancestor != NULL);
1694 nlinks = 0;
1695 space = 0;
1696 continue; /* Restart with a new ancestor */
1697 }
1698 b = r->branch;
1699 }
1700 if(nl)
1701 *nl = nlinks; /* Return the number of links that can stretch */
1702 if(bpcommon)
1703 *bpcommon = branchpoint; /* Return the ancestral branchpoint on the common branch */
1704 return space;
1705 }
1706
1707 static int stretch_branches(rcsfile_t *rcs, branch_t *br1, branch_t *br2, int totalstretch)
1708 {
1709 revision_t *r;
1710 revision_t *bpcommon = NULL;
1711 branch_t *ancestor = NULL;
1712 branch_t *b;
1713 int i;
1714 int space;
1715 int nlinks;
1716 int dy;
1717 int rest;
1718
1719 space = space_available(rcs, br1, br2, &nlinks, &bpcommon);
1720 if(bpcommon)
1721 ancestor = bpcommon->branch;
1722
1723 #ifdef DEBUG
1724 if(space == -1)
1725 return 0;
1726 fprintf(stderr, "stretch_branches: space available %d over %d links common %s\n", space, nlinks, ancestor->branch->branch);
1727 #endif
1728 if(space < totalstretch)
1729 return 0;
1730
1731 dy = totalstretch / nlinks;
1732 rest = totalstretch - dy * nlinks;
1733
1734 r = br1->branchpoint;
1735 b = r->branch;
1736 while(1)
1737 {
1738 int rtag = b == ancestor ? index_of_revision(bpcommon)+1 : 0;
1739 for(i = index_of_revision(r); i >= rtag; i--)
1740 {
1741 int s, q;
1742 if(i > 0)
1743 s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
1744 else
1745 s = b->revs[i]->y - (b->y + b->h);
1746 q = conf.rev_maxline - s;
1747 if(q > 0)
1748 {
1749 int d = rest ? rest/nlinks+1 : 0;
1750 if(q >= dy+d)
1751 {
1752 move_trunk(b->revs[i], dy+d);
1753 }
1754 else
1755 {
1756 move_trunk(b->revs[i], q);
1757 rest += dy+d - q;
1758 }
1759 rest -= d;
1760 nlinks--;
1761 }
1762 }
1763 if(b == ancestor)
1764 break;
1765 r = b->branchpoint;
1766 assert(r != NULL); /* else 'space_available' wouldn't have returned positively */
1767 b = r->branch;
1768 }
1769 return 1;
1770 }
1771
1772 static branch_t *find_collision_branch(rcsfile_t *rcs, branch_t *b)
1773 {
1774 int i;
1775 int dist = INT_MAX;
1776 branch_t *col = NULL;
1777
1778 for(i = 0; i < rcs->nbranches; i++)
1779 {
1780 int t = branch_distance(rcs->branches[i], b);
1781 if(t > 0 && t < dist)
1782 {
1783 dist = t;
1784 col = rcs->branches[i];
1785 }
1786 }
1787 return col;
1788 }
1789
1790 void auto_stretch(rcsfile_t *rcs)
1791 {
1792 int i;
1793 int safeguard;
1794
1795 for(i = 0, safeguard = LOOPSAFEGUARD; i < rcs->nbranches && safeguard; i++)
1796 {
1797 int bl, pr;
1798 branch_t *b = rcs->branches[i];
1799 if(!b->branchpoint)
1800 continue;
1801 branch_bbox(b, &bl, NULL, NULL, NULL);
1802 branch_bbox(b->branchpoint->branch, NULL, &pr, NULL, NULL);
1803 if(bl - conf.branch_margin - pr > 0)
1804 {
1805 branch_t *col;
1806 int spaceneeded;
1807 /* There is a potential to move branch b further left.
1808 * All branches obstructing this one from moving further
1809 * left must be originating from revisions below
1810 * b->branchpoint until a common ancester.
1811 * So, we search all branches for a branch that lies left
1812 * of b and is closest to b. This is then the collission
1813 * branch that needs to be moved.
1814 */
1815 col = find_collision_branch(rcs, b);
1816 if(!col)
1817 continue;
1818 spaceneeded = space_needed(col, b);
1819 if(spaceneeded < 0)
1820 continue;
1821 #ifdef DEBUG
1822 fprintf(stderr, "auto_stretch: %s collides %s need %d\n", b->branch->branch, col->branch->branch, spaceneeded);
1823 #endif
1824 /* Trace the collision branch back to find the common ancester
1825 * of both col and b. All revisions encountered while traversing
1826 * backwards must be stretched, including all revisions on the
1827 * common ancester from where the branches sprout.
1828 */
1829 if(stretch_branches(rcs, col, b, spaceneeded))
1830 {
1831 if(kern_tree(rcs))
1832 {
1833 /* Restart the process because movement can
1834 * cause more movement.
1835 */
1836 i = 0 - 1; /* -1 for the i++ of the loop */
1837 safeguard--; /* Prevent infinite loop, just in case */
1838 }
1839 /*return;*/
1840 }
1841 }
1842 }
1843 if(!quiet && !safeguard)
1844 fprintf(stderr, "auto_stretch: safeguard terminated possible infinite loop; please report.\n");
1845 }
1846
1847 void make_layout(rcsfile_t *rcs)
1848 {
1849 int i, j;
1850 int x, y;
1851 int w, h;
1852 int w2;
1853
1854 /* Remove all unwanted revisions */
1855 if(conf.strip_untagged)
1856 {
1857 int fr = conf.strip_first_rev ? 0 : 1;
1858 for(i = 0; i < rcs->nbranches; i++)
1859 {
1860 branch_t *bp = rcs->branches[i];
1861 for(j = fr; j < bp->nrevs-1; j++)
1862 {
1863 if(!bp->revs[j]->ntags && !bp->revs[j]->nbranches)
1864 {
1865 memmove(&bp->revs[j], &bp->revs[j+1], (bp->nrevs-j-1) * sizeof(bp->revs[0]));
1866 bp->nrevs--;
1867 bp->revs[j]->stripped = 1;
1868 j--;
1869 }
1870 }
1871 }
1872 }
1873
1874 /* Calculate the box-sizes of the revisions */
1875 for(i = 0; i < rcs->nsrev; i++)
1876 {
1877 revision_t *rp;
1878 int w;
1879 int h;
1880 rp = rcs->srev[i];
1881 rp->revtext = expand_string(conf.rev_text, rcs, rp, rp->rev, NULL, rp->ntags ? rp->tags[0] : NULL);
1882 w = get_swidth(rp->revtext, &conf.rev_text_font);
1883 j = get_swidth(rp->rev->rev, &conf.rev_font);
1884 if(j > w)
1885 w = j;
1886 h = get_sheight(rp->revtext, &conf.rev_text_font) + get_sheight(rp->rev->rev, &conf.rev_font);
1887 for(j = 0; j < rp->ntags; j++)
1888 {
1889 int ww = get_swidth(rp->tags[j]->tag, &conf.tag_font);
1890 if(ww > w) w = ww;
1891 h += get_sheight(rp->tags[j]->tag, &conf.tag_font) + conf.rev_separator;
1892 }
1893 rp->w = w + conf.rev_lspace + conf.rev_rspace;
1894 rp->h = h + conf.rev_tspace + conf.rev_bspace;
1895 }
1896
1897 /* Calculate the box-sizes of the branches */
1898 for(i = 0; i < rcs->nbranches; i++)
1899 {
1900 branch_t *bp = rcs->branches[i];
1901 int w;
1902 int h;
1903 w = get_swidth(bp->branch->branch, &conf.branch_font);
1904 h = get_sheight(bp->branch->branch, &conf.branch_font);
1905 for(j = 0; j < bp->ntags; j++)
1906 {
1907 int ww = get_swidth(bp->tags[j]->tag, &conf.branch_tag_font);
1908 if(ww > w) w = ww;
1909 h += get_sheight(bp->tags[j]->tag, &conf.branch_tag_font);
1910 }
1911 w += conf.branch_lspace + conf.branch_rspace;
1912 h += conf.branch_tspace + conf.branch_bspace;
1913 bp->w = w;
1914 bp->h = h;
1915 for(j = 0; j < bp->nrevs; j++)
1916 {
1917 if(bp->revs[j]->w > w)
1918 w = bp->revs[j]->w;
1919 h += bp->revs[j]->h + conf.rev_minline;
1920 }
1921 if(conf.branch_dupbox)
1922 h += bp->h + conf.rev_minline;
1923 bp->th = h;
1924 bp->tw = w;
1925 }
1926
1927 /* Calculate the relative positions of revs in a branch */
1928 for(i = 0; i < rcs->nbranches; i++)
1929 {
1930 branch_t *b = rcs->branches[i];
1931 x = b->tw/2;
1932 y = b->h;
1933 b->cx = x;
1934 b->y = 0;
1935 for(j = 0; j < b->nrevs; j++)
1936 {
1937 y += conf.rev_minline;
1938 b->revs[j]->cx = x;
1939 b->revs[j]->y = y;
1940 y += b->revs[j]->h;
1941 }
1942 }
1943
1944 /* Initially reposition the branches from bottom to top progressively right */
1945 x = rcs->branches[0]->cx;
1946 w2 = rcs->branches[0]->tw / 2;
1947 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
1948 {
1949 initial_reposition_branch(rcs->branches[0]->revs[i], &x, &w2);
1950 }
1951
1952 /* Initially move branches left if there is room */
1953 kern_tree(rcs);
1954
1955 /* Try to kern the branches more by expanding the inter-revision spacing */
1956 if(conf.auto_stretch)
1957 auto_stretch(rcs);
1958
1959 /* Move everything w.r.t. the top-left margin */
1960 for(i = 0; i < rcs->nbranches; i++)
1961 move_branch(rcs->branches[i], conf.margin_left, conf.margin_top);
1962
1963 /* Calculate overall image size */
1964 x = rcs->branches[0]->cx - rcs->branches[0]->tw/2;
1965 y = rcs->branches[0]->y;
1966 w = rcs->branches[0]->tw;
1967 h = rcs->branches[0]->th;
1968 for(i = 1; i < rcs->nbranches; i++)
1969 rect_union(&x, &y, &w, &h, rcs->branches[i]);
1970 rcs->tw = w;
1971 rcs->th = h;
1972
1973 /* Flip the entire tree */
1974 if(conf.upside_down)
1975 {
1976 y += rcs->th;
1977 for(i = 0; i < rcs->nbranches; i++)
1978 {
1979 branch_t *b = rcs->branches[i];
1980 for(j = 0; j < b->nrevs; j++)
1981 {
1982 revision_t *r = b->revs[j];
1983 r->y = y - r->y - r->h + conf.margin_top;
1984 }
1985 b->y = y - b->y - b->h + conf.margin_top;
1986 }
1987 }
1988 }
1989
1990 /*
1991 **************************************************************************
1992 * Imagemap functions
1993 **************************************************************************
1994 */
1995 void make_imagemap(rcsfile_t *rcs, FILE *fp)
1996 {
1997 int i, j;
1998 fprintf(fp, "<map name=\"%s\">\n", conf.map_name);
1999 for(i = 0; i < rcs->nbranches; i++)
2000 {
2001 branch_t *b = rcs->branches[i];
2002 tag_t *tag = b->ntags ? b->tags[0] : NULL;
2003 char *bhref = expand_string(conf.map_branch_href, rcs, NULL, b->branch, NULL, tag);
2004 char *balt = expand_string(conf.map_branch_alt, rcs, NULL, b->branch, NULL, tag);
2005 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2006 bhref,
2007 b->cx - b->w/2, b->y, b->cx + b->w/2, b->y + b->h,
2008 balt);
2009 for(j = 0; j < b->nrevs; j++)
2010 {
2011 revision_t *r = b->revs[j];
2012 revision_t* r1;
2013 int xoff;
2014 int x1;
2015 int x2;
2016 int y1;
2017 int y2;
2018 char *href;
2019 char *alt;
2020
2021 tag = r->ntags ? r->tags[0] : NULL;
2022 href = expand_string(conf.map_rev_href, rcs, r, r->rev, NULL, tag);
2023 alt = expand_string(conf.map_rev_alt, rcs, r, r->rev, NULL, tag);
2024 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2025 href,
2026 r->cx - r->w/2, r->y, r->cx + r->w/2, r->y + r->h,
2027 alt);
2028 xfree(href);
2029 xfree(alt);
2030 if(j > 0 || b->branchpoint)
2031 {
2032 if(j > 0)
2033 {
2034 r1 = b->revs[j-1];
2035 xoff = MIN(r->w, r1->w)/4;
2036 y1 = conf.upside_down ? r1->y : r1->y + r1->h;
2037 }
2038 else
2039 {
2040 r1 = b->branchpoint;
2041 xoff = MIN(r->w, b->w)/4;
2042 y1 = conf.upside_down ? b->y : b->y + b->h;
2043 }
2044 x1 = r->cx - xoff;
2045 x2 = r->cx + xoff;
2046 y2 = conf.upside_down ? r->y + r->h : r->y;
2047 href = expand_string(conf.map_diff_href, rcs, r, r->rev, r1->rev, tag);
2048 alt = expand_string(conf.map_diff_alt, rcs, r, r->rev, r1->rev, tag);
2049 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2050 href,
2051 x1, (conf.upside_down ? y2 : y1) + 1, x2, (conf.upside_down ? y1 : y2) - 1,
2052 alt);
2053 xfree(href);
2054 xfree(alt);
2055 }
2056 }
2057 if(conf.branch_dupbox)
2058 {
2059 int y;
2060 if(conf.upside_down)
2061 y = b->y + b->h - b->th;
2062 else
2063 y = b->y - b->h + b->th;
2064 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2065 bhref,
2066 b->cx - b->w/2, y, b->cx + b->w/2, y + b->h,
2067 balt);
2068 }
2069 xfree(bhref);
2070 xfree(balt);
2071 }
2072 fprintf(fp, "</map>\n");
2073 }
2074
2075 /*
2076 **************************************************************************
2077 * Program entry
2078 **************************************************************************
2079 */
2080 static const char usage_str[] =
2081 "Usage: cvsgraph [options] <file>\n"
2082 " -b Add a branch box at both sides of the trunk (config value is negated)\n"
2083 " -c <file> Read alternative config from <file>\n"
2084 " -d <level> Enable debug mode at <level>\n"
2085 " -h This message\n"
2086 " -i Generate an imagemap instead of image\n"
2087 " -I <file> Also write the imagemap to <file>\n"
2088 " -k Auto stretch the tree (config value is negated)\n"
2089 " -M <name> Use <name> as imagemap name\n"
2090 " -m <mod> Use <mod> as cvs module\n"
2091 " -o <file> Output to <file>\n"
2092 " -O <opt=val> Set option opt to value val\n"
2093 " -q Be quiet (i.e. no warnings)\n"
2094 " -r <path> Use <path> as cvsroot path\n"
2095 " -s Strip untagged revisions (config value is negated)\n"
2096 " -S Also strip the first revision (config value is negated)\n"
2097 " -u Upside down image (mirror vertically; config value is negated)\n"
2098 " -V Print version and exit\n"
2099 " -[0-9] <txt> Use <txt> for expansion\n"
2100 ;
2101
2102 #define VERSION_STR "1.2.1"
2103 #define NOTICE_STR "Copyright (c) 2001,2002 B.Stultiens"
2104
2105 static void append_slash(char **path)
2106 {
2107 int l;
2108 assert(path != NULL);
2109 assert(*path != NULL);
2110 l = strlen(*path);
2111 if(!l || (*path)[l-1] == '/')
2112 return;
2113 *path = xrealloc(*path, l+2);
2114 strcat(*path, "/");
2115 }
2116
2117 int main(int argc, char *argv[])
2118 {
2119 extern int rcs_flex_debug;
2120 extern int rcsdebug;
2121 int optc;
2122 char *confpath = NULL;
2123 char *outfile = NULL;
2124 char *cvsroot = NULL;
2125 char *cvsmodule = NULL;
2126 int imagemap = 0;
2127 int upsidedown = 0;
2128 int bdupbox = 0;
2129 int stripuntag = 0;
2130 int stripfirst = 0;
2131 int autostretch = 0;
2132 char *imgmapname = NULL;
2133 char *imgmapfile = NULL;
2134 int lose = 0;
2135 FILE *fp;
2136 char *rcsfilename;
2137 rcsfile_t *rcs;
2138 gdImagePtr im;
2139
2140 while((optc = getopt(argc, argv, "0:1:2:3:4:5:6:7:8:9:bc:d:hI:ikM:m:O:o:qr:SsuV")) != EOF)
2141 {
2142 switch(optc)
2143 {
2144 case 'b':
2145 bdupbox = 1;
2146 break;
2147 case 'c':
2148 confpath = xstrdup(optarg);
2149 break;
2150 case 'd':
2151 debuglevel = strtol(optarg, NULL, 0);
2152 break;
2153 case 'I':
2154 imgmapfile = xstrdup(optarg);
2155 break;
2156 case 'i':
2157 imagemap = 1;
2158 break;
2159 case 'k':
2160 autostretch = 1;
2161 break;
2162 case 'M':
2163 imgmapname = xstrdup(optarg);
2164 break;
2165 case 'm':
2166 cvsmodule = xstrdup(optarg);
2167 break;
2168 case 'O':
2169 stack_option(optarg);
2170 break;
2171 case 'o':
2172 outfile = xstrdup(optarg);
2173 break;
2174 case 'q':
2175 quiet = 1;
2176 break;
2177 case 'r':
2178 cvsroot = xstrdup(optarg);
2179 break;
2180 case 'S':
2181 stripfirst = 1;
2182 break;
2183 case 's':
2184 stripuntag = 1;
2185 break;
2186 case 'u':
2187 upsidedown = 1;
2188 break;
2189 case 'V':
2190 fprintf(stdout, "cvsgraph v%s, %s\n", VERSION_STR, NOTICE_STR);
2191 return 0;
2192 case 'h':
2193 fprintf(stdout, "%s", usage_str);
2194 return 0;
2195 default:
2196 if(isdigit(optc))
2197 {
2198 conf.expand[optc-'0'] = xstrdup(optarg);
2199 }
2200 else
2201 lose++;
2202 }
2203 }
2204
2205 if(lose)
2206 {
2207 fprintf(stderr, "%s", usage_str);
2208 return 1;
2209 }
2210
2211 if(debuglevel)
2212 {
2213 setvbuf(stdout, NULL, 0, _IONBF);
2214 setvbuf(stderr, NULL, 0, _IONBF);
2215 }
2216 rcs_flex_debug = (debuglevel & DEBUG_RCS_LEX) != 0;
2217 rcsdebug = (debuglevel & DEBUG_RCS_YACC) != 0;
2218
2219 /* Set defaults */
2220 conf.tag_font.gdfont = gdFontTiny;
2221 conf.rev_font.gdfont = gdFontTiny;
2222 conf.branch_font.gdfont = gdFontTiny;
2223 conf.branch_tag_font.gdfont = gdFontTiny;
2224 conf.title_font.gdfont = gdFontTiny;
2225 conf.rev_text_font.gdfont = gdFontTiny;
2226
2227 conf.anti_alias = 1;
2228 conf.thick_lines = 1;
2229
2230 conf.cvsroot = xstrdup("");
2231 conf.cvsmodule = xstrdup("");
2232 conf.date_format = xstrdup("%d-%b-%Y %H:%M:%S");
2233 conf.title = xstrdup("");
2234 conf.map_name = xstrdup("CvsGraphImageMap");
2235 conf.map_branch_href = xstrdup("href=\"unset: conf.map_branch_href\"");
2236 conf.map_branch_alt = xstrdup("alt=\"%B\"");
2237 conf.map_rev_href = xstrdup("href=\"unset: conf.map_rev_href\"");
2238 conf.map_rev_alt = xstrdup("alt=\"%R\"");
2239 conf.map_diff_href = xstrdup("href=\"unset: conf.map_diff_href\"");
2240 conf.map_diff_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
2241 conf.rev_text = xstrdup("%d");
2242
2243 conf.color_bg = white_color;
2244 conf.branch_bgcolor = white_color;
2245 conf.branch_color = black_color;
2246 conf.branch_tag_color = black_color;
2247 conf.rev_color = black_color;
2248 conf.rev_bgcolor = white_color;
2249 conf.tag_color = black_color;
2250 conf.title_color = black_color;
2251 conf.rev_text_color = black_color;
2252
2253 conf.image_quality = 100;
2254 conf.rev_maxline = -1; /* Checked later to set to default */
2255
2256 read_config(confpath);
2257
2258 if(conf.rev_maxline == -1) conf.rev_maxline = 5 * conf.rev_minline;
2259
2260 /* Set overrides */
2261 if(cvsroot) conf.cvsroot = cvsroot;
2262 if(cvsmodule) conf.cvsmodule = cvsmodule;
2263 if(imgmapname) conf.map_name = imgmapname;
2264 if(upsidedown) conf.upside_down = !conf.upside_down;
2265 if(bdupbox) conf.branch_dupbox = !conf.branch_dupbox;
2266 if(stripuntag) conf.strip_untagged = !conf.strip_untagged;
2267 if(stripfirst) conf.strip_first_rev = !conf.strip_first_rev;
2268 if(autostretch) conf.auto_stretch = !conf.auto_stretch;
2269
2270 if(conf.rev_minline >= conf.rev_maxline)
2271 {
2272 if(conf.auto_stretch && !quiet)
2273 fprintf(stderr, "Auto stretch is only possible if rev_minline < rev_maxline\n");
2274 conf.auto_stretch = 0;
2275 }
2276
2277 if(conf.thick_lines < 1)
2278 conf.thick_lines = 1;
2279 if(conf.thick_lines > 11)
2280 conf.thick_lines = 11;
2281
2282 append_slash(&conf.cvsroot);
2283 append_slash(&conf.cvsmodule);
2284
2285 if(optind >= argc)
2286 {
2287 #ifdef __WIN32__
2288 /* Bad hack for DOS/Windows */
2289 if(setmode(fileno(stdin), O_BINARY) == -1)
2290 {
2291 perror("Set binary mode for stdin");
2292 return 1;
2293 }
2294 #endif
2295 rcsfilename = NULL;
2296 }
2297 else
2298 rcsfilename = argv[optind];
2299
2300 rcs = get_rcsfile(conf.cvsroot, conf.cvsmodule, rcsfilename);
2301 if(!rcs)
2302 return 1;
2303
2304 if(debuglevel & DEBUG_RCS_FILE)
2305 dump_rcsfile(rcs);
2306
2307 if(!reorganise_branches(rcs))
2308 return 1;
2309
2310 if(!assign_tags(rcs))
2311 return 1;
2312
2313 if(outfile)
2314 {
2315 if((fp = fopen(outfile, "wb")) == NULL)
2316 {
2317 perror(outfile);
2318 return 1;
2319 }
2320 }
2321 else
2322 {
2323 fp = stdout;
2324 #ifdef __WIN32__
2325 /* Bad hack for DOS/Windows */
2326 if(setmode(fileno(fp), O_BINARY) == -1)
2327 {
2328 perror("Set binary mode for stdout");
2329 return 1;
2330 }
2331 #endif
2332 }
2333
2334 make_layout(rcs);
2335
2336 if(!imagemap)
2337 {
2338 /* Create an image */
2339 im = make_image(rcs);
2340
2341 switch(conf.image_type)
2342 {
2343 #ifdef HAVE_IMAGE_GIF
2344 # ifndef HAVE_IMAGE_PNG
2345 default:
2346 # endif
2347 case IMAGE_GIF:
2348 gdImageGif(im, fp);
2349 break;
2350 #endif
2351 #ifdef HAVE_IMAGE_PNG
2352 default:
2353 case IMAGE_PNG:
2354 gdImagePng(im, fp);
2355 break;
2356 #endif
2357 #ifdef HAVE_IMAGE_JPEG
2358 # if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG)
2359 default:
2360 # endif
2361 case IMAGE_JPEG:
2362 gdImageJpeg(im, fp, conf.image_quality);
2363 break;
2364 #endif
2365 }
2366
2367 gdImageDestroy(im);
2368 }
2369 else
2370 {
2371 /* Create an imagemap */
2372 make_imagemap(rcs, fp);
2373 }
2374
2375 /* Also create imagemap to file if requested */
2376 if(imgmapfile)
2377 {
2378 FILE *ifp = fopen(imgmapfile, "wb");
2379 if(!ifp)
2380 {
2381 perror(imgmapfile);
2382 return 1;
2383 }
2384 make_imagemap(rcs, ifp);
2385 fclose(ifp);
2386 }
2387
2388 if(outfile)
2389 fclose(fp);
2390
2391 return 0;
2392 }

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0