[aadk-commits] dbailey: uClinux/trunk r553 - /uClinux/trunk/uClinux-dist/user/busybox/miscut...

SVN commits to the AADK repository aadk-commits at lists.digium.com
Wed Aug 8 14:06:11 CDT 2007


Author: dbailey
Date: Wed Aug  8 14:06:10 2007
New Revision: 553

URL: http://svn.digium.com/view/aadk?view=rev&rev=553
Log:
Updated to make use getopt command line parsing

Modified:
    uClinux/trunk/uClinux-dist/user/busybox/miscutils/ztmonitor.c

Modified: uClinux/trunk/uClinux-dist/user/busybox/miscutils/ztmonitor.c
URL: http://svn.digium.com/view/aadk/uClinux/trunk/uClinux-dist/user/busybox/miscutils/ztmonitor.c?view=diff&rev=553&r1=552&r2=553
==============================================================================
--- uClinux/trunk/uClinux-dist/user/busybox/miscutils/ztmonitor.c (original)
+++ uClinux/trunk/uClinux-dist/user/busybox/miscutils/ztmonitor.c Wed Aug  8 14:06:10 2007
@@ -59,7 +59,18 @@
  * the main loop in case we ever add a signal
  * handler.
  */
-static FILE*  ofh[4] = {0, 0, 0, 0};
+enum ofh_type {
+	ofh_def = 0,
+ ofh_tx,
+ ofh_pre,
+ ofh_pretx,
+ ofh_last
+};
+
+struct outfile_info {
+	FILE*	fd;
+	char*	file_name;
+};
 
 static int stereo = 0;
 static int verbose = 0;
