~ubuntu-branches/ubuntu/natty/bc/natty

« back to all changes in this revision

Viewing changes to dc/stack.c

  • Committer: Bazaar Package Importer
  • Author(s): John Hasler
  • Date: 2007-12-02 14:46:56 UTC
  • mto: (3.2.1 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20071202144656-vz98qh9dd1zwdyny
Tags: upstream-1.06.94
ImportĀ upstreamĀ versionĀ 1.06.94

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* 
2
2
 * implement stack functions for dc
3
3
 *
4
 
 * Copyright (C) 1994, 1997, 1998 Free Software Foundation, Inc.
 
4
 * Copyright (C) 1994, 1997, 1998, 2000, 2005, 2006 Free Software Foundation, Inc.
5
5
 *
6
6
 * This program is free software; you can redistribute it and/or modify
7
7
 * it under the terms of the GNU General Public License as published by
18
18
 * program's author (see below) or write to:
19
19
 *
20
20
 *    The Free Software Foundation, Inc.
21
 
 *    59 Temple Place, Suite 330
22
 
 *    Boston, MA 02111 USA
 
21
 *    51 Franklin Street, Fifth Floor
 
22
 *    Boston, MA 02110-1301  USA
23
23
 */
24
24
 
25
25
/* This module is the only one that knows what stacks (both the
41
41
#define Empty_Stack     fprintf(stderr, "%s: stack empty\n", progname)
42
42
 
43
43
 
44
 
/* simple linked-list implementaion suffices: */
 
44
/* simple linked-list implementation suffices: */
45
45
struct dc_list {
46
46
        dc_data value;
47
47
        struct dc_array *array; /* opaque */
53
53
static dc_list *dc_stack=NULL;
54
54
 
55
55
/* the named register stacks */
56
 
static dc_list *dc_register[DC_REGCOUNT];
 
56
typedef dc_list *dc_listp;
 
57
static dc_listp dc_register[DC_REGCOUNT];
57
58
 
58
59
 
59
60
/* allocate a new dc_list item */
86
87
        dc_data b;
87
88
        dc_data r;
88
89
 
89
 
        if (!dc_stack || !dc_stack->link){
 
90
        if (dc_stack == NULL  ||  dc_stack->link == NULL){
90
91
                Empty_Stack;
91
92
                return;
92
93
        }
126
127
        dc_data r1;
127
128
        dc_data r2;
128
129
 
129
 
        if (!dc_stack || !dc_stack->link){
 
130
        if (dc_stack == NULL  ||  dc_stack->link == NULL){
130
131
                Empty_Stack;
131
132
                return;
132
133
        }
164
165
        dc_data a;
165
166
        dc_data b;
166
167
 
167
 
        if (!dc_stack || !dc_stack->link){
 
168
        if (dc_stack == NULL  ||  dc_stack->link == NULL){
168
169
                Empty_Stack;
169
170
                return 0;
170
171
        }
198
199
        dc_data c;
199
200
        dc_data r;
200
201
 
201
 
        if (!dc_stack || !dc_stack->link || !dc_stack->link->link){
 
202
        if (dc_stack == NULL
 
203
                        || dc_stack->link == NULL
 
204
                        || dc_stack->link->link == NULL){
202
205
                Empty_Stack;
203
206
                return;
204
207
        }
244
247
        dc_list *n;
245
248
        dc_list *t;
246
249
 
247
 
        for (n=dc_stack; n; n=t){
 
250
        for (n=dc_stack; n!=NULL; n=t){
248
251
                t = n->link;
249
252
                if (n->value.dc_type == DC_NUMBER)
250
253
                        dc_free_num(&n->value.v.number);
297
300
dc_top_of_stack DC_DECLARG((result))
298
301
        dc_data *result DC_DECLEND
299
302
{
300
 
        if (!dc_stack){
 
303
        if (dc_stack == NULL){
301
304
                Empty_Stack;
302
305
                return DC_FAIL;
303
306
        }
322
325
 
323
326
        regid = regmap(regid);
324
327
        r = dc_register[regid];
325
 
        if ( ! r ){
 
328
        if (r==NULL || r->value.dc_type==DC_UNINITIALIZED){
326
329
                fprintf(stderr, "%s: register ", progname);
327
330
                dc_show_id(stderr, regid, " is empty\n");
328
331
                return DC_FAIL;
344
347
 
345
348
        regid = regmap(regid);
346
349
        r = dc_register[regid];
347
 
        if ( ! r )
 
350
        if (r == NULL)
348
351
                dc_register[regid] = dc_alloc();
349
352
        else if (r->value.dc_type == DC_NUMBER)
350
353
                dc_free_num(&r->value.v.number);
369
372
        dc_list *r;
370
373
 
371
374
        r = dc_stack;
372
 
        if (!r){
 
375
        if (r==NULL || r->value.dc_type==DC_UNINITIALIZED){
373
376
                Empty_Stack;
374
377
                return DC_FAIL;
375
378
        }
396
399
 
397
400
        stackid = regmap(stackid);
398
401
        r = dc_register[stackid];
399
 
        if (!r){
 
402
        if (r == NULL){
400
403
                fprintf(stderr, "%s: stack register ", progname);
401
404
                dc_show_id(stderr, stackid, " is empty\n");
402
405
                return DC_FAIL;
418
421
        dc_list *n;
419
422
        int depth=0;
420
423
 
421
 
        for (n=dc_stack; n; n=n->link)
 
424
        for (n=dc_stack; n!=NULL; n=n->link)
422
425
                ++depth;
423
426
        return depth;
424
427
}
442
445
                if (discard_p == DC_TOSS)
443
446
                        dc_free_num(&value.v.number);
444
447
        } else if (value.dc_type == DC_STRING) {
445
 
                length = dc_strlen(value.v.string);
 
448
                length = (int) dc_strlen(value.v.string);
446
449
                if (discard_p == DC_TOSS)
447
450
                        dc_free_str(&value.v.string);
448
451
        } else {
462
465
{
463
466
        dc_list *n;
464
467
 
465
 
        for (n=dc_stack; n; n=n->link)
 
468
        for (n=dc_stack; n!=NULL; n=n->link)
466
469
                dc_print(n->value, obase, DC_WITHNL, DC_KEEP);
467
470
}
468
471
 
475
478
        int array_id DC_DECLEND
476
479
{
477
480
        dc_list *r = dc_register[regmap(array_id)];
478
 
        return r ? r->array : NULL;
 
481
        return r == NULL ? NULL : r->array;
479
482
}
480
483
 
481
484
/* set the current array head for the named array */
488
491
 
489
492
        array_id = regmap(array_id);
490
493
        r = dc_register[array_id];
491
 
        if ( ! r )
 
494
        if (r == NULL)
492
495
                r = dc_register[array_id] = dc_alloc();
493
496
        r->array = new_head;
494
497
}
 
498
 
 
499
 
 
500
/*
 
501
 * Local Variables:
 
502
 * mode: C
 
503
 * tab-width: 4
 
504
 * End:
 
505
 * vi: set ts=4 :
 
506
 */