/[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.13 - (show annotations)
Tue Nov 20 15:00:06 2001 UTC (16 years ago) by bertho
Branch: MAIN
CVS Tags: REL_1_1_3
Changes since 1.12: +30 -12 lines
File MIME type: text/plain
implemented diffs from a branch to a branchpoint.
Many thanks to Ed Rapoport (erapoport {at} stratasys.com) for donating this patch.
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 #include <ctype.h>
35 #include <time.h>
36
37 #include <gd.h>
38 #include <gdfontt.h>
39
40 #include "config.h"
41 #include "cvsgraph.h"
42 #include "utils.h"
43 #include "readconf.h"
44 #include "rcs.h"
45
46 #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 /*#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 #define ALIGN_HX 0x0f
71 #define ALIGN_VT 0x00
72 #define ALIGN_VC 0x10
73 #define ALIGN_VB 0x20
74 #define ALIGN_VX 0xf0
75
76 /*
77 **************************************************************************
78 * Globals
79 **************************************************************************
80 */
81
82 config_t conf;
83 int debuglevel;
84 color_t white_color = {255, 255, 255, 0};
85 color_t black_color = {0, 0, 0, 0};
86
87
88 /*
89 **************************************************************************
90 * Dubug routines
91 **************************************************************************
92 */
93 void dump_rev(char *p, rev_t *r)
94 {
95 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 }
101
102 void dump_id(char *p, char *d)
103 {
104 printf("%s", p);
105 if(d)
106 printf("'%s'\n", d);
107 else
108 printf("<null>\n");
109 }
110
111 void dump_idrev(char *p, idrev_t *t)
112 {
113 printf("%s", p);
114 if(t)
115 {
116 printf("'%s' -> ", t->id);
117 dump_rev("", t->rev);
118 }
119 else
120 printf("<null>\n");
121 }
122
123 void dump_tag(char *p, tag_t *t)
124 {
125 printf("%s", p);
126 if(t)
127 {
128 printf("'%s' -> ", t->tag);
129 dump_rev("", t->rev);
130 }
131 else
132 printf("<null>\n");
133 }
134
135 void dump_delta(char *p, delta_t *d)
136 {
137 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 {
145 printf("%sdelta.branch: ", p);
146 dump_rev("", d->branches->revs[i]);
147 }
148 printf("%sdelta.next : ", p);
149 dump_rev("", d->next);
150 printf("\n");
151 }
152
153 void dump_dtext(char *p, dtext_t *d)
154 {
155 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 }
161
162 void dump_rcsfile(rcsfile_t *rcs)
163 {
164 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 }
191
192 /*
193 **************************************************************************
194 * Read the rcs file
195 **************************************************************************
196 */
197 rcsfile_t *get_rcsfile(const char *cvsroot, const char *module, const char *file)
198 {
199 char *cmd = NULL;
200 int rv;
201
202 cmd = xmalloc(strlen(cvsroot) + strlen(module) + strlen(file) + 1);
203 sprintf(cmd, "%s%s%s", cvsroot, module, file);
204 if(!(rcsin = fopen(cmd, "r")))
205 {
206 perror(cmd);
207 return NULL;
208 }
209 input_file = cmd;
210 line_number = 1;
211 rv = rcsparse();
212 fclose(rcsin);
213 if(rv)
214 return NULL;
215 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 }
222
223 /*
224 **************************************************************************
225 * Sort and find helpers
226 **************************************************************************
227 */
228 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 int compare_rev(int bcmp, const rev_t *r1, const rev_t *r2)
240 {
241 int d1, d2;
242 char *c1, *c2;
243 char *v1, *v2;
244 char *s1, *s2;
245 int retval = 0;
246 assert(r1 != NULL);
247 assert(r2 != NULL);
248 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
263 d1 = count_dots(c1);
264 d2 = count_dots(c2);
265 if(d1 != d2)
266 {
267 return d1 - d2;
268 }
269
270 s1 = v1 = xstrdup(c1);
271 s2 = v2 = xstrdup(c2);
272 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 /*
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 {
332 return compare_rev(0, (rev_t *)r, (*(delta_t **)d)->rev);
333 }
334
335 delta_t *find_delta(delta_t **dl, int n, rev_t *r)
336 {
337 delta_t **d;
338 d = bsearch(r, dl, n, sizeof(*dl), search_delta);
339 if(!d)
340 return NULL;
341 return *d;
342 }
343
344 int sort_dtext(const void *d1, const void *d2)
345 {
346 return compare_rev(0, (*(dtext_t **)d1)->rev, (*(dtext_t **)d2)->rev);
347 }
348
349 int search_dtext(const void *r, const void *d)
350 {
351 return compare_rev(0, (rev_t *)r, (*(dtext_t **)d)->rev);
352 }
353
354 dtext_t *find_dtext(dtext_t **dl, int n, rev_t *r)
355 {
356 dtext_t **d;
357 d = bsearch(r, dl, n, sizeof(*dl), search_dtext);
358 if(!d)
359 return NULL;
360 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
451 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 }
552
553 int search_branch(const void *t, const void *b)
554 {
555 return compare_rev(1, (rev_t *)t, (*(branch_t **)b)->branch);
556 }
557
558 char *previous_rev(const char *c)
559 {
560 int dots = count_dots(c);
561 char *cptr;
562 char *r;
563 if(!dots)
564 {
565 fprintf(stderr, "FIXME: previous_rev(\"%s\"): Cannot determine parent branch revision\n", c);
566 return xstrdup("1.0"); /* FIXME: don't know what the parent is */
567 }
568 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 fprintf(stderr, "FIXME: previous_rev(\"%s\"): Going beyond top-level?\n", c);
577 /* 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 }
596
597 int assign_tags(rcsfile_t *rcs)
598 {
599 int i;
600 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 * no commits within the new branch yet. So, we add the
646 * branch and try to continue.
647 */
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 if(!quiet)
656 fprintf(stderr, "No branch found for tag '%s:%s'\n", t->tag, t->rev->branch);
657 }
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 {
686 if(!quiet)
687 fprintf(stderr, "No revision found for tag '%s:%s'\n", t->tag, t->rev->rev);
688 }
689 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 {
704 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 }
711 return 1;
712 }
713
714 /*
715 **************************************************************************
716 * 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 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 {
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 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 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 /*
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 break;
837 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 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 case 'P': if(prev && prev->rev) add_string_str(prev->rev); break;
862 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 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 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 * Drawing routines
882 **************************************************************************
883 */
884 int get_swidth(const char *s, font_t *f)
885 {
886 int n;
887 int m;
888 if(!s || !*s)
889 return 0;
890 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 }
903
904 int get_sheight(const char *s, font_t *f)
905 {
906 int nl;
907 if(!s || !*s)
908 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 void draw_rbox(gdImagePtr im, int x1, int y1, int x2, int y2, int r, color_t *color, color_t *bgcolor)
918 {
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 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 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 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 }
941 gdImageFillToBorder(im, (x1+x2)/2, (y1+y2)/2, color->id, bgcolor->id);
942 }
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 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 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 draw_rbox(im, lx, ty, rx, ty+r->h, 0, &conf.rev_color, &conf.rev_bgcolor);
987 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 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 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 /*draw_rbox(im, cx-b->tw/2-1, ty-1, cx+b->tw/2+1, ty+b->th+1, 0, &conf.title_color);*/
1006 draw_rbox(im, lx, ty, rx, ty+b->h, 5, &conf.branch_color, &conf.branch_bgcolor);
1007 yy = conf.branch_tspace;
1008 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 {
1012 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 }
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 int x1 = r->cx + r->w/2 + 2;
1030 int y1 = r->y + r->h/2;
1031 int x2 = b->cx;
1032 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 void alloc_color(gdImagePtr im, color_t *c)
1038 {
1039 c->id = gdImageColorAllocate(im, c->r, c->g, c->b);
1040 }
1041
1042 gdImagePtr make_image(rcsfile_t *rcs)
1043 {
1044 gdImagePtr im;
1045 int i;
1046 char *cptr;
1047
1048 im = gdImageCreate(rcs->tw+conf.margin_left+conf.margin_right, rcs->th+conf.margin_top+conf.margin_bottom);
1049 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
1060 for(i = 0; i < rcs->nbranches; i++)
1061 draw_branch(im, rcs->branches[i]->cx, rcs->branches[i]->y, rcs->branches[i]);
1062 for(i = 0; i < rcs->nbranches; i++)
1063 {
1064 if(rcs->branches[i]->branchpoint)
1065 draw_connector(im, rcs->branches[i]);
1066 }
1067 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 xfree(cptr);
1070
1071 return im;
1072 }
1073
1074 /*
1075 **************************************************************************
1076 * Layout routines
1077 **************************************************************************
1078 */
1079 void move_branch(branch_t *b, int x, int y)
1080 {
1081 int i;
1082 b->cx += x;
1083 b->y += y;
1084 for(i = 0; i < b->nrevs; i++)
1085 {
1086 b->revs[i]->cx += x;
1087 b->revs[i]->y += y;
1088 }
1089 }
1090
1091 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 *x += *w + conf.rev_minline + b->tw/2 - b->cx;
1098 *w = b->tw/2;
1099 move_branch(b, *x, r->y + r->h/2 + conf.branch_connect);
1100 *x = b->cx;
1101 /* 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 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 int xx1 = b->cx - b->tw/2;
1116 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 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 {
1167 int i, j;
1168 int x, y;
1169 int w, h;
1170 int w2;
1171 int moved;
1172
1173 /* Calculate the box-sizes of the revisions */
1174 for(i = 0; i < rcs->nsrev; i++)
1175 {
1176 revision_t *rp;
1177 int w;
1178 int h;
1179 rp = rcs->srev[i];
1180 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 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 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 {
1206 int ww = get_swidth(bp->tags[j]->tag, &conf.branch_font);
1207 if(ww > w) w = ww;
1208 h += get_sheight(bp->tags[j]->tag, &conf.branch_font);
1209 }
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 b->cx = x;
1231 b->y = 0;
1232 for(j = 0; j < b->nrevs; j++)
1233 {
1234 y += conf.rev_minline;
1235 b->revs[j]->cx = x;
1236 b->revs[j]->y = y;
1237 y += b->revs[j]->h;
1238 }
1239 }
1240
1241 /* Reposition the branches */
1242 x = rcs->branches[0]->cx;
1243 w2 = rcs->branches[0]->tw / 2;
1244 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
1245 {
1246 reposition_branch(rcs->branches[0]->revs[i], &x, &w2);
1247 }
1248
1249 /* 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 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 x = rcs->branches[0]->cx - rcs->branches[0]->tw/2;
1265 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 * Imagemap functions
1277 **************************************************************************
1278 */
1279 void make_imagemap(rcsfile_t *rcs, FILE *fp)
1280 {
1281 int i, j;
1282 char *href;
1283 char *alt;
1284 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 tag_t *tag = b->ntags ? b->tags[0] : NULL;
1289 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 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
1292 href,
1293 b->cx - b->w/2, b->y, b->cx + b->w/2, b->y + b->h,
1294 alt);
1295 xfree(href);
1296 xfree(alt);
1297 for(j = 0; j < b->nrevs; j++)
1298 {
1299 revision_t *r = b->revs[j];
1300 revision_t* r1;
1301 int xoff;
1302 int x1;
1303 int x2;
1304 int y1;
1305
1306 tag = r->ntags ? r->tags[0] : NULL;
1307 href = expand_string(conf.map_rev_href, rcs, r, r->rev, NULL, tag);
1308 alt = expand_string(conf.map_rev_alt, rcs, r, r->rev, NULL, tag);
1309 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
1310 href,
1311 r->cx - r->w/2, r->y, r->cx + r->w/2, r->y + r->h,
1312 alt);
1313 xfree(href);
1314 xfree(alt);
1315 if ( j > 0 || b->branchpoint )
1316 {
1317 if ( j > 0 )
1318 {
1319 r1 = b->revs[j-1];
1320 xoff = MIN(r->w, r1->w)/4;
1321 y1 = r1->y + r1->h;
1322 }
1323 else
1324 {
1325 r1 = b->branchpoint;
1326 xoff = MIN(r->w, b->w)/4;
1327 y1 = b->y + b->h;
1328 }
1329 x1 = r->cx - xoff;
1330 x2 = r->cx + xoff;
1331
1332 href = expand_string(conf.map_diff_href, rcs, r, r->rev, r1->rev, tag);
1333 alt = expand_string(conf.map_diff_alt, rcs, r, r->rev, r1->rev, tag);
1334 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
1335 href,
1336 x1, y1 + 1, x2, r->y - 1,
1337 alt);
1338 xfree(href);
1339 xfree(alt);
1340 }
1341 }
1342 }
1343 fprintf(fp, "</map>\n");
1344 }
1345
1346 /*
1347 **************************************************************************
1348 * Configuration
1349 **************************************************************************
1350 */
1351 int read_config(const char *path)
1352 {
1353 FILE *fp;
1354 int r;
1355
1356 if(path)
1357 {
1358 if((fp = fopen(path, "r")) == NULL)
1359 {
1360 return 0;
1361 }
1362 else
1363 input_file = path;
1364 }
1365 else
1366 {
1367 if((fp = fopen("./" CONFFILENAME, "r")) == NULL)
1368 {
1369 if((fp = fopen(ETCDIR "/" CONFFILENAME, "r")) == NULL)
1370 {
1371 return 0;
1372 }
1373 else
1374 input_file = ETCDIR "/" CONFFILENAME;
1375 }
1376 else
1377 input_file = "./" CONFFILENAME;
1378 }
1379
1380 yyin = fp;
1381 r = yyparse();
1382 fclose(fp);
1383 input_file = NULL;
1384 return r == 0;
1385 }
1386
1387 /*
1388 **************************************************************************
1389 * Program entry
1390 **************************************************************************
1391 */
1392 static const char usage_str[] =
1393 "Usage: cvsgraph [options] <file>\n"
1394 " -c <file> Read alternative config from <file>\n"
1395 " -d <level> Enable debug mode at <level>\n"
1396 " -h This message\n"
1397 " -i Generate an imagemap instead of image\n"
1398 " -M <name> Use <name> as imagemap name\n"
1399 " -m <mod> Use <mod> as cvs module\n"
1400 " -o <file> Output to <file>\n"
1401 " -q Be quiet (i.e. no warnings)\n"
1402 " -r <path> Use <path> as cvsroot path\n"
1403 " -V Print version and exit\n"
1404 " -[0-9] <txt> Use <txt> for expansion\n"
1405 ;
1406
1407 #define VERSION_STR "1.1.2"
1408 #define NOTICE_STR "Copyright (c) 2001 B.Stultiens"
1409
1410 void append_slash(char **path)
1411 {
1412 int l;
1413 assert(path != NULL);
1414 assert(*path != NULL);
1415 l = strlen(*path);
1416 if(!l || (*path)[l-1] == '/')
1417 return;
1418 *path = xrealloc(*path, l+2);
1419 strcat(*path, "/");
1420 }
1421
1422 int main(int argc, char *argv[])
1423 {
1424 extern int yy_flex_debug;
1425 extern int rcs_flex_debug;
1426 extern int yydebug;
1427 extern int rcsdebug;
1428 int optc;
1429 char *confpath = NULL;
1430 char *outfile = NULL;
1431 char *cvsroot = NULL;
1432 char *cvsmodule = NULL;
1433 int imagemap = 0;
1434 char *imgmapname = NULL;
1435 int lose = 0;
1436 FILE *fp;
1437 rcsfile_t *rcs;
1438 gdImagePtr im;
1439
1440 while((optc = getopt(argc, argv, "0:1:2:3:4:5:6:7:8:9:c:d:hiM:m:o:qr:V")) != EOF)
1441 {
1442 switch(optc)
1443 {
1444 case 'c':
1445 confpath = xstrdup(optarg);
1446 break;
1447 case 'd':
1448 debuglevel = strtol(optarg, NULL, 0);
1449 break;
1450 case 'i':
1451 imagemap = 1;
1452 break;
1453 case 'M':
1454 imgmapname = xstrdup(optarg);
1455 break;
1456 case 'm':
1457 cvsmodule = xstrdup(optarg);
1458 break;
1459 case 'o':
1460 outfile = xstrdup(optarg);
1461 break;
1462 case 'q':
1463 quiet = 1;
1464 break;
1465 case 'r':
1466 cvsroot = xstrdup(optarg);
1467 break;
1468 case 'V':
1469 fprintf(stdout, "cvsgraph v%s, %s\n", VERSION_STR, NOTICE_STR);
1470 return 0;
1471 case 'h':
1472 fprintf(stdout, "%s", usage_str);
1473 return 0;
1474 default:
1475 if(isdigit(optc))
1476 {
1477 conf.expand[optc-'0'] = xstrdup(optarg);
1478 }
1479 else
1480 lose++;
1481 }
1482 }
1483
1484 if(optind >= argc)
1485 {
1486 fprintf(stderr, "Missing inputfile\n");
1487 lose++;
1488 }
1489
1490 if(lose)
1491 {
1492 fprintf(stderr, "%s", usage_str);
1493 return 1;
1494 }
1495
1496 if(debuglevel)
1497 {
1498 setvbuf(stdout, NULL, 0, _IONBF);
1499 setvbuf(stderr, NULL, 0, _IONBF);
1500 }
1501 yy_flex_debug = (debuglevel & DEBUG_CONF_LEX) != 0;
1502 rcs_flex_debug = (debuglevel & DEBUG_RCS_LEX) != 0;
1503 yydebug = (debuglevel & DEBUG_CONF_YACC) != 0;
1504 rcsdebug = (debuglevel & DEBUG_RCS_YACC) != 0;
1505
1506 /* Set defaults */
1507 conf.tag_font = gdFontTiny;
1508 conf.rev_font = gdFontTiny;
1509 conf.branch_font = gdFontTiny;
1510 conf.title_font = gdFontTiny;
1511 conf.rev_text_font = gdFontTiny;
1512
1513 conf.cvsroot = xstrdup("");
1514 conf.cvsmodule = xstrdup("");
1515 conf.date_format = xstrdup("%d-%b-%Y %H:%M:%S");
1516 conf.title = xstrdup("");
1517 conf.map_name = xstrdup("CvsGraphImageMap");
1518 conf.map_branch_href = xstrdup("href=\"unset: conf.map_branch_href\"");
1519 conf.map_branch_alt = xstrdup("alt=\"%B\"");
1520 conf.map_rev_href = xstrdup("href=\"unset: conf.map_rev_href\"");
1521 conf.map_rev_alt = xstrdup("alt=\"%R\"");
1522 conf.map_diff_href = xstrdup("href=\"unset: conf.map_diff_href\"");
1523 conf.map_diff_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
1524 conf.rev_text = xstrdup("%d");
1525
1526 conf.color_bg = white_color;
1527 conf.branch_bgcolor = white_color;
1528 conf.branch_color = black_color;
1529 conf.rev_color = black_color;
1530 conf.rev_bgcolor = white_color;
1531 conf.tag_color = black_color;
1532 conf.title_color = black_color;
1533 conf.rev_text_color = black_color;
1534
1535 conf.image_quality = 100;
1536
1537 if(!read_config(confpath))
1538 {
1539 fprintf(stderr, "Error reading config file\n");
1540 return 1;
1541 }
1542
1543 /* Set overrides */
1544 if(cvsroot) conf.cvsroot = cvsroot;
1545 if(cvsmodule) conf.cvsmodule = cvsmodule;
1546 if(imgmapname) conf.map_name = imgmapname;
1547
1548 append_slash(&conf.cvsroot);
1549 append_slash(&conf.cvsmodule);
1550
1551 rcs = get_rcsfile(conf.cvsroot, conf.cvsmodule, argv[optind]);
1552 if(!rcs)
1553 return 1;
1554
1555 if(debuglevel & DEBUG_RCS_FILE)
1556 dump_rcsfile(rcs);
1557
1558 if(!reorganise_branches(rcs))
1559 return 1;
1560
1561 if(!assign_tags(rcs))
1562 return 1;
1563
1564 if(outfile)
1565 {
1566 if((fp = fopen(outfile, "w")) == NULL)
1567 {
1568 perror(outfile);
1569 return 1;
1570 }
1571 }
1572 else
1573 fp = stdout;
1574
1575 make_layout(rcs);
1576
1577 if(!imagemap)
1578 {
1579 /* Create an image */
1580 im = make_image(rcs);
1581
1582 switch(conf.image_type)
1583 {
1584 #ifdef HAVE_IMAGE_GIF
1585 # ifndef HAVE_IMAGE_PNG
1586 default:
1587 # endif
1588 case IMAGE_GIF:
1589 gdImageGif(im, fp);
1590 break;
1591 #endif
1592 #ifdef HAVE_IMAGE_PNG
1593 default:
1594 case IMAGE_PNG:
1595 gdImagePng(im, fp);
1596 break;
1597 #endif
1598 #ifdef HAVE_IMAGE_JPEG
1599 # if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG)
1600 default:
1601 # endif
1602 case IMAGE_JPEG:
1603 gdImageJpeg(im, fp, conf.image_quality);
1604 break;
1605 #endif
1606 }
1607
1608 gdImageDestroy(im);
1609 }
1610 else
1611 {
1612 /* Create an imagemap */
1613 make_imagemap(rcs, fp);
1614 }
1615
1616 if(outfile)
1617 fclose(fp);
1618
1619 return 0;
1620 }

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0