summaryrefslogtreecommitdiff
path: root/Src/Modules
diff options
context:
space:
mode:
Diffstat (limited to 'Src/Modules')
-rw-r--r--Src/Modules/datetime.c73
-rw-r--r--Src/Modules/datetime.mdd2
-rw-r--r--Src/Modules/db_gdbm.c10
-rw-r--r--Src/Modules/pcre.c60
-rw-r--r--Src/Modules/pcre.mdd2
-rw-r--r--Src/Modules/zftp.c2
6 files changed, 124 insertions, 25 deletions
diff --git a/Src/Modules/datetime.c b/Src/Modules/datetime.c
index 45818b968..a4e7eca86 100644
--- a/Src/Modules/datetime.c
+++ b/Src/Modules/datetime.c
@@ -151,6 +151,69 @@ getcurrentsecs(UNUSED(Param pm))
return (zlong) time(NULL);
}
+static double
+getcurrentrealtime(Param pm)
+{
+#ifdef HAVE_CLOCK_GETTIME
+ struct timespec now;
+
+ if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
+ zwarn("%s: unable to retrieve time: %e", pm->node.nam, errno);
+ return (double)0.0;
+ }
+
+ return (double)now.tv_sec + (double)now.tv_nsec * 1e-9;
+#else
+ struct timeval now;
+ struct timezone dummy_tz;
+
+ (void)pm;
+ gettimeofday(&now, &dummy_tz);
+
+ return (double)now.tv_sec + (double)now.tv_usec * 1e-6;
+#endif
+}
+
+static char **
+getcurrenttime(Param pm)
+{
+ char **arr;
+ char buf[DIGBUFSIZE];
+
+#ifdef HAVE_CLOCK_GETTIME
+ struct timespec now;
+
+ if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
+ zwarn("%s: unable to retrieve time: %e", pm->node.nam, errno);
+ return NULL;
+ }
+
+ arr = (char **)zhalloc(3 * sizeof(*arr));
+ sprintf(buf, "%ld", (long)now.tv_sec);
+ arr[0] = dupstring(buf);
+ sprintf(buf, "%ld", now.tv_nsec);
+ arr[1] = dupstring(buf);
+ arr[2] = NULL;
+
+ return arr;
+#else
+ struct timeval now;
+ struct timezone dummy_tz;
+
+ (void)pm;
+ gettimeofday(&now, &dummy_tz);
+
+ arr = (char **)zhalloc(3 * sizeof(*arr));
+ sprintf(buf, "%ld", (long)now.tv_sec);
+ arr[0] = dupstring(buf);
+ sprintf(buf, "%ld", (long)now.tv_usec * 1000);
+ arr[1] = dupstring(buf);
+ arr[2] = NULL;
+
+ return arr;
+#endif
+}
+
static struct builtin bintab[] = {
BUILTIN("strftime", 0, bin_strftime, 2, 2, 0, "qrs:", NULL),
};
@@ -158,9 +221,19 @@ static struct builtin bintab[] = {
static const struct gsu_integer epochseconds_gsu =
{ getcurrentsecs, NULL, stdunsetfn };
+static const struct gsu_float epochrealtime_gsu =
+{ getcurrentrealtime, NULL, stdunsetfn };
+
+static const struct gsu_array epochtime_gsu =
+{ getcurrenttime, NULL, stdunsetfn };
+
static struct paramdef patab[] = {
SPECIALPMDEF("EPOCHSECONDS", PM_INTEGER|PM_READONLY,
&epochseconds_gsu, NULL, NULL),
+ SPECIALPMDEF("EPOCHREALTIME", PM_FFLOAT|PM_READONLY,
+ &epochrealtime_gsu, NULL, NULL),
+ SPECIALPMDEF("epochtime", PM_ARRAY|PM_READONLY,
+ &epochtime_gsu, NULL, NULL)
};
static struct features module_features = {
diff --git a/Src/Modules/datetime.mdd b/Src/Modules/datetime.mdd
index 0e5ffffb2..b7c1a4a95 100644
--- a/Src/Modules/datetime.mdd
+++ b/Src/Modules/datetime.mdd
@@ -4,6 +4,6 @@ link=either
load=no
functions='Functions/Calendar/*'
-autofeatures="b:strftime p:EPOCHSECONDS"
+autofeatures="b:strftime p:EPOCHSECONDS p:EPOCHREALTIME p:epochtime"
objects="datetime.o"
diff --git a/Src/Modules/db_gdbm.c b/Src/Modules/db_gdbm.c
index bdbbe19d8..9a2a7a5b9 100644
--- a/Src/Modules/db_gdbm.c
+++ b/Src/Modules/db_gdbm.c
@@ -39,7 +39,9 @@
#include <gdbm.h>
+#if 0 /* what is this for? */
static char *backtype = "db/gdbm";
+#endif
static const struct gsu_scalar gdbm_gsu =
{ gdbmgetfn, gdbmsetfn, gdbmunsetfn };
@@ -138,7 +140,6 @@ static void
gdbmsetfn(Param pm, char *val)
{
datum key, content;
- int ret;
GDBM_FILE dbf;
key.dptr = pm->node.nam;
@@ -147,7 +148,7 @@ gdbmsetfn(Param pm, char *val)
content.dsize = strlen(content.dptr) + 1;
dbf = (GDBM_FILE)(pm->u.hash->tmpdata);
- ret = gdbm_store(dbf, key, content, GDBM_REPLACE);
+ (void)gdbm_store(dbf, key, content, GDBM_REPLACE);
}
/**/
@@ -155,14 +156,13 @@ static void
gdbmunsetfn(Param pm, int um)
{
datum key;
- int ret;
GDBM_FILE dbf;
key.dptr = pm->node.nam;
key.dsize = strlen(key.dptr) + 1;
dbf = (GDBM_FILE)(pm->u.hash->tmpdata);
- ret = gdbm_delete(dbf, key);
+ (void)gdbm_delete(dbf, key);
}
/**/
@@ -171,12 +171,10 @@ getgdbmnode(HashTable ht, const char *name)
{
int len;
char *nameu;
- datum key;
Param pm = NULL;
nameu = dupstring(name);
unmetafy(nameu, &len);
- key.dptr = nameu;
pm = (Param) hcalloc(sizeof(struct param));
pm->node.nam = nameu;
diff --git a/Src/Modules/pcre.c b/Src/Modules/pcre.c
index e1a897944..2e3556a8d 100644
--- a/Src/Modules/pcre.c
+++ b/Src/Modules/pcre.c
@@ -77,6 +77,7 @@ bin_pcre_compile(char *nam, char **args, Options ops, UNUSED(int func))
{
int pcre_opts = 0, pcre_errptr;
const char *pcre_error;
+ char *target;
if(OPT_ISSET(ops,'a')) pcre_opts |= PCRE_ANCHORED;
if(OPT_ISSET(ops,'i')) pcre_opts |= PCRE_CASELESS;
@@ -92,8 +93,13 @@ bin_pcre_compile(char *nam, char **args, Options ops, UNUSED(int func))
if (pcre_pattern)
pcre_free(pcre_pattern);
- pcre_pattern = pcre_compile(*args, pcre_opts, &pcre_error, &pcre_errptr, NULL);
+ target = ztrdup(*args);
+ unmetafy(target, NULL);
+
+ pcre_pattern = pcre_compile(target, pcre_opts, &pcre_error, &pcre_errptr, NULL);
+ free(target);
+
if (pcre_pattern == NULL)
{
zwarnnam(nam, "error in regex: %s", pcre_error);
@@ -161,7 +167,7 @@ zpcre_get_substrings(char *arg, int *ovec, int ret, char *matchvar,
sprintf(offset_all, "%d %d", ovec[0], ovec[1]);
setsparam("ZPCRE_OP", ztrdup(offset_all));
}
- match_all = ztrdup(captures[0]);
+ match_all = metafy(captures[0], -1, META_DUP);
setsparam(matchvar, match_all);
/*
* If we're setting match, mbegin, mend we only do
@@ -169,7 +175,15 @@ zpcre_get_substrings(char *arg, int *ovec, int ret, char *matchvar,
* (c.f. regex.c).
*/
if (!want_begin_end || nelem) {
- matches = zarrdup(&captures[capture_start]);
+ char **x, **y;
+ y = &captures[capture_start];
+ matches = x = (char **) zalloc(sizeof(char *) * (arrlen(y) + 1));
+ do {
+ if (*y)
+ *x++ = metafy(*y, -1, META_DUP);
+ else
+ *x++ = NULL;
+ } while (*y++);
setaparam(substravar, matches);
}
@@ -255,6 +269,7 @@ bin_pcre_match(char *nam, char **args, Options ops, UNUSED(int func))
{
int ret, capcount, *ovec, ovecsize, c;
char *matched_portion = NULL;
+ char *plaintext = NULL;
char *receptacle = NULL;
int return_value = 1;
/* The subject length and offset start are both int values in pcre_exec */
@@ -278,7 +293,7 @@ bin_pcre_match(char *nam, char **args, Options ops, UNUSED(int func))
}
/* For the entire match, 'Return' the offset byte positions instead of the matched string */
if(OPT_ISSET(ops,'b')) want_offset_pair = 1;
-
+
if(!*args) {
zwarnnam(nam, "not enough arguments");
}
@@ -288,26 +303,28 @@ bin_pcre_match(char *nam, char **args, Options ops, UNUSED(int func))
zwarnnam(nam, "error %d in fullinfo", ret);
return 1;
}
-
+
ovecsize = (capcount+1)*3;
ovec = zalloc(ovecsize*sizeof(int));
-
- subject_len = (int)strlen(*args);
+
+ plaintext = ztrdup(*args);
+ unmetafy(plaintext, NULL);
+ subject_len = (int)strlen(plaintext);
if (offset_start < 0 || offset_start >= subject_len)
ret = PCRE_ERROR_NOMATCH;
else
- ret = pcre_exec(pcre_pattern, pcre_hints, *args, subject_len, offset_start, 0, ovec, ovecsize);
+ ret = pcre_exec(pcre_pattern, pcre_hints, plaintext, subject_len, offset_start, 0, ovec, ovecsize);
if (ret==0) return_value = 0;
else if (ret==PCRE_ERROR_NOMATCH) /* no match */;
else if (ret>0) {
- zpcre_get_substrings(*args, ovec, ret, matched_portion, receptacle,
+ zpcre_get_substrings(plaintext, ovec, ret, matched_portion, receptacle,
want_offset_pair, 0, 0);
return_value = 0;
}
else {
- zwarnnam(nam, "error in pcre_exec");
+ zwarnnam(nam, "error in pcre_exec [%d]", ret);
}
if (ovec)
@@ -322,7 +339,7 @@ cond_pcre_match(char **a, int id)
{
pcre *pcre_pat;
const char *pcre_err;
- char *lhstr, *rhre, *avar=NULL;
+ char *lhstr, *rhre, *lhstr_plain, *rhre_plain, *avar=NULL;
int r = 0, pcre_opts = 0, pcre_errptr, capcnt, *ov, ovsize;
int return_value = 0;
@@ -331,6 +348,10 @@ cond_pcre_match(char **a, int id)
lhstr = cond_str(a,0,0);
rhre = cond_str(a,1,0);
+ lhstr_plain = ztrdup(lhstr);
+ rhre_plain = ztrdup(rhre);
+ unmetafy(lhstr_plain, NULL);
+ unmetafy(rhre_plain, NULL);
pcre_pat = NULL;
ov = NULL;
@@ -339,7 +360,7 @@ cond_pcre_match(char **a, int id)
switch(id) {
case CPCRE_PLAIN:
- pcre_pat = pcre_compile(rhre, pcre_opts, &pcre_err, &pcre_errptr, NULL);
+ pcre_pat = pcre_compile(rhre_plain, pcre_opts, &pcre_err, &pcre_errptr, NULL);
if (pcre_pat == NULL) {
zwarn("failed to compile regexp /%s/: %s", rhre, pcre_err);
break;
@@ -347,7 +368,7 @@ cond_pcre_match(char **a, int id)
pcre_fullinfo(pcre_pat, NULL, PCRE_INFO_CAPTURECOUNT, &capcnt);
ovsize = (capcnt+1)*3;
ov = zalloc(ovsize*sizeof(int));
- r = pcre_exec(pcre_pat, NULL, lhstr, strlen(lhstr), 0, 0, ov, ovsize);
+ r = pcre_exec(pcre_pat, NULL, lhstr_plain, strlen(lhstr_plain), 0, 0, ov, ovsize);
/* r < 0 => error; r==0 match but not enough size in ov
* r > 0 => (r-1) substrings found; r==1 => no substrings
*/
@@ -356,13 +377,16 @@ cond_pcre_match(char **a, int id)
return_value = 1;
break;
}
- else if (r==PCRE_ERROR_NOMATCH) return 0; /* no match */
+ else if (r==PCRE_ERROR_NOMATCH) {
+ return_value = 0; /* no match */
+ break;
+ }
else if (r<0) {
- zwarn("pcre_exec() error: %d", r);
+ zwarn("pcre_exec() error [%d]", r);
break;
}
else if (r>0) {
- zpcre_get_substrings(lhstr, ov, r, NULL, avar, 0,
+ zpcre_get_substrings(lhstr_plain, ov, r, NULL, avar, 0,
isset(BASHREMATCH),
!isset(BASHREMATCH));
return_value = 1;
@@ -371,6 +395,10 @@ cond_pcre_match(char **a, int id)
break;
}
+ if (lhstr_plain)
+ free(lhstr_plain);
+ if(rhre_plain)
+ free(rhre_plain);
if (pcre_pat)
pcre_free(pcre_pat);
if (ov)
diff --git a/Src/Modules/pcre.mdd b/Src/Modules/pcre.mdd
index 3e1579117..6eb3c691b 100644
--- a/Src/Modules/pcre.mdd
+++ b/Src/Modules/pcre.mdd
@@ -1,5 +1,5 @@
name=zsh/pcre
-link=`if test x$enable_pcre = xyes; then echo dynamic; else echo no; fi`
+link=`if test x$enable_pcre = xyes && (pcre-config --version >/dev/null 2>/dev/null); then echo dynamic; else echo no; fi`
load=no
autofeatures="b:pcre_compile b:pcre_study b:pcre_match"
diff --git a/Src/Modules/zftp.c b/Src/Modules/zftp.c
index 8d688abd4..d16e2f618 100644
--- a/Src/Modules/zftp.c
+++ b/Src/Modules/zftp.c
@@ -2520,7 +2520,7 @@ zftp_local(UNUSED(char *name), char **args, int flags)
printf("%s %s\n", output64(sz), mt);
#else
DPUTS(sizeof(sz) > 4, "Shell compiled with wrong off_t size");
- printf("%ld %s\n", sz, mt);
+ printf("%ld %s\n", (long)sz, mt);
#endif
zsfree(mt);
if (dofd)