home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / libsrc87 / scanf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  11.5 KB  |  550 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2.    This file is part of the GNU C Library.
  3.    
  4.    The GNU C Library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public License as
  6.    published by the Free Software Foundation; either version 2 of the
  7.    License, or (at your option) any later version.
  8.    
  9.    The GNU C Library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU Library General Public
  15.    License along with the GNU C Library; see the file COPYING.LIB.  If
  16.    not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17.    Cambridge, MA 02139, USA.  */
  18.  
  19. /* 
  20.  * adapted for atariST gcc lib
  21.  *
  22.  * NOTES: interface is different from equivalent gnuC lib function
  23.  *        it was munged to match our old _scanf().
  24.  *
  25.  *      if __NO_FLOAT__ is defined then the floating point stuff
  26.  *      gets nuked (for iio*.olb) as per our old scanf.c.
  27.  *
  28.  *  It is very important to read and understand the GNU Library General 
  29.  *  Public License. It specifies rights and conditions that are different
  30.  *  from the GNU copyleft.
  31.  *
  32.  *    ++jrb bammi@cadence.com
  33.  */
  34.  
  35. #include <errno.h>
  36. #include <limits.h>
  37. #include <ctype.h>
  38. #include <stdarg.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42.  
  43. /* the code assumes this definition, note: traditional <ctype> def
  44.  * of tolower will break the code. Ansi def should be ok.
  45.  */
  46. #ifdef tolower
  47. #  undef tolower
  48. #  define    tolower(c)    (isupper(c) ? (c)^0x20 : (c))
  49. #endif
  50.  
  51. #ifndef __NO_FLOAT__
  52. #  define FLOATS 1
  53. #else
  54. #  define FLOATS 0
  55. #endif
  56.  
  57. #define TEN_MUL(X)    ((((X) << 2) + (X)) << 1)
  58.  
  59. #ifdef    __GNUC__
  60. #define    HAVE_LONGLONG
  61. #define    LONGLONG    long long
  62. #else
  63. #define    LONGLONG    long
  64. #endif
  65.  
  66.  
  67. #define    inchar()    ((c = ((*get)(s))) == EOF ? EOF : (++read_in, c))
  68. #define unchar(c)    (--read_in, (*unget)(c, s))
  69. #define    conv_error()    return((c == EOF || ((*unget)(c, s))), done)
  70. #define input_error()    return( done < 1 ? EOF : done )
  71. #define    memory_error()    return((errno = ENOMEM), EOF)
  72.  
  73.  
  74. /* Read formatted input from S according to the format string
  75.    FORMAT, using the argument list in ARG.
  76.    Return the number of assignments made, or -1 for an input error.  */
  77. int _scanf(s, get, unget, format, arg)
  78. register FILE *s;
  79. int (*get) __PROTO((FILE *));
  80. int (*unget) __PROTO((int, FILE *));
  81. const char *format;
  82. va_list arg;
  83. {
  84.     register const char *f = format;
  85.     register char fc;        /* Current character of the format.  */
  86.     register size_t done = 0;    /* Assignments done.  */
  87.     register size_t read_in = 0;    /* Chars read in.  */
  88.     register int c;        /* Last char read.  */
  89.     register int do_assign;    /* Whether to do an assignment.  */
  90.     register int width;        /* Maximum field width.  */
  91.     
  92.     /* Type modifiers.  */
  93.     char is_short, is_long, is_long_double;
  94. #ifdef    HAVE_LONGLONG
  95.     /* We use the `L' modifier for `long long int'.  */
  96. #define    is_longlong    is_long_double
  97. #else
  98. #define    is_longlong    0
  99. #endif
  100. #if FLOATS
  101.     /* Status for reading F-P nums.  */
  102.     char got_dot, got_e;
  103. #endif
  104.     /* If a [...] is a [^...].  */
  105.     char not_in;
  106.     /* Base for integral numbers.  */
  107.     int base;
  108.     /* Integral holding variables.  */
  109.     long int num;
  110.     unsigned long int unum;
  111. #if FLOATS
  112.     /* Floating-point holding variable.  */
  113.     long double fp_num;
  114. #endif
  115.     /* Character-buffer pointer.  */
  116.     register char *str;
  117.     /* Workspace.  */
  118.     char work[256];
  119.     char *w;        /* Pointer into WORK.  */
  120.     
  121.     if ((format == NULL) || (!*format))
  122.     {
  123.     errno = EINVAL;
  124.     input_error();
  125.     }
  126.     
  127. # define decimal ('.')    /* should really come from locale stuff that we dont */
  128.     /* have as yet */
  129.     c = inchar();
  130.     
  131.     /* Run through the format string.  */
  132.     while (*f != '\0')
  133.     {
  134. #if 0 /* no mb support as yet */
  135.     if (!isascii(*f))
  136.     {
  137.         /* Non-ASCII, may be a multibyte.  */
  138.         int len = mblen(f, strlen(f));
  139.         if (len > 0)
  140.         {
  141.         while (len-- > 0)
  142.             if (c == EOF)
  143.             input_error();
  144.             else if (c == *f++)
  145.             (void) inchar();
  146.             else
  147.             conv_error();
  148.         continue;
  149.         }
  150.         }
  151. #endif      
  152.     
  153.     fc = *f++;
  154.     if (fc != '%')
  155.     {
  156.         /* Characters other than format specs must just match.  */
  157.         if (c == EOF)
  158.         input_error();
  159.         if (isspace(fc))
  160.         {
  161.         /* Whitespace characters match any amount of whitespace.  */
  162.         while (isspace (c))
  163.             (void)inchar ();
  164.         continue;
  165.         }
  166.         else if (c == fc)
  167.         (void) inchar();
  168.         else
  169.         conv_error();
  170.         continue;
  171.     }
  172.     
  173.     /* Check for the assignment-suppressant.  */
  174.     if (*f == '*')
  175.     {
  176.         do_assign = 0;
  177.         ++f;
  178.     }
  179.     else
  180.         do_assign = 1;
  181.     
  182.     /* Find the maximum field width.  */
  183.     width = 0;
  184.     while (isdigit(*f))
  185.     {
  186.         width *= 10;
  187.         width += *f++ - '0';
  188.     }
  189.     if (width == 0)
  190.         width = -1;
  191.     
  192.     /* Check for type modifiers.  */
  193.     is_short = is_long = is_long_double = 0;
  194.     while (*f == 'h' || *f == 'l' || *f == 'L')
  195.         switch (*f++)
  196.         {
  197.           case 'h':
  198.         /* int's are short int's.  */
  199.         is_short = 1;
  200.         break;
  201.           case 'l':
  202. #ifdef HAVE_LONGLONG
  203.         if (is_long)
  204.             /* A double `l' is equivalent to an `L'.  */
  205.             is_longlong = 1;
  206.         else
  207. #endif
  208.             /* int's are long int's.  */
  209.             is_long = 1;
  210.         break;
  211.           case 'L':
  212.         /* double's are long double's, and int's are long long int's.  */
  213.         is_long_double = 1;
  214.         break;
  215.         }
  216.     
  217.     /* End of the format string?  */
  218.     if (*f == '\0')
  219.         conv_error();
  220.     
  221.     /* Find the conversion specifier.  */
  222.     w = work;
  223.     fc = *f++;
  224.     if (fc != '[' && fc != 'c' && fc != 'n')
  225.         /* Eat whitespace.  */
  226.         while (isspace(c))
  227.         (void) inchar();
  228.     switch (fc)
  229.     {
  230.       case '%':    /* Must match a literal '%'.  */
  231.         if (c != fc)
  232.         conv_error();
  233.         else
  234.         c = inchar();
  235.         break;
  236.         
  237.       case 'n':    /* Answer number of assignments done.  */
  238.         if (do_assign)
  239.         *va_arg(arg, int *) = read_in - 1; /* -1 is debatable ++jrb */
  240.         break;
  241.         
  242.       case 'c':    /* Match characters.  */
  243.         if (do_assign)
  244.         {
  245.         str = va_arg(arg, char *);
  246.         if (str == NULL)
  247.             conv_error();
  248.         }
  249.         
  250.         if (c == EOF)
  251.         input_error();
  252.         
  253.         if (width == -1)
  254.         width = 1;
  255. /* mjr:    */        
  256.         if (do_assign) {
  257.         do
  258.             *str++ = c;
  259.         while (inchar() != EOF && --width > 0);
  260.         } else
  261.         while (inchar() != EOF && --width > 0)
  262.             ;
  263.  
  264.         if (do_assign)
  265.         ++done;
  266.         
  267.         if (c == EOF)
  268.         input_error();
  269.         break;
  270.         
  271.       case 's':    /* Read a string.  */
  272.         if (do_assign)
  273.         {
  274.         str = va_arg(arg, char *);
  275.         if (str == NULL)
  276.             conv_error();
  277.         }
  278.         
  279.         if (c == EOF)
  280.         input_error();
  281.         
  282.         do
  283.         {
  284.         if (isspace(c))
  285.             break;
  286.         if (do_assign)
  287.             *str++ = c;
  288.         if (width > 0 && --width == 0)
  289.             break;
  290.         } while (inchar() != EOF);
  291.         
  292.         if (do_assign)
  293.         {
  294.         *str = '\0';
  295.         ++done;
  296.         }
  297.         
  298.         if (c == EOF)
  299.         input_error();
  300.         break;
  301.         
  302.       case 'x':    /* Hexadecimal integer.  */
  303.       case 'X':    /* Ditto.  */ 
  304.         base = 16;
  305.         goto number;
  306.         
  307.       case 'o':    /* Octal integer.  */
  308.         base = 8;
  309.         goto number;
  310.         
  311.       case 'u':    /* Decimal integer.  */
  312.       case 'd':    /* Ditto.  */
  313.         base = 10;
  314.         goto number;
  315.         
  316.       case 'i':    /* Generic number.  */
  317.         base = 0;
  318.         
  319.       number:;
  320.         if (c == EOF)
  321.         input_error();
  322.         
  323.         /* Check for a sign.  */
  324.         if (c == '-' || c == '+')
  325.         {
  326.         *w++ = c;
  327.         if (width > 0)
  328.             --width;
  329.         (void) inchar();
  330.         }
  331.         
  332.         /* Look for a leading indication of base.  */
  333.         if (c == '0')
  334.         {
  335.         if (width > 0)
  336.             --width;
  337.         *w++ = '0';
  338.         
  339.         (void) inchar();
  340.         
  341.         if (tolower(c) == 'x')
  342.         {
  343.             /* one char look ahead to see if its really a lead ind */
  344.             int savec = c;
  345.             int peekc = inchar();
  346.             c = savec;
  347.             (void)unchar(peekc);
  348.             if(isxdigit(peekc))
  349.                     {  
  350.             if (base == 0)
  351.                 base = 16;
  352.             if (base == 16)
  353.             {
  354.                 if (width > 0)
  355.                 --width;
  356.                 (void) inchar();
  357.             }
  358.             }
  359.         }
  360.         else if (base == 0)
  361.             if((c >= '0') && (c <= '7')) 
  362.             base = 8;
  363.         }
  364.         
  365.         if (base == 0)
  366.         base = 10;
  367.         
  368.         /* Read the number into WORK.  */
  369.         do
  370.         {
  371.         if (base == 16 ? !isxdigit(c) :
  372.             (!isdigit(c) || ((c - '0') >= base)))
  373.             break;
  374.         *w++ = c;
  375.         if (width > 0)
  376.             --width;
  377.         } while (inchar() != EOF && width != 0);
  378.         
  379.         if (w == work ||
  380.         (w - work == 1 && (work[0] == '+' || work[0] == '-')))
  381.         /* There was no number.  */
  382.         conv_error();
  383.         
  384.         /* Convert the number.  */
  385.         *w = '\0';
  386.         num = strtol(work, &w, base);
  387.         if (w == work)
  388.         conv_error();
  389.         
  390.         if (do_assign)
  391.         {
  392.         if (is_longlong)
  393.             *va_arg(arg, LONGLONG int *) = num;
  394.         else if (is_long)
  395.             *va_arg(arg, long int *) = num;
  396.         else if (is_short)
  397.             *va_arg(arg, short int *) = (short int) num;
  398.         else
  399.             *va_arg(arg, int *) = (int) num;
  400.         ++done;
  401.         }
  402.         break;
  403.         
  404. #if FLOATS
  405.       case 'e':    /* Floating-point numbers.  */
  406.       case 'E':
  407.       case 'f':
  408.       case 'g':
  409.       case 'G':
  410.         if (c == EOF)
  411.         input_error();
  412.         
  413.         /* Check for a sign.  */
  414.         if (c == '-' || c == '+')
  415.         {
  416.         *w++ = c;
  417.         if (inchar() == EOF)
  418.             input_error();
  419.         if (width > 0)
  420.             --width;
  421.         }
  422.         
  423.         got_dot = got_e = 0;
  424.         do
  425.         {
  426.         if (isdigit(c))
  427.             *w++ = c;
  428.         else if (got_e && w[-1] == 'e' && (c == '-' || c == '+'))
  429.             *w++ = c;
  430.         else if (!got_e && tolower(c) == 'e')
  431.         {
  432.             *w++ = 'e';
  433.             got_e = got_dot = 1;
  434.         }
  435.         else if (c == decimal && !got_dot)
  436.         {
  437.             *w++ = c;
  438.             got_dot = 1;
  439.         }
  440.         else
  441.             break;
  442.         if (width > 0)
  443.             --width;
  444.         } while (inchar() != EOF && width != 0);
  445.         
  446.         if (w == work)
  447.         conv_error();
  448. #if 0
  449.         if (w[-1] == '-' || w[-1] == '+' || w[-1] == 'e')
  450.         conv_error();
  451. #else
  452.         if (w[-1] == '-' || w[-1] == '+')
  453.         conv_error();
  454.  
  455.         if(tolower(w[-1]) == 'e')
  456.         {
  457.         unchar(c);
  458.             --w;
  459.             c = *w;
  460.         }
  461. #endif        
  462.         /* Convert the number.  */
  463.         *w = '\0';
  464.         fp_num = strtod(work, &w);
  465.         if (w == work)
  466.         conv_error();
  467.         
  468.         if (do_assign)
  469.         {
  470.         if (is_long_double)
  471.             *va_arg(arg, long double *) = fp_num;
  472.         else if (is_long)
  473.             *va_arg(arg, double *) = (double) fp_num;
  474.         else
  475.             *va_arg(arg, float *) = (float) fp_num;
  476.         ++done;
  477.         }
  478.         break;
  479. #endif /* FLOATS */
  480.         
  481.       case '[':    /* Character class.  */
  482.         if (do_assign)
  483.         {
  484.         str = va_arg(arg, char *);
  485.         if (str == NULL)
  486.             conv_error();
  487.         }
  488.         
  489.         if (c == EOF)
  490.         input_error();
  491.         
  492.         if (*f == '^')
  493.         {
  494.         ++f;
  495.         not_in = 1;
  496.         }
  497.         else
  498.         not_in = 0;
  499.         w = (char *)f;        /* remember start of class */
  500.         bzero (work, 256);
  501.         while ((fc = *f++) != '\0' && (fc != ']' || f - 1 == w))
  502.         {
  503.         /* Add the character to the list.  */
  504.         work[(unsigned char)fc] = 1;
  505.         /* Look ahead for a range.  */
  506.         if (f[0] == '-' && f[1] != '\0' && f[1] != ']')
  507.           {
  508.             /* Add all characters from the one before the '-'
  509.                up to the next format char.  */
  510.             unsigned char end = f[1];
  511.             while ((unsigned char)++fc <= end)
  512.             work[(unsigned char)fc] = 1;
  513.             f += 2;
  514.           }
  515.         }
  516.         if (fc == '\0')
  517.         conv_error();
  518.         
  519.         work[0] = not_in;
  520.         unum = read_in;
  521.         do
  522.         {
  523.         if (work[(unsigned char)c] == not_in)
  524.             break;
  525.         if (do_assign)
  526.             *str++ = c;
  527.         if (width > 0)
  528.             --width;
  529.         } while (inchar() != EOF && width != 0);
  530.         if (read_in == unum)
  531.         conv_error();
  532.         
  533.         if (do_assign)
  534.         {
  535.         *str = '\0';
  536.         ++done;
  537.         }
  538.         break;
  539.         
  540.       case 'p':    /* Generic pointer.  */
  541.         base = 16;
  542.         /* A PTR must be the same size as a `long int'.  */
  543.         is_long = 1;
  544.         goto number;
  545.     }
  546.     }
  547.     
  548.     conv_error();
  549. }
  550.