[PATCH 47] Refactored ft_getnames() and used the new function in

com_let().
This commit is contained in:
Holger Vogt 2019-05-26 18:32:55 +02:00
parent 016c558d77
commit 018025df1e
4 changed files with 32 additions and 26 deletions

View File

@ -17,7 +17,6 @@ com_let(wordlist *wl)
char *p, *q, *s;
int indices[MAXDIMS];
int numdims;
wordlist fake_wl;
int need_open;
int offset, length;
struct pnode *names;
@ -27,7 +26,6 @@ com_let(wordlist *wl)
int newvec;
char *rhs;
fake_wl.wl_next = NULL;
if (!wl) {
com_display(NULL);
@ -80,8 +78,7 @@ com_let(wordlist *wl)
/* evaluate expression between s and q */
/* va, indexing */
fake_wl.wl_word = s;
names = ft_getpnames(&fake_wl, TRUE);
names = ft_getpnames_from_string(s, TRUE);
if (!names) {
/* XXX error message */
tfree(p);
@ -130,10 +127,9 @@ com_let(wordlist *wl)
}
/* evaluate rhs */
fake_wl.wl_word = rhs;
names = ft_getpnames(&fake_wl, TRUE);
names = ft_getpnames_from_string(rhs, TRUE);
if (names == NULL) {
/* XXX error message */
fprintf(cp_err, "Error: RHS \"%s\" invalid\n", rhs);
tfree(p);
return;
}

View File

@ -812,7 +812,7 @@ op_ind(struct pnode *arg1, struct pnode *arg2)
/* Apply a function to an argument. Complex functions are called as follows:
* cx_something(data, type, length, &newlength, &newtype),
* and returns a char * that is cast to complex or double.
* and returns a void * that is cast to complex or double.
*/
static void *

View File

@ -30,26 +30,16 @@ void db_print_pnode_tree(struct pnode *p, char *print);
struct pnode *
ft_getpnames(wordlist *wl, bool check)
ft_getpnames_from_string(const char *sz, bool check)
{
struct pnode *pn = NULL;
char *xsbuf, *sbuf;
int rv;
struct pnode *pn;
if (!wl) {
fprintf(cp_err, "Warning: NULL arithmetic expression\n");
return (NULL);
/* The first argument to PPparse is not const char **, but it does not
* appear to modify the string that is being parsed */
if (PPparse((char **) &sz, &pn) != 0) {
return (struct pnode *) NULL;
}
xsbuf = sbuf = wl_flatten(wl);
rv = PPparse(&sbuf, &pn);
tfree(xsbuf);
if (rv)
return (NULL);
/* If validation is requested, do it and return NULL on failure. The
* structure must also be freed if the check fails since it is not
* being returned. */
@ -58,7 +48,26 @@ ft_getpnames(wordlist *wl, bool check)
return (struct pnode *) NULL;
}
return (pn);
return pn;
} /* end of function ft_getpnames_from_string */
struct pnode *
ft_getpnames(const wordlist *wl, bool check)
{
/* Validate input */
if (!wl) {
(void) fprintf(cp_err, "Warning: NULL arithmetic expression\n");
return (struct pnode *) NULL;
}
/* Convert the list to a string then parse the string */
const char * const sz = wl_flatten(wl);
struct pnode * const pn = ft_getpnames_from_string(sz, check);
txfree((void *) sz);
return pn; /* Return the parsed result */
} /* end of function ft_getpnames */

View File

@ -268,7 +268,8 @@ extern bool ft_stricterror;
extern struct func ft_funcs[];
extern struct func func_not;
extern struct func func_uminus;
extern struct pnode *ft_getpnames(wordlist *wl, bool check);
extern struct pnode *ft_getpnames(const wordlist *wl, bool check);
struct pnode *ft_getpnames_from_string(const char *sz, bool check);
extern struct pnode *alloc_pnode(void);
#define free_pnode(ptr) \
do { \