Ejemplo n.º 1
0
static size_t common_pref_len(const char *a, const char *b)
{
	size_t i;
	size_t a_off, b_off;
	wchar_t ca, cb;

	i = 0;
	a_off = 0;
	b_off = 0;

	while (true) {
		ca = str_decode(a, &a_off, STR_NO_LIMIT);
		cb = str_decode(b, &b_off, STR_NO_LIMIT);

		if (ca == '\0' || cb == '\0' || ca != cb)
			break;
		++i;
	}

	return i;
}
Ejemplo n.º 2
0
static int log_printf_str_write(const char *str, size_t size, void *data)
{
	size_t offset = 0;
	size_t chars = 0;
	
	while (offset < size) {
		kio_push_char(str_decode(str, &offset, size));
		chars++;
	}
	
	log_append((const uint8_t *)str, size);
	
	return chars;
}
Ejemplo n.º 3
0
static int mail_get_subject(char *buf, struct MailHeader *mh)
{
	char *subject = mh->subject;

	while (isspace(*(buf)))
		++buf;

	str_notab(buf);
	str_trim(buf);
	str_decode((unsigned char *)buf);
	str_ansi(subject, buf, SUBJECT_LEN);

	return 0;
}
Ejemplo n.º 4
0
static void tinput_insert_string(tinput_t *ti, const char *str)
{
	size_t ilen = min(str_length(str), INPUT_MAX_SIZE - ti->nc);
	if (ilen == 0)
		return;
	
	unsigned new_width = LIN_TO_COL(ti, ti->text_coord) + ti->nc + ilen;
	unsigned new_height = (new_width / ti->con_cols) + 1;
	if (new_height >= ti->con_rows) {
		/* Disallow text longer than 1 page for now. */
		return;
	}
	
	if (ti->nc > 0) {
		size_t i;
		for (i = ti->nc; i > ti->pos; i--)
			ti->buffer[i + ilen - 1] = ti->buffer[i - 1];
	}
	
	size_t off = 0;
	size_t i = 0;
	while (i < ilen) {
		wchar_t c = str_decode(str, &off, STR_NO_LIMIT);
		if (c == '\0')
			break;
		
		/* Filter out non-printable chars. */
		if (c < 32)
			c = 32;
		
		ti->buffer[ti->pos + i] = c;
		i++;
	}
	
	ti->pos += ilen;
	ti->nc += ilen;
	ti->buffer[ti->nc] = '\0';
	ti->sel_start = ti->pos;
	
	tinput_display_tail(ti, ti->pos - ilen, 0);
	tinput_update_origin(ti);
	tinput_position_caret(ti);
}
Ejemplo n.º 5
0
static void insert_clipboard_data(void)
{
	char *str;
	size_t off;
	wchar_t c;
	int rc;

	rc = clipboard_get_str(&str);
	if (rc != EOK || str == NULL)
		return;

	off = 0;

	while (true) {
		c = str_decode(str, &off, STR_NO_LIMIT);
		if (c == '\0')
			break;

		insert_char(c);
	}

	free(str);
}
Ejemplo n.º 6
0
/** Insert file at caret position.
 *
 * Reads in the contents of a file and inserts them at the current position
 * of the caret.
 */
