Browse Source

Plan 9 from Bell Labs 2009-02-05

David du Colombier 15 years ago
parent
commit
c5e8f7f864
4 changed files with 39 additions and 8 deletions
  1. 7 4
      sys/man/1/awk
  2. 21 3
      sys/src/cmd/awk/run.c
  3. 10 0
      sys/src/cmd/file.c
  4. 1 1
      sys/src/cmd/sed.c

+ 7 - 4
sys/man/1/awk

@@ -204,6 +204,9 @@ The built-in function
 .BI fflush( expr )
 flushes any buffered output for the file or pipe
 .IR expr .
+If
+.IR expr
+is omitted or is a null string, all open files are flushed.
 .PP
 The mathematical functions
 .BR exp ,
@@ -218,11 +221,11 @@ Other built-in functions:
 .TF length
 .TP
 .B length
-the length of its argument
-taken as a string,
-or of
+If its argument is a string, the string's length is returned.
+If its argument is an array, the number of subscripts in the array is returned.
+If no argument, the length of
 .B $0
-if no argument.
+is returned.
 .TP
 .B rand
 random number on (0,1)

+ 21 - 3
sys/src/cmd/awk/run.c

@@ -1454,14 +1454,20 @@ Cell *bltin(Node **a, int n)	/* builtin functions. a[0] is type, a[1] is arg lis
 	char mbc[50];
 	Node *nextarg;
 	FILE *fp;
+	void flush_all(void);
 
 	t = ptoi(a[0]);
 	x = execute(a[1]);
 	nextarg = a[1]->nnext;
 	switch (t) {
 	case FLENGTH:
-		p = getsval(x);
-		u = (Awkfloat) countposn(p, strlen(p)); break;
+		if (isarr(x))
+			u = ((Array *) x->sval)->nelem;	/* GROT. should be function*/
+		else {
+			p = getsval(x);
+			u = (Awkfloat) countposn(p, strlen(p));
+		}
+		break;
 	case FLOG:
 		u = errcheck(log(getfval(x)), "log"); break;
 	case FINT:
@@ -1518,7 +1524,10 @@ Cell *bltin(Node **a, int n)	/* builtin functions. a[0] is type, a[1] is arg lis
 		free(buf);
 		return x;
 	case FFLUSH:
-		if ((fp = openfile(FFLUSH, getsval(x))) == NULL)
+		if (isrec(x) || strlen(getsval(x)) == 0) {
+			flush_all();	/* fflush() or fflush("") -> all */
+			u = 0;
+		} else if ((fp = openfile(FFLUSH, getsval(x))) == NULL)
 			u = EOF;
 		else
 			u = fflush(fp);
@@ -1713,6 +1722,15 @@ void closeall(void)
 		}
 }
 
+void flush_all(void)
+{
+	int i;
+
+	for (i = 0; i < FOPEN_MAX; i++)
+		if (files[i].fp)
+			fflush(files[i].fp);
+}
+
 void backsub(char **pb_ptr, char **sptr_ptr);
 
 Cell *sub(Node **a, int nnn)	/* substitute command */

+ 10 - 0
sys/src/cmd/file.c

@@ -889,6 +889,16 @@ iff(void)
 		print("%s\n", mime? "audio/x-aiff": "aiff audio");
 		return 1;
 	}
+	if (strncmp((char*)buf, "RIFF", 4) == 0) {
+		if (strncmp((char*)buf+8, "WAVE", 4) == 0)
+			print("%s\n", mime? "audio/wave": "wave audio");
+		else if (strncmp((char*)buf+8, "AVI ", 4) == 0)
+			print("%s\n", mime? "video/avi": "avi video");
+		else
+			print("%s\n", mime? "application/octet-stream":
+				"riff file");
+		return 1;
+	}
 	return 0;
 }
 

+ 1 - 1
sys/src/cmd/sed.c

@@ -995,7 +995,7 @@ match(Reprog *pattern, Rune *buf)
 		return 0;
 	subexp[0].rsp = buf;
 	subexp[0].ep = 0;
-	if (rregexec(pattern, linebuf, subexp, MAXSUB)) {
+	if (rregexec(pattern, linebuf, subexp, MAXSUB) > 0) {
 		loc1 = subexp[0].rsp;
 		loc2 = subexp[0].rep;
 		return 1;