コード例 #1
0
ファイル: shell.c プロジェクト: jimiszm/peos
void list(int argc, char *argv[])    
{
    char *type;

    if (argc < 2) {
        printf("usage: %s models|processes|actions\n", argv[0]);
	return;
    }
    type = argv[1];

    if (strncmp(type, "m", strlen("m")) == 0) {
	list_models();
    } 
    else if (strncmp(type, "p", strlen("p")) == 0) {
	list_instances();
    } 
    else if (strncmp(type, "a", strlen("a")) == 0) {
	list_actions();
    } 
    else {
	printf("usage: list models|processes|actions\n");
    }
}
コード例 #2
0
ファイル: rigctld.c プロジェクト: DF4OR/hamlib
int main (int argc, char *argv[])
{
	RIG *my_rig;		/* handle to rig (instance) */
	rig_model_t my_model = RIG_MODEL_DUMMY;

	int retcode;		/* generic return code from functions */

	int verbose = 0;
	int show_conf = 0;
	int dump_caps_opt = 0;
	const char *rig_file=NULL, *ptt_file=NULL, *dcd_file=NULL;
	ptt_type_t ptt_type = RIG_PTT_NONE;
	dcd_type_t dcd_type = RIG_DCD_NONE;
	int serial_rate = 0;
	char *civaddr = NULL;	/* NULL means no need to set conf */
	char conf_parms[MAXCONFLEN] = "";

	struct addrinfo hints, *result, *saved_result;
	int sock_listen;
	int reuseaddr = 1;
	char host[NI_MAXHOST];
	char serv[NI_MAXSERV];

	while(1) {
		int c;
		int option_index = 0;

		c = getopt_long (argc, argv, SHORT_OPTIONS,
			long_options, &option_index);
		if (c == -1)
			break;

		switch(c) {
			case 'h':
				usage();
				exit(0);
			case 'V':
				version();
				exit(0);
			case 'm':
				if (!optarg) {
						usage();	/* wrong arg count */
						exit(1);
				}
				my_model = atoi(optarg);
				break;
			case 'r':
				if (!optarg) {
						usage();	/* wrong arg count */
						exit(1);
				}
				rig_file = optarg;
				break;
			case 'p':
				if (!optarg) {
						usage();	/* wrong arg count */
						exit(1);
				}
				ptt_file = optarg;
				break;
			case 'd':
				if (!optarg) {
						usage();	/* wrong arg count */
						exit(1);
				}
				dcd_file = optarg;
				break;
			case 'P':
				if (!optarg) {
						usage();	/* wrong arg count */
						exit(1);
				}
				if (!strcmp(optarg, "RIG"))
					ptt_type = RIG_PTT_RIG;
				else if (!strcmp(optarg, "DTR"))
					ptt_type = RIG_PTT_SERIAL_DTR;
				else if (!strcmp(optarg, "RTS"))
					ptt_type = RIG_PTT_SERIAL_RTS;
				else if (!strcmp(optarg, "PARALLEL"))
					ptt_type = RIG_PTT_PARALLEL;
				else if (!strcmp(optarg, "CM108"))
					ptt_type = RIG_PTT_CM108;
				else if (!strcmp(optarg, "NONE"))
					ptt_type = RIG_PTT_NONE;
				else
					ptt_type = atoi(optarg);
				break;
			case 'D':
				if (!optarg) {
						usage();	/* wrong arg count */
						exit(1);
				}
				if (!strcmp(optarg, "RIG"))
					dcd_type = RIG_DCD_RIG;
				else if (!strcmp(optarg, "DSR"))
					dcd_type = RIG_DCD_SERIAL_DSR;
				else if (!strcmp(optarg, "CTS"))
					dcd_type = RIG_DCD_SERIAL_CTS;
				else if (!strcmp(optarg, "CD"))
					dcd_type = RIG_DCD_SERIAL_CAR;
				else if (!strcmp(optarg, "PARALLEL"))
					dcd_type = RIG_DCD_PARALLEL;
				else if (!strcmp(optarg, "NONE"))
					dcd_type = RIG_DCD_NONE;
				else
					dcd_type = atoi(optarg);
				break;
			case 'c':
				if (!optarg) {
						usage();	/* wrong arg count */
						exit(1);
				}
				civaddr = optarg;
				break;
			case 's':
				if (!optarg) {
						usage();	/* wrong arg count */
						exit(1);
				}
				serial_rate = atoi(optarg);
				break;
			case 'C':
				if (!optarg) {
						usage();	/* wrong arg count */
						exit(1);
				}
				if (*conf_parms != '\0')
						strcat(conf_parms, ",");
				strncat(conf_parms, optarg, MAXCONFLEN-strlen(conf_parms));
				break;
			case 't':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				portno = optarg;
				break;
			case 'T':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				src_addr = optarg;
				break;
			case 'o':
				vfo_mode++;
				break;
			case 'v':
				verbose++;
				break;
			case 'L':
				show_conf++;
				break;
			case 'l':
				list_models();
				exit(0);
			case 'u':
				dump_caps_opt++;
				break;
			default:
				usage();	/* unknown option? */
				exit(1);
		}
	}

  rig_set_debug(verbose);

	rig_debug(RIG_DEBUG_VERBOSE, "rigctld, %s\n", hamlib_version);
	rig_debug(RIG_DEBUG_VERBOSE, "Report bugs to "
			"<*****@*****.**>\n\n");

	my_rig = rig_init(my_model);

	if (!my_rig) {
		fprintf(stderr, "Unknown rig num %d, or initialization error.\n",
						my_model);
		fprintf(stderr, "Please check with --list option.\n");
		exit(2);
	}

	retcode = set_conf(my_rig, conf_parms);
	if (retcode != RIG_OK) {
		fprintf(stderr, "Config parameter error: %s\n", rigerror(retcode));
		exit(2);
	}

	if (rig_file)
		strncpy(my_rig->state.rigport.pathname, rig_file, FILPATHLEN - 1);

	/*
	 * ex: RIG_PTT_PARALLEL and /dev/parport0
	 */
	if (ptt_type != RIG_PTT_NONE)
		my_rig->state.pttport.type.ptt = ptt_type;
	if (dcd_type != RIG_DCD_NONE)
		my_rig->state.dcdport.type.dcd = dcd_type;
	if (ptt_file)
		strncpy(my_rig->state.pttport.pathname, ptt_file, FILPATHLEN - 1);
	if (dcd_file)
		strncpy(my_rig->state.dcdport.pathname, dcd_file, FILPATHLEN - 1);
	/* FIXME: bound checking and port type == serial */
	if (serial_rate != 0)
		my_rig->state.rigport.parm.serial.rate = serial_rate;
	if (civaddr)
        	rig_set_conf(my_rig, rig_token_lookup(my_rig, "civaddr"), civaddr);

	/*
	 * print out conf parameters
	 */
	if (show_conf) {
		rig_token_foreach(my_rig, print_conf_list, (rig_ptr_t)my_rig);
	}

	/*
	 * print out conf parameters, and exits immediately
	 * We may be interested only in only caps, and rig_open may fail.
	 */
	if (dump_caps_opt) {
		dumpcaps(my_rig, stdout);
		rig_cleanup(my_rig); /* if you care about memory */
		exit(0);
	}

	retcode = rig_open(my_rig);
	if (retcode != RIG_OK) {
	  	fprintf(stderr,"rig_open: error = %s \n", rigerror(retcode));
		exit(2);
	}

	if (verbose > 0)
		printf("Opened rig model %d, '%s'\n", my_rig->caps->rig_model,
				my_rig->caps->model_name);
	rig_debug(RIG_DEBUG_VERBOSE, "Backend version: %s, Status: %s\n",
			my_rig->caps->version, rig_strstatus(my_rig->caps->status));

#ifdef __MINGW32__
# ifndef SO_OPENTYPE
#  define SO_OPENTYPE     0x7008
# endif
# ifndef SO_SYNCHRONOUS_NONALERT
#  define SO_SYNCHRONOUS_NONALERT 0x20
# endif
# ifndef INVALID_SOCKET
#  define INVALID_SOCKET -1
# endif

    WSADATA wsadata;
    if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
	  	fprintf(stderr,"WSAStartup socket error\n");
		exit(1);
	}

	int sockopt = SO_SYNCHRONOUS_NONALERT;
	setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&sockopt, sizeof(sockopt));
