~ubuntu-branches/ubuntu/intrepid/bc/intrepid

« back to all changes in this revision

Viewing changes to dc/stack.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-12-05 12:24:03 UTC
  • mfrom: (3.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20071205122403-rv1a7x90ktu1wl95
Tags: 1.06.94-3ubuntu1
* Merge with Debian; remaining changes:
  - Make bc/dc notice read and write errors on its input and output.

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
44
44
  }while(0)
45
45
 
46
46
 
47
 
/* simple linked-list implementaion suffices: */
 
47
/* simple linked-list implementation suffices: */
48
48
struct dc_list {
49
49
        dc_data value;
50
50
        struct dc_array *array; /* opaque */
56
56
static dc_list *dc_stack=NULL;
57
57
 
58
58
/* the named register stacks */
59
 
static dc_list *dc_register[DC_REGCOUNT];
 
59
typedef dc_list *dc_listp;
 
60
static dc_listp dc_register[DC_REGCOUNT];
60
61
 
61
62
 
62
63
/* allocate a new dc_list item */
89
90
        dc_data b;
90
91
        dc_data r;
91
92
 
92
 
        if (!dc_stack || !dc_stack->link){
 
93
        if (dc_stack == NULL  ||  dc_stack->link == NULL){
93
94
                Empty_Stack;
94
95
                return;
95
96
        }
130
131
        dc_data r1;
131
132
        dc_data r2;
132
133
 
133
 
        if (!dc_stack || !dc_stack->link){
 
134
        if (dc_stack == NULL  ||  dc_stack->link == NULL){
134
135
                Empty_Stack;
135
136
                return;
136
137
        }
169
170
        dc_data a;
170
171
        dc_data b;
171
172
 
172
 
        if (!dc_stack || !dc_stack->link){
 
173
        if (dc_stack == NULL  ||  dc_stack->link == NULL){
173
174
                Empty_Stack;
174
175
                return 0;
175
176
        }
204
205
        dc_data c;
205
206
        dc_data r;
206
207
 
207
 
        if (!dc_stack || !dc_stack->link || !dc_stack->link->link){
 
208
        if (dc_stack == NULL
 
209
                        || dc_stack->link == NULL
 
210
                        || dc_stack->link->link == NULL){
208
211
                Empty_Stack;
209
212
                return;
210
213
        }
251
254
        dc_list *n;
252
255
        dc_list *t;
253
256
 
254
 
        for (n=dc_stack; n; n=t){
 
257
        for (n=dc_stack; n!=NULL; n=t){
255
258
                t = n->link;
256
259
                if (n->value.dc_type == DC_NUMBER)
257
260
                        dc_free_num(&n->value.v.number);
304
307
dc_top_of_stack DC_DECLARG((result))
305
308
        dc_data *result DC_DECLEND
306
309
{
307
 
        if (!dc_stack){
 
310
        if (dc_stack == NULL){
308
311
                Empty_Stack;
309
312
                return DC_FAIL;
310
313
        }
329
332
 
330
333
        regid = regmap(regid);
331
334
        r = dc_register[regid];
332
 
        if ( ! r ){
 
335
        if (r==NULL || r->value.dc_type==DC_UNINITIALIZED){
333
336
                fprintf(stderr, "%s: register ", progname);
334
337
                checkferror_output(stderr);
335
338
                dc_show_id(stderr, regid, " is empty\n");
352
355
 
353
356
        regid = regmap(regid);
354
357
        r = dc_register[regid];
355
 
        if ( ! r )
 
358
        if (r == NULL)
356
359
                dc_register[regid] = dc_alloc();
357
360
        else if (r->value.dc_type == DC_NUMBER)
358
361
                dc_free_num(&r->value.v.number);
377
380
        dc_list *r;
378
381
 
379
382
        r = dc_stack;
380
 
        if (!r){
 
383
        if (r==NULL || r->value.dc_type==DC_UNINITIALIZED){
381
384
                Empty_Stack;
382
385
                return DC_FAIL;
383
386
        }
404
407
 
405
408
        stackid = regmap(stackid);
406
409
        r = dc_register[stackid];
407
 
        if (!r){
 
410
        if (r == NULL){
408
411
                fprintf(stderr, "%s: stack register ", progname);
409
412
                checkferror_output(stderr);
410
413
                dc_show_id(stderr, stackid, " is empty\n");
427
430
        dc_list *n;
428
431
        int depth=0;
429
432
 
430
 
        for (n=dc_stack; n; n=n->link)
 
433
        for (n=dc_stack; n!=NULL; n=n->link)
431
434
                ++depth;
432
435
        return depth;
433
436
}
451
454
                if (discard_p == DC_TOSS)
452
455
                        dc_free_num(&value.v.number);
453
456
        } else if (value.dc_type == DC_STRING) {
454
 
                length = dc_strlen(value.v.string);
 
457
                length = (int) dc_strlen(value.v.string);
455
458
                if (discard_p == DC_TOSS)
456
459
                        dc_free_str(&value.v.string);
457
460
        } else {
471
474
{
472
475
        dc_list *n;
473
476
 
474
 
        for (n=dc_stack; n; n=n->link)
 
477
        for (n=dc_stack; n!=NULL; n=n->link)
475
478
                dc_print(n->value, obase, DC_WITHNL, DC_KEEP);
476
479
}
477
480
 
484
487
        int array_id DC_DECLEND
485
488
{
486
489
        dc_list *r = dc_register[regmap(array_id)];
487
 
        return r ? r->array : NULL;
 
490
        return r == NULL ? NULL : r->array;
488
491
}
489
492
 
490
493
/* set the current array head for the named array */
497
500
 
498
501
        array_id = regmap(array_id);
499
502
        r = dc_register[array_id];
500
 
        if ( ! r )
 
503
        if (r == NULL)
501
504
                r = dc_register[array_id] = dc_alloc();
502
505
        r->array = new_head;
503
506
}
 
507
 
 
508
 
 
509
/*
 
510
 * Local Variables:
 
511
 * mode: C
 
512
 * tab-width: 4
 
513
 * End:
 
514
 * vi: set ts=4 :
 
515
 */