Esempio n. 1
0
static int spellignore(const char *word)
{
char	buf[100];
const char *c;
char	*p, *q;
FILE	*fp=opendict("r");

	if (!fp)	return (0);
	while (fgets(buf, sizeof(buf), fp) != NULL)
	{
		if ((p=strchr(buf, '\n')) != 0)	*p=0;
		if (strcmp(word, buf) == 0)
		{
			fclose(fp);
			return (1);
		}
	}
	fclose(fp);

	c=cgi("globignore");

	p=malloc(strlen(c)+1);
	if (!p)	enomem();
	strcpy(p, c);

	for (q=p; (q=strtok(q, ":")) != 0; q=0)
		if (strcmp(q, word) == 0)
		{
			free(p);
			return (1);
		}

	return (0);
}
Esempio n. 2
0
static void spelladd(const char *word)
{
FILE	*fp=opendict("a");

	if (fp)
	{
		fprintf(fp, "%s\n", word);
		fclose(fp);
	}
}
Esempio n. 3
0
void
search(const char *query, const char *dictfile, void (*callback)(const char *, size_t)) {
    if (!dict) {
        dict = opendict(dictfile, raiseError);
        if (!dict) {
            return;
        }
    }

    const char *p = dict;
    while (p < dict + len) {
        const char *line = p;
        int endofline = 0;

        while (p < dict + len && !endofline) {
            const char *q = query;

            /* match as much of the query as possible */
            while (*p && *q && *p == *q) {
                p++;
                q++;
            }

            /* Matched if we're at the end of the search term AND
             * at the end of a word in the dictionary
             */
            if (*q == 0 && (*p == ',' || *p == '\n')) {
                while (*p != '\n') {
                    p++;
                }
                callback(line, p - line);
                continue;
            }

            /* Not a full match - skip to next word */
            while (*p != ',' && *p != '\n') {
                p++;
            }

            if (*p == '\n') {
                endofline = 1;
            }

            p++;
        }
    }

    return;
}
Esempio n. 4
0
int
main(int argc, char *argv[])
{
	int ch, done;
	char *bspec, *p, *seed;

	batch = debug = reuse = selfuse;
	bspec = seed = NULL;
	minlength = -1;
	tlimit = 180;		/* 3 minutes is standard */

	while ((ch = getopt(argc, argv, "Bbcds:t:w:")) != -1)
		switch(ch) {
		case 'B':
			grid = 5;
			break;
		case 'b':
			batch = 1;
			break;
		case 'c':
			challenge = 1;
			break;
		case 'd':
			debug = 1;
			break;
		case 's':
			seed = optarg;
			break;
		case 't':
			if ((tlimit = atoi(optarg)) < 1)
				errx(1, "bad time limit");
			break;
		case 'w':
			if ((minlength = atoi(optarg)) < 3)
				errx(1, "min word length must be > 2");
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	/* process final arguments */
	if (argc > 0) {
		if (strcmp(argv[0], "+") == 0)
			reuse = 1;
		else if (strcmp(argv[0], "++") == 0)
			selfuse = 1;
	}

	if (reuse || selfuse) {
		argc -= 1;
		argv += 1;
	}

	if (argc > 0) {
		if (islower(argv[0][0])) {
			if (strlen(argv[0]) != ncubes) {
				usage();
			} else {
				/* This board is assumed to be valid... */
				bspec = argv[0];
			}
		} else {
		  	usage();
		}
	}

	if (batch && bspec == NULL)
		errx(1, "must give both -b and a board setup");

	init();
	if (batch) {
		newgame(bspec);
		while ((p = batchword(stdin)) != NULL)
			(void) printf("%s\n", p);
		exit(0);
	}
	setup(seed);
	prompt("Loading the dictionary...");
	if ((dictfp = opendict(DICT)) == NULL) {
		warn("%s", DICT);
		cleanup();
		exit(1);
	}
#ifdef LOADDICT
	if (loaddict(dictfp) < 0) {
		warnx("can't load %s", DICT);
		cleanup();
		exit(1);
	}
	(void)fclose(dictfp);
	dictfp = NULL;
#endif
	if (loadindex(DICTINDEX) < 0) {
		warnx("can't load %s", DICTINDEX);
		cleanup();
		exit(1);
	}

	prompt("Type <space> to begin...");
	while (inputch() != ' ');

	for (done = 0; !done;) {
		newgame(bspec);
		bspec = NULL;	/* reset for subsequent games */
		playgame();
		prompt("Type <space> to continue, any cap to quit...");
		delay(10);	/* wait for user to quit typing */
		flushin(stdin);
		for (;;) {
			ch = inputch();
			if (ch == '\033')
				findword();
			else if (ch == '\014' || ch == '\022')	/* ^l or ^r */
				redraw();
			else {
				if (isupper(ch)) {
					done = 1;
					break;
				}
				if (ch == ' ')
					break;
			}
		}
	}
	cleanup();
	exit(0);
}
Esempio n. 5
0
void
exec_opendict()
{
	char	filename[MAXPATHLEN];
	char	password[PASSWORDLEN];
	DICT	*dict;
	DICTL   *dictl, *dltp, *tmp;

	get_nstring(filename, sizeof(filename));
	get_nstring(password, sizeof(password));

	if (!work_base)
		longjmp(error_ret, SJ3_NotConnected);

	if (make_full_path(filename, sizeof(filename)))
		longjmp(error_ret, SJ3_TooLongParameter);

	if (!(dict = (DICT *)opendict(filename, password)))
		longjmp(error_ret, serv_errno);
	if (!dictlist && global_work_base) {
		DICTL *b_dictl = NULL;

		dltp = global_work_base -> Jdictlist;
		dictl = NULL;
		while (dltp) {
			dictl = (DICTL *)malloc(sizeof *dictl);
			if (!dictl) {
				for (tmp = dictlist; tmp; tmp->next)
					free(tmp);
				dictlist = NULL;
			}
			if (!dictlist)
				dictlist = dictl;
			dictl->dict = dltp->dict;
			((DictFile *)(dictl -> dict))->refcnt++;
			dictl->next = NULL;
			if (b_dictl)
				b_dictl->next = dictl;
			b_dictl = dictl;
			dltp = dltp->next;
		}
	}
	for (dltp = dictlist; dltp && dltp->dict != dict; dltp = dltp->next) ;

	if (!dltp) {
		if (!(dictl = (DICTL *)malloc(sizeof *dictl))) {
			closedict((DictFile *)dict);
			longjmp(error_ret, SJ3_NotEnoughMemory);
		}

		dictl -> dict = dict;
		dictl -> next = dictlist;
		dictlist = dictl;
	}

	if (cur_cli -> work -> lock == cur_cli -> fd) {
		if (dict -> maxunit != 0)
			lock_dict((DictFile *)dict, cur_cli -> fd);
	}

	put_int(SJ3_NormalEnd);
	put_int((int)dict->dicid);
}
Esempio n. 6
0
int main(int argc, char *argv[])
{
    //Game vars
    int playerfd[2];
    char playerid = '1';
    char board[BSIZE][BSIZE];
    char bufrack[8];
    int x, y, num;
    char bufx[3], bufy[3];
    int bag[27] = {9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1, 2};
    char **dict;
    char word[MAXWORDSIZE];
    char bufout[MAXDATASIZE];

    dict = opendict(DICT);

    srandom(time(NULL));

    clearsqarray(BSIZE, board);

    int listens, newsock, sin_size, bigfd, numbytes, i, j;
    char buffer[MAXDATASIZE];
    struct sockaddr_in localaddr, newaddr, getaddr;
    fd_set master, tempfd;
    int yes = 1;

    listens = socket(PF_INET, SOCK_STREAM, 0);
    setsockopt(listens, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

    localaddr.sin_family = AF_INET;
    localaddr.sin_port = htons(PORT);
    localaddr.sin_addr.s_addr = INADDR_ANY;
    memset(&(localaddr.sin_zero), '\0', sizeof(localaddr.sin_zero));

    bind(listens, (struct sockaddr *) &localaddr, sizeof(localaddr));
    listen(listens, 10);

    FD_ZERO(&master);
    FD_ZERO(&tempfd);

    FD_SET(listens, &master);

    bigfd = listens;

    for(;;)
    {
        tempfd = master;

        select(bigfd + 1, &tempfd, NULL, NULL, NULL);

        for(i = 0; i <= bigfd; i++)
        {
            if(FD_ISSET(i, &tempfd))
            {
                if(i == listens)
                {
                    sin_size = sizeof(newaddr);
                    newsock = accept(listens, (struct sockaddr *) &newaddr, &sin_size);
                    
                    FD_SET(newsock, &master);
                    if(newsock > bigfd) 
                        bigfd = newsock;

                    printf("Connection from: %s Socket: %i\n", inet_ntoa(newaddr.sin_addr), newsock);

                    //playerid stuff
                    if(atoi(&playerid) < 2)
                        playerfd[atoi(&playerid) - 1] = newsock;

                    send(newsock, &playerid, sizeof(char), 0);
                    playerid++;

                    //send rack
                    randomrack(bufrack, bag, RACKSIZE);
                    send(newsock, bufrack, strlen(bufrack), 0);
                }
                else
                {
                    if((numbytes = recv(i, buffer, sizeof(buffer), 0)) == 0)
                    {
                        printf("Connection closed: socket %i\n", i);
                        
                        if(playerid == '3' || playerid == '1')
                            playerid = '1';
                        else
                            playerid--;

                        close(i);
                        FD_CLR(i, &master);
                    }
                    else if(numbytes < 0)
                    {
                        perror("recv");
                    }
                    else
                    {
                        //Data handling
                        getpeername(i, (struct sockaddr *) &getaddr, &sin_size);

                        if(buffer[0] != 'c' && buffer [0] != 'r' && buffer[0] != 'p')
                            printf("DATA From: %s Socket: %i Data: %s\n", inet_ntoa(getaddr.sin_addr), i, buffer);

                        send(i, "ACK", 4, 0);

                        if(strcmp(buffer, "q") == 0)
                            exit(0);

                        switch(buffer[0])
                        {
                            case 'c':
                                printf("CHAT From: %s Player: %i Message: %s\n", inet_ntoa(getaddr.sin_addr), i - 3, buffer + 1);

                                for(j = 0; j <= bigfd; j++)
                                    if(FD_ISSET(j, &master))
                                        if(j != listens && j != i)
                                            send(j, buffer, numbytes, 0);
                                break;
                            case 'r':
                                num = atoi(buffer + 1);
                                memset(bufrack, '\0', sizeof(bufrack));
                                randomrack(bufrack, bag, num);

                                sprintf(bufout, "r%s", bufrack);
                                send(i, bufout, strlen(bufout), 0);

                                printf("RACK From: %s Player: %i Number: %i Letters: %s\n", inet_ntoa(getaddr.sin_addr), i - 3, num, bufrack); 
                                break;
                            case 'p':
                                strncpy(bufx, buffer + 2, 2);
                                bufx[2] = '\0';
                                strncpy(bufy, buffer + 4, 2);
                                bufy[2] = '\0';

                                x = atoi(bufx);
                                y = atoi(bufy);

                                strncpy(word, buffer + 6, sizeof(word)); 
                                word[strlen(buffer + 6)] = '\0';

                                printf("PLACE From: %s Player: %i Direction: %c X: %i Y: %i Word: %s\n", inet_ntoa(getaddr.sin_addr), i - 3, buffer[1], x, y, word);

                                if(searchdict(word) != 1)
                                {
                                    send(i, "c*** Word not found ***", 25, 0);
                                    printf("DICT %s not found\n", word);
                                    break;
                                }

                                switch(buffer[1])
                                {
                                    case 'a':
                                        strplc_a(x, y, BSIZE, board, word, strlen(word));
                                        break;
                                    case 'd':
                                        strplc_d(x, y, BSIZE, board, word, strlen(word));
                                        break;
                                }

                                for(j = 0; j <= bigfd; j++)
                                    if(FD_ISSET(j, &master))
                                        if(j != listens)
                                            sendboard(j, buffer, *board, BSIZE);

                                sleep(1);

                                num = strlen(word);
                                memset(bufrack, '\0', sizeof(bufrack));
                                randomrack(bufrack, bag, num);

                                sprintf(bufout, "d%sr%s", word, bufrack);
                                send(i, bufout, strlen(bufout), 0);

                                printf("RACK From: %s Player: %i Number: %i Letters: %s\n", inet_ntoa(getaddr.sin_addr), i - 3, num, bufrack);
                                break;
                        }

                        memset(buffer, '\0', strlen(buffer));
                    }
                }
            }
        }
    }

    return 0;
}