#endif

	/*
	 * Prepare listening socket
	 */
	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
	hints.ai_socktype = SOCK_STREAM;/* TCP socket */
	hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
	hints.ai_protocol = 0;          /* Any protocol */

	retcode = getaddrinfo(src_addr, portno, &hints, &result);
	if (retcode != 0) {
	    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retcode));
	    exit(2);
	}
  saved_result = result;

	do
		{
			sock_listen = socket(result->ai_family, result->ai_socktype,
													 result->ai_protocol);
			if (sock_listen < 0) {
				handle_error (RIG_DEBUG_ERR, "socket");
				freeaddrinfo(saved_result);		/* No longer needed */
				exit(2);
			}

      if (setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR,
										 (char *)&reuseaddr, sizeof(reuseaddr)) < 0) {
				handle_error (RIG_DEBUG_ERR, "setsockopt");
				freeaddrinfo(saved_result);		/* No longer needed */
				exit (1);
			}

#ifdef __MINGW32__
			/* allow IPv4 mapped to IPv6 clients, MS default this to 1! */
			sockopt = 0;
      if (setsockopt(sock_listen, IPPROTO_IPV6, IPV6_V6ONLY,
										 (char *)&sockopt, sizeof(sockopt)) < 0) {
				handle_error (RIG_DEBUG_ERR, "setsockopt");
				freeaddrinfo(saved_result);		/* No longer needed */
				exit (1);
			}
