コード例 #1
0
ファイル: buf.c プロジェクト: shmibs/tok8x
buf_t* buf_read(FILE *f)
{
	const int sigsize = 8;
	wint_t wc;
	uint8_t y;
	int ret;
	buf_t *b;
	wchar_t wcswap[sigsize];
	uint8_t yswap[2];
	int i;

	/* read sig bytes or first sigsize chars */
	for(i = 0; i < sigsize; i++) {
		wc = getwc(f);
		EXIT_FERROR(f);
		if(wc == WEOF) {
			b = buf_new(false);
			EXIT_NULL(b);
			buf_push_nwchar(b, wcswap, i);
			return b;
		}
		wcswap[i] = (wchar_t)wc;
	}

	/* check for sig to determine if 8xp */
	if( !wcsncmp(wcswap, L"**TI83F*", sigsize) ) {
		b = buf_new(true);
	} else {
		b = buf_new(false);
	}

	/* not 8xp, so just read the rest */
	if( !b->is_8xp) {
		buf_push_nwchar(b, wcswap, sigsize);
		while( (wc = getwc(f)) != WEOF) {
			buf_push_wchar(b, (wchar_t)wc);
		}

		return b;
	}

	/* discard remainder of header */
	for(i = sigsize; i < HEADER_SIZE; i++) {
		ret = fread(&y, sizeof(uint8_t), 1, f);
		EXIT_FERROR(f);
		/* EOF this early means malformed 8xp */
		if(ret <= 0) {
			printf("\e[1mtok8x:\e[0m"
					"\e[1;31merror:\e[0m %s\n",
					"malformed .8xp input"
					);
			exit(EIO);
		}
	}
コード例 #2
0
ファイル: exec.c プロジェクト: CodeMonk/fish
/**
   Returns the interpreter for the specified script. Returns 0 if file
   is not a script with a shebang. This function leaks memory on every
   call. Only use it in the execve error handler which calls exit
   right afterwards, anyway.
 */
static wchar_t *get_interpreter( wchar_t *file )
{
	string_buffer_t sb;
	FILE *fp = wfopen( file, "r" );
	sb_init( &sb );
	wchar_t *res = 0;
	if( fp )
	{
		while( 1 )
		{
			wint_t ch = getwc( fp );
			if( ch == WEOF )
				break;
			if( ch == L'\n' )
				break;
			sb_append_char( &sb, (wchar_t)ch );
		}
	}
	
	res = (wchar_t *)sb.buff;
	
	if( !wcsncmp( L"#! /", res, 4 ) )
		return res+3;
	if( !wcsncmp( L"#!/", res, 3 ) )
		return res+2;
	return 0;
}
コード例 #3
0
ファイル: y4.c プロジェクト: Sunshine-OS/svr4-dev
static int
gtnm(void)
{
	register int s, val, c;

	/* read and convert an integer from the standard input */
	/* return the terminating character */
	/* blanks, tabs, and newlines are ignored */

	s = 1;
	val = 0;

	while ((c = getwc(finput)) != EOF) {
		if (iswdigit(c))
			val = val * 10 + c - L'0';
		else if (c == L'-')
			s = -1;
		else
			break;
	}
	*optimmem++ = s*val;
	if (optimmem >= &tracemem[new_memsize])
		exp_mem(0);
	return (c);
}
コード例 #4
0
ファイル: fmt.c プロジェクト: AhmadTux/freebsd
/* Get a single line from a stream. Expand tabs, strip control
 * characters and trailing whitespace, and handle backspaces.
 * Return the address of the buffer containing the line, and
 * put the length of the line in |lengthp|.
 * This can cope with arbitrarily long lines, and with lines
 * without terminating \n.
 * If there are no characters left or an error happens, we
 * return 0.
 * Don't confuse |spaces_pending| here with the global
 * |pending_spaces|.
 */
static wchar_t *
get_line(FILE *stream, size_t *lengthp) {
  static wchar_t *buf=NULL;
  static size_t length=0;
  size_t len=0;
  wint_t ch;
  size_t spaces_pending=0;
  int troff=0;
  size_t col=0;
  int cwidth;

  if (buf==NULL) { length=100; buf=XMALLOC(length * sizeof(wchar_t)); }
  while ((ch=getwc(stream)) != '\n' && ch != WEOF) {
    if (len+spaces_pending==0 && ch=='.' && !format_troff) troff=1;
    if (ch==' ') ++spaces_pending;
    else if (troff || iswprint(ch)) {
      while (len+spaces_pending >= length) {
        length*=2; buf=xrealloc(buf, length * sizeof(wchar_t));
      }
      while (spaces_pending > 0) { --spaces_pending; buf[len++]=' '; col++; }
      buf[len++] = ch;
      col += (cwidth = wcwidth(ch)) > 0 ? cwidth : 1;
    }
    else if (ch=='\t')
      spaces_pending += tab_width - (col+spaces_pending)%tab_width;
    else if (ch=='\b') { if (len) --len; if (col) --col; }
  }
  *lengthp=len;
  return (len>0 || ch!=WEOF) ? buf : 0;
}
コード例 #5
0
ファイル: luiolib.c プロジェクト: gitrider/wxsj2
static int uread_until (lua_State *L, FILE *f, const wchar_t *p, int pl) {
  int c;
  int j;
  int next[LUA_MAXUNTIL+1];
  luaL_Buffer b;
  luaL_buffinit(L, &b, 1);
  uprep_read_until(next, p, pl);
  j = 0;
  while ((c = getwc(f)) != (wchar_t)EOF) {
  NoRead:
    if (c == p[j]) {
      j++;  /* go to next char in pattern */
      if (j == pl) {  /* complete match? */
        luaL_pushresult(&b);  /* close buffer */
        return 1;  /* always success */
      }
    }
    else if (j == 0)
      luaL_putwchar(&b, c);
    else {  /* match fail */
      luaL_addlustring(&b, p, j - next[j]);  /* put failed part on result */
      j = next[j];  /* backtrack pattern index */
      goto NoRead;  /* repeat without reading next char */
    }
  }
  /* end of file without a match */
  luaL_addlustring(&b, p, j);  /* put failed part on result */
  luaL_pushresult(&b);  /* close buffer */
  return (lua_strlen(L, -1) > 0);
}
コード例 #6
0
BOOL GetNextLine(FILE* fp,LPWSTR pwszDest,int iSize)
{
	//BOOL bRet;
	WCHAR c =0 ;
	int x = 0;
	do 
	{
		if (x < iSize )
        {
            // Get a character
            c = getwc(fp);
		    // Handle the end
            if (x ==0 && ((char)c) == EOF )
			    return FALSE;
                	
		    // If it is NOT a carriage return - save the character
            // (note the cast to (char): as EOF is a char -1.
            // with a WIDE character this becomes 65535)
            // so we cast DOWN the char to test for the potential -1
            if (c!= '\r' && ((char)c) != EOF && c != '\n')
			    pwszDest[x++] = c;
	        else
                break;
        }			
	} while (c != '\r' && c != EOF && c != '\n' && c !=65535);

	pwszDest[x] = NULL;
	return TRUE;
}
コード例 #7
0
ファイル: t_io.c プロジェクト: 2trill2spill/freebsd
ATF_TC_BODY(bad_big5_getwc, tc)
{
	const char buf[] = { 0xcf, 0x20 };
	struct ibuf ib = {
		.buf = buf,
		.buflen = sizeof(buf),
	};
	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);

	ATF_REQUIRE(fp != NULL);
	setlocale(LC_CTYPE, "zh_TW.Big5");
	ATF_REQUIRE_EQ(getwc(fp), WEOF);
	fclose(fp);
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, bad_big5_wprintf);
	ATF_TP_ADD_TC(tp, bad_big5_swprintf);
	ATF_TP_ADD_TC(tp, good_big5_wprintf);
	ATF_TP_ADD_TC(tp, good_big5_swprintf);
	ATF_TP_ADD_TC(tp, good_big5_getwc);
	ATF_TP_ADD_TC(tp, bad_big5_getwc);

	return atf_no_error();
}
コード例 #8
0
ファイル: dict-check.c プロジェクト: starsep/Spellcheck
/**
 Wczytuje cały input
 @return vector znaków, które wczytano
 */
