summaryrefslogtreecommitdiffstats
path: root/libmscgen/mscgen_msc.c
blob: a18e2619bea5cad7efb5bca831c2782497702b91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/***************************************************************************
 *
 * $Id: msc.c 175 2011-02-06 21:07:43Z Michael.McTernan $
 *
 * The message sequence parser ADT.
 * Copyright (C) 2009 Michael C McTernan, Michael.McTernan.2001@cs.bris.ac.uk
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 ***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "mscgen_config.h"
#include "mscgen_safe.h"
#include "mscgen_msc.h"

/***************************************************************************
 * Structures
 ***************************************************************************/

struct MscEntityTag
{
    char                *label;
    struct MscAttribTag *attr;
    struct MscEntityTag *next;
};

struct MscEntityListTag
{
    unsigned int         elements;
    struct MscEntityTag *head, *tail;
};

struct MscArcTag
{
    char                *src, *dst;
    MscArcType           type;
    unsigned int         inputLine;
    struct MscAttribTag *attr;
    struct MscArcTag    *next;
};

struct MscArcListTag
{
    unsigned int         elements;
    unsigned int         parElements;
    struct MscArcTag    *head, *tail;
};


struct MscAttribTag
{
    MscAttribType       type;
    char                *value;
    struct MscAttribTag *next;
};

struct MscOptTag
{
    MscOptType          type;
    char               *value;
    struct MscOptTag   *next;
};

struct MscTag
{
    struct MscOptTag        *optList;
    struct MscEntityListTag *entityList;
    struct MscArcListTag    *arcList;

    struct MscArcTag        *nextArc;
    struct MscEntityTag     *nextEntity;
};

/***************************************************************************
 * Local Functions
 ***************************************************************************/

/** Find come attribute in an attribute list.
 *
 * \param[in] attr  Head of the linked list to search.
 * \param[in] a     The attribute type to find.
 * \retval  NULL    If the attribute was not found or the passed list was NULL.
 */
static const char *findAttrib(const struct MscAttribTag *attr, MscAttribType a)
{
    while(attr != NULL && attr->type != a)
    {
        attr = attr->next;
    }

    if(attr != NULL)
    {
        return attr->value;
    }
    else
    {
        return NULL;
    }
}


/** Free the memory underlying a list of attributes.
 */
static void freeAttribList(struct MscAttribTag *attr)
{
    while(attr)
    {
        struct MscAttribTag *next = attr->next;
        free(attr->value);
        free(attr);
        attr = next;
    }
}

/***************************************************************************
 * Option Functions
 ***************************************************************************/

/* Allocate some option and set it's value.
 */
struct MscOptTag *MscAllocOpt(MscOptType  type,
                              char       *value)
{
    struct MscOptTag *a = malloc_s(sizeof(struct MscOptTag));

    a->type  = type;
    a->value = value;
    a->next  = NULL;

    return a;
}

/* Link one or two options together.
 */
struct MscOptTag *MscLinkOpt(struct MscOptTag *head,
                             struct MscOptTag *newHead)
{
    struct MscOptTag *tail = newHead;

    assert(newHead);

    /* Find the end of the list */
    while(tail->next)
    {
        tail = tail->next;
    }

    tail->next = head;
    return newHead;
}

/* MscPrettyOptType
 *  Returns a string that is the human readable form of the option
 *  code passed to the function.
 */
const char *MscPrettyOptType(MscOptType t)
{
    switch(t)
    {
        case MSC_OPT_HSCALE:      return "hscale";
        case MSC_OPT_WIDTH:       return "width";
        case MSC_OPT_ARCGRADIENT: return "arcgradient";
        default:
            return "unknown";
    }
}

struct MscOptTag *MscFindOpt(struct MscOptTag *list,
                             MscOptType        type)
{
    struct MscOptTag *elem = list;

    while(elem && elem->type != type)
    {
        elem = elem->next;
    }

    if(elem && elem->type == type)
    {
        return elem;
    }

    return NULL;
}

void MscPrintOptList(struct MscOptTag *list)
{
    struct MscOptTag *elem = list;

    while(elem)
    {
        printf("%p: %s=%s\n", elem, MscPrettyOptType(elem->type), elem->value);
        elem = elem->next;
    }
}

/***************************************************************************
 * Entity Functions
 ***************************************************************************/

/* MscAllocEntity
 *  Allocate some entity and set it's name.
 */