#endif

      if (0 == bind(sock_listen, result->ai_addr, result->ai_addrlen)) {
				break;
			}
			handle_error (RIG_DEBUG_WARN, "binding failed (trying next interface)");
#ifdef __MINGW32__
			closesocket (sock_listen);
#else
			close (sock_listen);
#endif
		} while ((result = result->ai_next) != NULL);

	freeaddrinfo(saved_result);		/* No longer needed */
  if (NULL == result)
		{
			rig_debug(RIG_DEBUG_ERR, "bind error - no available interface\n");
			exit (1);
		}

	if (listen(sock_listen, 4) < 0) {
		handle_error (RIG_DEBUG_ERR, "listening");
		exit (1);
	}

	/*
	 * main loop accepting connections
	 */
	do {
#ifdef HAVE_PTHREAD
		pthread_t thread;
		pthread_attr_t attr;
#endif
		struct handle_data *arg;

		arg = malloc(sizeof(struct handle_data));
		if (!arg) {
			rig_debug(RIG_DEBUG_ERR, "malloc: %s\n", strerror(errno));
			exit (1);
		}

		arg->rig = my_rig;
		arg->clilen = sizeof (arg->cli_addr);
		arg->sock = accept(sock_listen, (struct sockaddr *)&arg->cli_addr, &arg->clilen);
		if (arg->sock < 0) {
			handle_error (RIG_DEBUG_ERR, "accept");
			break;
		}

		if ((retcode = getnameinfo ((struct sockaddr const *)&arg->cli_addr, arg->clilen, host, sizeof (host)
																, serv, sizeof (serv), NI_NOFQDN)) < 0)
			{
				rig_debug (RIG_DEBUG_WARN, "Peer lookup error: %s", gai_strerror (retcode));
			}
		rig_debug(RIG_DEBUG_VERBOSE, "Connection opened from %s:%s\n",
							host, serv);

#ifdef HAVE_PTHREAD
		pthread_attr_init(&attr);
		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

		retcode = pthread_create(&thread, &attr, handle_socket, arg);
		if (retcode != 0) {
			rig_debug(RIG_DEBUG_ERR, "pthread_create: %s\n", strerror(retcode));
			break;
		}
#else
		handle_socket(arg);
#endif
	}
	while (retcode == 0);

	rig_close(my_rig); /* close port */
	rig_cleanup(my_rig); /* if you care about memory */

#ifdef __MINGW32__
	WSACleanup();
