Example #1
0
/*
 * main()
 */
int
main(int ac, char *av[])
{
	struct ngm_connect ngc;
	const char *path = NULL;
	const char *hook = DEFAULT_HOOKNAME;
	int     csock, dsock;
	int     asciiFlag = 0;
	int     loopFlag = 0;
	int	noInput = 0;
	int	execFlag = 0;
	int	ch;

	if ((msgs = sl_init()) == NULL)
		err(EX_OSERR, NULL);

	/* Parse flags */
	while ((ch = getopt(ac, av, "aedlm:nsS")) != -1) {
		switch (ch) {
		case 'a':
			asciiFlag = 1;
			break;
		case 'd':
			NgSetDebug(NgSetDebug(-1) + 1);
			break;
		case 'e':
			execFlag = 1;
			break;
		case 'l':
			loopFlag = 1;
			break;
		case 'n':
			noInput = 1;
			break;
		case 'm':
			if (sl_add(msgs, optarg) == -1)
				err(EX_OSERR, NULL);
			break;
		case 's':
			outfd = STDIN_FILENO;
			break;
		case 'S':
			infd = STDOUT_FILENO;
			break;
		case '?':
		default:
			Usage();
		}
	}
	ac -= optind;
	av += optind;

	if (execFlag) {
		if (asciiFlag || loopFlag) {
			fprintf(stderr, "conflicting options\n");
			Usage();
		}
		if (ac < 3)
			Usage();
		path = av[0];
		hook = av[1];
		av += 2;
		ac -= 2;
	} else {
		/* Get params */
		switch (ac) {
		case 2:
			hook = av[1];
			/* FALLTHROUGH */
		case 1:
			path = av[0];
			break;
		default:
			Usage();
		}
	}

	/* Get sockets */
	if (NgMkSockNode(NULL, &csock, &dsock) < 0)
		errx(EX_OSERR, "can't get sockets");

	/* Connect socket node to specified node */
	snprintf(ngc.path, sizeof(ngc.path), "%s", path);
	snprintf(ngc.ourhook, sizeof(ngc.ourhook), NG_SOCK_HOOK_NAME);
	snprintf(ngc.peerhook, sizeof(ngc.peerhook), "%s", hook);

	if (NgSendMsg(csock, ".",
	    NGM_GENERIC_COOKIE, NGM_CONNECT, &ngc, sizeof(ngc)) < 0)
		errx(EX_OSERR, "can't connect to node");

	if (execFlag) {
		/* move dsock to fd 0 and 1 */
		(void)close(0);
		(void)close(1);
		if (!noInput)
			(void)dup2(dsock, 0);
		(void)dup2(dsock, 1);

		send_msgs(csock, path);

		/* try executing the program */
		(void)execv(av[0], av);
		err(EX_OSERR, "%s", av[0]);

	} else
		send_msgs(csock, path);

	/* Close standard input if not reading from it */
	if (noInput)
		fclose(stdin);

	/* Relay data */
	while (1) {
		fd_set  rfds;

		/* Setup bits */
		FD_ZERO(&rfds);
		if (!noInput)
			FD_SET(infd, &rfds);
		FD_SET(dsock, &rfds);

		/* Wait for something to happen */
		if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0)
			err(EX_OSERR, "select");

		/* Check data from socket */
		if (FD_ISSET(dsock, &rfds)) {
			char    buf[BUF_SIZE];
			int     rl, wl;

			/* Read packet from socket */
			if ((rl = NgRecvData(dsock,
			    buf, sizeof(buf), NULL)) < 0)
				err(EX_OSERR, "read(hook)");
			if (rl == 0)
				errx(EX_OSERR, "read EOF from hook?!");

			/* Write packet to stdout */
			if (asciiFlag)
				WriteAscii((u_char *) buf, rl);
			else if ((wl = write(outfd, buf, rl)) != rl) {
				if (wl < 0) {
					err(EX_OSERR, "write(stdout)");
				} else {
					errx(EX_OSERR,
					    "stdout: read %d, wrote %d",
					    rl, wl);
				}
			}
			/* Loopback */
			if (loopFlag) {
				if (NgSendData(dsock, NG_SOCK_HOOK_NAME, buf, rl) < 0)
					err(EX_OSERR, "write(hook)");
			}
		}

		/* Check data from stdin */
		if (FD_ISSET(infd, &rfds)) {
			char    buf[BUF_SIZE];
			int     rl;

			/* Read packet from stdin */
			if ((rl = read(infd, buf, sizeof(buf))) < 0)
				err(EX_OSERR, "read(stdin)");
			if (rl == 0)
				errx(EX_OSERR, "EOF(stdin)");

			/* Write packet to socket */
			if (NgSendData(dsock, NG_SOCK_HOOK_NAME, buf, rl) < 0)
				err(EX_OSERR, "write(hook)");
		}
	}
}
Example #2
0
Var* ff_write(vfuncptr func, Var* arg)
{

	Var* ob         = NULL;
	char* filename  = NULL;
	char* title     = NULL;
	char* type      = NULL;
	char* separator = NULL; /* for csv */
	int header      = 0;    /* for csv */
	int force       = 0;    /* Force file overwrite */
	int hdf_old     = 0;    // write hdf file backward like davinci used to
	unsigned short iom_type_idx, iom_type_found;

	Alist alist[9];
	alist[0]      = make_alist("object", ID_UNK, NULL, &ob);
	alist[1]      = make_alist("filename", ID_STRING, NULL, &filename);
	alist[2]      = make_alist("type", ID_ENUM, NULL, &type);
	alist[3]      = make_alist("title", ID_STRING, NULL, &title);
	alist[4]      = make_alist("force", DV_INT32, NULL, &force);
	alist[5]      = make_alist("separator", ID_STRING, NULL, &separator);
	alist[6]      = make_alist("header", DV_INT32, NULL, &header);
	alist[7]      = make_alist("hdf_old", DV_INT32, NULL, &hdf_old);
	alist[8].name = NULL;

	if (parse_args(func, arg, alist) == 0) return (NULL);

	/**
	** Make sure user specified an object
	**/
	if (ob == NULL) {
		parse_error("%s: No object specified.", func->name);
		return (NULL);
	}

	/**
	** get filename.  Verify type
	**/
	if (filename == NULL) {
		parse_error("No filename specified.");
		return (NULL);
	}
	filename = dv_locate_file(filename);

	if (type == NULL) {
		parse_error("No type specified.");
		return (NULL);
	}

	/*
	** Get title string
	*/
	if (title == NULL) {
		title = (char*)"DV data product";
	}

	/* Check type against list of types supported by iomedley. */

	iom_type_idx = iom_type_found = 0;

	while (iom_filetypes[iom_type_idx]) {
		if (!strcasecmp(type, iom_filetypes[iom_type_idx])) {
			iom_type_found = 1;
			break;
		}
		iom_type_idx++;
	}

	if (iom_type_found)
		dv_WriteIOM(ob, filename, type, force);
	else if (!strcasecmp(type, "raw"))
		dv_WriteRaw(ob, filename, force);
	else if (!strcasecmp(type, "vicar"))
		dv_WriteVicar(ob, filename, force);
	else if (!strcasecmp(type, "grd"))
		dv_WriteGRD(ob, filename, force, title, (char*)"davinci");
	/*    else if (!strcasecmp(type, "pnm"))    dv_WritePNM(ob, filename, force); */
	else if (!strcasecmp(type, "pgm"))
		dv_WritePGM(ob, filename, force);
	else if (!strcasecmp(type, "ppm"))
		dv_WritePPM(ob, filename, force);
	else if (!strcasecmp(type, "ascii"))
		WriteAscii(ob, filename, force);
	else if (!strcasecmp(type, "csv"))
		dv_WriteCSV(ob, filename, separator, header, force);
	else if (!strcasecmp(type, "ers"))
		dv_WriteERS(ob, filename, force);
	else if (!strcasecmp(type, "imath"))
		dv_WriteIMath(ob, filename, force);
	else if (!strcasecmp(type, "isis"))
		dv_WriteISIS(ob, filename, force, title);
	else if (!strcasecmp(type, "envi"))
		dv_WriteENVI(ob, filename, force);
	else if (!strcasecmp(type, "specpr")) {
		if (!force && file_exists(filename)) {
			parse_error("File %s already exists.\n", filename);
			return NULL;
		}
		WriteSpecpr(ob, filename, title);
	}

/*
** Below here are optional packages
*/
#ifdef HAVE_LIBHDF5
	else if (!strcasecmp(type, "hdf")) {
		struct stat statbuf;
		if (!force && !stat(filename, &statbuf)) {
			parse_error("File %s already exists.\n", filename);
			return NULL;
		}

		/* force ? */
		WriteHDF5(-1, filename, ob, hdf_old);
	}
#endif

#ifdef BUILD_MODULE_SUPPORT
	else if (iomod_handler_for_type(type))
		write_to_io_module(ob, filename, type, force);
#endif

#if 0
#ifdef HAVE_LIBMAGICK
	// else if (dvio_ValidGfx(type, GFX_type))    dv_WriteGFX_Image(ob, filename, force, GFX_type);
	else if (1) paramdvWriteImage(ob, filename, type, force);
#endif
#endif
	else {
		sprintf(error_buf, "Unrecognized type: %s", type);
		parse_error(NULL);
		return (NULL);
	}

	free(filename);

	return (NULL);
}