Example #1
0
/**
 * Programme principal
 */
int main(){
	srand(time(NULL));
	checkSignal();
	
	mainMenu(grid);

	return 0;
}
Example #2
0
int main(int argc, char* argv[], char* envp[])
{

  /* Command line args */
  if(argc < 2)
  {
    fprintf( stderr, "Usage:\t%s target [options]\n", argv[0] );
    return( -1 );
  }

  char* target = argv[1];
  
  /* Install the signal handler */
  checkSignal( signal(SIGHUP,	sigHandler));
  checkSignal( signal(SIGINT,	sigHandler));
  checkSignal( signal(SIGQUIT,	sigHandler));
  checkSignal( signal(SIGTERM,	sigHandler));
  checkSignal( signal(SIGUSR1,	sigHandler));
  checkSignal( signal(SIGUSR2,	sigHandler));
  checkSignal( signal(SIGPIPE,	SIG_IGN));
  checkSignal( signal(SIGALRM,	SIG_IGN));

  /* Fork */
  if (!(pid = fork()))
  {
    /* execute */
    execve(target, & argv[1], envp);
    fprintf( stderr, "Error executing target: %s", target );
    return RC_EXEC;
  }

  /* Wait for the child to exit then return. (Or we exit from signal handler.) */
  int status;
  wait(&status);
  return status;

}
Example #3
0
int main(int argc, char** argv)
{
	init_sig_handling();
	atexit(cleanup);

	// Get arguments
	long speed = DEFAULT_SPEED;
	char *devpath = NULL;
	char *filepath = NULL;
	int noisy = 1;
	int verbose = 0;
	int compress = 0;
	int interactive = isatty(STDIN_FILENO);
	unsigned max_unconfirmed = 0;
	{
		int opt;
		while ((opt = getopt(argc, argv, "h?qvcs:u:f:")) >= 0) {
			switch(opt) {
			case 's':			/* Speed */
				speed = strtol(optarg, NULL, 10);
				break;

			case 'f':
				filepath = optarg;
				break;

			case 'u':
				max_unconfirmed = strtol(optarg, NULL, 10);
				break;

			case 'q':			/* Quiet */
				noisy = 0;
				break;

			case 'v':			/* Verbose */
				verbose = 1;
				break;

			case 'c':
				compress = 1;
				break;

			case '?':			/* Help */
			case 'h':
				usage(argc, argv);
				fprintf(stderr, HELP);
				exit(EXIT_SUCCESS);
				break;

				

			default:
				break;
			}
		}
		switch(argc - optind) {

		case 1:
			devpath = argv[optind];
			break;

		case 0:
			if(noisy) {
				printf("Guessing a likely USB serial device...\n");
			}
			devpath = guessSerial();
			if(devpath == NULL) {
				fprintf(stderr, "Unable to autodetect any USB serial devices; if you are certain the device is available, please manually specify the path.\n");
				usage(argc, argv);
				exit(EXIT_FAILURE);
			}
			break;

		default:
			fprintf(stderr, "Too many arguments!\n");
			usage(argc, argv);
			exit(EXIT_FAILURE);
		}
		
		if(filepath == NULL) {
			filepath = "-";
		}
	}
	if(noisy) {
		printf("Serial device:\t%s\n", devpath);
		printf("Line speed:\t%ld\n", speed);
		printf("Gcode file:\t%s\n", filepath);
	}

	/* Open FDs */
	serial = serial_open(devpath, speed);
	if(serial == NULL) {
		fprintf(stderr, "Error opening serial device %s: %s\n", devpath, serial_strerror(serial_errno));
		exit(EXIT_FAILURE);
	}

	if(strncmp("-", filepath, 1) == 0) {
		if(noisy) {
			printf("Will read gcode from standard input");
			if(interactive) {
				printf("; enter Ctrl-D (EOF) to finish.");
			}
			printf("\n");
		}
		/* input defaults to stdin */
	} else {
		input = open(filepath, O_RDONLY);
		if(input < 0) {
			fprintf(stderr, "Unable to open gcode file \"%s\": %s\n", filepath, strerror(errno));
			exit(EXIT_FAILURE);
		}
		interactive = 0;
	}

#ifdef UNIX
	struct pollfd fds[FD_COUNT];
	fds[FD_INPUT].fd = input;
	fds[FD_INPUT].events = POLLIN;
	fds[FD_SERIAL].fd = serial->handle;
	fds[FD_SERIAL].events = POLLIN;
#endif

	char serialbuf[SERIAL_BUFSIZE];
	char gcodebuf[GCODE_BUFSIZE];
	int ret = 0;
	int confpoint = 0;				/* N chars of CONFIRM_MSG found. */
	int startpoint = 0;				/* N chars of START_MSG found. */
	size_t len;
	size_t gcpoint = 0;
	int gccomment = 0;
	unsigned unconfirmed = 0;
	int inputdone = 0;
	while(1) {
		debug("Polling...");
#ifdef UNIX
		if(inputdone) {
			if(unconfirmed == 0) {
				debug("Last message confirmed, exiting.");
				exit(EXIT_SUCCESS);
			}
			ret = poll(&fds[FD_SERIAL], 1, -1);
		} else {
			ret = poll(fds, FD_COUNT, -1);
		}
#elif WINDOWS
#error TODO: Modify windows code to poll stdin as well as serial.
		switch(WaitForMultipleObjects(1, &(serial->handle), FALSE, -1)) {
		case WAIT_FAILED:
			/* TODO: Get error */
			ret = -1;
			break;

		case WAIT_TIMEOUT:
			ret = 0;
			break;

		case WAIT_OBJECT_0:
			ret = 1;
			break;

		default:
			break;
		}
				
#endif
				
		if(ret < 0) {
			checkSignal();
			fprintf(stderr, "Error during poll: %s\n", strerror(errno));
			fprintf(stderr, "Giving up.\n");
			exit(EXIT_FAILURE);
		}

		/* TODO: Windows */
		/* FIXME: Why are replies shifted forwards by one? */
		if(fds[FD_SERIAL].revents & POLLIN) {
			/* We've got reply data! */
			debug("Got serial.");
			len = serial_read(serial, serialbuf, sizeof(serialbuf)-1);
			
			if(verbose || interactive) {
				fwrite(serialbuf, sizeof(char), len, stdout);
				fflush(stdout);
			}
			
			/* Scan for confirmation message */
			int i;
			for(i = 0; i < len; i++) {
				if(serialbuf[i] == CONFIRM_MSG[confpoint]) {
					confpoint++;
					if(confpoint >= strlen(CONFIRM_MSG)) {
						debug("Message receipt confirmed!");
						if(unconfirmed > 0) { /* Sanity check */
							unconfirmed--;
						}
						/* Got confirmation, resume polling for and sending gcode. */
						fds[FD_INPUT].events = POLLIN;
						/* Clear gcode buffer for next message */
						gcpoint = 0;
						
						confpoint = 0;
					}
				} else {
					confpoint = 0;
				}
				
				if(serialbuf[i] == START_MSG[startpoint]) {
					startpoint++;
					if(startpoint >= strlen(START_MSG) && unconfirmed) {
						debug("Machine was reset.");
						/* Machine just started, and thus hasn't been
						 * listening, so we have to resend the last block. */
						serial_write(serial, gcodebuf, gcpoint);
						serial_write(serial, "\r\n", 2);
						debug("Resent last block.");
					}
				} else {
					startpoint = 0;
				}
			}
		}

		/* TODO: Windows */
		/* POLLHUP handles some stdin EOF situations missed by POLLIN */
		if(fds[FD_INPUT].revents & POLLIN || fds[FD_INPUT].revents & POLLHUP) {
			/* We've got input data! */
			int ret;
			char ch;
			ret = read(fds[FD_INPUT].fd, &ch, 1);
			
			if(ret == 0) {
				/* We're at EOF */
				debug("Got EOF; input complete.\n");
				fds[FD_INPUT].events = 0;
				inputdone = 1;
				continue;
			} else if(ret < 0) {
				/* Something went wrong */
				checkSignal();
				fprintf(stderr, "Error reading gcode: %s\n", strerror(errno));
				fprintf(stderr, "Giving up.\n");
				exit(EXIT_FAILURE);
			}
				
			switch(ch) {
			case '\r':
			case '\n':
				{
					gccomment = 0;
					if(gcpoint == 0) {
						break;
					}

					serial_write(serial, gcodebuf, gcpoint);
					serial_write(serial, "\r\n", 2);
					unconfirmed++;
					
					debug("Sent complete block.");

					if(verbose && !interactive) {
						fwrite(gcodebuf, sizeof(char), gcpoint, stdout);
						printf("\n");
					}

					if(unconfirmed > max_unconfirmed) {
						/* Stop polling input until we have some confirmation */
						fds[FD_INPUT].events = 0;
					}

					break;
				}

			case ';':
				if(compress) {
					gccomment = 1;
				}
			case ' ':
			case '\t':
				if(compress) {
					break;
				}
			default:
				if(!gccomment) {
					gcodebuf[gcpoint++] = ch;
				}
				break;
			}
		}
	}

	if(noisy) {
		printf("Successfully completed!\n");
	}

	exit(EXIT_SUCCESS);
}
Example #4
0
int main()
{
    {
        // Check the default constructor
        EntityRef ref;
    }

    {
        // Check the default constructor initialises to NULL via get
        EntityRef ref;

        assert(ref.get() == 0);
    }

    {
        // Check the default constructor initialises to NULL via dereference
        EntityRef ref;

        assert(&(*ref) == 0);
    }

    {
        // Check the default constructor initialises to NULL via ->
        EntityRef ref;

        assert(ref.operator->() == 0);
    }

    {
        // Check the default constructor initialises to NULL via ==
        EntityRef ref;

        assert(ref == 0);
    }

    {
        // Check the initialising constructor via get
        Entity * e = new Entity("1", 1);
        EntityRef ref(e);

        assert(ref.get() == e);
    }

    {
        // Check the initialising constructor via dereference
        Entity * e = new Entity("1", 1);
        EntityRef ref(e);

        assert(&(*ref) == e);
    }

    {
        // Check the initialising constructor via ->
        Entity * e = new Entity("1", 1);
        EntityRef ref(e);

        assert(ref.operator->() == e);
    }

    {
        // Check the initialising constructor via ==
        Entity * e = new Entity("1", 1);
        EntityRef ref(e);

        assert(ref == e);
    }

    {
        // Check the copy constructor
        Entity * e = new Entity("1", 1);
        EntityRef ref(e);
        EntityRef ref2(ref);

        assert(ref2.get() == e);
    }

    {
        // Check the comparison operator
        Entity * e = new Entity("1", 1);
        EntityRef ref(e);
        EntityRef ref2(e);

        assert(ref == ref2);
    }

    {
        // Check the comparison operator
        Entity * e = new Entity("1", 1);
        Entity * e2 = new Entity("2", 2);
        EntityRef ref(e);
        EntityRef ref2(e2);

        assert(!(ref == ref2));
    }

#if 0
    // These tests should be included should we add operator!=
    {
        // Check the comparison operator
        Entity e("1", 1);
        EntityRef ref(&e);
        EntityRef ref2(&e);

        assert(!(ref != ref2));
    }

    {
        // Check the comparison operator
        Entity e("1", 1);
        Entity e2("2", 2);
        EntityRef ref(&e);
        EntityRef ref2(&e2);

        assert(ref != ref2);
    }
#endif

    {
        // Check the less than operator
        Entity * e = new Entity("1", 1);
        EntityRef ref(e);
        EntityRef ref2(e);

        assert(!(ref < ref2) && !(ref2 < ref));
    }

    {
        // Check the less than operator
        Entity * e = new Entity("1", 1);
        Entity * e2 = new Entity("2", 2);
        EntityRef ref(e);
        EntityRef ref2(e2);

        assert(ref < ref2 || ref2 < ref);
    }

    {
        // Check the assignment operator
        Entity * e = new Entity("1", 1);
        EntityRef ref;

        ref = EntityRef(e);

        assert(ref.get() == e);
    }

    {
        // Check that destroying the Entity makes the reference null.
        Entity e("1", 1);
        Entity * container = new Entity("2", 2);

        // Set the location of the entity being tested, as destroy requires it.
        e.m_location.m_loc = container;
        // Make sure the container has a contains structure, as destroy
        // requires it.
        container->m_contains = new LocatedEntitySet;
        // Increment the refcount on the container, else the tested Entity's
        // destructor will delete it.
        container->incRef();

        EntityRef ref(&e);

        assert(ref.get() == &e);
        e.destroy();
        assert(ref.get() == 0);
    }

    checkSignal();
}