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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0