/[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.12 - (hide annotations)
Thu Mar 22 21:42:48 2001 UTC (16 years, 7 months ago) by bertho
Branch: MAIN
CVS Tags: REL_1_1_2
Changes since 1.11: +30 -8 lines
File MIME type: text/plain
- Dead tags (i.e. a tag without a valid revision or branch) does no
  longer print a message in quiet mode.
- Added expansions for cvsroot and cvsmodule without trailing '/'
- Remove core-files in a 'make clean'.
- Fixed default config file to use entities < and >.
1 bertho 1.1 /*
2     * CvsGraph graphical representation generator of brances and revisions
3     * of a file in cvs/rcs.
4     *
5     * Copyright (C) 2001 B. Stultiens
6     *
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     #include <stdio.h>
23     #include <stdlib.h>
24     #include <unistd.h>
25     #include <string.h>
26     #include <assert.h>
27     #include <sys/types.h>
28     #include <sys/stat.h>
29     #include <sys/wait.h>
30     #include <fcntl.h>
31     #include <regex.h>
32     #include <errno.h>
33     #include <getopt.h>
34 bertho 1.8 #include <ctype.h>
35 bertho 1.9 #include <time.h>
36    
37 bertho 1.1 #include <gd.h>
38     #include <gdfontt.h>
39    
40 bertho 1.5 #include "config.h"
41 bertho 1.1 #include "cvsgraph.h"
42     #include "utils.h"
43     #include "readconf.h"
44 bertho 1.7 #include "rcs.h"
45 bertho 1.1
46 bertho 1.5 #if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG) && !defined(HAVE_IMAGE_JPEG)
47     # error No image output format available. Check libgd
48     #endif
49    
50    
51 bertho 1.1 /*#define DEBUG 1*/
52    
53     #define CONFFILENAME "cvsgraph.conf"
54    
55     #ifndef ETCDIR
56     # define ETCDIR "/usr/local/etc"
57     #endif
58    
59     #ifndef MAX
60     # define MAX(a,b) ((a) > (b) ? (a) : (b))
61     #endif
62    
63     #ifndef MIN
64     # define MIN(a,b) ((a) < (b) ? (a) : (b))
65     #endif
66    
67     #define ALIGN_HL 0x00
68     #define ALIGN_HC 0x01
69     #define ALIGN_HR 0x02
70 bertho 1.9 #define ALIGN_HX 0x0f
71 bertho 1.1 #define ALIGN_VT 0x00
72     #define ALIGN_VC 0x10
73     #define ALIGN_VB 0x20
74     #define ALIGN_VX 0xf0
75 bertho 1.9
76 bertho 1.1 /*
77     **************************************************************************
78     * Globals
79     **************************************************************************
80     */
81    
82     config_t conf;
83 bertho 1.7 int debuglevel;
84 bertho 1.9 color_t white_color = {255, 255, 255, 0};
85     color_t black_color = {0, 0, 0, 0};
86    
87 bertho 1.1
88     /*
89     **************************************************************************
90 bertho 1.7 * Dubug routines
91 bertho 1.1 **************************************************************************
92     */
93 bertho 1.7 void dump_rev(char *p, rev_t *r)
94 bertho 1.1 {
95 bertho 1.7 printf("%s", p);
96     if(r)
97     printf("'%s', '%s', %d\n", r->rev, r->branch, r->isbranch);
98     else
99     printf("<null>\n");
100 bertho 1.1 }
101    
102 bertho 1.7 void dump_id(char *p, char *d)
103 bertho 1.1 {
104 bertho 1.7 printf("%s", p);
105     if(d)
106     printf("'%s'\n", d);
107     else
108     printf("<null>\n");
109 bertho 1.1 }
110    
111 bertho 1.7 void dump_idrev(char *p, idrev_t *t)
112 bertho 1.1 {
113 bertho 1.7 printf("%s", p);
114     if(t)
115 bertho 1.1 {
116 bertho 1.7 printf("'%s' -> ", t->id);
117     dump_rev("", t->rev);
118 bertho 1.1 }
119     else
120 bertho 1.7 printf("<null>\n");
121 bertho 1.1 }
122    
123 bertho 1.7 void dump_tag(char *p, tag_t *t)
124 bertho 1.1 {
125 bertho 1.7 printf("%s", p);
126     if(t)
127 bertho 1.1 {
128 bertho 1.7 printf("'%s' -> ", t->tag);
129     dump_rev("", t->rev);
130 bertho 1.1 }
131 bertho 1.7 else
132     printf("<null>\n");
133 bertho 1.1 }
134    
135 bertho 1.7 void dump_delta(char *p, delta_t *d)
136 bertho 1.1 {
137 bertho 1.7 int i;
138     printf("%sdelta.rev : ", p);
139     dump_rev("", d->rev);
140     printf("%sdelta.date : %s\n", p, d->date);
141     printf("%sdelta.author: %s\n", p, d->author);
142     printf("%sdelta.state : %s\n", p, d->state);
143     for(i = 0; d->branches && i < d->branches->nrevs; i++)
144 bertho 1.1 {
145 bertho 1.7 printf("%sdelta.branch: ", p);
146     dump_rev("", d->branches->revs[i]);
147 bertho 1.1 }
148 bertho 1.7 printf("%sdelta.next : ", p);
149     dump_rev("", d->next);
150     printf("\n");
151 bertho 1.1 }
152    
153 bertho 1.7 void dump_dtext(char *p, dtext_t *d)
154 bertho 1.1 {
155 bertho 1.7 printf("%sdtext.rev : ", p);
156     dump_rev("", d->rev);
157     printf("%sdtext.log : %d bytes\n", p, d->log ? strlen(d->log) : -1);
158     printf("%sdtext.text : %d bytes\n", p, d->text ? strlen(d->text) : -1);
159     printf("\n");
160 bertho 1.1 }
161    
162 bertho 1.7 void dump_rcsfile(rcsfile_t *rcs)
163 bertho 1.1 {
164 bertho 1.7 int i;
165     printf("root : '%s'\n", rcs->root);
166     printf("module : '%s'\n", rcs->module);
167     printf("file : '%s'\n", rcs->file);
168     dump_rev("head : ", rcs->head);
169     dump_rev("branch : ", rcs->branch);
170     printf("access :\n");
171     for(i = 0; rcs->access && i < rcs->access->nids; i++)
172     dump_id("\t", rcs->access->ids[i]);
173     printf("tags :\n");
174     for(i = 0; rcs->tags && i < rcs->tags->ntags; i++)
175     dump_tag("\t", rcs->tags->tags[i]);
176     printf("locks :%s\n", rcs->strict ? " (strict)" : "");
177     for(i = 0; rcs->locks && i < rcs->locks->nidrevs; i++)
178     dump_idrev("\t", rcs->locks->idrevs[i]);
179     printf("comment: '%s'\n", rcs->comment);
180     printf("expand : '%s'\n", rcs->expand ? rcs->expand : "(default -kv)");
181     printf("deltas :\n");
182     for(i = 0; rcs->deltas && i < rcs->deltas->ndeltas; i++)
183     dump_delta("\t", rcs->deltas->deltas[i]);
184     printf("desc : '%s'\n", rcs->desc);
185     printf("dtexts :\n");
186     for(i = 0; rcs->dtexts && i < rcs->dtexts->ndtexts; i++)
187     dump_dtext("\t", rcs->dtexts->dtexts[i]);
188    
189     fflush(stdout);
190 bertho 1.1 }
191    
192 bertho 1.7 /*
193     **************************************************************************
194     * Read the rcs file
195     **************************************************************************
196     */
197     rcsfile_t *get_rcsfile(const char *cvsroot, const char *module, const char *file)
198 bertho 1.1 {
199 bertho 1.7 char *cmd = NULL;
200     int rv;
201 bertho 1.1
202 bertho 1.9 cmd = xmalloc(strlen(cvsroot) + strlen(module) + strlen(file) + 1);
203     sprintf(cmd, "%s%s%s", cvsroot, module, file);
204 bertho 1.7 if(!(rcsin = fopen(cmd, "r")))
205 bertho 1.9 {
206     perror(cmd);
207 bertho 1.1 return NULL;
208 bertho 1.9 }
209 bertho 1.7 input_file = cmd;
210     line_number = 1;
211     rv = rcsparse();
212     fclose(rcsin);
213     if(rv)
214 bertho 1.1 return NULL;
215 bertho 1.7 xfree(cmd);
216     input_file = NULL;
217     rcsfile->root = xstrdup(cvsroot);
218     rcsfile->module = xstrdup(module);
219     rcsfile->file = xstrdup(file);
220     return rcsfile;
221 bertho 1.1 }
222    
223     /*
224     **************************************************************************
225     * Sort and find helpers
226     **************************************************************************
227     */
228 bertho 1.5 int count_dots(const char *s)
229     {
230     int i;
231     for(i = 0; *s; s++)
232     {
233     if(*s == '.')
234     i++;
235     }
236     return i;
237     }
238    
239 bertho 1.7 int compare_rev(int bcmp, const rev_t *r1, const rev_t *r2)
240 bertho 1.5 {
241     int d1, d2;
242 bertho 1.7 char *c1, *c2;
243 bertho 1.5 char *v1, *v2;
244     char *s1, *s2;
245     int retval = 0;
246     assert(r1 != NULL);
247     assert(r2 != NULL);
248 bertho 1.7 if(bcmp)
249     {
250     assert(r1->branch != NULL);
251     assert(r2->branch != NULL);
252     c1 = r1->branch;
253     c2 = r2->branch;
254     }
255     else
256     {
257     assert(r1->rev != NULL);
258     assert(r2->rev != NULL);
259     c1 = r1->rev;
260     c2 = r2->rev;
261     }
262 bertho 1.5
263 bertho 1.7 d1 = count_dots(c1);
264     d2 = count_dots(c2);
265 bertho 1.5 if(d1 != d2)
266     {
267     return d1 - d2;
268     }
269    
270 bertho 1.7 s1 = v1 = xstrdup(c1);
271     s2 = v2 = xstrdup(c2);
272 bertho 1.5 while(1)
273     {
274     char *vc1 = strchr(s1, '.');
275     char *vc2 = strchr(s2, '.');
276     if(vc1 && vc2)
277     *vc1 = *vc2 = '\0';
278     if(*s1 && *s2)
279     {
280     d1 = atoi(s1);
281     d2 = atoi(s2);
282     if(d1 != d2)
283     {
284     retval = d1 - d2;
285     break;
286     }
287     }
288     if(!vc1 || !vc2)
289     break;
290     s1 = vc1 + 1;
291     s2 = vc2 + 1;
292     }
293     xfree(v1);
294     xfree(v2);
295     return retval;
296     }
297    
298 bertho 1.7 /*
299     **************************************************************************
300     * Reorganise the rcsfile for the branches
301     *
302     * Basically, we have a list of deltas (i.e. administrative info on
303     * revisions) and a list of delta text (the actual logs and diffs).
304     * The deltas are linked through the 'next' and the 'branches' fields
305     * which describe the tree-structure of revisions.
306     * The reorganisation means that we put each delta and corresponding
307     * delta text in a revision structure and assign it to a specific
308     * branch. This is required because we want to be able to calculate
309     * the bounding boxes of each branch. The revisions expand vertically
310     * and the branches expand horizontally.
311     * The reorganisation is performed in these steps:
312     * 1 - sort deltas and detla text on revision number for quick lookup
313     * 2 - start at the denoted head revision:
314     * * create a branch structure and add this revision
315     * * for each 'branches' in the delta do:
316     * - walk all 'branches' of the delta and recursively goto 2
317     * with the denoted branch delta as new head
318     * - backlink the newly create sub-branch to the head revision
319     * so that we can draw them recursively
320     * * set head to the 'next' field and goto 2 until no next is
321     * available
322     * 3 - update the administration
323     **************************************************************************
324     */
325     int sort_delta(const void *d1, const void *d2)
326     {
327     return compare_rev(0, (*(delta_t **)d1)->rev, (*(delta_t **)d2)->rev);
328     }
329    
330     int search_delta(const void *r, const void *d)
331 bertho 1.1 {
332 bertho 1.7 return compare_rev(0, (rev_t *)r, (*(delta_t **)d)->rev);
333 bertho 1.1 }
334    
335 bertho 1.7 delta_t *find_delta(delta_t **dl, int n, rev_t *r)
336 bertho 1.1 {
337 bertho 1.7 delta_t **d;
338     d = bsearch(r, dl, n, sizeof(*dl), search_delta);
339     if(!d)
340     return NULL;
341     return *d;
342 bertho 1.1 }
343    
344 bertho 1.7 int sort_dtext(const void *d1, const void *d2)
345 bertho 1.1 {
346 bertho 1.7 return compare_rev(0, (*(dtext_t **)d1)->rev, (*(dtext_t **)d2)->rev);
347 bertho 1.1 }
348    
349 bertho 1.7 int search_dtext(const void *r, const void *d)
350 bertho 1.1 {
351 bertho 1.7 return compare_rev(0, (rev_t *)r, (*(dtext_t **)d)->rev);
352 bertho 1.1 }
353    
354 bertho 1.7 dtext_t *find_dtext(dtext_t **dl, int n, rev_t *r)
355 bertho 1.1 {
356 bertho 1.7 dtext_t **d;
357     d = bsearch(r, dl, n, sizeof(*dl), search_dtext);
358     if(!d)
359 bertho 1.1 return NULL;
360 bertho 1.7 return *d;
361     }
362    
363     rev_t *dup_rev(const rev_t *r)
364     {
365     rev_t *t = xmalloc(sizeof(*t));
366     t->rev = xstrdup(r->rev);
367     t->branch = xstrdup(r->branch);
368     t->isbranch = r->isbranch;
369     return t;
370     }
371    
372     branch_t *new_branch(delta_t *d, dtext_t *t)
373     {
374     branch_t *b = xmalloc(sizeof(*b));
375     revision_t *r = xmalloc(sizeof(*r));
376     r->delta = d;
377     r->dtext = t;
378     r->rev = d->rev;
379     r->branch = b;
380     b->branch = dup_rev(d->rev);
381     b->branch->isbranch = 1;
382     b->nrevs = 1;
383     b->revs = xmalloc(sizeof(b->revs[0]));
384     b->revs[0] = r;
385     return b;
386     }
387    
388     revision_t *add_to_branch(branch_t *b, delta_t *d, dtext_t *t)
389     {
390     revision_t *r = xmalloc(sizeof(*r));
391     r->delta = d;
392     r->dtext = t;
393     r->rev = d->rev;
394     r->branch = b;
395     b->revs = xrealloc(b->revs, (b->nrevs+1) * sizeof(b->revs[0]));
396     b->revs[b->nrevs] = r;
397     b->nrevs++;
398     return r;
399     }
400    
401     void build_branch(branch_t ***bl, int *nbl, delta_t **sdl, int nsdl, dtext_t **sdt, int nsdt, delta_t *head)
402     {
403     branch_t *b;
404     dtext_t *text;
405     revision_t *currev;
406    
407     assert(head != NULL);
408    
409     if(head->flag)
410     {
411     fprintf(stderr, "Circular reference on '%s' in branchpoint\n", head->rev->rev);
412     return;
413     }
414     head->flag++;
415     text = find_dtext(sdt, nsdt, head->rev);
416    
417     /* Create a new branch for this head */
418     b = new_branch(head, text);
419     *bl = xrealloc(*bl, (*nbl+1)*sizeof((*bl)[0]));
420     (*bl)[*nbl] = b;
421     (*nbl)++;
422     currev = b->revs[0];
423     while(1)
424     {
425     /* Process all sub-branches */
426     if(head->branches)
427     {
428     int i;
429     for(i = 0; i < head->branches->nrevs; i++)
430     {
431     delta_t *d = find_delta(sdl, nsdl, head->branches->revs[i]);
432     int btag = *nbl;
433     if(!d)
434     continue;
435     build_branch(bl, nbl, sdl, nsdl, sdt, nsdt, d);
436    
437     /* Set the new branch's origin */
438     (*bl)[btag]->branchpoint = currev;
439    
440     /* Add branch to this revision */
441     currev->branches = xrealloc(currev->branches, (currev->nbranches+1)*sizeof(currev->branches[0]));
442     currev->branches[currev->nbranches] = (*bl)[btag];
443     currev->nbranches++;
444     }
445     }
446    
447     /* Walk through the next list */
448     if(!head->next)
449     return;
450 bertho 1.9
451 bertho 1.7 head = find_delta(sdl, nsdl, head->next);
452     if(!head)
453     {
454     fprintf(stderr, "Next revision (%s) not found in deltalist\n", head->next->rev);
455     return;
456     }
457     if(head->flag)
458     {
459     fprintf(stderr, "Circular reference on '%s'\n", head->rev->rev);
460     return;
461     }
462     head->flag++;
463     text = find_dtext(sdt, nsdt, head->rev);
464     currev = add_to_branch(b, head, text);
465     }
466     }
467    
468     int reorganise_branches(rcsfile_t *rcs)
469     {
470     delta_t **sdelta;
471     int nsdelta;
472     dtext_t **sdtext;
473     int nsdtext;
474     delta_t *head;
475     branch_t **bl;
476     int nbl;
477     int i;
478    
479     assert(rcs->deltas != NULL);
480     assert(rcs->head != NULL);
481    
482     /* Make a new list for quick lookup */
483     nsdelta = rcs->deltas->ndeltas;
484     sdelta = xmalloc(nsdelta * sizeof(sdelta[0]));
485     memcpy(sdelta, rcs->deltas->deltas, nsdelta * sizeof(sdelta[0]));
486     qsort(sdelta, nsdelta, sizeof(sdelta[0]), sort_delta);
487    
488     /* Do the same for the delta text */
489     nsdtext = rcs->dtexts->ndtexts;
490     sdtext = xmalloc(nsdtext * sizeof(sdtext[0]));
491     memcpy(sdtext, rcs->dtexts->dtexts, nsdtext * sizeof(sdtext[0]));
492     qsort(sdtext, nsdtext, sizeof(sdtext[0]), sort_dtext);
493    
494     /* Start from the head of the trunk */
495     head = find_delta(sdelta, nsdelta, rcs->head);
496     if(!head)
497     {
498     fprintf(stderr, "Head revision (%s) not found in deltalist\n", rcs->head->rev);
499     return 0;
500     }
501     bl = NULL;
502     nbl = 0;
503     build_branch(&bl, &nbl, sdelta, nsdelta, sdtext, nsdtext, head);
504    
505     /* Reverse the head branch */
506     for(i = 0; i < bl[0]->nrevs/2; i++)
507     {
508     revision_t *r;
509     r = bl[0]->revs[i];
510     bl[0]->revs[i] = bl[0]->revs[bl[0]->nrevs-i-1];
511     bl[0]->revs[bl[0]->nrevs-i-1] = r;
512     }
513    
514     /* Update the branch-number of the head because it was reversed */
515     xfree(bl[0]->branch->branch);
516     bl[0]->branch->branch = xstrdup(bl[0]->revs[0]->rev->branch);
517    
518     /* Keep the admin */
519     rcs->branches = bl;
520     rcs->nbranches = nbl;
521     rcs->sdelta = sdelta;
522     rcs->nsdelta = nsdelta;
523     rcs->sdtext = sdtext;
524     rcs->nsdtext = nsdtext;
525     rcs->active = bl[0];
526     return 1;
527     }
528    
529     /*
530     **************************************************************************
531     * Assign the symbolic tags to the revisions and branches
532     *
533     * The tags point to revision numbers somewhere in the tree structure
534     * of branches and revisions. First we make a sorted list of all
535     * revisions and then we assign each tag to the proper revision.
536     **************************************************************************
537     */
538     int sort_revision(const void *r1, const void *r2)
539     {
540     return compare_rev(0, (*(revision_t **)r1)->delta->rev, (*(revision_t **)r2)->delta->rev);
541     }
542    
543     int search_revision(const void *t, const void *r)
544     {
545     return compare_rev(0, (rev_t *)t, (*(revision_t **)r)->delta->rev);
546     }
547    
548     int sort_branch(const void *b1, const void *b2)
549     {
550     return compare_rev(1, (*(branch_t **)b1)->branch, (*(branch_t **)b2)->branch);
551 bertho 1.1 }
552    
553 bertho 1.7 int search_branch(const void *t, const void *b)
554 bertho 1.1 {
555 bertho 1.7 return compare_rev(1, (rev_t *)t, (*(branch_t **)b)->branch);
556 bertho 1.1 }
557    
558 bertho 1.7 char *previous_rev(const char *c)
559 bertho 1.1 {
560 bertho 1.7 int dots = count_dots(c);
561     char *cptr;
562     char *r;
563     if(!dots)
564 bertho 1.9 {
565     fprintf(stderr, "FIXME: previous_rev(\"%s\"): Cannot determine parent branch revision\n", c);
566 bertho 1.7 return xstrdup("1.0"); /* FIXME: don't know what the parent is */
567 bertho 1.9 }
568 bertho 1.7 if(dots & 1)
569     {
570     /* Is is a revision we want the parent of */
571     r = xstrdup(c);
572     cptr = strrchr(r, '.');
573     assert(cptr != NULL);
574     if(dots == 1)
575     {
576 bertho 1.9 fprintf(stderr, "FIXME: previous_rev(\"%s\"): Going beyond top-level?\n", c);
577 bertho 1.7 /* FIXME: What is the parent of 1.1? */
578     cptr[1] = '\0';
579     strcat(r, "0");
580     return r;
581     }
582     /* Here we have a "x.x[.x.x]+" case */
583     *cptr = '\0';
584     cptr = strrchr(r, '.');
585     assert(cptr != NULL);
586     *cptr = '\0';
587     return r;
588     }
589     /* It is a branch we want the parent of */
590     r = xstrdup(c);
591     cptr = strrchr(r, '.');
592     assert(cptr != NULL);
593     *cptr = '\0';
594     return r;
595 bertho 1.1 }
596    
597 bertho 1.7 int assign_tags(rcsfile_t *rcs)
598 bertho 1.1 {
599     int i;
600 bertho 1.7 int nr;
601    
602     for(i = nr = 0; i < rcs->nbranches; i++)
603     nr += rcs->branches[i]->nrevs;
604    
605     rcs->srev = xmalloc(nr * sizeof(rcs->srev[0]));
606     rcs->nsrev = nr;
607     for(i = nr = 0; i < rcs->nbranches; i++)
608     {
609     memcpy(&rcs->srev[nr], rcs->branches[i]->revs, rcs->branches[i]->nrevs * sizeof(rcs->branches[i]->revs[0]));
610     nr += rcs->branches[i]->nrevs;
611     }
612    
613     qsort(rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), sort_revision);
614     qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
615    
616     if(!rcs->branch)
617     {
618     /* The main trunk is the active trunk */
619     rcs->tags->tags = xrealloc(rcs->tags->tags, (rcs->tags->ntags+1)*sizeof(rcs->tags->tags[0]));
620     rcs->tags->tags[rcs->tags->ntags] = xmalloc(sizeof(tag_t));
621     rcs->tags->tags[rcs->tags->ntags]->tag = xstrdup("MAIN");
622     rcs->tags->tags[rcs->tags->ntags]->rev = xmalloc(sizeof(rev_t));
623     rcs->tags->tags[rcs->tags->ntags]->rev->rev = xstrdup(rcs->active->branch->rev);
624     rcs->tags->tags[rcs->tags->ntags]->rev->branch = xstrdup(rcs->active->branch->branch);
625     rcs->tags->tags[rcs->tags->ntags]->rev->isbranch = 1;
626     rcs->tags->ntags++;
627     }
628    
629     /* We should have at least two tags (HEAD and MAIN) */
630     assert(rcs->tags != 0);
631    
632     for(i = 0; i < rcs->tags->ntags; i++)
633     {
634     tag_t *t = rcs->tags->tags[i];
635     if(t->rev->isbranch)
636     {
637     branch_t **b;
638     add_btag:
639     b = bsearch(t->rev, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
640     if(!b)
641     {
642     rev_t rev;
643     revision_t **r;
644     /* This happens for the magic branch numbers if there are
645 bertho 1.12 * no commits within the new branch yet. So, we add the
646     * branch and try to continue.
647 bertho 1.7 */
648     rev.rev = previous_rev(t->rev->branch);
649     rev.branch = NULL;
650     rev.isbranch = 0;
651     r = bsearch(&rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
652     xfree(rev.rev);
653     if(!r)
654     {
655 bertho 1.12 if(!quiet)
656     fprintf(stderr, "No branch found for tag '%s:%s'\n", t->tag, t->rev->branch);
657 bertho 1.7 }
658     else
659     {
660     rcs->branches = xrealloc(rcs->branches, (rcs->nbranches+1)*sizeof(rcs->branches[0]));
661     rcs->branches[rcs->nbranches] = xmalloc(sizeof(branch_t));
662     rcs->branches[rcs->nbranches]->branch = dup_rev(t->rev);
663     rcs->branches[rcs->nbranches]->branchpoint = *r;
664     (*r)->branches = xrealloc((*r)->branches, ((*r)->nbranches+1)*sizeof((*r)->branches[0]));
665     (*r)->branches[(*r)->nbranches] = rcs->branches[rcs->nbranches];
666     (*r)->nbranches++;
667     rcs->nbranches++;
668     /* Resort the branches */
669     qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
670     goto add_btag;
671     }
672     }
673     else
674     {
675     branch_t *bb = *b;
676     bb->tags = xrealloc(bb->tags, (bb->ntags+1)*sizeof(bb->tags[0]));
677     bb->tags[bb->ntags] = t;
678     bb->ntags++;
679     }
680     }
681     else
682     {
683     revision_t **r = bsearch(t->rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
684     if(!r)
685 bertho 1.12 {
686     if(!quiet)
687     fprintf(stderr, "No revision found for tag '%s:%s'\n", t->tag, t->rev->rev);
688     }
689 bertho 1.7 else
690     {
691     revision_t *rr = *r;
692     rr->tags = xrealloc(rr->tags, (rr->ntags+1)*sizeof(rr->tags[0]));
693     rr->tags[rr->ntags] = t;
694     rr->ntags++;
695     }
696     }
697     }
698    
699     /* We need to reset the first in the list of branches to the
700     * active branch to ensure the drawing of all
701     */
702     if(rcs->active != rcs->branches[0])
703 bertho 1.1 {
704 bertho 1.7 branch_t **b = bsearch(rcs->active->branch, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
705     branch_t *t;
706     assert(b != NULL);
707     t = *b;
708     *b = rcs->branches[0];
709     rcs->branches[0] = t;
710 bertho 1.1 }
711 bertho 1.7 return 1;
712 bertho 1.1 }
713    
714     /*
715     **************************************************************************
716 bertho 1.8 * String expansion
717     **************************************************************************
718     */
719     static char *_string;
720     static int _nstring;
721     static int _nastring;
722    
723     void add_string_str(const char *s)
724     {
725     int l = strlen(s) + 1;
726     if(_nstring + l > _nastring)
727     {
728     _nastring += 128;
729     _string = xrealloc(_string, _nastring * sizeof(_string[0]));
730     }
731     memcpy(_string+_nstring, s, l);
732     _nstring += l-1;
733     }
734    
735     void add_string_ch(int ch)
736     {
737     char buf[2];
738     buf[0] = ch;
739     buf[1] = '\0';
740     add_string_str(buf);
741     }
742    
743 bertho 1.9 void add_string_date(const char *d)
744     {
745     struct tm tm, *tmp;
746     int n;
747     time_t t;
748     char *buf;
749     int nbuf;
750    
751     memset(&tm, 0, sizeof(tm));
752     n = sscanf(d, "%d.%d.%d.%d.%d.%d",
753     &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
754     &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
755     tm.tm_mon--;
756     if(tm.tm_year > 1900)
757     tm.tm_year -= 1900;
758     t = mktime(&tm);
759     if(n != 6 || t == (time_t)(-1))
760     {
761     add_string_str("<invalid date>");
762     return;
763     }
764    
765     tmp = localtime(&t);
766     nbuf = strlen(conf.date_format) * 16; /* Should be enough to hold all types of expansions */
767     buf = xmalloc(nbuf);
768     strftime(buf, nbuf, conf.date_format, tmp);
769     add_string_str(buf);
770     xfree(buf);
771     }
772    
773     char *expand_string(const char *s, rcsfile_t *rcs, revision_t *r, rev_t *rev, rev_t *prev, tag_t *tag)
774 bertho 1.8 {
775     char nb[32];
776     char nr[32];
777     char *base;
778    
779     if(!s)
780     return xstrdup("");
781    
782     _nstring = 0;
783     if(_string)
784     _string[0] = '\0';
785    
786     sprintf(nb, "%d", rcs->nbranches);
787     sprintf(nr, "%d", rcs->nsrev);
788     for(; *s; s++)
789     {
790     if(*s == '%')
791     {
792     switch(*++s)
793     {
794 bertho 1.12 case 'c':
795     case 'C':
796     add_string_str(conf.cvsroot);
797     if(*s == 'C' && conf.cvsroot[0] && conf.cvsroot[strlen(conf.cvsroot)-1] == '/')
798     {
799     /* Strip the trailing '/' */
800     _nstring--;
801     _string[_nstring] = '\0';
802     }
803     break;
804 bertho 1.8 case 'f':
805     case 'F':
806     base = strrchr(rcs->file, '/');
807     if(!base)
808     add_string_str(rcs->file);
809     else
810     add_string_str(base+1);
811     if(*s == 'F' && _string[_nstring-1] == 'v' && _string[_nstring-2] == ',')
812     {
813     _nstring -= 2;
814     _string[_nstring] = '\0';
815     }
816     break;
817     case 'p':
818     base = strrchr(rcs->file, '/');
819     if(base)
820     {
821     char *c = xstrdup(rcs->file);
822     base = strrchr(c, '/');
823     assert(base != NULL);
824     base[1] = '\0';
825     add_string_str(c);
826     xfree(c);
827     }
828 bertho 1.9 /*
829     * We should not add anything here because we can encounter
830     * a completely empty path, in which case we do not want
831     * to add any slash. This prevents a inadvertent root redirect.
832     *
833     * else
834     * add_string_str("/");
835     */
836 bertho 1.8 break;
837 bertho 1.12 case 'm':
838     case 'M':
839     add_string_str(conf.cvsmodule);
840     if(*s == 'M' && conf.cvsmodule[0] && conf.cvsmodule[strlen(conf.cvsmodule)-1] == '/')
841     {
842     /* Strip the trailing '/' */
843     _nstring--;
844     _string[_nstring] = '\0';
845     }
846     break;
847 bertho 1.8 case 'r': add_string_str(nr); break;
848     case 'b': add_string_str(nb); break;
849     case '%': add_string_ch('%'); break;
850     case '0': if(conf.expand[0]) add_string_str(conf.expand[0]); break;
851     case '1': if(conf.expand[1]) add_string_str(conf.expand[1]); break;
852     case '2': if(conf.expand[2]) add_string_str(conf.expand[2]); break;
853     case '3': if(conf.expand[3]) add_string_str(conf.expand[3]); break;
854     case '4': if(conf.expand[4]) add_string_str(conf.expand[4]); break;
855     case '5': if(conf.expand[5]) add_string_str(conf.expand[5]); break;
856     case '6': if(conf.expand[6]) add_string_str(conf.expand[6]); break;
857     case '7': if(conf.expand[7]) add_string_str(conf.expand[7]); break;
858     case '8': if(conf.expand[8]) add_string_str(conf.expand[8]); break;
859     case '9': if(conf.expand[9]) add_string_str(conf.expand[9]); break;
860     case 'R': if(rev && rev->rev) add_string_str(rev->rev); break;
861 bertho 1.9 case 'P': if(prev && prev->rev) add_string_str(prev->rev); break;
862 bertho 1.8 case 'B': if(rev && rev->branch) add_string_str(rev->branch); break;
863     case 't': if(tag && tag->tag) add_string_str(tag->tag); break;
864 bertho 1.9 case 'd': if(r && r->delta && r->delta->date) add_string_date(r->delta->date); break;
865     case 's': if(r && r->delta && r->delta->state) add_string_str(r->delta->state); break;
866     case 'a': if(r && r->delta && r->delta->author) add_string_str(r->delta->author); break;
867 bertho 1.8 default:
868     add_string_ch('%');
869     add_string_ch(*s);
870     break;
871     }
872     }
873     else
874     add_string_ch(*s);
875     }
876     return xstrdup(_string);
877     }
878    
879     /*
880     **************************************************************************
881 bertho 1.1 * Drawing routines
882     **************************************************************************
883     */
884     int get_swidth(const char *s, font_t *f)
885     {
886 bertho 1.9 int n;
887     int m;
888     if(!s || !*s)
889 bertho 1.1 return 0;
890 bertho 1.9 for(n = m = 0; *s; n++, s++)
891     {
892     if(*s == '\n')
893     {
894     if(n > m)
895     m = n;
896     n = 0;
897     }
898     }
899     if(n > m)
900     m = n;
901     return m * (*f)->w;
902 bertho 1.1 }
903    
904     int get_sheight(const char *s, font_t *f)
905     {
906     int nl;
907 bertho 1.9 if(!s || !*s)
908 bertho 1.1 return 0;
909     for(nl = 1; *s; s++)
910     {
911     if(*s == '\n' && s[1])
912     nl++;
913     }
914     return nl * (*f)->h;
915     }
916    
917 bertho 1.9 void draw_rbox(gdImagePtr im, int x1, int y1, int x2, int y2, int r, color_t *color, color_t *bgcolor)
918 bertho 1.1 {
919     int r2 = 2*r;
920     gdImageLine(im, x1+r, y1, x2-r, y1, color->id);
921     gdImageLine(im, x1+r, y2, x2-r, y2, color->id);
922     gdImageLine(im, x1, y1+r, x1, y2-r, color->id);
923     gdImageLine(im, x2, y1+r, x2, y2-r, color->id);
924 bertho 1.9 if(conf.box_shadow)
925     {
926     gdImageLine(im, x1+r+1, y2+1, x2-r, y2+1, black_color.id);
927     gdImageLine(im, x2+1, y1+r+1, x2+1, y2-r, black_color.id);
928     }
929 bertho 1.1 if(r)
930     {
931     gdImageArc(im, x1+r, y1+r, r2, r2, 180, 270, color->id);
932     gdImageArc(im, x2-r, y1+r, r2, r2, 270, 360, color->id);
933     gdImageArc(im, x1+r, y2-r, r2, r2, 90, 180, color->id);
934     gdImageArc(im, x2-r, y2-r, r2, r2, 0, 90, color->id);
935 bertho 1.9 if(conf.box_shadow)
936     {
937     /* FIXME: Pixelization is not correct here */
938     gdImageArc(im, x2-r+1, y2-r+1, r2, r2, 0, 90, black_color.id);
939     }
940 bertho 1.1 }
941 bertho 1.9 gdImageFillToBorder(im, (x1+x2)/2, (y1+y2)/2, color->id, bgcolor->id);
942 bertho 1.1 }
943    
944     void draw_string(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
945     {
946     int xx, yy;
947     switch(align & ALIGN_HX)
948     {
949     default:
950     case ALIGN_HL: xx = 0; break;
951     case ALIGN_HC: xx = -get_swidth(s, f)/2; break;
952     case ALIGN_HR: xx = -get_swidth(s, f); break;
953     }
954     switch(align & ALIGN_VX)
955     {
956     default:
957     case ALIGN_VT: yy = 0; break;
958     case ALIGN_VC: yy = -get_sheight(s, f)/2; break;
959     case ALIGN_VB: yy = -get_sheight(s, f); break;
960     }
961     gdImageString(im, *f, x+xx+1, y+yy, s, c->id);
962     }
963    
964 bertho 1.9 void draw_stringnl(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
965     {
966     char *t;
967     char *d;
968     d = s = xstrdup(s);
969     do
970     {
971     t = strchr(s, '\n');
972     if(t)
973     *t = '\0';
974     draw_string(im, s, f, x, y, align, c);
975     y += get_sheight(s, f);
976     s = t+1;
977     } while(t);
978     xfree(d);
979     }
980    
981 bertho 1.1 void draw_rev(gdImagePtr im, int cx, int ty, revision_t *r)
982     {
983     int lx = cx - r->w/2;
984     int rx = lx + r->w;
985     int i;
986 bertho 1.9 draw_rbox(im, lx, ty, rx, ty+r->h, 0, &conf.rev_color, &conf.rev_bgcolor);
987 bertho 1.1 ty += conf.rev_tspace;
988     draw_string(im, r->rev->rev, &conf.rev_font, cx, ty, ALIGN_HC, &conf.rev_color);
989     ty += get_sheight(r->rev->rev, &conf.rev_font);
990 bertho 1.9 draw_stringnl(im, r->revtext, &conf.rev_text_font, cx, ty, ALIGN_HC, &conf.rev_text_color);
991     ty += get_sheight(r->revtext, &conf.rev_text_font);
992 bertho 1.1 for(i = 0; i < r->ntags; i++)
993     {
994     draw_string(im, r->tags[i]->tag, &conf.tag_font, cx, ty, ALIGN_HC, &conf.tag_color);
995     ty += get_sheight(r->tags[i]->tag, &conf.tag_font);
996     }
997     }
998    
999     void draw_branch(gdImagePtr im, int cx, int ty, branch_t *b)
1000     {
1001     int lx = cx - b->w/2;
1002     int rx = lx + b->w;
1003     int yy;
1004     int i;
1005 bertho 1.7 /*draw_rbox(im, cx-b->tw/2-1, ty-1, cx+b->tw/2+1, ty+b->th+1, 0, &conf.title_color);*/
1006 bertho 1.9 draw_rbox(im, lx, ty, rx, ty+b->h, 5, &conf.branch_color, &conf.branch_bgcolor);
1007 bertho 1.1 yy = conf.branch_tspace;
1008 bertho 1.7 draw_string(im, b->branch->branch, &conf.branch_font, cx, ty+yy, ALIGN_HC, &conf.branch_color);
1009     yy += get_sheight(b->branch->branch, &conf.branch_font);
1010     for(i = 0; i < b->ntags; i++)
1011 bertho 1.1 {
1012 bertho 1.7 draw_string(im, b->tags[i]->tag, &conf.branch_font, cx, ty+yy, ALIGN_HC, &conf.branch_color);
1013     yy += get_sheight(b->tags[i]->tag, &conf.branch_font);
1014 bertho 1.1 }
1015    
1016     ty += b->h;
1017     for(i = 0; i < b->nrevs; i++)
1018     {
1019     gdImageLine(im, cx, ty, cx, ty+conf.rev_minline, conf.rev_color.id);
1020     ty += conf.rev_minline;
1021     draw_rev(im, cx, ty, b->revs[i]);
1022     ty += b->revs[i]->h;
1023     }
1024     }
1025    
1026     void draw_connector(gdImagePtr im, branch_t *b)
1027     {
1028     revision_t *r = b->branchpoint;
1029 bertho 1.7 int x1 = r->cx + r->w/2 + 2;
1030 bertho 1.1 int y1 = r->y + r->h/2;
1031 bertho 1.7 int x2 = b->cx;
1032 bertho 1.1 int y2 = b->y;
1033     gdImageLine(im, x1, y1, x2, y1, conf.branch_color.id);
1034     gdImageLine(im, x2, y1, x2, y2, conf.branch_color.id);
1035     }
1036    
1037 bertho 1.9 void alloc_color(gdImagePtr im, color_t *c)
1038     {
1039     c->id = gdImageColorAllocate(im, c->r, c->g, c->b);
1040     }
1041    
1042 bertho 1.7 gdImagePtr make_image(rcsfile_t *rcs)
1043 bertho 1.1 {
1044     gdImagePtr im;
1045     int i;
1046 bertho 1.8 char *cptr;
1047 bertho 1.1
1048     im = gdImageCreate(rcs->tw+conf.margin_left+conf.margin_right, rcs->th+conf.margin_top+conf.margin_bottom);
1049 bertho 1.9 alloc_color(im, &conf.color_bg);
1050     alloc_color(im, &conf.tag_color);
1051     alloc_color(im, &conf.rev_color);
1052     alloc_color(im, &conf.rev_bgcolor);
1053     alloc_color(im, &conf.rev_text_color);
1054     alloc_color(im, &conf.branch_color);
1055     alloc_color(im, &conf.branch_bgcolor);
1056     alloc_color(im, &conf.title_color);
1057     alloc_color(im, &black_color);
1058     alloc_color(im, &white_color);
1059 bertho 1.1
1060     for(i = 0; i < rcs->nbranches; i++)
1061 bertho 1.7 draw_branch(im, rcs->branches[i]->cx, rcs->branches[i]->y, rcs->branches[i]);
1062 bertho 1.1 for(i = 0; i < rcs->nbranches; i++)
1063     {
1064     if(rcs->branches[i]->branchpoint)
1065     draw_connector(im, rcs->branches[i]);
1066     }
1067 bertho 1.9 cptr = expand_string(conf.title, rcs, NULL, NULL, NULL, NULL);
1068     draw_stringnl(im, cptr, &conf.title_font, conf.title_x, conf.title_y, conf.title_align, &conf.title_color);
1069 bertho 1.8 xfree(cptr);
1070 bertho 1.1
1071     return im;
1072     }
1073    
1074 bertho 1.7 /*
1075     **************************************************************************
1076     * Layout routines
1077     **************************************************************************
1078     */
1079 bertho 1.1 void move_branch(branch_t *b, int x, int y)
1080     {
1081     int i;
1082 bertho 1.7 b->cx += x;
1083 bertho 1.1 b->y += y;
1084     for(i = 0; i < b->nrevs; i++)
1085     {
1086 bertho 1.7 b->revs[i]->cx += x;
1087 bertho 1.1 b->revs[i]->y += y;
1088     }
1089     }
1090    
1091 bertho 1.6 void reposition_branch(revision_t *r, int *x, int *w)
1092     {
1093     int i, j;
1094     for(j = 0; j < r->nbranches; j++)
1095     {
1096     branch_t *b = r->branches[j];
1097 bertho 1.7 *x += *w + conf.rev_minline + b->tw/2 - b->cx;
1098 bertho 1.6 *w = b->tw/2;
1099 bertho 1.7 move_branch(b, *x, r->y + r->h/2 + conf.branch_connect);
1100     *x = b->cx;
1101 bertho 1.6 /* Recurse to move branches of branched revisions */
1102     for(i = b->nrevs-1; i >= 0; i--)
1103     {
1104     reposition_branch(b->revs[i], x, w);
1105     }
1106     }
1107     }
1108    
1109 bertho 1.1 void rect_union(int *x, int *y, int *w, int *h, branch_t *b)
1110     {
1111     int x1 = *x;
1112     int x2 = x1 + *w;
1113     int y1 = *y;
1114     int y2 = y1 + *h;
1115 bertho 1.7 int xx1 = b->cx - b->tw/2;
1116 bertho 1.1 int xx2 = xx1 + b->tw;
1117     int yy1 = b->y;
1118     int yy2 = yy1 + b->th;
1119     x1 = MIN(x1, xx1);
1120     x2 = MAX(x2, xx2);
1121     y1 = MIN(y1, yy1);
1122     y2 = MAX(y2, yy2);
1123     *x = x1;
1124     *y = y1;
1125     *w = x2 - x1;
1126     *h = y2 - y1;
1127     }
1128    
1129 bertho 1.7 int branch_intersects(int top, int bottom, int left, branch_t *b)
1130     {
1131     int br = b->cx + b->tw/2;
1132     int bt = b->y - conf.branch_connect - conf.branch_margin/2;
1133     int bb = b->y + b->th + conf.branch_margin/2;
1134     return !(bt > bottom || bb < top || br >= left);
1135     }
1136    
1137     int kern_branch(rcsfile_t *rcs, branch_t *b)
1138     {
1139     int left = b->cx - b->tw/2;
1140     int top = b->y - conf.branch_connect - conf.branch_margin/2;
1141     int bottom = b->y + b->th + conf.branch_margin/2;
1142     int i;
1143     int xpos = 0;
1144    
1145     for(i = 0; i < rcs->nbranches; i++)
1146     {
1147     branch_t *bp = rcs->branches[i];
1148     if(bp == b)
1149     continue;
1150     if(branch_intersects(top, bottom, left, bp))
1151     {
1152     int m = bp->cx + bp->tw/2 + conf.branch_margin;
1153     if(m > xpos)
1154     xpos = m;
1155     }
1156     }
1157     if(xpos && (b->cx - b->tw/2) - xpos > 0)
1158     {
1159     move_branch(b, xpos - (b->cx - b->tw/2), 0);
1160     return 1;
1161     }
1162     return 0;
1163     }
1164    
1165     void make_layout(rcsfile_t *rcs)
1166 bertho 1.1 {
1167     int i, j;
1168     int x, y;
1169     int w, h;
1170     int w2;
1171 bertho 1.7 int moved;
1172 bertho 1.1
1173     /* Calculate the box-sizes of the revisions */
1174 bertho 1.7 for(i = 0; i < rcs->nsrev; i++)
1175 bertho 1.1 {
1176     revision_t *rp;
1177     int w;
1178     int h;
1179 bertho 1.7 rp = rcs->srev[i];
1180 bertho 1.9 rp->revtext = expand_string(conf.rev_text, rcs, rp, rp->rev, NULL, rp->ntags ? rp->tags[0] : NULL);
1181     w = get_swidth(rp->revtext, &conf.rev_text_font);
1182     j = get_swidth(rp->rev->rev, &conf.rev_font);
1183     if(j > w)
1184     w = j;
1185     h = get_sheight(rp->revtext, &conf.rev_text_font) + get_sheight(rp->rev->rev, &conf.rev_font);
1186 bertho 1.1 for(j = 0; j < rp->ntags; j++)
1187     {
1188     int ww = get_swidth(rp->tags[j]->tag, &conf.tag_font);
1189     if(ww > w) w = ww;
1190     h += get_sheight(rp->tags[j]->tag, &conf.tag_font) + conf.rev_separator;
1191     }
1192     rp->w = w + conf.rev_lspace + conf.rev_rspace;
1193     rp->h = h + conf.rev_tspace + conf.rev_bspace;
1194     }
1195    
1196     /* Calculate the box-sizes of the branches */
1197     for(i = 0; i < rcs->nbranches; i++)
1198     {
1199     branch_t *bp = rcs->branches[i];
1200     int w;
1201     int h;
1202 bertho 1.7 w = get_swidth(bp->branch->branch, &conf.branch_font);
1203     h = get_sheight(bp->branch->branch, &conf.branch_font);
1204     for(j = 0; j < bp->ntags; j++)
1205 bertho 1.1 {
1206 bertho 1.7 int ww = get_swidth(bp->tags[j]->tag, &conf.branch_font);
1207 bertho 1.1 if(ww > w) w = ww;
1208 bertho 1.7 h += get_sheight(bp->tags[j]->tag, &conf.branch_font);
1209 bertho 1.1 }
1210     w += conf.branch_lspace + conf.branch_rspace;
1211     h += conf.branch_tspace + conf.branch_bspace;
1212     bp->w = w;
1213     bp->h = h;
1214     for(j = 0; j < bp->nrevs; j++)
1215     {
1216     if(bp->revs[j]->w > w)
1217     w = bp->revs[j]->w;
1218     h += bp->revs[j]->h + conf.rev_minline;
1219     }
1220     bp->th = h;
1221     bp->tw = w;
1222     }
1223    
1224     /* Calculate the relative positions of revs in a branch */
1225     for(i = 0; i < rcs->nbranches; i++)
1226     {
1227     branch_t *b = rcs->branches[i];
1228     x = b->tw/2;
1229     y = b->h;
1230 bertho 1.7 b->cx = x;
1231 bertho 1.1 b->y = 0;
1232     for(j = 0; j < b->nrevs; j++)
1233     {
1234     y += conf.rev_minline;
1235 bertho 1.7 b->revs[j]->cx = x;
1236 bertho 1.1 b->revs[j]->y = y;
1237     y += b->revs[j]->h;
1238     }
1239     }
1240    
1241 bertho 1.6 /* Reposition the branches */
1242 bertho 1.7 x = rcs->branches[0]->cx;
1243 bertho 1.1 w2 = rcs->branches[0]->tw / 2;
1244     for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
1245     {
1246 bertho 1.6 reposition_branch(rcs->branches[0]->revs[i], &x, &w2);
1247 bertho 1.1 }
1248    
1249 bertho 1.7 /* Try to move branches left if there is room (kerning) */
1250     for(moved = 1; moved; )
1251     {
1252     moved = 0;
1253     for(i = 1; i < rcs->nbranches; i++)
1254     {
1255     moved += kern_branch(rcs, rcs->branches[i]);
1256     }
1257     }
1258    
1259     /* Move everything w.r.t. the top-left margin */
1260 bertho 1.1 for(i = 0; i < rcs->nbranches; i++)
1261     move_branch(rcs->branches[i], conf.margin_left, conf.margin_top);
1262    
1263     /* Calculate overall image size */
1264 bertho 1.7 x = rcs->branches[0]->cx - rcs->branches[0]->tw/2;
1265 bertho 1.1 y = rcs->branches[0]->y;
1266     w = rcs->branches[0]->tw;
1267     h = rcs->branches[0]->th;
1268     for(i = 1; i < rcs->nbranches; i++)
1269     rect_union(&x, &y, &w, &h, rcs->branches[i]);
1270     rcs->tw = w;
1271     rcs->th = h;
1272     }
1273    
1274     /*
1275     **************************************************************************
1276 bertho 1.6 * Imagemap functions
1277     **************************************************************************
1278     */
1279 bertho 1.7 void make_imagemap(rcsfile_t *rcs, FILE *fp)
1280 bertho 1.6 {
1281     int i, j;
1282 bertho 1.8 char *href;
1283     char *alt;
1284 bertho 1.6 fprintf(fp, "<map name=\"%s\">\n", conf.map_name);
1285     for(i = 0; i < rcs->nbranches; i++)
1286     {
1287     branch_t *b = rcs->branches[i];
1288 bertho 1.8 tag_t *tag = b->ntags ? b->tags[0] : NULL;
1289 bertho 1.9 href = expand_string(conf.map_branch_href, rcs, NULL, b->branch, NULL, tag);
1290     alt = expand_string(conf.map_branch_alt, rcs, NULL, b->branch, NULL, tag);
1291 bertho 1.8 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
1292     href,
1293 bertho 1.7 b->cx - b->w/2, b->y, b->cx + b->w/2, b->y + b->h,
1294 bertho 1.8 alt);
1295     xfree(href);
1296     xfree(alt);
1297 bertho 1.6 for(j = 0; j < b->nrevs; j++)
1298     {
1299     revision_t *r = b->revs[j];
1300 bertho 1.8 tag = r->ntags ? r->tags[0] : NULL;
1301 bertho 1.9 href = expand_string(conf.map_rev_href, rcs, r, r->rev, NULL, tag);
1302     alt = expand_string(conf.map_rev_alt, rcs, r, r->rev, NULL, tag);
1303 bertho 1.8 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
1304     href,
1305 bertho 1.7 r->cx - r->w/2, r->y, r->cx + r->w/2, r->y + r->h,
1306 bertho 1.8 alt);
1307     xfree(href);
1308     xfree(alt);
1309 bertho 1.9 if(j > 0)
1310     {
1311     revision_t *r1 = b->revs[j-1];
1312     int x1 = r->cx - MIN(r->w, r1->w)/4;
1313     int x2 = r->cx + MIN(r->w, r1->w)/4;
1314     href = expand_string(conf.map_diff_href, rcs, r, r->rev, r1->rev, tag);
1315     alt = expand_string(conf.map_diff_alt, rcs, r, r->rev, r1->rev, tag);
1316     fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
1317     href,
1318 bertho 1.11 x1, r1->y + r1->h + 1, x2, r->y - 1,
1319 bertho 1.9 alt);
1320     xfree(href);
1321     xfree(alt);
1322     }
1323 bertho 1.6 }
1324     }
1325     fprintf(fp, "</map>\n");
1326     }
1327    
1328     /*
1329     **************************************************************************
1330 bertho 1.1 * Configuration
1331     **************************************************************************
1332     */
1333     int read_config(const char *path)
1334     {
1335     FILE *fp;
1336     int r;
1337 bertho 1.7
1338 bertho 1.1 if(path)
1339     {
1340     if((fp = fopen(path, "r")) == NULL)
1341     {
1342     return 0;
1343     }
1344 bertho 1.7 else
1345     input_file = path;
1346 bertho 1.1 }
1347     else
1348     {
1349     if((fp = fopen("./" CONFFILENAME, "r")) == NULL)
1350     {
1351     if((fp = fopen(ETCDIR "/" CONFFILENAME, "r")) == NULL)
1352     {
1353     return 0;
1354     }
1355 bertho 1.7 else
1356     input_file = ETCDIR "/" CONFFILENAME;
1357 bertho 1.1 }
1358 bertho 1.7 else
1359     input_file = "./" CONFFILENAME;
1360 bertho 1.1 }
1361    
1362     yyin = fp;
1363     r = yyparse();
1364     fclose(fp);
1365 bertho 1.7 input_file = NULL;
1366 bertho 1.1 return r == 0;
1367     }
1368    
1369     /*
1370     **************************************************************************
1371     * Program entry
1372     **************************************************************************
1373     */
1374     static const char usage_str[] =
1375     "Usage: cvsgraph [options] <file>\n"
1376 bertho 1.8 " -c <file> Read alternative config from <file>\n"
1377     " -d <level> Enable debug mode at <level>\n"
1378     " -h This message\n"
1379     " -i Generate an imagemap instead of image\n"
1380     " -M <name> Use <name> as imagemap name\n"
1381     " -m <mod> Use <mod> as cvs module\n"
1382     " -o <file> Output to <file>\n"
1383     " -q Be quiet (i.e. no warnings)\n"
1384     " -r <path> Use <path> as cvsroot path\n"
1385     " -V Print version and exit\n"
1386     " -[0-9] <txt> Use <txt> for expansion\n"
1387 bertho 1.1 ;
1388    
1389 bertho 1.12 #define VERSION_STR "1.1.2"
1390 bertho 1.1 #define NOTICE_STR "Copyright (c) 2001 B.Stultiens"
1391    
1392 bertho 1.9 void append_slash(char **path)
1393     {
1394     int l;
1395     assert(path != NULL);
1396     assert(*path != NULL);
1397     l = strlen(*path);
1398     if(!l || (*path)[l-1] == '/')
1399     return;
1400     *path = xrealloc(*path, l+2);
1401     strcat(*path, "/");
1402     }
1403    
1404 bertho 1.1 int main(int argc, char *argv[])
1405     {
1406 bertho 1.7 extern int yy_flex_debug;
1407     extern int rcs_flex_debug;
1408     extern int yydebug;
1409     extern int rcsdebug;
1410 bertho 1.1 int optc;
1411     char *confpath = NULL;
1412     char *outfile = NULL;
1413     char *cvsroot = NULL;
1414     char *cvsmodule = NULL;
1415 bertho 1.7 int imagemap = 0;
1416     char *imgmapname = NULL;
1417 bertho 1.1 int lose = 0;
1418     FILE *fp;
1419 bertho 1.7 rcsfile_t *rcs;
1420 bertho 1.1 gdImagePtr im;
1421    
1422 bertho 1.8 while((optc = getopt(argc, argv, "0:1:2:3:4:5:6:7:8:9:c:d:hiM:m:o:qr:V")) != EOF)
1423 bertho 1.1 {
1424     switch(optc)
1425     {
1426     case 'c':
1427     confpath = xstrdup(optarg);
1428     break;
1429 bertho 1.7 case 'd':
1430     debuglevel = strtol(optarg, NULL, 0);
1431     break;
1432 bertho 1.6 case 'i':
1433 bertho 1.7 imagemap = 1;
1434     break;
1435     case 'M':
1436     imgmapname = xstrdup(optarg);
1437 bertho 1.6 break;
1438 bertho 1.1 case 'm':
1439     cvsmodule = xstrdup(optarg);
1440     break;
1441     case 'o':
1442     outfile = xstrdup(optarg);
1443     break;
1444 bertho 1.7 case 'q':
1445     quiet = 1;
1446     break;
1447 bertho 1.1 case 'r':
1448     cvsroot = xstrdup(optarg);
1449     break;
1450     case 'V':
1451     fprintf(stdout, "cvsgraph v%s, %s\n", VERSION_STR, NOTICE_STR);
1452     return 0;
1453     case 'h':
1454     fprintf(stdout, "%s", usage_str);
1455     return 0;
1456     default:
1457 bertho 1.8 if(isdigit(optc))
1458     {
1459     conf.expand[optc-'0'] = xstrdup(optarg);
1460     }
1461     else
1462     lose++;
1463 bertho 1.1 }
1464     }
1465    
1466     if(optind >= argc)
1467     {
1468     fprintf(stderr, "Missing inputfile\n");
1469     lose++;
1470     }
1471    
1472     if(lose)
1473     {
1474     fprintf(stderr, "%s", usage_str);
1475     return 1;
1476     }
1477    
1478 bertho 1.7 if(debuglevel)
1479     {
1480     setvbuf(stdout, NULL, 0, _IONBF);
1481     setvbuf(stderr, NULL, 0, _IONBF);
1482     }
1483     yy_flex_debug = (debuglevel & DEBUG_CONF_LEX) != 0;
1484     rcs_flex_debug = (debuglevel & DEBUG_RCS_LEX) != 0;
1485     yydebug = (debuglevel & DEBUG_CONF_YACC) != 0;
1486     rcsdebug = (debuglevel & DEBUG_RCS_YACC) != 0;
1487    
1488 bertho 1.1 /* Set defaults */
1489 bertho 1.9 conf.tag_font = gdFontTiny;
1490     conf.rev_font = gdFontTiny;
1491     conf.branch_font = gdFontTiny;
1492     conf.title_font = gdFontTiny;
1493     conf.rev_text_font = gdFontTiny;
1494    
1495     conf.cvsroot = xstrdup("");
1496     conf.cvsmodule = xstrdup("");
1497     conf.date_format = xstrdup("%d-%b-%Y %H:%M:%S");
1498     conf.title = xstrdup("");
1499     conf.map_name = xstrdup("CvsGraphImageMap");
1500     conf.map_branch_href = xstrdup("href=\"unset: conf.map_branch_href\"");
1501     conf.map_branch_alt = xstrdup("alt=\"%B\"");
1502     conf.map_rev_href = xstrdup("href=\"unset: conf.map_rev_href\"");
1503     conf.map_rev_alt = xstrdup("alt=\"%R\"");
1504     conf.map_diff_href = xstrdup("href=\"unset: conf.map_diff_href\"");
1505 bertho 1.12 conf.map_diff_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
1506 bertho 1.10 conf.rev_text = xstrdup("%d");
1507 bertho 1.9
1508     conf.color_bg = white_color;
1509     conf.branch_bgcolor = white_color;
1510     conf.branch_color = black_color;
1511     conf.rev_color = black_color;
1512     conf.rev_bgcolor = white_color;
1513     conf.tag_color = black_color;
1514     conf.title_color = black_color;
1515     conf.rev_text_color = black_color;
1516 bertho 1.10
1517     conf.image_quality = 100;
1518 bertho 1.1
1519     if(!read_config(confpath))
1520     {
1521     fprintf(stderr, "Error reading config file\n");
1522     return 1;
1523     }
1524    
1525     /* Set overrides */
1526 bertho 1.7 if(cvsroot) conf.cvsroot = cvsroot;
1527     if(cvsmodule) conf.cvsmodule = cvsmodule;
1528     if(imgmapname) conf.map_name = imgmapname;
1529 bertho 1.1
1530 bertho 1.9 append_slash(&conf.cvsroot);
1531     append_slash(&conf.cvsmodule);
1532    
1533 bertho 1.7 rcs = get_rcsfile(conf.cvsroot, conf.cvsmodule, argv[optind]);
1534 bertho 1.1 if(!rcs)
1535     return 1;
1536    
1537 bertho 1.7 if(debuglevel & DEBUG_RCS_FILE)
1538     dump_rcsfile(rcs);
1539 bertho 1.1
1540 bertho 1.7 if(!reorganise_branches(rcs))
1541     return 1;
1542 bertho 1.1
1543 bertho 1.7 if(!assign_tags(rcs))
1544     return 1;
1545 bertho 1.1
1546     if(outfile)
1547     {
1548     if((fp = fopen(outfile, "w")) == NULL)
1549     {
1550     perror(outfile);
1551     return 1;
1552     }
1553     }
1554     else
1555     fp = stdout;
1556 bertho 1.5
1557 bertho 1.7 make_layout(rcs);
1558    
1559     if(!imagemap)
1560 bertho 1.5 {
1561 bertho 1.7 /* Create an image */
1562     im = make_image(rcs);
1563    
1564     switch(conf.image_type)
1565     {
1566 bertho 1.5 #ifdef HAVE_IMAGE_GIF
1567 bertho 1.7 # ifndef HAVE_IMAGE_PNG
1568     default:
1569     # endif
1570     case IMAGE_GIF:
1571     gdImageGif(im, fp);
1572     break;
1573 bertho 1.5 #endif
1574     #ifdef HAVE_IMAGE_PNG
1575 bertho 1.7 default:
1576     case IMAGE_PNG:
1577     gdImagePng(im, fp);
1578     break;
1579 bertho 1.5 #endif
1580     #ifdef HAVE_IMAGE_JPEG
1581     # if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG)
1582 bertho 1.7 default:
1583 bertho 1.5 # endif
1584 bertho 1.7 case IMAGE_JPEG:
1585     gdImageJpeg(im, fp, conf.image_quality);
1586     break;
1587 bertho 1.5 #endif
1588 bertho 1.7 }
1589    
1590     gdImageDestroy(im);
1591     }
1592     else
1593     {
1594     /* Create an imagemap */
1595     make_imagemap(rcs, fp);
1596 bertho 1.5 }
1597    
1598 bertho 1.1 if(outfile)
1599     fclose(fp);
1600 bertho 1.6
1601 bertho 1.1 return 0;
1602     }
1603    

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0