/* depend: */
/* cflags: */
-/* linker: debug.o fdprintf.o parser.o -lm -lreadline */
+/* linker: debug.o parser.o -lm -lreadline */
#include <malloc.h>
#include <stddef.h>
#include <stdio.h>
+#include <stdlib.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "debug.h"
-#include "fdprintf.h"
#include "parser.h"
/* constants */
int usage (int ret)
{
- int fd = ret ? stdfderr : stdfdout;
- fdprintf (fd, "usage: %s\n", progname);
- fdprintf (fd, " -h : help message\n");
- fdprintf (fd, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
- fdprintf (fd, " -p : precision (%d)\n", precision);
- fdprintf (fd, " -v : verbose level (%d)\n", verbose);
+ FILE *fid = ret ? stderr : stdout;
+ fprintf (fid, "usage: %s\n", progname);
+ fprintf (fid, " -h : help message\n");
+ fprintf (fid, " -n : no readline mode (%s)\n", mode ? "yes" : "no");
+ fprintf (fid, " -p : precision (%d)\n", precision);
+ fprintf (fid, " -v : verbose level (%d)\n", verbose);
return ret;
}
while (argc-- > 1) {
char *arg = *(++argv);
if (arg[0] != '-') {
- PRINTERR ("%s: invalid option -- %s\n", progname, arg);
+ fprintf (stderr, "%s: invalid option -- %s\n", progname, arg);
return usage (1);
}
char c = arg[1];
case 'p':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg == NULL) {
- PRINTERR ("%s: missing precision\n", progname);
+ fprintf (stderr, "%s: missing precision\n", progname);
return usage (1);
}
precision = atoi (arg);
case 'v':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg == NULL) {
- PRINTERR ("%s: missing verbose level\n", progname);
+ fprintf (stderr, "%s: missing verbose level\n", progname);
return usage (1);
}
verbose = atoi (arg);
/* add line into history */
add_history (buffer);
- VERBOSE (INFO, PRINTOUT ("line (%d): '%s'\n", where_history (), buffer));
+ VERBOSE (INFO, fprintf (stdout, "line (%d): '%s'\n", where_history (), buffer));
if (where_history () == 10) {
HIST_ENTRY *last = remove_history (0);
if (last) {
}
}
} else {
- if (read (stdfdin, buffer, BUFFER_SIZE) == 0) {
+ if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) {
break;
}
nb = 0;
line[nb++] = ++pt;
}
}
- VERBOSE (INFO, PRINTOUT ("line: '%s'\n", buffer));
+ VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer));
}
/* look for end of line */
}
element_t *element = parser (line[i], NULL, 0);
if (element == ERROR_OP) {
- VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", line[i]));
+ VERBOSE (WARNING, fprintf (stdout, "error while parsing: %s\n", line[i]));
ret = 1;
} else {
VERBOSE (INFO, print_element (element, 0));
- PRINTOUT (format, evaluate_element (element, 0));
+ fprintf (stdout, format, evaluate_element (element, 0));
delelement (element);
ret = 0;
}
+++ /dev/null
-/*
- File name : fprintf.c
- Date of creation : 05/12/2022
- Version : 1.0
- Copyright : Soft'n'design
- Author : Laurent Mazet <mazet@softndesign.org>
-
- Description : This file contains embedded printf
-
- History :
- - initial version
-*/
-
-#include <stdarg.h>
-#include <stdint.h>
-#include <unistd.h>
-
-#include "fdprintf.h"
-
-int stdfdin = STDIN_FILENO;
-int stdfdout = STDOUT_FILENO;
-int stdfderr = STDERR_FILENO;
-
-unsigned int nextpow (unsigned int x, int base) {
- unsigned int n = 0;
- while (x) {
- n++;
- x = x / base;
- }
- return (n == 0) ? 1 : n;
-}
-
-char *itoa (char *str, unsigned u, unsigned int sz)
-{
- unsigned int i = (sz == 0 ) ? nextpow (u, 10) : sz;
- char *s = str;
- while (i > 0) {
- str[i - 1] = '0' + (u % 10);
- u /= 10;
- i--;
- s++;
- }
- return s;
-}
-
-double tenpower(int n)
-{
- double t = 1.0;
- int i;
- for (i = 0; i < n; i++) {
- t *= 10;
- }
- for (i = 0; i > n; i--) {
- t /= 10;
- }
- return t;
-}
-
-int getexponant (double *f, int maxexp)
-{
- int exp = 0;
- while (*f > 10) {
- *f /= 10;
- exp++;
- }
- while (*f < 1) {
- *f *= 10;
- exp--;
- }
- *f += tenpower (maxexp - 1);
- if (*f >= 10) {
- *f /= 10;
- exp++;
- }
-
- return exp;
-}
-/* simple fprintf function */
-
-int fdprintf (int fd, const char *fmt, ...)
-{
- char buffer[1024 + 1] = { 0 };
- char *str = buffer;
-
- va_list ap;
- va_start (ap, fmt);
- while (*fmt) {
- char *s;
- double f = 0.0;
- int d = 0;
- unsigned int u;
- char c = *fmt++;
-
- /* copy standard char */
- if (c != '%') {
- *str++ = c;
- } else {
- int t = 0;
- char w = '0';
- int i, sz = 0;
- void *p = NULL;
-
- /* stamp */
- if ((*fmt == ' ') || (*fmt == '0')) {
- w = *fmt++;
- }
-
- /* size */
- if ((*fmt >= '1') && (*fmt <= '9')) {
- sz = *fmt++ - '0';
- }
-
- /* process format char */
- switch (*fmt++) {
- case '%': /* percent */
- *str++ = '%';
- break;
- case 'c': /* char */
- c = (char) va_arg (ap, int);
- *str++ = c;
- break;
- case 'd': /* int */
- d = va_arg (ap, int);
- if (d < 0) {
- *str++ = '-';
- d = -d;
- }
- t = 1;
- /* fall through */
- case 'u': /* unsigned int */
- str = itoa (str, (t) ? (unsigned int)d : va_arg (ap, unsigned int), 0);
- break;
- case 'f': /* float */
- f = va_arg (ap, double);
- if (f == 0) {
- *str++ = '0';
- break;
- }
- if (f < 0) {
- *str++ = '-';
- f = -f;
- }
- if (sz == 0) sz = 6;
- t = getexponant (&f, -sz);
- u = (int)f;
- str = itoa (str, u, 0);
- d = (int)((f - u) * tenpower (sz));
- if (d > 0) {
- *str++ = '.';
- str = itoa (str, d, sz);
- }
- while (*(str - 1) == '0') {
- str--;
- }
- if (t != 0) {
- *str++ = 'e';
- if (t < 0) {
- *str++ = '-';
- t = -t;
- }
- str = itoa (str, t, 0);
- }
- break;
- case 'p': /* pointer */
- *str++ = '0';
- *str++ = 'x';
- w = '0';
- sz = sizeof (void *) * 2;
- p = va_arg (ap, void *);
- /* fall through */
- case 'x': /* integer hexa */
- if (!p) {
- u = va_arg (ap, unsigned int);
- if (sz == 0) {
- sz = nextpow (u, 16);
- }
- } else {
- u = (uintptr_t)p;
- }
- for (i = sz, t = 1; i > 0; i--) {
- char x = (char)((u >> (i * 4 - 4)) & 0xf);
- if ((t == 1) && (x == 0)) {
- *str++ = w;
- } else {
- *str++ = (x > 9) ? 'a' + x - 10 : '0' + x;
- t = 0;
- }
- }
- break;
- case 's': /* string */
- s = va_arg (ap, char *);
- while (s && *s)
- *str++ = *s++;
- break;
- default:
- *str++ = '?';
- }
- }
- }
- va_end (ap);
-
- /* output string */
- int n = str - buffer;
- if (n < (int)sizeof (buffer) - 1) {
- return write (fd, buffer, n);
- }
- return 0;
-}
-
-/* vim: set ts=4 sw=4 et: */
+++ /dev/null
-#ifndef __FDPRINTF_H__
-#define __FDPRINTF_H__
-
-int fdprintf (int fd, const char *fmt, ...);
-
-extern int stdfdin;
-extern int stdfdout;
-extern int stdfderr;
-
-#define PRINTOUT(fmt...) fdprintf (stdfdout, fmt)
-#define PRINTERR(fmt...) fdprintf (stdfderr, fmt)
-
-#endif /* __FDPRINTF_H__ */
-
-/* vim: set ts=4 sw=4 et: */
#include <stdlib.h>
#include "debug.h"
-#include "fdprintf.h"
#include "parser.h"
{
element_t *new = (element_t *) calloc (1, sizeof (element_t));
if (new == NULL) {
- VERBOSE (ERROR, fdprintf (stdfderr, "can't allocate memory\n"));
+ VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
return NULL;
}
new->func = function;
element_t *root = NULL;
int i;
- VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n"));
/* main loop */
while (*str != '\0') {
int found = 0;
element_t *new = NULL;
- VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str));
+ VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
/* skip spaces and tabs */
/* check for open bracket */
if (*str == '(') {
- VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "start processing bracket\n"));
if (root) {
do {
found = 0;
root->ops[0] = new;
}
str++;
- VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
continue;
}
for (i = 0; i < NB_OPERATORS; i++) {
keyword_t *operator = operators + i;
if (codecmp (operator->keyword, str) == 0) {
- VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
if (root) {
if ((prio) && (prio > operator->prio)) {
- VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
*next = str;
return root;
}
str += operator->offset;
- VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
+ VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
delelement (root);
return ERROR_OP;
return ERROR_OP;
}
found = 1;
- VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
break;
}
}
for (i = 0; i < NB_FUNCTIONS; i++) {
keyword_t *function = functions + i;
if (codecmp (function->keyword, str) == 0) {
- VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
if (root == NULL) {
- VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
+ VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
new = newelement (function->func, function->nbops, function->prio);
if (new == NULL) {
return ERROR_OP;
}
str += function->offset;
found = 1;
- VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
break;
}
}
if (((*str >= '0') && (*str <= '9')) ||
(*str == '.') || (*str == '+') || (*str == '-')) {
- VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
char *pt;
double value = strtod (str, &pt);
- VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
+ VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
if (str != pt) {
if (root == NULL) {
new = newelement (Val, 1, 5);
} else if (root->func == Val) {
if ((*str == '+') || (*str == '-')) {
if ((prio) && (prio > 1)) {
- VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
*next = str;
return root;
}
}
found = 1;
}
- VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
}
/* error */
}
for (i = 0; i < level; i++) {
- PRINTOUT (" ");
+ fprintf (stdout, " ");
}
switch (root->func) {
case Hel: func = "Help"; break;
}
- PRINTOUT ("Function: %s\n", func);
+ fprintf (stdout, "Function: %s\n", func);
if ((root->func == Val) && (root->ops[0] == NULL)) {
for (i = 0; i < level; i++) {
- PRINTOUT (" ");
+ fprintf (stdout, " ");
}
- PRINTOUT ("value: %f\n", root->value);
+ fprintf (stdout, "value: %f\n", root->value);
} else {
for (i = 0; i < root->nbops; i++) {
print_element (root->ops[i], level + 1);
void quit (void)
{
- PRINTOUT ("bye\n");
+ fprintf (stdout, "bye\n");
exit (0);
}
void help (void)
{
- PRINTOUT ("calc is a simple calculator\n\n");
- PRINTOUT ("supported operators:\n");
- PRINTOUT (" + - * / %% ^\n\n");
- PRINTOUT ("supported functions:\n");
- PRINTOUT (" pow sqrt cos sin atan log exp\n\n");
- PRINTOUT ("miscellaneous functions:\n");
- PRINTOUT (" quit help\n");
+ fprintf (stdout, "calc is a simple calculator\n\n");
+ fprintf (stdout, "supported operators:\n");
+ fprintf (stdout, " + - * / %% ^\n\n");
+ fprintf (stdout, "supported functions:\n");
+ fprintf (stdout, " pow sqrt cos sin atan log exp\n\n");
+ fprintf (stdout, "miscellaneous functions:\n");
+ fprintf (stdout, " quit help\n");
}
/* evaluate element tree */
char nextmask = mask;
if ((root == NULL) || (root == ERROR_OP)) {
- VERBOSE (WARNING, PRINTOUT ("error while evaluating\n"));
+ VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
return 0;
}
if (root->ops[1]) {
op1 = evaluate_element (root->ops[1], nextmask);
} else {
- VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[1])\n"));
+ VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
return 0;
}
/* fallthrough */
if (root->ops[0]) {
op0 = evaluate_element (root->ops[0], 0);
} else {
- VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[0])\n"));
+ VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
return 0;
}
break;