static int file_insert(char *fname)
{
	FILE *f;
	wchar_t c;
	char buf[BUF_SIZE];
	int bcnt;
	int n_read;
	size_t off;

	f = fopen(fname, "rt");
	if (f == NULL)
		return EINVAL;

	bcnt = 0;

	while (true) {
		if (bcnt < STR_BOUNDS(1)) {
			n_read = fread(buf + bcnt, 1, BUF_SIZE - bcnt, f);
			bcnt += n_read;
		}

		off = 0;
		c = str_decode(buf, &off, bcnt);
		if (c == '\0')
			break;

		bcnt -= off;
		memcpy(buf, buf + off, bcnt);

		insert_char(c);
	}

	fclose(f);

	return EOK;
}
Ejemplo n.º 7
0
Archivo: more.c Proyecto: yrchen/Athena
int 
more(char *fpath, int promptend)
{
  extern char *strcasestr();
  static char *head[4] = {"§@ªÌ", "¼ÐÃD", "®É¶¡", "Âà«H"};
  char *ptr, *word, buf[1024], *ch1;
  struct stat st;
  FILE *fp;
  usint pagebreak[MAXPAGES], pageno, lino;
  int line, ch, viewed, pos, numbytes;
  int header = 0;
  int local = 0;
  char search_char0 = 0;
  static char search_str[81] = "";
  typedef char *(*FPTR) ();
  static FPTR fptr;
  int searching = 0;
  int scrollup = 0;
  char *http[80] = {
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  /* Ptt */
  char pagemode = 0;
  char pagecount = 0;

  inmore = 1;
  memset(pagebreak, 0, sizeof(pagebreak));
  if (*search_str)
    search_char0 = *search_str;
  *search_str = 0;
  if (!(fp = fopen(fpath, "r")))
  {
    inmore = 0;
    return -1;
  }

  if (fstat(fileno(fp), &st))
  {
    inmore = 0;
    fclose(fp);
    return -1;
  }

  pagebreak[0] = pageno = viewed = line = pos = 0;
  clear();

  while ((numbytes = readln(fp, buf)) || (line == t_lines))
  {
    if (scrollup)
    {
      rscroll();
      move(0, 0);
    }
    if (numbytes)		/* ¤@¯ë¸ê®Æ³B²z */
    {
      if (!viewed)		/* begin of file */
      {
	if (showansi)		/* header processing */
	{
	  if (!strncmp(buf, str_author1, LEN_AUTHOR1))
	  {
	    line = 3;
	    word = buf + LEN_AUTHOR1;
	    local = 1;
	  }
	  else if (!strncmp(buf, str_author2, LEN_AUTHOR2))
	  {
	    line = 4;
	    word = buf + LEN_AUTHOR2;
	  }

	  while (pos < line)
	  {
	    if (!local)
	      str_decode(word);

	    if (!pos && ((ptr = strstr(word, str_post1)) ||
			 (ptr = strstr(word, str_post2))))
	    {
	      ptr[-1] = '\0';

	      /* hialan §ïÃC¦â */
	      clrtoeol();
	      prints("¢~\033[46;37m%s\033[m%-55.55s%.4s\033[1;33m%-13s\033[36m¢¡\n", head[0], word, ptr, ptr + 5);
	    }
	    else if (pos < (local ? 3 : 4))
	    {
	      clrtoeol();
	      prints("¢x\033[46;37m%s\033[m%-72.72s¢x\033[m\n", head[pos], word);
	    }

	    viewed += numbytes;
	    numbytes = readln(fp, buf);

	    if (!pos && viewed >= 79)	/* ²Ä¤@¦æ¤Óªø¤F */
	    {
	      if (memcmp(buf, head[1], 2))	/* ²Ä¤G¦æ¤£¬O [¼Ð....] */
	      {
		viewed += numbytes;	/* Ū¤U¤@¦æ¶i¨Ó³B²z */
		numbytes = readln(fp, buf);
	      }
	    }

	    pos++;
	  }
	  if (pos)
	  {
	    header = 1;

	    outs("¢¢¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w\033[0;30;47m y)¦^À³  /)·j´M¤å¦r  =[]<>)¥DÃD¦¡¾\\Ū \033[0;1;36m¢£\n");

	    line = pos = (local ? 4 : 5);
	  }
	}
	lino = pos;
	word = NULL;
      }
      /* ¡°³B²z¤Þ¥ÎªÌ & ¤Þ¨¥ */

      if ((buf[1] == ' ') && (buf[0] == ':' || buf[0] == '>'))
	word = "";
      if ((buf[1] == ' ') && (buf[0] == ':' || buf[0] == '>') && (buf[2] == ':' || buf[2] == '>'))
	word = "";
      if ((buf[0] == '¡' && buf[1] == '°') || !strncmp(buf, "==>", 3))
	word = "";

      ch1 = buf;
      while (1)			/* Ptt */
      {
	int i;
	char e, *ch2;
	if (ch2 = strstr(ch1, "gopher://"));
	else if (ch2 = strstr(ch1, "mailto:"));
	else if (ch2 = strstr(ch1, "board://"));	/* ¤ä´©ª½±µ¶i¤J¬ÝªO */
	else
	  break;

	for (e = 0; ch2[e] != ' ' && ch2[e] != '\n' && ch2[e] != '\0'
	     && ch2[e] != '"' && ch2[e] != ';' && ch2[e] != ']'; e++);
	for (i = 0; http[i] && i < 80; i++)
	  if (!strncmp(http[i], ch2, e) && http[e] == 0)
	    break;
	if (!http[i])
	{
	  http[i] = (char *)malloc(e + 1);
	  strncpy(http[i], ch2, e);
	  http[i][e] = 0;
	  pagecount++;
	}
	ch1 = &ch2[7];
      }

      if (word)
	outs(word);
      {
	char msg[500], *pos;

	if (*search_str && (pos = fptr(buf, search_str)))
	{
	  char SearchStr[41];
	  char buf1[100], *pos1;

	  strncpy(SearchStr, pos, strlen(search_str));
	  SearchStr[strlen(search_str)] = 0;
	  searching = 0;
	  sprintf(msg, "%.*s%s", pos - buf, buf,
		  SearchStr);
	  while (pos = fptr(pos1 = pos + strlen(search_str), search_str))
	  {
	    sprintf(buf1, "%.*s%s", pos - pos1, pos1, SearchStr);
	    strcat(msg, buf1);
	  }
	  strcat(msg, pos1);
	  outs(Ptt_prints(msg, NO_RELOAD));
	}
	else
	{
	  outs(Ptt_prints(buf, NO_RELOAD));
	}
      }
      if (word)
      {
	outs("");
	word = NULL;
      }
      outc('\n');

      if (beep)
      {
	bell();
	beep = 0;
      }

      if (line < b_lines)	/* ¤@¯ë¸ê®ÆŪ¨ú */
	line++;

      if (line == b_lines && searching == -1)
      {
	if (pageno > 0)
	  fseek(fp, (off_t) (viewed = pagebreak[--pageno]), SEEK_SET);
	else
	  searching = 0;
	lino = pos = line = 0;
	clear();
	continue;
      }

      if (scrollup)
      {
	move(line = b_lines, 0);
	clrtoeol();
	for (pos = 1; pos < b_lines; pos++)
	  viewed += readln(fp, buf);
      }
      else if (pos == b_lines)	/* ±²°Ê¿Ã¹õ */
	scroll();
      else
	pos++;


      if (!scrollup && ++lino >= b_lines && pageno < MAXPAGES - 1)
      {
	pagebreak[++pageno] = viewed;
	lino = 1;
      }

      if (scrollup)
      {
	lino = scrollup;
	scrollup = 0;
      }
      viewed += numbytes;	/* ²Ö­pŪ¹L¸ê®Æ */
    }
    else
      line = b_lines;		/* end of END */


    if (promptend && (!searching && line == b_lines || viewed == st.st_size))
    {
      move(b_lines, 0);
      if (viewed == st.st_size)
      {
	if (searching == 1)
	  searching = 0;
      }
      else if (pageno == 1 && lino == 1)
      {
	if (searching == -1)
	  searching = 0;
      }
      prints("%s  ÂsÄý P.%d(%d%%)  ", COLOR2, pageno, (viewed * 100) / st.st_size);
      prints("%s                 ¡ö¡ô¡õ¡÷|PgUp|PgDn|Home|End)¾ÉÄý  h)»¡©ú \033[m", COLOR3);

      move(b_lines, 0);		/* ¸Ñ¨Mª½±µ¸õ¨ì¬ÝªOªºBug */
      while (line == b_lines || (line > 0 && viewed == st.st_size))
      {
	switch (ch = igetkey())
	{
	case 'h':
	case 'H':
	case '?':
	  show_file("etc/help/MORE.help", 0, 24, ONLY_COLOR);
	  pressanykey_old(NULL);
	  if (pageno)
	    pageno--;
	  lino = line = 0;
	  break;
	case ':':
	  {
	    char buf[10];
	    int i = 0;

	    getdata(b_lines - 1, 0, "Goto Page: ", buf, 5, DOECHO, 0);
	    sscanf(buf, "%d", &i);
	    if (0 < i && i < MAXPAGES && (i == 1 || pagebreak[i - 1]))
	      pageno = i - 1;
	    else if (pageno)
	      pageno--;
	    lino = line = 0;
	    break;
	  }

	case '/':
	  {
	    char ans[4] = "n";
	    *search_str = search_char0;
	    getdata(b_lines - 1, 0, "[·j´M]ÃöÁä¦r:", search_str, 40, DOECHO, 0);
	    if (*search_str)
	    {
	      searching = 1;
	      if (getdata(b_lines - 1, 0, "°Ï¤À¤j¤p¼g(Y/N/Q)? [N] ", ans, 4, LCECHO, 0) && *ans == 'y')
		fptr = strstr;
	      else
		fptr = strcasestr;
	    }
	    if (*ans == 'q')
	      searching = 0;
	    if (pageno)
	      pageno--;
	    lino = line = 0;
	    break;
	  }
	case 'n':
	  if (*search_str)
	  {
	    searching = 1;
	    if (pageno)
	      pageno--;
	    lino = line = 0;
	  }
	  break;
	case 'N':
	  if (*search_str)
	  {
	    searching = -1;
	    if (pageno)
	      pageno--;
	    lino = line = 0;
	  }
	  break;
	case 'r':
	case 'R':
	case 'Y':
	case 'y':
	case 'A':
	case 'a':
	case 'F':
	case 'B':
	case KEY_LEFT:
	case 'q':
	case 'b':
	case 'f':
	case ']':		/* Kaede ¬°¤F¥DÃD¾\Ū¤è«K */
	case '[':		/* Kaede ¬°¤F¥DÃD¾\Ū¤è«K */
	case '=':		/* Kaede ¬°¤F¥DÃD¾\Ū¤è«K */
	  fclose(fp);
	  inmore = 0;
	  return ch;
	  
	case Ctrl('F'):
	case KEY_PGDN:
	  line = 1;
	  break;
	case 't':
	  if (viewed == st.st_size)
	  {			/* ch ']' */
	    fclose(fp);
	    inmore = 0;
	    return ']';
	  }
	  line = 1;
	  break;
	case ' ':
	  if (viewed == st.st_size)
	  {			/* ch 'f' */
	    fclose(fp);
	    inmore = 0;
	    return 'f';
	  }
	  line = 1;
	  break;
	case KEY_RIGHT:
	  if (viewed == st.st_size)
	  {			/* ch 'q' */
	    fclose(fp);
	    inmore = 0;
	    return 'q';
	  }
	  line = 1;
	  pagemode = 0;
	  break;
	case '\r':
	case '\n':
	  if (pagemode)
	  {
	    more_web(http[pagemode - 1], YEA);
	    /* pagebreak[0] = pageno = viewed = line = pos = 0; */
	    pagemode = 0;
	    *search_str = 0;
	    if (pageno)
	      pageno--;
	    lino = line = 0;
	    break;
	  }

	case KEY_DOWN:
	  if (viewed == st.st_size ||
	      promptend == 2 && (ch == '\r' || ch == '\n'))
	  {					/* ch 'f' */
	    fclose(fp);		
	    inmore = 0;
	    return 'f';
	  }
	  line = t_lines - 2;
	  break;

	case '$':
	case 'G':
	case KEY_END:
	  line = t_lines;
	  break;

	case '0':
	case 'g':
	case KEY_HOME:
	  pageno = line = 0;
	  break;

	case 'E':
	  if (strcmp(fpath, "etc/ve.hlp"))
	  {			/* ch 'q' */
	    fclose(fp);
	    inmore = 0;
	    vedit(fpath, HAS_PERM(PERM_SYSOP) ? 0 : 2);
	    return 'q';
	  }
	  break;

	case KEY_ESC:
	  if (KEY_ESC_arg == 'n')
	  {
	    edit_note();
	    if (pageno)
	      pageno--;
	    lino = line = 0;
	  }
	  else if (KEY_ESC_arg == 'c')
	    capture_screen();
	  break;

	case Ctrl('I'):
	  if (!pagecount)
	    break;
	  pagemode = (pagemode % pagecount) + 1;
	  strcpy(search_str, http[pagemode - 1]);
	  fptr = strstr;
	  if (pageno)
	    pageno--;
	  lino = line = 0;
	  break;

	case KEY_UP:
	  line = -1;
	  break;

	case Ctrl('B'):
	case KEY_PGUP:
	  if (pageno > 1)
	  {
	    if (lino < 2)
	      pageno -= 2;
	    else
	      pageno--;
	    lino = line = 0;
	  }
	  else if (pageno && lino > 1)
	    pageno = line = 0;
	  break;
	case Ctrl('H'):
	  if (pageno > 1)
	  {
	    if (lino < 2)
	      pageno -= 2;
	    else
	      pageno--;
	    lino = line = 0;
	  }
	  else if (pageno && lino > 1)
	    pageno = line = 0;
	  else
	  {			/* ch 'b' */
	    fclose(fp);
	    inmore = 0;
	    return 'b';
	  }
	}
      }

      if (line > 0)
      {
	move(b_lines, 0);
	clrtoeol();
	refresh();
      }
      else if (line < 0)
      {				/* Line scroll up */
	if (pageno <= 1)
	{
	  if (lino == 1 || !pageno)
	  {			/* ch 'b' */
	    fclose(fp);
	    inmore = 0;
	    return 'b';
	  }

	  /* ­¶¤º¤W±² sheng 20020113 */
	  if (header && lino <= 6 - local)
	  {
	    fseek(fp, (off_t) (viewed = pagebreak[scrollup = lino = pageno = 0] = 0), SEEK_SET);
	    clear();
	  }
	}

	if (pageno && lino > 1)
	{
	  line = lino - 2;
	  if (pageno == 1 && header)
	    line = lino - 3;

	  scrollup = lino - 1;
	  fseek(fp, (off_t) (viewed = pagebreak[pageno - 1]), SEEK_SET);
	  while (line--)
	    viewed += readln(fp, buf);
	}
	else if (pageno > 1)
	{
	  scrollup = b_lines - 1;

	  line = b_lines - 2;
	  if (pageno == 2 && header)
	    line = b_lines - 3;

	  fseek(fp, (off_t) (viewed = pagebreak[--pageno - 1]), SEEK_SET);
	  while (line--)
	    viewed += readln(fp, buf);
	}
	line = pos = 0;
      }
      else
      {
	pos = 0;
	fseek(fp, (off_t) (viewed = pagebreak[pageno]), SEEK_SET);
	clear();
      }
    }
  }

  fclose(fp);
  if (promptend)
  {
    pressanykey(NULL);
    clear();
  }
  else
    outs(reset_color);

  inmore = 0;
  return 'q';		/* ch 'q'*/
}
Ejemplo n.º 8
0
static int parse_address(const char *buf, char *name, char *addr)
{
	const char *ptr, *p;
	char *str;
	size_t len;
	*name = *addr = '\0';

	if ((str = strchr(buf, '<')) && (ptr = strrchr(str, '>'))) {
		++str;
		while (isspace(*str))
			++str;
		while (isspace(*(ptr - 1)))
			--ptr;
		if (ptr <= str) {
			strcpy(addr, "NULL");
		} else {
			len = ptr - str;
			strncpy(addr, str, len);
			addr[len] = '\0';
		}
		if (!strchr(addr, '@')) {
			/*
			 * from local host
			 */
			strcat(addr, "@localhost");
		}

		ptr = buf;
		while (isspace(*ptr))
			++ptr;
		if (*ptr == '"') {
			/*
			 * handle "name" <address>
			 */
			if (!(p = strrchr(++ptr, '"'))) {
				/*
				 * handle "name <address>
				 * Which is error accutally...
				 */
				p = strchr(ptr, '<');
				while (isspace(*(p - 1)))
					--p;
			}
		} else {
			/*
			 * handle name <address>
			 */
			p = strchr(ptr, '<');
			while (isspace(*(p - 1)))
				--p;
		}

		if (p > ptr) {
			/*
			 * Have name
			 */
			len = p - ptr;
			strncpy(name, ptr, len);
			name[len] = '\0';
			str_decode((unsigned char *)name);
		}

	} else {
		/*
		 * Address only
		 * '<' and '>' used here is for missing each other to form a pair
		 */
		ptr = buf;
		while (*ptr && (isspace(*ptr) || *ptr == '<'))
			++ptr;

		p = ptr;
		while (*p != '\0' &&
		       !isspace(*p) &&
		       *p != '>' &&
		       *p != ',')
			++p;
		if (p <= ptr)
			return -1;

		len = p - ptr;
		strncpy(addr, ptr, len);
		addr[len] = '\0';

		str_trim(addr);
		if (!strlen(addr))
			return -1;

#if 0
		if (!strchr(addr, '@'))	/* ¥Ñ local host ±H«H */
			strcat(addr, "@localhost");
#endif
	}

	return 0;
}
Ejemplo n.º 9
0
void insertscore(int score, int level)
{
	int i;
	int j;
	size_t off;
	cons_event_t ev;
	kbd_event_t *kev;
	
	clear_screen();
	moveto(10, 10);
	puts("Insert your name: ");
	str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
	    "Player");
	i = 6;
	off = 6;
	
	moveto(10 , 28);
	printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME-i,
	    "........................................");
	
	while (1) {
		console_flush(console);
		if (!console_get_event(console, &ev))
			exit(1);
		
		if (ev.type != CEV_KEY || ev.ev.key.type == KEY_RELEASE)
			continue;
		
		kev = &ev.ev.key;
		
		if (kev->key == KC_ENTER || kev->key == KC_NENTER)
			break;
		
		if (kev->key == KC_BACKSPACE) {
			if (i > 0) {
				wchar_t uc;
				
				--i;
				while (off > 0) {
					--off;
					size_t otmp = off;
					uc = str_decode(scores[NUMSPOTS - 1].hs_name,
					    &otmp, STR_BOUNDS(MAXLOGNAME) + 1);
					if (uc != U_SPECIAL)
						break;
				}
				
				scores[NUMSPOTS - 1].hs_name[off] = '\0';
			}
		} else if (kev->c != '\0') {
			if (i < (MAXLOGNAME - 1)) {
				if (chr_encode(kev->c, scores[NUMSPOTS - 1].hs_name,
				    &off, STR_BOUNDS(MAXLOGNAME) + 1) == EOK) {
					++i;
				}
				scores[NUMSPOTS - 1].hs_name[off] = '\0';
			}
		}
		
		moveto(10, 28);
		printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i,
		    "........................................");
	}
	
	scores[NUMSPOTS - 1].hs_score = score;
	scores[NUMSPOTS - 1].hs_level = level;
	
	i = NUMSPOTS - 1;
	while ((i > 0) && (scores[i - 1].hs_score < score))
		i--;
	
	for (j = NUMSPOTS - 2; j > i; j--)
		copyhiscore(j, j-1);
	
	copyhiscore(i, NUMSPOTS - 1);
}
Ejemplo n.º 10
0
int stun_attr_decode(struct stun_attr **attrp, struct mbuf *mb,
		     const uint8_t *tid, struct stun_unknown_attr *ua)
{
	struct stun_attr *attr;
	size_t start, len;
	uint32_t i, n;
	int err = 0;

	if (!mb || !attrp)
		return EINVAL;

	if (mbuf_get_left(mb) < 4)
		return EBADMSG;

	attr = mem_zalloc(sizeof(*attr), destructor);
	if (!attr)
		return ENOMEM;

	attr->type = htons(mbuf_read_u16(mb));
	len = htons(mbuf_read_u16(mb));

	if (mbuf_get_left(mb) < len)
		goto badmsg;

	start = mb->pos;

	switch (attr->type) {

	case STUN_ATTR_MAPPED_ADDR:
	case STUN_ATTR_ALT_SERVER:
	case STUN_ATTR_RESP_ORIGIN:
	case STUN_ATTR_OTHER_ADDR:
		tid = NULL;
		/*@fallthrough@*/
	case STUN_ATTR_XOR_PEER_ADDR:
	case STUN_ATTR_XOR_RELAY_ADDR:
	case STUN_ATTR_XOR_MAPPED_ADDR:
		err = stun_addr_decode(mb, &attr->v.sa, tid);
		break;

	case STUN_ATTR_CHANGE_REQ:
		if (len != 4)
			goto badmsg;

		n = ntohl(mbuf_read_u32(mb));
		attr->v.change_req.ip   = (n >> 2) & 0x1;
		attr->v.change_req.port = (n >> 1) & 0x1;
		break;

	case STUN_ATTR_USERNAME:
	case STUN_ATTR_REALM:
	case STUN_ATTR_NONCE:
	case STUN_ATTR_SOFTWARE:
		err = str_decode(mb, &attr->v.str, len);
		break;

	case STUN_ATTR_MSG_INTEGRITY:
		if (len != 20)
			goto badmsg;

		err = mbuf_read_mem(mb, attr->v.msg_integrity, 20);
		break;

	case STUN_ATTR_ERR_CODE:
		if (len < 4)
			goto badmsg;

		mb->pos += 2;
		attr->v.err_code.code  = (mbuf_read_u8(mb) & 0x7) * 100;
		attr->v.err_code.code += mbuf_read_u8(mb);
		err = str_decode(mb, &attr->v.err_code.reason, len - 4);
		break;

	case STUN_ATTR_UNKNOWN_ATTR:
		for (i=0; i<len/2; i++) {
			uint16_t type = ntohs(mbuf_read_u16(mb));

			if (i >= ARRAY_SIZE(attr->v.unknown_attr.typev))
				continue;

			attr->v.unknown_attr.typev[i] = type;
			attr->v.unknown_attr.typec++;
		}
		break;

	case STUN_ATTR_CHANNEL_NUMBER:
	case STUN_ATTR_RESP_PORT:
		if (len < 2)
			goto badmsg;

	        attr->v.uint16 = ntohs(mbuf_read_u16(mb));
		break;

	case STUN_ATTR_LIFETIME:
	case STUN_ATTR_PRIORITY:
	case STUN_ATTR_FINGERPRINT:
		if (len != 4)
			goto badmsg;

	        attr->v.uint32 = ntohl(mbuf_read_u32(mb));
		break;

	case STUN_ATTR_DATA:
	case STUN_ATTR_PADDING:
		attr->v.mb.buf  = mem_ref(mb->buf);
		attr->v.mb.size = mb->size;
		attr->v.mb.pos  = mb->pos;
		attr->v.mb.end  = mb->pos + len;
		mb->pos += len;
		break;

	case STUN_ATTR_REQ_ADDR_FAMILY:
	case STUN_ATTR_REQ_TRANSPORT:
		if (len < 1)
			goto badmsg;

	        attr->v.uint8 = mbuf_read_u8(mb);
		break;

	case STUN_ATTR_EVEN_PORT:
		if (len < 1)
			goto badmsg;

		attr->v.even_port.r = (mbuf_read_u8(mb) >> 7) & 0x1;
		break;

	case STUN_ATTR_DONT_FRAGMENT:
	case STUN_ATTR_USE_CAND:
		if (len > 0)
			goto badmsg;

		/* no value */
		break;

	case STUN_ATTR_RSV_TOKEN:
	case STUN_ATTR_CONTROLLING:
	case STUN_ATTR_CONTROLLED:
		if (len != 8)
			goto badmsg;

	        attr->v.uint64 = sys_ntohll(mbuf_read_u64(mb));
		break;

	default:
		mb->pos += len;

		if (attr->type >= 0x8000)
			break;

		if (ua && ua->typec < ARRAY_SIZE(ua->typev))
			ua->typev[ua->typec++] = attr->type;
		break;
	}

	if (err)
		goto error;

	/* padding */
	while (((mb->pos - start) & 0x03) && mbuf_get_left(mb))
		++mb->pos;

	*attrp = attr;

	return 0;

 badmsg:
	err = EBADMSG;
 error:
	mem_deref(attr);

	return err;
}
Ejemplo n.º 11
0
static char *post_article(char *homepath, char *userid, char *board, int (*writebody)(), char *pathname, char *firstpath)
{
    struct fileheader header;
    struct fileheader threadfh;

/*    char        *subject = SUBJECT;  */
    char index[MAXPATHLEN];
    static char name[MAXPATHLEN];
    char article[MAXPATHLEN];
    FILE *fidx;
    int fh;
    int ret;
    int linkflag;
    char conv_buf[256];
    char old_path[255];

    getcwd(old_path, 255);
    sprintf(index, "%s/.DIR", homepath);
    if ((fidx = fopen(index, "r")) == NULL) {
        if ((fidx = fopen(index, "w")) == NULL) {
            innbbsdlog(":Err: Unable to post in %s.\n", homepath);
            return NULL;
        }
    }
    fclose(fidx);

    if (GET_POSTFILENAME(name, homepath) < 0)
	{
		innbbslog(" Err: can't get a postfile name\n");
		return NULL;
	}
	sprintf(article, "%s/%s", homepath, name);
	fh = open(article, O_WRONLY, 0644);

#ifdef DEBUG
    printf("post to %s\n", article);
#endif

    linkflag = 1;
    if (firstpath && *firstpath) {
        close(fh);
        unlink(article);
#ifdef DEBUGLINK
        innbbsdlog("try to link %s to %s", firstpath, article);
#endif
        linkflag = link(firstpath, article);
        if (linkflag) {
            fh = open(article, O_CREAT | O_EXCL | O_WRONLY, 0644);
        }
    }
    if (linkflag != 0) {
        if (writebody) {
            if ((*writebody) (fh, board, pathname) < 0)
                return NULL;
        } else {
            if (bbspost_write_post(fh, board, pathname) < 0)
                return NULL;
        }
        close(fh);
    }
    bzero((void *) &header, sizeof(header));
    strcpy(header.filename, name);
    strncpy(header.owner, userid, OWNER_LEN);
    header.owner[OWNER_LEN - 1] = 0;

    str_decode((unsigned char*)conv_buf, (unsigned char*)SUBJECT);
    /* big 标题转码,original patch by dgwang @ 笔山书院 */
    if(strstr(SUBJECT,"=?big5?") || strstr(SUBJECT,"=?Big5?") ||
       strstr(SUBJECT,"=?BIG5?") ){
       int len;
       len = strlen(conv_buf);
       big2gb(conv_buf,&len,0,getSession());
    }

    strnzhcpy(header.title, conv_buf, ARTICLE_TITLE_LEN);
    header.innflag[1] = 'M';
	set_posttime(&header);
    /*
     * if append record record, should return fail message 
     */
    chdir(BBSHOME);
    resolve_boards();
    linkflag = find_thread(&threadfh, board, header.title);
    header.eff_size = get_effsize(article);
    ret = after_post(NULL, &header, board, linkflag ? &threadfh : NULL, 0, getSession());
    if ((ret < 0) && (ret != -2)) {
        innbbslog(":Err:after_post Unable to post.\n");
        chdir(old_path);
        return NULL;
    }
    chdir(old_path);
    return name;
}
Ejemplo n.º 12
0
/* process post write */
static int bbspost_write_post(int fh, char *board, char *filename)
{
    char *fptr, *ptr;
    FILE *fhfd;
    char conv_buf[256];

    fhfd = fdopen(fh, "w");
    if (fhfd == NULL) {
        innbbsdlog("can't fdopen, maybe disk full\n");
        return -1;
    }
    if (strlen(SUBJECT) > 256)
        FAILED;
    str_decode((unsigned char*)conv_buf, (unsigned char*)SUBJECT);
    /* big 标题转码,original patch by dgwang @ 笔山书院 */
    if(strstr(SUBJECT,"=?big5?") || strstr(SUBJECT,"=?Big5?") ||
       strstr(SUBJECT,"=?BIG5?") ){
       int len;
       len = strlen(conv_buf);
       big2gb(conv_buf,&len,0,getSession());
    }

    if (fprintf(fhfd, "%s%s, %s%s\n", FromTxt, FROM, BoardTxt, board) == EOF
        || fprintf(fhfd, "%s%.70s\n", SubjectTxt, conv_buf) == EOF || fprintf(fhfd, "%s%.43s (%s)\n", OrganizationTxt, SITE, DATE) == EOF || fprintf(fhfd, "%s%s\n", PathTxt, PATH) == EOF)
        FAILED;

    if (POSTHOST != NULL) {
        if (fprintf(fhfd, "出  处: %.70s\n", POSTHOST) == EOF)
            FAILED;
    }
    if (fprintf(fhfd, "\n") == EOF)
        FAILED;
    for (fptr = BODY, ptr = strchr(fptr, '\r'); ptr != NULL && *ptr != '\0'; fptr = ptr + 1, ptr = strchr(fptr, '\r')) {
        int ch = *ptr;

        *ptr = '\0';
        /*
         * 990530.edwardc 这里应该要处理,采用留住单一 "." 的方式 
         */
        if (fptr[0] == '.' && fptr[1] == '.')
            fptr++;
        if (fputs(fptr, fhfd) == EOF)
            FAILED;
        *ptr = ch;
    }

    /*
     * 990530.edwardc 这里也是 
     */
    if (fptr[0] == '.' && fptr[1] == '.')
        fptr++;

    if (fputs(fptr, fhfd) == EOF)
        FAILED;

    fflush(fhfd);
    fclose(fhfd);
    return 0;
  failed:
    fclose(fhfd);
    return -1;
}
Ejemplo n.º 13
0
static unsigned int cat_file(const char *fname, size_t blen, bool hex,
    off64_t head, off64_t tail, bool tail_first)
{
	int fd, bytes = 0, count = 0, reads = 0;
	char *buff = NULL;
	int i;
	size_t offset = 0, copied_bytes = 0;
	off64_t file_size = 0, length = 0;

	bool reading_stdin = dash_represents_stdin && (str_cmp(fname, "-") == 0);
	
	if (reading_stdin) {
		fd = fileno(stdin);
		/* Allow storing the whole UTF-8 character. */
		blen = STR_BOUNDS(1);
	} else
		fd = open(fname, O_RDONLY);
	
	if (fd < 0) {
		printf("Unable to open %s\n", fname);
		return 1;
	}

	if (NULL == (buff = (char *) malloc(blen + 1))) {
		close(fd);
		printf("Unable to allocate enough memory to read %s\n",
			fname);
		return 1;
	}

	if (tail != CAT_FULL_FILE) {
		file_size = lseek(fd, 0, SEEK_END);
		if (head == CAT_FULL_FILE) {
			head = file_size;
			length = tail;
		} else if (tail_first) {
			length = head;
		} else {
			if (tail > head)
				tail = head;
			length = tail;
		}

		if (tail_first) {
			lseek(fd, (tail >= file_size) ? 0 : (file_size - tail), SEEK_SET);
		} else {
			lseek(fd, ((head - tail) >= file_size) ? 0 : (head - tail), SEEK_SET);
		}
	} else
		length = head;

	do {
		size_t bytes_to_read;
		if (reading_stdin) {
			bytes_to_read = 1;
		} else {
			if ((length != CAT_FULL_FILE) &&
			    (length - (off64_t)count <= (off64_t)(blen - copied_bytes))) {
				bytes_to_read = (size_t) (length - count);
			} else {
				bytes_to_read = blen - copied_bytes;
			}
		}
		
		bytes = read(fd, buff + copied_bytes, bytes_to_read);
		copied_bytes = 0;

		if (bytes > 0) {
			buff[bytes] = '\0';
			offset = 0;
			for (i = 0; i < bytes && !should_quit; i++) {
				if (hex) {
					paged_char(hexchars[((uint8_t)buff[i])/16]);
					paged_char(hexchars[((uint8_t)buff[i])%16]);
					paged_char(((count+i+1) & 0xf) == 0 ? '\n' : ' ');
				}
				else {
					wchar_t c = str_decode(buff, &offset, bytes);
					if (c == 0) {
						/* Reached end of string */
						break;
					} else if (c == U_SPECIAL && offset + 2 >= (size_t)bytes) {
						/* If an extended character is cut off due to the size of the buffer,
						   we will copy it over to the next buffer so it can be read correctly. */
						copied_bytes = bytes - offset + 1;
						memcpy(buff, buff + offset - 1, copied_bytes);
						break;
					}
					paged_char(c);
				}
				
			}
			count += bytes;
			reads++;
		}
		
		if (reading_stdin)
			fflush(stdout);
	} while (bytes > 0 && !should_quit && (count < length || length == CAT_FULL_FILE));

	close(fd);
	if (bytes == -1) {
		printf("Error reading %s\n", fname);
		free(buff);
		return 1;
	}

	free(buff);

	return 0;
}
Ejemplo n.º 14
0
static void pane_row_range_display(int r0, int r1)
{
	int i, j, fill;
	spt_t rb, re, dep, pt;
	coord_t rbc, rec;
	char row_buf[ROW_BUF_SIZE];
	wchar_t c;
	size_t pos, size;
	int s_column;
	coord_t csel_start, csel_end, ctmp;

	/* Determine selection start and end. */

	tag_get_pt(&pane.sel_start, &pt);
	spt_get_coord(&pt, &csel_start);

	tag_get_pt(&pane.caret_pos, &pt);
	spt_get_coord(&pt, &csel_end);

	if (coord_cmp(&csel_start, &csel_end) > 0) {
		ctmp = csel_start;
		csel_start = csel_end;
		csel_end = ctmp;
	}

	/* Draw rows from the sheet. */

	console_set_pos(con, 0, 0);
	for (i = r0; i < r1; ++i) {
		/* Starting point for row display */
		rbc.row = pane.sh_row + i;
		rbc.column = pane.sh_column;
		sheet_get_cell_pt(&doc.sh, &rbc, dir_before, &rb);

		/* Ending point for row display */
		rec.row = pane.sh_row + i;
		rec.column = pane.sh_column + pane.columns;
		sheet_get_cell_pt(&doc.sh, &rec, dir_before, &re);

		/* Copy the text of the row to the buffer. */
		sheet_copy_out(&doc.sh, &rb, &re, row_buf, ROW_BUF_SIZE, &dep);

		/* Display text from the buffer. */

		if (coord_cmp(&csel_start, &rbc) <= 0 &&
		    coord_cmp(&rbc, &csel_end) < 0) {
			console_flush(con);
			console_set_style(con, STYLE_SELECTED);
			console_flush(con);
		}

		console_set_pos(con, 0, i);
		size = str_size(row_buf);
		pos = 0;
		s_column = pane.sh_column;
		while (pos < size) {
			if ((csel_start.row == rbc.row) && (csel_start.column == s_column)) {
				console_flush(con);
				console_set_style(con, STYLE_SELECTED);
				console_flush(con);
			}
	
			if ((csel_end.row == rbc.row) && (csel_end.column == s_column)) {
				console_flush(con);
				console_set_style(con, STYLE_NORMAL);
				console_flush(con);
			}
	
			c = str_decode(row_buf, &pos, size);
			if (c != '\t') {
				printf("%lc", (wint_t) c);
				s_column += 1;
			} else {
				fill = 1 + ALIGN_UP(s_column, TAB_WIDTH)
				    - s_column;

				for (j = 0; j < fill; ++j)
					putchar(' ');
				s_column += fill;
			}
		}

		if ((csel_end.row == rbc.row) && (csel_end.column == s_column)) {
			console_flush(con);
			console_set_style(con, STYLE_NORMAL);
			console_flush(con);
		}

		/* Fill until the end of display area. */

		if (str_length(row_buf) < (unsigned) scr_columns)
			fill = scr_columns - str_length(row_buf);
		else
			fill = 0;

		for (j = 0; j < fill; ++j)
			putchar(' ');
		console_flush(con);
		console_set_style(con, STYLE_NORMAL);
	}

	pane.rflags |= REDRAW_CARET;
}
Ejemplo n.º 15
0
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
mod_file_t * module_load_info_vfs( const char * szLoadName, mod_file_t * pmod )
{
    /// @details BB@> this function actually reads in the module data

    vfs_FILE * fileread;
    int cnt;
    char cTmp;

    // clear all the module info
    if ( NULL == pmod ) return NULL;

    memset( pmod, 0, sizeof( *pmod ) );

    // see if we can open the file
    fileread = vfs_openRead( szLoadName );
    if ( NULL == fileread ) return NULL;

    // Read basic data
    fget_next_name( fileread, pmod->longname, sizeof( pmod->longname ) );
    fget_next_string( fileread, pmod->reference, SDL_arraysize( pmod->reference ) );
    pmod->unlockquest.id    = fget_next_idsz( fileread );
    pmod->unlockquest.level = fget_int( fileread );

    pmod->importamount = fget_next_int( fileread );
    pmod->allowexport  = fget_next_bool( fileread );
    pmod->minplayers   = fget_next_int( fileread );
    pmod->maxplayers   = fget_next_int( fileread );

    cTmp = fget_next_char( fileread );
    if ( 'T' == toupper( cTmp ) )  pmod->respawnvalid = btrue;
    if ( 'A' == toupper( cTmp ) )  pmod->respawnvalid = RESPAWN_ANYTIME;

    fget_next_char( fileread );
    pmod->rtscontrol = bfalse;        //< depecrated, not in use

    fget_next_string( fileread, pmod->rank, SDL_arraysize( pmod->rank ) );
    str_trim( pmod->rank );

    // convert the special ranks of "unranked" or "-" ("rank 0") to an empty string
    if ( 0 == strncmp( pmod->rank, "-", RANKSIZE ) )
    {
        pmod->rank[0] = CSTR_END;
    }
    else if ( 'U' == toupper( pmod->rank[0] ) )
    {
        pmod->rank[0] = CSTR_END;
    }
    pmod->rank[RANKSIZE-1] = CSTR_END;

    // Read the summary
    for ( cnt = 0; cnt < SUMMARYLINES; cnt++ )
    {
        // load the string
        fget_next_string( fileread,  pmod->summary[cnt], SDL_arraysize( pmod->summary[cnt] ) );
        pmod->summary[cnt][SUMMARYSIZE-1] = CSTR_END;

        // remove the '_' characters
        str_decode( pmod->summary[cnt], SDL_arraysize( pmod->summary[cnt] ), pmod->summary[cnt] );
    }

    // Assume default module type as a sidequest
    pmod->moduletype = FILTER_SIDE;

    // Read expansions
    while ( goto_colon( NULL, fileread, btrue ) )
    {
        IDSZ idsz = fget_idsz( fileread );

        // Read module type
        if ( idsz == MAKE_IDSZ( 'T', 'Y', 'P', 'E' ) )
        {
            // grab the expansion value
            cTmp = fget_first_letter( fileread );

            // parse the expansion value
            if ( 'M' == toupper( cTmp ) )  pmod->moduletype = FILTER_MAIN;
            else if ( 'S' == toupper( cTmp ) )  pmod->moduletype = FILTER_SIDE;
            else if ( 'T' == toupper( cTmp ) )  pmod->moduletype = FILTER_TOWN;
            else if ( 'F' == toupper( cTmp ) )  pmod->moduletype = FILTER_FUN;
            else if ( 'S' == toupper( cTmp ) )  pmod->moduletype = FILTER_STARTER;
        }
        else if ( idsz == MAKE_IDSZ( 'B', 'E', 'A', 'T' ) )
        {
            pmod->beaten = btrue;
        }
    }

    vfs_close( fileread );

    return pmod;
}