File: mactab.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 56,880 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,429; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,701; csh: 3,928; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (697 lines) | stat: -rw-r--r-- 15,716 bytes parent folder | download | duplicates (6)
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
/*                                                               -*- C -*-
 *  MACTAB.C
 *
 *  (c)Copyright 1995 by Tobias Ferber,  All Rights Reserved
 *
 *  This file 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 1 of the License,
 *  or (at your option) any later version.
 *
 *  This file 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; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* $VER: $Id: mactab.c,v 1.1 2001-07-18 11:05:53 root Exp $ */

#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

#include "mactab.h"

/* repair buggy realloc() implementation */
#define my_realloc(ptr,size)  ( (ptr) ? realloc(ptr,size) : malloc(size) )

/* #of macro entries added when expanding dynamically */
#define MACRESIZE  10

/* bytesize of a macro table for `n' macros */
#define MACTABSIZE(n)  (size_t)( 2*(n) * sizeof(char *) + 2 * sizeof(char *))

typedef struct macnode {
  struct macnode *succ, *pred;
  int handle;           /* a unique macnode handle */
  int reserved, used;   /* #of macros allocated (reserved) and used in mactab */
  char **mactab;        /* the mactro table for strexpand() */
} macnode_t;


/* globals += */
static int new_handle= 1;
static macnode_t *maclist= (macnode_t *)0;

static macnode_t *macnode_head(macnode_t *this) { return (this && this->pred) ? macnode_head(this->pred) : this; }
static macnode_t *macnode_tail(macnode_t *this) { return (this && this->succ) ? macnode_tail(this->succ) : this; }

/* expand the `macnode.mactab' table for `num_macros' macros */
static int macnode_expand(macnode_t *this, int num_macros)
{
  int result= 0;

  if(this)
  {
    char **new= (char **)my_realloc( (void *)(this->mactab), MACTABSIZE(this->reserved + num_macros) );

    if(new)
    {
      this->mactab= new;
      result= (this->reserved += num_macros);
    }
  }

  return result;
}


/* allocate a new `macnode_t' structure with a `mactab' holding `num_macros' macros */
static macnode_t *macnode_new(int num_macros)
{
  macnode_t *new= (macnode_t *)malloc( sizeof(macnode_t) );

  if(new)
  {
    new->succ     =
    new->pred     = (macnode_t *)0;

    new->reserved =
    new->used     = 0;

    new->mactab   = (char **)0;

    /* allocate the macro table */
    if( macnode_expand(new,num_macros) )
    {
      /* set the last mactro to NIL */
      new->mactab[0] =
      new->mactab[1] = (char *)0;

      /* give this macnode a handle */
      new->handle= new_handle++;
    }
    else /* !new->mactab */
    {
      free(new);
      new= (macnode_t *)0;
    }
  }

  return new;
}


/* remove `this' from the `maclist' -- return `this' */
static macnode_t *macnode_remove(macnode_t *this)
{
  if(this)
  {
    if(this->pred)
      this->pred->succ= this->succ;

    else
      maclist= this->succ;

    if(this->succ)
      this->succ->pred= this->pred;

    this->pred =
    this->succ = (macnode_t *)0;
  }

  return this;
}


/* add a macnode_t list `this' to the global `maclist' */
static void macnode_add(macnode_t *this)
{
  if(this)
  {
    macnode_tail(this)->succ= maclist;
    maclist= macnode_head(this);
  }
}


/* remove `this' from the `maclist' and dispose it */
static macnode_t *macnode_dispose(macnode_t *this)
{
  if(this)
  {
    macnode_remove(this);

    if(this->mactab)
    {
      int n;

      for(n=0; n < 2*this->used; n++)
      {
        if( (this->mactab)[n] )
          free( (this->mactab)[n] );
      }

      free(this->mactab);
    }
    free(this);
  }
  return (macnode_t *)0;
}