#endif

	return 0;
}
コード例 #3
0
ファイル: rotctld.c プロジェクト: GomSpace/hamlib
int main (int argc, char *argv[])
{
	ROT *my_rot;		/* handle to rot (instance) */
	rot_model_t my_model = ROT_MODEL_DUMMY;

	int retcode;		/* generic return code from functions */

	int verbose = 0;
	int show_conf = 0;
	int dump_caps_opt = 0;
	const char *rot_file=NULL;
	int serial_rate = 0;
	char conf_parms[MAXCONFLEN] = "";

	struct addrinfo hints, *result;
	int sock_listen;
	int reuseaddr = 1;

	while(1) {
		int c;
		int option_index = 0;

		c = getopt_long (argc, argv, SHORT_OPTIONS,
			long_options, &option_index);
		if (c == -1)
			break;

		switch(c) {
			case 'h':
				usage();
				exit(0);
			case 'V':
				version();
				exit(0);
			case 'm':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				my_model = atoi(optarg);
				break;
			case 'r':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				rot_file = optarg;
				break;
			case 's':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				serial_rate = atoi(optarg);
				break;
			case 'C':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				if (*conf_parms != '\0')
					strcat(conf_parms, ",");
				strncat(conf_parms, optarg, MAXCONFLEN-strlen(conf_parms));
				break;
			case 't':
				if (!optarg) {
					usage();        /* wrong arg count */
					exit(1);
				}
				portno = optarg;
				break;
			case 'T':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				src_addr = optarg;
				break;
			case 'v':
				verbose++;
				break;
			case 'L':
				show_conf++;
				break;
			case 'l':
				list_models();
				exit(0);
			case 'u':
				dump_caps_opt++;
				break;
			default:
				usage();	/* unknown option? */
				exit(1);
		}
	}

	rig_set_debug(verbose);

	rig_debug(RIG_DEBUG_VERBOSE, "rotctld, %s\n", hamlib_version);
	rig_debug(RIG_DEBUG_VERBOSE, "Report bugs to "
			"<*****@*****.**>\n\n");

  	my_rot = rot_init(my_model);

	if (!my_rot) {
		fprintf(stderr, "Unknown rot num %d, or initialization error.\n",
						my_model);
		fprintf(stderr, "Please check with --list option.\n");
		exit(2);
	}

	retcode = set_conf(my_rot, conf_parms);
	if (retcode != RIG_OK) {
		fprintf(stderr, "Config parameter error: %s\n", rigerror(retcode));
		exit(2);
	}

	if (rot_file)
		strncpy(my_rot->state.rotport.pathname, rot_file, FILPATHLEN - 1);

	/* FIXME: bound checking and port type == serial */
	if (serial_rate != 0)
		my_rot->state.rotport.parm.serial.rate = serial_rate;

	/*
	 * print out conf parameters
	 */
	if (show_conf) {
		rot_token_foreach(my_rot, print_conf_list, (rig_ptr_t)my_rot);
	}

	/*
	 * print out conf parameters, and exits immediately
	 * We may be interested only in only caps, and rig_open may fail.
	 */
	if (dump_caps_opt) {
		dumpcaps_rot(my_rot, stdout);
		rot_cleanup(my_rot); /* if you care about memory */
		exit(0);
	}

	retcode = rot_open(my_rot);
	if (retcode != RIG_OK) {
	  	fprintf(stderr,"rot_open: error = %s \n", rigerror(retcode));
		exit(2);
	}

	if (verbose > 0)
		printf("Opened rot model %d, '%s'\n", my_rot->caps->rot_model,
						my_rot->caps->model_name);

	rig_debug(RIG_DEBUG_VERBOSE, "Backend version: %s, Status: %s\n",
			my_rot->caps->version, rig_strstatus(my_rot->caps->status));

#ifdef __MINGW32__
# ifndef SO_OPENTYPE
#  define SO_OPENTYPE     0x7008
# endif
# ifndef SO_SYNCHRONOUS_NONALERT
#  define SO_SYNCHRONOUS_NONALERT 0x20
# endif
# ifndef INVALID_SOCKET
#  define INVALID_SOCKET -1
# endif

	WSADATA wsadata;
	if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
		fprintf(stderr,"WSAStartup socket error\n");
		exit(1);
	}

	int sockopt = SO_SYNCHRONOUS_NONALERT;
	setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&sockopt, sizeof(sockopt));