vector * read_input(void)
{
	vector *buffer = vector_new(sizeof(wchar_t), BUFFER_START_SIZE);
	wchar_t c;
	while((c = getwc(stdin)) != (wchar_t)WEOF)
		vector_push_back(buffer, &c);
	return buffer;
}
コード例 #9
0
ファイル: string2.c プロジェクト: hseos/hseos-course
/*
используем Wide Char API для обработки текста
*/
int main(void)
{
    wchar_t c;
    setlocale(LC_ALL, "");
    while ((c = getwc(stdin)) != EOF) {
        wprintf(L"%Lc\n", (int) c);
    }
}
コード例 #10
0
ファイル: common.cpp プロジェクト: berdario/fish-shell
int fgetws2( wchar_t **b, int *len, FILE *f )
{
	int i=0;
	wint_t c;
	
	wchar_t *buff = *b;

	while( 1 )
	{
		/* Reallocate the buffer if necessary */
		if( i+1 >= *len )
		{
			int new_len = maxi( 128, (*len)*2);
			buff = (wchar_t *)realloc( buff, sizeof(wchar_t)*new_len );
			if( buff == 0 )
			{
				DIE_MEM();
			}
			else
			{
				*len = new_len;
				*b = buff;
			}			
		}
		
		errno=0;		
		
		c = getwc( f );
		
		if( errno == EILSEQ )
		{
			continue;
		}
	
//fwprintf( stderr, L"b\n" );
		
		switch( c )
		{
			/* End of line */ 
			case WEOF:
			case L'\n':
			case L'\0':
				buff[i]=L'\0';
				return i;				
				/* Ignore carriage returns */
			case L'\r':
				break;
				
			default:
				buff[i++]=c;
				break;
		}		


	}

}
コード例 #11
0
ファイル: FileStdStream.cpp プロジェクト: ByeDream/pixellight
int FileStdStream::GetC()
{
	#ifdef WIN32
		// Read character
		if (IsReadable())
			return ((m_nStringFormat == String::ASCII) ? getc(m_pFile) : getwc(m_pFile));

		// Error!
		return -1;
	#else
		// Read character
		return m_pFile ? getc(m_pFile) : -1;
	#endif
}
コード例 #12
0
ファイル: AppParse.cpp プロジェクト: rabbie/syslogagent
/********************************************************
ernogetc --	wrapper for widechar and char getc
********************************************************/
char ernogetc(FILE *fp,bool unicode) {
	char a;

	try{
		if (unicode) {
			a=getwc(fp);
			//since a is not wide, it's automatically cleaned
		} else {
			a=getc(fp);
		}
		return a;
	}
	catch(...) { //is it a wide char file?
	}

}
コード例 #13
0
ファイル: writelog.cpp プロジェクト: biglad/WinUAE
TCHAR console_getch (void)
{
	if (realconsole) {
		return getwc (stdin);
	} else if (consoleopen < 0) {
		DWORD len;
		TCHAR out[2];
		
		for (;;) {
			out[0] = 0;
			ReadConsole (stdinput, out, 1, &len, 0);
			if (len > 0)
				return out[0];
		}
	}
	return 0;
}
コード例 #14
0
ファイル: dict-check.c プロジェクト: Pand9/IPP-2015
/**
 * Weryfikuje tekst ze strumienia z podanym słownikiem.
 *
 * Wypisuje informacje na standardowe wyjście i opcjonalnie wyjście błędów.
 * Tekst jest przepisywany na standardowe wyjście, za jednym wyjątkiem: słowa
 * (zdefiniowane jako spójne ciągi liter, zgodnie z tym, co zwraca funkcja
 * biblioteczna iswalpha) są poprzedzane znakiem '#', jeśli nie ma ich
 * w słowniku.
 *
 * Opcjonalnie, na wyjście błędów wypisywane są: pozycje, słowa i podpowiedzi
 * do słów ze standardowego wejścia, których nie ma w słowniku.
 *
 * @param [in] dict Słownik, z którym jest weryfikowany tekst.
 * @param [in] verbose Jeśli "true", drukowane są podpowiedzi na stderr.
 * @param [in] in Strumień tekstu. Jest wczytywany do napotkania znaku WEOF.
 */