/* find the macnode_t in the `maclist' via handle */
static macnode_t *macnode_find(int handle)
{
  macnode_t *this= maclist;

  while( this && (this->handle != handle) )
    this= this->succ;

  return this;
}


/* get the pointer to a macro `lhs' in `this->mactab' or the pointer to a new entry */
static char **macnode_getmac(macnode_t *this, char *lhs)
{
  char **tab= (char **)0;

  if(this && this->mactab && lhs && *lhs)
  {
    int n;

    for(n=0, tab= this->mactab; n < 2*this->used; n+=2, tab= &tab[2])
    {
      if( *tab && !strcmp(*tab,lhs) )
        break;
    }
  }

  return tab;
}



/* modify a macro `lhs' w/ value `rhs' in `this' macnode_t's `mactab'.
   The `rhs' of an existing macro `lhs' is replaced with the given `rhs',
   an `rhs' == (char *)0 can be used to remove `lhs' from `this->mactab'. */

static int macnode_setmac(macnode_t *this, char *lhs, char *rhs)
{
  int err= 0;

  if(this && lhs && *lhs)
  {
    char **ptr= macnode_getmac(this,lhs);

    if(ptr)
    {
      if(*ptr) /* we found the macro `lhs' at the position `ptr' */
      {
        /* free the old `rhs' value */
        if(ptr[1])
          free(ptr[1]);

        if(rhs) /* set the new `rhs' value */
        {
          ptr[1]= strdup(rhs);

          if(!ptr[1])
            err= 3;
        }

        else /* remove this macro */
        {
          if(ptr[0])
            free(ptr[0]);

          while(ptr[2])
          {
            ptr[0]= ptr[2];
            ptr[1]= ptr[3];

            ptr= &ptr[2];
          }

          ptr[0]= ptr[1]= (char *)0;

          this->used--;
        }
      }

      else if(rhs) /* `lhs' is a new macro */
      {
        if(this->used + 1 > this->reserved)
        {
          /* resize the macro table (add MACRESIZE new macro entries) */
          if( macnode_expand(this, MACRESIZE) == 0 )
            err= 4;
        }

        if(err == 0)
        {
          char *l= strdup(lhs);
          char *r= strdup(rhs);

          if(l && r)
          {
            ptr= &(this->mactab)[2*this->used];

            ptr[0]= l;
            ptr[1]= r;

            ptr[2]=
            ptr[3]= (char *)0;

            this->used++;
          }
          else /* strdup() failed */
          {
            if(l) free(l);
            if(r) free(r);

            err= 3;
          }
        }
      }
      /* otherwise this was a no-op (:< */
    }
    else err= 2;
  }
  else err= 1;

  return err;
}

/*
**  PUBLIC
*/


/****** mactab/mactab ********************************************************
*
*   NAME
*       mactab -- Get the macro table associated with a handle
*
*   SYNOPSIS
*       table= mactab( handle );
*
*       char **mactab( int handle );
*
*   FUNCTION
*       This function returns the macro table with the given handle `handle'.
*       The returned macro table can be directly passed to strexpand().  This
*       function will not fail if `handle' is a legal mactab handle.
*
*   INPUTS
*       handle        - The handle of the macro table
*
*   RESULT
*       table         - A macro table for strexpand()
*
*   EXAMPLE
*       {
*         \* create a new macro table *\
*         int my_handle = mactab_new( 3 );
*
*         if(my_handle != 0)
*         {
*           \* initialize macros for a "ASCII->Texinfo" conversion *\
*           int err= mactab_add( my_handle,   "@",  "@@"
*                                             "{",  "@{",
*                                             "}",  "@}",  (char *)0 );
*           if(err == 0)
*           {
*             \* convert our `ascii_string' into Texinfo *\
*             char *texi_string= strexpad( ascii_string, mactab(my_handle) );
*
*             if(texi_string)
*             {
*               \* print the converted string to stdout and dispose it *\
*               puts(texi_string);
*               free(texi_string);
*             }
*             else out_of_memory();
*           }
*           else out_of_memory();
*
*           \* free our macro table *\
*           my_handle= mactab_dispose(my_handle);
*         }
*         else out_of_memory();
*       }
*
*   NOTES
*
*   BUGS
*
*   SEE ALSO
*       mactab_new(), mactab_add(), strexpand()
*
******************************************************************************
*
*  get the macro table `mactab' of the macnode_t in `maclist' with handle `handle'
*/

