sc-im: add wordexp implementation (#4689)

Issue: https://github.com/termux/termux-packages/issues/4614
This commit is contained in:
its-pointless 2019-12-23 10:00:34 +11:00 committed by Leonid Pliushch
parent a9c0090a05
commit f7290bda4f
6 changed files with 259 additions and 83 deletions

View File

@ -3,13 +3,14 @@ TERMUX_PKG_DESCRIPTION="An improved version of sc, a spreadsheet calculator"
TERMUX_PKG_LICENSE="BSD"
TERMUX_PKG_MAINTAINER="Leonid Plyushch <leonid.plyushch@gmail.com>"
TERMUX_PKG_VERSION=0.7.0
TERMUX_PKG_REVISION=4
TERMUX_PKG_REVISION=5
TERMUX_PKG_SRCURL=https://github.com/andmarti1424/sc-im/archive/v${TERMUX_PKG_VERSION}.tar.gz
TERMUX_PKG_SHA256=87225918cb6f52bbc068ee6b12eaf176c7c55ba9739b29ca08cb9b6699141cad
TERMUX_PKG_DEPENDS="libandroid-support, libzip, ncurses"
TERMUX_PKG_BUILD_IN_SRC=true
termux_step_post_configure() {
CFLAGS+=" $CPPFLAGS -I$TERMUX_PREFIX/include/libandroid-support"
CFLAGS+=" $CPPFLAGS -I$TERMUX_PREFIX/include/libandroid-support -I$TERMUX_PKG_BUILDER_DIR"
cp $TERMUX_PKG_BUILDER_DIR/wordexp.c .
cp -rf src/* .
}

View File

@ -1,34 +1,11 @@
--- cmds_command.c 2018-02-23 06:39:28.313538544 +0000
+++ src/src/cmds_command.c 2018-02-23 06:39:49.702839355 +0000
@@ -46,7 +46,6 @@
--- ../cache/sc-im-0.6.0/src/cmds_command.c 2017-04-20 00:14:03.000000000 +0000
+++ ./src/cmds_command.c 2017-11-17 23:48:43.595551651 +0000
@@ -2,7 +2,7 @@
#include <wchar.h>
#include <stdlib.h>
#include <ctype.h> // for isprint()
-#include <wordexp.h>
+#include "wordexp.h"
#include "sc.h" // for rescol
#include "conf.h"
#include "cmds_command.h"
@@ -73,6 +72,23 @@
#include "undo.h"
#endif
+#define WRDE_NOCMD 0
+
+typedef struct {
+ size_t we_wordc;
+ char **we_wordv;
+ size_t we_offs;
+} wordexp_t;
+
+static inline int wordexp(const char *c, wordexp_t *w, int _i)
+{
+ return -1;
+}
+
+static inline void wordfree(wordexp_t *__wordexp)
+{
+}
+
extern char * rev;
extern struct dictionary * user_conf_d;

View File

@ -1,34 +1,20 @@
--- file.c 2018-02-23 06:39:56.329524606 +0000
+++ src/src/file.c 2018-02-23 06:40:14.550410296 +0000
@@ -53,7 +53,6 @@
--- ./src/file.c.orig 2017-12-13 17:48:59.000000000 +0000
+++ ./src/file.c 2017-12-30 00:00:31.625092570 +0000
@@ -53,7 +53,7 @@
#include <unistd.h>
#include <wchar.h>
#include <sys/wait.h>
-#include <wordexp.h>
+#include "wordexp.h"
#include "conf.h"
#include "maps.h"
@@ -82,6 +81,23 @@
extern int pthread_exists;
#endif
+#define WRDE_NOCMD 0
+
+typedef struct {
+ size_t we_wordc;
+ char **we_wordv;
+ size_t we_offs;
+} wordexp_t;
+
+static inline int wordexp(const char *c, wordexp_t *w, int _i)
+{
+ return -1;
+}
+
+static inline void wordfree(wordexp_t *__wordexp)
+{
+}
+
/**
* \brief Erase the database (tbl, etc.)
*
@@ -829,7 +829,7 @@
(void) dup(pipefd[3]); // connect to second pipe
}
(void) signal(SIGINT, SIG_DFL); // reset
- execl("/bin/sh", "sh", "-c", efname, 0, (char *) NULL);
+ execl("@TERMUX_PREFIX@/bin/sh", "sh", "-c", efname, 0, (char *) NULL);
exit (-127);
} else { // else parent
*rpid = pid;

View File

@ -1,34 +1,11 @@
--- main.c 2018-02-23 06:41:58.248538336 +0000
+++ src/src/main.c 2018-02-23 06:42:28.433438297 +0000
@@ -60,7 +60,6 @@
--- ../cache/sc-im-0.6.0/src/main.c 2017-04-20 00:14:03.000000000 +0000
+++ ./src/main.c 2017-11-17 23:42:40.258889357 +0000
@@ -6,7 +6,7 @@
#include <fcntl.h> // for F_GETFL O_NONBLOCK F_SETFL
#include <locale.h>
#include <wchar.h>
-#include <wordexp.h>
+#include "wordexp.h"
#include <sys/ioctl.h> // for ioctl
#include "main.h"
@@ -90,6 +89,23 @@
#include "lua.h"
#endif
+#define WRDE_NOCMD 0
+
+typedef struct {
+ size_t we_wordc;
+ char **we_wordv;
+ size_t we_offs;
+} wordexp_t;
+
+static inline int wordexp(const char *c, wordexp_t *w, int _i)
+{
+ return -1;
+}
+
+static inline void wordfree(wordexp_t *__wordexp)
+{
+}
+
int currow = 0; /**< Current row of the selected cell. */
int curcol = 0; /**< Current column of the selected cell. */
int lastrow = 0;

194
packages/sc-im/wordexp.c Normal file
View File

@ -0,0 +1,194 @@
#include "wordexp.h"
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
//##include <bthread.h>
#include <sys/types.h>
static void reap(pid_t pid)
{
int status;
for (;;) {
if (waitpid(pid, &status, 0) < 0) {
if (errno != EINTR) return;
} else {
if (WIFEXITED(status)) return;
}
}
}
static char *getword(FILE *f)
{
char *s = 0;
return getdelim(&s, (size_t [1]){0}, 0, f) < 0 ? 0 : s;
}
static int do_wordexp(const char *s, wordexp_t *we, int flags)
{
size_t i, l;
int sq=0, dq=0;
size_t np=0;
char *w, **tmp;
char *redir = (flags & WRDE_SHOWERR) ? "" : "2>/dev/null";
int err = 0;
FILE *f;
size_t wc = 0;
char **wv = 0;
int p[2];
pid_t pid;
sigset_t set;
sigset_t all_set;
if (flags & WRDE_REUSE) wordfree(we);
if (flags & WRDE_NOCMD) for (i=0; s[i]; i++) switch (s[i]) {
case '\\':
if (!sq) i++;
break;
case '\'':
if (!dq) sq^=1;
break;
case '"':
if (!sq) dq^=1;
break;
case '(':
if (np) {
np++;
break;
}
case ')':
if (np) {
np--;
break;
}
case '\n':
case '|':
case '&':
case ';':
case '<':
case '>':
case '{':
case '}':
if (!(sq|dq|np)) return WRDE_BADCHAR;
break;
case '$':
if (sq) break;
if (s[i+1]=='(' && s[i+2]=='(') {
i += 2;
np += 2;
break;
} else if (s[i+1] != '(') break;
case '`':
if (sq) break;
return WRDE_CMDSUB;
}
if (flags & WRDE_APPEND) {
wc = we->we_wordc;
wv = we->we_wordv;
}
i = wc;
if (flags & WRDE_DOOFFS) {
if (we->we_offs > SIZE_MAX/sizeof(void *)/4)
goto nospace;
i += we->we_offs;
} else {
we->we_offs = 0;
}
if (pipe2(p, O_CLOEXEC) < 0) goto nospace;
// sigfillset(&all_set);
// sigprocmask(SIG_SETMASK, &all_set, &set);
pid = fork();
// sigprocmask(SIG_SETMASK, &set, &all_set);
if (pid < 0) {
close(p[0]);
close(p[1]);
goto nospace;
}
if (!pid) {
if (p[1] == 1) fcntl(1, F_SETFD, 0);
else dup2(p[1], 1);
execl("/data/data/com.termux/files/usr/bin/sh", "sh", "-c",
"eval \"printf %s\\\\\\\\0 x $1 $2\"",
"sh", s, redir, (char *)0);
_exit(1);
}
close(p[1]);
f = fdopen(p[0], "r");
if (!f) {
close(p[0]);
kill(pid, SIGKILL);
reap(pid);
goto nospace;
}
l = wv ? i+1 : 0;
free(getword(f));
if (feof(f)) {
fclose(f);
reap(pid);
return WRDE_SYNTAX;
}
while ((w = getword(f))) {
if (i+1 >= l) {
l += l/2+10;
tmp = realloc(wv, l*sizeof(char *));
if (!tmp) break;
wv = tmp;
}
wv[i++] = w;
wv[i] = 0;
}
if (!feof(f)) err = WRDE_NOSPACE;
fclose(f);
reap(pid);
if (!wv) wv = calloc(i+1, sizeof *wv);
we->we_wordv = wv;
we->we_wordc = i;
if (flags & WRDE_DOOFFS) {
if (wv) for (i=we->we_offs; i; i--)
we->we_wordv[i-1] = 0;
we->we_wordc -= we->we_offs;
}
return err;
nospace:
if (!(flags & WRDE_APPEND)) {
we->we_wordc = 0;
we->we_wordv = 0;
}
return WRDE_NOSPACE;
}
int wordexp(const char *restrict s, wordexp_t *restrict we, int flags)
{
int r, cs;
//pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
r = do_wordexp(s, we, flags);
//pthread_setcancelstate(cs, 0);
return r;
}
void wordfree(wordexp_t *we)
{
size_t i;
if (!we->we_wordv) return;
for (i=0; i<we->we_wordc; i++) free(we->we_wordv[we->we_offs+i]);
free(we->we_wordv);
we->we_wordv = 0;
we->we_wordc = 0;
}

41
packages/sc-im/wordexp.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef _WORDEXP_H
#define _WORDEXP_H
#ifdef __cplusplus
extern "C" {
#endif
#include <features.h>
#define __NEED_size_t
//#include <sys/types.h>
#define WRDE_DOOFFS 1
#define WRDE_APPEND 2
#define WRDE_NOCMD 4
#define WRDE_REUSE 8
#define WRDE_SHOWERR 16
#define WRDE_UNDEF 32
#include <sys/types.h>
typedef struct {
size_t we_wordc;
char **we_wordv;
size_t we_offs;
} wordexp_t;
#define WRDE_NOSYS -1
#define WRDE_NOSPACE 1
#define WRDE_BADCHAR 2
#define WRDE_BADVAL 3
#define WRDE_CMDSUB 4
#define WRDE_SYNTAX 5
int wordexp (const char *__restrict, wordexp_t *__restrict, int);
void wordfree (wordexp_t *);
#ifdef __cplusplus
}
#endif
#endif