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

Re: fg jobs info



On Wed, 05 Sep 2007 08:30:00 -0700
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Sep 5, 10:28am, Peter Stephenson wrote:
> }
> } > add a variable that automagically returns the text of the 
> } > fg'd job.
> } 
> } That's $jobtexts.
> 
> Well ...
> 
> $jobtexts returns the text of any/all of the jobs, by job number.
> 
> That doesn't solve the problem of knowing which job number is the job
> in the foreground.

It's easy and probably useful to make it possible to look up jobs in the
special variables by non-numeric arguments, as below.  You only get normal
job numbers back out when scanning keys, however.

> OTOH, I can't figure out how knowing that could be useful, because
> when a job is in the foreground the shell is blocked in zwaitjob(),
> so there's no way for it to do anything useful with the job text.

It doesn't fix this, however.

Index: Doc/Zsh/mod_parameter.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_parameter.yo,v
retrieving revision 1.7
diff -u -r1.7 mod_parameter.yo
--- Doc/Zsh/mod_parameter.yo	17 Sep 2006 19:28:46 -0000	1.7
+++ Doc/Zsh/mod_parameter.yo	5 Sep 2007 15:51:19 -0000
@@ -124,11 +124,19 @@
 item(tt(jobdirs))(
 This associative array maps job numbers to the directories from which the
 job was started (which may not be the current directory of the job).
+
+The keys of the associative arrays are usually valid job numbers,
+and these are the values output with, for example, tt(${(k)jobdirs}).
+Non-numeric job references may be used when looking up a value;
+for example, tt(${jobdirs[%+]}) refers to the current job.
 )
 vindex(jobtexts)
 item(tt(jobtexts))(
 This associative array maps job numbers to the texts of the command lines
 that were used to start the jobs.
+
+Handling of the keys of the associative array is as described for
+tt(jobdirs) above.
 )
 vindex(jobstates)
 item(tt(jobstates))(
@@ -142,6 +150,9 @@
 otherwise. This is followed by one `var(pid)tt(=)var(state)' for every
 process in the job. The var(pid)s are, of course, the process IDs and
 the var(state) describes the state of that process.
+
+Handling of the keys of the associative array is as described for
+tt(jobdirs) above.
 )
 vindex(nameddirs)
 item(tt(nameddirs))(
Index: Src/jobs.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/jobs.c,v
retrieving revision 1.59
diff -u -r1.59 jobs.c
--- Src/jobs.c	4 Sep 2007 20:43:52 -0000	1.59
+++ Src/jobs.c	5 Sep 2007 15:51:20 -0000
@@ -1468,8 +1468,8 @@
  * to a job number.                                             */
 
 /**/
-static int
-getjob(char *s, char *prog)
+mod_export int
+getjob(const char *s, const char *prog)
 {
     int jobnum, returnval, mymaxjob;
     Job myjobtab;
@@ -1489,7 +1489,8 @@
     /* "%%", "%+" and "%" all represent the current job */
     if (*s == '%' || *s == '+' || !*s) {
 	if (curjob == -1) {
-	    zwarnnam(prog, "no current job");
+	    if (prog)
+		zwarnnam(prog, "no current job");
 	    returnval = -1;
 	    goto done;
 	}
@@ -1499,7 +1500,8 @@
     /* "%-" represents the previous job */
     if (*s == '-') {
 	if (prevjob == -1) {
-	    zwarnnam(prog, "no previous job");
+	    if (prog)
+		zwarnnam(prog, "no previous job");
 	    returnval = -1;
 	    goto done;
 	}
@@ -1521,7 +1523,8 @@
 	    returnval = jobnum;
 	    goto done;
 	}
-	zwarnnam(prog, "%%%s: no such job", s);
+	if (prog)
+	    zwarnnam(prog, "%%%s: no such job", s);
 	returnval = -1;
 	goto done;
     }
@@ -1538,7 +1541,8 @@
 			returnval = jobnum;
 			goto done;
 		    }
-	zwarnnam(prog, "job not found: %s", s);
+	if (prog)
+	    zwarnnam(prog, "job not found: %s", s);
 	returnval = -1;
 	goto done;
     }
@@ -2299,7 +2303,7 @@
 
 /**/
 int
-findjobnam(char *s)
+findjobnam(const char *s)
 {
     int jobnum;
 
Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.166
diff -u -r1.166 utils.c
--- Src/utils.c	30 Aug 2007 15:18:25 -0000	1.166
+++ Src/utils.c	5 Sep 2007 15:51:22 -0000
@@ -5017,7 +5017,7 @@
 
 /**/
 mod_export int
-strpfx(char *s, char *t)
+strpfx(const char *s, const char *t)
 {
     while (*s && *s == *t)
 	s++, t++;
Index: Src/Modules/parameter.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/parameter.c,v
retrieving revision 1.44
diff -u -r1.44 parameter.c
--- Src/Modules/parameter.c	6 Jul 2007 21:52:40 -0000	1.44
+++ Src/Modules/parameter.c	5 Sep 2007 15:51:23 -0000
@@ -1007,13 +1007,18 @@
 {
     Param pm = NULL;
     int job;
+    char *pend;
 
     pm = (Param) hcalloc(sizeof(struct param));
     pm->node.nam = dupstring(name);
     pm->node.flags = PM_SCALAR | PM_READONLY;
     pm->gsu.s = &nullsetscalar_gsu;
 
-    if ((job = atoi(name)) >= 1 && job <= maxjob &&
+    job = strtod(name, &pend);
+    /* Non-numeric keys are looked up by job name */
+    if (*pend)
+	job = getjob(name, NULL);
+    if (job >= 1 && job <= maxjob &&
 	jobtab[job].stat && jobtab[job].procs &&
 	!(jobtab[job].stat & STAT_NOPRINT))
 	pm->u.str = pmjobtext(job);
@@ -1104,13 +1109,17 @@
 {
     Param pm = NULL;
     int job;
+    char *pend;
 
     pm = (Param) hcalloc(sizeof(struct param));
     pm->node.nam = dupstring(name);
     pm->node.flags = PM_SCALAR | PM_READONLY;
     pm->gsu.s = &nullsetscalar_gsu;
 
-    if ((job = atoi(name)) >= 1 && job <= maxjob &&
+    job = strtod(name, &pend);
+    if (*pend)
+	job = getjob(name, NULL);
+    if (job >= 1 && job <= maxjob &&
 	jobtab[job].stat && jobtab[job].procs &&
 	!(jobtab[job].stat & STAT_NOPRINT))
 	pm->u.str = pmjobstate(job);
@@ -1166,13 +1175,17 @@
 {
     Param pm = NULL;
     int job;
+    char *pend;
 
     pm = (Param) hcalloc(sizeof(struct param));
     pm->node.nam = dupstring(name);
     pm->node.flags = PM_SCALAR | PM_READONLY;
     pm->gsu.s = &nullsetscalar_gsu;
 
-    if ((job = atoi(name)) >= 1 && job <= maxjob &&
+    job = strtod(name, &pend);
+    if (*pend)
+	job = getjob(name, NULL);
+    if (job >= 1 && job <= maxjob &&
 	jobtab[job].stat && jobtab[job].procs &&
 	!(jobtab[job].stat & STAT_NOPRINT))
 	pm->u.str = pmjobdir(job);



.



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