/[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.17 - (show annotations)
Wed Dec 19 18:10:58 2001 UTC (15 years, 11 months ago) by bertho
Branch: MAIN
Changes since 1.16: +33 -22 lines
File MIME type: text/plain
- Updated the docu a bit
- Renamed the branch_mirror to branch_dupbox which is more appropriate
- strip_untagged displays now draw dashed connector lines to indicate a jump
1 /*
2 * CvsGraph graphical representation generator of brances and revisions
3 * of a file in cvs/rcs.
4 *
5 * Copyright (C) 2001 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
38 #ifdef HAVE_GETOPT_H
39 # include <getopt.h>
40 #endif
41
42 #include <gd.h>
43 #include <gdfontt.h>
44
45 #include "cvsgraph.h"
46 #include "utils.h"
47 #include "readconf.h"
48 #include "rcs.h"
49
50 #if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG) && !defined(HAVE_IMAGE_JPEG)
51 # error No image output format available. Check libgd
52 #endif
53
54
55 /*#define DEBUG 1*/
56
57 #define CONFFILENAME "cvsgraph.conf"
58
59 #ifndef ETCDIR
60 # define ETCDIR "/usr/local/etc"
61 #endif
62
63 #ifndef MAX
64 # define MAX(a,b) ((a) > (b) ? (a) : (b))
65 #endif
66
67 #ifndef MIN
68 # define MIN(a,b) ((a) < (b) ? (a) : (b))
69 #endif
70
71 #define ALIGN_HL 0x00
72 #define ALIGN_HC 0x01
73 #define ALIGN_HR 0x02
74 #define ALIGN_HX 0x0f
75 #define ALIGN_VT 0x00
76 #define ALIGN_VC 0x10
77 #define ALIGN_VB 0x20
78 #define ALIGN_VX 0xf0
79
80 /*
81 **************************************************************************
82 * Globals
83 **************************************************************************
84 */
85
86 config_t conf;
87 int debuglevel;
88 color_t white_color = {255, 255, 255, 0};
89 color_t black_color = {0, 0, 0, 0};
90
91
92 /*
93 **************************************************************************
94 * Debug routines
95 **************************************************************************
96 */
97 void dump_rev(char *p, rev_t *r)
98 {
99 printf("%s", p);
100 if(r)
101 printf("'%s', '%s', %d\n", r->rev, r->branch, r->isbranch);
102 else
103 printf("<null>\n");
104 }
105
106 void dump_id(char *p, char *d)
107 {
108 printf("%s", p);
109 if(d)
110 printf("'%s'\n", d);
111 else
112 printf("<null>\n");
113 }
114
115 void dump_idrev(char *p, idrev_t *t)
116 {
117 printf("%s", p);
118 if(t)
119 {
120 printf("'%s' -> ", t->id);
121 dump_rev("", t->rev);
122 }
123 else
124 printf("<null>\n");
125 }
126
127 void dump_tag(char *p, tag_t *t)
128 {
129 printf("%s", p);
130 if(t)
131 {
132 printf("'%s' -> ", t->tag);
133 dump_rev("", t->rev);
134 }
135 else
136 printf("<null>\n");
137 }
138
139 void dump_delta(char *p, delta_t *d)
140 {
141 int i;
142 printf("%sdelta.rev : ", p);
143 dump_rev("", d->rev);
144 printf("%sdelta.date : %s\n", p, d->date);
145 printf("%sdelta.author: %s\n", p, d->author);
146 printf("%sdelta.state : %s\n", p, d->state);
147 for(i = 0; d->branches && i < d->branches->nrevs; i++)
148 {
149 printf("%sdelta.branch: ", p);
150 dump_rev("", d->branches->revs[i]);
151 }
152 printf("%sdelta.next : ", p);
153 dump_rev("", d->next);
154 printf("\n");
155 }
156
157 void dump_dtext(char *p, dtext_t *d)
158 {
159 printf("%sdtext.rev : ", p);
160 dump_rev("", d->rev);
161 printf("%sdtext.log : %d bytes\n", p, d->log ? strlen(d->log) : -1);
162 printf("%sdtext.text : %d bytes\n", p, d->text ? strlen(d->text) : -1);
163 printf("\n");
164 }
165
166 void dump_rcsfile(rcsfile_t *rcs)
167 {
168 int i;
169 printf("root : '%s'\n", rcs->root);
170 printf("module : '%s'\n", rcs->module);
171 printf("file : '%s'\n", rcs->file);
172 dump_rev("head : ", rcs->head);
173 dump_rev("branch : ", rcs->branch);
174 printf("access :\n");
175 for(i = 0; rcs->access && i < rcs->access->nids; i++)
176 dump_id("\t", rcs->access->ids[i]);
177 printf("tags :\n");
178 for(i = 0; rcs->tags && i < rcs->tags->ntags; i++)
179 dump_tag("\t", rcs->tags->tags[i]);
180 printf("locks :%s\n", rcs->strict ? " (strict)" : "");
181 for(i = 0; rcs->locks && i < rcs->locks->nidrevs; i++)
182 dump_idrev("\t", rcs->locks->idrevs[i]);
183 printf("comment: '%s'\n", rcs->comment);
184 printf("expand : '%s'\n", rcs->expand ? rcs->expand : "(default -kv)");
185 printf("deltas :\n");
186 for(i = 0; rcs->deltas && i < rcs->deltas->ndeltas; i++)
187 dump_delta("\t", rcs->deltas->deltas[i]);
188 printf("desc : '%s'\n", rcs->desc);
189 printf("dtexts :\n");
190 for(i = 0; rcs->dtexts && i < rcs->dtexts->ndtexts; i++)
191 dump_dtext("\t", rcs->dtexts->dtexts[i]);
192
193 fflush(stdout);
194 }
195
196 /*
197 **************************************************************************
198 * Read the rcs file
199 **************************************************************************
200 */
201 rcsfile_t *get_rcsfile(const char *cvsroot, const char *module, const char *file)
202 {
203 char *cmd = NULL;
204 int rv;
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 line_number = 1;
215 rv = rcsparse();
216 fclose(rcsin);
217 if(rv)
218 return NULL;
219 xfree(cmd);
220 input_file = NULL;
221 rcsfile->root = xstrdup(cvsroot);
222 rcsfile->module = xstrdup(module);
223 rcsfile->file = xstrdup(file);
224 return rcsfile;
225 }
226
227 /*
228 **************************************************************************
229 * Sort and find helpers
230 **************************************************************************
231 */
232 int count_dots(const char *s)
233 {
234 int i;
235 for(i = 0; *s; s++)
236 {
237 if(*s == '.')
238 i++;
239 }
240 return i;
241 }
242
243 int compare_rev(int bcmp, const rev_t *r1, const rev_t *r2)
244 {
245 int d1, d2;
246 char *c1, *c2;
247 char *v1, *v2;
248 char *s1, *s2;
249 int retval = 0;
250 assert(r1 != NULL);
251 assert(r2 != NULL);
252 if(bcmp)
253 {
254 assert(r1->branch != NULL);
255 assert(r2->branch != NULL);
256 c1 = r1->branch;
257 c2 = r2->branch;
258 }
259 else
260 {
261 assert(r1->rev != NULL);
262 assert(r2->rev != NULL);
263 c1 = r1->rev;
264 c2 = r2->rev;
265 }
266
267 d1 = count_dots(c1);
268 d2 = count_dots(c2);
269 if(d1 != d2)
270 {
271 return d1 - d2;
272 }
273
274 s1 = v1 = xstrdup(c1);
275 s2 = v2 = xstrdup(c2);
276 while(1)
277 {
278 char *vc1 = strchr(s1, '.');
279 char *vc2 = strchr(s2, '.');
280 if(vc1 && vc2)
281 *vc1 = *vc2 = '\0';
282 if(*s1 && *s2)
283 {
284 d1 = atoi(s1);
285 d2 = atoi(s2);
286 if(d1 != d2)
287 {
288 retval = d1 - d2;
289 break;
290 }
291 }
292 if(!vc1 || !vc2)
293 break;
294 s1 = vc1 + 1;
295 s2 = vc2 + 1;
296 }
297 xfree(v1);
298 xfree(v2);
299 return retval;
300 }
301
302 /*
303 **************************************************************************
304 * Reorganise the rcsfile for the branches
305 *
306 * Basically, we have a list of deltas (i.e. administrative info on
307 * revisions) and a list of delta text (the actual logs and diffs).
308 * The deltas are linked through the 'next' and the 'branches' fields
309 * which describe the tree-structure of revisions.
310 * The reorganisation means that we put each delta and corresponding
311 * delta text in a revision structure and assign it to a specific
312 * branch. This is required because we want to be able to calculate
313 * the bounding boxes of each branch. The revisions expand vertically
314 * and the branches expand horizontally.
315 * The reorganisation is performed in these steps:
316 * 1 - sort deltas and delta text on revision number for quick lookup
317 * 2 - start at the denoted head revision:
318 * * create a branch structure and add this revision
319 * * for each 'branches' in the delta do:
320 * - walk all 'branches' of the delta and recursively goto 2
321 * with the denoted branch delta as new head
322 * - backlink the newly create sub-branch to the head revision
323 * so that we can draw them recursively
324 * * set head to the 'next' field and goto 2 until no next is
325 * available
326 * 3 - update the administration
327 **************************************************************************
328 */
329 int sort_delta(const void *d1, const void *d2)
330 {
331 return compare_rev(0, (*(delta_t **)d1)->rev, (*(delta_t **)d2)->rev);
332 }
333
334 int search_delta(const void *r, const void *d)
335 {
336 return compare_rev(0, (rev_t *)r, (*(delta_t **)d)->rev);
337 }
338
339 delta_t *find_delta(delta_t **dl, int n, rev_t *r)
340 {
341 delta_t **d;
342 d = bsearch(r, dl, n, sizeof(*dl), search_delta);
343 if(!d)
344 return NULL;
345 return *d;
346 }
347
348 int sort_dtext(const void *d1, const void *d2)
349 {
350 return compare_rev(0, (*(dtext_t **)d1)->rev, (*(dtext_t **)d2)->rev);
351 }
352
353 int search_dtext(const void *r, const void *d)
354 {
355 return compare_rev(0, (rev_t *)r, (*(dtext_t **)d)->rev);
356 }
357
358 dtext_t *find_dtext(dtext_t **dl, int n, rev_t *r)
359 {
360 dtext_t **d;
361 d = bsearch(r, dl, n, sizeof(*dl), search_dtext);
362 if(!d)
363 return NULL;
364 return *d;
365 }
366
367 rev_t *dup_rev(const rev_t *r)
368 {
369 rev_t *t = xmalloc(sizeof(*t));
370 t->rev = xstrdup(r->rev);
371 t->branch = xstrdup(r->branch);
372 t->isbranch = r->isbranch;
373 return t;
374 }
375
376 branch_t *new_branch(delta_t *d, dtext_t *t)
377 {
378 branch_t *b = xmalloc(sizeof(*b));
379 revision_t *r = xmalloc(sizeof(*r));
380 r->delta = d;
381 r->dtext = t;
382 r->rev = d->rev;
383 r->branch = b;
384 b->branch = dup_rev(d->rev);
385 b->branch->isbranch = 1;
386 b->nrevs = 1;
387 b->revs = xmalloc(sizeof(b->revs[0]));
388 b->revs[0] = r;
389 return b;
390 }
391
392 revision_t *add_to_branch(branch_t *b, delta_t *d, dtext_t *t)
393 {
394 revision_t *r = xmalloc(sizeof(*r));
395 r->delta = d;
396 r->dtext = t;
397 r->rev = d->rev;
398 r->branch = b;
399 b->revs = xrealloc(b->revs, (b->nrevs+1) * sizeof(b->revs[0]));
400 b->revs[b->nrevs] = r;
401 b->nrevs++;
402 return r;
403 }
404
405 void build_branch(branch_t ***bl, int *nbl, delta_t **sdl, int nsdl, dtext_t **sdt, int nsdt, delta_t *head)
406 {
407 branch_t *b;
408 dtext_t *text;
409 revision_t *currev;
410
411 assert(head != NULL);
412
413 if(head->flag)
414 {
415 fprintf(stderr, "Circular reference on '%s' in branchpoint\n", head->rev->rev);
416 return;
417 }
418 head->flag++;
419 text = find_dtext(sdt, nsdt, head->rev);
420
421 /* Create a new branch for this head */
422 b = new_branch(head, text);
423 *bl = xrealloc(*bl, (*nbl+1)*sizeof((*bl)[0]));
424 (*bl)[*nbl] = b;
425 (*nbl)++;
426 currev = b->revs[0];
427 while(1)
428 {
429 /* Process all sub-branches */
430 if(head->branches)
431 {
432 int i;
433 for(i = 0; i < head->branches->nrevs; i++)
434 {
435 delta_t *d = find_delta(sdl, nsdl, head->branches->revs[i]);
436 int btag = *nbl;
437 if(!d)
438 continue;
439 build_branch(bl, nbl, sdl, nsdl, sdt, nsdt, d);
440
441 /* Set the new branch's origin */
442 (*bl)[btag]->branchpoint = currev;
443
444 /* Add branch to this revision */
445 currev->branches = xrealloc(currev->branches, (currev->nbranches+1)*sizeof(currev->branches[0]));
446 currev->branches[currev->nbranches] = (*bl)[btag];
447 currev->nbranches++;
448 }
449 }
450
451 /* Walk through the next list */
452 if(!head->next)
453 return;
454
455 head = find_delta(sdl, nsdl, head->next);
456 if(!head)
457 {
458 fprintf(stderr, "Next revision (%s) not found in deltalist\n", head->next->rev);
459 return;
460 }
461 if(head->flag)
462 {
463 fprintf(stderr, "Circular reference on '%s'\n", head->rev->rev);
464 return;
465 }
466 head->flag++;
467 text = find_dtext(sdt, nsdt, head->rev);
468 currev = add_to_branch(b, head, text);
469 }
470 }
471
472 int reorganise_branches(rcsfile_t *rcs)
473 {
474 delta_t **sdelta;
475 int nsdelta;
476 dtext_t **sdtext;
477 int nsdtext;
478 delta_t *head;
479 branch_t **bl;
480 int nbl;
481 int i;
482
483 assert(rcs->deltas != NULL);
484 assert(rcs->head != NULL);
485
486 /* Make a new list for quick lookup */
487 nsdelta = rcs->deltas->ndeltas;
488 sdelta = xmalloc(nsdelta * sizeof(sdelta[0]));
489 memcpy(sdelta, rcs->deltas->deltas, nsdelta * sizeof(sdelta[0]));
490 qsort(sdelta, nsdelta, sizeof(sdelta[0]), sort_delta);
491
492 /* Do the same for the delta text */
493 nsdtext = rcs->dtexts->ndtexts;
494 sdtext = xmalloc(nsdtext * sizeof(sdtext[0]));
495 memcpy(sdtext, rcs->dtexts->dtexts, nsdtext * sizeof(sdtext[0]));
496 qsort(sdtext, nsdtext, sizeof(sdtext[0]), sort_dtext);
497
498 /* Start from the head of the trunk */
499 head = find_delta(sdelta, nsdelta, rcs->head);
500 if(!head)
501 {
502 fprintf(stderr, "Head revision (%s) not found in deltalist\n", rcs->head->rev);
503 return 0;
504 }
505 bl = NULL;
506 nbl = 0;
507 build_branch(&bl, &nbl, sdelta, nsdelta, sdtext, nsdtext, head);
508
509 /* Reverse the head branch */
510 for(i = 0; i < bl[0]->nrevs/2; i++)
511 {
512 revision_t *r;
513 r = bl[0]->revs[i];
514 bl[0]->revs[i] = bl[0]->revs[bl[0]->nrevs-i-1];
515 bl[0]->revs[bl[0]->nrevs-i-1] = r;
516 }
517
518 /* Update the branch-number of the head because it was reversed */
519 xfree(bl[0]->branch->branch);
520 bl[0]->branch->branch = xstrdup(bl[0]->revs[0]->rev->branch);
521
522 /* Keep the admin */
523 rcs->branches = bl;
524 rcs->nbranches = nbl;
525 rcs->sdelta = sdelta;
526 rcs->nsdelta = nsdelta;
527 rcs->sdtext = sdtext;
528 rcs->nsdtext = nsdtext;
529 rcs->active = bl[0];
530 return 1;
531 }
532
533 /*
534 **************************************************************************
535 * Assign the symbolic tags to the revisions and branches
536 *
537 * The tags point to revision numbers somewhere in the tree structure
538 * of branches and revisions. First we make a sorted list of all
539 * revisions and then we assign each tag to the proper revision.
540 **************************************************************************
541 */
542 int sort_revision(const void *r1, const void *r2)
543 {
544 return compare_rev(0, (*(revision_t **)r1)->delta->rev, (*(revision_t **)r2)->delta->rev);
545 }
546
547 int search_revision(const void *t, const void *r)
548 {
549 return compare_rev(0, (rev_t *)t, (*(revision_t **)r)->delta->rev);
550 }
551
552 int sort_branch(const void *b1, const void *b2)
553 {
554 return compare_rev(1, (*(branch_t **)b1)->branch, (*(branch_t **)b2)->branch);
555 }
556
557 int search_branch(const void *t, const void *b)
558 {
559 return compare_rev(1, (rev_t *)t, (*(branch_t **)b)->branch);
560 }
561
562 char *previous_rev(const char *c)
563 {
564 int dots = count_dots(c);
565 char *cptr;
566 char *r;
567 if(!dots)
568 {
569 fprintf(stderr, "FIXME: previous_rev(\"%s\"): Cannot determine parent branch revision\n", c);
570 return xstrdup("1.0"); /* FIXME: don't know what the parent is */
571 }
572 if(dots & 1)
573 {
574 /* Is is a revision we want the parent of */
575 r = xstrdup(c);
576 cptr = strrchr(r, '.');
577 assert(cptr != NULL);
578 if(dots == 1)
579 {
580 fprintf(stderr, "FIXME: previous_rev(\"%s\"): Going beyond top-level?\n", c);
581 /* FIXME: What is the parent of 1.1? */
582 cptr[1] = '\0';
583 strcat(r, "0");
584 return r;
585 }
586 /* Here we have a "x.x[.x.x]+" case */
587 *cptr = '\0';
588 cptr = strrchr(r, '.');
589 assert(cptr != NULL);
590 *cptr = '\0';
591 return r;
592 }
593 /* It is a branch we want the parent of */
594 r = xstrdup(c);
595 cptr = strrchr(r, '.');
596 assert(cptr != NULL);
597 *cptr = '\0';
598 return r;
599 }
600
601 int assign_tags(rcsfile_t *rcs)
602 {
603 int i;
604 int nr;
605
606 for(i = nr = 0; i < rcs->nbranches; i++)
607 nr += rcs->branches[i]->nrevs;
608
609 rcs->srev = xmalloc(nr * sizeof(rcs->srev[0]));
610 rcs->nsrev = nr;
611 for(i = nr = 0; i < rcs->nbranches; i++)
612 {
613 memcpy(&rcs->srev[nr], rcs->branches[i]->revs, rcs->branches[i]->nrevs * sizeof(rcs->branches[i]->revs[0]));
614 nr += rcs->branches[i]->nrevs;
615 }
616
617 qsort(rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), sort_revision);
618 qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
619
620 if(!rcs->branch)
621 {
622 /* The main trunk is the active trunk */
623 rcs->tags->tags = xrealloc(rcs->tags->tags, (rcs->tags->ntags+1)*sizeof(rcs->tags->tags[0]));
624 rcs->tags->tags[rcs->tags->ntags] = xmalloc(sizeof(tag_t));
625 rcs->tags->tags[rcs->tags->ntags]->tag = xstrdup("MAIN");
626 rcs->tags->tags[rcs->tags->ntags]->rev = xmalloc(sizeof(rev_t));
627 rcs->tags->tags[rcs->tags->ntags]->rev->rev = xstrdup(rcs->active->branch->rev);
628 rcs->tags->tags[rcs->tags->ntags]->rev->branch = xstrdup(rcs->active->branch->branch);
629 rcs->tags->tags[rcs->tags->ntags]->rev->isbranch = 1;
630 rcs->tags->ntags++;
631 }
632
633 /* We should have at least two tags (HEAD and MAIN) */
634 assert(rcs->tags != 0);
635
636 for(i = 0; i < rcs->tags->ntags; i++)
637 {
638 tag_t *t = rcs->tags->tags[i];
639 if(t->rev->isbranch)
640 {
641 branch_t **b;
642 add_btag:
643 b = bsearch(t->rev, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
644 if(!b)
645 {
646 rev_t rev;
647 revision_t **r;
648 /* This happens for the magic branch numbers if there are
649 * no commits within the new branch yet. So, we add the
650 * branch and try to continue.
651 */
652 rev.rev = previous_rev(t->rev->branch);
653 rev.branch = NULL;
654 rev.isbranch = 0;
655 r = bsearch(&rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
656 xfree(rev.rev);
657 if(!r)
658 {
659 if(!quiet)
660 fprintf(stderr, "No branch found for tag '%s:%s'\n", t->tag, t->rev->branch);
661 }
662 else
663 {
664 rcs->branches = xrealloc(rcs->branches, (rcs->nbranches+1)*sizeof(rcs->branches[0]));
665 rcs->branches[rcs->nbranches] = xmalloc(sizeof(branch_t));
666 rcs->branches[rcs->nbranches]->branch = dup_rev(t->rev);
667 rcs->branches[rcs->nbranches]->branchpoint = *r;
668 (*r)->branches = xrealloc((*r)->branches, ((*r)->nbranches+1)*sizeof((*r)->branches[0]));
669 (*r)->branches[(*r)->nbranches] = rcs->branches[rcs->nbranches];
670 (*r)->nbranches++;
671 rcs->nbranches++;
672 /* Resort the branches */
673 qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
674 goto add_btag;
675 }
676 }
677 else
678 {
679 branch_t *bb = *b;
680 bb->tags = xrealloc(bb->tags, (bb->ntags+1)*sizeof(bb->tags[0]));
681 bb->tags[bb->ntags] = t;
682 bb->ntags++;
683 }
684 }
685 else
686 {
687 revision_t **r = bsearch(t->rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
688 if(!r)
689 {
690 if(!quiet)
691 fprintf(stderr, "No revision found for tag '%s:%s'\n", t->tag, t->rev->rev);
692 }
693 else
694 {
695 revision_t *rr = *r;
696 rr->tags = xrealloc(rr->tags, (rr->ntags+1)*sizeof(rr->tags[0]));
697 rr->tags[rr->ntags] = t;
698 rr->ntags++;
699 }
700 }
701 }
702
703 /* We need to reset the first in the list of branches to the
704 * active branch to ensure the drawing of all
705 */
706 if(rcs->active != rcs->branches[0])
707 {
708 branch_t **b = bsearch(rcs->active->branch, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
709 branch_t *t;
710 assert(b != NULL);
711 t = *b;
712 *b = rcs->branches[0];
713 rcs->branches[0] = t;
714 }
715 return 1;
716 }
717
718 /*
719 **************************************************************************
720 * String expansion
721 **************************************************************************
722 */
723 static char *_string;
724 static int _nstring;
725 static int _nastring;
726
727 void add_string_str(const char *s)
728 {
729 int l = strlen(s) + 1;
730 if(_nstring + l > _nastring)
731 {
732 _nastring += 128;
733 _string = xrealloc(_string, _nastring * sizeof(_string[0]));
734 }
735 memcpy(_string+_nstring, s, l);
736 _nstring += l-1;
737 }
738
739 void add_string_ch(int ch)
740 {
741 char buf[2];
742 buf[0] = ch;
743 buf[1] = '\0';
744 add_string_str(buf);
745 }
746
747 void add_string_date(const char *d)
748 {
749 struct tm tm, *tmp;
750 int n;
751 time_t t;
752 char *buf;
753 int nbuf;
754
755 memset(&tm, 0, sizeof(tm));
756 n = sscanf(d, "%d.%d.%d.%d.%d.%d",
757 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
758 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
759 tm.tm_mon--;
760 if(tm.tm_year > 1900)
761 tm.tm_year -= 1900;
762 t = mktime(&tm);
763 if(n != 6 || t == (time_t)(-1))
764 {
765 add_string_str("<invalid date>");
766 return;
767 }
768
769 tmp = localtime(&t);
770 nbuf = strlen(conf.date_format) * 16; /* Should be enough to hold all types of expansions */
771 buf = xmalloc(nbuf);
772 strftime(buf, nbuf, conf.date_format, tmp);
773 add_string_str(buf);
774 xfree(buf);
775 }
776
777 char *expand_string(const char *s, rcsfile_t *rcs, revision_t *r, rev_t *rev, rev_t *prev, tag_t *tag)
778 {
779 char nb[32];
780 char nr[32];
781 char *base;
782
783 if(!s)
784 return xstrdup("");
785
786 _nstring = 0;
787 if(_string)
788 _string[0] = '\0';
789
790 sprintf(nb, "%d", rcs->nbranches);
791 sprintf(nr, "%d", rcs->nsrev);
792 for(; *s; s++)
793 {
794 if(*s == '%')
795 {
796 switch(*++s)
797 {
798 case 'c':
799 case 'C':
800 add_string_str(conf.cvsroot);
801 if(*s == 'C' && conf.cvsroot[0] && conf.cvsroot[strlen(conf.cvsroot)-1] == '/')
802 {
803 /* Strip the trailing '/' */
804 _nstring--;
805 _string[_nstring] = '\0';
806 }
807 break;
808 case 'f':
809 case 'F':
810 base = strrchr(rcs->file, '/');
811 if(!base)
812 add_string_str(rcs->file);
813 else
814 add_string_str(base+1);
815 if(*s == 'F' && _string[_nstring-1] == 'v' && _string[_nstring-2] == ',')
816 {
817 _nstring -= 2;
818 _string[_nstring] = '\0';
819 }
820 break;
821 case 'p':
822 base = strrchr(rcs->file, '/');
823 if(base)
824 {
825 char *c = xstrdup(rcs->file);
826 base = strrchr(c, '/');
827 assert(base != NULL);
828 base[1] = '\0';
829 add_string_str(c);
830 xfree(c);
831 }
832 /*
833 * We should not add anything here because we can encounter
834 * a completely empty path, in which case we do not want
835 * to add any slash. This prevents an inadvertent root redirect.
836 *
837 * else
838 * add_string_str("/");
839 */
840 break;
841 case 'm':
842 case 'M':
843 add_string_str(conf.cvsmodule);
844 if(*s == 'M' && conf.cvsmodule[0] && conf.cvsmodule[strlen(conf.cvsmodule)-1] == '/')
845 {
846 /* Strip the trailing '/' */
847 _nstring--;
848 _string[_nstring] = '\0';
849 }
850 break;
851 case 'r': add_string_str(nr); break;
852 case 'b': add_string_str(nb); break;
853 case '%': add_string_ch('%'); break;
854 case '0': if(conf.expand[0]) add_string_str(conf.expand[0]); break;
855 case '1': if(conf.expand[1]) add_string_str(conf.expand[1]); break;
856 case '2': if(conf.expand[2]) add_string_str(conf.expand[2]); break;
857 case '3': if(conf.expand[3]) add_string_str(conf.expand[3]); break;
858 case '4': if(conf.expand[4]) add_string_str(conf.expand[4]); break;
859 case '5': if(conf.expand[5]) add_string_str(conf.expand[5]); break;
860 case '6': if(conf.expand[6]) add_string_str(conf.expand[6]); break;
861 case '7': if(conf.expand[7]) add_string_str(conf.expand[7]); break;
862 case '8': if(conf.expand[8]) add_string_str(conf.expand[8]); break;
863 case '9': if(conf.expand[9]) add_string_str(conf.expand[9]); break;
864 case 'R': if(rev && rev->rev) add_string_str(rev->rev); break;
865 case 'P': if(prev && prev->rev) add_string_str(prev->rev); break;
866 case 'B': if(rev && rev->branch) add_string_str(rev->branch); break;
867 case 't': if(tag && tag->tag) add_string_str(tag->tag); break;
868 case 'd': if(r && r->delta && r->delta->date) add_string_date(r->delta->date); break;
869 case 's': if(r && r->delta && r->delta->state) add_string_str(r->delta->state); break;
870 case 'a': if(r && r->delta && r->delta->author) add_string_str(r->delta->author); break;
871 default:
872 add_string_ch('%');
873 add_string_ch(*s);
874 break;
875 }
876 }
877 else
878 add_string_ch(*s);
879 }
880 return xstrdup(_string);
881 }
882
883 /*
884 **************************************************************************
885 * Drawing routines
886 **************************************************************************
887 */
888 int get_swidth(const char *s, font_t *f)
889 {
890 int n;
891 int m;
892 if(!s || !*s)
893 return 0;
894 for(n = m = 0; *s; n++, s++)
895 {
896 if(*s == '\n')
897 {
898 if(n > m)
899 m = n;
900 n = 0;
901 }
902 }
903 if(n > m)
904 m = n;
905 return m * (*f)->w;
906 }
907
908 int get_sheight(const char *s, font_t *f)
909 {
910 int nl;
911 if(!s || !*s)
912 return 0;
913 for(nl = 1; *s; s++)
914 {
915 if(*s == '\n' && s[1])
916 nl++;
917 }
918 return nl * (*f)->h;
919 }
920
921 void draw_rbox(gdImagePtr im, int x1, int y1, int x2, int y2, int r, color_t *color, color_t *bgcolor)
922 {
923 int r2 = 2*r;
924 gdImageLine(im, x1+r, y1, x2-r, y1, color->id);
925 gdImageLine(im, x1+r, y2, x2-r, y2, color->id);
926 gdImageLine(im, x1, y1+r, x1, y2-r, color->id);
927 gdImageLine(im, x2, y1+r, x2, y2-r, color->id);
928 if(conf.box_shadow)
929 {
930 gdImageLine(im, x1+r+1, y2+1, x2-r, y2+1, black_color.id);
931 gdImageLine(im, x2+1, y1+r+1, x2+1, y2-r, black_color.id);
932 }
933 if(r)
934 {
935 gdImageArc(im, x1+r, y1+r, r2, r2, 180, 270, color->id);
936 gdImageArc(im, x2-r, y1+r, r2, r2, 270, 360, color->id);
937 gdImageArc(im, x1+r, y2-r, r2, r2, 90, 180, color->id);
938 gdImageArc(im, x2-r, y2-r, r2, r2, 0, 90, color->id);
939 if(conf.box_shadow)
940 {
941 /* FIXME: Pixelization is not correct here */
942 gdImageArc(im, x2-r+1, y2-r+1, r2, r2, 0, 90, black_color.id);
943 }
944 }
945 gdImageFillToBorder(im, (x1+x2)/2, (y1+y2)/2, color->id, bgcolor->id);
946 }
947
948 void draw_string(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
949 {
950 int xx, yy;
951 switch(align & ALIGN_HX)
952 {
953 default:
954 case ALIGN_HL: xx = 0; break;
955 case ALIGN_HC: xx = -get_swidth(s, f)/2; break;
956 case ALIGN_HR: xx = -get_swidth(s, f); break;
957 }
958 switch(align & ALIGN_VX)
959 {
960 default:
961 case ALIGN_VT: yy = 0; break;
962 case ALIGN_VC: yy = -get_sheight(s, f)/2; break;
963 case ALIGN_VB: yy = -get_sheight(s, f); break;
964 }
965 gdImageString(im, *f, x+xx+1, y+yy, s, c->id);
966 }
967
968 void draw_stringnl(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
969 {
970 char *t;
971 char *d;
972 d = s = xstrdup(s);
973 do
974 {
975 t = strchr(s, '\n');
976 if(t)
977 *t = '\0';
978 draw_string(im, s, f, x, y, align, c);
979 y += get_sheight(s, f);
980 s = t+1;
981 } while(t);
982 xfree(d);
983 }
984
985 void draw_rev(gdImagePtr im, revision_t *r)
986 {
987 int lx = r->cx - r->w/2;
988 int rx = lx + r->w;
989 int i;
990 int ty = r->y;
991 draw_rbox(im, lx, ty, rx, ty+r->h, 0, &conf.rev_color, &conf.rev_bgcolor);
992 ty += conf.rev_tspace;
993 draw_string(im, r->rev->rev, &conf.rev_font, r->cx, ty, ALIGN_HC, &conf.rev_color);
994 ty += get_sheight(r->rev->rev, &conf.rev_font);
995 draw_stringnl(im, r->revtext, &conf.rev_text_font, r->cx, ty, ALIGN_HC, &conf.rev_text_color);
996 ty += get_sheight(r->revtext, &conf.rev_text_font);
997 for(i = 0; i < r->ntags; i++)
998 {
999 draw_string(im, r->tags[i]->tag, &conf.tag_font, r->cx, ty, ALIGN_HC, &conf.tag_color);
1000 ty += get_sheight(r->tags[i]->tag, &conf.tag_font);
1001 }
1002 }
1003
1004 void draw_branch_box(gdImagePtr im, branch_t *b, int ypos)
1005 {
1006 int lx = b->cx - b->w/2;
1007 int rx = lx + b->w;
1008 int i;
1009 int yy;
1010
1011 draw_rbox(im, lx, ypos, rx, ypos+b->h, 5, &conf.branch_color, &conf.branch_bgcolor);
1012 yy = conf.branch_tspace;
1013 draw_string(im, b->branch->branch, &conf.branch_font, b->cx, ypos+yy, ALIGN_HC, &conf.branch_color);
1014 yy += get_sheight(b->branch->branch, &conf.branch_font);
1015 for(i = 0; i < b->ntags; i++)
1016 {
1017 draw_string(im, b->tags[i]->tag, &conf.branch_font, b->cx, ypos+yy, ALIGN_HC, &conf.branch_color);
1018 yy += get_sheight(b->tags[i]->tag, &conf.branch_font);
1019 }
1020 }
1021
1022 void draw_branch(gdImagePtr im, branch_t *b)
1023 {
1024 int yy;
1025 int i;
1026 int line[4];
1027
1028 line[0] = conf.rev_color.id;
1029 line[1] = gdTransparent;
1030 line[1] = gdTransparent;
1031 line[3] = conf.rev_color.id;
1032
1033 draw_branch_box(im, b, b->y);
1034
1035 if(conf.upside_down)
1036 {
1037 yy = b->y;
1038 for(i = 0; i < b->nrevs; i++)
1039 {
1040 revision_t *r = b->revs[i];
1041 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1042 gdImageLine(im, r->cx, yy, r->cx, r->y+r->h, gdStyled);
1043 draw_rev(im, r);
1044 yy = r->y;
1045 }
1046 if(conf.branch_dupbox)
1047 {
1048 i = b->y - b->th + b->h;
1049 gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1050 draw_branch_box(im, b, i);
1051 }
1052 }
1053 else
1054 {
1055 yy = b->y + b->h;
1056 for(i = 0; i < b->nrevs; i++)
1057 {
1058 revision_t *r = b->revs[i];
1059 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1060 gdImageLine(im, r->cx, yy, r->cx, r->y, gdStyled);
1061 draw_rev(im, r);
1062 yy = r->y + r->h;
1063 }
1064 if(conf.branch_dupbox)
1065 {
1066 i = b->y + b->th - b->h;
1067 gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1068 draw_branch_box(im, b, i);
1069 }
1070 }
1071 }
1072
1073 void draw_connector(gdImagePtr im, branch_t *b)
1074 {
1075 revision_t *r = b->branchpoint;
1076 int x1 = r->cx + r->w/2 + 2;
1077 int y1 = r->y + r->h/2;
1078 int x2 = b->cx;
1079 int y2 = b->y;
1080 if(conf.upside_down)
1081 y2 += b->h;
1082 gdImageLine(im, x1, y1, x2, y1, conf.branch_color.id);
1083 gdImageLine(im, x2, y1, x2, y2, conf.branch_color.id);
1084 }
1085
1086 void alloc_color(gdImagePtr im, color_t *c)
1087 {
1088 c->id = gdImageColorAllocate(im, c->r, c->g, c->b);
1089 }
1090
1091 gdImagePtr make_image(rcsfile_t *rcs)
1092 {
1093 gdImagePtr im;
1094 int i;
1095 char *cptr;
1096
1097 cptr = expand_string(conf.title, rcs, NULL, NULL, NULL, NULL);
1098 i = get_swidth(cptr, &conf.title_font);
1099 if(rcs->tw+conf.margin_left+conf.margin_right > i)
1100 i = rcs->tw+conf.margin_left+conf.margin_right;
1101 im = gdImageCreate(i, rcs->th+conf.margin_top+conf.margin_bottom);
1102 alloc_color(im, &conf.color_bg);
1103 alloc_color(im, &conf.tag_color);
1104 alloc_color(im, &conf.rev_color);
1105 alloc_color(im, &conf.rev_bgcolor);
1106 alloc_color(im, &conf.rev_text_color);
1107 alloc_color(im, &conf.branch_color);
1108 alloc_color(im, &conf.branch_bgcolor);
1109 alloc_color(im, &conf.title_color);
1110 alloc_color(im, &black_color);
1111 alloc_color(im, &white_color);
1112
1113 for(i = 0; i < rcs->nbranches; i++)
1114 draw_branch(im, rcs->branches[i]);
1115 for(i = 0; i < rcs->nbranches; i++)
1116 {
1117 if(rcs->branches[i]->branchpoint)
1118 draw_connector(im, rcs->branches[i]);
1119 }
1120 draw_stringnl(im, cptr, &conf.title_font, conf.title_x, conf.title_y, conf.title_align, &conf.title_color);
1121 xfree(cptr);
1122
1123 return im;
1124 }
1125
1126 /*
1127 **************************************************************************
1128 * Layout routines
1129 **************************************************************************
1130 */
1131 void move_branch(branch_t *b, int x, int y)
1132 {
1133 int i;
1134 b->cx += x;
1135 b->y += y;
1136 for(i = 0; i < b->nrevs; i++)
1137 {
1138 b->revs[i]->cx += x;
1139 b->revs[i]->y += y;
1140 }
1141 }
1142
1143 void reposition_branch(revision_t *r, int *x, int *w)
1144 {
1145 int i, j;
1146 for(j = 0; j < r->nbranches; j++)
1147 {
1148 branch_t *b = r->branches[j];
1149 *x += *w + conf.rev_minline + b->tw/2 - b->cx;
1150 *w = b->tw/2;
1151 move_branch(b, *x, r->y + r->h/2 + conf.branch_connect);
1152 *x = b->cx;
1153 /* Recurse to move branches of branched revisions */
1154 for(i = b->nrevs-1; i >= 0; i--)
1155 {
1156 reposition_branch(b->revs[i], x, w);
1157 }
1158 }
1159 }
1160
1161 void rect_union(int *x, int *y, int *w, int *h, branch_t *b)
1162 {
1163 int x1 = *x;
1164 int x2 = x1 + *w;
1165 int y1 = *y;
1166 int y2 = y1 + *h;
1167 int xx1 = b->cx - b->tw/2;
1168 int xx2 = xx1 + b->tw;
1169 int yy1 = b->y;
1170 int yy2 = yy1 + b->th;
1171 x1 = MIN(x1, xx1);
1172 x2 = MAX(x2, xx2);
1173 y1 = MIN(y1, yy1);
1174 y2 = MAX(y2, yy2);
1175 *x = x1;
1176 *y = y1;
1177 *w = x2 - x1;
1178 *h = y2 - y1;
1179 }
1180
1181 int branch_intersects(int top, int bottom, int left, branch_t *b)
1182 {
1183 int br = b->cx + b->tw/2;
1184 int bt = b->y - conf.branch_connect - conf.branch_margin/2;
1185 int bb = b->y + b->th + conf.branch_margin/2;
1186 return !(bt > bottom || bb < top || br >= left);
1187 }
1188
1189 int kern_branch(rcsfile_t *rcs, branch_t *b)
1190 {
1191 int left = b->cx - b->tw/2;
1192 int top = b->y - conf.branch_connect - conf.branch_margin/2;
1193 int bottom = b->y + b->th + conf.branch_margin/2;
1194 int i;
1195 int xpos = 0;
1196
1197 for(i = 0; i < rcs->nbranches; i++)
1198 {
1199 branch_t *bp = rcs->branches[i];
1200 if(bp == b)
1201 continue;
1202 if(branch_intersects(top, bottom, left, bp))
1203 {
1204 int m = bp->cx + bp->tw/2 + conf.branch_margin;
1205 if(m > xpos)
1206 xpos = m;
1207 }
1208 }
1209 if(xpos && (b->cx - b->tw/2) - xpos > 0)
1210 {
1211 move_branch(b, xpos - (b->cx - b->tw/2), 0);
1212 return 1;
1213 }
1214 return 0;
1215 }
1216
1217 void make_layout(rcsfile_t *rcs)
1218 {
1219 int i, j;
1220 int x, y;
1221 int w, h;
1222 int w2;
1223 int moved;
1224
1225 /* Remove all unwanted revisions */
1226 if(conf.strip_untagged)
1227 {
1228 for(i = 0; i < rcs->nbranches; i++)
1229 {
1230 branch_t *bp = rcs->branches[i];
1231 for(j = 0; j < bp->nrevs-1; j++)
1232 {
1233 if(!bp->revs[j]->ntags && !bp->revs[j]->nbranches)
1234 {
1235 memmove(&bp->revs[j], &bp->revs[j+1], (bp->nrevs-j-1) * sizeof(bp->revs[0]));
1236 bp->nrevs--;
1237 bp->revs[j]->stripped = 1;
1238 j--;
1239 }
1240 }
1241 }
1242 }
1243
1244 /* Calculate the box-sizes of the revisions */
1245 for(i = 0; i < rcs->nsrev; i++)
1246 {
1247 revision_t *rp;
1248 int w;
1249 int h;
1250 rp = rcs->srev[i];
1251 rp->revtext = expand_string(conf.rev_text, rcs, rp, rp->rev, NULL, rp->ntags ? rp->tags[0] : NULL);
1252 w = get_swidth(rp->revtext, &conf.rev_text_font);
1253 j = get_swidth(rp->rev->rev, &conf.rev_font);
1254 if(j > w)
1255 w = j;
1256 h = get_sheight(rp->revtext, &conf.rev_text_font) + get_sheight(rp->rev->rev, &conf.rev_font);
1257 for(j = 0; j < rp->ntags; j++)
1258 {
1259 int ww = get_swidth(rp->tags[j]->tag, &conf.tag_font);
1260 if(ww > w) w = ww;
1261 h += get_sheight(rp->tags[j]->tag, &conf.tag_font) + conf.rev_separator;
1262 }
1263 rp->w = w + conf.rev_lspace + conf.rev_rspace;
1264 rp->h = h + conf.rev_tspace + conf.rev_bspace;
1265 }
1266
1267 /* Calculate the box-sizes of the branches */
1268 for(i = 0; i < rcs->nbranches; i++)
1269 {
1270 branch_t *bp = rcs->branches[i];
1271 int w;
1272 int h;
1273 w = get_swidth(bp->branch->branch, &conf.branch_font);
1274 h = get_sheight(bp->branch->branch, &conf.branch_font);
1275 for(j = 0; j < bp->ntags; j++)
1276 {
1277 int ww = get_swidth(bp->tags[j]->tag, &conf.branch_font);
1278 if(ww > w) w = ww;
1279 h += get_sheight(bp->tags[j]->tag, &conf.branch_font);
1280 }
1281 w += conf.branch_lspace + conf.branch_rspace;
1282 h += conf.branch_tspace + conf.branch_bspace;
1283 bp->w = w;
1284 bp->h = h;
1285 for(j = 0; j < bp->nrevs; j++)
1286 {
1287 if(bp->revs[j]->w > w)
1288 w = bp->revs[j]->w;
1289 h += bp->revs[j]->h + conf.rev_minline;
1290 }
1291 if(conf.branch_dupbox)
1292 h += bp->h + conf.rev_minline;
1293 bp->th = h;
1294 bp->tw = w;
1295 }
1296
1297 /* Calculate the relative positions of revs in a branch */
1298 for(i = 0; i < rcs->nbranches; i++)
1299 {
1300 branch_t *b = rcs->branches[i];
1301 x = b->tw/2;
1302 y = b->h;
1303 b->cx = x;
1304 b->y = 0;
1305 for(j = 0; j < b->nrevs; j++)
1306 {
1307 y += conf.rev_minline;
1308 b->revs[j]->cx = x;
1309 b->revs[j]->y = y;
1310 y += b->revs[j]->h;
1311 }
1312 }
1313
1314 /* Reposition the branches */
1315 x = rcs->branches[0]->cx;
1316 w2 = rcs->branches[0]->tw / 2;
1317 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
1318 {
1319 reposition_branch(rcs->branches[0]->revs[i], &x, &w2);
1320 }
1321
1322 /* Try to move branches left if there is room (kerning) */
1323 for(moved = 1; moved; )
1324 {
1325 moved = 0;
1326 for(i = 1; i < rcs->nbranches; i++)
1327 {
1328 moved += kern_branch(rcs, rcs->branches[i]);
1329 }
1330 }
1331
1332 /* Move everything w.r.t. the top-left margin */
1333 for(i = 0; i < rcs->nbranches; i++)
1334 move_branch(rcs->branches[i], conf.margin_left, conf.margin_top);
1335
1336 /* Calculate overall image size */
1337 x = rcs->branches[0]->cx - rcs->branches[0]->tw/2;
1338 y = rcs->branches[0]->y;
1339 w = rcs->branches[0]->tw;
1340 h = rcs->branches[0]->th;
1341 for(i = 1; i < rcs->nbranches; i++)
1342 rect_union(&x, &y, &w, &h, rcs->branches[i]);
1343 rcs->tw = w;
1344 rcs->th = h;
1345
1346 /* Flip the entire tree */
1347 if(conf.upside_down)
1348 {
1349 y += rcs->th;
1350 for(i = 0; i < rcs->nbranches; i++)
1351 {
1352 branch_t *b = rcs->branches[i];
1353 for(j = 0; j < b->nrevs; j++)
1354 {
1355 revision_t *r = b->revs[j];
1356 r->y = y - r->y - r->h + conf.margin_top;
1357 }
1358 b->y = y - b->y - b->h + conf.margin_top;
1359 }
1360 }
1361 }
1362
1363 /*
1364 **************************************************************************
1365 * Imagemap functions
1366 **************************************************************************
1367 */
1368 void make_imagemap(rcsfile_t *rcs, FILE *fp)
1369 {
1370 int i, j;
1371 fprintf(fp, "<map name=\"%s\">\n", conf.map_name);
1372 for(i = 0; i < rcs->nbranches; i++)
1373 {
1374 branch_t *b = rcs->branches[i];
1375 tag_t *tag = b->ntags ? b->tags[0] : NULL;
1376 char *bhref = expand_string(conf.map_branch_href, rcs, NULL, b->branch, NULL, tag);
1377 char *balt = expand_string(conf.map_branch_alt, rcs, NULL, b->branch, NULL, tag);
1378 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
1379 bhref,
1380 b->cx - b->w/2, b->y, b->cx + b->w/2, b->y + b->h,
1381 balt);
1382 for(j = 0; j < b->nrevs; j++)
1383 {
1384 revision_t *r = b->revs[j];
1385 revision_t* r1;
1386 int xoff;
1387 int x1;
1388 int x2;
1389 int y1;
1390 int y2;
1391 char *href;
1392 char *alt;
1393
1394 tag = r->ntags ? r->tags[0] : NULL;
1395 href = expand_string(conf.map_rev_href, rcs, r, r->rev, NULL, tag);
1396 alt = expand_string(conf.map_rev_alt, rcs, r, r->rev, NULL, tag);
1397 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
1398 href,
1399 r->cx - r->w/2, r->y, r->cx + r->w/2, r->y + r->h,
1400 alt);
1401 xfree(href);
1402 xfree(alt);
1403 if(j > 0 || b->branchpoint)
1404 {
1405 if(j > 0)
1406 {
1407 r1 = b->revs[j-1];
1408 xoff = MIN(r->w, r1->w)/4;
1409 y1 = conf.upside_down ? r1->y : r1->y + r1->h;
1410 }
1411 else
1412 {
1413 r1 = b->branchpoint;
1414 xoff = MIN(r->w, b->w)/4;
1415 y1 = conf.upside_down ? b->y : b->y + b->h;
1416 }
1417 x1 = r->cx - xoff;
1418 x2 = r->cx + xoff;
1419 y2 = conf.upside_down ? r->y + r->h : r->y;
1420 href = expand_string(conf.map_diff_href, rcs, r, r->rev, r1->rev, tag);
1421 alt = expand_string(conf.map_diff_alt, rcs, r, r->rev, r1->rev, tag);
1422 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
1423 href,
1424 x1, y1 + 1, x2, y2 - 1,
1425 alt);
1426 xfree(href);
1427 xfree(alt);
1428 }
1429 }
1430 if(conf.branch_dupbox)
1431 {
1432 int y;
1433 if(conf.upside_down)
1434 y = b->y + b->h - b->th;
1435 else
1436 y = b->y - b->h + b->th;
1437 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
1438 bhref,
1439 b->cx - b->w/2, y, b->cx + b->w/2, y + b->h,
1440 balt);
1441 }
1442 xfree(bhref);
1443 xfree(balt);
1444 }
1445 fprintf(fp, "</map>\n");
1446 }
1447
1448 /*
1449 **************************************************************************
1450 * Configuration
1451 **************************************************************************
1452 */
1453 int read_config(const char *path)
1454 {
1455 FILE *fp;
1456 int r;
1457
1458 if(path)
1459 {
1460 if((fp = fopen(path, "r")) == NULL)
1461 {
1462 return 0;
1463 }
1464 else
1465 input_file = path;
1466 }
1467 else
1468 {
1469 if((fp = fopen("./" CONFFILENAME, "r")) == NULL)
1470 {
1471 if((fp = fopen(ETCDIR "/" CONFFILENAME, "r")) == NULL)
1472 {
1473 return 0;
1474 }
1475 else
1476 input_file = ETCDIR "/" CONFFILENAME;
1477 }
1478 else
1479 input_file = "./" CONFFILENAME;
1480 }
1481
1482 yyin = fp;
1483 r = yyparse();
1484 fclose(fp);
1485 input_file = NULL;
1486 return r == 0;
1487 }
1488
1489 /*
1490 **************************************************************************
1491 * Program entry
1492 **************************************************************************
1493 */
1494 static const char usage_str[] =
1495 "Usage: cvsgraph [options] <file>\n"
1496 " -b Add a branch box at both sides of the trunk (config value is negated)\n"
1497 " -c <file> Read alternative config from <file>\n"
1498 " -d <level> Enable debug mode at <level>\n"
1499 " -h This message\n"
1500 " -i Generate an imagemap instead of image\n"
1501 " -M <name> Use <name> as imagemap name\n"
1502 " -m <mod> Use <mod> as cvs module\n"
1503 " -o <file> Output to <file>\n"
1504 " -q Be quiet (i.e. no warnings)\n"
1505 " -r <path> Use <path> as cvsroot path\n"
1506 " -s Strip untagged revisions (config value is negated)\n"
1507 " -u Upside down image (mirror vertically; config value is negated)\n"
1508 " -V Print version and exit\n"
1509 " -[0-9] <txt> Use <txt> for expansion\n"
1510 ;
1511
1512 #define VERSION_STR "1.1.4"
1513 #define NOTICE_STR "Copyright (c) 2001 B.Stultiens"
1514
1515 void append_slash(char **path)
1516 {
1517 int l;
1518 assert(path != NULL);
1519 assert(*path != NULL);
1520 l = strlen(*path);
1521 if(!l || (*path)[l-1] == '/')
1522 return;
1523 *path = xrealloc(*path, l+2);
1524 strcat(*path, "/");
1525 }
1526
1527 int main(int argc, char *argv[])
1528 {
1529 extern int yy_flex_debug;
1530 extern int rcs_flex_debug;
1531 extern int yydebug;
1532 extern int rcsdebug;
1533 int optc;
1534 char *confpath = NULL;
1535 char *outfile = NULL;
1536 char *cvsroot = NULL;
1537 char *cvsmodule = NULL;
1538 int imagemap = 0;
1539 int upsidedown = 0;
1540 int bdupbox = 0;
1541 int stripuntag = 0;
1542 char *imgmapname = NULL;
1543 int lose = 0;
1544 FILE *fp;
1545 rcsfile_t *rcs;
1546 gdImagePtr im;
1547
1548 while((optc = getopt(argc, argv, "0:1:2:3:4:5:6:7:8:9:bc:d:hiM:m:o:qr:suV")) != EOF)
1549 {
1550 switch(optc)
1551 {
1552 case 'b':
1553 bdupbox = 1;
1554 break;
1555 case 'c':
1556 confpath = xstrdup(optarg);
1557 break;
1558 case 'd':
1559 debuglevel = strtol(optarg, NULL, 0);
1560 break;
1561 case 'i':
1562 imagemap = 1;
1563 break;
1564 case 'M':
1565 imgmapname = xstrdup(optarg);
1566 break;
1567 case 'm':
1568 cvsmodule = xstrdup(optarg);
1569 break;
1570 case 'o':
1571 outfile = xstrdup(optarg);
1572 break;
1573 case 'q':
1574 quiet = 1;
1575 break;
1576 case 'r':
1577 cvsroot = xstrdup(optarg);
1578 break;
1579 case 's':
1580 stripuntag = 1;
1581 break;
1582 case 'u':
1583 upsidedown = 1;
1584 break;
1585 case 'V':
1586 fprintf(stdout, "cvsgraph v%s, %s\n", VERSION_STR, NOTICE_STR);
1587 return 0;
1588 case 'h':
1589 fprintf(stdout, "%s", usage_str);
1590 return 0;
1591 default:
1592 if(isdigit(optc))
1593 {
1594 conf.expand[optc-'0'] = xstrdup(optarg);
1595 }
1596 else
1597 lose++;
1598 }
1599 }
1600
1601 if(optind >= argc)
1602 {
1603 fprintf(stderr, "Missing inputfile\n");
1604 lose++;
1605 }
1606
1607 if(lose)
1608 {
1609 fprintf(stderr, "%s", usage_str);
1610 return 1;
1611 }
1612
1613 if(debuglevel)
1614 {
1615 setvbuf(stdout, NULL, 0, _IONBF);
1616 setvbuf(stderr, NULL, 0, _IONBF);
1617 }
1618 yy_flex_debug = (debuglevel & DEBUG_CONF_LEX) != 0;
1619 rcs_flex_debug = (debuglevel & DEBUG_RCS_LEX) != 0;
1620 yydebug = (debuglevel & DEBUG_CONF_YACC) != 0;
1621 rcsdebug = (debuglevel & DEBUG_RCS_YACC) != 0;
1622
1623 /* Set defaults */
1624 conf.tag_font = gdFontTiny;
1625 conf.rev_font = gdFontTiny;
1626 conf.branch_font = gdFontTiny;
1627 conf.title_font = gdFontTiny;
1628 conf.rev_text_font = gdFontTiny;
1629
1630 conf.cvsroot = xstrdup("");
1631 conf.cvsmodule = xstrdup("");
1632 conf.date_format = xstrdup("%d-%b-%Y %H:%M:%S");
1633 conf.title = xstrdup("");
1634 conf.map_name = xstrdup("CvsGraphImageMap");
1635 conf.map_branch_href = xstrdup("href=\"unset: conf.map_branch_href\"");
1636 conf.map_branch_alt = xstrdup("alt=\"%B\"");
1637 conf.map_rev_href = xstrdup("href=\"unset: conf.map_rev_href\"");
1638 conf.map_rev_alt = xstrdup("alt=\"%R\"");
1639 conf.map_diff_href = xstrdup("href=\"unset: conf.map_diff_href\"");
1640 conf.map_diff_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
1641 conf.rev_text = xstrdup("%d");
1642
1643 conf.color_bg = white_color;
1644 conf.branch_bgcolor = white_color;
1645 conf.branch_color = black_color;
1646 conf.rev_color = black_color;
1647 conf.rev_bgcolor = white_color;
1648 conf.tag_color = black_color;
1649 conf.title_color = black_color;
1650 conf.rev_text_color = black_color;
1651
1652 conf.image_quality = 100;
1653
1654 if(!read_config(confpath))
1655 {
1656 fprintf(stderr, "Error reading config file\n");
1657 return 1;
1658 }
1659
1660 /* Set overrides */
1661 if(cvsroot) conf.cvsroot = cvsroot;
1662 if(cvsmodule) conf.cvsmodule = cvsmodule;
1663 if(imgmapname) conf.map_name = imgmapname;
1664 if(upsidedown) conf.upside_down = !conf.upside_down;
1665 if(bdupbox) conf.branch_dupbox = !conf.branch_dupbox;
1666 if(stripuntag) conf.strip_untagged = !conf.strip_untagged;
1667
1668 append_slash(&conf.cvsroot);
1669 append_slash(&conf.cvsmodule);
1670
1671 rcs = get_rcsfile(conf.cvsroot, conf.cvsmodule, argv[optind]);
1672 if(!rcs)
1673 return 1;
1674
1675 if(debuglevel & DEBUG_RCS_FILE)
1676 dump_rcsfile(rcs);
1677
1678 if(!reorganise_branches(rcs))
1679 return 1;
1680
1681 if(!assign_tags(rcs))
1682 return 1;
1683
1684 if(outfile)
1685 {
1686 if((fp = fopen(outfile, "wb")) == NULL)
1687 {
1688 perror(outfile);
1689 return 1;
1690 }
1691 }
1692 else
1693 {
1694 fp = stdout;
1695 #ifdef __WIN32__
1696 /* Bad hack for DOS/Windows */
1697 if(setmode(fileno(fp), O_BINARY) == -1)
1698 {
1699 perror("Set binary mode for stdout");
1700 return 1;
1701 }
1702 #endif
1703 }
1704
1705 make_layout(rcs);
1706
1707 if(!imagemap)
1708 {
1709 /* Create an image */
1710 im = make_image(rcs);
1711
1712 switch(conf.image_type)
1713 {
1714 #ifdef HAVE_IMAGE_GIF
1715 # ifndef HAVE_IMAGE_PNG
1716 default:
1717 # endif
1718 case IMAGE_GIF:
1719 gdImageGif(im, fp);
1720 break;
1721 #endif
1722 #ifdef HAVE_IMAGE_PNG
1723 default:
1724 case IMAGE_PNG:
1725 gdImagePng(im, fp);
1726 break;
1727 #endif
1728 #ifdef HAVE_IMAGE_JPEG
1729 # if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG)
1730 default:
1731 # endif
1732 case IMAGE_JPEG:
1733 gdImageJpeg(im, fp, conf.image_quality);
1734 break;
1735 #endif
1736 }
1737
1738 gdImageDestroy(im);
1739 }
1740 else
1741 {
1742 /* Create an imagemap */
1743 make_imagemap(rcs, fp);
1744 }
1745
1746 if(outfile)
1747 fclose(fp);
1748
1749 return 0;
1750 }

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0