1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 * This file is part of Mes.
7 * Mes is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or (at
10 * your option) any later version.
12 * Mes is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Mes. If not, see <http://www.gnu.org/licenses/>.
29 return c >= '0' && c <= '9';
35 return isdigit (c) || c >= 'a' && c <= 'f';
41 return (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || c == ' ');
45 isnumber (int c, int base)
48 return (c >= '0') && (c <= '1');
50 return (c >= '0') && (c <= '7');
58 abtoi (char const **p, int base)
69 while (isnumber (*s, base))
72 int m = *s > '9' ? 'a' - 10 : '0';
88 number_to_ascii (int x, int base, int signed_p)
90 static char itoa_buf[12];
91 char *p = itoa_buf + 11;
96 if (signed_p && x < 0)
105 *p-- = i > 9 ? 'a' + i - 10 : '0' + i;
109 if (sign && *(p + 1) != '0')
116 itoab (int x, int base)
118 return number_to_ascii (x, base, 1);
124 return number_to_ascii (x, 10, 1);
130 return number_to_ascii (x, 10, 0);
133 int _ungetc_pos = -1;
135 char _ungetc_buf[10];
142 if (_ungetc_pos == -1)
144 int r = read (fd, &c, 1);
151 i = _ungetc_buf[_ungetc_pos];
152 if (_ungetc_fd != fd && i == 10)
154 // FIXME: Nyacc's ungetc exposes harmless libmec.c bug
155 // we need one unget position per FD
160 else if (_ungetc_fd != fd)
162 eputs (" ***MES LIB C*** fdgetc ungetc conflict unget-fd=");
163 eputs (itoa (_ungetc_fd));
164 eputs (", fdgetc-fd=");
167 eputs (itoa ( _ungetc_buf[_ungetc_pos]));
171 i = _ungetc_buf[_ungetc_pos];
173 if (_ungetc_pos == -1)
183 fdputc (int c, int fd)
185 write (fd, (char*)&c, 1);
190 fdputs (char const* s, int fd)
198 fdungetc (int c, int fd)
202 if (_ungetc_pos == -1)
204 else if (_ungetc_fd != fd)
206 eputs (" ***MES LIB C*** fdungetc ungetc conflict unget-fd=");
207 eputs (itoa (_ungetc_fd));
208 eputs (", fdungetc-fd=");
214 _ungetc_buf[_ungetc_pos] = c;
221 return _ungetc_pos > -1;
224 #if POSIX || __x86_64__
227 eputs (char const* s)
230 write (STDERR, s, i);
238 return fdputc (c, STDERR);