char **mactab(int handle)
{
  macnode_t *this= macnode_find(handle);
  return this ? this->mactab : (char **)0;
}


/****** mactab/mactab_new ****************************************************
*
*   NAME
*       mactab_new -- Allocate a new macro table
*
*   SYNOPSIS
*       handle= mactab_new( num_macros );
*
*       int mactab_new( int num_macros );
*
*   FUNCTION
*       This function allocates a new macro table which offers enough room
*       to hold `num_macros' macros plus one NULL entry.  The value of
*       `num_macros' should be > 0, however if more macros are added, then
*       the macro table will be expanded dynamically.
*
*   INPUTS
*       num_macros    - The number of macros reserved for this macro table.
*                       (should be > 0)
*
*   RESULT
*       handle        - The handle of the allocated macro table or
*                       0 in case of an error.
*
*   EXAMPLE
*
*   NOTES
*
*   BUGS
*
*   SEE ALSO
*       mactab_dispose(), mactab(), mactab_add(), strexpand()
*
******************************************************************************
*
*  allocate a new macnode_t and reserve `num_macros' macros in the mactab
*/

int mactab_new(int num_macros)
{
  macnode_t *this= macnode_new(num_macros);

  if(this)
    macnode_add(this);

  return this ? this->handle : 0;
}


/****** mactab/mactab_dispose ************************************************
*
*   NAME
*       mactab_dispose -- Free one (or all) macro tables
*
*   SYNOPSIS
*       null_handle= mactab_dispose( handle );
*
*       int mactab_dispose( int handle );
*
*   FUNCTION
*       This function removes the macro table associated with `handle'
*       and returns all memory allocated for it to the system free pool.
*       If a negative handle is passed to this function then *ALL*
*       macro tables will be disposed.
*
*   INPUTS
*       handle        - The handle of the macro table to delete or
*                       `-1' to delete all macro tables.
*
*   RESULT
*       null_handle   - a handle which is never associated with a
*                       macro table.
*
*   EXAMPLE
*
*   NOTES
*
*   BUGS
*
*   SEE ALSO
*       mactab_new(), mactab_remove()
*
******************************************************************************
*
*  dispose the macnode_t with handle `handle' or all macnode_t's if handle < 0
*/

int mactab_dispose(int handle)
{
  if(handle > 0)
  {
    macnode_t *this= macnode_find(handle);

    if(this)
      this= macnode_dispose(this);
  }
  else if(handle < 0)
  {
    while(maclist)
    {
      macnode_t *this= maclist;
      maclist= maclist->succ;
      macnode_dispose(this);
    }
  }

  return 0;
}

/****** mactab/mactab_remove *************************************************
*
*   NAME
*       mactab_remove -- Remove one or more macros from a macro table
*
*   SYNOPSIS
*       error= mactab_remove( handle, macro1, macro2, ..., (char *)0 );
*
*       int mactab_remove( int handle, ... );
*
*   FUNCTION
*       This function removes all given macros from the macro table associated
*       with the handle `handle'.  The list of macro names must be terminated
*       with a (char *)0.
*
*   INPUTS
*       handle        - The handle of the macro table
*       ...           - A list of macro names with a trailing (char *)0
*
*   RESULT
*       error         - != 0 in case of an error,  0 otherwise
*
*   EXAMPLE
*
*   NOTES
*
*   BUGS
*
*   SEE ALSO
*       mactab_get(), mactab_add(), mactab_dispose()
*
******************************************************************************
*
*  delete the given macros from the `mactab' of the macnode with handle `handle'
*/