#endif

	/*
	 * Prepare listening socket
	 */
	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
	hints.ai_socktype = SOCK_STREAM;/* TCP socket */
	hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
	hints.ai_protocol = 0;          /* Any protocol */

	retcode = getaddrinfo(src_addr, portno, &hints, &result);
	if (retcode != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retcode));
		exit(2);
	}

	sock_listen = socket(result->ai_family, result->ai_socktype,
			result->ai_protocol);
	if (sock_listen < 0)  {
		perror("ERROR opening socket");
		exit(1);
	}

	if (setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR,
				(char *)&reuseaddr,sizeof(reuseaddr)) < 0) {
		rig_debug(RIG_DEBUG_ERR, "setsockopt: %s\n", strerror(errno));
		exit (1);
	}
	if (bind(sock_listen, result->ai_addr, result->ai_addrlen) < 0) {
		rig_debug(RIG_DEBUG_ERR, "binding: %s\n", strerror(errno));
		exit (1);
	}

	freeaddrinfo(result);           /* No longer needed */

	if (listen(sock_listen,4) < 0) {
		rig_debug(RIG_DEBUG_ERR, "listening: %s\n", strerror(errno));
		exit (1);
	}

	/*
	 * main loop accepting connections
	 */
	do {
#ifdef HAVE_PTHREAD
		pthread_t thread;
		pthread_attr_t attr;
#endif
		struct handle_data *arg;

		arg = malloc(sizeof(struct handle_data));
		if (!arg) {
			rig_debug(RIG_DEBUG_ERR, "malloc: %s\n", strerror(errno));
			exit (1);
		}

		arg->rot = my_rot;
		arg->clilen = sizeof(arg->cli_addr);
		arg->sock = accept(sock_listen, (struct sockaddr *) &arg->cli_addr,
				&arg->clilen);
		if (arg->sock < 0) {
			rig_debug(RIG_DEBUG_ERR, "accept: %s\n", strerror(errno));
			break;
		}

		rig_debug(RIG_DEBUG_VERBOSE, "Connection opened from %s:%d\n",
				inet_ntoa(arg->cli_addr.sin_addr),
				ntohs(arg->cli_addr.sin_port));

#ifdef HAVE_PTHREAD
		pthread_attr_init(&attr);
		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

		retcode = pthread_create(&thread, &attr, handle_socket, arg);
		if (retcode != 0) {
			rig_debug(RIG_DEBUG_ERR, "pthread_create: %s\n", strerror(retcode));
			break;
		}
#else
		handle_socket(arg);
#endif
	}
	while (retcode == 0);

	rot_close(my_rot); /* close port */
	rot_cleanup(my_rot); /* if you care about memory */

#ifdef __MINGW32__
	WSACleanup();
