/[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.25 - (show annotations)
Thu Jul 18 15:26:34 2002 UTC (15 years, 5 months ago) by bertho
Branch: MAIN
Changes since 1.24: +345 -90 lines
File MIME type: text/plain
First changes for left_right drawing of the tree.
Still missing proper branch_dupbox, upside_down and imagemap support.
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;
1146 int rx;
1147 int x2;
1148 int i;
1149 int ty;
1150
1151 if(conf.left_right)
1152 {
1153 lx = r->cx;
1154 rx = r->cx + r->w;
1155 ty = r->y - r->h/2;
1156 x2 = r->cx + r->w/2;
1157 }
1158 else
1159 {
1160 lx = r->cx - r->w/2;
1161 rx = lx + r->w;
1162 ty = r->y;
1163 x2 = r->cx;
1164 }
1165 draw_rbox(im, lx, ty, rx, ty+r->h, 0, &conf.rev_color, &conf.rev_bgcolor);
1166 ty += conf.rev_tspace;
1167 draw_string(im, r->rev->rev, &conf.rev_font, x2, ty, ALIGN_HC, &conf.rev_color);
1168 ty += get_sheight(r->rev->rev, &conf.rev_font);
1169 draw_stringnl(im, r->revtext, &conf.rev_text_font, x2, ty, ALIGN_HC, &conf.rev_text_color);
1170 ty += get_sheight(r->revtext, &conf.rev_text_font);
1171 for(i = 0; i < r->ntags; i++)
1172 {
1173 draw_string(im, r->tags[i]->tag, &conf.tag_font, x2, ty, ALIGN_HC, &conf.tag_color);
1174 ty += get_sheight(r->tags[i]->tag, &conf.tag_font) + conf.rev_separator;
1175 }
1176 }
1177
1178 static void draw_branch_box(gdImagePtr im, branch_t *b, int ypos)
1179 {
1180 int lx;
1181 int rx;
1182 int i;
1183 int yy;
1184 int x2;
1185
1186 if(conf.left_right)
1187 {
1188 lx = b->cx;
1189 rx = lx + b->w;
1190 x2 = b->cx + b->w/2;
1191 }
1192 else
1193 {
1194 lx = b->cx - b->w/2;
1195 rx = lx + b->w;
1196 x2 = b->cx;
1197 }
1198 draw_rbox(im, lx, ypos, rx, ypos+b->h, 5, &conf.branch_color, &conf.branch_bgcolor);
1199 yy = conf.branch_tspace;
1200 draw_string(im, b->branch->branch, &conf.branch_font, x2, ypos+yy, ALIGN_HC, &conf.branch_color);
1201 yy += get_sheight(b->branch->branch, &conf.branch_font);
1202 for(i = 0; i < b->ntags; i++)
1203 {
1204 draw_string(im, b->tags[i]->tag, &conf.branch_tag_font, x2, ypos+yy, ALIGN_HC, &conf.branch_tag_color);
1205 yy += get_sheight(b->tags[i]->tag, &conf.branch_font);
1206 }
1207 }
1208
1209 static void draw_branch(gdImagePtr im, branch_t *b)
1210 {
1211 int yy, xx;
1212 int i;
1213 int line[4];
1214 int l;
1215 int sign;
1216
1217 line[0] = conf.rev_color.id;
1218 line[1] = gdTransparent;
1219 line[1] = gdTransparent;
1220 line[3] = conf.rev_color.id;
1221
1222 draw_branch_box(im, b, conf.left_right ? b->y - b->h/2 : b->y);
1223
1224 if(conf.left_right)
1225 {
1226 if(conf.upside_down)
1227 {
1228 xx = b->cx;
1229 for(i = 0; i < b->nrevs; i++)
1230 {
1231 revision_t *r = b->revs[i];
1232 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1233 gdImageLine(im, xx, r->y, r->cx+r->w, r->y, gdStyled);
1234 for(sign = l = 1; l < conf.thick_lines; l++)
1235 {
1236 int pp = (l+1)/2*sign;
1237 gdImageLine(im, xx, r->y+pp, r->cx+r->w, r->y+pp, gdStyled);
1238 sign *= -1;
1239 }
1240 draw_rev(im, r);
1241 xx = r->cx;
1242 }
1243 if(conf.branch_dupbox)
1244 {
1245 i = b->cx - b->tw + b->w;
1246 gdImageLine(im, xx, b->y, i, b->y, conf.rev_color.id);
1247 for(sign = l = 1; l < conf.thick_lines; l++)
1248 {
1249 int pp = (l+1)/2*sign;
1250 gdImageLine(im, xx, b->y+pp, i, b->y+pp, conf.rev_color.id);
1251 sign *= -1;
1252 }
1253 draw_branch_box(im, b, i);
1254 }
1255 }
1256 else
1257 {
1258 xx = b->cx + b->w;
1259 for(i = 0; i < b->nrevs; i++)
1260 {
1261 revision_t *r = b->revs[i];
1262 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1263 gdImageLine(im, xx, r->y, r->cx, r->y, gdStyled);
1264 for(sign = l = 1; l < conf.thick_lines; l++)
1265 {
1266 int pp = (l+1)/2*sign;
1267 gdImageLine(im, xx, r->y+pp, r->cx, r->y+pp, gdStyled);
1268 sign *= -1;
1269 }
1270 draw_rev(im, r);
1271 xx = r->cx + r->w;
1272 }
1273 if(conf.branch_dupbox)
1274 {
1275 i = b->cx + b->tw - b->w;
1276 gdImageLine(im, xx, b->y, i, b->y, conf.rev_color.id);
1277 for(sign = l = 1; l < conf.thick_lines; l++)
1278 {
1279 int pp = (l+1)/2*sign;
1280 gdImageLine(im, xx, b->y+pp, i, b->y+pp, conf.rev_color.id);
1281 sign *= -1;
1282 }
1283 draw_branch_box(im, b, i);
1284 }
1285 }
1286 }
1287 else
1288 {
1289 if(conf.upside_down)
1290 {
1291 yy = b->y;
1292 for(i = 0; i < b->nrevs; i++)
1293 {
1294 revision_t *r = b->revs[i];
1295 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1296 gdImageLine(im, r->cx, yy, r->cx, r->y+r->h, gdStyled);
1297 for(sign = l = 1; l < conf.thick_lines; l++)
1298 {
1299 int pp = (l+1)/2*sign;
1300 gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y+r->h, gdStyled);
1301 sign *= -1;
1302 }
1303 draw_rev(im, r);
1304 yy = r->y;
1305 }
1306 if(conf.branch_dupbox)
1307 {
1308 i = b->y - b->th + b->h;
1309 gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1310 for(sign = l = 1; l < conf.thick_lines; l++)
1311 {
1312 int pp = (l+1)/2*sign;
1313 gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, conf.rev_color.id);
1314 sign *= -1;
1315 }
1316 draw_branch_box(im, b, i);
1317 }
1318 }
1319 else
1320 {
1321 yy = b->y + b->h;
1322 for(i = 0; i < b->nrevs; i++)
1323 {
1324 revision_t *r = b->revs[i];
1325 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1326 gdImageLine(im, r->cx, yy, r->cx, r->y, gdStyled);
1327 for(sign = l = 1; l < conf.thick_lines; l++)
1328 {
1329 int pp = (l+1)/2*sign;
1330 gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y, gdStyled);
1331 sign *= -1;
1332 }
1333 draw_rev(im, r);
1334 yy = r->y + r->h;
1335 }
1336 if(conf.branch_dupbox)
1337 {
1338 i = b->y + b->th - b->h;
1339 gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1340 for(sign = l = 1; l < conf.thick_lines; l++)
1341 {
1342 int pp = (l+1)/2*sign;
1343 gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, conf.rev_color.id);
1344 sign *= -1;
1345 }
1346 draw_branch_box(im, b, i);
1347 }
1348 }
1349 }
1350 }
1351
1352 static void draw_connector(gdImagePtr im, branch_t *b)
1353 {
1354 int l;
1355 int sign;
1356 revision_t *r = b->branchpoint;
1357 int x1 = r->cx + r->w/2 + 2;
1358 int y1 = r->y + r->h/2;
1359 int x2 = b->cx;
1360 int y2 = b->y;
1361
1362 if(conf.left_right)
1363 {
1364 x2 = r->cx + r->w/2;
1365 y2 = r->y + r->h/2 + 2;
1366 x1 = b->cx;
1367 y1 = b->y;
1368 if(conf.upside_down)
1369 x1 += b->w;
1370 }
1371 else
1372 {
1373 x1 = r->cx + r->w/2 + 2;
1374 y1 = r->y + r->h/2;
1375 x2 = b->cx;
1376 y2 = b->y;
1377 if(conf.upside_down)
1378 y2 += b->h;
1379 }
1380 gdImageLine(im, x1, y1, x2, y1, conf.branch_color.id);
1381 gdImageLine(im, x2, y1, x2, y2, conf.branch_color.id);
1382 for(sign = l = 1; l < conf.thick_lines; l++)
1383 {
1384 int pp = (l+1)/2*sign;
1385 gdImageLine(im, x1, y1+pp, x2-pp, y1+pp, conf.branch_color.id);
1386 gdImageLine(im, x2+pp, y1-pp, x2+pp, y2, conf.branch_color.id);
1387 sign *= -1;
1388 }
1389 }
1390
1391 static void alloc_color(gdImagePtr im, color_t *c)
1392 {
1393 c->id = gdImageColorAllocate(im, c->r, c->g, c->b);
1394 }
1395
1396 gdImagePtr make_image(rcsfile_t *rcs)
1397 {
1398 gdImagePtr im;
1399 int i;
1400 char *cptr;
1401
1402 cptr = expand_string(conf.title, rcs, NULL, NULL, NULL, NULL);
1403 i = get_swidth(cptr, &conf.title_font);
1404 if(rcs->tw+conf.margin_left+conf.margin_right > i)
1405 i = rcs->tw+conf.margin_left+conf.margin_right;
1406 im = gdImageCreate(i, rcs->th+conf.margin_top+conf.margin_bottom);
1407 alloc_color(im, &conf.color_bg);
1408 alloc_color(im, &conf.tag_color);
1409 alloc_color(im, &conf.rev_color);
1410 alloc_color(im, &conf.rev_bgcolor);
1411 alloc_color(im, &conf.rev_text_color);
1412 alloc_color(im, &conf.branch_color);
1413 alloc_color(im, &conf.branch_tag_color);
1414 alloc_color(im, &conf.branch_bgcolor);
1415 alloc_color(im, &conf.title_color);
1416 alloc_color(im, &black_color);
1417 alloc_color(im, &white_color);
1418
1419 if(conf.transparent_bg)
1420 gdImageColorTransparent(im, conf.color_bg.id);
1421
1422 for(i = 0; i < rcs->nbranches; i++)
1423 draw_branch(im, rcs->branches[i]);
1424 for(i = 0; i < rcs->nbranches; i++)
1425 {
1426 if(rcs->branches[i]->branchpoint)
1427 draw_connector(im, rcs->branches[i]);
1428 }
1429 draw_stringnl(im, cptr, &conf.title_font, conf.title_x, conf.title_y, conf.title_align, &conf.title_color);
1430 xfree(cptr);
1431
1432 return im;
1433 }
1434
1435 /*
1436 **************************************************************************
1437 * Layout routines
1438 *
1439 * Branch BBox:
1440 * left = center_x - total_width / 2 (cx-tw)/2
1441 * right = center_x + total_width / 2 (cx+tw)/2
1442 * top = y_pos (y)
1443 * bottom = y_pos + total_height (y+th)
1444 *
1445 * Margins of branches:
1446 *
1447 * . .
1448 * . .
1449 * +--------------+
1450 * ^
1451 * | branch_margin .
1452 * v .
1453 * ----------------+ .
1454 * | ^ |
1455 * | | branch_connect |
1456 * | v |
1457 *..-+ +t-----+------+ +------+------+
1458 * | l | | |
1459 * | <--> | branch bbox | <--> | branch bbox |
1460 * | | | r | | |
1461 *..-+ | +------------b+ | +-------------+
1462 * | ^ branch_margin
1463 * | | branch_margin
1464 * | v
1465 * | +-------------+
1466 * | . .
1467 * | . .
1468 * |
1469 * branch_margin
1470 *
1471 * FIXME: There are probable som +/-1 errors in the code...
1472 * (notably shadows are not calculated in the margins)
1473 **************************************************************************
1474 */
1475 static void move_branch(branch_t *b, int x, int y)
1476 {
1477 int i;
1478 b->cx += x;
1479 b->y += y;
1480 for(i = 0; i < b->nrevs; i++)
1481 {
1482 b->revs[i]->cx += x;
1483 b->revs[i]->y += y;
1484 }
1485 }
1486
1487 static void initial_reposition_branch(revision_t *r, int *x, int *w)
1488 {
1489 int i, j;
1490 for(j = 0; j < r->nbranches; j++)
1491 {
1492 branch_t *b = r->branches[j];
1493 *x += *w + conf.rev_minline + b->tw/2 - b->cx;
1494 *w = b->tw/2;
1495 move_branch(b, *x, r->y + r->h/2 + conf.branch_connect);
1496 *x = b->cx;
1497 /* Recurse to move branches of branched revisions */
1498 for(i = b->nrevs-1; i >= 0; i--)
1499 {
1500 initial_reposition_branch(b->revs[i], x, w);
1501 }
1502 }
1503 }
1504
1505 static void initial_reposition_branch_lr(revision_t *r, int *y, int *h)
1506 {
1507 int i, j;
1508 for(j = 0; j < r->nbranches; j++)
1509 {
1510 branch_t *b = r->branches[j];
1511 *y += *h + conf.rev_minline + b->th/2 - b->y;
1512 *h = b->th/2;
1513 move_branch(b, r->cx + r->w/2 + conf.branch_connect, *y);
1514 *y = b->y;
1515 /* Recurse to move branches of branched revisions */
1516 for(i = b->nrevs-1; i >= 0; i--)
1517 {
1518 initial_reposition_branch(b->revs[i], y, h);
1519 }
1520 }
1521 }
1522
1523 static void rect_union(int *x, int *y, int *w, int *h, branch_t *b)
1524 {
1525 int x1 = *x;
1526 int x2 = x1 + *w;
1527 int y1 = *y;
1528 int y2 = y1 + *h;
1529 int xx1;
1530 int xx2;
1531 int yy1;
1532 int yy2;
1533
1534 if(conf.left_right)
1535 {
1536 xx1 = b->cx;
1537 yy1 = b->y - b->th/2;
1538 }
1539 else
1540 {
1541 xx1 = b->cx - b->tw/2;
1542 yy1 = b->y;
1543 }
1544 xx2 = xx1 + b->tw;
1545 yy2 = yy1 + b->th;
1546
1547 x1 = MIN(x1, xx1);
1548 x2 = MAX(x2, xx2);
1549 y1 = MIN(y1, yy1);
1550 y2 = MAX(y2, yy2);
1551 *x = x1;
1552 *y = y1;
1553 *w = x2 - x1;
1554 *h = y2 - y1;
1555 }
1556
1557 static int branch_intersects(int top, int bottom, int left, branch_t *b)
1558 {
1559 int br = b->cx + b->tw/2;
1560 int bt = b->y - conf.branch_connect - conf.branch_margin/2;
1561 int bb = b->y + b->th + conf.branch_margin/2;
1562 return !(bt > bottom || bb < top || br >= left);
1563 }
1564
1565 static int branch_intersects_lr(int left, int right, int top, branch_t *b)
1566 {
1567 int bt = b->y + b->th/2;
1568 int bl = b->cx - conf.branch_connect - conf.branch_margin/2;
1569 int br = b->cx + b->tw + conf.branch_margin/2;
1570 return !(bl > right || br < left || bt >= top);
1571 }
1572
1573 static int kern_branch(rcsfile_t *rcs, branch_t *b)
1574 {
1575 int left = b->cx - b->tw/2;
1576 int top = b->y - conf.branch_connect - conf.branch_margin/2;
1577 int bottom = b->y + b->th + conf.branch_margin/2;
1578 int i;
1579 int xpos = 0;
1580
1581 for(i = 0; i < rcs->nbranches; i++)
1582 {
1583 branch_t *bp = rcs->branches[i];
1584 if(bp == b)
1585 continue;
1586 if(branch_intersects(top, bottom, left, bp))
1587 {
1588 int m = bp->cx + bp->tw/2 + conf.branch_margin;
1589 if(m > xpos)
1590 xpos = m;
1591 }
1592 }
1593 if(xpos && (b->cx - b->tw/2) - xpos > 0)
1594 {
1595 move_branch(b, xpos - (b->cx - b->tw/2), 0);
1596 return 1;
1597 }
1598 return 0;
1599 }
1600
1601 static int kern_branch_lr(rcsfile_t *rcs, branch_t *b)
1602 {
1603 int top = b->y - b->th/2;
1604 int left = b->cx - conf.branch_connect - conf.branch_margin/2;
1605 int right = b->cx + b->tw + conf.branch_margin/2;
1606 int i;
1607 int ypos = 0;
1608
1609 for(i = 0; i < rcs->nbranches; i++)
1610 {
1611 branch_t *bp = rcs->branches[i];
1612 if(bp == b)
1613 continue;
1614 if(branch_intersects_lr(left, right, top, bp))
1615 {
1616 int m = bp->y + bp->th/2 + conf.branch_margin;
1617 if(m > ypos)
1618 ypos = m;
1619 }
1620 }
1621 if(ypos && (b->y - b->th/2) - ypos > 0)
1622 {
1623 move_branch(b, 0, ypos - (b->y - b->th/2));
1624 return 1;
1625 }
1626 return 0;
1627 }
1628
1629 static int kern_tree(rcsfile_t *rcs)
1630 {
1631 int i;
1632 int moved;
1633 int safeguard;
1634 int totalmoved = 0;
1635 for(moved = 1, safeguard = LOOPSAFEGUARD; moved && safeguard; safeguard--)
1636 {
1637 moved = 0;
1638 for(i = 1; i < rcs->nbranches; i++)
1639 {
1640 if(conf.left_right)
1641 moved += kern_branch_lr(rcs, rcs->branches[i]);
1642 else
1643 moved += kern_branch(rcs, rcs->branches[i]);
1644 }
1645 totalmoved += moved;
1646 #ifdef DEBUG
1647 fprintf(stderr, "kern_tree: moved=%d\n", moved);
1648 #endif
1649 }
1650 if(!quiet && !safeguard)
1651 fprintf(stderr, "kern_tree: safeguard terminated possible infinite loop; please report.\n");
1652 return totalmoved;
1653 }
1654
1655 static int index_of_revision(revision_t *r)
1656 {
1657 branch_t *b = r->branch;
1658 int i;
1659 for(i = 0; i < b->nrevs; i++)
1660 {
1661 if(r == b->revs[i])
1662 return i;
1663 }
1664 fprintf(stderr, "index_of_revision: Cannot find revision in branch\n");
1665 return 0;
1666 }
1667
1668 static void branch_bbox(branch_t *br, int *l, int *r, int *t, int *b)
1669 {
1670 if(l) *l = br->cx - br->tw/2;
1671 if(r) *r = br->cx + br->tw/2;
1672 if(t) *t = br->y;
1673 if(b) *b = br->y + br->th + (conf.branch_dupbox ? conf.rev_minline + br->h : 0);
1674 }
1675
1676 static void branch_ext_bbox(branch_t *br, int *l, int *r, int *t, int *b)
1677 {
1678 int extra = conf.branch_margin & 1; /* Correct +/-1 error on div 2 */
1679 branch_bbox(br, l, r, t, b);
1680 if(l) *l -= conf.branch_margin/2;
1681 if(r) *r += conf.branch_margin/2 + extra;
1682 if(t) *t -= conf.branch_connect + conf.branch_margin/2;
1683 if(b) *b += conf.branch_margin/2 + extra;
1684 }
1685
1686 static int branch_distance(branch_t *br1, branch_t *br2)
1687 {
1688 int l1, r1, t1, b1;
1689 int l2, r2, t2, b2;
1690 assert(br1 != NULL);
1691 assert(br2 != NULL);
1692 branch_bbox(br1, &l1, &r1, NULL, NULL);
1693 branch_bbox(br2, &l2, &r2, NULL, NULL);
1694 branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
1695 branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
1696 /* Return:
1697 * - 0 if branches have no horizontal overlap
1698 * - positive if b1 is left of b2
1699 * - negative if b2 is left of b1
1700 */
1701 if((t1 > t2 && t1 < b2) || (b1 > t2 && b1 < b2))
1702 return l1 < l2 ? l2 - r1 : -(l1 - r2);
1703 else
1704 return 0;
1705 }
1706
1707 static int space_needed(branch_t *br1, branch_t *br2)
1708 {
1709 int t1, b1;
1710 int t2, b2;
1711 assert(br1 != NULL);
1712 assert(br2 != NULL);
1713 assert(br1->cx < br2->cx); /* br1 must be left of br2 */
1714 branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
1715 branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
1716 /* Return:
1717 * - positive if top br1 is located lower than br2
1718 * - negatve is top br2 is located lower than br1
1719 */
1720 if(t1 > t2)
1721 return -(t1 - b2);
1722 else
1723 return t2 - b1;
1724 }
1725
1726 static void move_yr_branch(branch_t *b, int dy)
1727 {
1728 int i, j;
1729 #ifdef DEBUG
1730 /* fprintf(stderr, "move_yr_branch: b=%s, dy=%d\n", b->branch->branch, dy);*/
1731 #endif
1732 b->y += dy;
1733 for(i = 0; i < b->nrevs; i++)
1734 {
1735 b->revs[i]->y += dy;
1736 for(j = 0; j < b->revs[i]->nbranches; j++)
1737 {
1738 #ifdef DEBUG
1739 /* fprintf(stderr, ".");*/
1740 #endif
1741 move_yr_branch(b->revs[i]->branches[j], dy);
1742 }
1743 }
1744 }
1745
1746 static void move_trunk(revision_t *r, int dy)
1747 {
1748 int i, j;
1749 branch_t *b = r->branch;
1750 b->th += dy;
1751 for(i = index_of_revision(r); i < b->nrevs; i++)
1752 {
1753 #ifdef DEBUG
1754 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);
1755 #endif
1756 b->revs[i]->y += dy;
1757 for(j = 0; j < b->revs[i]->nbranches; j++)
1758 {
1759 move_yr_branch(b->revs[i]->branches[j], dy);
1760 }
1761 }
1762 }
1763
1764 static int space_below(rcsfile_t *rcs, revision_t *r)
1765 {
1766 int i, j;
1767 int bl, br, bb;
1768 int space = INT_MAX;
1769 branch_t *b = r->branch;
1770 branch_t *minb = NULL;
1771
1772 branch_ext_bbox(b, &bl, &br, NULL, &bb);
1773 for(i = 0; i < rcs->nbranches; i++)
1774 {
1775 int tbl, tbr, tbt;
1776 branch_t *tb = rcs->branches[i];
1777 branch_ext_bbox(tb, &tbl, &tbr, &tbt, NULL);
1778 if(tb == b)
1779 continue;
1780 if(tbt > bb) /* Must be below our branch */
1781 {
1782 if(tb->branchpoint) /* Take account for the horiz connector */
1783 tbl = tb->branchpoint->cx + tb->branchpoint->branch->tw/2;
1784 if((bl >= tbl && bl <= tbr) || (br <= tbr && br >= tbl))
1785 {
1786 int s = tbt - bb - conf.branch_connect;
1787 if(s < space)
1788 {
1789 space = s;
1790 minb = tb;
1791 }
1792 }
1793 }
1794 }
1795 if(b->branchpoint)
1796 {
1797 for(i = index_of_revision(r); i < b->nrevs; i++)
1798 {
1799 for(j = 0; j < b->revs[i]->nbranches; j++)
1800 {
1801 int s = space_below(rcs, b->revs[i]->branches[j]->revs[0]);
1802 if(s < space)
1803 space = s;
1804 }
1805 }
1806 }
1807 #ifdef DEBUG
1808 fprintf(stderr, "space_below: from %s have %d to %s\n", b->branch->branch, space, minb ? minb->branch->branch : "<recursed>");
1809 #endif
1810 return space;
1811 }
1812
1813 static int space_available(rcsfile_t *rcs, branch_t *colbr, branch_t *tagbr, int *nl, revision_t **bpcommon)
1814 {
1815 int i;
1816 int space = 0;
1817 int nlinks = 0;
1818 revision_t *r;
1819 branch_t *b;
1820 branch_t *ancestor;
1821 revision_t *branchpoint;
1822
1823 if(!tagbr->branchpoint || !colbr->branchpoint)
1824 {
1825 if(!quiet)
1826 fprintf(stderr, "space_available: Trying to stretch the top?\n");
1827 return 0;
1828 }
1829
1830 r = colbr->branchpoint;
1831 b = r->branch;
1832 branchpoint = tagbr->branchpoint;
1833 ancestor = branchpoint->branch;
1834 assert(b != NULL);
1835 assert(ancestor != NULL);
1836
1837 while(1)
1838 {
1839 int s;
1840 int rtag = b == ancestor ? index_of_revision(branchpoint)+1 : 0;
1841 for(i = index_of_revision(r); i >= rtag; i--)
1842 {
1843 if(i > 0)
1844 s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
1845 else
1846 s = b->revs[i]->y - (b->y + b->h);
1847 if(s < conf.rev_maxline)
1848 {
1849 space += conf.rev_maxline - s;
1850 nlinks++;
1851 }
1852 }
1853 s = space_below(rcs, r);
1854 if(s < space)
1855 space = s;
1856 #ifdef DEBUG
1857 if(space < 0)
1858 return -1;
1859 #endif
1860 if(b == ancestor)
1861 break;
1862 r = b->branchpoint;
1863 if(!r)
1864 {
1865 /* Not a common ancestor */
1866 r = colbr->branchpoint;
1867 b = r->branch;
1868 branchpoint = ancestor->branchpoint;
1869 if(!branchpoint)
1870 {
1871 if(!quiet)
1872 fprintf(stderr, "space_available: No common ancestor?\n");
1873 return 0;
1874 }
1875 ancestor = branchpoint->branch;
1876 assert(ancestor != NULL);
1877 nlinks = 0;
1878 space = 0;
1879 continue; /* Restart with a new ancestor */
1880 }
1881 b = r->branch;
1882 }
1883 if(nl)
1884 *nl = nlinks; /* Return the number of links that can stretch */
1885 if(bpcommon)
1886 *bpcommon = branchpoint; /* Return the ancestral branchpoint on the common branch */
1887 return space;
1888 }
1889
1890 static int stretch_branches(rcsfile_t *rcs, branch_t *br1, branch_t *br2, int totalstretch)
1891 {
1892 revision_t *r;
1893 revision_t *bpcommon = NULL;
1894 branch_t *ancestor = NULL;
1895 branch_t *b;
1896 int i;
1897 int space;
1898 int nlinks;
1899 int dy;
1900 int rest;
1901
1902 space = space_available(rcs, br1, br2, &nlinks, &bpcommon);
1903 if(bpcommon)
1904 ancestor = bpcommon->branch;
1905
1906 #ifdef DEBUG
1907 if(space == -1)
1908 return 0;
1909 fprintf(stderr, "stretch_branches: space available %d over %d links common %s\n", space, nlinks, ancestor->branch->branch);
1910 #endif
1911 if(space < totalstretch)
1912 return 0;
1913
1914 dy = totalstretch / nlinks;
1915 rest = totalstretch - dy * nlinks;
1916
1917 r = br1->branchpoint;
1918 b = r->branch;
1919 while(1)
1920 {
1921 int rtag = b == ancestor ? index_of_revision(bpcommon)+1 : 0;
1922 for(i = index_of_revision(r); i >= rtag; i--)
1923 {
1924 int s, q;
1925 if(i > 0)
1926 s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
1927 else
1928 s = b->revs[i]->y - (b->y + b->h);
1929 q = conf.rev_maxline - s;
1930 if(q > 0)
1931 {
1932 int d = rest ? rest/nlinks+1 : 0;
1933 if(q >= dy+d)
1934 {
1935 move_trunk(b->revs[i], dy+d);
1936 }
1937 else
1938 {
1939 move_trunk(b->revs[i], q);
1940 rest += dy+d - q;
1941 }
1942 rest -= d;
1943 nlinks--;
1944 }
1945 }
1946 if(b == ancestor)
1947 break;
1948 r = b->branchpoint;
1949 assert(r != NULL); /* else 'space_available' wouldn't have returned positively */
1950 b = r->branch;
1951 }
1952 return 1;
1953 }
1954
1955 static branch_t *find_collision_branch(rcsfile_t *rcs, branch_t *b)
1956 {
1957 int i;
1958 int dist = INT_MAX;
1959 branch_t *col = NULL;
1960
1961 for(i = 0; i < rcs->nbranches; i++)
1962 {
1963 int t = branch_distance(rcs->branches[i], b);
1964 if(t > 0 && t < dist)
1965 {
1966 dist = t;
1967 col = rcs->branches[i];
1968 }
1969 }
1970 return col;
1971 }
1972
1973 void auto_stretch(rcsfile_t *rcs)
1974 {
1975 int i;
1976 int safeguard;
1977
1978 for(i = 0, safeguard = LOOPSAFEGUARD; i < rcs->nbranches && safeguard; i++)
1979 {
1980 int bl, pr;
1981 branch_t *b = rcs->branches[i];
1982 if(!b->branchpoint)
1983 continue;
1984 branch_bbox(b, &bl, NULL, NULL, NULL);
1985 branch_bbox(b->branchpoint->branch, NULL, &pr, NULL, NULL);
1986 if(bl - conf.branch_margin - pr > 0)
1987 {
1988 branch_t *col;
1989 int spaceneeded;
1990 /* There is a potential to move branch b further left.
1991 * All branches obstructing this one from moving further
1992 * left must be originating from revisions below
1993 * b->branchpoint until a common ancester.
1994 * So, we search all branches for a branch that lies left
1995 * of b and is closest to b. This is then the collission
1996 * branch that needs to be moved.
1997 */
1998 col = find_collision_branch(rcs, b);
1999 if(!col)
2000 continue;
2001 spaceneeded = space_needed(col, b);
2002 if(spaceneeded < 0)
2003 continue;
2004 #ifdef DEBUG
2005 fprintf(stderr, "auto_stretch: %s collides %s need %d\n", b->branch->branch, col->branch->branch, spaceneeded);
2006 #endif
2007 /* Trace the collision branch back to find the common ancester
2008 * of both col and b. All revisions encountered while traversing
2009 * backwards must be stretched, including all revisions on the
2010 * common ancester from where the branches sprout.
2011 */
2012 if(stretch_branches(rcs, col, b, spaceneeded))
2013 {
2014 if(kern_tree(rcs))
2015 {
2016 /* Restart the process because movement can
2017 * cause more movement.
2018 */
2019 i = 0 - 1; /* -1 for the i++ of the loop */
2020 safeguard--; /* Prevent infinite loop, just in case */
2021 }
2022 /*return;*/
2023 }
2024 }
2025 }
2026 if(!quiet && !safeguard)
2027 fprintf(stderr, "auto_stretch: safeguard terminated possible infinite loop; please report.\n");
2028 }
2029
2030 void make_layout(rcsfile_t *rcs)
2031 {
2032 int i, j;
2033 int x, y;
2034 int w, h;
2035 int w2;
2036
2037 /* Remove all unwanted revisions */
2038 if(conf.strip_untagged)
2039 {
2040 int fr = conf.strip_first_rev ? 0 : 1;
2041 for(i = 0; i < rcs->nbranches; i++)
2042 {
2043 branch_t *bp = rcs->branches[i];
2044 for(j = fr; j < bp->nrevs-1; j++)
2045 {
2046 if(!bp->revs[j]->ntags && !bp->revs[j]->nbranches)
2047 {
2048 memmove(&bp->revs[j], &bp->revs[j+1], (bp->nrevs-j-1) * sizeof(bp->revs[0]));
2049 bp->nrevs--;
2050 bp->revs[j]->stripped = 1;
2051 j--;
2052 }
2053 }
2054 }
2055 }
2056
2057 /* Calculate the box-sizes of the revisions */
2058 for(i = 0; i < rcs->nsrev; i++)
2059 {
2060 revision_t *rp;
2061 int w;
2062 int h;
2063 rp = rcs->srev[i];
2064 rp->revtext = expand_string(conf.rev_text, rcs, rp, rp->rev, NULL, rp->ntags ? rp->tags[0] : NULL);
2065 w = get_swidth(rp->revtext, &conf.rev_text_font);
2066 j = get_swidth(rp->rev->rev, &conf.rev_font);
2067 if(j > w)
2068 w = j;
2069 h = get_sheight(rp->revtext, &conf.rev_text_font) + get_sheight(rp->rev->rev, &conf.rev_font);
2070 for(j = 0; j < rp->ntags; j++)
2071 {
2072 int ww = get_swidth(rp->tags[j]->tag, &conf.tag_font);
2073 if(ww > w) w = ww;
2074 h += get_sheight(rp->tags[j]->tag, &conf.tag_font) + conf.rev_separator;
2075 }
2076 rp->w = w + conf.rev_lspace + conf.rev_rspace;
2077 rp->h = h + conf.rev_tspace + conf.rev_bspace;
2078 }
2079
2080 /* Calculate the box-sizes of the branches */
2081 for(i = 0; i < rcs->nbranches; i++)
2082 {
2083 branch_t *bp = rcs->branches[i];
2084 int w;
2085 int h;
2086 w = get_swidth(bp->branch->branch, &conf.branch_font);
2087 h = get_sheight(bp->branch->branch, &conf.branch_font);
2088 for(j = 0; j < bp->ntags; j++)
2089 {
2090 int ww = get_swidth(bp->tags[j]->tag, &conf.branch_tag_font);
2091 if(ww > w) w = ww;
2092 h += get_sheight(bp->tags[j]->tag, &conf.branch_tag_font);
2093 }
2094 w += conf.branch_lspace + conf.branch_rspace;
2095 h += conf.branch_tspace + conf.branch_bspace;
2096 bp->w = w;
2097 bp->h = h;
2098 if(conf.left_right)
2099 {
2100 for(j = 0; j < bp->nrevs; j++)
2101 {
2102 if(bp->revs[j]->h > h)
2103 h = bp->revs[j]->h;
2104 w += bp->revs[j]->w + conf.rev_minline;
2105 }
2106 if(conf.branch_dupbox)
2107 w += bp->w + conf.rev_minline;
2108 }
2109 else
2110 {
2111 for(j = 0; j < bp->nrevs; j++)
2112 {
2113 if(bp->revs[j]->w > w)
2114 w = bp->revs[j]->w;
2115 h += bp->revs[j]->h + conf.rev_minline;
2116 }
2117 if(conf.branch_dupbox)
2118 h += bp->h + conf.rev_minline;
2119 }
2120 bp->th = h;
2121 bp->tw = w;
2122 }
2123
2124 /* Calculate the relative positions of revs in a branch */
2125 if(conf.left_right)
2126 {
2127 for(i = 0; i < rcs->nbranches; i++)
2128 {
2129 branch_t *b = rcs->branches[i];
2130 y = b->th/2;
2131 x = b->w;
2132 b->y = y;
2133 b->cx = 0;
2134 for(j = 0; j < b->nrevs; j++)
2135 {
2136 x += conf.rev_minline;
2137 b->revs[j]->y = y;
2138 b->revs[j]->cx = x;
2139 x += b->revs[j]->w;
2140 }
2141 }
2142 }
2143 else
2144 {
2145 for(i = 0; i < rcs->nbranches; i++)
2146 {
2147 branch_t *b = rcs->branches[i];
2148 x = b->tw/2;
2149 y = b->h;
2150 b->cx = x;
2151 b->y = 0;
2152 for(j = 0; j < b->nrevs; j++)
2153 {
2154 y += conf.rev_minline;
2155 b->revs[j]->cx = x;
2156 b->revs[j]->y = y;
2157 y += b->revs[j]->h;
2158 }
2159 }
2160 }
2161
2162 /* Initially reposition the branches from bottom to top progressively right */
2163 if(conf.left_right)
2164 {
2165 x = rcs->branches[0]->y;
2166 w2 = rcs->branches[0]->th / 2;
2167 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
2168 {
2169 initial_reposition_branch_lr(rcs->branches[0]->revs[i], &x, &w2);
2170 }
2171 }
2172 else
2173 {
2174 x = rcs->branches[0]->cx;
2175 w2 = rcs->branches[0]->tw / 2;
2176 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
2177 {
2178 initial_reposition_branch(rcs->branches[0]->revs[i], &x, &w2);
2179 }
2180 }
2181
2182 /* Initially move branches left if there is room */
2183 kern_tree(rcs);
2184
2185 /* Try to kern the branches more by expanding the inter-revision spacing */
2186 if(conf.auto_stretch && !conf.left_right)
2187 auto_stretch(rcs);
2188
2189 /* Move everything w.r.t. the top-left margin */
2190 for(i = 0; i < rcs->nbranches; i++)
2191 move_branch(rcs->branches[i], conf.margin_left, conf.margin_top);
2192
2193 /* Calculate overall image size */
2194 if(conf.left_right)
2195 {
2196 x = rcs->branches[0]->cx;
2197 y = rcs->branches[0]->y - rcs->branches[0]->th/2;
2198 }
2199 else
2200 {
2201 x = rcs->branches[0]->cx - rcs->branches[0]->tw/2;
2202 y = rcs->branches[0]->y;
2203 }
2204 w = rcs->branches[0]->tw;
2205 h = rcs->branches[0]->th;
2206 for(i = 1; i < rcs->nbranches; i++)
2207 rect_union(&x, &y, &w, &h, rcs->branches[i]);
2208 rcs->tw = w;
2209 rcs->th = h;
2210
2211 /* Flip the entire tree */
2212 if(conf.upside_down)
2213 {
2214 if(conf.left_right)
2215 {
2216 x += rcs->tw;
2217 for(i = 0; i < rcs->nbranches; i++)
2218 {
2219 branch_t *b = rcs->branches[i];
2220 for(j = 0; j < b->nrevs; j++)
2221 {
2222 revision_t *r = b->revs[j];
2223 r->cx = x - r->cx - r->w + conf.margin_top;
2224 }
2225 b->cx = x - b->cx - b->w + conf.margin_top;
2226 }
2227 }
2228 else
2229 {
2230 y += rcs->th;
2231 for(i = 0; i < rcs->nbranches; i++)
2232 {
2233 branch_t *b = rcs->branches[i];
2234 for(j = 0; j < b->nrevs; j++)
2235 {
2236 revision_t *r = b->revs[j];
2237 r->y = y - r->y - r->h + conf.margin_top;
2238 }
2239 b->y = y - b->y - b->h + conf.margin_top;
2240 }
2241 }
2242 }
2243 }
2244
2245 /*
2246 **************************************************************************
2247 * Imagemap functions
2248 **************************************************************************
2249 */
2250 void make_imagemap(rcsfile_t *rcs, FILE *fp)
2251 {
2252 int i, j;
2253 fprintf(fp, "<map name=\"%s\">\n", conf.map_name);
2254 for(i = 0; i < rcs->nbranches; i++)
2255 {
2256 branch_t *b = rcs->branches[i];
2257 tag_t *tag = b->ntags ? b->tags[0] : NULL;
2258 char *bhref = expand_string(conf.map_branch_href, rcs, NULL, b->branch, NULL, tag);
2259 char *balt = expand_string(conf.map_branch_alt, rcs, NULL, b->branch, NULL, tag);
2260 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2261 bhref,
2262 b->cx - b->w/2, b->y, b->cx + b->w/2, b->y + b->h,
2263 balt);
2264 for(j = 0; j < b->nrevs; j++)
2265 {
2266 revision_t *r = b->revs[j];
2267 revision_t* r1;
2268 int xoff;
2269 int x1;
2270 int x2;
2271 int y1;
2272 int y2;
2273 char *href;
2274 char *alt;
2275
2276 tag = r->ntags ? r->tags[0] : NULL;
2277 href = expand_string(conf.map_rev_href, rcs, r, r->rev, NULL, tag);
2278 alt = expand_string(conf.map_rev_alt, rcs, r, r->rev, NULL, tag);
2279 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2280 href,
2281 r->cx - r->w/2, r->y, r->cx + r->w/2, r->y + r->h,
2282 alt);
2283 xfree(href);
2284 xfree(alt);
2285 if(j > 0 || b->branchpoint)
2286 {
2287 if(j > 0)
2288 {
2289 r1 = b->revs[j-1];
2290 xoff = MIN(r->w, r1->w)/4;
2291 y1 = conf.upside_down ? r1->y : r1->y + r1->h;
2292 }
2293 else
2294 {
2295 r1 = b->branchpoint;
2296 xoff = MIN(r->w, b->w)/4;
2297 y1 = conf.upside_down ? b->y : b->y + b->h;
2298 }
2299 x1 = r->cx - xoff;
2300 x2 = r->cx + xoff;
2301 y2 = conf.upside_down ? r->y + r->h : r->y;
2302 href = expand_string(conf.map_diff_href, rcs, r, r->rev, r1->rev, tag);
2303 alt = expand_string(conf.map_diff_alt, rcs, r, r->rev, r1->rev, tag);
2304 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2305 href,
2306 x1, (conf.upside_down ? y2 : y1) + 1, x2, (conf.upside_down ? y1 : y2) - 1,
2307 alt);
2308 xfree(href);
2309 xfree(alt);
2310 }
2311 }
2312 if(conf.branch_dupbox)
2313 {
2314 int y;
2315 if(conf.upside_down)
2316 y = b->y + b->h - b->th;
2317 else
2318 y = b->y - b->h + b->th;
2319 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2320 bhref,
2321 b->cx - b->w/2, y, b->cx + b->w/2, y + b->h,
2322 balt);
2323 }
2324 xfree(bhref);
2325 xfree(balt);
2326 }
2327 fprintf(fp, "</map>\n");
2328 }
2329
2330 /*
2331 **************************************************************************
2332 * Program entry
2333 **************************************************************************
2334 */
2335 static const char usage_str[] =
2336 "Usage: cvsgraph [options] <file>\n"
2337 " -b Add a branch box at both sides of the trunk (config value is negated)\n"
2338 " -c <file> Read alternative config from <file>\n"
2339 " -d <level> Enable debug mode at <level>\n"
2340 " -h This message\n"
2341 " -i Generate an imagemap instead of image\n"
2342 " -I <file> Also write the imagemap to <file>\n"
2343 " -k Auto stretch the tree (config value is negated)\n"
2344 " -M <name> Use <name> as imagemap name\n"
2345 " -m <mod> Use <mod> as cvs module\n"
2346 " -o <file> Output to <file>\n"
2347 " -O <opt=val> Set option opt to value val\n"
2348 " -q Be quiet (i.e. no warnings)\n"
2349 " -r <path> Use <path> as cvsroot path\n"
2350 " -s Strip untagged revisions (config value is negated)\n"
2351 " -S Also strip the first revision (config value is negated)\n"
2352 " -u Upside down image (mirror vertically; config value is negated)\n"
2353 " -V Print version and exit\n"
2354 " -[0-9] <txt> Use <txt> for expansion\n"
2355 ;
2356
2357 #define VERSION_STR "1.2.1"
2358 #define NOTICE_STR "Copyright (c) 2001,2002 B.Stultiens"
2359
2360 static void append_slash(char **path)
2361 {
2362 int l;
2363 assert(path != NULL);
2364 assert(*path != NULL);
2365 l = strlen(*path);
2366 if(!l || (*path)[l-1] == '/')
2367 return;
2368 *path = xrealloc(*path, l+2);
2369 strcat(*path, "/");
2370 }
2371
2372 int main(int argc, char *argv[])
2373 {
2374 extern int rcs_flex_debug;
2375 extern int rcsdebug;
2376 int optc;
2377 char *confpath = NULL;
2378 char *outfile = NULL;
2379 char *cvsroot = NULL;
2380 char *cvsmodule = NULL;
2381 int imagemap = 0;
2382 int upsidedown = 0;
2383 int bdupbox = 0;
2384 int stripuntag = 0;
2385 int stripfirst = 0;
2386 int autostretch = 0;
2387 char *imgmapname = NULL;
2388 char *imgmapfile = NULL;
2389 int lose = 0;
2390 FILE *fp;
2391 char *rcsfilename;
2392 rcsfile_t *rcs;
2393 gdImagePtr im;
2394
2395 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)
2396 {
2397 switch(optc)
2398 {
2399 case 'b':
2400 bdupbox = 1;
2401 break;
2402 case 'c':
2403 confpath = xstrdup(optarg);
2404 break;
2405 case 'd':
2406 debuglevel = strtol(optarg, NULL, 0);
2407 break;
2408 case 'I':
2409 imgmapfile = xstrdup(optarg);
2410 break;
2411 case 'i':
2412 imagemap = 1;
2413 break;
2414 case 'k':
2415 autostretch = 1;
2416 break;
2417 case 'M':
2418 imgmapname = xstrdup(optarg);
2419 break;
2420 case 'm':
2421 cvsmodule = xstrdup(optarg);
2422 break;
2423 case 'O':
2424 stack_option(optarg);
2425 break;
2426 case 'o':
2427 outfile = xstrdup(optarg);
2428 break;
2429 case 'q':
2430 quiet = 1;
2431 break;
2432 case 'r':
2433 cvsroot = xstrdup(optarg);
2434 break;
2435 case 'S':
2436 stripfirst = 1;
2437 break;
2438 case 's':
2439 stripuntag = 1;
2440 break;
2441 case 'u':
2442 upsidedown = 1;
2443 break;
2444 case 'V':
2445 fprintf(stdout, "cvsgraph v%s, %s\n", VERSION_STR, NOTICE_STR);
2446 return 0;
2447 case 'h':
2448 fprintf(stdout, "%s", usage_str);
2449 return 0;
2450 default:
2451 if(isdigit(optc))
2452 {
2453 conf.expand[optc-'0'] = xstrdup(optarg);
2454 }
2455 else
2456 lose++;
2457 }
2458 }
2459
2460 if(lose)
2461 {
2462 fprintf(stderr, "%s", usage_str);
2463 return 1;
2464 }
2465
2466 if(debuglevel)
2467 {
2468 setvbuf(stdout, NULL, 0, _IONBF);
2469 setvbuf(stderr, NULL, 0, _IONBF);
2470 }
2471 rcs_flex_debug = (debuglevel & DEBUG_RCS_LEX) != 0;
2472 rcsdebug = (debuglevel & DEBUG_RCS_YACC) != 0;
2473
2474 /* Set defaults */
2475 conf.tag_font.gdfont = gdFontTiny;
2476 conf.rev_font.gdfont = gdFontTiny;
2477 conf.branch_font.gdfont = gdFontTiny;
2478 conf.branch_tag_font.gdfont = gdFontTiny;
2479 conf.title_font.gdfont = gdFontTiny;
2480 conf.rev_text_font.gdfont = gdFontTiny;
2481
2482 conf.anti_alias = 1;
2483 conf.thick_lines = 1;
2484
2485 conf.cvsroot = xstrdup("");
2486 conf.cvsmodule = xstrdup("");
2487 conf.date_format = xstrdup("%d-%b-%Y %H:%M:%S");
2488 conf.title = xstrdup("");
2489 conf.map_name = xstrdup("CvsGraphImageMap");
2490 conf.map_branch_href = xstrdup("href=\"unset: conf.map_branch_href\"");
2491 conf.map_branch_alt = xstrdup("alt=\"%B\"");
2492 conf.map_rev_href = xstrdup("href=\"unset: conf.map_rev_href\"");
2493 conf.map_rev_alt = xstrdup("alt=\"%R\"");
2494 conf.map_diff_href = xstrdup("href=\"unset: conf.map_diff_href\"");
2495 conf.map_diff_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
2496 conf.rev_text = xstrdup("%d");
2497
2498 conf.color_bg = white_color;
2499 conf.branch_bgcolor = white_color;
2500 conf.branch_color = black_color;
2501 conf.branch_tag_color = black_color;
2502 conf.rev_color = black_color;
2503 conf.rev_bgcolor = white_color;
2504 conf.tag_color = black_color;
2505 conf.title_color = black_color;
2506 conf.rev_text_color = black_color;
2507
2508 conf.image_quality = 100;
2509 conf.rev_maxline = -1; /* Checked later to set to default */
2510
2511 read_config(confpath);
2512
2513 if(conf.rev_maxline == -1) conf.rev_maxline = 5 * conf.rev_minline;
2514
2515 /* Set overrides */
2516 if(cvsroot) conf.cvsroot = cvsroot;
2517 if(cvsmodule) conf.cvsmodule = cvsmodule;
2518 if(imgmapname) conf.map_name = imgmapname;
2519 if(upsidedown) conf.upside_down = !conf.upside_down;
2520 if(bdupbox) conf.branch_dupbox = !conf.branch_dupbox;
2521 if(stripuntag) conf.strip_untagged = !conf.strip_untagged;
2522 if(stripfirst) conf.strip_first_rev = !conf.strip_first_rev;
2523 if(autostretch) conf.auto_stretch = !conf.auto_stretch;
2524
2525 if(conf.rev_minline >= conf.rev_maxline)
2526 {
2527 if(conf.auto_stretch && !quiet)
2528 fprintf(stderr, "Auto stretch is only possible if rev_minline < rev_maxline\n");
2529 conf.auto_stretch = 0;
2530 }
2531
2532 if(conf.thick_lines < 1)
2533 conf.thick_lines = 1;
2534 if(conf.thick_lines > 11)
2535 conf.thick_lines = 11;
2536
2537 append_slash(&conf.cvsroot);
2538 append_slash(&conf.cvsmodule);
2539
2540 if(optind >= argc)
2541 {
2542 #ifdef __WIN32__
2543 /* Bad hack for DOS/Windows */
2544 if(setmode(fileno(stdin), O_BINARY) == -1)
2545 {
2546 perror("Set binary mode for stdin");
2547 return 1;
2548 }
2549 #endif
2550 rcsfilename = NULL;
2551 }
2552 else
2553 rcsfilename = argv[optind];
2554
2555 rcs = get_rcsfile(conf.cvsroot, conf.cvsmodule, rcsfilename);
2556 if(!rcs)
2557 return 1;
2558
2559 if(debuglevel & DEBUG_RCS_FILE)
2560 dump_rcsfile(rcs);
2561
2562 if(!reorganise_branches(rcs))
2563 return 1;
2564
2565 if(!assign_tags(rcs))
2566 return 1;
2567
2568 if(outfile)
2569 {
2570 if((fp = fopen(outfile, "wb")) == NULL)
2571 {
2572 perror(outfile);
2573 return 1;
2574 }
2575 }
2576 else
2577 {
2578 fp = stdout;
2579 #ifdef __WIN32__
2580 /* Bad hack for DOS/Windows */
2581 if(setmode(fileno(fp), O_BINARY) == -1)
2582 {
2583 perror("Set binary mode for stdout");
2584 return 1;
2585 }
2586 #endif
2587 }
2588
2589 make_layout(rcs);
2590
2591 if(!imagemap)
2592 {
2593 /* Create an image */
2594 im = make_image(rcs);
2595
2596 switch(conf.image_type)
2597 {
2598 #ifdef HAVE_IMAGE_GIF
2599 # ifndef HAVE_IMAGE_PNG
2600 default:
2601 # endif
2602 case IMAGE_GIF:
2603 gdImageGif(im, fp);
2604 break;
2605 #endif
2606 #ifdef HAVE_IMAGE_PNG
2607 default:
2608 case IMAGE_PNG:
2609 gdImagePng(im, fp);
2610 break;
2611 #endif
2612 #ifdef HAVE_IMAGE_JPEG
2613 # if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG)
2614 default:
2615 # endif
2616 case IMAGE_JPEG:
2617 gdImageJpeg(im, fp, conf.image_quality);
2618 break;
2619 #endif
2620 }
2621
2622 gdImageDestroy(im);
2623 }
2624 else
2625 {
2626 /* Create an imagemap */
2627 make_imagemap(rcs, fp);
2628 }
2629
2630 /* Also create imagemap to file if requested */
2631 if(imgmapfile)
2632 {
2633 FILE *ifp = fopen(imgmapfile, "wb");
2634 if(!ifp)
2635 {
2636 perror(imgmapfile);
2637 return 1;
2638 }
2639 make_imagemap(rcs, ifp);
2640 fclose(ifp);
2641 }
2642
2643 if(outfile)
2644 fclose(fp);
2645
2646 return 0;
2647 }

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0