struct MscEntityTag *MscAllocEntity(char *entityName)
{
    struct MscEntityTag *e = malloc_s(sizeof(struct MscEntityListTag));

    e->label = entityName;
    e->attr  = NULL;
    e->next  = NULL;

    return e;
}


/* MscLinkEntity
 *  Link some entity onto a list, possibly producing a new head element.
 */
struct MscEntityListTag *MscLinkEntity(struct MscEntityListTag *list,
                                       struct MscEntityTag     *elem)
{
    /* Check if the list has been allocated or not */
    if(list == NULL)
    {
        list = zalloc_s(sizeof(struct MscEntityListTag));
    }

    /* Check for an empty list */
    if(list->head == NULL)
    {
        list->head = list->tail = elem;
    }
    else
    {
        /* Add to tail */
        list->tail->next = elem;
        list->tail = elem;
    }

    /* Increment count of elements */
    list->elements++;

    return list;
}


void MscPrintEntityList(struct MscEntityListTag *list)
{
    struct MscEntityTag *elem = list->head;

    while(elem)
    {
        printf("%p: %s\n", elem, elem->label);
        MscPrintAttrib(elem->attr);
        elem = elem->next;
    }
}

/***************************************************************************
 * Arc Functions
 ***************************************************************************/

/* MscAllocArc
 *  Allocate an arc, filling in the src and dst entities.
 */
struct MscArcTag *MscAllocArc(char        *srcEntity,
                              char        *dstEntity,
                              MscArcType   type,
                              unsigned int inputLine)
{
    struct MscArcTag *a = malloc_s(sizeof(struct MscArcTag));

    /* A discontinuity arcs are not between entities */
    if(type == MSC_ARC_DISCO)
    {
        assert(srcEntity == NULL && dstEntity == NULL);
    }

    a->inputLine = inputLine;
    a->src  = srcEntity;
    a->dst  = dstEntity;
    a->type = type;
    a->next = NULL;
    a->attr = NULL;

    return a;
}


/* MscLinkArc
 *  Link some entity onto a list, possibly producing a new head element.
 */
struct MscArcListTag *MscLinkArc(struct MscArcListTag *list,
                                 struct MscArcTag     *elem)
{
    /* Check if the list has been allocated or not */
    if(list == NULL)
    {
        list = zalloc_s(sizeof(struct MscArcListTag));
    }

    /* Check for an empty list */
    if(list->head == NULL)
    {
        list->head = list->tail = elem;
    }
    else
    {
        /* Add to tail */
        list->tail->next = elem;
        list->tail = elem;
    }

    /* Increment count of elements */
    list->elements++;
    if(elem->type == MSC_ARC_PARALLEL)
    {
        /* A parallel arc is a place holder, and indicates the next arc
         *  is on the same line.  It also needs to account itself, so subtract
         *  two here.
         */
        list->parElements += 2;
    }

    return list;
}


/* MscPrintArcList
 *  Dump and arc list.
 */
void MscPrintArcList(struct MscArcListTag *list)
{
    struct MscArcTag *elem = list->head;

    while(elem)
    {
        printf("%p: '%s' -> '%s'\n", elem, elem->src, elem->dst);
        MscPrintAttrib(elem->attr);

        elem = elem->next;
    }
}


/***************************************************************************
 * Attribute functions
 ***************************************************************************/

/* MscAllocAttrib
 *  Allocate some attribute.
 */
struct MscAttribTag *MscAllocAttrib(MscAttribType  type,
                                    char          *value)
{
    struct MscAttribTag *a = malloc_s(sizeof(struct MscAttribTag));

    a->type  = type;
    a->value = value;
    a->next  = NULL;

    return a;
}


/* MscLinkAttrib
 *  Link some attributes.  The ordering of attributes is semi-important
 *  so the list is grown from the tail.
 */
struct MscAttribTag *MscLinkAttrib(struct MscAttribTag *head,
                                   struct MscAttribTag *newHead)
{
    struct MscAttribTag *tail = newHead;

    assert(newHead);

    /* Find the end of the list */
    while(tail->next)
    {
        tail = tail->next;
    }

    tail->next = head;
    return newHead;
}


/* MscArcLinkAttrib
 *  Attach some attributes to some arc.
 */
void MscArcLinkAttrib(struct MscArcTag    *arc,
                      struct MscAttribTag *att)
{
    if(arc->attr)
    {
        arc->attr = MscLinkAttrib(arc->attr, att);
    }
    else
    {
        arc->attr = att;
    }
}