@@ -302,100 +313,94 @@
 int ztmonitor_main(int argc, char *argv[])
 {
 	int afd = -1;
+	struct outfile_info outfile[4] = {
+		{NULL, NULL},{NULL, NULL},{NULL, NULL},{NULL, NULL}};
 	int pfd[4] = {-1, -1, -1, -1};
 	short buf[8192];
 	short buf2[16384];
-	char  output_file[255];
+	int outfile_active = 0;
 	int res, res2;
 	int visual = 0;
 	int multichannel = 0;
 	int ossoutput = 0;
 	int preecho = 0;
-	int savefile = 0;
-	int x, i;
+	int x;
 	struct zt_confinfo zc;
-
-	if ((argc < 2) || (atoi(argv[1]) < 1)) {
+	int opt;
+	int opt_visual = 0;
+	int chan_num = -1;
+
+	while((opt = getopt(argc, argv, "hvmopf:r:t:F:R:T:")) != -1) {
+		switch(opt) {
+			case 'h': usage(); exit(0); break;
+			case '?': usage(); exit(1); break;
+			case 'v': opt_visual++; break;
+			case 'm': multichannel=1; break;
+			case 'o': ossoutput=1; break;
+			case 'p': preecho=1; break;
+			case 'f': outfile[ofh_def].file_name = optarg; outfile_active =1;   break;
+			case 'r': outfile[ofh_def].file_name = optarg; outfile_active =1;  multichannel = 1; break;
+			case 't': outfile[ofh_tx].file_name = optarg;  outfile_active =1;  multichannel = 1; break;
+			case 'F': outfile[ofh_pre].file_name = optarg; outfile_active =1;  preecho = 1;      break;
+			case 'R': outfile[ofh_pre].file_name = optarg; outfile_active =1;  multichannel = 1; preecho = 1; break;
+			case 'T': outfile[ofh_pretx].file_name = optarg; outfile_active =1;multichannel = 1; preecho = 1; break;
+		}
+	}
+	/* XXX: have a single "verbose" variable? or separate those 
+	* two options? */
+	if (opt_visual >= 1) {
+		visual = 1;
+		multichannel=1;
+	}
+	if (opt_visual >= 2)
+		verbose = 1;
+
+	for(x = 0; x < 4; x++) {
+		if (outfile[x].file_name && ((outfile[x].fd = fopen(outfile[x].file_name, "w"))<0)) {
+			fprintf(stderr, PROG ": Could not open %s for writing: %s\n", 
+					outfile[x].file_name, strerror(errno));
+			exit(1);
+		}
+	}
+#if 0
+		if (output_file)
+		printf("Started recording. To stop: cress ctrl-C. "
+					   "Recording a raw slinear file, to get a wav file:\n"
+					   "  sox -r 8000 -s -w -c %d %s %s.wav\n", 
+					   (multichannel)? 2:1, output_file, output_file);
+#endif 
+
+	/* XXX: if POSIXLY_CORRECT is set, getopt will stop parsing at the 
+					   * first non-option argument. In the case of ztmonitor, the 
+					   * first argument used to be a non-option (the channel), hence
+					   * we rely on a posix-incompatibility.
+	 * Workaround: in the presence of POSIXLY_CORRECT, use:
+					   * ztmonitor [options] <CHAN_NUM>
+	*/
+	
+	if (argv[optind])
+		chan_num =atoi(argv[optind]); 
+	if (chan_num < 1) {
+		fprintf(stderr,"Error: missing channel number\n");
 		usage();
 		exit(1);
 	}
-	for (i = 2; i < argc; ++i) {
-		if (!strcmp(argv[i], "-v")) {
-			if (visual)
-				verbose = 1;
-			visual = 1;
-			multichannel = 1;
-		} else if (!strcmp(argv[i], "-vv")) {
-			visual = 1;
-			verbose = 1;
-			multichannel = 1;
-		} else if ((!strcmp(argv[i], "-f") || !strcmp(argv[i], "-r") || !strcmp(argv[i], "-t")
-				|| !strcmp(argv[i], "-F") || !strcmp(argv[i], "-R") || !strcmp(argv[i], "-T"))
-				&& (i+1) < argc) {
-			/* Set which file descriptor to use */
-			if (!strcmp(argv[i], "-f")) {
-				savefile = 1;
-				x = 0;
-			} else if (!strcmp(argv[i], "-r")) {
-				savefile = 1;
-				multichannel = 1;
-				x = 0;
-			} else if (!strcmp(argv[i], "-t")) {
-				savefile = 1;
-				multichannel = 1;
-				x = 1;
-			} else if (!strcmp(argv[i], "-F")) {
-				savefile = 1;
-				preecho = 1;
-				x = 2;
-			} else if (!strcmp(argv[i], "-R")) {
-				savefile = 1;
-				multichannel = 1;
-				preecho = 1;
-				x = 2;
-			} else if (!strcmp(argv[i], "-T")) {
-				savefile = 1;
-				multichannel = 1;
-				preecho = 1;
-				x = 3;
-			} else
-				x = 0;
-
-			++i; /* we care about the file name */
-			if (strlen(argv[i]) < 255 ) {
-				strcpy(output_file, argv[i]);
-				fprintf(stderr, "Output to %s\n", output_file);
-				if ((ofh[x] = fopen(output_file, "w"))<0) {
-					fprintf(stderr, "Could not open %s for writing: %s\n", output_file, strerror(errno));
-					exit(1);
-				}
-				fprintf(stderr, "Run e.g., 'sox -r 8000 -s -w -c 1 %s file.wav' to convert.\n", output_file);
-			} else {
-				fprintf(stderr, "File Name %s too long\n",argv[i+1]);
-			}
-		} else if (!strcmp(argv[i], "-m")) {
-			multichannel = 1;
-		} else if (!strcmp(argv[i], "-o")) {
-			ossoutput = 1;
-		} else if (!strcmp(argv[i], "-p")) {
-			preecho = 1;
-		}
-	}
 
 	if (ossoutput) {
 		if (multichannel) {
-			printf("Multi-channel audio is enabled.  OSS output will be disabled.\n");
+			fprintf(stderr, "Multi-channel audio is enabled.  OSS output will be disabled.\n");
 			ossoutput = 0;
 		} else {
 			/* Open audio */
 			if ((afd = audio_open()) < 0) {
-				printf("Cannot open audio ...\n");
+				fprintf(stderr, "Cannot open audio ...\n");
 				ossoutput = 0;
 			}
 		}
 	}
-	if (!ossoutput && !multichannel && !savefile) {
-		fprintf(stderr, "Nothing to do with the stream(s) ...\n");
+	if (!ossoutput && !multichannel && !outfile_active) {
+		fprintf(stderr, "No output defined. Aborting.\n");
+		usage();
 		exit(1);
 	}
 
@@ -414,7 +419,7 @@
 	if (multichannel) {
 		memset(&zc, 0, sizeof(zc));
 		zc.chan = 0;
-		zc.confno = atoi(argv[1]);
+		zc.confno = chan_num;
 		/* Two pseudo's, one for tx, one for rx */
 		zc.confmode = ZT_CONF_MONITORTX;
 		if (ioctl(pfd[0], ZT_SETCONF, &zc) < 0) {
@@ -423,7 +428,7 @@
 		}
 		memset(&zc, 0, sizeof(zc));
 		zc.chan = 0;
-		zc.confno = atoi(argv[1]);
+		zc.confno = chan_num;
 		zc.confmode = ZT_CONF_MONITOR;
 		if (ioctl(pfd[1], ZT_SETCONF, &zc) < 0) {
 			fprintf(stderr, "Unable to monitor: %s\n", strerror(errno));
@@ -432,7 +437,7 @@
 		if (preecho) {
 			memset(&zc, 0, sizeof(zc));
 			zc.chan = 0;
-			zc.confno = atoi(argv[1]);
+			zc.confno = chan_num;
 			/* Two pseudo's, one for tx, one for rx */
 			zc.confmode = ZT_CONF_MONITOR_TX_PREECHO;
 			if (ioctl(pfd[2], ZT_SETCONF, &zc) < 0) {
@@ -441,7 +446,7 @@
 			}
 			memset(&zc, 0, sizeof(zc));
 			zc.chan = 0;
-			zc.confno = atoi(argv[1]);
+			zc.confno = chan_num;
 			zc.confmode = ZT_CONF_MONITOR_RX_PREECHO;
 			if (ioctl(pfd[3], ZT_SETCONF, &zc) < 0) {
 				fprintf(stderr, "Unable to monitor: %s\n", strerror(errno));
@@ -451,7 +456,7 @@
 	} else {
 		memset(&zc, 0, sizeof(zc));
 		zc.chan = 0;
-		zc.confno = atoi(argv[1]);
+		zc.confno = chan_num;
 		zc.confmode = ZT_CONF_MONITORBOTH;
 		if (ioctl(pfd[0], ZT_SETCONF, &zc) < 0) {
 			fprintf(stderr, "Unable to monitor: %s\n", strerror(errno));
@@ -460,7 +465,7 @@
 		if (preecho) {
 			memset(&zc, 0, sizeof(zc));
 			zc.chan = 0;
-			zc.confno = atoi(argv[1]);
+			zc.confno = chan_num;
 			zc.confmode = ZT_CONF_MONITORBOTH_PREECHO;
 			if (ioctl(pfd[2], ZT_SETCONF, &zc) < 0) {
 				fprintf(stderr, "Unable to monitor: %s\n", strerror(errno));
@@ -480,15 +485,15 @@
 		res = read(pfd[0], buf, sizeof(buf));
 		if (res < 1)
 			break;
-		if (ofh[0])
-			fwrite(buf, 1, res, ofh[0]);
+		if (NULL != outfile[ofh_def].fd)
+			fwrite(buf, 1, res, outfile[ofh_def].fd);
 
 		if (multichannel) {
 			res2 = read(pfd[1], buf2, res);
 			if (res2 < 1)
 				break;
-			if (ofh[1])
-				fwrite(buf2, 1, res2, ofh[1]);
+			if (NULL != outfile[ofh_tx].fd)
+				fwrite(buf2, 1, res2, outfile[ofh_tx].fd);
 
 			if (visual) {
 				if (res == res2)
@@ -502,23 +507,23 @@
 			res = read(pfd[2], buf, sizeof(buf));
 			if (res < 1)
 				break;
-			if (ofh[2])
-				fwrite(buf, 1, res, ofh[2]);
+			if (NULL != outfile[ofh_pre].fd)
+				fwrite(buf, 1, res, outfile[ofh_pre].fd);
 
 			if (multichannel) {
 				res2 = read(pfd[3], buf2, res);
 				if (res2 < 1)
 					break;
-				if (ofh[3])
-					fwrite(buf2, 1, res, ofh[3]);
-
-				/* XXX How are we going to visualize the preecho set of streams?
+				if (NULL != outfile[ofh_pretx].fd)
+					fwrite(buf2, 1, res, outfile[ofh_pretx].fd);
+
+	/* XXX How are we going to visualize the preecho set of streams?
 				if (visual) {
-					if (res == res2)
-						visualize((short *)buf, (short *)buf2, res/2);
-					else
-						printf("Huh?  res = %d, res2 = %d?\n", res, res2);
-				} */
+				if (res == res2)
+				visualize((short *)buf, (short *)buf2, res/2);
+				else
+				printf("Huh?  res = %d, res2 = %d?\n", res, res2);
+			} */
 			}
 		}
 
@@ -531,9 +536,10 @@
 				write(afd, buf, res);
 		}
 	}
-	if (ofh[0]) fclose(ofh[0]);
-	if (ofh[1]) fclose(ofh[1]);
-	if (ofh[2]) fclose(ofh[2]);
-	if (ofh[3]) fclose(ofh[3]);
+
+	for (x=0; x < 4; x++) 
+		if (0 < outfile[x].fd ) 
+			fclose(outfile[x].fd);
+	
 	exit(0);
 }




More information about the aadk-commits mailing list