void process_input(const struct dictionary * dict, bool verbose, FILE * in)
{
    struct parser_state pstate = { .dict = dict, .verbose = verbose,
        .line_num = 1, .column_num = 1, .last_non_word_column_num = 0,
        .buffer_iterator = word_buffer };

    wchar_t c;
    do
    {
        c = getwc(in);
        if (iswalpha(c))
        {
            cache_letter(&pstate.buffer_iterator, c);
        }
        else
        {
            if (word_buffer != pstate.buffer_iterator)
                process_word(&pstate);
            process_not_letter(c);
        }
        parser_state_update_position(&pstate, c);
    } while (WEOF != (wint_t) c);
}

/**
 * Przetwarza tekst ze standardowego wejścia i sprawdza go ze słownikiem
 * zapisanym w danej ścieżce. Format słownika musi odpowiadać formatowi
 * obsługiwanemu przez bibliotekę dictionary.
 * Szczegóły - patrz także: process_input.
 * @param [in] dict_path Ścieżka do słownika do wczytania.
 * @param [in] verbose Gdy flaga jest ustawiona, dodatkowe podpowiedzi
 * drukowane są na wyjście błędów.
 * @return Niezerowa wartość w przypadku błędu przy odczycie słownika z dysku.
 * 0 wpp.
 */
int process(const char * dict_path, bool verbose)
{
    int error_value;
    struct dictionary * dict;
    if (0 != (error_value = load_dictionary(dict_path, &dict)))
        return error_value;

    process_input(dict, verbose, stdin);
    dictionary_done(dict);
    return 0;
}
コード例 #15
0
ファイル: tst_getwc.c プロジェクト: AdvancedC/glibc
int
main (void)
{
  wchar_t buf[100];
  size_t n = 0;

  wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
  while (! feof (stdin) && n < sizeof (buf) - 1)
    {
      buf[n] = getwc (stdin);
      if (buf[n] == WEOF)
	break;
      ++n;
    }
  buf[n] = L'\0';

  return wcscmp (buf, L"This is a test of getwc\n") != 0;
}
コード例 #16
0
ファイル: trie.c プロジェクト: starsep/Spellcheck
/**
 Wczytuje z pliku dany węzęł
 @param[in,out] node Węzeł
 @param[in,out] stream Plik
 @param[in,out] alpha Alfabet
 @return Czy się udało
 */
static bool node_load(trie_node *node, FILE *stream, alphabet *alpha)
{
	assert(node != NULL);
	wchar_t c;
	bool res = true;
	while((c = getwc(stream)) != (wchar_t)WEOF)
	{
		if(c == END_OF_TRIE_FILE)
			return res;
		if(c == END_OF_WORD)
		{
			node->end_of_word = true;
			return true;
		}
		if(c == NOT_END_OF_WORD)
			return true;
		res &= node_load(node_add_son(node, c), stream, alpha);
		alphabet_add(alpha, c);
	}
	return false;
}
コード例 #17
0
ファイル: t_io.c プロジェクト: 2trill2spill/freebsd
ATF_TC_BODY(good_big5_getwc, tc)
{
	const char buf[] = { 0xcf, 0x40 };
	struct ibuf ib = {
		.buf = buf,
		.buflen = sizeof(buf),
	};
	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);

	ATF_REQUIRE(fp != NULL);
	setlocale(LC_CTYPE, "zh_TW.Big5");
	/* XXX implementation detail knowledge (wchar_t encoding) */
	ATF_REQUIRE_EQ(getwc(fp), 0xcf40);
	fclose(fp);
}