/* MscEntityLinkAttrib
 *  Attach some attributes to some entity.
 */
void MscEntityLinkAttrib(struct MscEntityTag *ent,
                         struct MscAttribTag *att)
{
    if(ent->attr)
    {
        ent->attr = MscLinkAttrib(ent->attr, att);
    }
    else
    {
        ent->attr = att;
    }
}


/* MscPrintAttrib
 *  String a human readable version of the passed attribute list to stdout.
 */
void MscPrintAttrib(const struct MscAttribTag *att)
{
    while(att)
    {
        printf("  %s = %s\n", MscPrettyAttribType(att->type), att->value);
        att = att->next;
    }

}


/* MscPrettyAttribType
 *  Returns a string that is the human readable form of the attribute
 *  code passed to the function.
 */
const char *MscPrettyAttribType(MscAttribType t)
{
    switch(t)
    {
        case MSC_ATTR_LABEL:             return "label";
        case MSC_ATTR_URL:               return "url";
        case MSC_ATTR_ID:                return "id";
        case MSC_ATTR_IDURL:             return "idurl";
        case MSC_ATTR_LINE_COLOUR:       return "linecolour";
        case MSC_ATTR_TEXT_COLOUR:       return "textcolour";
        case MSC_ATTR_TEXT_BGCOLOUR:     return "textbgcolour";
        case MSC_ATTR_ARC_LINE_COLOUR:   return "arclinecolour";
        case MSC_ATTR_ARC_TEXT_COLOUR:   return "arctextcolour";
        case MSC_ATTR_ARC_TEXT_BGCOLOUR: return "arctextbgcolour";
        case MSC_ATTR_NO_ARROWS:         return "noarrows";
        case MSC_ATTR_BI_ARROWS  :       return "biarrows";
        default:
            return "<unknown>";
    }
}


/***************************************************************************
 * MSC Functions
 ***************************************************************************/

struct MscTag *MscAlloc(struct MscOptTag        *optList,
                        struct MscEntityListTag *entityList,
                        struct MscArcListTag    *arcList)
{
    struct MscTag *m = malloc_s(sizeof(struct MscTag));

    /* Copy the lists */
    m->optList    = optList;
    m->entityList = entityList;
    m->arcList    = arcList;

    /* Reset the iterators */
    MscResetEntityIterator(m);
    MscResetArcIterator(m);

    return m;
}

void MscFree(struct MscTag *m)
{
    struct MscOptTag    *opt    = m->optList;
    struct MscEntityTag *entity = m->entityList->head;
    struct MscArcTag    *arc    = m->arcList->head;

    while(opt)
    {
        struct MscOptTag *next = opt->next;

        free(opt->value);
        free(opt);

        opt = next;
    }

    while(entity)
    {
        struct MscEntityTag *next = entity->next;

        freeAttribList(entity->attr);
        free(entity->label);
        free(entity);

        entity = next;
    }

    while(arc)
    {
        struct MscArcTag *next = arc->next;

        freeAttribList(arc->attr);
        free(arc->dst);
        free(arc->src);
        free(arc);

        arc = next;
    }

    free(m->entityList);
    free(m->arcList);
    free(m);
}

void MscPrint(struct MscTag *m)
{
    printf("Option list (%d options)\n", MscGetNumOpts(m));
    MscPrintOptList(m->optList);

    printf("Entity list (%d entities, %d parallel)\n",
           MscGetNumEntities(m), MscGetNumParallelArcs(m));
    MscPrintEntityList(m->entityList);

    printf("\nArc list (%d arcs)\n", MscGetNumArcs(m));
    MscPrintArcList(m->arcList);
}

unsigned int MscGetNumEntities(struct MscTag *m)
{
    return m->entityList->elements;
}

unsigned int MscGetNumArcs(Msc m)
{
    return m->arcList->elements;
}

unsigned int MscGetNumParallelArcs(Msc m)
{
    return m->arcList->parElements;
}

unsigned int MscGetNumOpts(Msc m)
{
    struct MscOptTag *elem  = m->optList;
    unsigned int      count = 0;

    while(elem)
    {
        count++;
        elem = elem->next;
    }

    return count;
}

