/[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.54 - (hide annotations)
Sat Oct 23 20:13:12 2004 UTC (13 years ago) by bertho
Branch: MAIN
Changes since 1.53: +2 -1 lines
File MIME type: text/plain
Fix total branch-count when branches are folded.
1 bertho 1.1 /*
2     * CvsGraph graphical representation generator of brances and revisions
3     * of a file in cvs/rcs.
4     *
5 bertho 1.47 * Copyright (C) 2001,2002,2003,2004 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 bertho 1.44 #include <stdarg.h>
27 bertho 1.1 #include <unistd.h>
28     #include <string.h>
29     #include <assert.h>
30     #include <sys/types.h>
31     #include <sys/stat.h>
32 bertho 1.45 #ifdef HAVE_SYS_WAIT_H
33     # include <sys/wait.h>
34     #endif
35 bertho 1.1 #include <fcntl.h>
36     #include <regex.h>
37     #include <errno.h>
38 bertho 1.8 #include <ctype.h>
39 bertho 1.9 #include <time.h>
40 bertho 1.18 #include <limits.h>
41 bertho 1.30 #include <regex.h>
42 bertho 1.35 #include <math.h>
43 bertho 1.9
44 bertho 1.14 #ifdef HAVE_GETOPT_H
45     # include <getopt.h>
46     #endif
47    
48 bertho 1.1 #include <gd.h>
49     #include <gdfontt.h>
50    
51     #include "cvsgraph.h"
52     #include "utils.h"
53     #include "readconf.h"
54 bertho 1.7 #include "rcs.h"
55 bertho 1.1
56 bertho 1.5 #if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG) && !defined(HAVE_IMAGE_JPEG)
57     # error No image output format available. Check libgd
58     #endif
59    
60    
61 bertho 1.21 /*#define DEBUG 1*/
62 bertho 1.19 /*#define NOGDFILL 1*/
63 bertho 1.26 /*#define DEBUG_IMAGEMAP 1*/
64 bertho 1.19
65 bertho 1.32 #define LOOPSAFEGUARD 10000 /* Max itterations in possible infinite loops */
66 bertho 1.1
67     #ifndef MAX
68     # define MAX(a,b) ((a) > (b) ? (a) : (b))
69     #endif
70    
71     #ifndef MIN
72     # define MIN(a,b) ((a) < (b) ? (a) : (b))
73     #endif
74    
75     #define ALIGN_HL 0x00
76     #define ALIGN_HC 0x01
77     #define ALIGN_HR 0x02
78 bertho 1.9 #define ALIGN_HX 0x0f
79 bertho 1.1 #define ALIGN_VT 0x00
80     #define ALIGN_VC 0x10
81     #define ALIGN_VB 0x20
82     #define ALIGN_VX 0xf0
83 bertho 1.9
84 bertho 1.35 #ifndef M_PI /* math.h should have defined this */
85     # define M_PI 3.14159265358979323846
86     #endif
87     #define ROUND(f) ((f >= 0.0)?((int)(f + 0.5)):((int)(f - 0.5)))
88    
89     #define ARROW_LENGTH 12 /* Default arrow dimensions */
90     #define ARROW_WIDTH 3
91    
92 bertho 1.1 /*
93     **************************************************************************
94     * Globals
95     **************************************************************************
96     */
97    
98     config_t conf;
99 bertho 1.7 int debuglevel;
100 bertho 1.44
101     static color_t white_color = {255, 255, 255, 0};
102     static color_t black_color = {0, 0, 0, 0};
103    
104     static branch_t *subtree_branch = NULL; /* Set to the (first) subtree branch that we want to show */
105     static revision_t *subtree_rev = NULL; /* Set to the subtree revision which branches we want to show */
106    
107     static msg_stack_t *msg_stack = NULL; /* Messages that would otherwise be sent to stderr goto the image */
108     static int nmsg_stack = 0;
109 bertho 1.9
110 bertho 1.30 /*
111     **************************************************************************
112     * Forwards
113     **************************************************************************
114     */
115     static void zap_string(void);
116     static char *dup_string(void);
117     static void add_string_str(const char *s);
118     static void add_string_ch(int ch);
119     static void add_string_date(const char *d);
120     static void add_string_str_html(const char *s, int maxlen);
121     static void add_string_str_len(const char *s, int maxlen);
122 bertho 1.1
123 bertho 1.44 static void calc_subtree_size(branch_t *b, int *x, int *y, int *w, int *h);
124    
125 bertho 1.1 /*
126     **************************************************************************
127 bertho 1.17 * Debug routines
128 bertho 1.1 **************************************************************************
129     */
130 bertho 1.19 static void dump_rev(char *p, rev_t *r)
131 bertho 1.1 {
132 bertho 1.7 printf("%s", p);
133     if(r)
134     printf("'%s', '%s', %d\n", r->rev, r->branch, r->isbranch);
135     else
136     printf("<null>\n");
137 bertho 1.1 }
138    
139 bertho 1.19 static void dump_id(char *p, char *d)
140 bertho 1.1 {
141 bertho 1.7 printf("%s", p);
142     if(d)
143     printf("'%s'\n", d);
144     else
145     printf("<null>\n");
146 bertho 1.1 }
147    
148 bertho 1.19 static void dump_idrev(char *p, idrev_t *t)
149 bertho 1.1 {
150 bertho 1.7 printf("%s", p);
151     if(t)
152 bertho 1.1 {
153 bertho 1.7 printf("'%s' -> ", t->id);
154     dump_rev("", t->rev);
155 bertho 1.1 }
156     else
157 bertho 1.7 printf("<null>\n");
158 bertho 1.1 }
159    
160 bertho 1.19 static void dump_tag(char *p, tag_t *t)
161 bertho 1.1 {
162 bertho 1.7 printf("%s", p);
163     if(t)
164 bertho 1.1 {
165 bertho 1.7 printf("'%s' -> ", t->tag);
166     dump_rev("", t->rev);
167 bertho 1.1 }
168 bertho 1.7 else
169     printf("<null>\n");
170 bertho 1.1 }
171    
172 bertho 1.19 static void dump_delta(char *p, delta_t *d)
173 bertho 1.1 {
174 bertho 1.7 int i;
175     printf("%sdelta.rev : ", p);
176     dump_rev("", d->rev);
177     printf("%sdelta.date : %s\n", p, d->date);
178     printf("%sdelta.author: %s\n", p, d->author);
179     printf("%sdelta.state : %s\n", p, d->state);
180     for(i = 0; d->branches && i < d->branches->nrevs; i++)
181 bertho 1.1 {
182 bertho 1.7 printf("%sdelta.branch: ", p);
183     dump_rev("", d->branches->revs[i]);
184 bertho 1.1 }
185 bertho 1.7 printf("%sdelta.next : ", p);
186     dump_rev("", d->next);
187     printf("\n");
188 bertho 1.1 }
189    
190 bertho 1.19 static void dump_dtext(char *p, dtext_t *d)
191 bertho 1.1 {
192 bertho 1.7 printf("%sdtext.rev : ", p);
193     dump_rev("", d->rev);
194     printf("%sdtext.log : %d bytes\n", p, d->log ? strlen(d->log) : -1);
195     printf("%sdtext.text : %d bytes\n", p, d->text ? strlen(d->text) : -1);
196     printf("\n");
197 bertho 1.1 }
198    
199 bertho 1.19 static void dump_rcsfile(rcsfile_t *rcs)
200 bertho 1.1 {
201 bertho 1.7 int i;
202     printf("root : '%s'\n", rcs->root);
203     printf("module : '%s'\n", rcs->module);
204     printf("file : '%s'\n", rcs->file);
205     dump_rev("head : ", rcs->head);
206     dump_rev("branch : ", rcs->branch);
207     printf("access :\n");
208     for(i = 0; rcs->access && i < rcs->access->nids; i++)
209     dump_id("\t", rcs->access->ids[i]);
210     printf("tags :\n");
211     for(i = 0; rcs->tags && i < rcs->tags->ntags; i++)
212     dump_tag("\t", rcs->tags->tags[i]);
213     printf("locks :%s\n", rcs->strict ? " (strict)" : "");
214     for(i = 0; rcs->locks && i < rcs->locks->nidrevs; i++)
215     dump_idrev("\t", rcs->locks->idrevs[i]);
216     printf("comment: '%s'\n", rcs->comment);
217     printf("expand : '%s'\n", rcs->expand ? rcs->expand : "(default -kv)");
218     printf("deltas :\n");
219     for(i = 0; rcs->deltas && i < rcs->deltas->ndeltas; i++)
220     dump_delta("\t", rcs->deltas->deltas[i]);
221     printf("desc : '%s'\n", rcs->desc);
222     printf("dtexts :\n");
223     for(i = 0; rcs->dtexts && i < rcs->dtexts->ndtexts; i++)
224     dump_dtext("\t", rcs->dtexts->dtexts[i]);
225    
226     fflush(stdout);
227 bertho 1.1 }
228    
229 bertho 1.7 /*
230     **************************************************************************
231 bertho 1.44 * Error/Warning Message helpers
232     **************************************************************************
233     */
234     #define MSGBUFSIZE 256
235     void stack_msg(int severity, const char *fmt, ...)
236     {
237     va_list va;
238     int i;
239     char *buf = xmalloc(MSGBUFSIZE);
240     switch(severity)
241     {
242     case MSG_WARN: sprintf(buf, "Warning: "); break;
243     case MSG_ERR: sprintf(buf, "Error: "); break;
244     default: sprintf(buf, "Unqualified error: "); break;
245     }
246     i = strlen(buf);
247     assert(i < MSGBUFSIZE);
248     va_start(va, fmt);
249     vsnprintf(buf+i, MSGBUFSIZE-i, fmt, va);
250     va_end(va);
251     if(!msg_stack)
252     msg_stack = xmalloc(sizeof(*msg_stack));
253     else
254     {
255     msg_stack = xrealloc(msg_stack, (nmsg_stack+1)*sizeof(*msg_stack));
256     }
257     msg_stack[nmsg_stack].msg = buf;
258     msg_stack[nmsg_stack].severity = severity;
259     nmsg_stack++;
260     }
261    
262     /*
263     **************************************************************************
264 bertho 1.7 * Read the rcs file
265     **************************************************************************
266     */
267 bertho 1.44 static rcsfile_t *get_rcsfile(const char *cvsroot, const char *module, const char *file)
268 bertho 1.1 {
269 bertho 1.7 char *cmd = NULL;
270     int rv;
271 bertho 1.1
272 bertho 1.18 if(file)
273 bertho 1.9 {
274 bertho 1.18 cmd = xmalloc(strlen(cvsroot) + strlen(module) + strlen(file) + 1);
275     sprintf(cmd, "%s%s%s", cvsroot, module, file);
276     if(!(rcsin = fopen(cmd, "rb")))
277     {
278     perror(cmd);
279     return NULL;
280     }
281     input_file = cmd;
282     }
283     else
284     {
285     rcsin = stdin;
286     input_file = "<stdin>";
287 bertho 1.9 }
288 bertho 1.7 line_number = 1;
289     rv = rcsparse();
290 bertho 1.18 if(file)
291     {
292     fclose(rcsin);
293     xfree(cmd);
294     }
295 bertho 1.7 if(rv)
296 bertho 1.1 return NULL;
297 bertho 1.7 input_file = NULL;
298 bertho 1.18 if(file)
299     {
300     rcsfile->root = xstrdup(cvsroot);
301     rcsfile->module = xstrdup(module);
302     rcsfile->file = xstrdup(file);
303     }
304     else
305     {
306     rcsfile->root = xstrdup("");
307     rcsfile->module = xstrdup("");
308     rcsfile->file = xstrdup("<stdin>");
309     }
310 bertho 1.7 return rcsfile;
311 bertho 1.1 }
312    
313     /*
314     **************************************************************************
315     * Sort and find helpers
316     **************************************************************************
317     */
318 bertho 1.44 static int count_dots(const char *s)
319 bertho 1.5 {
320     int i;
321     for(i = 0; *s; s++)
322     {
323     if(*s == '.')
324     i++;
325     }
326     return i;
327     }
328    
329 bertho 1.44 static int compare_rev(int bcmp, const rev_t *r1, const rev_t *r2)
330 bertho 1.5 {
331     int d1, d2;
332 bertho 1.7 char *c1, *c2;
333 bertho 1.5 char *v1, *v2;
334     char *s1, *s2;
335     int retval = 0;
336     assert(r1 != NULL);
337     assert(r2 != NULL);
338 bertho 1.7 if(bcmp)
339     {
340     assert(r1->branch != NULL);
341     assert(r2->branch != NULL);
342     c1 = r1->branch;
343     c2 = r2->branch;
344     }
345     else
346     {
347     assert(r1->rev != NULL);
348     assert(r2->rev != NULL);
349     c1 = r1->rev;
350     c2 = r2->rev;
351     }
352 bertho 1.5
353 bertho 1.7 d1 = count_dots(c1);
354     d2 = count_dots(c2);
355 bertho 1.5 if(d1 != d2)
356     {
357     return d1 - d2;
358     }
359    
360 bertho 1.7 s1 = v1 = xstrdup(c1);
361     s2 = v2 = xstrdup(c2);
362 bertho 1.5 while(1)
363     {
364     char *vc1 = strchr(s1, '.');
365     char *vc2 = strchr(s2, '.');
366     if(vc1 && vc2)
367     *vc1 = *vc2 = '\0';
368     if(*s1 && *s2)
369     {
370     d1 = atoi(s1);
371     d2 = atoi(s2);
372     if(d1 != d2)
373     {
374     retval = d1 - d2;
375     break;
376     }
377     }
378     if(!vc1 || !vc2)
379     break;
380     s1 = vc1 + 1;
381     s2 = vc2 + 1;
382     }
383     xfree(v1);
384     xfree(v2);
385     return retval;
386     }
387    
388 bertho 1.7 /*
389     **************************************************************************
390     * Reorganise the rcsfile for the branches
391     *
392     * Basically, we have a list of deltas (i.e. administrative info on
393     * revisions) and a list of delta text (the actual logs and diffs).
394     * The deltas are linked through the 'next' and the 'branches' fields
395     * which describe the tree-structure of revisions.
396     * The reorganisation means that we put each delta and corresponding
397     * delta text in a revision structure and assign it to a specific
398     * branch. This is required because we want to be able to calculate
399     * the bounding boxes of each branch. The revisions expand vertically
400     * and the branches expand horizontally.
401     * The reorganisation is performed in these steps:
402 bertho 1.17 * 1 - sort deltas and delta text on revision number for quick lookup
403 bertho 1.7 * 2 - start at the denoted head revision:
404     * * create a branch structure and add this revision
405     * * for each 'branches' in the delta do:
406     * - walk all 'branches' of the delta and recursively goto 2
407     * with the denoted branch delta as new head
408     * - backlink the newly create sub-branch to the head revision
409     * so that we can draw them recursively
410     * * set head to the 'next' field and goto 2 until no next is
411     * available
412     * 3 - update the administration
413     **************************************************************************
414     */
415 bertho 1.19 static int sort_delta(const void *d1, const void *d2)
416 bertho 1.7 {
417     return compare_rev(0, (*(delta_t **)d1)->rev, (*(delta_t **)d2)->rev);
418     }
419    
420 bertho 1.19 static int search_delta(const void *r, const void *d)
421 bertho 1.1 {
422 bertho 1.7 return compare_rev(0, (rev_t *)r, (*(delta_t **)d)->rev);
423 bertho 1.1 }
424    
425 bertho 1.19 static delta_t *find_delta(delta_t **dl, int n, rev_t *r)
426 bertho 1.1 {
427 bertho 1.7 delta_t **d;
428 bertho 1.23 if(!n)
429     return NULL;
430 bertho 1.7 d = bsearch(r, dl, n, sizeof(*dl), search_delta);
431     if(!d)
432     return NULL;
433     return *d;
434 bertho 1.1 }
435    
436 bertho 1.19 static int sort_dtext(const void *d1, const void *d2)
437 bertho 1.1 {
438 bertho 1.7 return compare_rev(0, (*(dtext_t **)d1)->rev, (*(dtext_t **)d2)->rev);
439 bertho 1.1 }
440    
441 bertho 1.19 static int search_dtext(const void *r, const void *d)
442 bertho 1.1 {
443 bertho 1.7 return compare_rev(0, (rev_t *)r, (*(dtext_t **)d)->rev);
444 bertho 1.1 }
445    
446 bertho 1.19 static dtext_t *find_dtext(dtext_t **dl, int n, rev_t *r)
447 bertho 1.1 {
448 bertho 1.7 dtext_t **d;
449 bertho 1.23 if(!n)
450     return NULL;
451 bertho 1.7 d = bsearch(r, dl, n, sizeof(*dl), search_dtext);
452     if(!d)
453 bertho 1.1 return NULL;
454 bertho 1.7 return *d;
455     }
456    
457 bertho 1.19 static rev_t *dup_rev(const rev_t *r)
458 bertho 1.7 {
459     rev_t *t = xmalloc(sizeof(*t));
460     t->rev = xstrdup(r->rev);
461     t->branch = xstrdup(r->branch);
462     t->isbranch = r->isbranch;
463     return t;
464     }
465    
466 bertho 1.19 static branch_t *new_branch(delta_t *d, dtext_t *t)
467 bertho 1.7 {
468     branch_t *b = xmalloc(sizeof(*b));
469     revision_t *r = xmalloc(sizeof(*r));
470     r->delta = d;
471     r->dtext = t;
472     r->rev = d->rev;
473     r->branch = b;
474     b->branch = dup_rev(d->rev);
475     b->branch->isbranch = 1;
476     b->nrevs = 1;
477     b->revs = xmalloc(sizeof(b->revs[0]));
478     b->revs[0] = r;
479     return b;
480     }
481    
482 bertho 1.19 static revision_t *add_to_branch(branch_t *b, delta_t *d, dtext_t *t)
483 bertho 1.7 {
484     revision_t *r = xmalloc(sizeof(*r));
485     r->delta = d;
486     r->dtext = t;
487     r->rev = d->rev;
488     r->branch = b;
489     b->revs = xrealloc(b->revs, (b->nrevs+1) * sizeof(b->revs[0]));
490     b->revs[b->nrevs] = r;
491     b->nrevs++;
492     return r;
493     }
494    
495 bertho 1.47 static int sort_branch_height(const void *b1, const void *b2)
496     {
497     return (*(branch_t **)b1)->nrevs - (*(branch_t **)b2)->nrevs;
498     }
499    
500 bertho 1.44 static void build_branch(branch_t ***bl, int *nbl, delta_t **sdl, int nsdl, dtext_t **sdt, int nsdt, delta_t *head)
501 bertho 1.7 {
502     branch_t *b;
503     dtext_t *text;
504     revision_t *currev;
505    
506     assert(head != NULL);
507    
508     if(head->flag)
509     {
510 bertho 1.44 stack_msg(MSG_ERR, "Circular reference on '%s' in branchpoint\n", head->rev->rev);
511 bertho 1.7 return;
512     }
513     head->flag++;
514     text = find_dtext(sdt, nsdt, head->rev);
515    
516     /* Create a new branch for this head */
517     b = new_branch(head, text);
518     *bl = xrealloc(*bl, (*nbl+1)*sizeof((*bl)[0]));
519     (*bl)[*nbl] = b;
520     (*nbl)++;
521     currev = b->revs[0];
522     while(1)
523     {
524     /* Process all sub-branches */
525     if(head->branches)
526     {
527     int i;
528     for(i = 0; i < head->branches->nrevs; i++)
529     {
530     delta_t *d = find_delta(sdl, nsdl, head->branches->revs[i]);
531     int btag = *nbl;
532     if(!d)
533     continue;
534     build_branch(bl, nbl, sdl, nsdl, sdt, nsdt, d);
535    
536     /* Set the new branch's origin */
537     (*bl)[btag]->branchpoint = currev;
538    
539     /* Add branch to this revision */
540     currev->branches = xrealloc(currev->branches, (currev->nbranches+1)*sizeof(currev->branches[0]));
541     currev->branches[currev->nbranches] = (*bl)[btag];
542     currev->nbranches++;
543     }
544 bertho 1.47 if(conf.branch_resort)
545     qsort(currev->branches, currev->nbranches, sizeof(currev->branches[0]), sort_branch_height);
546 bertho 1.7 }
547    
548     /* Walk through the next list */
549     if(!head->next)
550     return;
551 bertho 1.9
552 bertho 1.7 head = find_delta(sdl, nsdl, head->next);
553     if(!head)
554     {
555 bertho 1.44 stack_msg(MSG_ERR, "Next revision (%s) not found in deltalist\n", head->next->rev);
556 bertho 1.7 return;
557     }
558     if(head->flag)
559     {
560 bertho 1.44 stack_msg(MSG_ERR, "Circular reference on '%s'\n", head->rev->rev);
561 bertho 1.7 return;
562     }
563     head->flag++;
564     text = find_dtext(sdt, nsdt, head->rev);
565     currev = add_to_branch(b, head, text);
566     }
567     }
568    
569     int reorganise_branches(rcsfile_t *rcs)
570     {
571     delta_t **sdelta;
572     int nsdelta;
573     dtext_t **sdtext;
574     int nsdtext;
575     delta_t *head;
576     branch_t **bl;
577     int nbl;
578     int i;
579    
580     assert(rcs->deltas != NULL);
581     assert(rcs->head != NULL);
582    
583     /* Make a new list for quick lookup */
584     nsdelta = rcs->deltas->ndeltas;
585     sdelta = xmalloc(nsdelta * sizeof(sdelta[0]));
586     memcpy(sdelta, rcs->deltas->deltas, nsdelta * sizeof(sdelta[0]));
587     qsort(sdelta, nsdelta, sizeof(sdelta[0]), sort_delta);
588    
589     /* Do the same for the delta text */
590 bertho 1.22 if(rcs->dtexts)
591     {
592     nsdtext = rcs->dtexts->ndtexts;
593     sdtext = xmalloc(nsdtext * sizeof(sdtext[0]));
594     memcpy(sdtext, rcs->dtexts->dtexts, nsdtext * sizeof(sdtext[0]));
595     qsort(sdtext, nsdtext, sizeof(sdtext[0]), sort_dtext);
596     }
597     else
598     {
599     nsdtext = 0;
600     sdtext = NULL;
601     }
602 bertho 1.7
603     /* Start from the head of the trunk */
604     head = find_delta(sdelta, nsdelta, rcs->head);
605     if(!head)
606     {
607 bertho 1.44 stack_msg(MSG_ERR, "Head revision (%s) not found in deltalist\n", rcs->head->rev);
608 bertho 1.7 return 0;
609     }
610     bl = NULL;
611     nbl = 0;
612     build_branch(&bl, &nbl, sdelta, nsdelta, sdtext, nsdtext, head);
613    
614     /* Reverse the head branch */
615     for(i = 0; i < bl[0]->nrevs/2; i++)
616     {
617     revision_t *r;
618     r = bl[0]->revs[i];
619     bl[0]->revs[i] = bl[0]->revs[bl[0]->nrevs-i-1];
620     bl[0]->revs[bl[0]->nrevs-i-1] = r;
621     }
622    
623     /* Update the branch-number of the head because it was reversed */
624     xfree(bl[0]->branch->branch);
625     bl[0]->branch->branch = xstrdup(bl[0]->revs[0]->rev->branch);
626    
627     /* Keep the admin */
628     rcs->branches = bl;
629     rcs->nbranches = nbl;
630     rcs->sdelta = sdelta;
631     rcs->nsdelta = nsdelta;
632     rcs->sdtext = sdtext;
633     rcs->nsdtext = nsdtext;
634     rcs->active = bl[0];
635     return 1;
636     }
637    
638     /*
639     **************************************************************************
640     * Assign the symbolic tags to the revisions and branches
641     *
642     * The tags point to revision numbers somewhere in the tree structure
643     * of branches and revisions. First we make a sorted list of all
644     * revisions and then we assign each tag to the proper revision.
645     **************************************************************************
646     */
647 bertho 1.19 static int sort_revision(const void *r1, const void *r2)
648 bertho 1.7 {
649     return compare_rev(0, (*(revision_t **)r1)->delta->rev, (*(revision_t **)r2)->delta->rev);
650     }
651    
652 bertho 1.19 static int search_revision(const void *t, const void *r)
653 bertho 1.7 {
654     return compare_rev(0, (rev_t *)t, (*(revision_t **)r)->delta->rev);
655     }
656    
657 bertho 1.19 static int sort_branch(const void *b1, const void *b2)
658 bertho 1.7 {
659     return compare_rev(1, (*(branch_t **)b1)->branch, (*(branch_t **)b2)->branch);
660 bertho 1.1 }
661    
662 bertho 1.19 static int search_branch(const void *t, const void *b)
663 bertho 1.1 {
664 bertho 1.7 return compare_rev(1, (rev_t *)t, (*(branch_t **)b)->branch);
665 bertho 1.1 }
666    
667 bertho 1.19 static char *previous_rev(const char *c)
668 bertho 1.1 {
669 bertho 1.7 int dots = count_dots(c);
670     char *cptr;
671     char *r;
672     if(!dots)
673 bertho 1.9 {
674 bertho 1.44 stack_msg(MSG_ERR, "FIXME: previous_rev(\"%s\"): Cannot determine parent branch revision\n", c);
675 bertho 1.7 return xstrdup("1.0"); /* FIXME: don't know what the parent is */
676 bertho 1.9 }
677 bertho 1.7 if(dots & 1)
678     {
679     /* Is is a revision we want the parent of */
680     r = xstrdup(c);
681     cptr = strrchr(r, '.');
682     assert(cptr != NULL);
683     if(dots == 1)
684     {
685 bertho 1.44 stack_msg(MSG_ERR, "FIXME: previous_rev(\"%s\"): Going beyond top-level?\n", c);
686 bertho 1.7 /* FIXME: What is the parent of 1.1? */
687     cptr[1] = '\0';
688     strcat(r, "0");
689     return r;
690     }
691     /* Here we have a "x.x[.x.x]+" case */
692     *cptr = '\0';
693     cptr = strrchr(r, '.');
694     assert(cptr != NULL);
695     *cptr = '\0';
696     return r;
697     }
698     /* It is a branch we want the parent of */
699     r = xstrdup(c);
700     cptr = strrchr(r, '.');
701     assert(cptr != NULL);
702     *cptr = '\0';
703     return r;
704 bertho 1.1 }
705    
706 bertho 1.30 static char *build_regex(size_t n, regmatch_t *m, const char *ms)
707     {
708     char *cptr;
709     int i;
710    
711     if(!conf.merge_to || !conf.merge_to[0])
712     return NULL;
713    
714     zap_string();
715     for(cptr = conf.merge_to; *cptr; cptr++)
716     {
717     if(*cptr == '%')
718     {
719     if(cptr[1] >= '1' && cptr[1] <= '9')
720     {
721     int idx = cptr[1] - '0';
722     regmatch_t *p = &m[idx];
723     if(idx < n && !(p->rm_so == -1 || p->rm_so >= p->rm_eo))
724     {
725     for(i = p->rm_so; i < p->rm_eo; i++)
726     {
727     if(strchr("^$.*+\\[{()", ms[i]))
728     add_string_ch('\\');
729     add_string_ch(ms[i]);
730     }
731     }
732     cptr++;
733     }
734     else
735     add_string_ch('%');
736     }
737     else
738     add_string_ch(*cptr);
739     }
740     return dup_string();
741     }
742    
743 bertho 1.49 static void find_merges_cvsnt(rcsfile_t *rcs)
744     {
745     int i;
746    
747     if(!conf.merge_cvsnt)
748     return;
749    
750     for(i = 0; i < rcs->nsrev; i++)
751     {
752     revision_t **r;
753    
754     if(!rcs->srev[i]->delta->mergepoint)
755     continue;
756    
757     r = bsearch(rcs->srev[i]->delta->mergepoint->rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
758     if(!r)
759     continue;
760     rcs->merges = xrealloc(rcs->merges, sizeof(rcs->merges[0]) * (rcs->nmerges+1));
761     rcs->merges[rcs->nmerges].type = TR_REVISION;
762     rcs->merges[rcs->nmerges].from.rev = *r;
763     rcs->merges[rcs->nmerges].to.rev = rcs->srev[i];
764     rcs->nmerges++;
765 bertho 1.52 (*r)->stripped = -1;
766     rcs->srev[i]->stripped = -1;
767 bertho 1.49 }
768     }
769    
770 bertho 1.31 static void find_merges(rcsfile_t *rcs)
771 bertho 1.1 {
772     int i;
773 bertho 1.30 int err;
774 bertho 1.31 int rcflags = REG_EXTENDED | (conf.merge_nocase ? REG_ICASE : 0);
775 bertho 1.30 regex_t *refrom = NULL;
776     regex_t *reto = NULL;
777     regmatch_t *matchfrom = NULL;
778    
779 bertho 1.31 if(!conf.merge_from || !conf.merge_from[0] || !conf.merge_to || !conf.merge_to[0])
780     return;
781    
782     refrom = xmalloc(sizeof(*refrom));
783     reto = xmalloc(sizeof(*reto));
784    
785     /* Compile the 'from' regex match for merge identification */
786     err = regcomp(refrom, conf.merge_from, rcflags);
787     if(err)
788 bertho 1.30 {
789 bertho 1.44 char *msg;
790     i = regerror(err, refrom, NULL, 0);
791     msg = xmalloc(i+1);
792     regerror(err, refrom, msg, i+1);
793     stack_msg(MSG_WARN, "%s", msg);
794     xfree(msg);
795 bertho 1.31 xfree(refrom);
796     xfree(reto);
797     return;
798     }
799     else
800     matchfrom = xmalloc((refrom->re_nsub+1) * sizeof(*matchfrom));
801 bertho 1.30
802 bertho 1.31 for(i = 0; i < rcs->tags->ntags; i++)
803     {
804     tag_t *t = rcs->tags->tags[i];
805    
806     /* Must be revision tags and not detached */
807     if(t->rev->isbranch || !t->logrev)
808     continue;
809    
810     /* Try to find merge tag matches */
811     if(!regexec(refrom, t->tag, refrom->re_nsub+1, matchfrom, 0))
812     {
813     int n;
814     char *to;
815    
816     to = build_regex(refrom->re_nsub+1, matchfrom, t->tag);
817     if(to)
818     {
819     err = regcomp(reto, to, rcflags);
820 bertho 1.44 if(err)
821 bertho 1.31 {
822     char *msg;
823     i = regerror(err, reto, NULL, 0);
824     msg = xmalloc(i+1);
825     regerror(err, reto, msg, i+1);
826 bertho 1.44 stack_msg(MSG_WARN, "%s", msg);
827     xfree(msg);
828 bertho 1.31 }
829     else if(!err)
830     {
831     for(n = 0; n < rcs->tags->ntags; n++)
832     {
833     tag_t *nt = rcs->tags->tags[n];
834     /* From and To never should match the same tag or belong to a branch */
835     if(n == i || nt->rev->isbranch || !nt->logrev)
836     continue;
837    
838 bertho 1.36 if(!regexec(reto, nt->tag, 0, NULL, 0))
839 bertho 1.31 {
840     /* Tag matches */
841     rcs->merges = xrealloc(rcs->merges,
842     sizeof(rcs->merges[0]) * (rcs->nmerges+1));
843 bertho 1.49 rcs->merges[rcs->nmerges].type = TR_TAG;
844     rcs->merges[rcs->nmerges].to.tag = nt;
845     rcs->merges[rcs->nmerges].from.tag = t;
846 bertho 1.31 rcs->nmerges++;
847 bertho 1.46 if(!conf.tag_ignore_merge)
848     {
849     nt->ignore = 0;
850     t->ignore = 0;
851     }
852 bertho 1.31 /* We cannot (should not) match multiple times */
853 bertho 1.46 if(!conf.merge_findall)
854     break;
855 bertho 1.31 }
856     }
857     regfree(reto);
858     }
859     xfree(to);
860     }
861 bertho 1.30 }
862     }
863 bertho 1.31 if(matchfrom) xfree(matchfrom);
864     if(refrom) { regfree(refrom); xfree(refrom); }
865     if(reto) xfree(reto);
866     }
867    
868     static void assign_tags(rcsfile_t *rcs)
869     {
870     int i;
871     int nr;
872 bertho 1.40 regex_t *regextag = NULL;
873    
874     if(conf.tag_ignore && conf.tag_ignore[0])
875     {
876     int err;
877     regextag = xmalloc(sizeof(*regextag));
878     err = regcomp(regextag, conf.tag_ignore, REG_EXTENDED | REG_NOSUB | (conf.tag_nocase ? REG_ICASE : 0));
879     if(err)
880     {
881 bertho 1.44 char *msg;
882     i = regerror(err, regextag, NULL, 0);
883     msg = xmalloc(i+1);
884     regerror(err, regextag, msg, i+1);
885     stack_msg(MSG_WARN, "%s", msg);
886     xfree(msg);
887 bertho 1.40 xfree(regextag);
888     regextag = NULL;
889     }
890     }
891 bertho 1.7
892     for(i = nr = 0; i < rcs->nbranches; i++)
893     nr += rcs->branches[i]->nrevs;
894    
895     rcs->srev = xmalloc(nr * sizeof(rcs->srev[0]));
896     rcs->nsrev = nr;
897     for(i = nr = 0; i < rcs->nbranches; i++)
898     {
899     memcpy(&rcs->srev[nr], rcs->branches[i]->revs, rcs->branches[i]->nrevs * sizeof(rcs->branches[i]->revs[0]));
900     nr += rcs->branches[i]->nrevs;
901     }
902    
903     qsort(rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), sort_revision);
904     qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
905    
906     if(!rcs->branch)
907     {
908     /* The main trunk is the active trunk */
909     rcs->tags->tags = xrealloc(rcs->tags->tags, (rcs->tags->ntags+1)*sizeof(rcs->tags->tags[0]));
910     rcs->tags->tags[rcs->tags->ntags] = xmalloc(sizeof(tag_t));
911     rcs->tags->tags[rcs->tags->ntags]->tag = xstrdup("MAIN");
912     rcs->tags->tags[rcs->tags->ntags]->rev = xmalloc(sizeof(rev_t));
913     rcs->tags->tags[rcs->tags->ntags]->rev->rev = xstrdup(rcs->active->branch->rev);
914     rcs->tags->tags[rcs->tags->ntags]->rev->branch = xstrdup(rcs->active->branch->branch);
915     rcs->tags->tags[rcs->tags->ntags]->rev->isbranch = 1;
916     rcs->tags->ntags++;
917     }
918    
919     /* We should have at least two tags (HEAD and MAIN) */
920 bertho 1.44 assert(rcs->tags != NULL);
921 bertho 1.7
922     for(i = 0; i < rcs->tags->ntags; i++)
923     {
924     tag_t *t = rcs->tags->tags[i];
925     if(t->rev->isbranch)
926     {
927     branch_t **b;
928     add_btag:
929     b = bsearch(t->rev, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
930     if(!b)
931     {
932     rev_t rev;
933     revision_t **r;
934     /* This happens for the magic branch numbers if there are
935 bertho 1.12 * no commits within the new branch yet. So, we add the
936     * branch and try to continue.
937 bertho 1.7 */
938     rev.rev = previous_rev(t->rev->branch);
939     rev.branch = NULL;
940     rev.isbranch = 0;
941     r = bsearch(&rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
942     xfree(rev.rev);
943     if(!r)
944     {
945 bertho 1.44 stack_msg(MSG_WARN, "No branch found for tag '%s:%s'", t->tag, t->rev->branch);
946 bertho 1.7 }
947     else
948     {
949     rcs->branches = xrealloc(rcs->branches, (rcs->nbranches+1)*sizeof(rcs->branches[0]));
950     rcs->branches[rcs->nbranches] = xmalloc(sizeof(branch_t));
951     rcs->branches[rcs->nbranches]->branch = dup_rev(t->rev);
952     rcs->branches[rcs->nbranches]->branchpoint = *r;
953     (*r)->branches = xrealloc((*r)->branches, ((*r)->nbranches+1)*sizeof((*r)->branches[0]));
954     (*r)->branches[(*r)->nbranches] = rcs->branches[rcs->nbranches];
955     (*r)->nbranches++;
956     rcs->nbranches++;
957     /* Resort the branches */
958     qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
959     goto add_btag;
960     }
961     }
962     else
963     {
964     branch_t *bb = *b;
965     bb->tags = xrealloc(bb->tags, (bb->ntags+1)*sizeof(bb->tags[0]));
966     bb->tags[bb->ntags] = t;
967     bb->ntags++;
968     }
969     }
970     else
971     {
972     revision_t **r = bsearch(t->rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
973     if(!r)
974 bertho 1.12 {
975 bertho 1.44 stack_msg(MSG_WARN, "No revision found for tag '%s:%s'\n", t->tag, t->rev->rev);
976 bertho 1.12 }
977 bertho 1.7 else
978     {
979     revision_t *rr = *r;
980 bertho 1.30 t->logrev = rr;
981 bertho 1.28 if(!conf.rev_maxtags || rr->ntags <= conf.rev_maxtags)
982     {
983     rr->tags = xrealloc(rr->tags, (rr->ntags+1)*sizeof(rr->tags[0]));
984     if(conf.rev_maxtags && rr->ntags == conf.rev_maxtags)
985     {
986     rr->tags[rr->ntags] = xmalloc(sizeof(tag_t));
987     rr->tags[rr->ntags]->tag = xstrdup("...");
988     rr->tags[rr->ntags]->rev = t->rev;
989     }
990     else
991     rr->tags[rr->ntags] = t;
992     rr->ntags++;
993     }
994 bertho 1.7 }
995 bertho 1.40
996 bertho 1.42 if(conf.tag_negate)
997 bertho 1.43 t->ignore++;
998 bertho 1.40 /* Mark the tag ignored if it matches the configuration */
999     if(regextag && !regexec(regextag, t->tag, 0, NULL, 0))
1000 bertho 1.42 {
1001     if(conf.tag_negate)
1002     t->ignore--;
1003     else
1004     t->ignore++;
1005     }
1006 bertho 1.7 }
1007     }
1008    
1009     /* We need to reset the first in the list of branches to the
1010     * active branch to ensure the drawing of all
1011     */
1012     if(rcs->active != rcs->branches[0])
1013 bertho 1.1 {
1014 bertho 1.7 branch_t **b = bsearch(rcs->active->branch, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
1015     branch_t *t;
1016     assert(b != NULL);
1017     t = *b;
1018     *b = rcs->branches[0];
1019     rcs->branches[0] = t;
1020 bertho 1.1 }
1021 bertho 1.40
1022     if(regextag)
1023     {
1024     regfree(regextag);
1025     xfree(regextag);
1026     }
1027 bertho 1.1 }
1028    
1029     /*
1030     **************************************************************************
1031 bertho 1.8 * String expansion
1032     **************************************************************************
1033     */
1034     static char *_string;
1035     static int _nstring;
1036     static int _nastring;
1037    
1038 bertho 1.30 static void zap_string(void)
1039     {
1040     _nstring = 0;
1041     if(_string)
1042     _string[0] = '\0';
1043     }
1044    
1045     static char *dup_string(void)
1046     {
1047     if(_string)
1048     return xstrdup(_string);
1049     else
1050     return "";
1051     }
1052    
1053 bertho 1.19 static void add_string_str(const char *s)
1054 bertho 1.8 {
1055     int l = strlen(s) + 1;
1056     if(_nstring + l > _nastring)
1057     {
1058 bertho 1.24 _nastring += MAX(128, l);
1059 bertho 1.8 _string = xrealloc(_string, _nastring * sizeof(_string[0]));
1060     }
1061     memcpy(_string+_nstring, s, l);
1062     _nstring += l-1;
1063     }
1064    
1065 bertho 1.19 static void add_string_ch(int ch)
1066 bertho 1.8 {
1067     char buf[2];
1068     buf[0] = ch;
1069     buf[1] = '\0';
1070     add_string_str(buf);
1071     }
1072    
1073 bertho 1.19 static void add_string_date(const char *d)
1074 bertho 1.9 {
1075     struct tm tm, *tmp;
1076     int n;
1077     time_t t;
1078     char *buf;
1079     int nbuf;
1080 bertho 1.48 char *env;
1081 bertho 1.9
1082     memset(&tm, 0, sizeof(tm));
1083     n = sscanf(d, "%d.%d.%d.%d.%d.%d",
1084     &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
1085     &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
1086     tm.tm_mon--;
1087     if(tm.tm_year > 1900)
1088     tm.tm_year -= 1900;
1089 bertho 1.48
1090     env = getenv("TZ");
1091     putenv("TZ=UTC0");
1092     t = mktime(&tm);
1093     if(env)
1094 bertho 1.53 {
1095     char *c = xmalloc(strlen(env) + 3 + 1); /* Extra space for TZ and = */
1096     sprintf(c, "TZ=%s", env);
1097     putenv(c);
1098     xfree(c);
1099     }
1100 bertho 1.48 else
1101 bertho 1.53 putenv("TZ");
1102 bertho 1.48
1103 bertho 1.9 if(n != 6 || t == (time_t)(-1))
1104     {
1105     add_string_str("<invalid date>");
1106     return;
1107     }
1108    
1109     tmp = localtime(&t);
1110 bertho 1.51 nbuf = (strlen(conf.date_format)+1) * 16; /* Should be enough to hold all types of expansions */
1111 bertho 1.9 buf = xmalloc(nbuf);
1112     strftime(buf, nbuf, conf.date_format, tmp);
1113     add_string_str(buf);
1114     xfree(buf);
1115     }
1116    
1117 bertho 1.22 static void add_string_str_html(const char *s, int maxlen)
1118     {
1119     int l = 0;
1120     char *str = xmalloc(6 * strlen(s) + 1); /* Should hold all char entity-expand */
1121     char *cptr = str;
1122     for(; *s; s++)
1123     {
1124     if(maxlen && l > abs(maxlen))
1125     {
1126     cptr += sprintf(cptr, "...");
1127     break;
1128     }
1129     if(*s < 0x20)
1130     {
1131     if(*s == '\n')
1132     {
1133     if(maxlen < 0)
1134     *cptr++ = ' ';
1135     else
1136 bertho 1.37 cptr += sprintf(cptr, "<br%s>", conf.html_level == HTMLLEVEL_X ? " /" : "");
1137 bertho 1.22 }
1138     }
1139 bertho 1.30 else if(*s >= 0x7f || *s == '"')
1140 bertho 1.22 cptr += sprintf(cptr, "&#%d;", (int)(unsigned char)*s);
1141     else if(*s == '<')
1142     cptr += sprintf(cptr, "&lt;");
1143     else if(*s == '>')
1144     cptr += sprintf(cptr, "&gt;");
1145 bertho 1.37 else if(*s == '&')
1146     cptr += sprintf(cptr, "&amp;");
1147     else if(*s == '"')
1148     cptr += sprintf(cptr, "&quot;");
1149 bertho 1.22 else
1150     *cptr++ = *s;
1151     l++;
1152     }
1153     *cptr = '\0';
1154     add_string_str(str);
1155     xfree(str);
1156     }
1157    
1158     static void add_string_str_len(const char *s, int maxlen)
1159     {
1160     int l = strlen(s);
1161     char *str = xmalloc(l + 1 + 3);
1162     strcpy(str, s);
1163     if(maxlen < l)
1164     sprintf(&str[maxlen], "...");
1165     add_string_str(str);
1166     xfree(str);
1167     }
1168    
1169 bertho 1.44 static char *expand_string(const char *s, rcsfile_t *rcs, revision_t *r, rev_t *rev, rev_t *prev, tag_t *tag)
1170 bertho 1.8 {
1171     char nb[32];
1172     char nr[32];
1173     char *base;
1174 bertho 1.30 char *exp;
1175 bertho 1.22 int l;
1176     char ch;
1177 bertho 1.8
1178     if(!s)
1179     return xstrdup("");
1180    
1181 bertho 1.30 zap_string();
1182 bertho 1.8
1183 bertho 1.54 sprintf(nb, "%d", rcs->nbranches + rcs->nfolds);
1184 bertho 1.8 sprintf(nr, "%d", rcs->nsrev);
1185     for(; *s; s++)
1186     {
1187     if(*s == '%')
1188     {
1189     switch(*++s)
1190     {
1191 bertho 1.12 case 'c':
1192     case 'C':
1193     add_string_str(conf.cvsroot);
1194     if(*s == 'C' && conf.cvsroot[0] && conf.cvsroot[strlen(conf.cvsroot)-1] == '/')
1195     {
1196     /* Strip the trailing '/' */
1197     _nstring--;
1198     _string[_nstring] = '\0';
1199     }
1200     break;
1201 bertho 1.8 case 'f':
1202     case 'F':
1203     base = strrchr(rcs->file, '/');
1204     if(!base)
1205     add_string_str(rcs->file);
1206     else
1207     add_string_str(base+1);
1208     if(*s == 'F' && _string[_nstring-1] == 'v' && _string[_nstring-2] == ',')
1209     {
1210     _nstring -= 2;
1211     _string[_nstring] = '\0';
1212     }
1213     break;
1214     case 'p':
1215     base = strrchr(rcs->file, '/');
1216     if(base)
1217     {
1218     char *c = xstrdup(rcs->file);
1219     base = strrchr(c, '/');
1220     assert(base != NULL);
1221     base[1] = '\0';
1222     add_string_str(c);
1223     xfree(c);
1224     }
1225 bertho 1.9 /*
1226     * We should not add anything here because we can encounter
1227     * a completely empty path, in which case we do not want
1228 bertho 1.17 * to add any slash. This prevents an inadvertent root redirect.
1229 bertho 1.9 *
1230     * else
1231     * add_string_str("/");
1232     */
1233 bertho 1.8 break;
1234 bertho 1.12 case 'm':
1235     case 'M':
1236     add_string_str(conf.cvsmodule);
1237     if(*s == 'M' && conf.cvsmodule[0] && conf.cvsmodule[strlen(conf.cvsmodule)-1] == '/')
1238     {
1239     /* Strip the trailing '/' */
1240     _nstring--;
1241     _string[_nstring] = '\0';
1242     }
1243     break;
1244 bertho 1.8 case 'r': add_string_str(nr); break;
1245     case 'b': add_string_str(nb); break;
1246     case '%': add_string_ch('%'); break;
1247     case '0': if(conf.expand[0]) add_string_str(conf.expand[0]); break;
1248     case '1': if(conf.expand[1]) add_string_str(conf.expand[1]); break;
1249     case '2': if(conf.expand[2]) add_string_str(conf.expand[2]); break;
1250     case '3': if(conf.expand[3]) add_string_str(conf.expand[3]); break;
1251     case '4': if(conf.expand[4]) add_string_str(conf.expand[4]); break;
1252     case '5': if(conf.expand[5]) add_string_str(conf.expand[5]); break;
1253     case '6': if(conf.expand[6]) add_string_str(conf.expand[6]); break;
1254     case '7': if(conf.expand[7]) add_string_str(conf.expand[7]); break;
1255     case '8': if(conf.expand[8]) add_string_str(conf.expand[8]); break;
1256     case '9': if(conf.expand[9]) add_string_str(conf.expand[9]); break;
1257     case 'R': if(rev && rev->rev) add_string_str(rev->rev); break;
1258 bertho 1.9 case 'P': if(prev && prev->rev) add_string_str(prev->rev); break;
1259 bertho 1.8 case 'B': if(rev && rev->branch) add_string_str(rev->branch); break;
1260     case 't': if(tag && tag->tag) add_string_str(tag->tag); break;
1261 bertho 1.9 case 'd': if(r && r->delta && r->delta->date) add_string_date(r->delta->date); break;
1262     case 's': if(r && r->delta && r->delta->state) add_string_str(r->delta->state); break;
1263     case 'a': if(r && r->delta && r->delta->author) add_string_str(r->delta->author); break;
1264 bertho 1.22 case 'L':
1265     case 'l':
1266     ch = *s;
1267     l = 0;
1268     if(s[1] == '[')
1269     {
1270     char *cptr = strchr(s, ']');
1271     char *eptr;
1272     if(cptr)
1273     {
1274     l = strtol(&s[2], &eptr, 10);
1275     if(eptr != cptr)
1276     l = 0;
1277     else
1278     s = cptr;
1279     }
1280     }
1281     if(!conf.parse_logs)
1282     add_string_str("N/A");
1283     else if(r && r->dtext && r->dtext->log)
1284     {
1285     if(ch == 'l')
1286     add_string_str_html(r->dtext->log, l);
1287     else
1288     add_string_str_len(r->dtext->log, abs(l));
1289     }
1290     break;
1291 bertho 1.30 case '(':
1292     base = dup_string();
1293     exp = expand_string(s+1, rcs, r, rev, prev, tag);
1294     zap_string();
1295     add_string_str(base);
1296     add_string_str_html(exp, 0);
1297     xfree(base);
1298     xfree(exp);
1299     /* Find the %) in this recursion level */
1300     for(; *s; s++)
1301     {
1302     if(*s == '%' && s[1] == ')')
1303 bertho 1.34 {
1304     s++;
1305 bertho 1.30 break;
1306 bertho 1.34 }
1307 bertho 1.30 }
1308     if(!*s)
1309     {
1310     s--; /* To end outer loop */
1311 bertho 1.44 stack_msg(MSG_WARN, "string expand: Missing %%) in expansion");
1312 bertho 1.30 }
1313     break;
1314     case ')':
1315     return dup_string();
1316 bertho 1.8 default:
1317     add_string_ch('%');
1318     add_string_ch(*s);
1319     break;
1320     }
1321     }
1322     else
1323     add_string_ch(*s);
1324     }
1325 bertho 1.30 return dup_string();
1326 bertho 1.8 }
1327    
1328     /*
1329     **************************************************************************
1330 bertho 1.1 * Drawing routines
1331     **************************************************************************
1332     */
1333 bertho 1.19 static int get_swidth(const char *s, font_t *f)
1334 bertho 1.1 {
1335 bertho 1.9 int n;
1336     int m;
1337     if(!s || !*s)
1338 bertho 1.1 return 0;
1339 bertho 1.19
1340     #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1341     if(conf.use_ttf && f->ttfont)
1342     {
1343     int bb[8];
1344     char *e;
1345     #ifdef HAVE_GDIMAGESTRINGFT
1346     e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1347     #else
1348     e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1349     #endif
1350     if(!e)
1351     return bb[2] - bb[6];
1352     }
1353     #endif
1354 bertho 1.9 for(n = m = 0; *s; n++, s++)
1355     {
1356     if(*s == '\n')
1357     {
1358     if(n > m)
1359     m = n;
1360     n = 0;
1361     }
1362     }
1363     if(n > m)
1364     m = n;
1365 bertho 1.22 return f->gdfont ? m * f->gdfont->w : m;
1366 bertho 1.1 }
1367    
1368 bertho 1.19 static int get_sheight(const char *s, font_t *f)
1369 bertho 1.1 {
1370     int nl;
1371 bertho 1.9 if(!s || !*s)
1372 bertho 1.1 return 0;
1373 bertho 1.19
1374     #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1375     if(conf.use_ttf && f->ttfont)
1376     {
1377     int bb[8];
1378     char *e;
1379     #ifdef HAVE_GDIMAGESTRINGFT
1380     e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1381     #else
1382     e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1383     #endif
1384     if(!e)
1385     return bb[3] - bb[7] + 4;
1386     }
1387     #endif
1388 bertho 1.1 for(nl = 1; *s; s++)
1389     {
1390     if(*s == '\n' && s[1])
1391     nl++;
1392     }
1393 bertho 1.19 return nl * f->gdfont->h;
1394 bertho 1.1 }
1395    
1396 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)
1397 bertho 1.1 {
1398     int r2 = 2*r;
1399 bertho 1.44 if(!r)
1400     gdImageFilledRectangle(im, x1, y1, x2, y2, bgcolor->id);
1401     #ifdef HAVE_GDIMAGEFILLEDARC
1402     else
1403     {
1404     gdImageFilledArc(im, x1+r, y1+r, r2, r2, 180, 270, bgcolor->id, gdArc);
1405     gdImageFilledArc(im, x2-r, y1+r, r2, r2, 270, 360, bgcolor->id, gdArc);
1406     gdImageFilledArc(im, x1+r, y2-r, r2, r2, 90, 180, bgcolor->id, gdArc);
1407     gdImageFilledArc(im, x2-r, y2-r, r2, r2, 0, 90, bgcolor->id, gdArc);
1408     gdImageFilledRectangle(im, x1+r, y1, x2-r, y1+r, bgcolor->id);
1409     gdImageFilledRectangle(im, x1, y1+r, x2, y2-r, bgcolor->id);
1410     gdImageFilledRectangle(im, x1+r, y2-r, x2-r, y2, bgcolor->id);
1411     }
1412     #endif
1413 bertho 1.1 gdImageLine(im, x1+r, y1, x2-r, y1, color->id);
1414     gdImageLine(im, x1+r, y2, x2-r, y2, color->id);
1415     gdImageLine(im, x1, y1+r, x1, y2-r, color->id);
1416     gdImageLine(im, x2, y1+r, x2, y2-r, color->id);
1417 bertho 1.9 if(conf.box_shadow)
1418     {
1419     gdImageLine(im, x1+r+1, y2+1, x2-r, y2+1, black_color.id);
1420     gdImageLine(im, x2+1, y1+r+1, x2+1, y2-r, black_color.id);
1421     }
1422 bertho 1.1 if(r)
1423     {
1424 bertho 1.18 /* FIXME: Pixelization is not perfect */
1425 bertho 1.1 gdImageArc(im, x1+r, y1+r, r2, r2, 180, 270, color->id);
1426     gdImageArc(im, x2-r, y1+r, r2, r2, 270, 360, color->id);
1427     gdImageArc(im, x1+r, y2-r, r2, r2, 90, 180, color->id);
1428 bertho 1.9 if(conf.box_shadow)
1429     {
1430     gdImageArc(im, x2-r+1, y2-r+1, r2, r2, 0, 90, black_color.id);
1431 bertho 1.18 gdImageArc(im, x2-r+1, y2-r, r2, r2, 0, 90, black_color.id);
1432     gdImageArc(im, x2-r, y2-r+1, r2, r2, 0, 90, black_color.id);
1433 bertho 1.9 }
1434 bertho 1.18 gdImageArc(im, x2-r, y2-r, r2, r2, 0, 90, color->id);
1435 bertho 1.44 #if !defined(NOGDFILL) && !defined(HAVE_GDIMAGEFILLEDARC)
1436     /* BUG: We clip manually because libgd segfaults on out of bound values */
1437     if((x1+x2)/2 >= 0 && (x1+x2)/2 < gdImageSX(im) && (y1+y2)/2 >= 0 && (y1+y2)/2 < gdImageSY(im))
1438     gdImageFillToBorder(im, (x1+x2)/2, (y1+y2)/2, color->id, bgcolor->id);
1439     #endif
1440 bertho 1.1 }
1441     }
1442    
1443 bertho 1.19 static void draw_string(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1444 bertho 1.1 {
1445 bertho 1.19 int h = get_sheight(s, f);
1446 bertho 1.1 int xx, yy;
1447     switch(align & ALIGN_HX)
1448     {
1449     default:
1450     case ALIGN_HL: xx = 0; break;
1451     case ALIGN_HC: xx = -get_swidth(s, f)/2; break;
1452     case ALIGN_HR: xx = -get_swidth(s, f); break;
1453     }
1454     switch(align & ALIGN_VX)
1455     {
1456     default:
1457     case ALIGN_VT: yy = 0; break;
1458 bertho 1.19 case ALIGN_VC: yy = h/2; break;
1459     case ALIGN_VB: yy = h; break;
1460 bertho 1.1 }
1461 bertho 1.19 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1462     if(conf.use_ttf && f->ttfont)
1463     {
1464     int bb[8];
1465     char *e;
1466     int cid = conf.anti_alias ? c->id : -c->id;
1467     #ifdef HAVE_GDIMAGESTRINGFT
1468     e = gdImageStringFT(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1469     #else
1470     e = gdImageStringTTF(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1471     #endif
1472     if(!e)
1473     return;
1474     }
1475     #endif
1476     yy = -yy;
1477     gdImageString(im, f->gdfont, x+xx+1, y+yy, s, c->id);
1478 bertho 1.1 }
1479    
1480 bertho 1.19 static void draw_stringnl(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1481 bertho 1.9 {
1482     char *t;
1483     char *d;
1484     d = s = xstrdup(s);
1485     do
1486     {
1487     t = strchr(s, '\n');
1488     if(t)
1489     *t = '\0';
1490     draw_string(im, s, f, x, y, align, c);
1491     y += get_sheight(s, f);
1492     s = t+1;
1493     } while(t);
1494     xfree(d);
1495     }
1496    
1497 bertho 1.19 static void draw_rev(gdImagePtr im, revision_t *r)
1498 bertho 1.1 {
1499 bertho 1.25 int lx;
1500     int rx;
1501     int x2;
1502 bertho 1.1 int i;
1503 bertho 1.25 int ty;
1504    
1505     if(conf.left_right)
1506     {
1507     lx = r->cx;
1508     rx = r->cx + r->w;
1509     ty = r->y - r->h/2;
1510     x2 = r->cx + r->w/2;
1511     }
1512     else
1513     {
1514     lx = r->cx - r->w/2;
1515     rx = lx + r->w;
1516     ty = r->y;
1517     x2 = r->cx;
1518     }
1519 bertho 1.9 draw_rbox(im, lx, ty, rx, ty+r->h, 0, &conf.rev_color, &conf.rev_bgcolor);
1520 bertho 1.1 ty += conf.rev_tspace;
1521 bertho 1.46 if(!conf.rev_hidenumber)
1522     {
1523     draw_string(im, r->rev->rev, &conf.rev_font, x2, ty, ALIGN_HC, &conf.rev_color);
1524     ty += get_sheight(r->rev->rev, &conf.rev_font);
1525     }
1526 bertho 1.25 draw_stringnl(im, r->revtext, &conf.rev_text_font, x2, ty, ALIGN_HC, &conf.rev_text_color);
1527 bertho 1.9 ty += get_sheight(r->revtext, &conf.rev_text_font);
1528 bertho 1.1 for(i = 0; i < r->ntags; i++)
1529     {
1530 bertho 1.25 draw_string(im, r->tags[i]->tag, &conf.tag_font, x2, ty, ALIGN_HC, &conf.tag_color);
1531 bertho 1.19 ty += get_sheight(r->tags[i]->tag, &conf.tag_font) + conf.rev_separator;
1532 bertho 1.1 }
1533     }
1534    
1535 bertho 1.26 static void draw_branch_box(gdImagePtr im, branch_t *b, int xp, int yp)
1536 bertho 1.1 {
1537 bertho 1.25 int lx;
1538     int rx;
1539 bertho 1.16 int i;
1540 bertho 1.1 int yy;
1541 bertho 1.25 int x2;
1542 bertho 1.16
1543 bertho 1.25 if(conf.left_right)
1544     {
1545     lx = b->cx;
1546     rx = lx + b->w;
1547     x2 = b->cx + b->w/2;
1548     }
1549     else
1550     {
1551     lx = b->cx - b->w/2;
1552     rx = lx + b->w;
1553     x2 = b->cx;
1554     }
1555 bertho 1.26 draw_rbox(im, lx+xp, yp, rx+xp, yp+b->h, 5, &conf.branch_color, &conf.branch_bgcolor);
1556 bertho 1.1 yy = conf.branch_tspace;
1557 bertho 1.32 if(!b->nfolds)
1558 bertho 1.1 {
1559 bertho 1.46 if(!conf.rev_hidenumber)
1560     {
1561     draw_string(im, b->branch->branch, &conf.branch_font, x2+xp, yp+yy, ALIGN_HC, &conf.branch_color);
1562     yy += get_sheight(b->branch->branch, &conf.branch_font);
1563     }
1564 bertho 1.32 for(i = 0; i < b->ntags; i++)
1565     {
1566     draw_string(im, b->tags[i]->tag, &conf.branch_tag_font, x2+xp, yp+yy, ALIGN_HC, &conf.branch_tag_color);
1567 bertho 1.34 yy += get_sheight(b->tags[i]->tag, &conf.branch_tag_font);
1568 bertho 1.32 }
1569     }
1570     else
1571     {
1572     int y1, y2;
1573     int tx = lx + b->fw + conf.branch_lspace;
1574     int nx = tx - get_swidth(" ", &conf.branch_font);
1575     draw_string(im, b->branch->branch, &conf.branch_font, nx+xp, yp+yy, ALIGN_HR, &conf.branch_color);
1576     y1 = get_sheight(b->branch->branch, &conf.branch_font);
1577     draw_string(im, b->tags[0]->tag, &conf.branch_tag_font, tx+xp, yp+yy, ALIGN_HL, &conf.branch_tag_color);
1578     y2 = get_sheight(b->tags[0]->tag, &conf.branch_font);
1579     yy += MAX(y1, y2);
1580     for(i = 0; i < b->nfolds; i++)
1581     {
1582     draw_string(im, b->folds[i]->branch->branch, &conf.branch_font, nx+xp, yp+yy, ALIGN_HR, &conf.branch_color);
1583     y1 = get_sheight(b->folds[i]->branch->branch, &conf.branch_font);
1584     draw_string(im, b->folds[i]->tags[0]->tag, &conf.branch_tag_font, tx+xp, yp+yy, ALIGN_HL, &conf.branch_tag_color);
1585 bertho 1.34 y2 = get_sheight(b->folds[i]->tags[0]->tag, &conf.branch_tag_font);
1586 bertho 1.32 yy += MAX(y1, y2);
1587     }
1588 bertho 1.1 }
1589 bertho 1.16 }
1590    
1591 bertho 1.19 static void draw_branch(gdImagePtr im, branch_t *b)
1592 bertho 1.16 {
1593 bertho 1.25 int yy, xx;
1594 bertho 1.16 int i;
1595 bertho 1.17 int line[4];
1596 bertho 1.20 int l;
1597     int sign;
1598 bertho 1.17
1599     line[0] = conf.rev_color.id;
1600     line[1] = gdTransparent;
1601     line[1] = gdTransparent;
1602     line[3] = conf.rev_color.id;
1603    
1604 bertho 1.44 /* Trivial clip the branch */
1605     if(conf.left_right)
1606     {
1607     if(b->cx > gdImageSX(im) || b->cx+b->tw < 0 || b->y-b->th/2 > gdImageSY(im) || b->y+b->th/2 < 0)
1608     return;
1609     }
1610     else
1611     {
1612     if(b->cx-b->tw/2 > gdImageSX(im) || b->cx+b->tw/2 < 0 || b->y > gdImageSY(im) || b->y+b->th < 0)
1613     return;
1614     }
1615    
1616 bertho 1.26 draw_branch_box(im, b, 0, conf.left_right ? b->y - b->h/2 : b->y);
1617 bertho 1.1
1618 bertho 1.25 if(conf.left_right)
1619 bertho 1.16 {
1620 bertho 1.25 if(conf.upside_down)
1621 bertho 1.16 {
1622 bertho 1.25 xx = b->cx;
1623     for(i = 0; i < b->nrevs; i++)
1624 bertho 1.20 {
1625 bertho 1.25 revision_t *r = b->revs[i];
1626     gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1627     gdImageLine(im, xx, r->y, r->cx+r->w, r->y, gdStyled);
1628     for(sign = l = 1; l < conf.thick_lines; l++)
1629     {
1630     int pp = (l+1)/2*sign;
1631     gdImageLine(im, xx, r->y+pp, r->cx+r->w, r->y+pp, gdStyled);
1632     sign *= -1;
1633     }
1634     draw_rev(im, r);
1635     xx = r->cx;
1636     }
1637 bertho 1.32 if(conf.branch_dupbox && b->nrevs)
1638 bertho 1.25 {
1639     i = b->cx - b->tw + b->w;
1640 bertho 1.26 gdImageLine(im, xx, b->y, i+b->w, b->y, conf.rev_color.id);
1641 bertho 1.25 for(sign = l = 1; l < conf.thick_lines; l++)
1642     {
1643     int pp = (l+1)/2*sign;
1644 bertho 1.26 gdImageLine(im, xx, b->y+pp, i+b->w, b->y+pp, conf.rev_color.id);
1645 bertho 1.25 sign *= -1;
1646     }
1647 bertho 1.26 draw_branch_box(im, b, i - b->cx, b->y - b->h/2);
1648 bertho 1.20 }
1649 bertho 1.16 }
1650 bertho 1.25 else
1651 bertho 1.16 {
1652 bertho 1.25 xx = b->cx + b->w;
1653     for(i = 0; i < b->nrevs; i++)
1654     {
1655     revision_t *r = b->revs[i];
1656     gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1657     gdImageLine(im, xx, r->y, r->cx, r->y, gdStyled);
1658     for(sign = l = 1; l < conf.thick_lines; l++)
1659     {
1660     int pp = (l+1)/2*sign;
1661     gdImageLine(im, xx, r->y+pp, r->cx, r->y+pp, gdStyled);
1662     sign *= -1;
1663     }
1664     draw_rev(im, r);
1665     xx = r->cx + r->w;
1666     }
1667 bertho 1.32 if(conf.branch_dupbox && b->nrevs)
1668 bertho 1.20 {
1669 bertho 1.25 i = b->cx + b->tw - b->w;
1670     gdImageLine(im, xx, b->y, i, b->y, conf.rev_color.id);
1671     for(sign = l = 1; l < conf.thick_lines; l++)
1672     {
1673     int pp = (l+1)/2*sign;
1674     gdImageLine(im, xx, b->y+pp, i, b->y+pp, conf.rev_color.id);
1675     sign *= -1;
1676     }
1677 bertho 1.26 draw_branch_box(im, b, i - b->cx, b->y - b->h/2);
1678 bertho 1.20 }
1679 bertho 1.16 }
1680     }
1681     else
1682 bertho 1.1 {
1683 bertho 1.25 if(conf.upside_down)
1684 bertho 1.16 {
1685 bertho 1.25 yy = b->y;
1686     for(i = 0; i < b->nrevs; i++)
1687 bertho 1.20 {
1688 bertho 1.25 revision_t *r = b->revs[i];
1689     gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1690     gdImageLine(im, r->cx, yy, r->cx, r->y+r->h, gdStyled);
1691     for(sign = l = 1; l < conf.thick_lines; l++)
1692     {
1693     int pp = (l+1)/2*sign;
1694     gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y+r->h, gdStyled);
1695     sign *= -1;
1696     }
1697     draw_rev(im, r);
1698     yy = r->y;
1699     }
1700 bertho 1.32 if(conf.branch_dupbox && b->nrevs)
1701 bertho 1.25 {
1702     i = b->y - b->th + b->h;
1703     gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1704     for(sign = l = 1; l < conf.thick_lines; l++)
1705     {
1706     int pp = (l+1)/2*sign;
1707     gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, conf.rev_color.id);
1708     sign *= -1;
1709     }
1710 bertho 1.26 draw_branch_box(im, b, 0, i);
1711 bertho 1.20 }
1712 bertho 1.16 }
1713 bertho 1.25 else
1714 bertho 1.16 {
1715 bertho 1.25 yy = b->y + b->h;
1716     for(i = 0; i < b->nrevs; i++)
1717     {
1718     revision_t *r = b->revs[i];
1719     gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1720     gdImageLine(im, r->cx, yy, r->cx, r->y, gdStyled);
1721     for(sign = l = 1; l < conf.thick_lines; l++)
1722     {
1723     int pp = (l+1)/2*sign;
1724     gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y, gdStyled);
1725     sign *= -1;
1726     }
1727     draw_rev(im, r);
1728     yy = r->y + r->h;
1729     }
1730 bertho 1.32 if(conf.branch_dupbox && b->nrevs)
1731 bertho 1.20 {
1732 bertho 1.25 i = b->y + b->th - b->h;
1733     gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1734     for(sign = l = 1; l < conf.thick_lines; l++)
1735     {
1736     int pp = (l+1)/2*sign;
1737     gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, conf.rev_color.id);
1738     sign *= -1;
1739     }
1740 bertho 1.26 draw_branch_box(im, b, 0, i);
1741 bertho 1.20 }
1742 bertho 1.16 }
1743 bertho 1.1 }
1744     }
1745    
1746 bertho 1.19 static void draw_connector(gdImagePtr im, branch_t *b)
1747 bertho 1.1 {
1748 bertho 1.20 int l;
1749     int sign;
1750 bertho 1.1 revision_t *r = b->branchpoint;
1751 bertho 1.7 int x1 = r->cx + r->w/2 + 2;
1752 bertho 1.1 int y1 = r->y + r->h/2;
1753 bertho 1.7 int x2 = b->cx;
1754 bertho 1.1 int y2 = b->y;
1755 bertho 1.25
1756     if(conf.left_right)
1757     {
1758     x2 = r->cx + r->w/2;
1759 bertho 1.26 y2 = r->y + r->h/2 + 3;
1760 bertho 1.25 x1 = b->cx;
1761     y1 = b->y;
1762     if(conf.upside_down)
1763     x1 += b->w;
1764     }
1765     else
1766     {
1767     x1 = r->cx + r->w/2 + 2;
1768     y1 = r->y + r->h/2;
1769     x2 = b->cx;
1770     y2 = b->y;
1771     if(conf.upside_down)
1772     y2 += b->h;
1773     }
1774 bertho 1.1 gdImageLine(im, x1, y1, x2, y1, conf.branch_color.id);
1775     gdImageLine(im, x2, y1, x2, y2, conf.branch_color.id);
1776 bertho 1.20 for(sign = l = 1; l < conf.thick_lines; l++)
1777     {
1778     int pp = (l+1)/2*sign;
1779 bertho 1.26 gdImageLine(im, x1, y1+pp, x2, y1+pp, conf.branch_color.id);
1780     gdImageLine(im, x2+pp, y1, x2+pp, y2, conf.branch_color.id);
1781 bertho 1.20 sign *= -1;
1782     }
1783 bertho 1.1 }
1784    
1785 bertho 1.31 static void draw_merges(gdImagePtr im, rcsfile_t *rcs, int dot)
1786 bertho 1.30 {
1787     int i;
1788     for(i = 0; i < rcs->nmerges; i++)
1789     {
1790 bertho 1.49 revision_t *fr;
1791     revision_t *tr;
1792     int colorid;
1793 bertho 1.30 int x1, x2, y1, y2;
1794 bertho 1.49 switch(rcs->merges[i].type)
1795     {
1796     case TR_TAG:
1797     fr = rcs->merges[i].from.tag->logrev;
1798     tr = rcs->merges[i].to.tag->logrev;
1799     colorid = conf.merge_color.id;
1800     break;
1801     case TR_REVISION:
1802     fr = rcs->merges[i].from.rev;
1803     tr = rcs->merges[i].to.rev;
1804     colorid = conf.merge_cvsnt_color.id;
1805     break;
1806     default:
1807     continue;
1808     }
1809 bertho 1.31 if(!fr || !tr || fr == tr)
1810     continue; /* This can happen with detached tags and self-references */
1811 bertho 1.30 if(conf.left_right)
1812     {
1813 bertho 1.32 if(fr->branch == tr->branch)
1814 bertho 1.30 {
1815 bertho 1.32 y1 = fr->y - fr->h/2;
1816 bertho 1.30 y2 = tr->y - tr->h/2;
1817     }
1818     else
1819     {
1820 bertho 1.32 if(fr->y < tr->y)
1821     {
1822     y1 = fr->y + fr->h/2;
1823     y2 = tr->y - tr->h/2;
1824     }
1825     else
1826     {
1827     y1 = fr->y - fr->h/2;
1828     y2 = tr->y + tr->h/2;
1829     }
1830 bertho 1.30 }
1831     x1 = fr->cx + fr->w/2;
1832     x2 = tr->cx + tr->w/2;
1833     }
1834     else
1835     {
1836 bertho 1.32 if(fr->branch == tr->branch)
1837 bertho 1.30 {
1838 bertho 1.32 x1 = fr->cx - fr->w/2;
1839 bertho 1.30 x2 = tr->cx - tr->w/2;
1840     }
1841     else
1842     {
1843 bertho 1.32 if(fr->cx < tr->cx)
1844     {
1845     x1 = fr->cx + fr->w/2;
1846     x2 = tr->cx - tr->w/2;
1847     }
1848     else
1849     {
1850     x1 = fr->cx - fr->w/2;
1851     x2 = tr->cx + tr->w/2;
1852     }
1853 bertho 1.30 }
1854 bertho 1.49 if(rcs->merges[i].type == TR_TAG)
1855     {
1856     y1 = fr->y + rcs->merges[i].from.tag->yofs;
1857     y2 = tr->y + rcs->merges[i].to.tag->yofs;
1858     }
1859     else
1860     {
1861     y1 = fr->y + fr->h/2;
1862     y2 = tr->y + tr->h/2;
1863     }
1864 bertho 1.30 }
1865 bertho 1.35 if(dot && !conf.merge_arrows)
1866 bertho 1.30 {
1867 bertho 1.31 int o = conf.left_right ? 1 : 0;
1868 bertho 1.49 gdImageArc(im, x2, y2+o, 8, 8, 0, 360, colorid);
1869 bertho 1.44 /* BUG: We clip manually because libgd segfaults on out of bound values */
1870     if(x2+1 >= 0 && x2+1 < gdImageSX(im) && y2+o+1 >= 0 && y2+o+1 < gdImageSY(im))
1871 bertho 1.49 gdImageFillToBorder(im, x2+1, y2+o+1, colorid, colorid);
1872 bertho 1.30 }
1873 bertho 1.35 else if(dot && conf.merge_arrows)
1874     {
1875     /*
1876     * Arrow patch from Haroon Rafique <haroon.rafique@utoronto.ca>
1877     * Slightly adapted to be more configurable.
1878     */
1879     int sx, sy; /* start point coordinates */
1880     int ex, ey; /* end point coordinates */
1881     double theta;
1882     double u1, v1, u2, v2;
1883     gdPoint p[3];
1884    
1885     sx = x1; sy = y1;
1886     ex = x2; ey = y2;
1887     if(conf.left_right)
1888     {
1889     if(fr->branch == tr->branch)
1890     {
1891     int yy = (y1 < y2 ? y1 : y2) - 5;
1892     /* line from (x1,yy) to (x2,yy) */
1893     sy = ey = yy;
1894     }
1895     else
1896     {
1897     if(y1 > y2)
1898     {
1899     /* line from (x1,y1-3) to (x2,y2+3+1) */
1900     sy = y1-3;
1901     ey = y2+3+1;
1902     }
1903     else
1904     {
1905     /* line from (x1,y1+3+1) to (x2,y2-3) */
1906     sy = y1+3+1;
1907     ey = y2-3;
1908     }
1909     }
1910     }
1911     else
1912     {
1913     if(fr->branch == tr->branch)
1914     {
1915     int xx = (x1 < x2 ? x1 : x2) - 5;
1916     /* line from (xx,y1) to (xx,y2) */
1917     sx = ex = xx;
1918     }
1919     else
1920     {
1921     if(x1 > x2)
1922     {
1923     /* line from (x1-3,y1) to (x2+3,y2) */
1924     sx = x1-3;
1925     ex = x2+3;
1926     }
1927     else
1928     {
1929     /* line from (x1+3,y1) to (x2-3,y2) */
1930     sx = x1+3;
1931     ex = x2-3;
1932     }
1933     }
1934     }
1935     /*
1936     * inspiration for arrow code comes from arrows.c in the
1937     * graphviz package. Thank you, AT&T
1938     */
1939     /* theta in radians */
1940     theta = atan2((double)(sy-ey), (double)(sx-ex));
1941     u1 = (double)conf.arrow_length * cos(theta);
1942     v1 = (double)conf.arrow_length * sin(theta);
1943     u2 = (double)conf.arrow_width * cos(theta + M_PI/2.0);
1944     v2 = (double)conf.arrow_width * sin(theta + M_PI/2.0);
1945     /* points of polygon (triangle) */
1946     p[0].x = ROUND(ex + u1 - u2);
1947     p[0].y = ROUND(ey + v1 - v2);
1948     p[1].x = ex;
1949     p[1].y = ey;
1950     p[2].x = ROUND(ex + u1 + u2);
1951     p[2].y = ROUND(ey + v1 + v2);
1952     /* draw the polygon (triangle) */
1953 bertho 1.49 gdImageFilledPolygon(im, p, 3, colorid);
1954 bertho 1.35 }
1955 bertho 1.30 else
1956     {
1957 bertho 1.31 if(conf.left_right)
1958 bertho 1.30 {
1959 bertho 1.32 if(fr->branch == tr->branch)
1960 bertho 1.31 {
1961 bertho 1.32 int yy = (y1 < y2 ? y1 : y2) - 5;
1962 bertho 1.49 gdImageLine(im, x1, y1, x1, yy, colorid);
1963     gdImageLine(im, x2, y2, x2, yy, colorid);
1964     gdImageLine(im, x1, yy, x2, yy, colorid);
1965 bertho 1.31 }
1966     else
1967     {
1968 bertho 1.32 if(y1 > y2)
1969     {
1970 bertho 1.49 gdImageLine(im, x1, y1, x1, y1-3, colorid);
1971     gdImageLine(im, x2, y2+1, x2, y2+3+1, colorid);
1972     gdImageLine(im, x1, y1-3, x2, y2+3+1, colorid);
1973 bertho 1.32 }
1974     else
1975     {
1976 bertho 1.49 gdImageLine(im, x1, y1+1, x1, y1+3+1, colorid);
1977     gdImageLine(im, x2, y2, x2, y2-3, colorid);
1978     gdImageLine(im, x1, y1+3+1, x2, y2-3, colorid);
1979 bertho 1.32 }
1980 bertho 1.31 }
1981 bertho 1.30 }
1982     else
1983     {
1984 bertho 1.32 if(fr->branch == tr->branch)
1985 bertho 1.31 {
1986 bertho 1.32 int xx = (x1 < x2 ? x1 : x2) - 5;
1987 bertho 1.49 gdImageLine(im, xx, y1, x1, y1, colorid);
1988     gdImageLine(im, xx, y2, x2, y2, colorid);
1989     gdImageLine(im, xx, y1, xx, y2, colorid);
1990 bertho 1.31 }
1991     else
1992     {
1993 bertho 1.32 if(x1 > x2)
1994     {
1995 bertho 1.49 gdImageLine(im, x1, y1, x1-3, y1, colorid);
1996     gdImageLine(im, x2, y2, x2+3, y2, colorid);
1997     gdImageLine(im, x1-3, y1, x2+3, y2, colorid);
1998 bertho 1.32 }
1999     else
2000     {
2001 bertho 1.49 gdImageLine(im, x1, y1, x1+3, y1, colorid);
2002     gdImageLine(im, x2, y2, x2-3, y2, colorid);
2003     gdImageLine(im, x1+3, y1, x2-3, y2, colorid);
2004 bertho 1.32 }
2005 bertho 1.31 }
2006 bertho 1.30 }
2007     }
2008     }
2009     }
2010    
2011 bertho 1.44 static void draw_messages(gdImagePtr im, int offset)
2012     {
2013     int i;
2014    
2015     for(i = 0; i < nmsg_stack; i++)
2016     {
2017     draw_stringnl(im, msg_stack[i].msg, &conf.msg_font, conf.margin_left, offset, ALIGN_HL|ALIGN_VT, &conf.msg_color);
2018     offset += msg_stack[i].h;
2019     }
2020     }
2021    
2022 bertho 1.19 static void alloc_color(gdImagePtr im, color_t *c)
2023 bertho 1.9 {
2024     c->id = gdImageColorAllocate(im, c->r, c->g, c->b);
2025     }
2026    
2027 bertho 1.44 static gdImagePtr make_image(rcsfile_t *rcs)
2028 bertho 1.1 {
2029     gdImagePtr im;
2030     int i;
2031 bertho 1.8 char *cptr;
2032 bertho 1.44 int w, h;
2033     int subx = 0, suby = 0;
2034     int subw, subh;
2035     int msgh = 0;
2036    
2037     if(subtree_branch)
2038     {
2039     subw = 0;
2040     subh = 0;
2041     if(subtree_rev)
2042     {
2043     for(i = 0; i < subtree_rev->nbranches; i++)
2044     calc_subtree_size(subtree_rev->branches[i], &subx, &suby, &subw, &subh);
2045     }
2046     else
2047     calc_subtree_size(subtree_branch, &subx, &suby, &subw, &subh);
2048     }
2049     else
2050     {
2051     subw = rcs->tw;
2052     subh = rcs->th;
2053     }
2054 bertho 1.1
2055 bertho 1.15 cptr = expand_string(conf.title, rcs, NULL, NULL, NULL, NULL);
2056 bertho 1.44 w = subw + conf.margin_left + conf.margin_right;
2057     h = subh + conf.margin_top + conf.margin_bottom;
2058 bertho 1.15 i = get_swidth(cptr, &conf.title_font);
2059 bertho 1.44 if(i > w)
2060     w = i;
2061    
2062     if(!quiet && nmsg_stack)
2063     {
2064     int msgw = 0;
2065     for(i = 0; i < nmsg_stack; i++)
2066     {
2067     int ww = msg_stack[i].w = get_swidth(msg_stack[i].msg, &conf.msg_font);
2068     int hh = msg_stack[i].h = get_sheight(msg_stack[i].msg, &conf.msg_font);
2069     msgh += hh;
2070     h += hh;
2071     if(ww > msgw)
2072     msgw = ww;
2073     }
2074     if(msgw > w)
2075     w = msgw;
2076     }
2077    
2078     im = gdImageCreate(w, h);
2079 bertho 1.9 alloc_color(im, &conf.color_bg);
2080     alloc_color(im, &conf.tag_color);
2081     alloc_color(im, &conf.rev_color);
2082     alloc_color(im, &conf.rev_bgcolor);
2083     alloc_color(im, &conf.rev_text_color);
2084     alloc_color(im, &conf.branch_color);
2085 bertho 1.19 alloc_color(im, &conf.branch_tag_color);
2086 bertho 1.9 alloc_color(im, &conf.branch_bgcolor);
2087     alloc_color(im, &conf.title_color);
2088 bertho 1.30 alloc_color(im, &conf.merge_color);
2089 bertho 1.49 alloc_color(im, &conf.merge_cvsnt_color);
2090 bertho 1.44 alloc_color(im, &conf.msg_color);
2091 bertho 1.9 alloc_color(im, &black_color);
2092     alloc_color(im, &white_color);
2093 bertho 1.1
2094 bertho 1.19 if(conf.transparent_bg)
2095     gdImageColorTransparent(im, conf.color_bg.id);
2096    
2097 bertho 1.30 if(!conf.merge_front)
2098 bertho 1.31 draw_merges(im, rcs, 0);
2099 bertho 1.30
2100 bertho 1.1 for(i = 0; i < rcs->nbranches; i++)
2101 bertho 1.32 {
2102 bertho 1.44 if(!rcs->branches[i]->folded && !(subtree_branch && !rcs->branches[i]->subtree_draw))
2103 bertho 1.32 draw_branch(im, rcs->branches[i]);
2104     }
2105 bertho 1.31
2106     draw_merges(im, rcs, 1); /* The dots of the merge dest */
2107    
2108 bertho 1.1 for(i = 0; i < rcs->nbranches; i++)
2109     {
2110     if(rcs->branches[i]->branchpoint)
2111     draw_connector(im, rcs->branches[i]);
2112     }
2113 bertho 1.44
2114     /* Clear the margins if we have a partial tree */
2115     if(subtree_branch)
2116     {
2117     gdImageFilledRectangle(im, 0, 0, w-1, conf.margin_top-1, conf.color_bg.id);
2118     gdImageFilledRectangle(im, 0, 0, conf.margin_left-1, h-1, conf.color_bg.id);
2119     gdImageFilledRectangle(im, 0, h-conf.margin_bottom, w-1, h-1, conf.color_bg.id);
2120     gdImageFilledRectangle(im, w-conf.margin_right, 0, w-1, h-1, conf.color_bg.id);
2121     }
2122    
2123 bertho 1.9 draw_stringnl(im, cptr, &conf.title_font, conf.title_x, conf.title_y, conf.title_align, &conf.title_color);
2124 bertho 1.8 xfree(cptr);
2125 bertho 1.1
2126 bertho 1.30 if(conf.merge_front)
2127 bertho 1.31 draw_merges(im, rcs, 0);
2128 bertho 1.30
2129 bertho 1.44 if(!quiet)
2130     draw_messages(im, h - conf.margin_bottom/2 - msgh);
2131    
2132 bertho 1.1 return im;
2133     }
2134    
2135 bertho 1.7 /*
2136     **************************************************************************
2137     * Layout routines
2138 bertho 1.19 *
2139     * Branch BBox:
2140     * left = center_x - total_width / 2 (cx-tw)/2
2141     * right = center_x + total_width / 2 (cx+tw)/2
2142     * top = y_pos (y)
2143     * bottom = y_pos + total_height (y+th)
2144     *
2145     * Margins of branches:
2146     *
2147     * . .
2148     * . .
2149     * +--------------+
2150     * ^
2151     * | branch_margin .
2152     * v .
2153     * ----------------+ .
2154     * | ^ |
2155     * | | branch_connect |
2156     * | v |
2157     *..-+ +t-----+------+ +------+------+
2158     * | l | | |
2159     * | <--> | branch bbox | <--> | branch bbox |
2160     * | | | r | | |
2161     *..-+ | +------------b+ | +-------------+
2162     * | ^ branch_margin
2163     * | | branch_margin
2164     * | v
2165     * | +-------------+
2166     * | . .
2167     * | . .
2168     * |
2169     * branch_margin
2170     *
2171     * FIXME: There are probable som +/-1 errors in the code...
2172     * (notably shadows are not calculated in the margins)
2173 bertho 1.7 **************************************************************************
2174     */
2175 bertho 1.19 static void move_branch(branch_t *b, int x, int y)
2176 bertho 1.1 {
2177     int i;
2178 bertho 1.7 b->cx += x;
2179 bertho 1.1 b->y += y;
2180     for(i = 0; i < b->nrevs; i++)
2181     {
2182 bertho 1.7 b->revs[i]->cx += x;
2183 bertho 1.1 b->revs[i]->y += y;
2184     }
2185     }
2186    
2187 bertho 1.19 static void initial_reposition_branch(revision_t *r, int *x, int *w)
2188 bertho 1.6 {
2189     int i, j;
2190     for(j = 0; j < r->nbranches; j++)
2191     {
2192     branch_t *b = r->branches[j];
2193 bertho 1.7 *x += *w + conf.rev_minline + b->tw/2 - b->cx;
2194 bertho 1.6 *w = b->tw/2;
2195 bertho 1.7 move_branch(b, *x, r->y + r->h/2 + conf.branch_connect);
2196     *x = b->cx;
2197 bertho 1.6 /* Recurse to move branches of branched revisions */
2198     for(i = b->nrevs-1; i >= 0; i--)
2199     {
2200 bertho 1.18 initial_reposition_branch(b->revs[i], x, w);
2201 bertho 1.6 }
2202     }
2203     }
2204    
2205 bertho 1.25 static void initial_reposition_branch_lr(revision_t *r, int *y, int *h)
2206     {
2207     int i, j;
2208     for(j = 0; j < r->nbranches; j++)
2209     {
2210     branch_t *b = r->branches[j];
2211     *y += *h + conf.rev_minline + b->th/2 - b->y;
2212     *h = b->th/2;
2213     move_branch(b, r->cx + r->w/2 + conf.branch_connect, *y);
2214     *y = b->y;
2215     /* Recurse to move branches of branched revisions */
2216     for(i = b->nrevs-1; i >= 0; i--)
2217     {
2218 bertho 1.32 initial_reposition_branch_lr(b->revs[i], y, h);
2219 bertho 1.25 }
2220     }
2221     }
2222    
2223 bertho 1.19 static void rect_union(int *x, int *y, int *w, int *h, branch_t *b)
2224 bertho 1.1 {
2225     int x1 = *x;
2226     int x2 = x1 + *w;
2227     int y1 = *y;
2228     int y2 = y1 + *h;
2229 bertho 1.25 int xx1;
2230     int xx2;
2231     int yy1;
2232     int yy2;
2233    
2234     if(conf.left_right)
2235     {
2236     xx1 = b->cx;
2237     yy1 = b->y - b->th/2;
2238     }
2239     else
2240     {
2241     xx1 = b->cx - b->tw/2;
2242     yy1 = b->y;
2243     }
2244     xx2 = xx1 + b->tw;
2245     yy2 = yy1 + b->th;
2246    
2247 bertho 1.1 x1 = MIN(x1, xx1);
2248     x2 = MAX(x2, xx2);
2249     y1 = MIN(y1, yy1);
2250     y2 = MAX(y2, yy2);
2251     *x = x1;
2252     *y = y1;
2253     *w = x2 - x1;
2254     *h = y2 - y1;
2255     }
2256    
2257 bertho 1.44 static void calc_subtree_size(branch_t *b, int *x, int *y, int *w, int *h)
2258     {
2259     int i, j;
2260    
2261     rect_union(x, y, w, h, b);
2262    
2263     for(i = 0; i < b->nrevs; i++)
2264     {
2265     for(j = 0; j < b->revs[i]->nbranches; j++)
2266     calc_subtree_size(b->revs[i]->branches[j], x, y, w, h);
2267     }
2268     }
2269    
2270 bertho 1.19 static int branch_intersects(int top, int bottom, int left, branch_t *b)
2271 bertho 1.7 {
2272     int br = b->cx + b->tw/2;
2273     int bt = b->y - conf.branch_connect - conf.branch_margin/2;
2274     int bb = b->y + b->th + conf.branch_margin/2;
2275     return !(bt > bottom || bb < top || br >= left);
2276     }
2277    
2278 bertho 1.25 static int branch_intersects_lr(int left, int right, int top, branch_t *b)
2279     {
2280     int bt = b->y + b->th/2;
2281     int bl = b->cx - conf.branch_connect - conf.branch_margin/2;
2282     int br = b->cx + b->tw + conf.branch_margin/2;
2283     return !(bl > right || br < left || bt >= top);
2284     }
2285    
2286 bertho 1.19 static int kern_branch(rcsfile_t *rcs, branch_t *b)
2287 bertho 1.7 {
2288     int left = b->cx - b->tw/2;
2289     int top = b->y - conf.branch_connect - conf.branch_margin/2;
2290     int bottom = b->y + b->th + conf.branch_margin/2;
2291     int i;
2292     int xpos = 0;
2293    
2294     for(i = 0; i < rcs->nbranches; i++)
2295     {
2296     branch_t *bp = rcs->branches[i];
2297     if(bp == b)
2298     continue;
2299     if(branch_intersects(top, bottom, left, bp))
2300     {
2301     int m = bp->cx + bp->tw/2 + conf.branch_margin;
2302     if(m > xpos)
2303     xpos = m;
2304     }
2305     }
2306     if(xpos && (b->cx - b->tw/2) - xpos > 0)
2307     {
2308     move_branch(b, xpos - (b->cx - b->tw/2), 0);
2309     return 1;
2310     }
2311     return 0;
2312     }
2313    
2314 bertho 1.25 static int kern_branch_lr(rcsfile_t *rcs, branch_t *b)
2315     {
2316     int top = b->y - b->th/2;
2317     int left = b->cx - conf.branch_connect - conf.branch_margin/2;
2318     int right = b->cx + b->tw + conf.branch_margin/2;
2319     int i;
2320     int ypos = 0;
2321    
2322     for(i = 0; i < rcs->nbranches; i++)
2323     {
2324     branch_t *bp = rcs->branches[i];
2325     if(bp == b)
2326     continue;
2327     if(branch_intersects_lr(left, right, top, bp))
2328     {
2329     int m = bp->y + bp->th/2 + conf.branch_margin;
2330     if(m > ypos)
2331     ypos = m;
2332     }
2333     }
2334     if(ypos && (b->y - b->th/2) - ypos > 0)
2335     {
2336     move_branch(b, 0, ypos - (b->y - b->th/2));
2337     return 1;
2338     }
2339     return 0;
2340     }
2341    
2342 bertho 1.19 static int kern_tree(rcsfile_t *rcs)
2343 bertho 1.18 {
2344     int i;
2345     int moved;
2346 bertho 1.19 int safeguard;
2347     int totalmoved = 0;
2348     for(moved = 1, safeguard = LOOPSAFEGUARD; moved && safeguard; safeguard--)
2349 bertho 1.18 {
2350     moved = 0;
2351     for(i = 1; i < rcs->nbranches; i++)
2352     {
2353 bertho 1.25 if(conf.left_right)
2354     moved += kern_branch_lr(rcs, rcs->branches[i]);
2355     else
2356     moved += kern_branch(rcs, rcs->branches[i]);
2357 bertho 1.18 }
2358 bertho 1.19 totalmoved += moved;
2359 bertho 1.18 #ifdef DEBUG
2360     fprintf(stderr, "kern_tree: moved=%d\n", moved);
2361     #endif
2362     }
2363 bertho 1.44 if(!safeguard)
2364     stack_msg(MSG_WARN, "kern_tree: safeguard terminated possible infinite loop; please report.");
2365 bertho 1.19 return totalmoved;
2366     }
2367    
2368     static int index_of_revision(revision_t *r)
2369     {
2370     branch_t *b = r->branch;
2371     int i;
2372     for(i = 0; i < b->nrevs; i++)
2373     {
2374     if(r == b->revs[i])
2375     return i;
2376     }
2377 bertho 1.44 stack_msg(MSG_ERR, "index_of_revision: Cannot find revision in branch\n");
2378 bertho 1.19 return 0;
2379 bertho 1.18 }
2380    
2381 bertho 1.19 static void branch_bbox(branch_t *br, int *l, int *r, int *t, int *b)
2382     {
2383     if(l) *l = br->cx - br->tw/2;
2384     if(r) *r = br->cx + br->tw/2;
2385     if(t) *t = br->y;
2386 bertho 1.32 if(b) *b = br->y + br->th + ((conf.branch_dupbox && br->nrevs) ? conf.rev_minline + br->h : 0);
2387 bertho 1.19 }
2388    
2389     static void branch_ext_bbox(branch_t *br, int *l, int *r, int *t, int *b)
2390     {
2391     int extra = conf.branch_margin & 1; /* Correct +/-1 error on div 2 */
2392     branch_bbox(br, l, r, t, b);
2393     if(l) *l -= conf.branch_margin/2;
2394     if(r) *r += conf.branch_margin/2 + extra;
2395     if(t) *t -= conf.branch_connect + conf.branch_margin/2;
2396     if(b) *b += conf.branch_margin/2 + extra;
2397     }
2398    
2399     static int branch_distance(branch_t *br1, branch_t *br2)
2400     {
2401     int l1, r1, t1, b1;
2402     int l2, r2, t2, b2;
2403     assert(br1 != NULL);
2404     assert(br2 != NULL);
2405     branch_bbox(br1, &l1, &r1, NULL, NULL);
2406     branch_bbox(br2, &l2, &r2, NULL, NULL);
2407     branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
2408     branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
2409     /* Return:
2410     * - 0 if branches have no horizontal overlap
2411     * - positive if b1 is left of b2
2412     * - negative if b2 is left of b1
2413     */
2414     if((t1 > t2 && t1 < b2) || (b1 > t2 && b1 < b2))
2415     return l1 < l2 ? l2 - r1 : -(l1 - r2);
2416     else
2417     return 0;
2418     }
2419    
2420     static int space_needed(branch_t *br1, branch_t *br2)
2421     {
2422     int t1, b1;
2423     int t2, b2;
2424     assert(br1 != NULL);
2425     assert(br2 != NULL);
2426     assert(br1->cx < br2->cx); /* br1 must be left of br2 */
2427     branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
2428     branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
2429     /* Return:
2430     * - positive if top br1 is located lower than br2
2431     * - negatve is top br2 is located lower than br1
2432     */
2433     if(t1 > t2)
2434     return -(t1 - b2);
2435     else
2436     return t2 - b1;
2437     }
2438    
2439     static void move_yr_branch(branch_t *b, int dy)
2440 bertho 1.18 {
2441     int i, j;
2442     #ifdef DEBUG
2443 bertho 1.23 /* fprintf(stderr, "move_yr_branch: b=%s, dy=%d\n", b->branch->branch, dy);*/
2444 bertho 1.18 #endif
2445 bertho 1.19 b->y += dy;
2446     for(i = 0; i < b->nrevs; i++)
2447 bertho 1.18 {
2448 bertho 1.19 b->revs[i]->y += dy;
2449     for(j = 0; j < b->revs[i]->nbranches; j++)
2450 bertho 1.18 {
2451 bertho 1.19 #ifdef DEBUG
2452 bertho 1.23 /* fprintf(stderr, ".");*/
2453 bertho 1.19 #endif
2454     move_yr_branch(b->revs[i]->branches[j], dy);
2455 bertho 1.18 }
2456     }
2457     }
2458    
2459 bertho 1.19 static void move_trunk(revision_t *r, int dy)
2460 bertho 1.18 {
2461 bertho 1.19 int i, j;
2462 bertho 1.18 branch_t *b = r->branch;
2463 bertho 1.19 b->th += dy;
2464     for(i = index_of_revision(r); i < b->nrevs; i++)
2465 bertho 1.18 {
2466 bertho 1.19 #ifdef DEBUG
2467     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);
2468     #endif
2469     b->revs[i]->y += dy;
2470     for(j = 0; j < b->revs[i]->nbranches; j++)
2471     {
2472     move_yr_branch(b->revs[i]->branches[j], dy);
2473     }
2474 bertho 1.18 }
2475     }
2476    
2477 bertho 1.19 static int space_below(rcsfile_t *rcs, revision_t *r)
2478 bertho 1.18 {
2479 bertho 1.19 int i, j;
2480     int bl, br, bb;
2481 bertho 1.18 int space = INT_MAX;
2482 bertho 1.19 branch_t *b = r->branch;
2483     branch_t *minb = NULL;
2484    
2485     branch_ext_bbox(b, &bl, &br, NULL, &bb);
2486 bertho 1.18 for(i = 0; i < rcs->nbranches; i++)
2487     {
2488 bertho 1.19 int tbl, tbr, tbt;
2489 bertho 1.18 branch_t *tb = rcs->branches[i];
2490 bertho 1.19 branch_ext_bbox(tb, &tbl, &tbr, &tbt, NULL);
2491 bertho 1.18 if(tb == b)
2492     continue;
2493 bertho 1.19 if(tbt > bb) /* Must be below our branch */
2494 bertho 1.18 {
2495 bertho 1.19 if(tb->branchpoint) /* Take account for the horiz connector */
2496     tbl = tb->branchpoint->cx + tb->branchpoint->branch->tw/2;
2497     if((bl >= tbl && bl <= tbr) || (br <= tbr && br >= tbl))
2498 bertho 1.18 {
2499 bertho 1.19 int s = tbt - bb - conf.branch_connect;
2500     if(s < space)
2501 bertho 1.18 {
2502 bertho 1.19 space = s;
2503     minb = tb;
2504 bertho 1.18 }
2505     }
2506     }
2507     }
2508 bertho 1.19 if(b->branchpoint)
2509     {
2510     for(i = index_of_revision(r); i < b->nrevs; i++)
2511     {
2512     for(j = 0; j < b->revs[i]->nbranches; j++)
2513     {
2514     int s = space_below(rcs, b->revs[i]->branches[j]->revs[0]);
2515     if(s < space)
2516     space = s;
2517     }
2518     }
2519     }
2520     #ifdef DEBUG
2521     fprintf(stderr, "space_below: from %s have %d to %s\n", b->branch->branch, space, minb ? minb->branch->branch : "<recursed>");
2522     #endif
2523 bertho 1.18 return space;
2524     }
2525    
2526 bertho 1.19 static int space_available(rcsfile_t *rcs, branch_t *colbr, branch_t *tagbr, int *nl, revision_t **bpcommon)
2527 bertho 1.18 {
2528 bertho 1.19 int i;
2529     int space = 0;
2530     int nlinks = 0;
2531     revision_t *r;
2532     branch_t *b;
2533     branch_t *ancestor;
2534     revision_t *branchpoint;
2535    
2536     if(!tagbr->branchpoint || !colbr->branchpoint)
2537 bertho 1.18 {
2538 bertho 1.44 stack_msg(MSG_WARN, "space_available: Trying to stretch the top?");
2539 bertho 1.19 return 0;
2540 bertho 1.18 }
2541 bertho 1.19
2542     r = colbr->branchpoint;
2543     b = r->branch;
2544     branchpoint = tagbr->branchpoint;
2545     ancestor = branchpoint->branch;
2546     assert(b != NULL);
2547     assert(ancestor != NULL);
2548    
2549     while(1)
2550 bertho 1.18 {
2551 bertho 1.19 int s;
2552     int rtag = b == ancestor ? index_of_revision(branchpoint)+1 : 0;
2553     for(i = index_of_revision(r); i >= rtag; i--)
2554 bertho 1.18 {
2555 bertho 1.19 if(i > 0)
2556     s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
2557     else
2558     s = b->revs[i]->y - (b->y + b->h);
2559     if(s < conf.rev_maxline)
2560     {
2561     space += conf.rev_maxline - s;
2562     nlinks++;
2563     }
2564 bertho 1.18 }
2565 bertho 1.19 s = space_below(rcs, r);
2566     if(s < space)
2567     space = s;
2568     #ifdef DEBUG
2569     if(space < 0)
2570     return -1;
2571     #endif
2572     if(b == ancestor)
2573     break;
2574     r = b->branchpoint;
2575     if(!r)
2576     {
2577     /* Not a common ancestor */
2578     r = colbr->branchpoint;
2579     b = r->branch;
2580     branchpoint = ancestor->branchpoint;
2581     if(!branchpoint)
2582     {
2583 bertho 1.44 stack_msg(MSG_WARN, "space_available: No common ancestor?");
2584 bertho 1.19 return 0;
2585     }
2586     ancestor = branchpoint->branch;
2587     assert(ancestor != NULL);
2588     nlinks = 0;
2589     space = 0;
2590     continue; /* Restart with a new ancestor */
2591     }
2592     b = r->branch;
2593     }
2594     if(nl)
2595     *nl = nlinks; /* Return the number of links that can stretch */
2596     if(bpcommon)
2597     *bpcommon = branchpoint; /* Return the ancestral branchpoint on the common branch */
2598     return space;
2599 bertho 1.18 }
2600    
2601 bertho 1.19 static int stretch_branches(rcsfile_t *rcs, branch_t *br1, branch_t *br2, int totalstretch)
2602 bertho 1.18 {
2603 bertho 1.19 revision_t *r;
2604     revision_t *bpcommon = NULL;
2605     branch_t *ancestor = NULL;
2606     branch_t *b;
2607 bertho 1.18 int i;
2608 bertho 1.19 int space;
2609     int nlinks;
2610     int dy;
2611     int rest;
2612    
2613     space = space_available(rcs, br1, br2, &nlinks, &bpcommon);
2614     if(bpcommon)
2615     ancestor = bpcommon->branch;
2616    
2617     #ifdef DEBUG
2618     if(space == -1)
2619     return 0;
2620     fprintf(stderr, "stretch_branches: space available %d over %d links common %s\n", space, nlinks, ancestor->branch->branch);
2621     #endif
2622     if(space < totalstretch)
2623     return 0;
2624    
2625     dy = totalstretch / nlinks;
2626     rest = totalstretch - dy * nlinks;
2627    
2628     r = br1->branchpoint;
2629     b = r->branch;
2630     while(1)
2631 bertho 1.18 {
2632 bertho 1.19 int rtag = b == ancestor ? index_of_revision(bpcommon)+1 : 0;
2633     for(i = index_of_revision(r); i >= rtag; i--)
2634     {
2635     int s, q;
2636     if(i > 0)
2637     s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
2638     else
2639     s = b->revs[i]->y - (b->y + b->h);
2640     q = conf.rev_maxline - s;
2641     if(q > 0)
2642     {
2643     int d = rest ? rest/nlinks+1 : 0;
2644     if(q >= dy+d)
2645     {
2646     move_trunk(b->revs[i], dy+d);
2647     }
2648     else
2649     {
2650     move_trunk(b->revs[i], q);
2651     rest += dy+d - q;
2652     }
2653     rest -= d;
2654     nlinks--;
2655     }
2656     }
2657     if(b == ancestor)
2658     break;
2659     r = b->branchpoint;
2660     assert(r != NULL); /* else 'space_available' wouldn't have returned positively */
2661     b = r->branch;
2662 bertho 1.18 }
2663 bertho 1.19 return 1;
2664 bertho 1.18 }
2665    
2666 bertho 1.19 static branch_t *find_collision_branch(rcsfile_t *rcs, branch_t *b)
2667 bertho 1.18 {
2668     int i;
2669 bertho 1.19 int dist = INT_MAX;
2670     branch_t *col = NULL;
2671    
2672 bertho 1.18 for(i = 0; i < rcs->nbranches; i++)
2673     {
2674 bertho 1.19 int t = branch_distance(rcs->branches[i], b);
2675     if(t > 0 && t < dist)
2676 bertho 1.18 {
2677 bertho 1.19 dist = t;
2678     col = rcs->branches[i];
2679 bertho 1.18 }
2680     }
2681 bertho 1.19 return col;
2682 bertho 1.18 }
2683    
2684 bertho 1.44 static void auto_stretch(rcsfile_t *rcs)
2685 bertho 1.18 {
2686 bertho 1.19 int i;
2687     int safeguard;
2688 bertho 1.18
2689 bertho 1.19 for(i = 0, safeguard = LOOPSAFEGUARD; i < rcs->nbranches && safeguard; i++)
2690 bertho 1.18 {
2691 bertho 1.19 int bl, pr;
2692     branch_t *b = rcs->branches[i];
2693     if(!b->branchpoint)
2694     continue;
2695     branch_bbox(b, &bl, NULL, NULL, NULL);
2696     branch_bbox(b->branchpoint->branch, NULL, &pr, NULL, NULL);
2697     if(bl - conf.branch_margin - pr > 0)
2698 bertho 1.18 {
2699 bertho 1.19 branch_t *col;
2700 bertho 1.18 int spaceneeded;
2701 bertho 1.19 /* There is a potential to move branch b further left.
2702     * All branches obstructing this one from moving further
2703     * left must be originating from revisions below
2704     * b->branchpoint until a common ancester.
2705     * So, we search all branches for a branch that lies left
2706     * of b and is closest to b. This is then the collission
2707     * branch that needs to be moved.
2708     */
2709     col = find_collision_branch(rcs, b);
2710     if(!col)
2711 bertho 1.18 continue;
2712 bertho 1.19 spaceneeded = space_needed(col, b);
2713     if(spaceneeded < 0)
2714     continue;
2715     #ifdef DEBUG
2716     fprintf(stderr, "auto_stretch: %s collides %s need %d\n", b->branch->branch, col->branch->branch, spaceneeded);
2717     #endif
2718     /* Trace the collision branch back to find the common ancester
2719     * of both col and b. All revisions encountered while traversing
2720     * backwards must be stretched, including all revisions on the
2721     * common ancester from where the branches sprout.
2722     */
2723     if(stretch_branches(rcs, col, b, spaceneeded))
2724 bertho 1.18 {
2725 bertho 1.19 if(kern_tree(rcs))
2726     {
2727     /* Restart the process because movement can
2728     * cause more movement.
2729     */
2730     i = 0 - 1; /* -1 for the i++ of the loop */
2731     safeguard--; /* Prevent infinite loop, just in case */
2732     }
2733 bertho 1.23 /*return;*/
2734 bertho 1.18 }
2735     }
2736     }
2737 bertho 1.44 if(!safeguard)
2738     stack_msg(MSG_ERR, "auto_stretch: safeguard terminated possible infinite loop; please report.");
2739 bertho 1.18 }
2740    
2741 bertho 1.32 static void fold_branch(rcsfile_t *rcs, revision_t *r)
2742     {
2743     int i, j;
2744     branch_t *btag = NULL;
2745    
2746     for(i = 0; i < r->nbranches; i++)
2747     {
2748     branch_t *b = r->branches[i];
2749     if(!b->nrevs && b->ntags < 2)
2750     {
2751     /* No commits in this branch and no duplicate tags */
2752     if(!btag)
2753     btag = b;
2754     else
2755     {
2756     /* We have consecutive empty branches, fold */
2757     b->folded = 1;
2758 bertho 1.44 b->folded_to = btag;
2759 bertho 1.32 for(j = 0; j < rcs->nbranches; j++)
2760     {
2761     if(b == rcs->branches[j])
2762     {
2763     /* Zap the branch from the admin */
2764     memmove(&rcs->branches[j],
2765     &rcs->branches[j+1],
2766     (rcs->nbranches - j - 1)*sizeof(rcs->branches[0]));
2767     rcs->nbranches--;
2768 bertho 1.54 rcs->nfolds++;
2769 bertho 1.32 break;
2770     }
2771    
2772     }
2773     memmove(&r->branches[i], &r->branches[i+1], (r->nbranches - i - 1)*sizeof(r->branches[0]));
2774     r->nbranches--;
2775     i--; /* We have one less now */
2776    
2777     /* Add to the fold-list */
2778     btag->folds = xrealloc(btag->folds, (btag->nfolds+1) * sizeof(btag->folds[0]));
2779     btag->folds[btag->nfolds] = b;
2780     btag->nfolds++;
2781     }
2782     }
2783     else
2784     {
2785 bertho 1.33 if(!conf.branch_foldall)
2786     btag = NULL; /* Start a new box */
2787 bertho 1.32 /* Recursively fold sub-branches */
2788     for(j = 0; j < b->nrevs; j++)
2789     fold_branch(rcs, b->revs[j]);
2790     }
2791     }
2792     }
2793    
2794 bertho 1.44 static void mark_subtree(branch_t *b)
2795     {
2796     int i, j;
2797     b->subtree_draw = 1;
2798     for(i = 0; i < b->nrevs; i++)
2799     {
2800     for(j = 0; j < b->revs[i]->nbranches; j++)
2801     mark_subtree(b->revs[i]->branches[j]);
2802     }
2803     }
2804    
2805     static void make_layout(rcsfile_t *rcs)
2806 bertho 1.1 {
2807     int i, j;
2808     int x, y;
2809     int w, h;
2810     int w2;
2811    
2812 bertho 1.16 /* Remove all unwanted revisions */
2813     if(conf.strip_untagged)
2814     {
2815 bertho 1.21 int fr = conf.strip_first_rev ? 0 : 1;
2816 bertho 1.16 for(i = 0; i < rcs->nbranches; i++)
2817     {
2818     branch_t *bp = rcs->branches[i];
2819 bertho 1.18 for(j = fr; j < bp->nrevs-1; j++)
2820 bertho 1.16 {
2821 bertho 1.52 if(!bp->revs[j]->ntags && bp->revs[j]->stripped >= 0 && !bp->revs[j]->nbranches)
2822 bertho 1.16 {
2823     memmove(&bp->revs[j], &bp->revs[j+1], (bp->nrevs-j-1) * sizeof(bp->revs[0]));
2824     bp->nrevs--;
2825 bertho 1.17 bp->revs[j]->stripped = 1;
2826 bertho 1.16 j--;
2827     }
2828     }
2829     }
2830     }
2831    
2832 bertho 1.44 /* Find the sub-tree(s) we want to see */
2833     if(conf.branch_subtree && conf.branch_subtree[0])
2834     {
2835     branch_t **b;
2836     revision_t **r;
2837     rev_t rev;
2838     int k;
2839     char *tag = conf.branch_subtree;
2840    
2841     /* First translate any symbolic tag to a real branch/revision number */
2842     if(rcs->tags)
2843     {
2844     for(k = 0; k < rcs->tags->ntags; k++)
2845     {
2846     if(!strcmp(conf.branch_subtree, rcs->tags->tags[k]->tag))
2847     {
2848     if(rcs->tags->tags[k]->rev->isbranch)
2849     tag = rcs->tags->tags[k]->rev->branch;
2850     else
2851     tag = rcs->tags->tags[k]->rev->rev;
2852     break;
2853     }
2854     }
2855     }
2856    
2857     /* Find the corresponding branch */
2858     rev.branch = tag;
2859     rev.rev = NULL;
2860     rev.isbranch = 1;
2861     b = bsearch(&rev, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
2862     if(b)
2863     {
2864     if((*b)->branchpoint)
2865     {
2866     subtree_branch = *b;
2867     for(k = 0; k < (*b)->branchpoint->nbranches; k++)
2868     mark_subtree((*b)->branchpoint->branches[k]);
2869     }
2870     /*
2871     * else -> we want everything.
2872     * This happens for the top level branch because it has no
2873     * branchpoint. We do not set the subtree_branch, which then
2874     * results in drawing the whole tree as if we did not select a
2875     * particular branch.
2876     */
2877     }
2878     else
2879     {
2880     /* Maybe it is a revision we want all subtrees from */
2881     rev.rev = tag;
2882     rev.branch = NULL;
2883     rev.isbranch = 0;
2884     r = bsearch(&rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
2885     if(r)
2886     {
2887     if((*r)->nbranches)
2888     {
2889     subtree_branch = (*r)->branches[0];
2890     subtree_rev = *r;
2891     for(k = 0; k < (*r)->nbranches; k++)
2892     mark_subtree((*r)->branches[k]);
2893     }
2894     /*
2895     * else -> we select everything.
2896     * This happens for the any revision that has no branches.
2897     * We do not set the subtree_branch, which then results in
2898     * drawing the whole tree as if we did not select a
2899     * particular revision's branches.
2900     */
2901     }
2902     }
2903     }
2904    
2905     /* Fold all empty branches in one box on the same branchpoint */
2906 bertho 1.32 if(conf.branch_fold)
2907     {
2908     for(i = 0; i < rcs->branches[0]->nrevs; i++)
2909     {
2910 bertho 1.44 if(rcs->branches[0]->revs[i]->nbranches > 0)
2911 bertho 1.32 fold_branch(rcs, rcs->branches[0]->revs[i]);
2912 bertho 1.40 }
2913     }
2914    
2915     /* Remove all unwanted tags */
2916     for(i = 0; i < rcs->nbranches; i++)
2917     {
2918     branch_t *bp = rcs->branches[i];
2919     for(j = 0; j < bp->nrevs; j++)
2920     {
2921     revision_t *r = bp->revs[j];
2922     int k;
2923     for(k = 0; k < r->ntags; k++)
2924     {
2925 bertho 1.43 if(r->tags[k]->ignore > 0)
2926 bertho 1.40 {
2927     memmove(&r->tags[k], &r->tags[k+1], (r->ntags-k-1) * sizeof(r->tags[0]));
2928     r->ntags--;
2929     k--;
2930     }
2931     }
2932 bertho 1.32 }
2933     }
2934    
2935 bertho 1.1 /* Calculate the box-sizes of the revisions */
2936 bertho 1.7 for(i = 0; i < rcs->nsrev; i++)
2937 bertho 1.1 {
2938     revision_t *rp;
2939     int w;
2940     int h;
2941 bertho 1.7 rp = rcs->srev[i];
2942 bertho 1.9 rp->revtext = expand_string(conf.rev_text, rcs, rp, rp->rev, NULL, rp->ntags ? rp->tags[0] : NULL);
2943     w = get_swidth(rp->revtext, &conf.rev_text_font);
2944     j = get_swidth(rp->rev->rev, &conf.rev_font);
2945     if(j > w)
2946     w = j;
2947 bertho 1.46 h = get_sheight(rp->revtext, &conf.rev_text_font);
2948     if(!conf.rev_hidenumber)
2949     h += get_sheight(rp->rev->rev, &conf.rev_font);
2950 bertho 1.1 for(j = 0; j < rp->ntags; j++)
2951     {
2952     int ww = get_swidth(rp->tags[j]->tag, &conf.tag_font);
2953 bertho 1.31 int th;
2954 bertho 1.1 if(ww > w) w = ww;
2955 bertho 1.31 th = get_sheight(rp->tags[j]->tag, &conf.tag_font) + conf.rev_separator;
2956     rp->tags[j]->yofs = h + th/2 + conf.rev_tspace;
2957     h += th;
2958 bertho 1.1 }
2959     rp->w = w + conf.rev_lspace + conf.rev_rspace;
2960     rp->h = h + conf.rev_tspace + conf.rev_bspace;
2961     }
2962    
2963     /* Calculate the box-sizes of the branches */
2964     for(i = 0; i < rcs->nbranches; i++)
2965     {
2966     branch_t *bp = rcs->branches[i];
2967     int w;
2968     int h;
2969 bertho 1.32 if(!bp->nfolds)
2970     {
2971     w = get_swidth(bp->branch->branch, &conf.branch_font);
2972 bertho 1.46 if(conf.rev_hidenumber)
2973     h = 0;
2974     else
2975     h = get_sheight(bp->branch->branch, &conf.branch_font);
2976 bertho 1.32 for(j = 0; j < bp->ntags; j++)
2977     {
2978     int ww = get_swidth(bp->tags[j]->tag, &conf.branch_tag_font);
2979     if(ww > w) w = ww;
2980     h += get_sheight(bp->tags[j]->tag, &conf.branch_tag_font);
2981     }
2982     }
2983     else
2984 bertho 1.1 {
2985 bertho 1.32 int h1, h2;
2986     int w1, w2;
2987     int fw;
2988     w1 = get_swidth(bp->branch->branch, &conf.branch_font);
2989     w1 += get_swidth(" ", &conf.branch_font);
2990     w2 = get_swidth(bp->tags[0]->tag, &conf.branch_tag_font);
2991     fw = w1;
2992     w = w1 + w2;
2993     h1 = get_sheight(bp->branch->branch, &conf.branch_font);
2994     h2 = get_sheight(bp->tags[0]->tag, &conf.branch_tag_font);
2995     h = MAX(h1, h2);
2996     for(j = 0; j < bp->nfolds; j++)
2997     {
2998     w1 = get_swidth(bp->folds[j]->branch->branch, &conf.branch_font);
2999     w1 += get_swidth(" ", &conf.branch_font);
3000     w2 = get_swidth(bp->folds[j]->tags[0]->tag, &conf.branch_tag_font);
3001     if(w1 > fw)
3002     fw = w1;
3003     if(w1 + w2 > w)
3004     w = w1 + w2;
3005     h1 = get_sheight(bp->folds[j]->branch->branch, &conf.branch_font);
3006     h2 = get_sheight(bp->folds[j]->tags[0]->tag, &conf.branch_tag_font);
3007     h += MAX(h1, h2);
3008     }
3009     bp->fw = fw;
3010 bertho 1.1 }
3011     w += conf.branch_lspace + conf.branch_rspace;
3012     h += conf.branch_tspace + conf.branch_bspace;
3013     bp->w = w;
3014     bp->h = h;
3015 bertho 1.25 if(conf.left_right)
3016     {
3017     for(j = 0; j < bp->nrevs; j++)
3018     {
3019     if(bp->revs[j]->h > h)
3020     h = bp->revs[j]->h;
3021     w += bp->revs[j]->w + conf.rev_minline;
3022     }
3023 bertho 1.32 if(conf.branch_dupbox && bp->nrevs)
3024 bertho 1.25 w += bp->w + conf.rev_minline;
3025     }
3026     else
3027 bertho 1.1 {
3028 bertho 1.25 for(j = 0; j < bp->nrevs; j++)
3029     {
3030     if(bp->revs[j]->w > w)
3031     w = bp->revs[j]->w;
3032     h += bp->revs[j]->h + conf.rev_minline;
3033     }
3034 bertho 1.32 if(conf.branch_dupbox && bp->nrevs)
3035 bertho 1.25 h += bp->h + conf.rev_minline;
3036 bertho 1.1 }
3037     bp->th = h;
3038     bp->tw = w;
3039     }
3040    
3041     /* Calculate the relative positions of revs in a branch */
3042 bertho 1.25 if(conf.left_right)
3043     {
3044     for(i = 0; i < rcs->nbranches; i++)
3045     {
3046     branch_t *b = rcs->branches[i];
3047     y = b->th/2;
3048     x = b->w;
3049     b->y = y;
3050     b->cx = 0;
3051     for(j = 0; j < b->nrevs; j++)
3052     {
3053     x += conf.rev_minline;
3054     b->revs[j]->y = y;
3055     b->revs[j]->cx = x;
3056     x += b->revs[j]->w;
3057     }
3058     }
3059     }
3060     else
3061 bertho 1.1 {
3062 bertho 1.25 for(i = 0; i < rcs->nbranches; i++)
3063     {
3064     branch_t *b = rcs->branches[i];
3065     x = b->tw/2;
3066     y = b->h;
3067     b->cx = x;
3068     b->y = 0;
3069     for(j = 0; j < b->nrevs; j++)
3070     {
3071     y += conf.rev_minline;
3072     b->revs[j]->cx = x;
3073     b->revs[j]->y = y;
3074     y += b->revs[j]->h;
3075     }
3076 bertho 1.1 }
3077     }
3078    
3079 bertho 1.18 /* Initially reposition the branches from bottom to top progressively right */
3080 bertho 1.25 if(conf.left_right)
3081     {
3082     x = rcs->branches[0]->y;
3083     w2 = rcs->branches[0]->th / 2;
3084     for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
3085     {
3086     initial_reposition_branch_lr(rcs->branches[0]->revs[i], &x, &w2);
3087     }
3088     }
3089     else
3090 bertho 1.1 {
3091 bertho 1.25 x = rcs->branches[0]->cx;
3092     w2 = rcs->branches[0]->tw / 2;
3093     for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
3094     {
3095     initial_reposition_branch(rcs->branches[0]->revs[i], &x, &w2);
3096     }
3097 bertho 1.1 }
3098    
3099 bertho 1.19 /* Initially move branches left if there is room */
3100     kern_tree(rcs);
3101    
3102 bertho 1.18 /* Try to kern the branches more by expanding the inter-revision spacing */
3103 bertho 1.25 if(conf.auto_stretch && !conf.left_right)
3104 bertho 1.19 auto_stretch(rcs);
3105 bertho 1.7
3106 bertho 1.1 /* Calculate overall image size */
3107 bertho 1.25 if(conf.left_right)
3108     {
3109     x = rcs->branches[0]->cx;
3110     y = rcs->branches[0]->y - rcs->branches[0]->th/2;
3111     }
3112     else
3113     {
3114     x = rcs->branches[0]->cx - rcs->branches[0]->tw/2;
3115     y = rcs->branches[0]->y;
3116     }
3117 bertho 1.1 w = rcs->branches[0]->tw;
3118     h = rcs->branches[0]->th;
3119     for(i = 1; i < rcs->nbranches; i++)
3120     rect_union(&x, &y, &w, &h, rcs->branches[i]);
3121     rcs->tw = w;
3122     rcs->th = h;
3123 bertho 1.16
3124     /* Flip the entire tree */
3125     if(conf.upside_down)
3126     {
3127 bertho 1.25 if(conf.left_right)
3128     {
3129     x += rcs->tw;
3130     for(i = 0; i < rcs->nbranches; i++)
3131     {
3132     branch_t *b = rcs->branches[i];
3133     for(j = 0; j < b->nrevs; j++)
3134     {
3135     revision_t *r = b->revs[j];
3136 bertho 1.44 r->cx = x - r->cx - r->w;
3137 bertho 1.25 }
3138 bertho 1.44 b->cx = x - b->cx - b->w;
3139 bertho 1.25 }
3140     }
3141     else
3142 bertho 1.16 {
3143 bertho 1.25 y += rcs->th;
3144     for(i = 0; i < rcs->nbranches; i++)
3145 bertho 1.16 {
3146 bertho 1.25 branch_t *b = rcs->branches[i];
3147     for(j = 0; j < b->nrevs; j++)
3148     {
3149     revision_t *r = b->revs[j];
3150 bertho 1.44 r->y = y - r->y - r->h;
3151 bertho 1.25 }
3152 bertho 1.44 b->y = y - b->y - b->h;
3153 bertho 1.16 }
3154     }
3155     }
3156 bertho 1.44
3157     /* Relocate the lot if we only draw a sub-tree */
3158     if(subtree_branch)
3159     {
3160     int xx, yy;
3161    
3162     if(subtree_branch->folded) /* Fix the reference if the branch got folded */
3163     subtree_branch = subtree_branch->folded_to;
3164    
3165     xx = conf.left_right ? subtree_branch->cx : subtree_branch->cx - subtree_branch->tw/2;
3166     yy = conf.left_right ? subtree_branch->y - subtree_branch->th/2 : subtree_branch->y;
3167     if(subtree_branch != rcs->branches[0])
3168     {
3169     if(conf.left_right)
3170     xx -= conf.branch_connect;
3171     else
3172     yy -= conf.branch_connect;
3173     }
3174     for(i = 0; i < rcs->nbranches; i++)
3175     move_branch(rcs->branches[i], -xx, -yy);
3176     }
3177    
3178     /* Move everything w.r.t. the top-left margin */
3179     for(i = 0; i < rcs->nbranches; i++)
3180     move_branch(rcs->branches[i], conf.margin_left, conf.margin_top);
3181 bertho 1.1 }
3182    
3183     /*
3184     **************************************************************************
3185 bertho 1.6 * Imagemap functions
3186     **************************************************************************
3187     */
3188 bertho 1.44 static void map_merge_box(rcsfile_t *rcs, FILE *fp, revision_t *fr, revision_t *tr, gdImagePtr im, int x1, int y1, int x2, int y2)
3189     {
3190     char *href = expand_string(conf.map_merge_href, rcs, tr, tr->rev, fr->rev, NULL);
3191     char *alt = expand_string(conf.map_merge_alt, rcs, tr, tr->rev, fr->rev, NULL);
3192     const char *htp = conf.html_level == HTMLLEVEL_X ? " /" : "";
3193    
3194     if(x1 > 0 && x2 > 0 && y1 > 0 && y2 > 0)
3195     fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3196     href, x1, y1, x2, y2, alt, htp);
3197     xfree(alt);
3198     xfree(href);
3199    
3200     if(im)
3201     {
3202     gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
3203     gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
3204     gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
3205     }
3206     }
3207    
3208     static void map_merges(rcsfile_t *rcs, FILE *fp, gdImagePtr im)
3209     {
3210     int i;
3211     int tagh2 = get_sheight("Hg", &conf.tag_font) / 2;
3212     int bm = conf.branch_margin / 2;
3213    
3214     for(i = 0; i < rcs->nmerges; i++)
3215     {
3216 bertho 1.49 revision_t *fr;
3217     revision_t *tr;
3218 bertho 1.44 int x1, x2, y1, y2;
3219 bertho 1.49 switch(rcs->merges[i].type)
3220     {
3221     case TR_TAG:
3222     fr = rcs->merges[i].from.tag->logrev;
3223     tr = rcs->merges[i].to.tag->logrev;
3224     break;
3225     case TR_REVISION:
3226     fr = rcs->merges[i].from.rev;
3227     tr = rcs->merges[i].to.rev;
3228     break;
3229     default:
3230     continue;
3231     }
3232 bertho 1.44 if(!fr || !tr || fr == tr)
3233     continue; /* This can happen with detached tags and self-references */
3234     if(conf.left_right)
3235     {
3236     if(fr->branch == tr->branch)
3237     {
3238     y1 = fr->y - fr->h/2;
3239     y2 = tr->y - tr->h/2;
3240     }
3241     else
3242     {
3243     if(fr->y < tr->y)
3244     {
3245     y1 = fr->y + fr->h/2;
3246     y2 = tr->y - tr->h/2;
3247     }
3248     else
3249     {
3250     y1 = fr->y - fr->h/2;
3251     y2 = tr->y + tr->h/2;
3252     }
3253     }
3254     x1 = fr->cx + fr->w/2;
3255     x2 = tr->cx + tr->w/2;
3256     }
3257     else
3258     {
3259     if(fr->branch == tr->branch)
3260     {
3261     x1 = fr->cx - fr->w/2;
3262     x2 = tr->cx - tr->w/2;
3263     }
3264     else
3265     {
3266     if(fr->cx < tr->cx)
3267     {
3268     x1 = fr->cx + fr->w/2;
3269     x2 = tr->cx - tr->w/2;
3270     }
3271     else
3272     {
3273     x1 = fr->cx - fr->w/2;
3274     x2 = tr->cx + tr->w/2;
3275     }
3276     }
3277 bertho 1.49 if(rcs->merges[i].type == TR_TAG)
3278     {
3279     y1 = fr->y + rcs->merges[i].from.tag->yofs;
3280     y2 = tr->y + rcs->merges[i].to.tag->yofs;
3281     }
3282     else
3283     {
3284     y1 = fr->y + fr->h/2;
3285     y2 = tr->y + tr->h/2;
3286     }
3287 bertho 1.44 }
3288    
3289     if(conf.left_right)
3290     {
3291     if(fr->branch == tr->branch)
3292     {
3293     map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1-bm, x1+bm, y1);
3294     map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2-bm, x2+bm, y2);
3295     }
3296     else
3297     {
3298     if(y1 > y2)
3299     {
3300     map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1-bm, x1+bm, y1);
3301     map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2, x2+bm, y2+bm);
3302     }
3303     else
3304     {
3305     map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1, x1+bm, y1+bm);
3306     map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2-bm, x2+bm, y2);
3307     }
3308     }
3309     }
3310     else
3311     {
3312     if(fr->branch == tr->branch)
3313     {
3314     map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1-tagh2, x1, y1+tagh2);
3315     map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2-tagh2, x2, y2+tagh2);
3316     }
3317     else
3318     {
3319     if(x1 > x2)
3320     {
3321     map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1-tagh2, x1, y1+tagh2);
3322     map_merge_box(rcs, fp, fr, tr, im, x2, y2-tagh2, x2+bm, y2+tagh2);
3323     }
3324     else
3325     {
3326     map_merge_box(rcs, fp, fr, tr, im, x1, y1-tagh2, x1+bm, y1+tagh2);
3327     map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2-tagh2, x2, y2+tagh2);
3328     }
3329     }
3330     }
3331     }
3332     }
3333    
3334     static void make_imagemap(rcsfile_t *rcs, FILE *fp, gdImagePtr im)
3335 bertho 1.6 {
3336     int i, j;
3337 bertho 1.29 const char *htp = conf.html_level == HTMLLEVEL_X ? " /" : "";
3338    
3339     switch(conf.html_level)
3340     {
3341     case HTMLLEVEL_4:
3342     fprintf(fp, "<map name=\"%s\" id=\"%s\">\n", conf.map_name, conf.map_name);
3343     break;
3344     case HTMLLEVEL_X:
3345     fprintf(fp, "<map id=\"%s\">\n", conf.map_name);
3346     break;
3347     default:
3348     fprintf(fp, "<map name=\"%s\">\n", conf.map_name);
3349     }
3350    
3351 bertho 1.6 for(i = 0; i < rcs->nbranches; i++)
3352     {
3353     branch_t *b = rcs->branches[i];
3354 bertho 1.8 tag_t *tag = b->ntags ? b->tags[0] : NULL;
3355 bertho 1.44 char *bhref;
3356     char *balt;
3357 bertho 1.26 int x1;
3358     int x2;
3359     int y1;
3360     int y2;
3361    
3362 bertho 1.44 if(subtree_branch && !b->subtree_draw)
3363     continue;
3364    
3365     bhref = expand_string(conf.map_branch_href, rcs, NULL, b->branch, NULL, tag);
3366     balt = expand_string(conf.map_branch_alt, rcs, NULL, b->branch, NULL, tag);
3367    
3368 bertho 1.34 if(!b->nfolds)
3369 bertho 1.26 {
3370 bertho 1.34 if(conf.left_right)
3371     {
3372     x1 = b->cx;
3373     y1 = b->y - b->h/2;
3374     x2 = b->cx + b->w;
3375     y2 = b->y + b->h/2;
3376     }
3377     else
3378     {
3379     x1 = b->cx - b->w/2;
3380     y1 = b->y;
3381     x2 = b->cx + b->w/2;
3382     y2 = b->y + b->h;
3383     }
3384     fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3385     bhref, x1, y1, x2, y2, balt, htp);
3386     if(im)
3387     {
3388     gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
3389     gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
3390     gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
3391     }
3392 bertho 1.26 }
3393     else
3394     {
3395 bertho 1.34 int yy1, yy2, yy;
3396     if(conf.left_right)
3397     {
3398     x1 = b->cx + conf.branch_lspace;
3399     y1 = b->y - b->h/2 + conf.branch_tspace;
3400     }
3401     else
3402     {
3403     x1 = b->cx - b->w/2 + conf.branch_lspace;
3404     y1 = b->y + conf.branch_tspace;
3405     }
3406     x2 = x1 + b->w - conf.branch_rspace;
3407    
3408     yy1 = get_sheight(b->branch->branch, &conf.branch_font);
3409     yy2 = get_sheight(b->tags[0]->tag, &conf.branch_tag_font);
3410     yy = MAX(yy1, yy2);
3411     y2 = y1 + yy;
3412     fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3413     bhref, x1, y1, x2, y2, balt, htp);
3414    
3415     y1 += yy;
3416     y2 += yy;
3417     for(j = 0; j < b->nfolds; j++)
3418     {
3419     branch_t *fb = b->folds[j];
3420     tag_t *t = fb->tags[0];
3421     xfree(bhref);
3422     xfree(balt);
3423     bhref = expand_string(conf.map_branch_href, rcs, NULL, fb->branch, NULL, t);
3424     balt = expand_string(conf.map_branch_alt, rcs, NULL, fb->branch, NULL, t);
3425     fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3426     bhref, x1, y1, x2, y2, balt, htp);
3427     yy1 = get_sheight(fb->branch->branch, &conf.branch_font);
3428     yy2 = get_sheight(fb->tags[0]->tag, &conf.branch_tag_font);
3429     yy = MAX(yy1, yy2);
3430     y1 += yy;
3431     y2 += yy;
3432     }
3433 bertho 1.26 }
3434 bertho 1.34
3435 bertho 1.6 for(j = 0; j < b->nrevs; j++)
3436     {
3437     revision_t *r = b->revs[j];
3438 bertho 1.13 revision_t* r1;
3439 bertho 1.26 int xoff = 1;
3440     int yoff = 1;
3441 bertho 1.16 char *href;
3442     char *alt;
3443 bertho 1.13
3444 bertho 1.8 tag = r->ntags ? r->tags[0] : NULL;
3445 bertho 1.9 href = expand_string(conf.map_rev_href, rcs, r, r->rev, NULL, tag);
3446     alt = expand_string(conf.map_rev_alt, rcs, r, r->rev, NULL, tag);
3447 bertho 1.26 if(conf.left_right)
3448     {
3449     x1 = r->cx;
3450     y1 = r->y - r->h/2;
3451     x2 = r->cx + r->w;
3452     y2 = r->y + r->h/2;
3453     }
3454     else
3455     {
3456     x1 = r->cx - r->w/2;
3457     y1 = r->y;
3458     x2 = r->cx + r->w/2;
3459     y2 = r->y + r->h;
3460     }
3461 bertho 1.29 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3462     href, x1, y1, x2, y2, alt, htp);
3463 bertho 1.26 if(im)
3464     {
3465     gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
3466     gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
3467     gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
3468     }
3469 bertho 1.8 xfree(href);
3470     xfree(alt);
3471 bertho 1.16 if(j > 0 || b->branchpoint)
3472 bertho 1.9 {
3473 bertho 1.16 if(j > 0)
3474     {
3475     r1 = b->revs[j-1];
3476 bertho 1.26 if(conf.left_right)
3477     {
3478     yoff = MIN(r->h, r1->h)/4;
3479     x1 = conf.upside_down ? r1->cx : r1->cx + r1->w;
3480     }
3481     else
3482     {
3483     xoff = MIN(r->w, r1->w)/4;
3484     y1 = conf.upside_down ? r1->y : r1->y + r1->h;
3485     }
3486 bertho 1.16 }
3487     else
3488     {
3489     r1 = b->branchpoint;
3490 bertho 1.26 if(conf.left_right)
3491     {
3492     yoff = MIN(r->h, b->h)/4;
3493     x1 = conf.upside_down ? b->cx : b->cx + b->w;
3494     }
3495     else
3496     {
3497     xoff = MIN(r->w, b->w)/4;
3498     y1 = conf.upside_down ? b->y : b->y + b->h;
3499     }
3500     }
3501     if(conf.left_right)
3502     {
3503     y1 = r->y - yoff;
3504     y2 = r->y + yoff;
3505     x2 = conf.upside_down ? r->cx + r->w : r->cx;
3506     yoff = 0;
3507     }
3508     else
3509     {
3510     x1 = r->cx - xoff;
3511     x2 = r->cx + xoff;
3512     y2 = conf.upside_down ? r->y + r->h : r->y;
3513     xoff = 0;
3514     }
3515     if(x1 > x2)
3516     {
3517     int tt = x1;
3518     x1 = x2;
3519     x2 = tt;
3520     }
3521     if(y1 > y2)
3522     {
3523     int tt = y1;
3524     y1 = y2;
3525     y2 = tt;
3526 bertho 1.16 }
3527     href = expand_string(conf.map_diff_href, rcs, r, r->rev, r1->rev, tag);
3528     alt = expand_string(conf.map_diff_alt, rcs, r, r->rev, r1->rev, tag);
3529 bertho 1.29 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3530 bertho 1.16 href,
3531 bertho 1.26 x1+xoff, y1+yoff, x2-xoff, y2-yoff,
3532 bertho 1.29 alt, htp);
3533 bertho 1.26 if(im)
3534     {
3535     gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
3536     gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
3537     gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
3538     }
3539 bertho 1.16 xfree(href);
3540     xfree(alt);
3541 bertho 1.9 }
3542 bertho 1.6 }
3543 bertho 1.32 if(conf.branch_dupbox && b->nrevs)
3544 bertho 1.16 {
3545 bertho 1.26 if(conf.left_right)
3546     {
3547     x1 = conf.upside_down ? b->cx + b->w - b->tw : b->cx - b->w + b->tw;
3548     y1 = b->y - b->h/2;
3549     x2 = x1 + b->w;
3550     y2 = b->y + b->h/2;
3551     }
3552 bertho 1.16 else
3553 bertho 1.26 {
3554     x1 = b->cx - b->w/2;
3555     y1 = conf.upside_down ? b->y + b->h - b->th : b->y - b->h + b->th;
3556     x2 = b->cx + b->w/2;
3557     y2 = y1 + b->h;
3558     }
3559 bertho 1.29 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3560     bhref, x1, y1, x2, y2, balt, htp);
3561 bertho 1.26 if(im)
3562     {
3563     gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
3564     gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
3565     gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
3566     }
3567 bertho 1.16 }
3568     xfree(bhref);
3569     xfree(balt);
3570 bertho 1.6 }
3571 bertho 1.44
3572     map_merges(rcs, fp, im);
3573    
3574 bertho 1.6 fprintf(fp, "</map>\n");
3575     }
3576    
3577     /*
3578     **************************************************************************
3579 bertho 1.1 * Program entry
3580     **************************************************************************
3581     */
3582     static const char usage_str[] =
3583     "Usage: cvsgraph [options] <file>\n"
3584 bertho 1.16 " -b Add a branch box at both sides of the trunk (config value is negated)\n"
3585 bertho 1.8 " -c <file> Read alternative config from <file>\n"
3586     " -d <level> Enable debug mode at <level>\n"
3587     " -h This message\n"
3588     " -i Generate an imagemap instead of image\n"
3589 bertho 1.18 " -I <file> Also write the imagemap to <file>\n"
3590 bertho 1.19 " -k Auto stretch the tree (config value is negated)\n"
3591 bertho 1.8 " -M <name> Use <name> as imagemap name\n"
3592     " -m <mod> Use <mod> as cvs module\n"
3593     " -o <file> Output to <file>\n"
3594 bertho 1.18 " -O <opt=val> Set option opt to value val\n"
3595 bertho 1.8 " -q Be quiet (i.e. no warnings)\n"
3596     " -r <path> Use <path> as cvsroot path\n"
3597 bertho 1.16 " -s Strip untagged revisions (config value is negated)\n"
3598 bertho 1.18 " -S Also strip the first revision (config value is negated)\n"
3599 bertho 1.16 " -u Upside down image (mirror vertically; config value is negated)\n"
3600 bertho 1.8 " -V Print version and exit\n"
3601 bertho 1.29 " -x [34x] Specify level of HTML 3.2 (default), 4.0 or XHTML\n"
3602 bertho 1.8 " -[0-9] <txt> Use <txt> for expansion\n"
3603 bertho 1.1 ;
3604    
3605 bertho 1.47 #define VERSION_STR "1.5.1"
3606 bertho 1.41 #define NOTICE_STR "Copyright (c) 2001,2002,2003,2004 B.Stultiens"
3607 bertho 1.1
3608 bertho 1.19 static void append_slash(char **path)
3609 bertho 1.9 {
3610     int l;
3611     assert(path != NULL);
3612     assert(*path != NULL);
3613     l = strlen(*path);
3614     if(!l || (*path)[l-1] == '/')
3615     return;
3616     *path = xrealloc(*path, l+2);
3617     strcat(*path, "/");
3618     }
3619    
3620 bertho 1.1 int main(int argc, char *argv[])
3621     {
3622 bertho 1.7 extern int rcs_flex_debug;
3623     extern int rcsdebug;
3624 bertho 1.1 int optc;
3625     char *confpath = NULL;
3626     char *outfile = NULL;
3627     char *cvsroot = NULL;
3628     char *cvsmodule = NULL;
3629 bertho 1.7 int imagemap = 0;
3630 bertho 1.16 int upsidedown = 0;
3631 bertho 1.17 int bdupbox = 0;
3632 bertho 1.16 int stripuntag = 0;
3633 bertho 1.18 int stripfirst = 0;
3634 bertho 1.19 int autostretch = 0;
3635 bertho 1.29 int htmllevel = 0;
3636 bertho 1.7 char *imgmapname = NULL;
3637 bertho 1.18 char *imgmapfile = NULL;
3638 bertho 1.1 int lose = 0;
3639     FILE *fp;
3640 bertho 1.18 char *rcsfilename;
3641 bertho 1.7 rcsfile_t *rcs;
3642 bertho 1.1 gdImagePtr im;
3643    
3644 bertho 1.29 while((optc = getopt(argc, argv, "0:1:2:3:4:5:6:7:8:9:bc:d:hI:ikM:m:O:o:qr:SsuVx:")) != EOF)
3645 bertho 1.1 {
3646     switch(optc)
3647     {
3648 bertho 1.16 case 'b':
3649 bertho 1.17 bdupbox = 1;
3650 bertho 1.16 break;
3651 bertho 1.1 case 'c':
3652     confpath = xstrdup(optarg);
3653     break;
3654 bertho 1.7 case 'd':
3655     debuglevel = strtol(optarg, NULL, 0);
3656     break;
3657 bertho 1.18 case 'I':
3658     imgmapfile = xstrdup(optarg);
3659     break;
3660 bertho 1.6 case 'i':
3661 bertho 1.7 imagemap = 1;
3662     break;
3663 bertho 1.18 case 'k':
3664 bertho 1.19 autostretch = 1;
3665 bertho 1.18 break;
3666 bertho 1.7 case 'M':
3667     imgmapname = xstrdup(optarg);
3668 bertho 1.6 break;
3669 bertho 1.1 case 'm':
3670     cvsmodule = xstrdup(optarg);
3671     break;
3672 bertho 1.18 case 'O':
3673     stack_option(optarg);
3674     break;
3675 bertho 1.1 case 'o':
3676     outfile = xstrdup(optarg);
3677     break;
3678 bertho 1.7 case 'q':
3679     quiet = 1;
3680     break;
3681 bertho 1.1 case 'r':
3682     cvsroot = xstrdup(optarg);
3683     break;
3684 bertho 1.18 case 'S':
3685     stripfirst = 1;
3686     break;
3687 bertho 1.16 case 's':
3688     stripuntag = 1;
3689     break;
3690     case 'u':
3691     upsidedown = 1;
3692     break;
3693 bertho 1.1 case 'V':
3694     fprintf(stdout, "cvsgraph v%s, %s\n", VERSION_STR, NOTICE_STR);
3695     return 0;
3696 bertho 1.29 case 'x':
3697     switch(optarg[0])
3698     {
3699     case '3':
3700     htmllevel = HTMLLEVEL_3;
3701     break;
3702     case '4':
3703     htmllevel = HTMLLEVEL_4;
3704     break;
3705     case 'x':
3706     htmllevel = HTMLLEVEL_X;
3707     break;
3708     default:
3709     fprintf(stderr, "Invalid HTML level in -x\n");
3710     lose++;
3711     }
3712     break;
3713 bertho 1.1 case 'h':
3714     fprintf(stdout, "%s", usage_str);
3715     return 0;
3716     default:
3717 bertho 1.8 if(isdigit(optc))
3718     {
3719     conf.expand[optc-'0'] = xstrdup(optarg);
3720     }
3721     else
3722     lose++;
3723 bertho 1.1 }
3724     }
3725    
3726     if(lose)
3727     {
3728     fprintf(stderr, "%s", usage_str);
3729     return 1;
3730     }
3731    
3732 bertho 1.7 if(debuglevel)
3733     {
3734     setvbuf(stdout, NULL, 0, _IONBF);
3735     setvbuf(stderr, NULL, 0, _IONBF);
3736     }
3737     rcs_flex_debug = (debuglevel & DEBUG_RCS_LEX) != 0;
3738     rcsdebug = (debuglevel & DEBUG_RCS_YACC) != 0;
3739    
3740 bertho 1.1 /* Set defaults */
3741 bertho 1.21 conf.tag_font.gdfont = gdFontTiny;
3742     conf.rev_font.gdfont = gdFontTiny;
3743     conf.branch_font.gdfont = gdFontTiny;
3744     conf.branch_tag_font.gdfont = gdFontTiny;
3745     conf.title_font.gdfont = gdFontTiny;
3746     conf.rev_text_font.gdfont = gdFontTiny;
3747 bertho 1.44 conf.msg_font.gdfont = gdFontTiny;
3748 bertho 1.21
3749 bertho 1.19 conf.anti_alias = 1;
3750 bertho 1.20 conf.thick_lines = 1;
3751 bertho 1.32 conf.branch_fold = 1;
3752 bertho 1.9
3753     conf.cvsroot = xstrdup("");
3754     conf.cvsmodule = xstrdup("");
3755     conf.date_format = xstrdup("%d-%b-%Y %H:%M:%S");
3756     conf.title = xstrdup("");
3757     conf.map_name = xstrdup("CvsGraphImageMap");
3758     conf.map_branch_href = xstrdup("href=\"unset: conf.map_branch_href\"");
3759     conf.map_branch_alt = xstrdup("alt=\"%B\"");
3760     conf.map_rev_href = xstrdup("href=\"unset: conf.map_rev_href\"");
3761     conf.map_rev_alt = xstrdup("alt=\"%R\"");
3762     conf.map_diff_href = xstrdup("href=\"unset: conf.map_diff_href\"");
3763 bertho 1.12 conf.map_diff_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
3764 bertho 1.44 conf.map_merge_href = xstrdup("href=\"unset: conf.map_merge_href\"");
3765     conf.map_merge_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
3766 bertho 1.10 conf.rev_text = xstrdup("%d");
3767 bertho 1.44 conf.branch_subtree = xstrdup("");
3768     conf.tag_ignore = xstrdup("");
3769 bertho 1.30 conf.merge_from = xstrdup("");
3770     conf.merge_to = xstrdup("");
3771 bertho 1.35 conf.merge_arrows = 1;
3772     conf.arrow_width = ARROW_WIDTH;
3773     conf.arrow_length = ARROW_LENGTH;
3774 bertho 1.9
3775     conf.color_bg = white_color;
3776     conf.branch_bgcolor = white_color;
3777     conf.branch_color = black_color;
3778 bertho 1.19 conf.branch_tag_color = black_color;
3779 bertho 1.9 conf.rev_color = black_color;
3780     conf.rev_bgcolor = white_color;
3781 bertho 1.30 conf.merge_color = black_color;
3782 bertho 1.49 conf.merge_cvsnt_color = black_color;
3783 bertho 1.9 conf.tag_color = black_color;
3784     conf.title_color = black_color;
3785     conf.rev_text_color = black_color;
3786 bertho 1.44 conf.msg_color = black_color;
3787 bertho 1.10
3788     conf.image_quality = 100;
3789 bertho 1.50 conf.image_compress = -1; /* Use default zlib setting */
3790 bertho 1.18 conf.rev_maxline = -1; /* Checked later to set to default */
3791    
3792     read_config(confpath);
3793 bertho 1.1
3794 bertho 1.18 if(conf.rev_maxline == -1) conf.rev_maxline = 5 * conf.rev_minline;
3795 bertho 1.1
3796     /* Set overrides */
3797 bertho 1.7 if(cvsroot) conf.cvsroot = cvsroot;
3798     if(cvsmodule) conf.cvsmodule = cvsmodule;
3799     if(imgmapname) conf.map_name = imgmapname;
3800 bertho 1.16 if(upsidedown) conf.upside_down = !conf.upside_down;
3801 bertho 1.17 if(bdupbox) conf.branch_dupbox = !conf.branch_dupbox;
3802 bertho 1.16 if(stripuntag) conf.strip_untagged = !conf.strip_untagged;
3803 bertho 1.18 if(stripfirst) conf.strip_first_rev = !conf.strip_first_rev;
3804 bertho 1.19 if(autostretch) conf.auto_stretch = !conf.auto_stretch;
3805 bertho 1.29 if(htmllevel) conf.html_level = htmllevel;
3806 bertho 1.18
3807     if(conf.rev_minline >= conf.rev_maxline)
3808     {
3809 bertho 1.44 if(conf.auto_stretch)
3810     stack_msg(MSG_WARN, "Auto stretch is only possible if rev_minline < rev_maxline");
3811 bertho 1.19 conf.auto_stretch = 0;
3812 bertho 1.18 }
3813 bertho 1.20
3814     if(conf.thick_lines < 1)
3815     conf.thick_lines = 1;
3816     if(conf.thick_lines > 11)
3817     conf.thick_lines = 11;
3818 bertho 1.1
3819 bertho 1.50 if(conf.image_quality < 0 || conf.image_quality > 100)
3820     {
3821     stack_msg(MSG_WARN, "JPEG quality (image_quality) must be between 0 and 100");
3822     conf.image_quality = 100;
3823     }
3824    
3825     if(conf.image_compress < -1 || conf.image_compress > 9)
3826     {
3827     stack_msg(MSG_WARN, "PNG compression (image_compress) must be between -1 and 9");
3828     conf.image_compress = -1;
3829     }
3830    
3831 bertho 1.9 append_slash(&conf.cvsroot);
3832     append_slash(&conf.cvsmodule);
3833    
3834 bertho 1.18 if(optind >= argc)
3835     {
3836     #ifdef __WIN32__
3837     /* Bad hack for DOS/Windows */
3838     if(setmode(fileno(stdin), O_BINARY) == -1)
3839     {
3840     perror("Set binary mode for stdin");
3841     return 1;
3842     }
3843     #endif
3844     rcsfilename = NULL;
3845     }
3846     else
3847     rcsfilename = argv[optind];
3848    
3849     rcs = get_rcsfile(conf.cvsroot, conf.cvsmodule, rcsfilename);
3850 bertho 1.1 if(!rcs)
3851     return 1;
3852    
3853 bertho 1.7 if(debuglevel & DEBUG_RCS_FILE)
3854     dump_rcsfile(rcs);
3855 bertho 1.1
3856 bertho 1.7 if(!reorganise_branches(rcs))
3857     return 1;
3858 bertho 1.1
3859 bertho 1.31 assign_tags(rcs);
3860     find_merges(rcs);
3861 bertho 1.49 find_merges_cvsnt(rcs);
3862 bertho 1.1
3863     if(outfile)
3864     {
3865 bertho 1.15 if((fp = fopen(outfile, "wb")) == NULL)
3866 bertho 1.1 {
3867     perror(outfile);
3868     return 1;
3869     }
3870     }
3871     else
3872 bertho 1.15 {
3873 bertho 1.1 fp = stdout;
3874 bertho 1.15 #ifdef __WIN32__
3875     /* Bad hack for DOS/Windows */
3876     if(setmode(fileno(fp), O_BINARY) == -1)
3877     {
3878     perror("Set binary mode for stdout");
3879     return 1;
3880     }
3881     #endif
3882     }
3883 bertho 1.5
3884 bertho 1.7 make_layout(rcs);
3885    
3886     if(!imagemap)
3887 bertho 1.5 {
3888 bertho 1.7 /* Create an image */
3889     im = make_image(rcs);
3890 bertho 1.50
3891     if(conf.image_interlace)
3892     gdImageInterlace(im, 1);
3893    
3894 bertho 1.26 #ifdef DEBUG_IMAGEMAP
3895     {
3896     FILE *nulfile = fopen("/dev/null", "w");
3897     make_imagemap(rcs, nulfile, im);
3898     fclose(nulfile);
3899     }
3900     #endif
3901 bertho 1.7 switch(conf.image_type)
3902     {
3903 bertho 1.5 #ifdef HAVE_IMAGE_GIF
3904 bertho 1.7 # ifndef HAVE_IMAGE_PNG
3905     default:
3906     # endif
3907     case IMAGE_GIF:
3908     gdImageGif(im, fp);
3909     break;
3910 bertho 1.5 #endif
3911     #ifdef HAVE_IMAGE_PNG
3912 bertho 1.7 default:
3913     case IMAGE_PNG:
3914 bertho 1.50 #ifdef HAVE_GDIMAGEPNGEX
3915     gdImagePngEx(im, fp, conf.image_compress);
3916     #else
3917 bertho 1.7 gdImagePng(im, fp);
3918 bertho 1.50 #endif
3919 bertho 1.7 break;
3920 bertho 1.5 #endif
3921     #ifdef HAVE_IMAGE_JPEG
3922     # if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG)
3923 bertho 1.7 default:
3924 bertho 1.5 # endif
3925 bertho 1.7 case IMAGE_JPEG:
3926     gdImageJpeg(im, fp, conf.image_quality);
3927     break;
3928 bertho 1.5 #endif
3929 bertho 1.7 }
3930    
3931     gdImageDestroy(im);
3932     }
3933     else
3934     {
3935     /* Create an imagemap */
3936 bertho 1.26 make_imagemap(rcs, fp, NULL);
3937 bertho 1.18 }
3938    
3939