Exemplo n.º 1
0
char *ExpandOptions(WScreen *scr, const char *cmdline)
{
	int ptr, optr, state, len, olen;
	char *out, *nout;
	char *selection = NULL;
	char *user_input = NULL;
#ifdef XDND
	char *dropped_thing = NULL;
#endif
	char tmpbuf[TMPBUFSIZE];
	int slen;

	len = strlen(cmdline);
	olen = len + 1;
	out = malloc(olen);
	if (!out) {
		wwarning(_("out of memory during expansion of \"%s\""), cmdline);
		return NULL;
	}
	*out = 0;
	ptr = 0;		/* input line pointer */
	optr = 0;		/* output line pointer */
	state = S_NORMAL;
	while (ptr < len) {
		switch (state) {
		case S_NORMAL:
			switch (cmdline[ptr]) {
			case '\\':
				state = S_ESCAPE;
				break;
			case '%':
				state = S_OPTION;
				break;
			default:
				state = S_NORMAL;
				out[optr++] = cmdline[ptr];
				break;
			}
			break;
		case S_ESCAPE:
			switch (cmdline[ptr]) {
			case 'n':
				out[optr++] = 10;
				break;

			case 'r':
				out[optr++] = 13;
				break;

			case 't':
				out[optr++] = 9;
				break;

			default:
				out[optr++] = cmdline[ptr];
			}
			state = S_NORMAL;
			break;
		case S_OPTION:
			state = S_NORMAL;
			switch (cmdline[ptr]) {
			case 'w':
				if (scr->focused_window && scr->focused_window->flags.focused) {
					snprintf(tmpbuf, sizeof(tmpbuf), "0x%x",
						 (unsigned int)scr->focused_window->client_win);
					slen = strlen(tmpbuf);
					olen += slen;
					nout = realloc(out, olen);
					if (!nout) {
						wwarning(_("out of memory during expansion of \"%%w\""));
						goto error;
					}
					out = nout;
					strcat(out, tmpbuf);
					optr += slen;
				} else {
					out[optr++] = ' ';
				}
				break;

			case 'W':
				snprintf(tmpbuf, sizeof(tmpbuf), "0x%x", (unsigned int)w_global.workspace.current + 1);
				slen = strlen(tmpbuf);
				olen += slen;
				nout = realloc(out, olen);
				if (!nout) {
					wwarning(_("out of memory during expansion of \"%%W\""));
					goto error;
				}
				out = nout;
				strcat(out, tmpbuf);
				optr += slen;
				break;

			case 'a':
			case 'A':
				ptr++;
				user_input = getuserinput(scr, cmdline, &ptr, cmdline[ptr-1] == 'A');
				if (user_input) {
					slen = strlen(user_input);
					olen += slen;
					nout = realloc(out, olen);
					if (!nout) {
						wwarning(_("out of memory during expansion of \"%%a\""));
						goto error;
					}
					out = nout;
					strcat(out, user_input);
					optr += slen;
				} else {
					/* Not an error, but user has Canceled the dialog box.
					 * This will make the command to not be performed. */
					goto error;
				}
				break;

#ifdef XDND
			case 'd':
				if (scr->xdestring) {
					dropped_thing = wstrdup(scr->xdestring);
				}
				if (!dropped_thing) {
					scr->flags.dnd_data_convertion_status = 1;
					goto error;
				}
				slen = strlen(dropped_thing);
				olen += slen;
				nout = realloc(out, olen);
				if (!nout) {
					wwarning(_("out of memory during expansion of \"%%d\""));
					goto error;
				}
				out = nout;
				strcat(out, dropped_thing);
				optr += slen;
				break;
#endif				/* XDND */

			case 's':
				if (!selection) {
					selection = getselection(scr);
				}
				if (!selection) {
					wwarning(_("selection not available"));
					goto error;
				}
				slen = strlen(selection);
				olen += slen;
				nout = realloc(out, olen);
				if (!nout) {
					wwarning(_("out of memory during expansion of \"%%s\""));
					goto error;
				}
				out = nout;
				strcat(out, selection);
				optr += slen;
				break;

			default:
				out[optr++] = '%';
				out[optr++] = cmdline[ptr];
			}
			break;
		}
		out[optr] = 0;
		ptr++;
	}
	if (selection)
		XFree(selection);
	return out;

 error:
	wfree(out);
	if (selection)
		XFree(selection);
	return NULL;
}
Exemplo n.º 2
0
int *checkifvalid(chess *q, int *locinput, int *prloc, int chk) {/* Checks for match and barrier */
	int valid, i, counter = 0, counter1 = 0;
	char *str;
	switch(locinput[0]) {
		case PAWN://Done
			if(prloc[0] == CONF) {//
				mvprintw(27, 110, "Ambiguity!");
				mvprintw(29, 110, "Please enter the previous location");
				while(1) {
					printstate(stdscr, q);
					str = getuserinput(stdscr);
					prloc = getinputlocationpr(q, str, locinput);
					if(prloc != NULL) {
						break;
					}
					else {
						mvprintw(27, 110, "Please enter correct input\n");
					}
				}
			}
			else if(prloc[0] == NOTFOUND) {
				//Location not found
				return NULL;
			}
			return prloc;
			break;
		case ROOK: case BISHOP: case QUEEN://Using list datastructure Done
			if(prloc[0] == NOTFOUND) {
				//printf("Location not found");
				return NULL;
			}
			else {//prloc found
				valid = match(q, prloc, locinput);
				if(valid == 1) {//valid
					valid = barrier(q, prloc, locinput, chk);//Check if there is any coin in between
					if(valid == 0) {//0 indicates no barrier
						counter++;
						counter1 = 1;
					}
				}
				if(prloc[4] != NOTFOUND) {

					valid = match(q, &prloc[4], locinput);
					if(valid == 1) {//valid
						valid = barrier(q, &prloc[4], locinput, chk);
						if(valid == 0) {
							counter++;
							counter1 = 2;
						}
					}
				}
				if(counter == 0) {//Location found but not matched or there is a barrier
					return NULL;
				}
				if(counter == 2) {
					while(1) {
						mvprintw(27, 110, "Ambiguity!");
						mvprintw(29, 110, "Please enter the previous location");
						printstate(stdscr, q);
						str = getuserinput(stdscr);
						prloc = getinputlocationpr(q, str, locinput);
						if(prloc != NULL) {
							return prloc;
						}
					}
				}
				else if(counter == 1 && counter1 == 1) {//If only first is valid
					return prloc;
				}
				else {//Second is valid
					return &prloc[4];
				}		
			}
			break;
		case KNIGHT:
			prloc = search(q, locinput[0], q->state, NULL, NULL);
			if(prloc[0] == NOTFOUND) {
				//printf("Not found\n");
				return NULL;
			}
			else {
				valid = match(q, prloc, locinput);
				if(valid == 1) {//valid
					valid = barrier(q, prloc, locinput, chk);//Check if there is any coin in between
					if(valid == 0) {//0 indicates no barrier
						counter++;
						counter1 = 1;
					}
				}
				if(prloc[4] != NOTFOUND) {
					valid = match(q, &prloc[4], locinput);
					if(valid == 1) {//valid
						valid = barrier(q, &prloc[4], locinput, chk);
						if(valid == 0) {
							counter++;
							counter1 = 2;
						}
					}
				}
				if(counter == 0) {
					return NULL;
				}
				if(counter == 2) {
					while(1) {
						mvprintw(27, 110, "Ambiguity!");
						mvprintw(29, 110, "Please enter the previous location");
						printstate(stdscr, q);
						str = getuserinput(stdscr);
						prloc = getinputlocationpr(q, str, locinput);
						if(prloc != NULL) 
							return prloc;
					}
				}
				else if(counter == 1 && counter1 == 1) {
					return prloc;
				}
				else {
					return &prloc[4];
				}		
			}
			break;
		case KING:
			valid = match(q, prloc, locinput);
			if(valid == 1) {
				return prloc;
			}
			return NULL;
	}
}