int MscGetEntityIndex(struct MscTag *m, const char *label)
{
    struct MscEntityTag *entity = m->entityList->head;
    int                  c      = 0;

    assert(label);

    while(entity != NULL && strcmp(entity->label, label) != 0)
    {
        entity = entity->next;
        c++;
    }

    if(entity == NULL)
    {
        return -1;
    }
    else
    {
        return c;
    }
}

void MscResetEntityIterator(struct MscTag *m)
{
    m->nextEntity = m->entityList->head;
}

Boolean MscNextEntity(struct MscTag *m)
{
    if(m->nextEntity->next != NULL)
    {
        m->nextEntity = m->nextEntity->next;
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}


const char *MscGetCurrentEntAttrib(struct MscTag *m, MscAttribType a)
{
    const char          *r;

    if(!m->nextEntity)
    {
       return NULL;
    }

    r = findAttrib(m->nextEntity->attr, a);

    /* If the entity label was sought but not found, return entity name */
    if(r == NULL && a == MSC_ATTR_LABEL)
    {
        return m->nextEntity->label;
    }
    else
    {
        return r;
    }
}


const char *MscGetEntAttrib(Msc m, unsigned int entIdx, MscAttribType a)
{
    struct MscEntityTag *entity;
    const char          *r;

    /* Find the entity */
    entity = m->entityList->head;
    while(entIdx > 0 && entity != NULL)
    {
        entity = entity->next;
        entIdx--;
    }

    /* Search the attribute list if the entity was found */
    if(entity)
    {
        r = findAttrib(entity->attr, a);

        /* If the entity label was sought but not found, return entity name */
        if(r == NULL && a == MSC_ATTR_LABEL)
        {
            return m->nextEntity->label;
        }
        else
        {
            return r;
        }
    }
    else
    {
        /* Entity was not found */
        return NULL;
    }
}


void MscResetArcIterator(struct MscTag *m)
{
    m->nextArc    = m->arcList->head;
}

Boolean MscNextArc(struct MscTag *m)
{
    if(m->nextArc->next != NULL)
    {
        m->nextArc = m->nextArc->next;
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

const char *MscGetCurrentArcSource(struct MscTag *m)
{
    return m->nextArc ? m->nextArc->src : NULL;
}

const char *MscGetCurrentArcDest(struct MscTag *m)
{
    return m->nextArc ? m->nextArc->dst : NULL;
}

MscArcType MscGetCurrentArcType(struct MscTag *m)
{
    return m->nextArc ? m->nextArc->type : MSC_INVALID_ARC_TYPE;
}

const char *MscGetCurrentArcAttrib(struct MscTag *m, MscAttribType a)
{
    struct MscAttribTag *attr;

    if(!m->nextArc)
    {
       return NULL;
    }

    attr = m->nextArc->attr;

    while(attr != NULL && attr->type != a)
    {
        attr = attr->next;
    }

    if(attr != NULL)
    {
        return attr->value;
    }
    else
    {
        return NULL;
    }

}

Boolean MscGetOptAsFloat(struct MscTag *m, MscOptType type, float *const f)
{
    struct MscOptTag *opt = MscFindOpt(m->optList, type);

    if(opt != NULL)
    {
        *f = (float)atof(opt->value);
        return *f != 0.0f;
    }

    return FALSE;
}

unsigned int MscGetCurrentArcInputLine(struct MscTag *m)
{
    if(m->nextArc)
    {
       return m->nextArc->inputLine;
    }
    else
    {
        return 0;
    }
}

Boolean MscGetOptAsBoolean(struct MscTag *m, MscOptType type, Boolean *const b)
{
    struct MscOptTag *opt = MscFindOpt(m->optList, type);

    if(opt != NULL)
    {
        const char *v = opt->value;

        if(strcasecmp(v, "true") == 0 || strcasecmp(v, "yes") == 0 ||
           strcasecmp(v, "on") == 0 || strcasecmp(v, "1") == 0)
        {
            *b = TRUE;
            return TRUE;
        }
        else if(strcasecmp(v, "false") == 0 || strcasecmp(v, "no") == 0 ||
                strcasecmp(v, "off") == 0 || strcasecmp(v, "0") == 0)
        {
            *b = FALSE;
            return TRUE;
        }
        else
        {
            fprintf(stderr, "Warning: Unrecognised boolean option value '%s'.  Valid values are 'true',\n"
                            "         'false', 'yes', 'no', 'on', 'off', '1' and '0'.\n",
                            v);
            return FALSE;
        }
    }

    return FALSE;
}
/* END OF FILE */