From 04d689072b44b9c024b0f5ad4c9811457b8715ae Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Thu, 19 Jan 2023 21:54:27 +0100 Subject: [PATCH] remove fdprintf --- calc.c | 32 ++++---- fdprintf.c | 210 ----------------------------------------------------- fdprintf.h | 15 ---- parser.c | 63 ++++++++-------- 4 files changed, 47 insertions(+), 273 deletions(-) delete mode 100644 fdprintf.c delete mode 100644 fdprintf.h diff --git a/calc.c b/calc.c index d361115..ecb01c3 100644 --- a/calc.c +++ b/calc.c @@ -1,17 +1,17 @@ /* depend: */ /* cflags: */ -/* linker: debug.o fdprintf.o parser.o -lm -lreadline */ +/* linker: debug.o parser.o -lm -lreadline */ #include #include #include +#include #include #include #include #include "debug.h" -#include "fdprintf.h" #include "parser.h" /* constants */ @@ -34,12 +34,12 @@ int precision = 6; 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; } @@ -70,7 +70,7 @@ int main (int argc, char *argv[]) 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]; @@ -82,7 +82,7 @@ int main (int argc, char *argv[]) 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); @@ -90,7 +90,7 @@ int main (int argc, char *argv[]) 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); @@ -126,7 +126,7 @@ int main (int argc, char *argv[]) /* 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) { @@ -134,7 +134,7 @@ int main (int argc, char *argv[]) } } } else { - if (read (stdfdin, buffer, BUFFER_SIZE) == 0) { + if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) { break; } nb = 0; @@ -145,7 +145,7 @@ int main (int argc, char *argv[]) line[nb++] = ++pt; } } - VERBOSE (INFO, PRINTOUT ("line: '%s'\n", buffer)); + VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer)); } /* look for end of line */ @@ -155,11 +155,11 @@ int main (int argc, char *argv[]) } 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; } diff --git a/fdprintf.c b/fdprintf.c deleted file mode 100644 index 46f27ac..0000000 --- a/fdprintf.c +++ /dev/null @@ -1,210 +0,0 @@ -/* - File name : fprintf.c - Date of creation : 05/12/2022 - Version : 1.0 - Copyright : Soft'n'design - Author : Laurent Mazet - - Description : This file contains embedded printf - - History : - - initial version -*/ - -#include -#include -#include - -#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: */ diff --git a/fdprintf.h b/fdprintf.h deleted file mode 100644 index 31369f7..0000000 --- a/fdprintf.h +++ /dev/null @@ -1,15 +0,0 @@ -#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: */ diff --git a/parser.c b/parser.c index cd0d6bf..bb0c405 100644 --- a/parser.c +++ b/parser.c @@ -3,7 +3,6 @@ #include #include "debug.h" -#include "fdprintf.h" #include "parser.h" @@ -35,7 +34,7 @@ element_t *newelement (func_t function, int nbops, int prio) { 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; @@ -118,13 +117,13 @@ element_t *parser (char *str, char **next, int prio) 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 */ @@ -136,7 +135,7 @@ element_t *parser (char *str, char **next, int prio) /* 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; @@ -172,7 +171,7 @@ element_t *parser (char *str, char **next, int prio) root->ops[0] = new; } str++; - VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n")); + VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n")); continue; } @@ -190,15 +189,15 @@ element_t *parser (char *str, char **next, int prio) 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; @@ -213,7 +212,7 @@ element_t *parser (char *str, char **next, int prio) return ERROR_OP; } found = 1; - VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n")); + VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n")); break; } } @@ -226,9 +225,9 @@ element_t *parser (char *str, char **next, int prio) 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; @@ -240,7 +239,7 @@ element_t *parser (char *str, char **next, int prio) } str += function->offset; found = 1; - VERBOSE (DEBUG, PRINTOUT ("stop processing function\n")); + VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n")); break; } } @@ -252,10 +251,10 @@ element_t *parser (char *str, char **next, int prio) 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); @@ -268,7 +267,7 @@ element_t *parser (char *str, char **next, int prio) } 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; } @@ -286,7 +285,7 @@ element_t *parser (char *str, char **next, int prio) } found = 1; } - VERBOSE (DEBUG, PRINTOUT ("stop processing value\n")); + VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n")); } /* error */ @@ -317,7 +316,7 @@ void print_element (element_t *root, int level) } for (i = 0; i < level; i++) { - PRINTOUT (" "); + fprintf (stdout, " "); } switch (root->func) { @@ -339,13 +338,13 @@ void print_element (element_t *root, int level) 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); @@ -357,7 +356,7 @@ void print_element (element_t *root, int level) void quit (void) { - PRINTOUT ("bye\n"); + fprintf (stdout, "bye\n"); exit (0); } @@ -365,13 +364,13 @@ void quit (void) 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 */ @@ -385,7 +384,7 @@ double evaluate_element (element_t *root, char mask) 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; } @@ -423,7 +422,7 @@ double evaluate_element (element_t *root, char mask) 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 */ @@ -436,7 +435,7 @@ double evaluate_element (element_t *root, char mask) 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; -- 2.30.2