~ubuntu-branches/ubuntu/maverick/bc/maverick

« back to all changes in this revision

Viewing changes to dc/eval.c

  • Committer: Bazaar Package Importer
  • Author(s): Ian Jackson
  • Date: 2006-04-04 17:21:02 UTC
  • Revision ID: james.westby@ubuntu.com-20060404172102-64vnydii80rn39jt
Tags: 1.06-19ubuntu1
Make dc notice read and write errors on its input and output.
I grepped for mentions of the strings `putc', `print', `getc', `FILE',
`stdin', `stdout' and `stderr' and added calls to new error-checking
functions unless it was clear from the immediately-surrounding code
that the program was exiting nonzero, or would exit nonzero if the
call failed.  I ignored hits in lib/getopt*, which seems to
pervasively ignore write errors when printing usage messages, in the
hope that these were correct.  I _think_ I got them all.  -iwj.

Show diffs side-by-side

added added

removed removed

Lines of Context:
92
92
static int
93
93
input_fil DC_DECLVOID()
94
94
{
 
95
        int c;
95
96
        if (input_pushback != EOF){
96
 
                int c = input_pushback;
 
97
                c = input_pushback;
97
98
                input_pushback = EOF;
98
99
                return c;
99
100
        }
100
 
        return getc(input_fil_fp);
 
101
        c = getc(input_fil_fp);
 
102
        checkferror_input(input_fil_fp);
 
103
        return c;
101
104
}
102
105
 
103
106
/* passed as an argument to dc_getnum */
275
278
                        tmpint = 0;
276
279
                        if (datum.dc_type == DC_NUMBER)
277
280
                                tmpint = dc_num2int(datum.v.number, DC_TOSS);
278
 
                        if ( ! (2 <= tmpint  &&  tmpint <= DC_IBASE_MAX) )
 
281
                        if ( ! (2 <= tmpint  &&  tmpint <= DC_IBASE_MAX) ) {
279
282
                                fprintf(stderr,
280
283
                                                "%s: input base must be a number \
281
284
between 2 and %d (inclusive)\n",
282
285
                                                progname, DC_IBASE_MAX);
283
 
                        else
 
286
                                checkferror_output(stderr);
 
287
                        } else
284
288
                                dc_ibase = tmpint;
285
289
                }
286
290
                break;
289
293
                        tmpint = -1;
290
294
                        if (datum.dc_type == DC_NUMBER)
291
295
                                tmpint = dc_num2int(datum.v.number, DC_TOSS);
292
 
                        if ( ! (tmpint >= 0) )
 
296
                        if ( ! (tmpint >= 0) ) {
293
297
                                fprintf(stderr,
294
298
                                                "%s: scale must be a nonnegative number\n",
295
299
                                                progname);
296
 
                        else
 
300
                                checkferror_output(stderr);
 
301
                        } else
297
302
                                dc_scale = tmpint;
298
303
                }
299
304
                break;
317
322
                        tmpint = 0;
318
323
                        if (datum.dc_type == DC_NUMBER)
319
324
                                tmpint = dc_num2int(datum.v.number, DC_TOSS);
320
 
                        if ( ! (tmpint > 1) )
 
325
                        if ( ! (tmpint > 1) ) {
321
326
                                fprintf(stderr,
322
327
                                                "%s: output base must be a number greater than 1\n",
323
328
                                                progname);
324
 
                        else
 
329
                                checkferror_output(stderr);
 
330
                        } else
325
331
                                dc_obase = tmpint;
326
332
                }
327
333
                break;
362
368
                                fprintf(stderr,
363
369
                                                "%s: square root of nonnumeric attempted\n",
364
370
                                                progname);
 
371
                                checkferror_output(stderr);
365
372
                        }else if (dc_sqrt(datum.v.number, dc_scale, &tmpnum) == DC_SUCCESS){
366
373
                                dc_free_num(&datum.v.number);
367
374
                                datum.v.number = tmpnum;
433
440
                        fprintf(stderr,
434
441
                                        "%s: Q command requires a number >= 1\n",
435
442
                                        progname);
 
443
                        checkferror_output(stderr);
436
444
                }
437
445
                break;
438
446
#if 0
479
487
                        if (datum.dc_type == DC_NUMBER)
480
488
                                tmpint = dc_num2int(datum.v.number, DC_TOSS);
481
489
                        if (dc_pop(&datum) == DC_SUCCESS){
482
 
                                if (tmpint < 0)
 
490
                                if (tmpint < 0) {
483
491
                                        fprintf(stderr,
484
492
                                                        "%s: array index must be a nonnegative integer\n",
485
493
                                                        progname);
486
 
                                else
 
494
                                        checkferror_output(stderr);
 
495
                                } else
487
496
                                        dc_array_set(peekc, tmpint, datum);
488
497
                        }
489
498
                }
495
504
                        tmpint = -1;
496
505
                        if (datum.dc_type == DC_NUMBER)
497
506
                                tmpint = dc_num2int(datum.v.number, DC_TOSS);
498
 
                        if (tmpint < 0)
 
507
                        if (tmpint < 0) {
499
508
                                fprintf(stderr,
500
509
                                                "%s: array index must be a nonnegative integer\n",
501
510
                                                progname);
502
 
                        else
 
511
                                checkferror_output(stderr);
 
512
                        } else
503
513
                                dc_push(dc_array_get(peekc, tmpint));
504
514
                }
505
515
                return DC_EATONE;
506
516
 
507
517
        default:        /* What did that user mean? */
508
518
                fprintf(stderr, "%s: ", progname);
 
519
                checkferror_output(stderr);
509
520
                dc_show_id(stdout, c, " unimplemented\n");
510
521
                break;
511
522
        }
533
544
                fprintf(stderr,
534
545
                                "%s: eval called with non-string argument\n",
535
546
                                progname);
 
547
                checkferror_output(stderr);
536
548
                return DC_OKAY;
537
549
        }
538
550
        s = dc_str2charp(string->v.string);
591
603
                case DC_EVALREG:
592
604
                        if (peekc == EOF){
593
605
                                fprintf(stderr, "%s: unexpected EOS\n", progname);
 
606
                                checkferror_output(stderr);
594
607
                                return DC_OKAY;
595
608
                        }else{
596
609
                                dc_data evalstr;
601
614
                                                        fprintf(stderr,
602
615
                                                                "%s: eval called with non-string argument\n",
603
616
                                                                progname);
 
617
                                                        checkferror_output(stderr);
604
618
                                                        return DC_OKAY;
605
619
                                                }
606
620
                                                dc_free_str(&string->v.string);
621
635
 
622
636
                case DC_EOF_ERROR:
623
637
                        fprintf(stderr, "%s: unexpected EOS\n", progname);
 
638
                        checkferror_output(stderr);
624
639
                        return DC_OKAY;
625
640
                }
626
641
        }
645
660
        stdin_lookahead = EOF;
646
661
        for (c=getc(fp); c!=EOF; c=peekc){
647
662
                peekc = getc(fp);
 
663
                checkferror_input(stdin);
648
664
                /*
649
665
                 * The following if() is the only place where ``stdin_lookahead''
650
666
                 * might be set to other than EOF:
667
683
                        fprintf(stderr,
668
684
                                        "%s: Q command argument exceeded string execution depth\n",
669
685
                                        progname);
670
 
                        if (stdin_lookahead != peekc  &&  fp == stdin)
 
686
                        checkferror_output(stderr);
 
687
                        if (stdin_lookahead != peekc  &&  fp == stdin) {
671
688
                                peekc = getc(fp);
 
689
                                checkferror_input(stdin);
 
690
                        }
672
691
                        break;
673
692
 
674
693
                case DC_INT:
709
728
                                        if (unwind_noexit != DC_TRUE)
710
729
                                                return DC_SUCCESS;
711
730
                                        fprintf(stderr, "%s: Q command argument exceeded string execution depth\n", progname);
 
731
                                        checkferror_output(stderr);
712
732
                                }
713
733
                        }
714
734
                        break;
715
735
 
716
736
                case DC_EOF_ERROR:
717
737
                        fprintf(stderr, "%s: unexpected EOF\n", progname);
 
738
                        checkferror_output(stderr);
718
739
                        return DC_FAIL;
719
740
                }
720
741
        }
 
742
        checkferror_input(fp);
721
743
        return DC_SUCCESS;
722
744
}
723
745