#endif

	return 0;
}
コード例 #4
0
ファイル: rigctl.c プロジェクト: dh1tw/hamlib
int main (int argc, char *argv[])
{
	RIG *my_rig;		/* handle to rig (nstance) */
	rig_model_t my_model = RIG_MODEL_DUMMY;

	int retcode;		/* generic return code from functions */
	int exitcode;

	int verbose = 0;
	int show_conf = 0;
	int dump_caps_opt = 0;
#ifdef HAVE_READLINE_HISTORY
	int rd_hist = 0;
	int sv_hist = 0;
	const char *hist_dir = NULL;
	const char hist_file[] = "/.rigctl_history";
	char *hist_path = NULL;
	struct stat hist_dir_stat;
#endif
	const char *rig_file=NULL, *ptt_file=NULL, *dcd_file=NULL;
	ptt_type_t ptt_type = RIG_PTT_NONE;
	dcd_type_t dcd_type = RIG_DCD_NONE;
	int serial_rate = 0;
	char *civaddr = NULL;	/* NULL means no need to set conf */
	char conf_parms[MAXCONFLEN] = "";

	while(1) {
		int c;
		int option_index = 0;

		c = getopt_long (argc, argv, SHORT_OPTIONS HST_SHRT_OPTS,
			long_options, &option_index);
		if (c == -1)
			break;

		switch(c) {
			case 'h':
				usage();
				exit(0);
			case 'V':
				version();
				exit(0);
			case 'm':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				my_model = atoi(optarg);
				break;
			case 'r':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				rig_file = optarg;
				break;
			case 'p':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				ptt_file = optarg;
				break;
			case 'd':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				dcd_file = optarg;
				break;
			case 'P':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				if (!strcmp(optarg, "RIG"))
					ptt_type = RIG_PTT_RIG;
				else if (!strcmp(optarg, "DTR"))
					ptt_type = RIG_PTT_SERIAL_DTR;
				else if (!strcmp(optarg, "RTS"))
					ptt_type = RIG_PTT_SERIAL_RTS;
				else if (!strcmp(optarg, "PARALLEL"))
					ptt_type = RIG_PTT_PARALLEL;
				else if (!strcmp(optarg, "CM108"))
					ptt_type = RIG_PTT_CM108;
				else if (!strcmp(optarg, "GPIO"))
					ptt_type = RIG_PTT_GPIO;
				else if (!strcmp(optarg, "GPION"))
					ptt_type = RIG_PTT_GPION;
				else if (!strcmp(optarg, "NONE"))
					ptt_type = RIG_PTT_NONE;
				else
					ptt_type = atoi(optarg);
				break;
			case 'D':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				if (!strcmp(optarg, "RIG"))
					dcd_type = RIG_DCD_RIG;
				else if (!strcmp(optarg, "DSR"))
					dcd_type = RIG_DCD_SERIAL_DSR;
				else if (!strcmp(optarg, "CTS"))
					dcd_type = RIG_DCD_SERIAL_CTS;
				else if (!strcmp(optarg, "CD"))
					dcd_type = RIG_DCD_SERIAL_CAR;
				else if (!strcmp(optarg, "PARALLEL"))
					dcd_type = RIG_DCD_PARALLEL;
				else if (!strcmp(optarg, "CM108"))
					dcd_type = RIG_DCD_CM108;
				else if (!strcmp(optarg, "NONE"))
					dcd_type = RIG_DCD_NONE;
				else
					dcd_type = atoi(optarg);
				break;
			case 'c':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				civaddr = optarg;
				break;
			case 't':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				if (strlen(optarg) > 1)
					send_cmd_term = strtol(optarg, NULL, 0);
				else
					send_cmd_term = optarg[0];
				break;
			case 's':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				serial_rate = atoi(optarg);
				break;
			case 'C':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				if (*conf_parms != '\0')
					strcat(conf_parms, ",");
				strncat(conf_parms, optarg, MAXCONFLEN-strlen(conf_parms));
				break;
			case 'o':
				vfo_mode++;
				break;
		  case 'n':
			  rig_no_restore_ai();
        break;
#ifdef HAVE_READLINE_HISTORY
			case 'i':
				rd_hist++;
				break;
			case 'I':
				sv_hist++;
				break;
#endif
			case 'v':
				verbose++;
				break;
			case 'L':
				show_conf++;
				break;
			case 'l':
				rig_set_debug(verbose);
				list_models();
				exit(0);
			case 'u':
				dump_caps_opt++;
				break;
			default:
				usage();	/* unknown option? */
				exit(1);
		}
	}

	rig_set_debug(verbose);

	rig_debug(RIG_DEBUG_VERBOSE, "rigctl, %s\n", hamlib_version);
	rig_debug(RIG_DEBUG_VERBOSE, "Report bugs to "
			"<*****@*****.**>\n\n");

	/*
	 * at least one command on command line,
	 * disable interactive mode
	 */
	if (optind < argc)
		interactive = 0;

  	my_rig = rig_init(my_model);

	if (!my_rig) {
		fprintf(stderr, "Unknown rig num %d, or initialization error.\n",
						my_model);
		fprintf(stderr, "Please check with --list option.\n");
		exit(2);
	}

	retcode = set_conf(my_rig, conf_parms);
	if (retcode != RIG_OK) {
		fprintf(stderr, "Config parameter error: %s\n", rigerror(retcode));
		exit(2);
	}

	if (rig_file)
		strncpy(my_rig->state.rigport.pathname, rig_file, FILPATHLEN - 1);

	/*
	 * ex: RIG_PTT_PARALLEL and /dev/parport0
	 */
	if (ptt_type != RIG_PTT_NONE)
		my_rig->state.pttport.type.ptt = ptt_type;
	if (dcd_type != RIG_DCD_NONE)
		my_rig->state.dcdport.type.dcd = dcd_type;
	if (ptt_file)
		strncpy(my_rig->state.pttport.pathname, ptt_file, FILPATHLEN - 1);
	if (dcd_file)
		strncpy(my_rig->state.dcdport.pathname, dcd_file, FILPATHLEN - 1);
	/* FIXME: bound checking and port type == serial */
	if (serial_rate != 0)
		my_rig->state.rigport.parm.serial.rate = serial_rate;
	if (civaddr)
        	rig_set_conf(my_rig, rig_token_lookup(my_rig, "civaddr"), civaddr);

	/*
	 * print out conf parameters
	 */
	if (show_conf) {
		dumpconf(my_rig, stdout);
	}

	/*
	 * print out capabilities, and exists immediately
	 * We may be interested only in only caps, and rig_open may fail.
	 */
	if (dump_caps_opt) {
		dumpcaps(my_rig, stdout);
		rig_cleanup(my_rig); /* if you care about memory */
		exit(0);
	}

	retcode = rig_open(my_rig);
	if (retcode != RIG_OK) {
	  	fprintf(stderr,"rig_open: error = %s \n", rigerror(retcode));
		exit(2);
	}

	if (verbose > 0)
		printf("Opened rig model %d, '%s'\n", my_rig->caps->rig_model,
				my_rig->caps->model_name);
	rig_debug(RIG_DEBUG_VERBOSE, "Backend version: %s, Status: %s\n",
			my_rig->caps->version, rig_strstatus(my_rig->caps->status));

	exitcode = 0;

#ifdef HAVE_LIBREADLINE
	if (interactive && prompt && have_rl) {
		rl_readline_name = "rigctl";
#ifdef HAVE_READLINE_HISTORY
		using_history();	/* Initialize Readline History */

		if (rd_hist || sv_hist) {
			if (!(hist_dir = getenv("RIGCTL_HIST_DIR")))
				hist_dir = getenv("HOME");

			if (((stat(hist_dir, &hist_dir_stat) == -1) && (errno == ENOENT))
				|| !(S_ISDIR(hist_dir_stat.st_mode))) {
				fprintf(stderr, "Warning: %s is not a directory!\n", hist_dir);
			}

			hist_path = (char *)calloc((sizeof(char) * (strlen(hist_dir) + strlen(hist_file) + 1)), sizeof(char));

			strncpy(hist_path, hist_dir, strlen(hist_dir));
			strncat(hist_path, hist_file, strlen(hist_file));
		}

		if (rd_hist && hist_path)
			if (read_history(hist_path) == ENOENT)
				fprintf(stderr, "Warning: Could not read history from %s\n", hist_path);
#endif
	}
#endif	/* HAVE_LIBREADLINE */

	do {
		retcode = rigctl_parse(my_rig, stdin, stdout, argv, argc);
		if (retcode == 2)
			exitcode = 2;
	}
	while (retcode == 0 || retcode == 2);

#ifdef HAVE_LIBREADLINE
	if (interactive && prompt && have_rl) {
#ifdef HAVE_READLINE_HISTORY
		if (sv_hist && hist_path)
			if (write_history(hist_path) == ENOENT)
				fprintf(stderr, "\nWarning: Could not write history to %s\n", hist_path);

		if ((rd_hist || sv_hist) && hist_path) {
			free(hist_path);
			hist_path = (char *)NULL;
		}
#endif
	}
#endif
	rig_close(my_rig); /* close port */
	rig_cleanup(my_rig); /* if you care about memory */

	return exitcode;
}
コード例 #5
0
ファイル: rotctl.c プロジェクト: JohannesKlug/hamlib
int main (int argc, char *argv[])
{
	ROT *my_rot;		/* handle to rot (instance) */
	rot_model_t my_model = ROT_MODEL_DUMMY;

	int retcode;		/* generic return code from functions */
	int exitcode;

	int verbose = 0;
	int show_conf = 0;
	int dump_caps_opt = 0;
#ifdef HAVE_READLINE_HISTORY
	int rd_hist = 0;
	int sv_hist = 0;
	const char *hist_dir = NULL;
	const char hist_file[] = "/.rotctl_history";
	char *hist_path = NULL;
	struct stat hist_dir_stat;
#endif
	const char *rot_file=NULL;
	int serial_rate = 0;
	char conf_parms[MAXCONFLEN] = "";

	while(1) {
		int c;
		int option_index = 0;

		c = getopt_long (argc, argv, SHORT_OPTIONS HST_SHRT_OPTS,
			long_options, &option_index);
		if (c == -1)
			break;

		switch(c) {
			case 'h':
				usage();
				exit(0);
			case 'V':
				version();
				exit(0);
			case 'm':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				my_model = atoi(optarg);
				break;
			case 'r':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				rot_file = optarg;
				break;
			case 's':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				serial_rate = atoi(optarg);
				break;
			case 'C':
				if (!optarg) {
					usage();	/* wrong arg count */
					exit(1);
				}
				if (*conf_parms != '\0')
					strcat(conf_parms, ",");
				strncat(conf_parms, optarg, MAXCONFLEN-strlen(conf_parms));
				break;
			case 't':
				if (!optarg) {
					usage();        /* wrong arg count */
					exit(1);
				}
				if (strlen(optarg) > 1)
					send_cmd_term = strtol(optarg, NULL, 0);
				else
					send_cmd_term = optarg[0];
				break;
#ifdef HAVE_READLINE_HISTORY
			case 'i':
				rd_hist++;
				break;
			case 'I':
				sv_hist++;
				break;
#endif
			case 'v':
				verbose++;
				break;
			case 'L':
				show_conf++;
				break;
			case 'l':
				rig_set_debug(0);
				list_models();
				exit(0);
			case 'u':
				dump_caps_opt++;
				break;
			default:
				usage();	/* unknown option? */
				exit(1);
		}
	}

	rig_set_debug(verbose);

	rig_debug(RIG_DEBUG_VERBOSE, "rotctl, %s\n", hamlib_version);
	rig_debug(RIG_DEBUG_VERBOSE, "Report bugs to "
			"<*****@*****.**>\n\n");

	/*
	 * at least one command on command line,
	 * disable interactive mode
	 */
	if (optind < argc)
		interactive = 0;

  	my_rot = rot_init(my_model);

	if (!my_rot) {
		fprintf(stderr, "Unknown rot num %d, or initialization error.\n",
						my_model);
		fprintf(stderr, "Please check with --list option.\n");
		exit(2);
	}

	retcode = set_conf(my_rot, conf_parms);
	if (retcode != RIG_OK) {
		fprintf(stderr, "Config parameter error: %s\n", rigerror(retcode));
		exit(2);
	}

	if (rot_file)
		strncpy(my_rot->state.rotport.pathname, rot_file, FILPATHLEN - 1);

	/* FIXME: bound checking and port type == serial */
	if (serial_rate != 0)
		my_rot->state.rotport.parm.serial.rate = serial_rate;

	/*
	 * print out conf parameters
	 */
	if (show_conf) {
		rot_token_foreach(my_rot, print_conf_list, (rig_ptr_t)my_rot);
	}

    /*
     * print out capabilities, and exists immediately
     * We may be interested only in caps, and rig_open may fail.
     */
    if (dump_caps_opt) {
        dumpcaps_rot(my_rot, stdout);
        rot_cleanup(my_rot); /* if you care about memory */
        exit(0);
    }

	retcode = rot_open(my_rot);
	if (retcode != RIG_OK) {
	  	fprintf(stderr,"rot_open: error = %s \n", rigerror(retcode));
		exit(2);
	}

	if (verbose > 0)
		printf("Opened rot model %d, '%s'\n", my_rot->caps->rot_model,
						my_rot->caps->model_name);

	rig_debug(RIG_DEBUG_VERBOSE, "Backend version: %s, Status: %s\n",
			my_rot->caps->version, rig_strstatus(my_rot->caps->status));

	exitcode = 0;

#ifdef HAVE_LIBREADLINE
	if (interactive && prompt && have_rl) {
		rl_readline_name = "rotctl";
#ifdef HAVE_READLINE_HISTORY
		using_history();	/* Initialize Readline History */

		if (rd_hist || sv_hist) {
			if (!(hist_dir = getenv("ROTCTL_HIST_DIR")))
				hist_dir = getenv("HOME");

			if (((stat(hist_dir, &hist_dir_stat) == -1) && (errno == ENOENT))
				|| !(S_ISDIR(hist_dir_stat.st_mode))) {
				fprintf(stderr, "Warning: %s is not a directory!\n", hist_dir);
			}

			hist_path = (char *)calloc((sizeof(char) * (strlen(hist_dir) + strlen(hist_file) + 1)), sizeof(char));

			strncpy(hist_path, hist_dir, strlen(hist_dir));
			strncat(hist_path, hist_file, strlen(hist_file));
		}

		if (rd_hist && hist_path)
			if (read_history(hist_path) == ENOENT)
				fprintf(stderr, "Warning: Could not read history from %s\n", hist_path);
#endif
	}
#endif	/* HAVE_LIBREADLINE */

	do {
		retcode = rotctl_parse(my_rot, stdin, stdout, argv, argc);
		if (retcode == 2)
			exitcode = 2;
	}
	while (retcode == 0 || retcode == 2);

#ifdef HAVE_LIBREADLINE
	if (interactive && prompt && have_rl) {
#ifdef HAVE_READLINE_HISTORY
		if (sv_hist && hist_path)
			if (write_history(hist_path) == ENOENT)
				fprintf(stderr, "\nWarning: Could not write history to %s\n", hist_path);

		if ((rd_hist || sv_hist) && hist_path) {
			free(hist_path);
			hist_path = (char *)NULL;
		}
	}
#endif
#endif
	rot_close(my_rot); /* close port */
	rot_cleanup(my_rot); /* if you care about memory */

	return exitcode;
}