[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

make audioctl more like sysctl/mixerctl



remove the -w flag

please review/test, this diff is scary.

-vincent

Index: audioctl.1
===================================================================
RCS file: /cvs/src/usr.bin/audioctl/audioctl.1,v
retrieving revision 1.17
diff -u -r1.17 audioctl.1
--- audioctl.1	2004/05/09 03:37:45	1.17
+++ audioctl.1	2004/06/09 21:26:45
@@ -52,7 +52,6 @@
 .Nm audioctl
 .Op Fl f Ar file
 .Op Fl n
-.Fl w
 .Ar name=value
 .Op Ar ...
 .Sh DESCRIPTION
@@ -72,7 +71,7 @@
 .Bl -tag -width Ds
 .It Fl a
 Print all device variables and their current values.
-.It Fl w Ar name=value
+.It Ar name=value
 Attempt to set the specified variable
 .Ar name
 to
@@ -95,7 +94,7 @@
 .Sh EXAMPLES
 To set the playing sampling rate to 11025 you can enter
 .Pp
-.Dl $ audioctl -w play.sample_rate=11025
+.Dl $ audioctl play.sample_rate=11025
 .Pp
 Note that many of the variables that can be inspected and changed
 are reset when the
@@ -106,7 +105,7 @@
 .Dl $ (cat file.au; audioctl -f /dev/audioctl -a) > /dev/audio
 or
 .Bd -literal -offset indent -compact
-$ (audioctl -f /dev/audioctl -w blocksize=1024; cat file.au) \e
+$ (audioctl -f /dev/audioctl blocksize=1024; cat file.au) \e
 	> /dev/audio
 .Ed
 .Sh SEE ALSO
Index: audioctl.c
===================================================================
RCS file: /cvs/src/usr.bin/audioctl/audioctl.c,v
retrieving revision 1.10
diff -u -r1.10 audioctl.c
--- audioctl.c	2003/06/21 01:39:07	1.10
+++ audioctl.c	2004/06/09 21:26:45
@@ -324,7 +324,7 @@
 	fprintf(stderr,
 	    "usage: %s [-f file] [-n] -a\n"
 	    "       %s [-f file] [-n] name [...]\n"
-	    "       %s [-f file] [-n] -w name=value [...]\n", __progname,
+	    "       %s [-f file] [-n] name=value [...]\n", __progname,
 		__progname, __progname);
 
 	exit(1);
@@ -334,8 +334,9 @@
 main(int argc, char **argv)
 {
 	int fd, i, ch;
-	int aflag = 0, wflag = 0;
+	int aflag = 0, canwrite;
 	struct stat dstat, ostat;
+	struct field *p;
 	char *file;
 	char *sep = "=";
     
@@ -348,7 +349,7 @@
 			aflag++;
 			break;
 		case 'w':
-			wflag++;
+			/* backward compatibility */
 			break;
 		case 'n':
 			sep = 0;
@@ -356,16 +357,19 @@
 		case 'f':
 			file = optarg;
 			break;
-		case '?':
 		default:
 			usage();
 		}
 	}
 	argc -= optind;
 	argv += optind;
-    
-	if ((fd = open(file, wflag ? O_RDWR : O_RDONLY)) < 0)
-		err(1, "%s", file);
+
+	if ((fd = open(file, O_RDWR)) < 0) {
+		if ((fd = open(file, O_RDONLY)) < 0)
+			err(1, "%s", file);
+		canwrite = 0;
+	} else
+		canwrite = 1;
     
 	/* Check if stdout is the same device as the audio device. */
 	if (fstat(fd, &dstat) < 0)
@@ -377,71 +381,65 @@
 	    minor(dstat.st_dev) == minor(ostat.st_dev))
 		/* We can't write to stdout so use stderr */
 		out = stderr;
+
+	if (!argc && !aflag)
+		usage();
 
-	if (!wflag)
-		getinfo(fd);
+	getinfo(fd);
 
-	if (!argc && aflag && !wflag) {
-		for(i = 0; fields[i].name; i++) {
+	if (aflag) {
+		for (i = 0; fields[i].name; i++) {
 			if (!(fields[i].flags & ALIAS)) {
 				prfield(&fields[i], sep);
 				fprintf(out, "\n");
 			}
 		}
-	} else if (argc > 0 && !aflag) {
-		struct field *p;
-		if (wflag) {
-			AUDIO_INITINFO(&info);
-			while(argc--) {
-				char *q;
+	} else {
+		while (argc--) {
+			char *q;
 		
-				if ((q = strchr(*argv, '='))) {
-					*q++ = 0;
-					p = findfield(*argv);
-					if (p == 0)
-						warnx("field `%s' does not exist", *argv);
+			if ((q = strchr(*argv, '=')) != NULL) {
+				*q++ = 0;
+				p = findfield(*argv);
+				if (p == 0)
+					warnx("field `%s' does not exist", *argv);
+				else {
+					if (!canwrite)
+						errx(1, "%s: permission denied",
+						    *argv);
+					if (p->flags & READONLY)
+						warnx("`%s' is read only", *argv);
 					else {
-						if (p->flags & READONLY)
-							warnx("`%s' is read only", *argv);
-						else {
-							rdfield(p, q);
-							if (p->valp == &fullduplex)
-								if (ioctl(fd, AUDIO_SETFD, &fullduplex) < 0)
-									err(1, "set failed");
-						}
-					}
-				} else
-					warnx("No `=' in %s", *argv);
-				argv++;
-			}
-			if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
-				err(1, "set failed");
-			if (sep) {
-				getinfo(fd);
-				for(i = 0; fields[i].name; i++) {
-					if (fields[i].flags & SET) {
-						fprintf(out, "%s: -> ", fields[i].name);
-						prfield(&fields[i], 0);
-						fprintf(out, "\n");
+						rdfield(p, q);
+						if (p->valp == &fullduplex)
+							if (ioctl(fd, AUDIO_SETFD,
+							    &fullduplex) < 0)
+								err(1, "set failed");
 					}
 				}
-			}
-		} else {
-			while(argc--) {
+			} else {
 				p = findfield(*argv);
-				if (p == 0) {
-					if (strchr(*argv, '='))
-						warnx("field %s does not exist (use -w to set a variable)", *argv);
-					else
-						warnx("field %s does not exist", *argv);
-				} else {
+				if (p == 0)
+					warnx("field %s does not exist", *argv);
+				else {
 					prfield(p, sep);
 					fprintf(out, "\n");
 				}
-				argv++;
 			}
+			argv++;
 		}
-	} else
-		usage();
+		if (canwrite && ioctl(fd, AUDIO_SETINFO, &info) < 0)
+			err(1, "set failed");
+		if (sep) {
+			getinfo(fd);
+			for (i = 0; fields[i].name; i++) {
+				if (fields[i].flags & SET) {
+					fprintf(out, "%s: -> ", fields[i].name);
+					prfield(&fields[i], 0);
+					fprintf(out, "\n");
+				}
+			}
+		}
+	}
 	exit(0);
 }