Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: size of externally visible integers



This promotes integers used within the shell to zlong when there is a
corresponding shell variable which is a zlong to avoid any involuntary
truncations.  Most of this is to handle history.  I don't think there
are any significant speed implications since searching is done with
pointers.

Where it's not possible to use a zlong internally there are some
warnings.  Some functions in params.c already claim the parameter is
uid_t, gid_t etc, so I took them at their word.

I think it might be a good idea to allow a negative SAVEHIST to mean
save history unconditionally, most useful when appending values.
(Currently this is caught and the value is set to 0.)  Obviously only
HISTSIZE values would be read back in.  Potentially it might be a good
idea to scan backward from the end of a long history file when reading
back in in such a case.

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.125
diff -u -r1.125 builtin.c
--- Src/builtin.c	8 Sep 2004 08:24:00 -0000	1.125
+++ Src/builtin.c	8 Sep 2004 15:24:26 -0000
@@ -1293,7 +1293,8 @@
 int
 bin_fc(char *nam, char **argv, Options ops, int func)
 {
-    int first = -1, last = -1, retval;
+    zlong first = -1, last = -1;
+    int retval;
     char *s;
     struct asgment *asgf = NULL, *asgl = NULL;
     Patprog pprog = NULL;
@@ -1305,15 +1306,15 @@
     }
     if (OPT_ISSET(ops,'p')) {
 	char *hf = "";
-	int hs = DEFAULT_HISTSIZE;
-	int shs = 0;
+	zlong hs = DEFAULT_HISTSIZE;
+	zlong shs = 0;
 	int level = OPT_ISSET(ops,'a') ? locallevel : -1;
 	if (*argv) {
 	    hf = *argv++;
 	    if (*argv) {
-		hs = atoi(*argv++);
+		hs = zstrtol(*argv++, NULL, 10);
 		if (*argv)
-		    shs = atoi(*argv++);
+		    shs = zstrtol(*argv++, NULL, 10);
 		else
 		    shs = hs;
 		if (*argv) {
@@ -1490,10 +1491,10 @@
 /* get the history event associated with s */
 
 /**/
-static int
+static zlong
 fcgetcomm(char *s)
 {
-    int cmd;
+    zlong cmd;
 
     /* First try to match a history number.  Negative *
      * numbers indicate reversed numbering.           */
@@ -1558,9 +1559,11 @@
 
 /**/
 static int
-fclist(FILE *f, Options ops, int first, int last, struct asgment *subs, Patprog pprog)
+fclist(FILE *f, Options ops, zlong first, zlong last,
+       struct asgment *subs, Patprog pprog)
 {
-    int fclistdone = 0, tmp;
+    int fclistdone = 0;
+    zlong tmp;
     char *s;
     Histent ent;
 
@@ -1592,7 +1595,9 @@
 
 	    /* do numbering */
 	    if (!OPT_ISSET(ops,'n')) {
-		fprintf(f, "%5d%c ", ent->histnum,
+		char buf[DIGBUFSIZE];
+		convbase(buf, ent->histnum, 10);
+		fprintf(f, "%5s%c ", buf,
 			ent->flags & HIST_FOREIGN? '*' : ' ');
 	    }
 	    /* output actual time (and possibly date) of execution of the
Index: Src/hist.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/hist.c,v
retrieving revision 1.52
diff -u -r1.52 hist.c
--- Src/hist.c	2 Jun 2004 22:14:25 -0000	1.52
+++ Src/hist.c	8 Sep 2004 15:24:31 -0000
@@ -79,7 +79,7 @@
  */
  
 /**/
-mod_export int curhist;
+mod_export zlong curhist;
 
 /**/
 struct histent curline;
@@ -87,7 +87,7 @@
 /* current line count of allocated history entries */
 
 /**/
-int histlinect;
+zlong histlinect;
 
 /* The history lines are kept in a hash, and also doubly-linked in a ring */
 
@@ -99,12 +99,12 @@
 /* capacity of history lists */
  
 /**/
-int histsiz;
+zlong histsiz;
  
 /* desired history-file size (in lines) */
  
 /**/
-int savehistsiz;
+zlong savehistsiz;
  
 /* if = 1, we have performed history substitution on the current line *
  * if = 2, we have used the 'p' modifier                              */
@@ -178,7 +178,7 @@
  
 /* default event (usually curhist-1, that is, "!!") */
  
-static int defev;
+static zlong defev;
 
 /* add a character to the current history word */
 
@@ -339,8 +339,10 @@
 static int
 histsubchar(int c)
 {
-    int ev, farg, evset = -1, larg, argc, cflag = 0, bflag = 0;
-    static int mev = -1, marg = -1;
+    int farg, evset = -1, larg, argc, cflag = 0, bflag = 0;
+    zlong ev;
+    static int marg = -1;
+    static zlong mev = -1;
     char buf[256], *ptr;
     char *sline;
     Histent ehist;
@@ -410,7 +412,7 @@
 		return -1;
 	    }
 	} else {
-	    int t0;
+	    zlong t0;
 
 	    for (;;) {
 		if (inblank(c) || c == ';' || c == ':' || c == '^' ||
@@ -448,7 +450,7 @@
 			ev = defev;
 		    evset = 0;
 		}
-	    } else if ((t0 = atoi(buf))) {
+	    } else if ((t0 = zstrtol(buf, NULL, 10))) {
 		ev = (t0 < 0) ? addhistnum(curhist,t0,HIST_FOREIGN) : t0;
 		evset = 1;
 	    } else if ((unsigned)*buf == bangchar) {
@@ -839,8 +841,8 @@
 }
 
 /**/
-mod_export int
-addhistnum(int hl, int n, int xflags)
+mod_export zlong
+addhistnum(zlong hl, int n, int xflags)
 {
     int dir = n < 0? -1 : n > 0? 1 : 0;
     Histent he = gethistent(hl, dir);
@@ -892,7 +894,7 @@
 
 /**/
 mod_export Histent
-gethistent(int ev, int nearmatch)
+gethistent(zlong ev, int nearmatch)
 {
     Histent he;
 
@@ -927,7 +929,7 @@
     Histent he = keep_going? next : hist_ring->down;
     next = he->down;
     if (isset(HISTEXPIREDUPSFIRST) && !(he->flags & HIST_DUP)) {
-	static int max_unique_ct = 0;
+	static zlong max_unique_ct = 0;
 	if (!keep_going)
 	    max_unique_ct = savehistsiz;
 	do {
@@ -1346,7 +1348,7 @@
 /* do ?foo? search */
 
 /**/
-static int
+static zlong
 hconsearch(char *str, int *marg)
 {
     int t1 = 0;
@@ -1370,7 +1372,7 @@
 /* do !foo search */
 
 /**/
-int
+zlong
 hcomsearch(char *str)
 {
     Histent he;
@@ -1795,7 +1797,7 @@
     char *text;
     time_t stim, mtim;
     off_t fpos, fsiz;
-    int next_write_ev;
+    zlong next_write_ev;
 } lasthist;
 
 static struct histsave {
@@ -1803,16 +1805,16 @@
     char *histfile;
     HashTable histtab;
     Histent hist_ring;
-    int curhist;
-    int histlinect;
-    int histsiz;
-    int savehistsiz;
+    zlong curhist;
+    zlong histlinect;
+    zlong histsiz;
+    zlong savehistsiz;
     int locallevel;
 } *histsave_stack;
 static int histsave_stack_size = 0;
 static int histsave_stack_pos = 0;
 
-static int histfile_linect;
+static zlong histfile_linect;
 
 static int
 readhistline(int start, char **bufp, int *bufsiz, FILE *in)
@@ -2006,7 +2008,7 @@
     char *t, *start = NULL;
     FILE *out;
     Histent he;
-    int xcurhist = curhist - !!(histactive & HA_ACTIVE);
+    zlong xcurhist = curhist - !!(histactive & HA_ACTIVE);
     int extended_history = isset(EXTENDEDHISTORY);
 
     if (!interact || savehistsiz <= 0 || !hist_ring
@@ -2345,7 +2347,7 @@
 
 /**/
 int
-pushhiststack(char *hf, int hs, int shs, int level)
+pushhiststack(char *hf, zlong hs, zlong shs, int level)
 {
     struct histsave *h;
     int curline_in_ring = (histactive & HA_ACTIVE) && hist_ring == &curline;
Index: Src/params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.87
diff -u -r1.87 params.c
--- Src/params.c	7 Jul 2004 15:00:58 -0000	1.87
+++ Src/params.c	8 Sep 2004 15:24:35 -0000
@@ -2493,7 +2493,7 @@
 mod_export zlong
 intvargetfn(Param pm)
 {
-    return *((zlong *)pm->u.data);
+    return *pm->u.valptr;
 }
 
 /* Function to set value of generic special integer *
@@ -2504,7 +2504,7 @@
 mod_export void
 intvarsetfn(Param pm, zlong x)
 {
-    *((zlong *)pm->u.data) = x;
+    *pm->u.valptr = x;
 }
 
 /* Function to set value of any ZLE-related integer *
@@ -2515,7 +2515,7 @@
 void
 zlevarsetfn(Param pm, zlong x)
 {
-    zlong *p = (zlong *)pm->u.data;
+    zlong *p = pm->u.valptr;
 
     *p = x;
     if (p == &lines || p == &columns)
@@ -2745,7 +2745,13 @@
 zlong
 intsecondsgetfn(Param pm)
 {
-    return (zlong)floatsecondsgetfn(pm);
+    struct timeval now;
+    struct timezone dummy_tz;
+
+    gettimeofday(&now, &dummy_tz);
+
+    return (zlong)(now.tv_sec - shtimer.tv_sec) +
+	(zlong)(now.tv_usec - shtimer.tv_usec) / (zlong)1000000;
 }
 
 /* Function to set value of special parameter `SECONDS' */
@@ -2754,7 +2760,16 @@
 void
 intsecondssetfn(Param pm, zlong x)
 {
-    floatsecondssetfn(pm, (double)x);
+    struct timeval now;
+    struct timezone dummy_tz;
+    zlong diff;
+
+    gettimeofday(&now, &dummy_tz);
+    diff = (zlong)now.tv_sec - x;
+    shtimer.tv_sec = diff;
+    if ((zlong)shtimer.tv_sec != diff)
+	zwarn("SECONDS truncated on assignment", NULL, 0);
+    shtimer.tv_usec = 0;
 }
 
 /**/
@@ -3090,6 +3105,8 @@
 errnosetfn(UNUSED(Param pm), zlong x)
 {
     errno = (int)x;
+    if ((zlong)errno != x)
+	zwarn("errno truncated on assignment", NULL, 0);
 }
 
 /* Function to get value for special parameter `ERRNO' */
Index: Src/prompt.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/prompt.c,v
retrieving revision 1.22
diff -u -r1.22 prompt.c
--- Src/prompt.c	16 Aug 2004 09:53:11 -0000	1.22
+++ Src/prompt.c	8 Sep 2004 15:24:36 -0000
@@ -384,7 +384,7 @@
 	    case 'h':
 	    case '!':
 		addbufspc(DIGBUFSIZE);
-		sprintf(bp, "%d", curhist);
+		convbase(bp, curhist, 10);
 		bp += strlen(bp);
 		break;
 	    case 'j':
@@ -676,7 +676,7 @@
 		    pputc('!');
 		} else {
 		    addbufspc(DIGBUFSIZE);
-		    sprintf(bp, "%d", curhist);
+		    convbase(bp, curhist, 10);
 		    bp += strlen(bp);
 		}
 	    }
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.59
diff -u -r1.59 zsh.h
--- Src/zsh.h	3 Sep 2004 09:47:49 -0000	1.59
+++ Src/zsh.h	8 Sep 2004 15:24:38 -0000
@@ -1129,6 +1129,7 @@
 	char **arr;		/* value if declared array   (PM_ARRAY)   */
 	char *str;		/* value if declared string  (PM_SCALAR)  */
 	zlong val;		/* value if declared integer (PM_INTEGER) */
+	zlong *valptr;		/* value if special pointer to integer */
 	double dval;		/* value if declared float
 				                    (PM_EFLOAT|PM_FFLOAT) */
         HashTable hash;		/* value if declared assoc   (PM_HASHED)  */
@@ -1334,7 +1335,7 @@
     short *words;		/* Position of words in history     */
 				/*   line:  as pairs of start, end  */
     int nwords;			/* Number of words in history line  */
-    int histnum;		/* A sequential history number      */
+    zlong histnum;		/* A sequential history number      */
 };
 
 #define HIST_MAKEUNIQUE	0x00000001	/* Kill this new entry if not unique */
Index: Src/Zle/compctl.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/compctl.c,v
retrieving revision 1.19
diff -u -r1.19 compctl.c
--- Src/Zle/compctl.c	2 Jun 2004 22:15:01 -0000	1.19
+++ Src/Zle/compctl.c	8 Sep 2004 15:24:42 -0000
@@ -3692,7 +3692,7 @@
 	/* We have a pattern to take things from the history. */
 	Patprog pprogc = NULL;
 	char *e, *h, hpatsav;
-	int i = addhistnum(curhist,-1,HIST_FOREIGN), n = cc->hnum;
+	zlong i = addhistnum(curhist,-1,HIST_FOREIGN), n = cc->hnum;
 	Histent he = gethistent(i, GETHIST_UPWARD);
 
 	/* Parse the pattern, if it isn't the null string. */

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************



Messages sorted by: Reverse Date, Date, Thread, Author