Пример #1
0
	/* load a list of connections */
	void load_conn(void)
	{
		int n1, n2;
		char c;

		/* if this is the first conn, create the graph */
		if (!nodes_fin) {
			g = gr_alloc(num_nodes);
			g->node2name = node2name;
		}
		nodes_fin = 1;

		/* load each n1-n2 pair */
		while(1) {
			eat_space();
			return_at_eol;
			n1 = token_node(0);
			c = fgetc(f);
			if (c != '-') {
				fprintf(stderr, "Error: expected '-' between nodes in conn list (got '%c' instead)\n", c);
				abort();
			}
			n2 = token_node(0);
			gr_add_(g, n1, n2, 1);
		}
	}
Пример #2
0
LispVal* lisp_read(FILE* f)
{
    eat_space(f);
    char c = peek(f);
    if (isdigit(c))
        return parse_number(f);
    else if (isalpha(c))
        return parse_atom(f);
    else if (c == '"')
        return parse_string(f);
    else if (c == '(')
        return parse_list(f);
    else
        return NULL;
}
Пример #3
0
	/* load a net1 or net2 statement */
	void load_net(int net)
	{
		int node;
		if (nodes_fin) {
			fprintf(stderr, "Error: net%d after conn\n", net);
			abort();
		}
		/* load each node and append */
		while(1) {
			eat_space();
			return_at_eol;
			node = token_node(1);
			nets[net][nn_net[net]] = node;
			nn_net[net]++;
		}
	}
Пример #4
0
	/* load a neut statement */
	void load_neut(void)
	{
		int node;
		if (nodes_fin) {
			fprintf(stderr, "Error: neut after conn\n");
			abort();
		}
		/* load each node and append */
		while(1) {
			eat_space();
			return_at_eol;
			node = token_node(1);
			neut[nn_neut] = node;
			nn_neut++;
		}
	}
Пример #5
0
Enigma *buildfromfile(const char *file)
{
	FILE *rc = fopen(file, "r");
	if (!rc)
		return 0;

	char buf[BUFSIZE], *p;
	char *patchboardconf = 0, *reflectorconf = 0;
	/* FIXME */
	int rotor_count = 0, alph_len = strlen(alphabet);
	char *rotor_permutations[10],
		 rotor_ticks[10],
		 rotor_stars[10];

	while ((p = fgets(buf, BUFSIZE - 1, rc))) {
		buf[strlen(buf) - 1] = 0;
		p = eat_space(p);

		if (*p == '#')
			continue;
		else if (strncmp(p, "alphabet", 8) == 0) {
			if (rotor_count || patchboardconf || reflectorconf ) {
				fprintf(stderr, _("Alphabet must be specified before all"
						" other components of the enigma\n"));
				return 0;
			}

			p = eat_space(p + 8);
			if (debug) {
				printf(_("Found alphabet: %s\n"), p);
			}
			alph_len = strlen(p);
			alphabet = malloc(sizeof(char) * alph_len);
			strncpy((char *)alphabet, p, alph_len);
		} else if (strncmp(p, "rotor", 5) == 0) {
			/* FIXME */
			if (rotor_count >= 10)
				continue;
			p = eat_space(p + 5);
			if (debug) {
				printf(_("Found rotor: %s\n"), p);
			}
			rotor_permutations[rotor_count] = malloc(sizeof(char) * alph_len);
			strncpy(rotor_permutations[rotor_count], p, alph_len);
			if (debug)
				printf(_("permutation is %s, "), rotor_permutations[rotor_count]);

			p = eat_space(p + alph_len);
			rotor_ticks[rotor_count] = *(p++);

			p = eat_space(p);
			rotor_stars[rotor_count] = *p;
			if (debug)
				printf(_("tick is %c, start is %c\n"), rotor_ticks[rotor_count],
						rotor_stars[rotor_count]);

			rotor_count++;
		} else if (strncmp(p, "reflector", 9) == 0) {
			if (reflectorconf) {
				fprintf(stderr, _("Duplicate reflector configuration\n"));
				return 0;
			}
			p = eat_space(p + 9);
			if (debug)
				printf(_("Found reflector: %s\n"), p);
			reflectorconf = malloc(sizeof(char) * alph_len);
			strncpy(reflectorconf, p, alph_len);
		} else if (strncmp(p, "patchboard", 10) == 0) {
			if (patchboardconf) {
				fprintf(stderr, _("Duplicate patchboard configuration\n"));
				return 0;
			}
			p = eat_space(p + 10);
			if (debug)
				printf(_("Found patchboard: %s\n"), p);
			patchboardconf = malloc(sizeof(char) * alph_len);
			strncpy(patchboardconf, p, alph_len);
		} else {
			fprintf(stderr, _("Illegal input in %s: %s"), file, buf);
			return 0;
		}
	}

	/* FIXME */
	if (!reflectorconf) {
		fprintf(stderr, _("Incomplete configuration, no reflector specified\n"));
		return 0;
	}

	/* FIXME */
	int i, *intperms[10], intticks[10], intstars[10];
	for (i = 0; i < rotor_count; i++) {
		intperms[i] = string_to_int(alphabet, rotor_permutations[i]);
		free(rotor_permutations[i]);

		intticks[i] = char_to_int(alphabet, rotor_ticks[i]);
		intstars[i] = char_to_int(alphabet, rotor_stars[i]);
	}
	int *intpatch = patchboardconf ? string_to_int(alphabet, patchboardconf) : 0;

	Enigma *e = enigma_create(intpatch,
			string_to_int(alphabet, reflectorconf), rotor_count, intperms,
			intticks, intstars);
	if (!e) {
		fprintf(stderr, _("Enigma creation failed\n"));
		return 0;
	}

	if (patchboardconf)
		free(patchboardconf);

	if (reflectorconf)
		free(reflectorconf);

	return e;
}