/[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.26 - (hide annotations)
Thu Jul 18 23:08:50 2002 UTC (15 years, 5 months ago) by bertho
Branch: MAIN
Changes since 1.25: +149 -46 lines
File MIME type: text/plain
Finished implementing left_right drawing.
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     rr->tags = xrealloc(rr->tags, (rr->ntags+1)*sizeof(rr->tags[0]));
728     rr->tags[rr->ntags] = t;
729     rr->ntags++;
730     }
731     }
732     }
733    
734     /* We need to reset the first in the list of branches to the
735     * active branch to ensure the drawing of all
736     */
737     if(rcs->active != rcs->branches[0])
738 bertho 1.1 {
739 bertho 1.7 branch_t **b = bsearch(rcs->active->branch, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
740     branch_t *t;
741     assert(b != NULL);
742     t = *b;
743     *b = rcs->branches[0];
744     rcs->branches[0] = t;
745 bertho 1.1 }
746 bertho 1.7 return 1;
747 bertho 1.1 }
748    
749     /*
750     **************************************************************************
751 bertho 1.8 * String expansion
752     **************************************************************************
753     */
754     static char *_string;
755     static int _nstring;
756     static int _nastring;
757    
758 bertho 1.19 static void add_string_str(const char *s)
759 bertho 1.8 {
760     int l = strlen(s) + 1;
761     if(_nstring + l > _nastring)
762     {
763 bertho 1.24 _nastring += MAX(128, l);
764 bertho 1.8 _string = xrealloc(_string, _nastring * sizeof(_string[0]));
765     }
766     memcpy(_string+_nstring, s, l);
767     _nstring += l-1;
768     }
769    
770 bertho 1.19 static void add_string_ch(int ch)
771 bertho 1.8 {
772     char buf[2];
773     buf[0] = ch;
774     buf[1] = '\0';
775     add_string_str(buf);
776     }
777    
778 bertho 1.19 static void add_string_date(const char *d)
779 bertho 1.9 {
780     struct tm tm, *tmp;
781     int n;
782     time_t t;
783     char *buf;
784     int nbuf;
785    
786     memset(&tm, 0, sizeof(tm));
787     n = sscanf(d, "%d.%d.%d.%d.%d.%d",
788     &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
789     &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
790     tm.tm_mon--;
791     if(tm.tm_year > 1900)
792     tm.tm_year -= 1900;
793     t = mktime(&tm);
794     if(n != 6 || t == (time_t)(-1))
795     {
796     add_string_str("<invalid date>");
797     return;
798     }
799    
800     tmp = localtime(&t);
801     nbuf = strlen(conf.date_format) * 16; /* Should be enough to hold all types of expansions */
802     buf = xmalloc(nbuf);
803     strftime(buf, nbuf, conf.date_format, tmp);
804     add_string_str(buf);
805     xfree(buf);
806     }
807    
808 bertho 1.22 static void add_string_str_html(const char *s, int maxlen)
809     {
810     int l = 0;
811     char *str = xmalloc(6 * strlen(s) + 1); /* Should hold all char entity-expand */
812     char *cptr = str;
813     for(; *s; s++)
814     {
815     if(maxlen && l > abs(maxlen))
816     {
817     cptr += sprintf(cptr, "...");
818     break;
819     }
820     if(*s < 0x20)
821     {
822     if(*s == '\n')
823     {
824     if(maxlen < 0)
825     *cptr++ = ' ';
826     else
827     cptr += sprintf(cptr, "<br>");
828     }
829     }
830     else if(*s >= 0x7f)
831     cptr += sprintf(cptr, "&#%d;", (int)(unsigned char)*s);
832     else if(*s == '<')
833     cptr += sprintf(cptr, "&lt;");
834     else if(*s == '>')
835     cptr += sprintf(cptr, "&gt;");
836     else
837     *cptr++ = *s;
838     l++;
839     }
840     *cptr = '\0';
841     add_string_str(str);
842     xfree(str);
843     }
844    
845     static void add_string_str_len(const char *s, int maxlen)
846     {
847     int l = strlen(s);
848     char *str = xmalloc(l + 1 + 3);
849     strcpy(str, s);
850     if(maxlen < l)
851     sprintf(&str[maxlen], "...");
852     add_string_str(str);
853     xfree(str);
854     }
855    
856 bertho 1.9 char *expand_string(const char *s, rcsfile_t *rcs, revision_t *r, rev_t *rev, rev_t *prev, tag_t *tag)
857 bertho 1.8 {
858     char nb[32];
859     char nr[32];
860     char *base;
861 bertho 1.22 int l;
862     char ch;
863 bertho 1.8
864     if(!s)
865     return xstrdup("");
866    
867     _nstring = 0;
868     if(_string)
869     _string[0] = '\0';
870    
871     sprintf(nb, "%d", rcs->nbranches);
872     sprintf(nr, "%d", rcs->nsrev);
873     for(; *s; s++)
874     {
875     if(*s == '%')
876     {
877     switch(*++s)
878     {
879 bertho 1.12 case 'c':
880     case 'C':
881     add_string_str(conf.cvsroot);
882     if(*s == 'C' && conf.cvsroot[0] && conf.cvsroot[strlen(conf.cvsroot)-1] == '/')
883     {
884     /* Strip the trailing '/' */
885     _nstring--;
886     _string[_nstring] = '\0';
887     }
888     break;
889 bertho 1.8 case 'f':
890     case 'F':
891     base = strrchr(rcs->file, '/');
892     if(!base)
893     add_string_str(rcs->file);
894     else
895     add_string_str(base+1);
896     if(*s == 'F' && _string[_nstring-1] == 'v' && _string[_nstring-2] == ',')
897     {
898     _nstring -= 2;
899     _string[_nstring] = '\0';
900     }
901     break;
902     case 'p':
903     base = strrchr(rcs->file, '/');
904     if(base)
905     {
906     char *c = xstrdup(rcs->file);
907     base = strrchr(c, '/');
908     assert(base != NULL);
909     base[1] = '\0';
910     add_string_str(c);
911     xfree(c);
912     }
913 bertho 1.9 /*
914     * We should not add anything here because we can encounter
915     * a completely empty path, in which case we do not want
916 bertho 1.17 * to add any slash. This prevents an inadvertent root redirect.
917 bertho 1.9 *
918     * else
919     * add_string_str("/");
920     */
921 bertho 1.8 break;
922 bertho 1.12 case 'm':
923     case 'M':
924     add_string_str(conf.cvsmodule);
925     if(*s == 'M' && conf.cvsmodule[0] && conf.cvsmodule[strlen(conf.cvsmodule)-1] == '/')
926     {
927     /* Strip the trailing '/' */
928     _nstring--;
929     _string[_nstring] = '\0';
930     }
931     break;
932 bertho 1.8 case 'r': add_string_str(nr); break;
933     case 'b': add_string_str(nb); break;
934     case '%': add_string_ch('%'); break;
935     case '0': if(conf.expand[0]) add_string_str(conf.expand[0]); break;
936     case '1': if(conf.expand[1]) add_string_str(conf.expand[1]); break;
937     case '2': if(conf.expand[2]) add_string_str(conf.expand[2]); break;
938     case '3': if(conf.expand[3]) add_string_str(conf.expand[3]); break;
939     case '4': if(conf.expand[4]) add_string_str(conf.expand[4]); break;
940     case '5': if(conf.expand[5]) add_string_str(conf.expand[5]); break;
941     case '6': if(conf.expand[6]) add_string_str(conf.expand[6]); break;
942     case '7': if(conf.expand[7]) add_string_str(conf.expand[7]); break;
943     case '8': if(conf.expand[8]) add_string_str(conf.expand[8]); break;
944     case '9': if(conf.expand[9]) add_string_str(conf.expand[9]); break;
945     case 'R': if(rev && rev->rev) add_string_str(rev->rev); break;
946 bertho 1.9 case 'P': if(prev && prev->rev) add_string_str(prev->rev); break;
947 bertho 1.8 case 'B': if(rev && rev->branch) add_string_str(rev->branch); break;
948     case 't': if(tag && tag->tag) add_string_str(tag->tag); break;
949 bertho 1.9 case 'd': if(r && r->delta && r->delta->date) add_string_date(r->delta->date); break;
950     case 's': if(r && r->delta && r->delta->state) add_string_str(r->delta->state); break;
951     case 'a': if(r && r->delta && r->delta->author) add_string_str(r->delta->author); break;
952 bertho 1.22 case 'L':
953     case 'l':
954     ch = *s;
955     l = 0;
956     if(s[1] == '[')
957     {
958     char *cptr = strchr(s, ']');
959     char *eptr;
960     if(cptr)
961     {
962     l = strtol(&s[2], &eptr, 10);
963     if(eptr != cptr)
964     l = 0;
965     else
966     s = cptr;
967     }
968     }
969     if(!conf.parse_logs)
970     add_string_str("N/A");
971     else if(r && r->dtext && r->dtext->log)
972     {
973     if(ch == 'l')
974     add_string_str_html(r->dtext->log, l);
975     else
976     add_string_str_len(r->dtext->log, abs(l));
977     }
978     break;
979 bertho 1.8 default:
980     add_string_ch('%');
981     add_string_ch(*s);
982     break;
983     }
984     }
985     else
986     add_string_ch(*s);
987     }
988     return xstrdup(_string);
989     }
990    
991     /*
992     **************************************************************************
993 bertho 1.1 * Drawing routines
994     **************************************************************************
995     */
996 bertho 1.19 static int get_swidth(const char *s, font_t *f)
997 bertho 1.1 {
998 bertho 1.9 int n;
999     int m;
1000     if(!s || !*s)
1001 bertho 1.1 return 0;
1002 bertho 1.19
1003     #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1004     if(conf.use_ttf && f->ttfont)
1005     {
1006     int bb[8];
1007     char *e;
1008     #ifdef HAVE_GDIMAGESTRINGFT
1009     e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1010     #else
1011     e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1012     #endif
1013     if(!e)
1014     return bb[2] - bb[6];
1015     }
1016     #endif
1017 bertho 1.9 for(n = m = 0; *s; n++, s++)
1018     {
1019     if(*s == '\n')
1020     {
1021     if(n > m)
1022     m = n;
1023     n = 0;
1024     }
1025     }
1026     if(n > m)
1027     m = n;
1028 bertho 1.22 return f->gdfont ? m * f->gdfont->w : m;
1029 bertho 1.1 }
1030    
1031 bertho 1.19 static int get_sheight(const char *s, font_t *f)
1032 bertho 1.1 {
1033     int nl;
1034 bertho 1.9 if(!s || !*s)
1035 bertho 1.1 return 0;
1036 bertho 1.19
1037     #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1038     if(conf.use_ttf && f->ttfont)
1039     {
1040     int bb[8];
1041     char *e;
1042     #ifdef HAVE_GDIMAGESTRINGFT
1043     e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1044     #else
1045     e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1046     #endif
1047     if(!e)
1048     return bb[3] - bb[7] + 4;
1049     }
1050     #endif
1051 bertho 1.1 for(nl = 1; *s; s++)
1052     {
1053     if(*s == '\n' && s[1])
1054     nl++;
1055     }
1056 bertho 1.19 return nl * f->gdfont->h;
1057 bertho 1.1 }
1058    
1059 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)
1060 bertho 1.1 {
1061     int r2 = 2*r;
1062     gdImageLine(im, x1+r, y1, x2-r, y1, color->id);
1063     gdImageLine(im, x1+r, y2, x2-r, y2, color->id);
1064     gdImageLine(im, x1, y1+r, x1, y2-r, color->id);
1065     gdImageLine(im, x2, y1+r, x2, y2-r, color->id);
1066 bertho 1.9 if(conf.box_shadow)
1067     {
1068     gdImageLine(im, x1+r+1, y2+1, x2-r, y2+1, black_color.id);
1069     gdImageLine(im, x2+1, y1+r+1, x2+1, y2-r, black_color.id);
1070     }
1071 bertho 1.1 if(r)
1072     {
1073 bertho 1.18 /* FIXME: Pixelization is not perfect */
1074 bertho 1.1 gdImageArc(im, x1+r, y1+r, r2, r2, 180, 270, color->id);
1075     gdImageArc(im, x2-r, y1+r, r2, r2, 270, 360, color->id);
1076     gdImageArc(im, x1+r, y2-r, r2, r2, 90, 180, color->id);
1077 bertho 1.9 if(conf.box_shadow)
1078     {
1079     gdImageArc(im, x2-r+1, y2-r+1, r2, r2, 0, 90, black_color.id);
1080 bertho 1.18 gdImageArc(im, x2-r+1, y2-r, r2, r2, 0, 90, black_color.id);
1081     gdImageArc(im, x2-r, y2-r+1, r2, r2, 0, 90, black_color.id);
1082 bertho 1.9 }
1083 bertho 1.18 gdImageArc(im, x2-r, y2-r, r2, r2, 0, 90, color->id);
1084 bertho 1.1 }
1085 bertho 1.19 #ifndef NOGDFILL
1086 bertho 1.9 gdImageFillToBorder(im, (x1+x2)/2, (y1+y2)/2, color->id, bgcolor->id);
1087 bertho 1.19 #endif
1088 bertho 1.1 }
1089    
1090 bertho 1.19 static void draw_string(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1091 bertho 1.1 {
1092 bertho 1.19 int h = get_sheight(s, f);
1093 bertho 1.1 int xx, yy;
1094     switch(align & ALIGN_HX)
1095     {
1096     default:
1097     case ALIGN_HL: xx = 0; break;
1098     case ALIGN_HC: xx = -get_swidth(s, f)/2; break;
1099     case ALIGN_HR: xx = -get_swidth(s, f); break;
1100     }
1101     switch(align & ALIGN_VX)
1102     {
1103     default:
1104     case ALIGN_VT: yy = 0; break;
1105 bertho 1.19 case ALIGN_VC: yy = h/2; break;
1106     case ALIGN_VB: yy = h; break;
1107 bertho 1.1 }
1108 bertho 1.19 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1109     if(conf.use_ttf && f->ttfont)
1110     {
1111     int bb[8];
1112     char *e;
1113     int cid = conf.anti_alias ? c->id : -c->id;
1114     #ifdef HAVE_GDIMAGESTRINGFT
1115     e = gdImageStringFT(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1116     #else
1117     e = gdImageStringTTF(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1118     #endif
1119     if(!e)
1120     return;
1121     }
1122     #endif
1123     yy = -yy;
1124     gdImageString(im, f->gdfont, x+xx+1, y+yy, s, c->id);
1125 bertho 1.1 }
1126    
1127 bertho 1.19 static void draw_stringnl(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1128 bertho 1.9 {
1129     char *t;
1130     char *d;
1131     d = s = xstrdup(s);
1132     do
1133     {
1134     t = strchr(s, '\n');
1135     if(t)
1136     *t = '\0';
1137     draw_string(im, s, f, x, y, align, c);
1138     y += get_sheight(s, f);
1139     s = t+1;
1140     } while(t);
1141     xfree(d);
1142     }
1143    
1144 bertho 1.19 static void draw_rev(gdImagePtr im, revision_t *r)
1145 bertho 1.1 {
1146 bertho 1.25 int lx;
1147     int rx;
1148     int x2;
1149 bertho 1.1 int i;
1150 bertho 1.25 int ty;
1151    
1152     if(conf.left_right)
1153     {
1154     lx = r->cx;
1155     rx = r->cx + r->w;
1156     ty = r->y - r->h/2;
1157     x2 = r->cx + r->w/2;
1158     }
1159     else
1160     {
1161     lx = r->cx - r->w/2;
1162     rx = lx + r->w;
1163     ty = r->y;
1164     x2 = r->cx;
1165     }
1166 bertho 1.9 draw_rbox(im, lx, ty, rx, ty+r->h, 0, &conf.rev_color, &conf.rev_bgcolor);
1167 bertho 1.1 ty += conf.rev_tspace;
1168 bertho 1.25 draw_string(im, r->rev->rev, &conf.rev_font, x2, ty, ALIGN_HC, &conf.rev_color);
1169 bertho 1.1 ty += get_sheight(r->rev->rev, &conf.rev_font);
1170 bertho 1.25 draw_stringnl(im, r->revtext, &conf.rev_text_font, x2, ty, ALIGN_HC, &conf.rev_text_color);
1171 bertho 1.9 ty += get_sheight(r->revtext, &conf.rev_text_font);
1172 bertho 1.1 for(i = 0; i < r->ntags; i++)
1173     {
1174 bertho 1.25 draw_string(im, r->tags[i]->tag, &conf.tag_font, x2, ty, ALIGN_HC, &conf.tag_color);
1175 bertho 1.19 ty += get_sheight(r->tags[i]->tag, &conf.tag_font) + conf.rev_separator;
1176 bertho 1.1 }
1177     }
1178    
1179 bertho 1.26 static void draw_branch_box(gdImagePtr im, branch_t *b, int xp, int yp)
1180 bertho 1.1 {
1181 bertho 1.25 int lx;
1182     int rx;
1183 bertho 1.16 int i;
1184 bertho 1.1 int yy;
1185 bertho 1.25 int x2;
1186 bertho 1.16
1187 bertho 1.25 if(conf.left_right)
1188     {
1189     lx = b->cx;
1190     rx = lx + b->w;
1191     x2 = b->cx + b->w/2;
1192     }
1193     else
1194     {
1195     lx = b->cx - b->w/2;
1196     rx = lx + b->w;
1197     x2 = b->cx;
1198     }
1199 bertho 1.26 draw_rbox(im, lx+xp, yp, rx+xp, yp+b->h, 5, &conf.branch_color, &conf.branch_bgcolor);
1200 bertho 1.1 yy = conf.branch_tspace;
1201 bertho 1.26 draw_string(im, b->branch->branch, &conf.branch_font, x2+xp, yp+yy, ALIGN_HC, &conf.branch_color);
1202 bertho 1.7 yy += get_sheight(b->branch->branch, &conf.branch_font);
1203     for(i = 0; i < b->ntags; i++)
1204 bertho 1.1 {
1205 bertho 1.26 draw_string(im, b->tags[i]->tag, &conf.branch_tag_font, x2+xp, yp+yy, ALIGN_HC, &conf.branch_tag_color);
1206 bertho 1.7 yy += get_sheight(b->tags[i]->tag, &conf.branch_font);
1207 bertho 1.1 }
1208 bertho 1.16 }
1209    
1210 bertho 1.19 static void draw_branch(gdImagePtr im, branch_t *b)
1211 bertho 1.16 {
1212 bertho 1.25 int yy, xx;
1213 bertho 1.16 int i;
1214 bertho 1.17 int line[4];
1215 bertho 1.20 int l;
1216     int sign;
1217 bertho 1.17
1218     line[0] = conf.rev_color.id;
1219     line[1] = gdTransparent;
1220     line[1] = gdTransparent;
1221     line[3] = conf.rev_color.id;
1222    
1223 bertho 1.26 draw_branch_box(im, b, 0, conf.left_right ? b->y - b->h/2 : b->y);
1224 bertho 1.1
1225 bertho 1.25 if(conf.left_right)
1226 bertho 1.16 {
1227 bertho 1.25 if(conf.upside_down)
1228 bertho 1.16 {
1229 bertho 1.25 xx = b->cx;
1230     for(i = 0; i < b->nrevs; i++)
1231 bertho 1.20 {
1232 bertho 1.25 revision_t *r = b->revs[i];
1233     gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1234     gdImageLine(im, xx, r->y, r->cx+r->w, r->y, gdStyled);
1235     for(sign = l = 1; l < conf.thick_lines; l++)
1236     {
1237     int pp = (l+1)/2*sign;
1238     gdImageLine(im, xx, r->y+pp, r->cx+r->w, r->y+pp, gdStyled);
1239     sign *= -1;
1240     }
1241     draw_rev(im, r);
1242     xx = r->cx;
1243     }
1244     if(conf.branch_dupbox)
1245     {
1246     i = b->cx - b->tw + b->w;
1247 bertho 1.26 gdImageLine(im, xx, b->y, i+b->w, b->y, conf.rev_color.id);
1248 bertho 1.25 for(sign = l = 1; l < conf.thick_lines; l++)
1249     {
1250     int pp = (l+1)/2*sign;
1251 bertho 1.26 gdImageLine(im, xx, b->y+pp, i+b->w, b->y+pp, conf.rev_color.id);
1252 bertho 1.25 sign *= -1;
1253     }
1254 bertho 1.26 draw_branch_box(im, b, i - b->cx, b->y - b->h/2);
1255 bertho 1.20 }
1256 bertho 1.16 }
1257 bertho 1.25 else
1258 bertho 1.16 {
1259 bertho 1.25 xx = b->cx + b->w;
1260     for(i = 0; i < b->nrevs; i++)
1261     {
1262     revision_t *r = b->revs[i];
1263     gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1264     gdImageLine(im, xx, r->y, r->cx, r->y, gdStyled);
1265     for(sign = l = 1; l < conf.thick_lines; l++)
1266     {
1267     int pp = (l+1)/2*sign;
1268     gdImageLine(im, xx, r->y+pp, r->cx, r->y+pp, gdStyled);
1269     sign *= -1;
1270     }
1271     draw_rev(im, r);
1272     xx = r->cx + r->w;
1273     }
1274     if(conf.branch_dupbox)
1275 bertho 1.20 {
1276 bertho 1.25 i = b->cx + b->tw - b->w;
1277     gdImageLine(im, xx, b->y, i, b->y, conf.rev_color.id);
1278     for(sign = l = 1; l < conf.thick_lines; l++)
1279     {
1280     int pp = (l+1)/2*sign;
1281     gdImageLine(im, xx, b->y+pp, i, b->y+pp, conf.rev_color.id);
1282     sign *= -1;
1283     }
1284 bertho 1.26 draw_branch_box(im, b, i - b->cx, b->y - b->h/2);
1285 bertho 1.20 }
1286 bertho 1.16 }
1287     }
1288     else
1289 bertho 1.1 {
1290 bertho 1.25 if(conf.upside_down)
1291 bertho 1.16 {
1292 bertho 1.25 yy = b->y;
1293     for(i = 0; i < b->nrevs; i++)
1294 bertho 1.20 {
1295 bertho 1.25 revision_t *r = b->revs[i];
1296     gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1297     gdImageLine(im, r->cx, yy, r->cx, r->y+r->h, gdStyled);
1298     for(sign = l = 1; l < conf.thick_lines; l++)
1299     {
1300     int pp = (l+1)/2*sign;
1301     gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y+r->h, gdStyled);
1302     sign *= -1;
1303     }
1304     draw_rev(im, r);
1305     yy = r->y;
1306     }
1307     if(conf.branch_dupbox)
1308     {
1309     i = b->y - b->th + b->h;
1310     gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1311     for(sign = l = 1; l < conf.thick_lines; l++)
1312     {
1313     int pp = (l+1)/2*sign;
1314     gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, conf.rev_color.id);
1315     sign *= -1;
1316     }
1317 bertho 1.26 draw_branch_box(im, b, 0, i);
1318 bertho 1.20 }
1319 bertho 1.16 }
1320 bertho 1.25 else
1321 bertho 1.16 {
1322 bertho 1.25 yy = b->y + b->h;
1323     for(i = 0; i < b->nrevs; i++)
1324     {
1325     revision_t *r = b->revs[i];
1326     gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1327     gdImageLine(im, r->cx, yy, r->cx, r->y, gdStyled);
1328     for(sign = l = 1; l < conf.thick_lines; l++)
1329     {
1330     int pp = (l+1)/2*sign;
1331     gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y, gdStyled);
1332     sign *= -1;
1333     }
1334     draw_rev(im, r);
1335     yy = r->y + r->h;
1336     }
1337     if(conf.branch_dupbox)
1338 bertho 1.20 {
1339 bertho 1.25 i = b->y + b->th - b->h;
1340     gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1341     for(sign = l = 1; l < conf.thick_lines; l++)
1342     {
1343     int pp = (l+1)/2*sign;
1344     gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, conf.rev_color.id);
1345     sign *= -1;
1346     }
1347 bertho 1.26 draw_branch_box(im, b, 0, i);
1348 bertho 1.20 }
1349 bertho 1.16 }
1350 bertho 1.1 }
1351     }
1352    
1353 bertho 1.19 static void draw_connector(gdImagePtr im, branch_t *b)
1354 bertho 1.1 {
1355 bertho 1.20 int l;
1356     int sign;
1357 bertho 1.1 revision_t *r = b->branchpoint;
1358 bertho 1.7 int x1 = r->cx + r->w/2 + 2;
1359 bertho 1.1 int y1 = r->y + r->h/2;
1360 bertho 1.7 int x2 = b->cx;
1361 bertho 1.1 int y2 = b->y;
1362 bertho 1.25
1363     if(conf.left_right)
1364     {
1365     x2 = r->cx + r->w/2;
1366 bertho 1.26 y2 = r->y + r->h/2 + 3;
1367 bertho 1.25 x1 = b->cx;
1368     y1 = b->y;
1369     if(conf.upside_down)
1370     x1 += b->w;
1371     }
1372     else
1373     {
1374     x1 = r->cx + r->w/2 + 2;
1375     y1 = r->y + r->h/2;
1376     x2 = b->cx;
1377     y2 = b->y;
1378     if(conf.upside_down)
1379     y2 += b->h;
1380     }
1381 bertho 1.1 gdImageLine(im, x1, y1, x2, y1, conf.branch_color.id);
1382     gdImageLine(im, x2, y1, x2, y2, conf.branch_color.id);
1383 bertho 1.20 for(sign = l = 1; l < conf.thick_lines; l++)
1384     {
1385     int pp = (l+1)/2*sign;
1386 bertho 1.26 gdImageLine(im, x1, y1+pp, x2, y1+pp, conf.branch_color.id);
1387     gdImageLine(im, x2+pp, y1, x2+pp, y2, conf.branch_color.id);
1388 bertho 1.20 sign *= -1;
1389     }
1390 bertho 1.1 }
1391    
1392 bertho 1.19 static void alloc_color(gdImagePtr im, color_t *c)
1393 bertho 1.9 {
1394     c->id = gdImageColorAllocate(im, c->r, c->g, c->b);
1395     }
1396    
1397 bertho 1.7 gdImagePtr make_image(rcsfile_t *rcs)
1398 bertho 1.1 {
1399     gdImagePtr im;
1400     int i;
1401 bertho 1.8 char *cptr;
1402 bertho 1.1
1403 bertho 1.15 cptr = expand_string(conf.title, rcs, NULL, NULL, NULL, NULL);
1404     i = get_swidth(cptr, &conf.title_font);
1405     if(rcs->tw+conf.margin_left+conf.margin_right > i)
1406     i = rcs->tw+conf.margin_left+conf.margin_right;
1407     im = gdImageCreate(i, rcs->th+conf.margin_top+conf.margin_bottom);
1408 bertho 1.9 alloc_color(im, &conf.color_bg);
1409     alloc_color(im, &conf.tag_color);
1410     alloc_color(im, &conf.rev_color);
1411     alloc_color(im, &conf.rev_bgcolor);
1412     alloc_color(im, &conf.rev_text_color);
1413     alloc_color(im, &conf.branch_color);
1414 bertho 1.19 alloc_color(im, &conf.branch_tag_color);
1415 bertho 1.9 alloc_color(im, &conf.branch_bgcolor);
1416     alloc_color(im, &conf.title_color);
1417     alloc_color(im, &black_color);
1418     alloc_color(im, &white_color);
1419 bertho 1.1
1420 bertho 1.19 if(conf.transparent_bg)
1421     gdImageColorTransparent(im, conf.color_bg.id);
1422    
1423 bertho 1.1 for(i = 0; i < rcs->nbranches; i++)
1424 bertho 1.16 draw_branch(im, rcs->branches[i]);
1425 bertho 1.1 for(i = 0; i < rcs->nbranches; i++)
1426     {
1427     if(rcs->branches[i]->branchpoint)
1428     draw_connector(im, rcs->branches[i]);
1429     }
1430 bertho 1.9 draw_stringnl(im, cptr, &conf.title_font, conf.title_x, conf.title_y, conf.title_align, &conf.title_color);
1431 bertho 1.8 xfree(cptr);
1432 bertho 1.1
1433     return im;
1434     }
1435    
1436 bertho 1.7 /*
1437     **************************************************************************
1438     * Layout routines
1439 bertho 1.19 *
1440     * Branch BBox:
1441     * left = center_x - total_width / 2 (cx-tw)/2
1442     * right = center_x + total_width / 2 (cx+tw)/2
1443     * top = y_pos (y)
1444     * bottom = y_pos + total_height (y+th)
1445     *
1446     * Margins of branches:
1447     *
1448     * . .
1449     * . .
1450     * +--------------+
1451     * ^
1452     * | branch_margin .
1453     * v .
1454     * ----------------+ .
1455     * | ^ |
1456     * | | branch_connect |
1457     * | v |
1458     *..-+ +t-----+------+ +------+------+
1459     * | l | | |
1460     * | <--> | branch bbox | <--> | branch bbox |
1461     * | | | r | | |
1462     *..-+ | +------------b+ | +-------------+
1463     * | ^ branch_margin
1464     * | | branch_margin
1465     * | v
1466     * | +-------------+
1467     * | . .
1468     * | . .
1469     * |
1470     * branch_margin
1471     *
1472     * FIXME: There are probable som +/-1 errors in the code...
1473     * (notably shadows are not calculated in the margins)
1474 bertho 1.7 **************************************************************************
1475     */
1476 bertho 1.19 static void move_branch(branch_t *b, int x, int y)
1477 bertho 1.1 {
1478     int i;
1479 bertho 1.7 b->cx += x;
1480 bertho 1.1 b->y += y;
1481     for(i = 0; i < b->nrevs; i++)
1482     {
1483 bertho 1.7 b->revs[i]->cx += x;
1484 bertho 1.1 b->revs[i]->y += y;
1485     }
1486     }
1487    
1488 bertho 1.19 static void initial_reposition_branch(revision_t *r, int *x, int *w)
1489 bertho 1.6 {
1490     int i, j;
1491     for(j = 0; j < r->nbranches; j++)
1492     {
1493     branch_t *b = r->branches[j];
1494 bertho 1.7 *x += *w + conf.rev_minline + b->tw/2 - b->cx;
1495 bertho 1.6 *w = b->tw/2;
1496 bertho 1.7 move_branch(b, *x, r->y + r->h/2 + conf.branch_connect);
1497     *x = b->cx;
1498 bertho 1.6 /* Recurse to move branches of branched revisions */
1499     for(i = b->nrevs-1; i >= 0; i--)
1500     {
1501 bertho 1.18 initial_reposition_branch(b->revs[i], x, w);
1502 bertho 1.6 }
1503     }
1504     }
1505    
1506 bertho 1.25 static void initial_reposition_branch_lr(revision_t *r, int *y, int *h)
1507     {
1508     int i, j;
1509     for(j = 0; j < r->nbranches; j++)
1510     {
1511     branch_t *b = r->branches[j];
1512     *y += *h + conf.rev_minline + b->th/2 - b->y;
1513     *h = b->th/2;
1514     move_branch(b, r->cx + r->w/2 + conf.branch_connect, *y);
1515     *y = b->y;
1516     /* Recurse to move branches of branched revisions */
1517     for(i = b->nrevs-1; i >= 0; i--)
1518     {
1519     initial_reposition_branch(b->revs[i], y, h);
1520     }
1521     }
1522     }
1523    
1524 bertho 1.19 static void rect_union(int *x, int *y, int *w, int *h, branch_t *b)
1525 bertho 1.1 {
1526     int x1 = *x;
1527     int x2 = x1 + *w;
1528     int y1 = *y;
1529     int y2 = y1 + *h;
1530 bertho 1.25 int xx1;
1531     int xx2;
1532     int yy1;
1533     int yy2;
1534    
1535     if(conf.left_right)
1536     {
1537     xx1 = b->cx;
1538     yy1 = b->y - b->th/2;
1539     }
1540     else
1541     {
1542     xx1 = b->cx - b->tw/2;
1543     yy1 = b->y;
1544     }
1545     xx2 = xx1 + b->tw;
1546     yy2 = yy1 + b->th;
1547    
1548 bertho 1.1 x1 = MIN(x1, xx1);
1549     x2 = MAX(x2, xx2);
1550     y1 = MIN(y1, yy1);
1551     y2 = MAX(y2, yy2);
1552     *x = x1;
1553     *y = y1;
1554     *w = x2 - x1;
1555     *h = y2 - y1;
1556     }
1557    
1558 bertho 1.19 static int branch_intersects(int top, int bottom, int left, branch_t *b)
1559 bertho 1.7 {
1560     int br = b->cx + b->tw/2;
1561     int bt = b->y - conf.branch_connect - conf.branch_margin/2;
1562     int bb = b->y + b->th + conf.branch_margin/2;
1563     return !(bt > bottom || bb < top || br >= left);
1564     }
1565    
1566 bertho 1.25 static int branch_intersects_lr(int left, int right, int top, branch_t *b)
1567     {
1568     int bt = b->y + b->th/2;
1569     int bl = b->cx - conf.branch_connect - conf.branch_margin/2;
1570     int br = b->cx + b->tw + conf.branch_margin/2;
1571     return !(bl > right || br < left || bt >= top);
1572     }
1573    
1574 bertho 1.19 static int kern_branch(rcsfile_t *rcs, branch_t *b)
1575 bertho 1.7 {
1576     int left = b->cx - b->tw/2;
1577     int top = b->y - conf.branch_connect - conf.branch_margin/2;
1578     int bottom = b->y + b->th + conf.branch_margin/2;
1579     int i;
1580     int xpos = 0;
1581    
1582     for(i = 0; i < rcs->nbranches; i++)
1583     {
1584     branch_t *bp = rcs->branches[i];
1585     if(bp == b)
1586     continue;
1587     if(branch_intersects(top, bottom, left, bp))
1588     {
1589     int m = bp->cx + bp->tw/2 + conf.branch_margin;
1590     if(m > xpos)
1591     xpos = m;
1592     }
1593     }
1594     if(xpos && (b->cx - b->tw/2) - xpos > 0)
1595     {
1596     move_branch(b, xpos - (b->cx - b->tw/2), 0);
1597     return 1;
1598     }
1599     return 0;
1600     }
1601    
1602 bertho 1.25 static int kern_branch_lr(rcsfile_t *rcs, branch_t *b)
1603     {
1604     int top = b->y - b->th/2;
1605     int left = b->cx - conf.branch_connect - conf.branch_margin/2;
1606     int right = b->cx + b->tw + conf.branch_margin/2;
1607     int i;
1608     int ypos = 0;
1609    
1610     for(i = 0; i < rcs->nbranches; i++)
1611     {
1612     branch_t *bp = rcs->branches[i];
1613     if(bp == b)
1614     continue;
1615     if(branch_intersects_lr(left, right, top, bp))
1616     {
1617     int m = bp->y + bp->th/2 + conf.branch_margin;
1618     if(m > ypos)
1619     ypos = m;
1620     }
1621     }
1622     if(ypos && (b->y - b->th/2) - ypos > 0)
1623     {
1624     move_branch(b, 0, ypos - (b->y - b->th/2));
1625     return 1;
1626     }
1627     return 0;
1628     }
1629    
1630 bertho 1.19 static int kern_tree(rcsfile_t *rcs)
1631 bertho 1.18 {
1632     int i;
1633     int moved;
1634 bertho 1.19 int safeguard;
1635     int totalmoved = 0;
1636     for(moved = 1, safeguard = LOOPSAFEGUARD; moved && safeguard; safeguard--)
1637 bertho 1.18 {
1638     moved = 0;
1639     for(i = 1; i < rcs->nbranches; i++)
1640     {
1641 bertho 1.25 if(conf.left_right)
1642     moved += kern_branch_lr(rcs, rcs->branches[i]);
1643     else
1644     moved += kern_branch(rcs, rcs->branches[i]);
1645 bertho 1.18 }
1646 bertho 1.19 totalmoved += moved;
1647 bertho 1.18 #ifdef DEBUG
1648     fprintf(stderr, "kern_tree: moved=%d\n", moved);
1649     #endif
1650     }
1651 bertho 1.19 if(!quiet && !safeguard)
1652     fprintf(stderr, "kern_tree: safeguard terminated possible infinite loop; please report.\n");
1653     return totalmoved;
1654     }
1655    
1656     static int index_of_revision(revision_t *r)
1657     {
1658     branch_t *b = r->branch;
1659     int i;
1660     for(i = 0; i < b->nrevs; i++)
1661     {
1662     if(r == b->revs[i])
1663     return i;
1664     }
1665     fprintf(stderr, "index_of_revision: Cannot find revision in branch\n");
1666     return 0;
1667 bertho 1.18 }
1668    
1669 bertho 1.19 static void branch_bbox(branch_t *br, int *l, int *r, int *t, int *b)
1670     {
1671     if(l) *l = br->cx - br->tw/2;
1672     if(r) *r = br->cx + br->tw/2;
1673     if(t) *t = br->y;
1674     if(b) *b = br->y + br->th + (conf.branch_dupbox ? conf.rev_minline + br->h : 0);
1675     }
1676    
1677     static void branch_ext_bbox(branch_t *br, int *l, int *r, int *t, int *b)
1678     {
1679     int extra = conf.branch_margin & 1; /* Correct +/-1 error on div 2 */
1680     branch_bbox(br, l, r, t, b);
1681     if(l) *l -= conf.branch_margin/2;
1682     if(r) *r += conf.branch_margin/2 + extra;
1683     if(t) *t -= conf.branch_connect + conf.branch_margin/2;
1684     if(b) *b += conf.branch_margin/2 + extra;
1685     }
1686    
1687     static int branch_distance(branch_t *br1, branch_t *br2)
1688     {
1689     int l1, r1, t1, b1;
1690     int l2, r2, t2, b2;
1691     assert(br1 != NULL);
1692     assert(br2 != NULL);
1693     branch_bbox(br1, &l1, &r1, NULL, NULL);
1694     branch_bbox(br2, &l2, &r2, NULL, NULL);
1695     branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
1696     branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
1697     /* Return:
1698     * - 0 if branches have no horizontal overlap
1699     * - positive if b1 is left of b2
1700     * - negative if b2 is left of b1
1701     */
1702     if((t1 > t2 && t1 < b2) || (b1 > t2 && b1 < b2))
1703     return l1 < l2 ? l2 - r1 : -(l1 - r2);
1704     else
1705     return 0;
1706     }
1707    
1708     static int space_needed(branch_t *br1, branch_t *br2)
1709     {
1710     int t1, b1;
1711     int t2, b2;
1712     assert(br1 != NULL);
1713     assert(br2 != NULL);
1714     assert(br1->cx < br2->cx); /* br1 must be left of br2 */
1715     branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
1716     branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
1717     /* Return:
1718     * - positive if top br1 is located lower than br2
1719     * - negatve is top br2 is located lower than br1
1720     */
1721     if(t1 > t2)
1722     return -(t1 - b2);
1723     else
1724     return t2 - b1;
1725     }
1726    
1727     static void move_yr_branch(branch_t *b, int dy)
1728 bertho 1.18 {
1729     int i, j;
1730     #ifdef DEBUG
1731 bertho 1.23 /* fprintf(stderr, "move_yr_branch: b=%s, dy=%d\n", b->branch->branch, dy);*/
1732 bertho 1.18 #endif
1733 bertho 1.19 b->y += dy;
1734     for(i = 0; i < b->nrevs; i++)
1735 bertho 1.18 {
1736 bertho 1.19 b->revs[i]->y += dy;
1737     for(j = 0; j < b->revs[i]->nbranches; j++)
1738 bertho 1.18 {
1739 bertho 1.19 #ifdef DEBUG
1740 bertho 1.23 /* fprintf(stderr, ".");*/
1741 bertho 1.19 #endif
1742     move_yr_branch(b->revs[i]->branches[j], dy);
1743 bertho 1.18 }
1744     }
1745     }
1746    
1747 bertho 1.19 static void move_trunk(revision_t *r, int dy)
1748 bertho 1.18 {
1749 bertho 1.19 int i, j;
1750 bertho 1.18 branch_t *b = r->branch;
1751 bertho 1.19 b->th += dy;
1752     for(i = index_of_revision(r); i < b->nrevs; i++)
1753 bertho 1.18 {
1754 bertho 1.19 #ifdef DEBUG
1755     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);
1756     #endif
1757     b->revs[i]->y += dy;
1758     for(j = 0; j < b->revs[i]->nbranches; j++)
1759     {
1760     move_yr_branch(b->revs[i]->branches[j], dy);
1761     }
1762 bertho 1.18 }
1763     }
1764    
1765 bertho 1.19 static int space_below(rcsfile_t *rcs, revision_t *r)
1766 bertho 1.18 {
1767 bertho 1.19 int i, j;
1768     int bl, br, bb;
1769 bertho 1.18 int space = INT_MAX;
1770 bertho 1.19 branch_t *b = r->branch;
1771     branch_t *minb = NULL;
1772    
1773     branch_ext_bbox(b, &bl, &br, NULL, &bb);
1774 bertho 1.18 for(i = 0; i < rcs->nbranches; i++)
1775     {
1776 bertho 1.19 int tbl, tbr, tbt;
1777 bertho 1.18 branch_t *tb = rcs->branches[i];
1778 bertho 1.19 branch_ext_bbox(tb, &tbl, &tbr, &tbt, NULL);
1779 bertho 1.18 if(tb == b)
1780     continue;
1781 bertho 1.19 if(tbt > bb) /* Must be below our branch */
1782 bertho 1.18 {
1783 bertho 1.19 if(tb->branchpoint) /* Take account for the horiz connector */
1784     tbl = tb->branchpoint->cx + tb->branchpoint->branch->tw/2;
1785     if((bl >= tbl && bl <= tbr) || (br <= tbr && br >= tbl))
1786 bertho 1.18 {
1787 bertho 1.19 int s = tbt - bb - conf.branch_connect;
1788     if(s < space)
1789 bertho 1.18 {
1790 bertho 1.19 space = s;
1791     minb = tb;
1792 bertho 1.18 }
1793     }
1794     }
1795     }
1796 bertho 1.19 if(b->branchpoint)
1797     {
1798     for(i = index_of_revision(r); i < b->nrevs; i++)
1799     {
1800     for(j = 0; j < b->revs[i]->nbranches; j++)
1801     {
1802     int s = space_below(rcs, b->revs[i]->branches[j]->revs[0]);
1803     if(s < space)
1804     space = s;
1805     }
1806     }
1807     }
1808     #ifdef DEBUG
1809     fprintf(stderr, "space_below: from %s have %d to %s\n", b->branch->branch, space, minb ? minb->branch->branch : "<recursed>");
1810     #endif
1811 bertho 1.18 return space;
1812     }
1813    
1814 bertho 1.19 static int space_available(rcsfile_t *rcs, branch_t *colbr, branch_t *tagbr, int *nl, revision_t **bpcommon)
1815 bertho 1.18 {
1816 bertho 1.19 int i;
1817     int space = 0;
1818     int nlinks = 0;
1819     revision_t *r;
1820     branch_t *b;
1821     branch_t *ancestor;
1822     revision_t *branchpoint;
1823    
1824     if(!tagbr->branchpoint || !colbr->branchpoint)
1825 bertho 1.18 {
1826 bertho 1.19 if(!quiet)
1827     fprintf(stderr, "space_available: Trying to stretch the top?\n");
1828     return 0;
1829 bertho 1.18 }
1830 bertho 1.19
1831     r = colbr->branchpoint;
1832     b = r->branch;
1833     branchpoint = tagbr->branchpoint;
1834     ancestor = branchpoint->branch;
1835     assert(b != NULL);
1836     assert(ancestor != NULL);
1837    
1838     while(1)
1839 bertho 1.18 {
1840 bertho 1.19 int s;
1841     int rtag = b == ancestor ? index_of_revision(branchpoint)+1 : 0;
1842     for(i = index_of_revision(r); i >= rtag; i--)
1843 bertho 1.18 {
1844 bertho 1.19 if(i > 0)
1845     s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
1846     else
1847     s = b->revs[i]->y - (b->y + b->h);
1848     if(s < conf.rev_maxline)
1849     {
1850     space += conf.rev_maxline - s;
1851     nlinks++;
1852     }
1853 bertho 1.18 }
1854 bertho 1.19 s = space_below(rcs, r);
1855     if(s < space)
1856     space = s;
1857     #ifdef DEBUG
1858     if(space < 0)
1859     return -1;
1860     #endif
1861     if(b == ancestor)
1862     break;
1863     r = b->branchpoint;
1864     if(!r)
1865     {
1866     /* Not a common ancestor */
1867     r = colbr->branchpoint;
1868     b = r->branch;
1869     branchpoint = ancestor->branchpoint;
1870     if(!branchpoint)
1871     {
1872     if(!quiet)
1873     fprintf(stderr, "space_available: No common ancestor?\n");
1874     return 0;
1875     }
1876     ancestor = branchpoint->branch;
1877     assert(ancestor != NULL);
1878     nlinks = 0;
1879     space = 0;
1880     continue; /* Restart with a new ancestor */
1881     }
1882     b = r->branch;
1883     }
1884     if(nl)
1885     *nl = nlinks; /* Return the number of links that can stretch */
1886     if(bpcommon)
1887     *bpcommon = branchpoint; /* Return the ancestral branchpoint on the common branch */
1888     return space;
1889 bertho 1.18 }
1890    
1891 bertho 1.19 static int stretch_branches(rcsfile_t *rcs, branch_t *br1, branch_t *br2, int totalstretch)
1892 bertho 1.18 {
1893 bertho 1.19 revision_t *r;
1894     revision_t *bpcommon = NULL;
1895     branch_t *ancestor = NULL;
1896     branch_t *b;
1897 bertho 1.18 int i;
1898 bertho 1.19 int space;
1899     int nlinks;
1900     int dy;
1901     int rest;
1902    
1903     space = space_available(rcs, br1, br2, &nlinks, &bpcommon);
1904     if(bpcommon)
1905     ancestor = bpcommon->branch;
1906    
1907     #ifdef DEBUG
1908     if(space == -1)
1909     return 0;
1910     fprintf(stderr, "stretch_branches: space available %d over %d links common %s\n", space, nlinks, ancestor->branch->branch);
1911     #endif
1912     if(space < totalstretch)
1913     return 0;
1914    
1915     dy = totalstretch / nlinks;
1916     rest = totalstretch - dy * nlinks;
1917    
1918     r = br1->branchpoint;
1919     b = r->branch;
1920     while(1)
1921 bertho 1.18 {
1922 bertho 1.19 int rtag = b == ancestor ? index_of_revision(bpcommon)+1 : 0;
1923     for(i = index_of_revision(r); i >= rtag; i--)
1924     {
1925     int s, q;
1926     if(i > 0)
1927     s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
1928     else
1929     s = b->revs[i]->y - (b->y + b->h);
1930     q = conf.rev_maxline - s;
1931     if(q > 0)
1932     {
1933     int d = rest ? rest/nlinks+1 : 0;
1934     if(q >= dy+d)
1935     {
1936     move_trunk(b->revs[i], dy+d);
1937     }
1938     else
1939     {
1940     move_trunk(b->revs[i], q);
1941     rest += dy+d - q;
1942     }
1943     rest -= d;
1944     nlinks--;
1945     }
1946     }
1947     if(b == ancestor)
1948     break;
1949     r = b->branchpoint;
1950     assert(r != NULL); /* else 'space_available' wouldn't have returned positively */
1951     b = r->branch;
1952 bertho 1.18 }
1953 bertho 1.19 return 1;
1954 bertho 1.18 }
1955    
1956 bertho 1.19 static branch_t *find_collision_branch(rcsfile_t *rcs, branch_t *b)
1957 bertho 1.18 {
1958     int i;
1959 bertho 1.19 int dist = INT_MAX;
1960     branch_t *col = NULL;
1961    
1962 bertho 1.18 for(i = 0; i < rcs->nbranches; i++)
1963     {
1964 bertho 1.19 int t = branch_distance(rcs->branches[i], b);
1965     if(t > 0 && t < dist)
1966 bertho 1.18 {
1967 bertho 1.19 dist = t;
1968     col = rcs->branches[i];
1969 bertho 1.18 }
1970     }
1971 bertho 1.19 return col;
1972 bertho 1.18 }
1973    
1974 bertho 1.19 void auto_stretch(rcsfile_t *rcs)
1975 bertho 1.18 {
1976 bertho 1.19 int i;
1977     int safeguard;
1978 bertho 1.18
1979 bertho 1.19 for(i = 0, safeguard = LOOPSAFEGUARD; i < rcs->nbranches && safeguard; i++)
1980 bertho 1.18 {
1981 bertho 1.19 int bl, pr;
1982     branch_t *b = rcs->branches[i];
1983     if(!b->branchpoint)
1984     continue;
1985     branch_bbox(b, &bl, NULL, NULL, NULL);
1986     branch_bbox(b->branchpoint->branch, NULL, &pr, NULL, NULL);
1987     if(bl - conf.branch_margin - pr > 0)
1988 bertho 1.18 {
1989 bertho 1.19 branch_t *col;
1990 bertho 1.18 int spaceneeded;
1991 bertho 1.19 /* There is a potential to move branch b further left.
1992     * All branches obstructing this one from moving further
1993     * left must be originating from revisions below
1994     * b->branchpoint until a common ancester.
1995     * So, we search all branches for a branch that lies left
1996     * of b and is closest to b. This is then the collission
1997     * branch that needs to be moved.
1998     */
1999     col = find_collision_branch(rcs, b);
2000     if(!col)
2001 bertho 1.18 continue;
2002 bertho 1.19 spaceneeded = space_needed(col, b);
2003     if(spaceneeded < 0)
2004     continue;
2005     #ifdef DEBUG
2006     fprintf(stderr, "auto_stretch: %s collides %s need %d\n", b->branch->branch, col->branch->branch, spaceneeded);
2007     #endif
2008     /* Trace the collision branch back to find the common ancester
2009     * of both col and b. All revisions encountered while traversing
2010     * backwards must be stretched, including all revisions on the
2011     * common ancester from where the branches sprout.
2012     */
2013     if(stretch_branches(rcs, col, b, spaceneeded))
2014 bertho 1.18 {
2015 bertho 1.19 if(kern_tree(rcs))
2016     {
2017     /* Restart the process because movement can
2018     * cause more movement.
2019     */
2020     i = 0 - 1; /* -1 for the i++ of the loop */
2021     safeguard--; /* Prevent infinite loop, just in case */
2022     }
2023 bertho 1.23 /*return;*/
2024 bertho 1.18 }
2025     }
2026     }
2027 bertho 1.19 if(!quiet && !safeguard)
2028     fprintf(stderr, "auto_stretch: safeguard terminated possible infinite loop; please report.\n");
2029 bertho 1.18 }
2030    
2031 bertho 1.7 void make_layout(rcsfile_t *rcs)
2032 bertho 1.1 {
2033     int i, j;
2034     int x, y;
2035     int w, h;
2036     int w2;
2037    
2038 bertho 1.16 /* Remove all unwanted revisions */
2039     if(conf.strip_untagged)
2040     {
2041 bertho 1.21 int fr = conf.strip_first_rev ? 0 : 1;
2042 bertho 1.16 for(i = 0; i < rcs->nbranches; i++)
2043     {
2044     branch_t *bp = rcs->branches[i];
2045 bertho 1.18 for(j = fr; j < bp->nrevs-1; j++)
2046 bertho 1.16 {
2047     if(!bp->revs[j]->ntags && !bp->revs[j]->nbranches)
2048     {
2049     memmove(&bp->revs[j], &bp->revs[j+1], (bp->nrevs-j-1) * sizeof(bp->revs[0]));
2050     bp->nrevs--;
2051 bertho 1.17 bp->revs[j]->stripped = 1;
2052 bertho 1.16 j--;
2053     }
2054     }
2055     }
2056     }
2057    
2058 bertho 1.1 /* Calculate the box-sizes of the revisions */
2059 bertho 1.7 for(i = 0; i < rcs->nsrev; i++)
2060 bertho 1.1 {
2061     revision_t *rp;
2062     int w;
2063     int h;
2064 bertho 1.7 rp = rcs->srev[i];
2065 bertho 1.9 rp->revtext = expand_string(conf.rev_text, rcs, rp, rp->rev, NULL, rp->ntags ? rp->tags[0] : NULL);
2066     w = get_swidth(rp->revtext, &conf.rev_text_font);
2067     j = get_swidth(rp->rev->rev, &conf.rev_font);
2068     if(j > w)
2069     w = j;
2070     h = get_sheight(rp->revtext, &conf.rev_text_font) + get_sheight(rp->rev->rev, &conf.rev_font);
2071 bertho 1.1 for(j = 0; j < rp->ntags; j++)
2072     {
2073     int ww = get_swidth(rp->tags[j]->tag, &conf.tag_font);
2074     if(ww > w) w = ww;
2075     h += get_sheight(rp->tags[j]->tag, &conf.tag_font) + conf.rev_separator;
2076     }
2077     rp->w = w + conf.rev_lspace + conf.rev_rspace;
2078     rp->h = h + conf.rev_tspace + conf.rev_bspace;
2079     }
2080    
2081     /* Calculate the box-sizes of the branches */
2082     for(i = 0; i < rcs->nbranches; i++)
2083     {
2084     branch_t *bp = rcs->branches[i];
2085     int w;
2086     int h;
2087 bertho 1.7 w = get_swidth(bp->branch->branch, &conf.branch_font);
2088     h = get_sheight(bp->branch->branch, &conf.branch_font);
2089     for(j = 0; j < bp->ntags; j++)
2090 bertho 1.1 {
2091 bertho 1.19 int ww = get_swidth(bp->tags[j]->tag, &conf.branch_tag_font);
2092 bertho 1.1 if(ww > w) w = ww;
2093 bertho 1.19 h += get_sheight(bp->tags[j]->tag, &conf.branch_tag_font);
2094 bertho 1.1 }
2095     w += conf.branch_lspace + conf.branch_rspace;
2096     h += conf.branch_tspace + conf.branch_bspace;
2097     bp->w = w;
2098     bp->h = h;
2099 bertho 1.25 if(conf.left_right)
2100     {
2101     for(j = 0; j < bp->nrevs; j++)
2102     {
2103     if(bp->revs[j]->h > h)
2104     h = bp->revs[j]->h;
2105     w += bp->revs[j]->w + conf.rev_minline;
2106     }
2107     if(conf.branch_dupbox)
2108     w += bp->w + conf.rev_minline;
2109     }
2110     else
2111 bertho 1.1 {
2112 bertho 1.25 for(j = 0; j < bp->nrevs; j++)
2113     {
2114     if(bp->revs[j]->w > w)
2115     w = bp->revs[j]->w;
2116     h += bp->revs[j]->h + conf.rev_minline;
2117     }
2118     if(conf.branch_dupbox)
2119     h += bp->h + conf.rev_minline;
2120 bertho 1.1 }
2121     bp->th = h;
2122     bp->tw = w;
2123     }
2124    
2125     /* Calculate the relative positions of revs in a branch */
2126 bertho 1.25 if(conf.left_right)
2127     {
2128     for(i = 0; i < rcs->nbranches; i++)
2129     {
2130     branch_t *b = rcs->branches[i];
2131     y = b->th/2;
2132     x = b->w;
2133     b->y = y;
2134     b->cx = 0;
2135     for(j = 0; j < b->nrevs; j++)
2136     {
2137     x += conf.rev_minline;
2138     b->revs[j]->y = y;
2139     b->revs[j]->cx = x;
2140     x += b->revs[j]->w;
2141     }
2142     }
2143     }
2144     else
2145 bertho 1.1 {
2146 bertho 1.25 for(i = 0; i < rcs->nbranches; i++)
2147     {
2148     branch_t *b = rcs->branches[i];
2149     x = b->tw/2;
2150     y = b->h;
2151     b->cx = x;
2152     b->y = 0;
2153     for(j = 0; j < b->nrevs; j++)
2154     {
2155     y += conf.rev_minline;
2156     b->revs[j]->cx = x;
2157     b->revs[j]->y = y;
2158     y += b->revs[j]->h;
2159     }
2160 bertho 1.1 }
2161     }
2162    
2163 bertho 1.18 /* Initially reposition the branches from bottom to top progressively right */
2164 bertho 1.25 if(conf.left_right)
2165     {
2166     x = rcs->branches[0]->y;
2167     w2 = rcs->branches[0]->th / 2;
2168     for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
2169     {
2170     initial_reposition_branch_lr(rcs->branches[0]->revs[i], &x, &w2);
2171     }
2172     }
2173     else
2174 bertho 1.1 {
2175 bertho 1.25 x = rcs->branches[0]->cx;
2176     w2 = rcs->branches[0]->tw / 2;
2177     for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
2178     {
2179     initial_reposition_branch(rcs->branches[0]->revs[i], &x, &w2);
2180     }
2181 bertho 1.1 }
2182    
2183 bertho 1.19 /* Initially move branches left if there is room */
2184     kern_tree(rcs);
2185    
2186 bertho 1.18 /* Try to kern the branches more by expanding the inter-revision spacing */
2187 bertho 1.25 if(conf.auto_stretch && !conf.left_right)
2188 bertho 1.19 auto_stretch(rcs);
2189 bertho 1.7
2190     /* Move everything w.r.t. the top-left margin */
2191 bertho 1.1 for(i = 0; i < rcs->nbranches; i++)
2192     move_branch(rcs->branches[i], conf.margin_left, conf.margin_top);
2193    
2194     /* Calculate overall image size */
2195 bertho 1.25 if(conf.left_right)
2196     {
2197     x = rcs->branches[0]->cx;
2198     y = rcs->branches[0]->y - rcs->branches[0]->th/2;
2199     }
2200     else
2201     {
2202     x = rcs->branches[0]->cx - rcs->branches[0]->tw/2;
2203     y = rcs->branches[0]->y;
2204     }
2205 bertho 1.1 w = rcs->branches[0]->tw;
2206     h = rcs->branches[0]->th;
2207     for(i = 1; i < rcs->nbranches; i++)
2208     rect_union(&x, &y, &w, &h, rcs->branches[i]);
2209     rcs->tw = w;
2210     rcs->th = h;
2211 bertho 1.16
2212     /* Flip the entire tree */
2213     if(conf.upside_down)
2214     {
2215 bertho 1.25 if(conf.left_right)
2216     {
2217     x += rcs->tw;
2218     for(i = 0; i < rcs->nbranches; i++)
2219     {
2220     branch_t *b = rcs->branches[i];
2221     for(j = 0; j < b->nrevs; j++)
2222     {
2223     revision_t *r = b->revs[j];
2224 bertho 1.26 r->cx = x - r->cx - r->w + conf.margin_left;
2225 bertho 1.25 }
2226 bertho 1.26 b->cx = x - b->cx - b->w + conf.margin_left;
2227 bertho 1.25 }
2228     }
2229     else
2230 bertho 1.16 {
2231 bertho 1.25 y += rcs->th;
2232     for(i = 0; i < rcs->nbranches; i++)
2233 bertho 1.16 {
2234 bertho 1.25 branch_t *b = rcs->branches[i];
2235     for(j = 0; j < b->nrevs; j++)
2236     {
2237     revision_t *r = b->revs[j];
2238     r->y = y - r->y - r->h + conf.margin_top;
2239     }
2240     b->y = y - b->y - b->h + conf.margin_top;
2241 bertho 1.16 }
2242     }
2243     }
2244 bertho 1.1 }
2245    
2246     /*
2247     **************************************************************************
2248 bertho 1.6 * Imagemap functions
2249     **************************************************************************
2250     */
2251 bertho 1.26 void make_imagemap(rcsfile_t *rcs, FILE *fp, gdImagePtr im)
2252 bertho 1.6 {
2253     int i, j;
2254     fprintf(fp, "<map name=\"%s\">\n", conf.map_name);
2255     for(i = 0; i < rcs->nbranches; i++)
2256     {
2257     branch_t *b = rcs->branches[i];
2258 bertho 1.8 tag_t *tag = b->ntags ? b->tags[0] : NULL;
2259 bertho 1.16 char *bhref = expand_string(conf.map_branch_href, rcs, NULL, b->branch, NULL, tag);
2260     char *balt = expand_string(conf.map_branch_alt, rcs, NULL, b->branch, NULL, tag);
2261 bertho 1.26 int x1;
2262     int x2;
2263     int y1;
2264     int y2;
2265    
2266     if(conf.left_right)
2267     {
2268     x1 = b->cx;
2269     y1 = b->y - b->h/2;
2270     x2 = b->cx + b->w;
2271     y2 = b->y + b->h/2;
2272     }
2273     else
2274     {
2275     x1 = b->cx - b->w/2;
2276     y1 = b->y;
2277     x2 = b->cx + b->w/2;
2278     y2 = b->y + b->h;
2279     }
2280 bertho 1.8 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2281 bertho 1.26 bhref, x1, y1, x2, y2, balt);
2282     if(im)
2283     {
2284     gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
2285     gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
2286     gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
2287     }
2288 bertho 1.6 for(j = 0; j < b->nrevs; j++)
2289     {
2290     revision_t *r = b->revs[j];
2291 bertho 1.13 revision_t* r1;
2292 bertho 1.26 int xoff = 1;
2293     int yoff = 1;
2294 bertho 1.16 char *href;
2295     char *alt;
2296 bertho 1.13
2297 bertho 1.8 tag = r->ntags ? r->tags[0] : NULL;
2298 bertho 1.9 href = expand_string(conf.map_rev_href, rcs, r, r->rev, NULL, tag);
2299     alt = expand_string(conf.map_rev_alt, rcs, r, r->rev, NULL, tag);
2300 bertho 1.26 if(conf.left_right)
2301     {
2302     x1 = r->cx;
2303     y1 = r->y - r->h/2;
2304     x2 = r->cx + r->w;
2305     y2 = r->y + r->h/2;
2306     }
2307     else
2308     {
2309     x1 = r->cx - r->w/2;
2310     y1 = r->y;
2311     x2 = r->cx + r->w/2;
2312     y2 = r->y + r->h;
2313     }
2314 bertho 1.8 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2315 bertho 1.26 href, x1, y1, x2, y2, alt);
2316     if(im)
2317     {
2318     gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
2319     gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
2320     gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
2321     }
2322 bertho 1.8 xfree(href);
2323     xfree(alt);
2324 bertho 1.16 if(j > 0 || b->branchpoint)
2325 bertho 1.9 {
2326 bertho 1.16 if(j > 0)
2327     {
2328     r1 = b->revs[j-1];
2329 bertho 1.26 if(conf.left_right)
2330     {
2331     yoff = MIN(r->h, r1->h)/4;
2332     x1 = conf.upside_down ? r1->cx : r1->cx + r1->w;
2333     }
2334     else
2335     {
2336     xoff = MIN(r->w, r1->w)/4;
2337     y1 = conf.upside_down ? r1->y : r1->y + r1->h;
2338     }
2339 bertho 1.16 }
2340     else
2341     {
2342     r1 = b->branchpoint;
2343 bertho 1.26 if(conf.left_right)
2344     {
2345     yoff = MIN(r->h, b->h)/4;
2346     x1 = conf.upside_down ? b->cx : b->cx + b->w;
2347     }
2348     else
2349     {
2350     xoff = MIN(r->w, b->w)/4;
2351     y1 = conf.upside_down ? b->y : b->y + b->h;
2352     }
2353     }
2354     if(conf.left_right)
2355     {
2356     y1 = r->y - yoff;
2357     y2 = r->y + yoff;
2358     x2 = conf.upside_down ? r->cx + r->w : r->cx;
2359     yoff = 0;
2360     }
2361     else
2362     {
2363     x1 = r->cx - xoff;
2364     x2 = r->cx + xoff;
2365     y2 = conf.upside_down ? r->y + r->h : r->y;
2366     xoff = 0;
2367     }
2368     if(x1 > x2)
2369     {
2370     int tt = x1;
2371     x1 = x2;
2372     x2 = tt;
2373     }
2374     if(y1 > y2)
2375     {
2376     int tt = y1;
2377     y1 = y2;
2378     y2 = tt;
2379 bertho 1.16 }
2380     href = expand_string(conf.map_diff_href, rcs, r, r->rev, r1->rev, tag);
2381     alt = expand_string(conf.map_diff_alt, rcs, r, r->rev, r1->rev, tag);
2382     fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2383     href,
2384 bertho 1.26 x1+xoff, y1+yoff, x2-xoff, y2-yoff,
2385 bertho 1.16 alt);
2386 bertho 1.26 if(im)
2387     {
2388     gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
2389     gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
2390     gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
2391     }
2392 bertho 1.16 xfree(href);
2393     xfree(alt);
2394 bertho 1.9 }
2395 bertho 1.6 }
2396 bertho 1.17 if(conf.branch_dupbox)
2397 bertho 1.16 {
2398 bertho 1.26 if(conf.left_right)
2399     {
2400     x1 = conf.upside_down ? b->cx + b->w - b->tw : b->cx - b->w + b->tw;
2401     y1 = b->y - b->h/2;
2402     x2 = x1 + b->w;
2403     y2 = b->y + b->h/2;
2404     }
2405 bertho 1.16 else
2406 bertho 1.26 {
2407     x1 = b->cx - b->w/2;
2408     y1 = conf.upside_down ? b->y + b->h - b->th : b->y - b->h + b->th;
2409     x2 = b->cx + b->w/2;
2410     y2 = y1 + b->h;
2411     }
2412 bertho 1.16 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2413 bertho 1.26 bhref, x1, y1, x2, y2, balt);
2414     if(im)
2415     {
2416     gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
2417     gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
2418     gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
2419     }
2420 bertho 1.16 }
2421     xfree(bhref);
2422     xfree(balt);
2423 bertho 1.6 }
2424     fprintf(fp, "</map>\n");
2425     }
2426    
2427     /*
2428     **************************************************************************
2429 bertho 1.1 * Program entry
2430     **************************************************************************
2431     */
2432     static const char usage_str[] =
2433     "Usage: cvsgraph [options] <file>\n"
2434 bertho 1.16 " -b Add a branch box at both sides of the trunk (config value is negated)\n"
2435 bertho 1.8 " -c <file> Read alternative config from <file>\n"
2436     " -d <level> Enable debug mode at <level>\n"
2437     " -h This message\n"
2438     " -i Generate an imagemap instead of image\n"
2439 bertho 1.18 " -I <file> Also write the imagemap to <file>\n"
2440 bertho 1.19 " -k Auto stretch the tree (config value is negated)\n"
2441 bertho 1.8 " -M <name> Use <name> as imagemap name\n"
2442     " -m <mod> Use <mod> as cvs module\n"
2443     " -o <file> Output to <file>\n"
2444 bertho 1.18 " -O <opt=val> Set option opt to value val\n"
2445 bertho 1.8 " -q Be quiet (i.e. no warnings)\n"
2446     " -r <path> Use <path> as cvsroot path\n"
2447 bertho 1.16 " -s Strip untagged revisions (config value is negated)\n"
2448 bertho 1.18 " -S Also strip the first revision (config value is negated)\n"
2449 bertho 1.16 " -u Upside down image (mirror vertically; config value is negated)\n"
2450 bertho 1.8 " -V Print version and exit\n"
2451     " -[0-9] <txt> Use <txt> for expansion\n"
2452 bertho 1.1 ;
2453    
2454 bertho 1.22 #define VERSION_STR "1.2.1"
2455 bertho 1.20 #define NOTICE_STR "Copyright (c) 2001,2002 B.Stultiens"
2456 bertho 1.1
2457 bertho 1.19 static void append_slash(char **path)
2458 bertho 1.9 {
2459     int l;
2460     assert(path != NULL);
2461     assert(*path != NULL);
2462     l = strlen(*path);
2463     if(!l || (*path)[l-1] == '/')
2464     return;
2465     *path = xrealloc(*path, l+2);
2466     strcat(*path, "/");
2467     }
2468    
2469 bertho 1.1 int main(int argc, char *argv[])
2470     {
2471 bertho 1.7 extern int rcs_flex_debug;
2472     extern int rcsdebug;
2473 bertho 1.1 int optc;
2474     char *confpath = NULL;
2475     char *outfile = NULL;
2476     char *cvsroot = NULL;
2477     char *cvsmodule = NULL;
2478 bertho 1.7 int imagemap = 0;
2479 bertho 1.16 int upsidedown = 0;
2480 bertho 1.17 int bdupbox = 0;
2481 bertho 1.16 int stripuntag = 0;
2482 bertho 1.18 int stripfirst = 0;
2483 bertho 1.19 int autostretch = 0;
2484 bertho 1.7 char *imgmapname = NULL;
2485 bertho 1.18 char *imgmapfile = NULL;
2486 bertho 1.1 int lose = 0;
2487     FILE *fp;
2488 bertho 1.18 char *rcsfilename;
2489 bertho 1.7 rcsfile_t *rcs;
2490 bertho 1.1 gdImagePtr im;
2491    
2492 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)
2493 bertho 1.1 {
2494     switch(optc)
2495     {
2496 bertho 1.16 case 'b':
2497 bertho 1.17 bdupbox = 1;
2498 bertho 1.16 break;
2499 bertho 1.1 case 'c':
2500     confpath = xstrdup(optarg);
2501     break;
2502 bertho 1.7 case 'd':
2503     debuglevel = strtol(optarg, NULL, 0);
2504     break;
2505 bertho 1.18 case 'I':
2506     imgmapfile = xstrdup(optarg);
2507     break;
2508 bertho 1.6 case 'i':
2509 bertho 1.7 imagemap = 1;
2510     break;
2511 bertho 1.18 case 'k':
2512 bertho 1.19 autostretch = 1;
2513 bertho 1.18 break;
2514 bertho 1.7 case 'M':
2515     imgmapname = xstrdup(optarg);
2516 bertho 1.6 break;
2517 bertho 1.1 case 'm':
2518     cvsmodule = xstrdup(optarg);
2519     break;
2520 bertho 1.18 case 'O':
2521     stack_option(optarg);
2522     break;
2523 bertho 1.1 case 'o':
2524     outfile = xstrdup(optarg);
2525     break;
2526 bertho 1.7 case 'q':
2527     quiet = 1;
2528     break;
2529 bertho 1.1 case 'r':
2530     cvsroot = xstrdup(optarg);
2531     break;
2532 bertho 1.18 case 'S':
2533     stripfirst = 1;
2534     break;
2535 bertho 1.16 case 's':
2536     stripuntag = 1;
2537     break;
2538     case 'u':
2539     upsidedown = 1;
2540     break;
2541 bertho 1.1 case 'V':
2542     fprintf(stdout, "cvsgraph v%s, %s\n", VERSION_STR, NOTICE_STR);
2543     return 0;
2544     case 'h':
2545     fprintf(stdout, "%s", usage_str);
2546     return 0;
2547     default:
2548 bertho 1.8 if(isdigit(optc))
2549     {
2550     conf.expand[optc-'0'] = xstrdup(optarg);
2551     }
2552     else
2553     lose++;
2554 bertho 1.1 }
2555     }
2556    
2557     if(lose)
2558     {
2559     fprintf(stderr, "%s", usage_str);
2560     return 1;
2561     }
2562    
2563 bertho 1.7 if(debuglevel)
2564     {
2565     setvbuf(stdout, NULL, 0, _IONBF);
2566     setvbuf(stderr, NULL, 0, _IONBF);
2567     }
2568     rcs_flex_debug = (debuglevel & DEBUG_RCS_LEX) != 0;
2569     rcsdebug = (debuglevel & DEBUG_RCS_YACC) != 0;
2570    
2571 bertho 1.1 /* Set defaults */
2572 bertho 1.21 conf.tag_font.gdfont = gdFontTiny;
2573     conf.rev_font.gdfont = gdFontTiny;
2574     conf.branch_font.gdfont = gdFontTiny;
2575     conf.branch_tag_font.gdfont = gdFontTiny;
2576     conf.title_font.gdfont = gdFontTiny;
2577     conf.rev_text_font.gdfont = gdFontTiny;
2578    
2579 bertho 1.19 conf.anti_alias = 1;
2580 bertho 1.20 conf.thick_lines = 1;
2581 bertho 1.9
2582     conf.cvsroot = xstrdup("");
2583     conf.cvsmodule = xstrdup("");
2584     conf.date_format = xstrdup("%d-%b-%Y %H:%M:%S");
2585     conf.title = xstrdup("");
2586     conf.map_name = xstrdup("CvsGraphImageMap");
2587     conf.map_branch_href = xstrdup("href=\"unset: conf.map_branch_href\"");
2588     conf.map_branch_alt = xstrdup("alt=\"%B\"");
2589     conf.map_rev_href = xstrdup("href=\"unset: conf.map_rev_href\"");
2590     conf.map_rev_alt = xstrdup("alt=\"%R\"");
2591     conf.map_diff_href = xstrdup("href=\"unset: conf.map_diff_href\"");
2592 bertho 1.12 conf.map_diff_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
2593 bertho 1.10 conf.rev_text = xstrdup("%d");
2594 bertho 1.9
2595     conf.color_bg = white_color;
2596     conf.branch_bgcolor = white_color;
2597     conf.branch_color = black_color;
2598 bertho 1.19 conf.branch_tag_color = black_color;
2599 bertho 1.9 conf.rev_color = black_color;
2600     conf.rev_bgcolor = white_color;
2601     conf.tag_color = black_color;
2602     conf.title_color = black_color;
2603     conf.rev_text_color = black_color;
2604 bertho 1.10
2605     conf.image_quality = 100;
2606 bertho 1.18 conf.rev_maxline = -1; /* Checked later to set to default */
2607    
2608     read_config(confpath);
2609 bertho 1.1
2610 bertho 1.18 if(conf.rev_maxline == -1) conf.rev_maxline = 5 * conf.rev_minline;
2611 bertho 1.1
2612     /* Set overrides */
2613 bertho 1.7 if(cvsroot) conf.cvsroot = cvsroot;
2614     if(cvsmodule) conf.cvsmodule = cvsmodule;
2615     if(imgmapname) conf.map_name = imgmapname;
2616 bertho 1.16 if(upsidedown) conf.upside_down = !conf.upside_down;
2617 bertho 1.17 if(bdupbox) conf.branch_dupbox = !conf.branch_dupbox;
2618 bertho 1.16 if(stripuntag) conf.strip_untagged = !conf.strip_untagged;
2619 bertho 1.18 if(stripfirst) conf.strip_first_rev = !conf.strip_first_rev;
2620 bertho 1.19 if(autostretch) conf.auto_stretch = !conf.auto_stretch;
2621 bertho 1.18
2622     if(conf.rev_minline >= conf.rev_maxline)
2623     {
2624 bertho 1.19 if(conf.auto_stretch && !quiet)
2625     fprintf(stderr, "Auto stretch is only possible if rev_minline < rev_maxline\n");
2626     conf.auto_stretch = 0;
2627 bertho 1.18 }
2628 bertho 1.20
2629     if(conf.thick_lines < 1)
2630     conf.thick_lines = 1;
2631     if(conf.thick_lines > 11)
2632     conf.thick_lines = 11;
2633 bertho 1.1
2634 bertho 1.9 append_slash(&conf.cvsroot);
2635     append_slash(&conf.cvsmodule);
2636    
2637 bertho 1.18 if(optind >= argc)
2638     {
2639     #ifdef __WIN32__
2640     /* Bad hack for DOS/Windows */
2641     if(setmode(fileno(stdin), O_BINARY) == -1)
2642     {
2643     perror("Set binary mode for stdin");
2644     return 1;
2645     }
2646     #endif
2647     rcsfilename = NULL;
2648     }
2649     else
2650     rcsfilename = argv[optind];
2651    
2652     rcs = get_rcsfile(conf.cvsroot, conf.cvsmodule, rcsfilename);
2653 bertho 1.1 if(!rcs)
2654     return 1;
2655    
2656 bertho 1.7 if(debuglevel & DEBUG_RCS_FILE)
2657     dump_rcsfile(rcs);
2658 bertho 1.1
2659 bertho 1.7 if(!reorganise_branches(rcs))
2660     return 1;
2661 bertho 1.1
2662 bertho 1.7 if(!assign_tags(rcs))
2663     return 1;
2664 bertho 1.1
2665     if(outfile)
2666     {
2667 bertho 1.15 if((fp = fopen(outfile, "wb")) == NULL)
2668 bertho 1.1 {
2669     perror(outfile);
2670     return 1;
2671     }
2672     }
2673     else
2674 bertho 1.15 {
2675 bertho 1.1 fp = stdout;
2676 bertho 1.15 #ifdef __WIN32__
2677     /* Bad hack for DOS/Windows */
2678     if(setmode(fileno(fp), O_BINARY) == -1)
2679     {
2680     perror("Set binary mode for stdout");
2681     return 1;
2682     }
2683     #endif
2684     }
2685 bertho 1.5
2686 bertho 1.7 make_layout(rcs);
2687    
2688     if(!imagemap)
2689 bertho 1.5 {
2690 bertho 1.7 /* Create an image */
2691     im = make_image(rcs);
2692 bertho 1.26 #ifdef DEBUG_IMAGEMAP
2693     {
2694     FILE *nulfile = fopen("/dev/null", "w");
2695     make_imagemap(rcs, nulfile, im);
2696     fclose(nulfile);
2697     }
2698     #endif
2699 bertho 1.7 switch(conf.image_type)
2700     {
2701 bertho 1.5 #ifdef HAVE_IMAGE_GIF
2702 bertho 1.7 # ifndef HAVE_IMAGE_PNG
2703     default:
2704     # endif
2705     case IMAGE_GIF:
2706     gdImageGif(im, fp);
2707     break;
2708 bertho 1.5 #endif
2709     #ifdef HAVE_IMAGE_PNG
2710 bertho 1.7 default:
2711     case IMAGE_PNG:
2712     gdImagePng(im, fp);
2713     break;
2714 bertho 1.5 #endif
2715     #ifdef HAVE_IMAGE_JPEG
2716     # if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG)
2717 bertho 1.7 default:
2718 bertho 1.5 # endif
2719 bertho 1.7 case IMAGE_JPEG:
2720     gdImageJpeg(im, fp, conf.image_quality);
2721     break;
2722 bertho 1.5 #endif
2723 bertho 1.7 }
2724    
2725     gdImageDestroy(im);
2726     }
2727     else
2728     {
2729     /* Create an imagemap */
2730 bertho 1.26 make_imagemap(rcs, fp, NULL);
2731 bertho 1.18 }
2732    
2733     /* Also create imagemap to file if requested */
2734     if(imgmapfile)
2735     {
2736     FILE *ifp = fopen(imgmapfile, "wb");
2737     if(!ifp)
2738     {
2739     perror(imgmapfile);
2740     return 1;
2741     }
2742 bertho 1.26 make_imagemap(rcs, ifp, NULL);
2743 bertho 1.18 fclose(ifp);
2744 bertho 1.5 }
2745    
2746 bertho 1.1 if(outfile)
2747     fclose(fp);
2748 bertho 1.6
2749 bertho 1.1 return 0;
2750     }
2751    

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0