~ubuntu-branches/ubuntu/vivid/bc/vivid-proposed

« back to all changes in this revision

Viewing changes to bc/util.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
 
/* util.c: Utility routines for bc. */
2
 
 
3
1
/*  This file is part of GNU bc.
4
 
    Copyright (C) 1991-1994, 1997, 2000 Free Software Foundation, Inc.
 
2
 
 
3
    Copyright (C) 1991-1994, 1997, 2006 Free Software Foundation, Inc.
5
4
 
6
5
    This program is free software; you can redistribute it and/or modify
7
6
    it under the terms of the GNU General Public License as published by
14
13
    GNU General Public License for more details.
15
14
 
16
15
    You should have received a copy of the GNU General Public License
17
 
    along with this program; see the file COPYING.  If not, write to
 
16
    along with this program; see the file COPYING.  If not, write to:
18
17
      The Free Software Foundation, Inc.
19
 
      59 Temple Place, Suite 330
20
 
      Boston, MA 02111 USA
 
18
      Foundation, Inc.  51 Franklin Street, Fifth Floor,
 
19
      Boston, MA 02110-1301  USA
21
20
 
22
21
    You may contact the author by:
23
22
       e-mail:  philnelson@acm.org
28
27
       
29
28
*************************************************************************/
30
29
 
 
30
/* util.c: Utility routines for bc. */
31
31
 
32
32
#include "bcdefs.h"
33
33
#ifndef VARARGS
35
35
#else
36
36
#include <varargs.h>
37
37
#endif
38
 
#include "global.h"
39
38
#include "proto.h"
40
39
 
41
40
 
237
236
    }
238
237
}
239
238
 
 
239
/* genstr management to avoid buffer overflow. */
 
240
void
 
241
set_genstr_size (size)
 
242
     int size;
 
243
{
 
244
  if (size > genlen) {
 
245
    if (genstr != NULL)
 
246
      free(genstr);
 
247
    genstr = (char *) bc_malloc (size);
 
248
    genlen = size;
 
249
  }
 
250
}
 
251
 
240
252
 
241
253
/* Initialize the code generator the parser. */
242
254
 
255
267
    init_load ();
256
268
  had_error = FALSE;
257
269
  did_gen = FALSE;
 
270
  set_genstr_size (64);
258
271
}
259
272
 
260
273
 
323
336
  else
324
337
    {
325
338
      out_col++;
326
 
      if (out_col == line_size-1)
 
339
      if (out_col == line_size-1 && line_size != 0)
327
340
        {
328
341
          putchar ('\\');
329
342
          putchar ('\n');
353
366
      if (!std_only)
354
367
        {
355
368
          out_col++;
356
 
          if (out_col == line_size-1)
 
369
          if (out_col == line_size-1 && line_size != 0)
357
370
            {
358
371
              putchar ('\\');
359
372
              putchar ('\n');
423
436
          /* The height increased. */
424
437
          (*root)->balance --;
425
438
          
426
 
      switch ((*root)->balance)
427
 
        {
428
 
        case  0:  /* no height increase. */
429
 
          return (FALSE);
430
 
        case -1:  /* height increase. */
431
 
          return (FALSE);
432
 
        case -2:  /* we need to do a rebalancing act. */
433
 
          A = *root;
434
 
          B = (*root)->left;
435
 
          if (B->balance <= 0)
436
 
            {
437
 
              /* Single Rotate. */
438
 
              A->left = B->right;
439
 
              B->right = A;
440
 
              *root = B;
441
 
              A->balance = 0;
442
 
              B->balance = 0;
443
 
            }
444
 
          else
445
 
            {
446
 
              /* Double Rotate. */
447
 
              *root = B->right;
448
 
              B->right = (*root)->left;
449
 
              A->left = (*root)->right;
450
 
              (*root)->left = B;
451
 
              (*root)->right = A;
452
 
              switch ((*root)->balance)
453
 
                {
454
 
                case -1:
455
 
                  A->balance = 1;
456
 
                  B->balance = 0;
457
 
                  break;
458
 
                case  0:
459
 
                  A->balance = 0;
460
 
                  B->balance = 0;
461
 
                  break;
462
 
                case  1:
463
 
                  A->balance = 0;
464
 
                  B->balance = -1;
465
 
                  break;
466
 
                }
467
 
              (*root)->balance = 0;
468
 
            }
469
 
        }     
 
439
          switch ((*root)->balance)
 
440
            {
 
441
            case  0:  /* no height increase. */
 
442
              return (FALSE);
 
443
            case -1:  /* height increase. */
 
444
              return (TRUE);
 
445
            case -2:  /* we need to do a rebalancing act. */
 
446
              A = *root;
 
447
              B = (*root)->left;
 
448
              if (B->balance <= 0)
 
449
                {
 
450
                  /* Single Rotate. */
 
451
                  A->left = B->right;
 
452
                  B->right = A;
 
453
                  *root = B;
 
454
                  A->balance = 0;
 
455
                  B->balance = 0;
 
456
                }
 
457
              else
 
458
                {
 
459
                  /* Double Rotate. */
 
460
                  *root = B->right;
 
461
                  B->right = (*root)->left;
 
462
                  A->left = (*root)->right;
 
463
                  (*root)->left = B;
 
464
                  (*root)->right = A;
 
465
                  switch ((*root)->balance)
 
466
                    {
 
467
                    case -1:
 
468
                      A->balance = 1;
 
469
                      B->balance = 0;
 
470
                      break;
 
471
                    case  0:
 
472
                      A->balance = 0;
 
473
                      B->balance = 0;
 
474
                      break;
 
475
                    case  1:
 
476
                      A->balance = 0;
 
477
                      B->balance = -1;
 
478
                      break;
 
479
                    }
 
480
                  (*root)->balance = 0;
 
481
                }
 
482
            }     
470
483
        } 
471
484
    }
472
485
  else
476
489
        {
477
490
          /* The height increased. */
478
491
          (*root)->balance ++;
 
492
 
479
493
          switch ((*root)->balance)
480
494
            {
481
495
            case 0:  /* no height increase. */
482
496
              return (FALSE);
483
497
            case 1:  /* height increase. */
484
 
              return (FALSE);
 
498
              return (TRUE);
485
499
            case 2:  /* we need to do a rebalancing act. */
486
500
              A = *root;
487
501
              B = (*root)->right;
579
593
          return (-id->a_name);
580
594
        }
581
595
      id->a_name = next_array++;
582
 
      a_names[id->a_name] = name;
583
596
      if (id->a_name < MAX_STORE)
584
597
        {
585
598
          if (id->a_name >= a_count)
586
599
            more_arrays ();
 
600
          a_names[id->a_name] = name;
587
601
          return (-id->a_name);
588
602
        }
589
603
      yyerror ("Too many array variables");
600
614
          return (id->f_name);
601
615
        }
602
616
      id->f_name = next_func++;
603
 
      f_names[id->f_name] = name;
604
617
      if (id->f_name < MAX_STORE)
605
618
        {
606
619
          if (id->f_name >= f_count)
607
620
            more_functions ();
 
621
          f_names[id->f_name] = name;
608
622
          return (id->f_name);
609
623
        }
610
624
      yyerror ("Too many functions");
617
631
          return (id->v_name);
618
632
        }
619
633
      id->v_name = next_var++;
620
 
      v_names[id->v_name - 1] = name;
621
634
      if (id->v_name <= MAX_STORE)
622
635
        {
623
636
          if (id->v_name >= v_count)
624
637
            more_variables ();
 
638
          v_names[id->v_name - 1] = name;
625
639
          return (id->v_name);
626
640
        }
627
641
      yyerror ("Too many variables");
633
647
  /* not reached */
634
648
}
635
649
 
