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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0