changeset 119:056616f7e8ab

sip-manual-out: convert command line to getopt style
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 29 Sep 2022 14:03:06 -0800
parents a4450ae8fd09
children c62d0f28da6f
files sip-manual-out/main.c
diffstat 1 files changed, 42 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/sip-manual-out/main.c	Thu Sep 29 13:03:27 2022 -0800
+++ b/sip-manual-out/main.c	Thu Sep 29 14:03:06 2022 -0800
@@ -58,32 +58,54 @@
 	sip_tx_packet(&msg, &sip_dest_sin);
 }
 
+static void
+preliminary_proc(argc, argv)
+	char **argv;
+{
+	extern int optind;
+	extern char *optarg;
+	char *logfile;
+	int opt, rc;
+
+	logfile = 0;
+	while ((opt = getopt(argc, argv, "l:")) != EOF) {
+		switch (opt) {
+		case 'l':
+			logfile = optarg;
+			continue;
+		default:
+		usage:
+			fprintf(stderr,
+			"usage: %s [options] dest-conf from-num to-num\n",
+				argv[0]);
+			exit(1);
+		}
+	}
+	if (argc != optind + 3)
+		goto usage;
+	read_config_file(argv[optind]);
+	open_sip_udp_socket();
+	obtain_dummy_rtp();
+	sip_dest_sin.sin_family = AF_INET;
+	sip_dest_sin.sin_addr = sip_dest_ip;
+	sip_dest_sin.sin_port = htons(sip_dest_port);
+	sprintf(from_uri, "<sip:%s@%s>;tag=out%u", argv[optind+1],
+		inet_ntoa(sip_bind_ip), ntohs(dummy_rtp_endp.sin_port));
+	sprintf(to_uri, "sip:%s@%s", argv[optind+2], sip_dest_domain);
+	if (logfile) {
+		rc = open_sip_log_file(logfile);
+		if (rc < 0)
+			exit(1);	/* error msg already printed */
+	}
+}
+
 main(argc, argv)
 	char **argv;
 {
 	fd_set fds;
 	int rc;
 
-	if (argc < 4 || argc > 5) {
-		fprintf(stderr,
-			"usage: %s dest-conf from-num to-num [logfile]\n",
-			argv[0]);
-		exit(1);
-	}
-	read_config_file(argv[1]);
-	open_sip_udp_socket();
-	obtain_dummy_rtp();
-	sip_dest_sin.sin_family = AF_INET;
-	sip_dest_sin.sin_addr = sip_dest_ip;
-	sip_dest_sin.sin_port = htons(sip_dest_port);
-	sprintf(from_uri, "<sip:%s@%s>;tag=out%u", argv[2],
-		inet_ntoa(sip_bind_ip), ntohs(dummy_rtp_endp.sin_port));
-	sprintf(to_uri, "sip:%s@%s", argv[3], sip_dest_domain);
-	if (argv[4]) {
-		rc = open_sip_log_file(argv[4]);
-		if (rc < 0)
-			exit(1);	/* error msg already printed */
-	}
+	preliminary_proc(argc, argv);
 	gettimeofday(&cur_event_time, 0);
 	sprintf(call_id, "%08u_%u@%s",
 		(unsigned)(cur_event_time.tv_sec % 100000000),