ATF_TC(bad_big5_getwc);
ATF_TC_HEAD(bad_big5_getwc, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar getwc");
}
コード例 #18
0
int console_get (TCHAR *out, int maxlen)
{
    *out = 0;

    set_console_input_mode(1);
    if (consoleopen > 0) {
        return console_get_gui (out, maxlen);
    } else if (realconsole) {
        DWORD totallen;

        *out = 0;
        totallen = 0;
        while (maxlen > 0) {
            *out = getwc (stdin);
            if (*out == 13)
                break;
            out++;
            maxlen--;
            totallen++;
        }
        *out = 0;
        return totallen;
    } else if (consoleopen < 0) {
        DWORD len, totallen;

        *out = 0;
        totallen = 0;
        while(maxlen > 0) {
            ReadConsole (stdinput, out, 1, &len, 0);
            if(*out == 13)
                break;
            out++;
            maxlen--;
            totallen++;
        }
        *out = 0;
        return totallen;
    }
    return 0;
}
コード例 #19
0
ファイル: paste.c プロジェクト: 2asoft/freebsd
static int
sequential(char **argv)
{
	FILE *fp;
	int cnt, failed, needdelim;
	wint_t ch;
	char *p;

	failed = 0;
	for (; (p = *argv); ++argv) {
		if (p[0] == '-' && !p[1])
			fp = stdin;
		else if (!(fp = fopen(p, "r"))) {
			warn("%s", p);
			failed = 1;
			continue;
		}
		cnt = needdelim = 0;
		while ((ch = getwc(fp)) != WEOF) {
			if (needdelim) {
				needdelim = 0;
				if (delim[cnt] != '\0')
					putwchar(delim[cnt]);
				if (++cnt == delimcnt)
					cnt = 0;
			}
			if (ch != '\n')
				putwchar(ch);
			else
				needdelim = 1;
		}
		if (needdelim)
			putwchar('\n');
		if (fp != stdin)
			(void)fclose(fp);
	}

	return (failed != 0);
}
コード例 #20
0
static void
filter(FILE *f)
{
	wint_t c;
	int i, w;

	while ((c = getwc(f)) != WEOF && col < MAXBUF) switch(c) {

	case '\b':
		if (col > 0)
			col--;
		continue;

	case '\t':
		col = (col+8) & ~07;
		if (col > maxcol)
			maxcol = col;
		continue;

	case '\r':
		col = 0;
		continue;

	case SO:
		mode |= ALTSET;
		continue;

	case SI:
		mode &= ~ALTSET;
		continue;

	case IESC:
		switch (c = getwc(f)) {

		case HREV:
			if (halfpos == 0) {
				mode |= SUPERSC;
				halfpos--;
			} else if (halfpos > 0) {
				mode &= ~SUBSC;
				halfpos--;
			} else {
				halfpos = 0;
				reverse();
			}
			continue;

		case HFWD:
			if (halfpos == 0) {
				mode |= SUBSC;
				halfpos++;
			} else if (halfpos < 0) {
				mode &= ~SUPERSC;
				halfpos++;
			} else {
				halfpos = 0;
				fwd();
			}
			continue;

		case FREV:
			reverse();
			continue;

		default:
			errx(1, "unknown escape sequence in input: %o, %o", IESC, c);
		}
		continue;

	case '_':
		if (obuf[col].c_char || obuf[col].c_width < 0) {
			while (col > 0 && obuf[col].c_width < 0)
				col--;
			w = obuf[col].c_width;
			for (i = 0; i < w; i++)
				obuf[col++].c_mode |= UNDERL | mode;
			if (col > maxcol)
				maxcol = col;
			continue;
		}
		obuf[col].c_char = '_';
		obuf[col].c_width = 1;
		/* FALLTHROUGH */
	case ' ':
		col++;
		if (col > maxcol)
			maxcol = col;
		continue;

	case '\n':
		flushln();
		continue;

	case '\f':
		flushln();
		putwchar('\f');
		continue;

	default:
		if ((w = wcwidth(c)) <= 0)	/* non printing */
			continue;
		if (obuf[col].c_char == '\0') {
			obuf[col].c_char = c;
			for (i = 0; i < w; i++)
				obuf[col + i].c_mode = mode;
			obuf[col].c_width = w;
			for (i = 1; i < w; i++)
				obuf[col + i].c_width = -1;
		} else if (obuf[col].c_char == '_') {
			obuf[col].c_char = c;
			for (i = 0; i < w; i++)
				obuf[col + i].c_mode |= UNDERL|mode;
			obuf[col].c_width = w;
			for (i = 1; i < w; i++)
				obuf[col + i].c_width = -1;
		} else if ((wint_t)obuf[col].c_char == c) {
			for (i = 0; i < w; i++)
				obuf[col + i].c_mode |= BOLD|mode;
		} else {
			w = obuf[col].c_width;
			for (i = 0; i < w; i++)
				obuf[col + i].c_mode = mode;
		}
		col += w;
		if (col > maxcol)
			maxcol = col;
		continue;
	}
	if (ferror(f))
		err(1, NULL);
	if (maxcol)
		flushln();
}
コード例 #21
0
ファイル: y1.c プロジェクト: AlfredArouna/illumos-gate
/* put out other arrays, copy the parsers */
static void
others()
{
	extern int gen_lines;
	int c, i, j;
	int tmpline;

	finput = fopen(parser, "r");
	if (finput == NULL)
/*
 * TRANSLATION_NOTE  -- This is a message from yacc.
 *	This message is passed to error() function.
 *	This error message is issued when yacc can not find
 *	the parser to be copied.
 */
		error(gettext(
		"cannot find parser %s"),
		    parser);

	warray(L"yyr1", levprd, nprod);

	aryfil(temp1, nprod, 0);
					/* had_act[i] is either 1 or 0 */
	PLOOP(1, i)
		temp1[i] = ((prdptr[i+1] - prdptr[i]-2) << 1) | had_act[i];
	warray(L"yyr2", temp1, nprod);

	aryfil(temp1, nstate, -10000000);
	TLOOP(i)
		for (j = tstates[i]; j != 0; j = mstates[j])
			temp1[j] = tokset[i].value;
	NTLOOP(i)
		for (j = ntstates[i]; j != 0; j = mstates[j])
			temp1[j] = -i;
	warray(L"yychk", temp1, nstate);

	warray(L"yydef", defact, nstate);

	if ((fdebug = fopen(DEBUGNAME, "r")) == NULL)
		error("cannot open yacc.debug");
	while ((c = getwc(fdebug)) != EOF)
		(void) putwc(c, ftable);
	(void) fclose(fdebug);
	ZAPFILE(DEBUGNAME);

	if (gen_lines)
		(void) fprintf(ftable, "# line\t1 \"%s\"\n", parser);
	tmpline = 1;
	/* copy parser text */
	while ((c = getwc(finput)) != EOF) {
		if (c == '\n')
			tmpline++;
		if (c == L'$') {
			if ((c = getwc(finput)) != L'A')
				(void) putwc(L'$', ftable);
			else { /* copy actions */
				tmpline++;
				faction = fopen(ACTNAME, "r");
				if (faction == NULL)
/*
 * TRANSLATION_NOTE  -- This is a message from yacc.
 *	This message is passed to error() function.
 *	This error is issued when yacc can not open a
 *	temporary file to be used. You do not need to
 *	use the word 'tempfile'. You can translate it to
 *	mean 'temporary file'.
 */
					error(gettext(
					"cannot open action tempfile"));
				while ((c = getwc(faction)) != EOF)
					(void) putwc(c, ftable);
				(void) fclose(faction);
				if (gen_lines)
					(void) fprintf(ftable,
					    "\n# line\t%d \"%s\"",
					    tmpline,
					    parser);
				ZAPFILE(ACTNAME);
				c = getwc(finput);
			}
		}
		(void) putwc(c, ftable);
	}
	(void) fclose(ftable);
}
コード例 #22
0
ファイル: net.c プロジェクト: knight-erraunt/freebsd
static int
do_protocol(const char *name, const struct addrinfo *ai)
{
	int cnt, line_len, s;
	FILE *fp;
	wint_t c, lastc;
	struct iovec iov[3];
	struct msghdr msg;
	static char slash_w[] = "/W ";
	static char neteol[] = "\r\n";

	s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
	if (s < 0) {
		warn("socket(%d, %d, %d)", ai->ai_family, ai->ai_socktype,
		     ai->ai_protocol);
		return -1;
	}

	msg.msg_name = (void *)ai->ai_addr;
	msg.msg_namelen = ai->ai_addrlen;
	msg.msg_iov = iov;
	msg.msg_iovlen = 0;
	msg.msg_control = 0;
	msg.msg_controllen = 0;
	msg.msg_flags = 0;

	/* -l flag for remote fingerd  */
	if (lflag) {
		iov[msg.msg_iovlen].iov_base = slash_w;
		iov[msg.msg_iovlen++].iov_len = 3;
	}
	/* send the name followed by <CR><LF> */
	iov[msg.msg_iovlen].iov_base = strdup(name);
	iov[msg.msg_iovlen++].iov_len = strlen(name);
	iov[msg.msg_iovlen].iov_base = neteol;
	iov[msg.msg_iovlen++].iov_len = 2;

	if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
		warn("connect");
		close(s);
		return -1;
	}

	if (sendmsg(s, &msg, 0) < 0) {
		warn("sendmsg");
		close(s);
		return -1;
	}

	/*
	 * Read from the remote system; once we're connected, we assume some
	 * data.  If none arrives, we hang until the user interrupts.
	 *
	 * If we see a <CR> or a <CR> with the high bit set, treat it as
	 * a newline; if followed by a newline character, only output one
	 * newline.
	 *
	 * Otherwise, all high bits are stripped; if it isn't printable and
	 * it isn't a space, we can simply set the 7th bit.  Every ASCII
	 * character with bit 7 set is printable.
	 */
	lastc = 0;
	if ((fp = fdopen(s, "r")) != NULL) {
		cnt = 0;
		line_len = 0;
		while ((c = getwc(fp)) != EOF) {
			if (++cnt > OUTPUT_MAX) {
				printf("\n\n Output truncated at %d bytes...\n",
					cnt - 1);
				break;
			}
			if (c == 0x0d) {
				if (lastc == '\r')	/* ^M^M - skip dupes */
					continue;
				c = '\n';
				lastc = '\r';
			} else {
				if (!iswprint(c) && !iswspace(c)) {
					c &= 0x7f;
					c |= 0x40;
				}
				if (lastc != '\r' || c != '\n')
					lastc = c;
				else {
					lastc = '\n';
					continue;
				}
			}
			putwchar(c);
			if (c != '\n' && ++line_len > _POSIX2_LINE_MAX) {
				putchar('\\');
				putchar('\n');
				lastc = '\r';
			}
			if (lastc == '\n' || lastc == '\r')
				line_len = 0;
		}
		if (ferror(fp)) {
			/*
			 * Assume that whatever it was set errno...
			 */
			warn("reading from network");
		}
		if (lastc != L'\n')
			putchar('\n');

		fclose(fp);
	}
	return 0;
}
コード例 #23
0
int
main(int argc, char *argv[])
{
	wint_t c;
	wchar_t *cp, *dp;
	int ch, i, w;

	setlocale(LC_ALL, "");

	while ((ch = getopt(argc, argv, "-2")) != -1)
		switch (ch) {
		case '-':
			suppresul = 1;
			break;
		case '2':
			printall = 1;
			break;
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	do {
		if (argc > 0) {
			if (freopen(argv[0], "r", stdin) == NULL) {
				fflush(stdout);
				err(1, "%s", argv[0]);
			}
			argc--;
			argv++;
		}
		for (;;) {
			c = getwc(stdin);
			if (c == WEOF) {
				pflush(outline);
				fflush(stdout);
				break;
			}
			switch (c) {
			case '\n':
				if (outline >= 265)
					pflush(62);
				outline += 2;
				outcol = 0;
				continue;
			case '\016':
			case '\017':
				continue;
			case 033:
				c = getwc(stdin);
				switch (c) {
				case '9':
					if (outline >= 266)
						pflush(62);
					outline++;
					continue;
				case '8':
					if (outline >= 1)
						outline--;
					continue;
				case '7':
					outline -= 2;
					if (outline < 0)
						outline = 0;
					continue;
				default:
					continue;
				}
			case '\b':
				if (outcol)
					outcol--;
				continue;
			case '\t':
				outcol += 8;
				outcol &= ~7;
				outcol--;
				c = ' ';
			default:
				if ((w = wcwidth(c)) <= 0)
					w = 1;	/* XXX */
				if (outcol + w > 132) {
					outcol += w;
					continue;
				}
				cp = &page[outline][outcol];
				outcol += w;
				if (c == '_') {
					if (suppresul)
						continue;
					cp += 132;
					c = '-';
				}
				if (*cp == 0) {
					for (i = 0; i < w; i++)
						cp[i] = c;
					dp = cp - (outcol - w);
					for (cp--; cp >= dp && *cp == 0; cp--)
						*cp = ' ';
				} else {
					if (plus(c, *cp) || plus(*cp, c))
						*cp = '+';
					else if (*cp == ' ' || *cp == 0) {
						for (i = 1; i < w; i++)
							if (cp[i] != ' ' &&
							    cp[i] != 0)
								goto cont;
						for (i = 0; i < w; i++)
							cp[i] = c;
					}
				}
cont:
				continue;
			}
		}
		if (ferror(stdin))
			err(1, NULL);
	} while (argc > 0);
	fflush(stdout);
	exit(0);
}
コード例 #24
0
ファイル: wchar_h.pass.cpp プロジェクト: okuoku/libcxx
int main()
{
    mbstate_t mb = {0};
    size_t s = 0;
    tm tm = {0};
    wint_t w = 0;
    ::FILE* fp = 0;
    __darwin_va_list va;
    char* ns = 0;
    wchar_t* ws = 0;
    static_assert((std::is_same<decltype(fwprintf(fp, L"")), int>::value), "");
    static_assert((std::is_same<decltype(fwscanf(fp, L"")), int>::value), "");
    static_assert((std::is_same<decltype(swprintf(ws, s, L"")), int>::value), "");
    static_assert((std::is_same<decltype(swscanf(L"", L"")), int>::value), "");
    static_assert((std::is_same<decltype(vfwprintf(fp, L"", va)), int>::value), "");
    static_assert((std::is_same<decltype(vfwscanf(fp, L"", va)), int>::value), "");
    static_assert((std::is_same<decltype(vswprintf(ws, s, L"", va)), int>::value), "");
    static_assert((std::is_same<decltype(vswscanf(L"", L"", va)), int>::value), "");
    static_assert((std::is_same<decltype(vwprintf(L"", va)), int>::value), "");
    static_assert((std::is_same<decltype(vwscanf(L"", va)), int>::value), "");
    static_assert((std::is_same<decltype(wprintf(L"")), int>::value), "");
    static_assert((std::is_same<decltype(wscanf(L"")), int>::value), "");
    static_assert((std::is_same<decltype(fgetwc(fp)), wint_t>::value), "");
    static_assert((std::is_same<decltype(fgetws(ws, 0, fp)), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(fputwc(L' ', fp)), wint_t>::value), "");
    static_assert((std::is_same<decltype(fputws(L"", fp)), int>::value), "");
    static_assert((std::is_same<decltype(fwide(fp, 0)), int>::value), "");
    static_assert((std::is_same<decltype(getwc(fp)), wint_t>::value), "");
    static_assert((std::is_same<decltype(getwchar()), wint_t>::value), "");
    static_assert((std::is_same<decltype(putwc(L' ', fp)), wint_t>::value), "");
    static_assert((std::is_same<decltype(putwchar(L' ')), wint_t>::value), "");
    static_assert((std::is_same<decltype(ungetwc(L' ', fp)), wint_t>::value), "");
    static_assert((std::is_same<decltype(wcstod(L"", (wchar_t**)0)), double>::value), "");
    static_assert((std::is_same<decltype(wcstof(L"", (wchar_t**)0)), float>::value), "");
    static_assert((std::is_same<decltype(wcstold(L"", (wchar_t**)0)), long double>::value), "");
    static_assert((std::is_same<decltype(wcstol(L"", (wchar_t**)0, 0)), long>::value), "");
    static_assert((std::is_same<decltype(wcstoll(L"", (wchar_t**)0, 0)), long long>::value), "");
    static_assert((std::is_same<decltype(wcstoul(L"", (wchar_t**)0, 0)), unsigned long>::value), "");
    static_assert((std::is_same<decltype(wcstoull(L"", (wchar_t**)0, 0)), unsigned long long>::value), "");
    static_assert((std::is_same<decltype(wcscpy(ws, L"")), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wcsncpy(ws, L"", s)), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wcscat(ws, L"")), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wcsncat(ws, L"", s)), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wcscmp(L"", L"")), int>::value), "");
    static_assert((std::is_same<decltype(wcscoll(L"", L"")), int>::value), "");
    static_assert((std::is_same<decltype(wcsncmp(L"", L"", s)), int>::value), "");
    static_assert((std::is_same<decltype(wcsxfrm(ws, L"", s)), size_t>::value), "");
    static_assert((std::is_same<decltype(wcschr((wchar_t*)0, L' ')), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wcscspn(L"", L"")), size_t>::value), "");
    static_assert((std::is_same<decltype(wcslen(L"")), size_t>::value), "");
    static_assert((std::is_same<decltype(wcspbrk((wchar_t*)0, L"")), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wcsrchr((wchar_t*)0, L' ')), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wcsspn(L"", L"")), size_t>::value), "");
    static_assert((std::is_same<decltype(wcsstr((wchar_t*)0, L"")), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wcstok(ws, L"", (wchar_t**)0)), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wmemchr((wchar_t*)0, L' ', s)), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wmemcmp(L"", L"", s)), int>::value), "");
    static_assert((std::is_same<decltype(wmemcpy(ws, L"", s)), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wmemmove(ws, L"", s)), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wmemset(ws, L' ', s)), wchar_t*>::value), "");
    static_assert((std::is_same<decltype(wcsftime(ws, s, L"", &tm)), size_t>::value), "");
    static_assert((std::is_same<decltype(btowc(0)), wint_t>::value), "");
    static_assert((std::is_same<decltype(wctob(w)), int>::value), "");
    static_assert((std::is_same<decltype(mbsinit(&mb)), int>::value), "");
    static_assert((std::is_same<decltype(mbrlen("", s, &mb)), size_t>::value), "");
    static_assert((std::is_same<decltype(mbrtowc(ws, "", s, &mb)), size_t>::value), "");
    static_assert((std::is_same<decltype(wcrtomb(ns, L' ', &mb)), size_t>::value), "");
    static_assert((std::is_same<decltype(mbsrtowcs(ws, (const char**)0, s, &mb)), size_t>::value), "");
    static_assert((std::is_same<decltype(wcsrtombs(ns, (const wchar_t**)0, s, &mb)), size_t>::value), "");
}
コード例 #25
0
ファイル: colrm.c プロジェクト: bdwalton/util-linux
int process_input(unsigned long first, unsigned long last)
{
	unsigned long ct = 0;
	wint_t c;
	unsigned long i;
	int w;
	int padding;

	for (;;) {
		c = getwc(stdin);
		if (c == WEOF)
			return 0;
		if (c == '\t')
			w = ((ct + 8) & ~7) - ct;
		else if (c == '\b')
			w = (ct ? ct - 1 : 0) - ct;
		else {
			w = wcwidth(c);
			if (w < 0)
				w = 0;
		}
		ct += w;
		if (c == '\n') {
			putwc(c, stdout);
			ct = 0;
			continue;

		}
		if (!first || ct < first) {
			putwc(c, stdout);
			continue;
		}
		break;
	}

	for (i = ct - w + 1; i < first; i++)
		putwc(' ', stdout);

	/* Loop getting rid of characters */
	while (!last || ct < last) {
		c = getwc(stdin);
		if (c == WEOF)
			return 0;
		if (c == '\n') {
			putwc(c, stdout);
			return 1;
		}
		if (c == '\t')
			ct = (ct + 8) & ~7;
		else if (c == '\b')
			ct = ct ? ct - 1 : 0;
		else {
			w = wcwidth(c);
			if (w < 0)
				w = 0;
			ct += w;
		}
	}

	padding = 0;

	/* Output last of the line */
	for (;;) {
		c = getwc(stdin);
		if (c == WEOF)
			break;
		if (c == '\n') {
			putwc(c, stdout);
			return 1;
		}
		if (padding == 0 && last < ct) {
			for (i = last; i < ct; i++)
				putwc(' ', stdout);
			padding = 1;
		}
		putwc(c, stdout);
	}
	return 0;
}
コード例 #26
0
ファイル: colrm.c プロジェクト: Claruarius/stblinux-2.6.37
int
main(int argc, char **argv)
{
    register int ct, first, last;
    register wint_t c;
    int i, w;
    int padding;

    setlocale(LC_ALL, "");

    first = 0;
    last = 0;
    if (argc > 1)
        first = atoi(*++argv);
    if (argc > 2)
        last = atoi(*++argv);

start:
    ct = 0;
loop1:
    c = getwc(stdin);
    if (c == WEOF)
        goto fin;
    if (c == '\t')
        w = ((ct + 8) & ~7) - ct;
    else if (c == '\b')
        w = (ct ? ct - 1 : 0) - ct;
    else {
        w = wcwidth(c);
        if (w < 0)
            w = 0;
    }
    ct += w;
    if (c == '\n') {
        putwc(c, stdout);
        goto start;
    }
    if (!first || ct < first) {
        putwc(c, stdout);
        goto loop1;
    }
    for (i = ct-w+1; i < first; i++)
        putwc(' ', stdout);

    /* Loop getting rid of characters */
    while (!last || ct < last) {
        c = getwc(stdin);
        if (c == WEOF)
            goto fin;
        if (c == '\n') {
            putwc(c, stdout);
            goto start;
        }
        if (c == '\t')
            ct = (ct + 8) & ~7;
        else if (c == '\b')
            ct = ct ? ct - 1 : 0;
        else {
            w = wcwidth(c);
            if (w < 0)
                w = 0;
            ct += w;
        }
    }

    padding = 0;

    /* Output last of the line */
    for (;;) {
        c = getwc(stdin);
        if (c == WEOF)
            break;
        if (c == '\n') {
            putwc(c, stdout);
            goto start;
        }
        if (padding == 0 && last < ct) {
            for (i = last; i <ct; i++)
                putwc(' ', stdout);
            padding = 1;
        }
        putwc(c, stdout);
    }
fin:
    fflush(stdout);
    if (ferror(stdout) || fclose(stdout))
        return 1;
    return 0;
}
コード例 #27
0
ファイル: paste.c プロジェクト: 2asoft/freebsd
static int
parallel(char **argv)
{
	LIST *lp;
	int cnt;
	wint_t ich;
	wchar_t ch;
	char *p;
	LIST *head, *tmp;
	int opencnt, output;

	for (cnt = 0, head = tmp = NULL; (p = *argv); ++argv, ++cnt) {
		if ((lp = malloc(sizeof(LIST))) == NULL)
			err(1, NULL);
		if (p[0] == '-' && !p[1])
			lp->fp = stdin;
		else if (!(lp->fp = fopen(p, "r")))
			err(1, "%s", p);
		lp->next = NULL;
		lp->cnt = cnt;
		lp->name = p;
		if (!head)
			head = tmp = lp;
		else {
			tmp->next = lp;
			tmp = lp;
		}
	}

	for (opencnt = cnt; opencnt;) {
		for (output = 0, lp = head; lp; lp = lp->next) {
			if (!lp->fp) {
				if (output && lp->cnt &&
				    (ch = delim[(lp->cnt - 1) % delimcnt]))
					putwchar(ch);
				continue;
			}
			if ((ich = getwc(lp->fp)) == WEOF) {
				if (!--opencnt)
					break;
				lp->fp = NULL;
				if (output && lp->cnt &&
				    (ch = delim[(lp->cnt - 1) % delimcnt]))
					putwchar(ch);
				continue;
			}
			/*
			 * make sure that we don't print any delimiters
			 * unless there's a non-empty file.
			 */
			if (!output) {
				output = 1;
				for (cnt = 0; cnt < lp->cnt; ++cnt)
					if ((ch = delim[cnt % delimcnt]))
						putwchar(ch);
			} else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
				putwchar(ch);
			if (ich == '\n')
				continue;
			do {
				putwchar(ich);
			} while ((ich = getwc(lp->fp)) != WEOF && ich != '\n');
		}
		if (output)
			putwchar('\n');
	}

	return (0);
}
コード例 #28
0
ファイル: cat.c プロジェクト: Broadcom/freebsd-nxt
static void
cook_cat(FILE *fp)
{
	int ch, gobble, line, prev;
	wint_t wch;

	/* Reset EOF condition on stdin. */
	if (fp == stdin && feof(stdin))
		clearerr(stdin);

	line = gobble = 0;
	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
		if (prev == '\n') {
			if (sflag) {
				if (ch == '\n') {
					if (gobble)
						continue;
					gobble = 1;
				} else
					gobble = 0;
			}
			if (nflag && (!bflag || ch != '\n')) {
				(void)fprintf(stdout, "%6d\t", ++line);
				if (ferror(stdout))
					break;
			}
		}
		if (ch == '\n') {
			if (eflag && putchar('$') == EOF)
				break;
		} else if (ch == '\t') {
			if (tflag) {
				if (putchar('^') == EOF || putchar('I') == EOF)
					break;
				continue;
			}
		} else if (vflag) {
			(void)ungetc(ch, fp);
			/*
			 * Our getwc(3) doesn't change file position
			 * on error.
			 */
			if ((wch = getwc(fp)) == WEOF) {
				if (ferror(fp) && errno == EILSEQ) {
					clearerr(fp);
					/* Resync attempt. */
					memset(&fp->_mbstate, 0, sizeof(mbstate_t));
					if ((ch = getc(fp)) == EOF)
						break;
					wch = ch;
					goto ilseq;
				} else
					break;
			}
			if (!iswascii(wch) && !iswprint(wch)) {
ilseq:
				if (putchar('M') == EOF || putchar('-') == EOF)
					break;
				wch = toascii(wch);
			}
			if (iswcntrl(wch)) {
				ch = toascii(wch);
				ch = (ch == '\177') ? '?' : (ch | 0100);
				if (putchar('^') == EOF || putchar(ch) == EOF)
					break;
				continue;
			}
			if (putwchar(wch) == WEOF)
				break;
			ch = -1;
			continue;
		}
		if (putchar(ch) == EOF)
			break;
	}
	if (ferror(fp)) {
		warn("%s", filename);
		rval = 1;
		clearerr(fp);
	}
	if (ferror(stdout))
		err(1, "stdout");
}
コード例 #29
0
ファイル: ul.c プロジェクト: Romutk/lab3.2n
void filter(FILE *f)
{
	wint_t c;
	int i, w;

	while ((c = getwc(f)) != WEOF) switch(c) {

	case '\b':
		setcol(col - 1);
		continue;

	case '\t':
		setcol((col+8) & ~07);
		continue;

	case '\r':
		setcol(0);
		continue;

	case SO:
		mode |= ALTSET;
		continue;

	case SI:
		mode &= ~ALTSET;
		continue;

	case IESC:
		switch (c = getwc(f)) {

		case HREV:
			if (halfpos == 0) {
				mode |= SUPERSC;
				halfpos--;
			} else if (halfpos > 0) {
				mode &= ~SUBSC;
				halfpos--;
			} else {
				halfpos = 0;
				reverse();
			}
			continue;

		case HFWD:
			if (halfpos == 0) {
				mode |= SUBSC;
				halfpos++;
			} else if (halfpos < 0) {
				mode &= ~SUPERSC;
				halfpos++;
			} else {
				halfpos = 0;
				fwd();
			}
			continue;

		case FREV:
			reverse();
			continue;

		default:
			errx(EXIT_FAILURE,
				_("unknown escape sequence in input: %o, %o"),
				IESC, c);
			break;
		}
		continue;

	case '_':
		if (obuf[col].c_char || obuf[col].c_width < 0) {
			while(col > 0 && obuf[col].c_width < 0)
				col--;
			w = obuf[col].c_width;
			for (i = 0; i < w; i++)
				obuf[col++].c_mode |= UNDERL | mode;
			setcol(col);
			continue;
		}
		obuf[col].c_char = '_';
		obuf[col].c_width = 1;
		/* fall through */
	case ' ':
		setcol(col + 1);
		continue;

	case '\n':
		flushln();
		continue;

	case '\f':
		flushln();
		putwchar('\f');
		continue;

	default:
		if (!iswprint(c))	/* non printing */
			continue;
		w = wcwidth(c);
		needcol(col + w);
		if (obuf[col].c_char == '\0') {
			obuf[col].c_char = c;
			for (i = 0; i < w; i++)
				obuf[col+i].c_mode = mode;
			obuf[col].c_width = w;
			for (i = 1; i < w; i++)
				obuf[col+i].c_width = -1;
		} else if (obuf[col].c_char == '_') {
			obuf[col].c_char = c;
			for (i = 0; i < w; i++)
				obuf[col+i].c_mode |= UNDERL|mode;
			obuf[col].c_width = w;
			for (i = 1; i < w; i++)
				obuf[col+i].c_width = -1;
		} else if (obuf[col].c_char == c) {
			for (i = 0; i < w; i++)
				obuf[col+i].c_mode |= BOLD|mode;
		} else {
			w = obuf[col].c_width;
			for (i = 0; i < w; i++)
				obuf[col+i].c_mode = mode;
		}
		setcol(col + w);
		continue;
	}
	if (maxcol)
		flushln();
}
コード例 #30
0
wint_t __cdecl _fgetwchar (
        void
        )
{
        return(getwc(stdin));
}