636
 
 
637
 
/* Print the welcome banner. */
638
 
 
639
 
void 
640
 
welcome()
641
 
{
642
 
  printf ("This is free software with ABSOLUTELY NO WARRANTY.\n");
643
 
  printf ("For details type `warranty'. \n");
644
 
  checkferror_output(stdout);
645
 
}
646
 
 
647
 
/* Print out the version information. */
648
 
void
649
 
show_bc_version()
650
 
{
651
 
  printf("%s %s\n%s\n", PACKAGE, VERSION, BC_COPYRIGHT);
652
 
  checkferror_output(stdout);
653
 
}
654
 
 
655
 
 
656
 
/* Print out the warranty information. */
657
 
 
658
 
void 
659
 
warranty(prefix)
660
 
     char *prefix;
661
 
{
662
 
  printf ("\n%s", prefix);
663
 
  show_bc_version ();
664
 
  printf ("\n"
665
 
"    This program is free software; you can redistribute it and/or modify\n"
666
 
"    it under the terms of the GNU General Public License as published by\n"
667
 
"    the Free Software Foundation; either version 2 of the License , or\n"
668
 
"    (at your option) any later version.\n\n"
669
 
"    This program is distributed in the hope that it will be useful,\n"
670
 
"    but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
671
 
"    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
672
 
"    GNU General Public License for more details.\n\n"
673
 
"    You should have received a copy of the GNU General Public License\n"
674
 
"    along with this program. If not, write to\n\n"
675
 
"       The Free Software Foundation, Inc.\n"
676
 
"       59 Temple Place, Suite 330\n"
677
 
"       Boston, MA 02111, USA.\n\n");
678
 
  checkferror_output(stdout);
679
 
}
680
 
 
681
650
/* Print out the limits of this program. */
682
651
 
683
652
void
698
667
/* bc_malloc will check the return value so all other places do not
699
668
   have to do it!  SIZE is the number of bytes to allocate. */
700
669
 
701
 
char *
 
670
void *
702
671
bc_malloc (size)
703
672
     int size;
704
673
{
705
 
  char *ptr;
 
674
  void *ptr;
706
675
 
707
 
  ptr = (char *) malloc (size);
 
676
  ptr = (void *) malloc (size);
708
677
  if (ptr == NULL)
709
678
    out_of_memory ();
710
679
 
795
764
        name = "(standard_in)";
796
765
      else
797
766
        name = file_name;
798
 
      fprintf (stderr,"%s %d: ",name,line_no);
 
767
      fprintf (stderr,"%s %d: Error: ",name,line_no);
799
768
      vfprintf (stderr, mesg, args);
800
769
      fprintf (stderr, "\n");
801
770
      checkferror_output(stderr);