static Boolean test_my_stristr(int verbose)
{
    static struct stringtest {
	char *str1;
	char *str2;
	char *result;
    } tests[] = {
	{ "paloozah", "ooz", "oozah" },
	{ "paloozah", "pa", "paloozah" },
	{ "paloozah", "par", NULL },
	{ "paloozah", "x", NULL },
	{ "PALOOZAH", "ooz", "OOZAH" },
	{ "PALOOZAH", "ah", "AH" },
	{ "PALOOZAH", "h", "H" },
	{ "h", "h", "h" },
	{ "H", "h", "H" },
	{ "", "h", NULL },
    };

    size_t i;
    Boolean result = True;
    for (i = 0; i < (sizeof tests / sizeof tests[0]); i++) {
	char *res_str = my_stristr(tests[i].str1, tests[i].str2);
	/* We want to verify that my_stristr(STR1, STR2) is either NULL or
	 * a substring of STR1, but, e.g., `"oozah" == "paloozah" + 3' may
	 * or may not be true (depending on compiler optimizations).  */
	char *test_str = tests[i].result ?
			 tests[i].str1 + strlen(tests[i].str1) - strlen(tests[i].result) :
			 NULL;
	if (verbose) {
	    INFO((stderr, "my_stristr(%s, %s) -> %s == %s?\n",
		  tests[i].str1,
		  tests[i].str2,
		  res_str,
		  test_str));
	}
	if (res_str != test_str) {
	    result = False;
	}
    }
    return result;
}
Example #2
0
/**
 * Find a trap kind based on its short description
 */
struct trap_kind *lookup_trap(const char *desc)
{
	int i;
	struct trap_kind *closest = NULL;

	/* Look for it */
	for (i = 1; i < z_info->trap_max; i++) {
		struct trap_kind *kind = &trap_info[i];
		if (!kind->name)
			continue;

		/* Test for equality */
		if (streq(desc, kind->desc))
			return kind;

		/* Test for close matches */
		if (!closest && my_stristr(kind->desc, desc))
			closest = kind;
	}

	/* Return our best match */
	return closest;
}
Example #3
0
int errno_test(void)
{
    enum { CUT = 6 };
    pj_status_t rc = 0;
    char errbuf[256];

    PJ_LOG(3,(THIS_FILE, "...errno test: check the msg carefully"));

    PJ_UNUSED_ARG(rc);
    
    /* 
     * Windows platform error. 
     */
#   ifdef ERROR_INVALID_DATA
    rc = PJ_STATUS_FROM_OS(ERROR_INVALID_DATA);
    pj_set_os_error(rc);

    /* Whole */
    pj_strerror(rc, errbuf, sizeof(errbuf));
    trim_newlines(errbuf);
    PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA: '%s'", errbuf));
    if (my_stristr(errbuf, "invalid") == NULL) {
        PJ_LOG(3, (THIS_FILE, 
                   "...error: expecting \"invalid\" string in the msg"));
#ifndef PJ_WIN32_WINCE
        return -20;
#endif
    }

    /* Cut version. */
    pj_strerror(rc, errbuf, CUT);
    PJ_LOG(3,(THIS_FILE, "...msg for rc=ERROR_INVALID_DATA (cut): '%s'", errbuf));
#   endif

    /*
     * Unix errors
     */
#   if defined(EINVAL) && !defined(PJ_SYMBIAN)
    rc = PJ_STATUS_FROM_OS(EINVAL);
    pj_set_os_error(rc);

    /* Whole */
    pj_strerror(rc, errbuf, sizeof(errbuf));
    trim_newlines(errbuf);
    PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL: '%s'", errbuf));
    if (my_stristr(errbuf, "invalid") == NULL) {
        PJ_LOG(3, (THIS_FILE, 
                   "...error: expecting \"invalid\" string in the msg"));
        return -30;
    }

    /* Cut */
    pj_strerror(rc, errbuf, CUT);
    PJ_LOG(3,(THIS_FILE, "...msg for rc=EINVAL (cut): '%s'", errbuf));
#   endif

    /* 
     * Windows WSA errors
     */
#   ifdef WSAEINVAL
    rc = PJ_STATUS_FROM_OS(WSAEINVAL);
    pj_set_os_error(rc);

    /* Whole */
    pj_strerror(rc, errbuf, sizeof(errbuf));
    trim_newlines(errbuf);
    PJ_LOG(3,(THIS_FILE, "...msg for rc=WSAEINVAL: '%s'", errbuf));
    if (my_stristr(errbuf, "invalid") == NULL) {
        PJ_LOG(3, (THIS_FILE, 
                   "...error: expecting \"invalid\" string in the msg"));
        return -40;
    }

    /* Cut */
    pj_strerror(rc, errbuf, CUT);
    PJ_LOG(3,(THIS_FILE, "...msg for rc=WSAEINVAL (cut): '%s'", errbuf));
#   endif

    pj_strerror(PJ_EBUG, errbuf, sizeof(errbuf));
    PJ_LOG(3,(THIS_FILE, "...msg for rc=PJ_EBUG: '%s'", errbuf));
    if (my_stristr(errbuf, "BUG") == NULL) {
        PJ_LOG(3, (THIS_FILE, 
                   "...error: expecting \"BUG\" string in the msg"));
        return -20;
    }

    pj_strerror(PJ_EBUG, errbuf, CUT);
    PJ_LOG(3,(THIS_FILE, "...msg for rc=PJ_EBUG, cut at %d chars: '%s'", 
              CUT, errbuf));

    /* Perror */
    pj_perror(3, THIS_FILE, PJ_SUCCESS, "...testing %s", "pj_perror");
    PJ_PERROR(3,(THIS_FILE, PJ_SUCCESS, "...testing %s", "PJ_PERROR"));

    return 0;
}
Example #4
0
/*
 * Show previous messages to the user
 *
 * The screen format uses line 0 and 23 for headers and prompts,
 * skips line 1 and 22, and uses line 2 thru 21 for old messages.
 *
 * This command shows you which commands you are viewing, and allows
 * you to "search" for strings in the recall.
 *
 * Note that messages may be longer than 80 characters, but they are
 * displayed using "infinite" length, with a special sub-command to
 * "slide" the virtual display to the left or right.
 *
 * Attempt to only highlight the matching portions of the string.
 */