int mactab_remove(int handle, ...)
{
  int err= 0;

  macnode_t *this= macnode_find(handle);

  if(this)
  {
    char *lhs;

    va_list argp;
    va_start(argp,handle);

    do {
      lhs= va_arg(argp, char *);

      if(lhs && *lhs)
      {
        /* set the `rhs' of each macro `lhs' to NIL */
        err= macnode_setmac(this, lhs, (char *)0);
      }
    } while(lhs && *lhs && !err);

    va_end(argp);
  }
  else err= 1;

  return err;
}


/****** mactab/mactab_add ****************************************************
*
*   NAME
*       mactab_add -- Add one or more macros to a macro table
*
*   SYNOPSIS
*       error= mactab_add( handle, macro1, value1, ..., (char *)0 );
*
*       int mactab_add( int handle, ... );
*
*   FUNCTION
*       This function adds the given macros to the macro table associated with
*       given handle `handle'.  A (char *)0 terminates the list of macro names
*       and macro values.  A macro value (char *)0 can be used to remove this
*       macro from the list of macros in this table and is equivalent to a
*       mactab_remove() call for this macro name.
*
*   INPUTS
*       handle        - The handle of the macro table
*       ...           - A list of macro names and macro values with a
*                       trailing (char *)0
*
*   RESULT
*       error         - != 0 in case of an error,  0 otherwise
*
*   EXAMPLE
*
*   NOTES
*
*   BUGS
*
*   SEE ALSO
*       mactab_remove(), mactab_new(), mactab_get(), mactab(), strexpand()
*
******************************************************************************
*
*  add some macros to the `mactab' of the macnode_t with handle `handle'
*/

int mactab_add(int handle, ...)
{
  int err= 0;

  macnode_t *this= macnode_find(handle);

  if(this)
  {
    char *lhs, *rhs;

    va_list argp;
    va_start(argp,handle);

    do {
      lhs= va_arg(argp, char *);

      if(lhs && *lhs)
      {
        rhs= va_arg(argp, char *);
        err= macnode_setmac(this,lhs,rhs);
      }
    } while(lhs && *lhs && !err);

    va_end(argp);
  }
  else err= 1;

  return err;
}


/****** mactab/mactab_get ****************************************************
*
*   NAME
*       mactab_get -- Get the value of a macro
*
*   SYNOPSIS
*       value= mactab_get( handle, name );
*
*       char *mactab_get( int handle, char *name );
*
*   FUNCTION
*       This function returns the value of the macro `name' out of the table
*       associated with the given handle `handle'.
*
*   INPUTS
*       handle        - The handle of the macro table
*       name          - The name of the macro
*
*   RESULT
*       value         - The value of the macro `name' or 
*                       (char *)0 if `name' was not found
*
*   EXAMPLE
*
*   NOTES
*
*   BUGS
*
*   SEE ALSO
*       mactab_new(), mactab_add(), mactab()
*
******************************************************************************
*
*  get the macro table `mactab' of the macnode_t in `maclist' with handle `handle'
*/

char *mactab_get(int handle, char *lhs)
{
  char *rhs= (char *)0;

  macnode_t *this= macnode_find(handle);

  if(this)
  {
    char **ptr= macnode_getmac(this,lhs);

    if(ptr && *ptr)
      rhs= ptr[1];
  }

  return rhs;
}


#ifdef DEBUG
#include <stdio.h>

void mactab_debug(FILE *fp)
{
  macnode_t *this;

  for(this= maclist; this; this= this->succ)
  {
    char **tab= this->mactab;

    fprintf(fp,"handle %d:  (reserved: %d, used: %d)\n",this->handle, this->reserved, this->used);

    if(tab)
    {
      int n;

      for(n=0; n<this->used; n++)
      {
        fprintf(fp,"\t\"%s\"\t\"%s\"\n", tab[0] ? tab[0] : "(nil)", tab[1] ? tab[1] : "(nil)");
        tab= &tab[2];
      }
    }

    fprintf(fp,"\n");
  }
}

#endif /* DEBUG */