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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0