void do_cmd_messages(void)
{
	ui_event ke;

	bool more = TRUE;

	int i, j, n, q;
	int wid, hgt;

	char shower[80] = "";



	/* Total messages */
	n = messages_num();

	/* Start on first message */
	i = 0;

	/* Start at leftmost edge */
	q = 0;

	/* Get size */
	Term_get_size(&wid, &hgt);

	/* Save screen */
	screen_save();

	/* Process requests until done */
	while (more)
	{
		/* Clear screen */
		Term_clear();

		/* Dump messages */
		for (j = 0; (j < hgt - 4) && (i + j < n); j++)
		{
			const char *msg;
			const char *str = message_str(i + j);
			byte attr = message_color(i + j);
			u16b count = message_count(i + j);

			if (count == 1)
				msg = str;
			else
				msg = format("%s <%dx>", str, count);

			/* Apply horizontal scroll */
			msg = ((int)strlen(msg) >= q) ? (msg + q) : "";

			/* Dump the messages, bottom to top */
			Term_putstr(0, hgt - 3 - j, -1, attr, msg);

			/* Highlight "shower" */
			if (shower[0])
			{
				str = msg;

				/* Display matches */
				while ((str = my_stristr(str, shower)) != NULL)
				{
					int len = strlen(shower);

					/* Display the match */
					Term_putstr(str-msg, hgt - 3 - j, len, TERM_YELLOW, str);

					/* Advance */
					str += len;
				}
			}
		}

		/* Display header */
		prt(format("Message recall (%d-%d of %d), offset %d", i, i + j - 1, n, q), 0, 0);

		/* Display prompt (not very informative) */
		if (shower[0])
			prt("[Movement keys to navigate, '-' for next, '=' to find]", hgt - 1, 0);
		else
			prt("[Movement keys to navigate, '=' to find, or ESCAPE to exit]", hgt - 1, 0);
			

		/* Get a command */
		ke = inkey_ex();


		/* Scroll forwards or backwards using mouse clicks */
		if (ke.type == EVT_MOUSE) {
			if (ke.mouse.y <= hgt / 2) {
				/* Go older if legal */
				if (i + 20 < n)
					i += 20;
			} else {
				/* Go newer */
				i = (i >= 20) ? (i - 20) : 0;
			}
		} else if (ke.type == EVT_KBRD) {
			switch (ke.key.code) {
				case ESCAPE: {
					more = FALSE;
					break;
				}

				case '=': {
					/* Get the string to find */
					prt("Find: ", hgt - 1, 0);
					if (!askfor_aux(shower, sizeof shower, NULL)) continue;
		
					/* Set to find */
					ke.key.code = '-';
					break;
				}

				case ARROW_LEFT:
				case '4':
					q = (q >= wid / 2) ? (q - wid / 2) : 0;
					break;

				case ARROW_RIGHT:
				case '6':
					q = q + wid / 2;
					break;

				case ARROW_UP:
				case '8':
					if (i + 1 < n) i += 1;
					break;

				case ARROW_DOWN:
				case '2':
				case '\r':
				case '\n':
					i = (i >= 1) ? (i - 1) : 0;
					break;

				case KC_PGUP:
				case 'p':
				case ' ':
					if (i + 20 < n) i += 20;
					break;

				case KC_PGDOWN:
				case 'n':
					i = (i >= 20) ? (i - 20) : 0;
					break;
			}
		}

		/* Find the next item */
		if (ke.key.code == '-' && shower[0])
		{
			s16b z;

			/* Scan messages */
			for (z = i + 1; z < n; z++)
			{
				/* Search for it */
				if (my_stristr(message_str(z), shower))
				{
					/* New location */
					i = z;

					/* Done */
					break;
				}
			}
		}
	}

	/* Load screen */
	screen_load();
}
Example #5
0
CALL_BACK MediaSniffer::cap_routine( MediaSniffer* ms )
{
	SniffRec rec;
	struct pcap_pkthdr *pkt_header;
	const Packet_Hdr *pkt_data;
	const tcphdr *tcph;
	const char *b, *e, *hb, *he, *pe;
	int ret, len, j;
	bool match;

	pcap_setnonblock( ms->caphandle_, 1, NULL );
	while( ms->run_ )
		{
		ret = pcap_next_ex( ms->caphandle_, &pkt_header, reinterpret_cast<const u_char**>(&pkt_data) );
		if( ret == 1 )
			{
			if( ntohs(pkt_data->eh.ether_type) == ETH_P_PPP_SES )
				{
				if( ntohs(pkt_data->ppp.proto) == PPP_IP )
					{
					tcph = reinterpret_cast<const struct tcphdr*>(reinterpret_cast<const char*>(pkt_data)
							+ sizeof(struct ether_header) + sizeof(struct pppoe_hdr) + sizeof(u_int16_t) + (pkt_data->ppp.iph.ihl << 2));
					}
				else{// IPv6
					tcph = reinterpret_cast<const struct tcphdr*>(reinterpret_cast<const char*>(pkt_data)
							+ sizeof(struct ether_header) + sizeof(struct pppoe_hdr) + sizeof(u_int16_t) + sizeof(struct ip6_hdr));
					}//end if
				}
			else if( ntohs(pkt_data->eh.ether_type) == ETH_P_IPV6 )
				{
				tcph = reinterpret_cast<const struct tcphdr*>(reinterpret_cast<const char*>(pkt_data)
						+ sizeof(struct ether_header) + sizeof(struct ip6_hdr));
				}
			else{// IPv4
				tcph = reinterpret_cast<const struct tcphdr*>(reinterpret_cast<const char*>(pkt_data)
						+ sizeof(struct ether_header) + (pkt_data->iph.ihl << 2));
				}//end if
			pe = reinterpret_cast<const char*>(pkt_data) + pkt_header->caplen;
			b = reinterpret_cast<const char*>(tcph) + (tcph->doff << 2) + sizeof(kGetTag);
			e = strnchr( b, ' ', pe - b );
			if( e == NULL )
				{
				e = pe;
				}//end if

			if( ms->key_[0] != NULL )
				{
				match = false;
				for( j = 0; ms->key_[j] != NULL; ++j )
					{
					if( my_stristr( b, ms->key_[j] ) != NULL )
						{
						match = true;
						break;
						}//end if
					}//end for
				}
			else{// do not filter
				match = true;
				}//end if

			if( match )
				{
				len = e - b;
				if( len > kHttpProLen && strncasecmp( b, kHttpPro, kHttpProLen ) == 0 )
					{
					rec.url = string( b, len );
					}
				else{
					rec.url = string( kHttpPro );
					// find 'Host: '
					hb = e + 1;
					if( hb < pe )
						{
						do	{
							hb = strnchr( hb, '\r', pe - hb );
							if( hb != NULL )
								{
								hb += 2;
								if( hb + kHostLen < pe && strncasecmp( hb, kHost, kHostLen ) == 0 )
									{
									hb += kHostLen;
									he = strnchr( hb, '\r', pe - hb );
									if( he != NULL )
										{
										rec.url.append( hb, he - hb );
										}//end if
									break;
									}//end if
								}//end if
							} while( hb != NULL && *hb != '\r' );
						}//end if
					rec.url.append( b -4, e - b+4 );
					}//end if

				if( ms->hash_ == NULL || !(*ms->hash_)[rec.url] )
					{
					// find 'User-Agent: '
					rec.ua.clear();
					hb = e + 1;
					if( hb < pe )
						{
						do	{
							hb = strnchr( hb, '\r', pe - hb );
							if( hb != NULL )
								{
								hb += 2;
								if( hb + kUserAgentLen < pe && strncasecmp( hb, kUserAgent, kUserAgentLen ) == 0 )
									{
									hb += kUserAgentLen;
									he = strnchr( hb, '\r', pe - hb );
									if( he != NULL )
										{
										rec.ua = string( hb, he - hb );
										}//end if
									break;
									}//end if
								}//end if
							} while( hb != NULL && *hb != '\r' );
						}//end if
					if( rec.ua.empty() )
						{
						rec.ua = "<null>";
						}//end if
					ms->buff_.push_back( rec );
					if( ms->show_rec_ != NULL )
						{
						ms->show_rec_( ms->sr_arg_, &rec );
						}//end if
					}//end if
				}//end if
			}
		else if( ret == 0 )
			{
			// do nothing
			MsSleep( LISTEN_TIMESLICE );
			}
		else{
			break;
			}//end if
		}//end while

	return 0;
}//end MediaSniffer::cap_routine
Example #6
0
/* Get spell by name */
bool get_spell_by_name(int *k, int *s, bool inven, bool equip, bool books)
{
	char buf[256];
	char *tok;
	int i, sn;
	size_t len;
	bool book_matched = FALSE;
	char *prompt = "Spell name: ";

	/* Hack -- show opening quote symbol */
	if (prompt_quote_hack) prompt = "Spell name: \"";

	buf[0] = '\0';
	if (!get_string(prompt, buf, 80))
	{
		return FALSE;
	}

	/* Hack -- remove final quote */
	len = strlen(buf);
	if (len == 0) return FALSE;
	if (buf[len-1] == '"') buf[len-1] = '\0';

	/* Split entry */
	tok = strtok(buf, "|");
	while (tok)
	{
		if (STRZERO(tok)) continue;
		/* Match against valid items */
		for (i = 0; i < INVEN_TOTAL; i++)
		for (sn = 0; sn < PY_MAX_SPELLS; sn++)
		{
			if (spell_info[i][sn][0] == '\0') break;
			if (!inven && i < INVEN_WIELD) continue;
			if (!equip && i >= INVEN_WIELD) continue;
			if (inventory[i].tval == 0) continue;

			/* Book-name match */
			if (/*get_item_okay(i) &&*/
			   books &&
			   my_stristr(inventory_name[i], tok))
			{
				(*k) = i;
				(*s) = -1;
				book_matched = TRUE;
			}

			/* Spell-name match */
			if (my_stristr(spell_info[i][sn], tok))
			{
				(*k) = i;
				(*s) = sn;
				/* Hack - also ask for projection */
				if (spell_flag[(i * SPELLS_PER_BOOK + sn)] & PY_SPELL_PROJECT)
				{
					if (get_check("Project? "))
						(*s) += SPELL_PROJECTED;
				}
				return TRUE;
			}
		}
		tok = strtok(NULL, "|");
	}
	if (books && book_matched) return TRUE;
	return FALSE;
}