author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Mon, 07 Jun 2010 23:21:56 +0200 | |
changeset 133 | f46f9606dbfb |
parent 118 | 17dff2c6fc88 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
77 | 2 |
* ncurses.c |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
0 | 4 |
* |
5 |
* Unicode != UTF-8 |
|
6 |
* unicode with ncursesw ( wide char) |
|
7 |
* gcc $< -lncursesw -o $@ |
|
8 |
*/ |
|
9 |
||
10 |
#include <stdio.h> |
|
11 |
#include <string.h> |
|
12 |
#include <stdlib.h> |
|
13 |
#include <fcntl.h> |
|
118
17dff2c6fc88
wrong header: it's just ncurses.h
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
115
diff
changeset
|
14 |
#include <ncurses.h> |
0 | 15 |
#include <wchar.h> |
16 |
#include <locale.h> |
|
17 |
||
18 |
#ifndef u_char |
|
19 |
typedef unsigned char u_char; |
|
20 |
#endif |
|
21 |
||
22 |
#define ESCAPE 0x1B |
|
23 |
||
24 |
#ifndef get_wch |
|
25 |
extern int get_wch (); |
|
26 |
#endif |
|
27 |
||
28 |
int check_locale () |
|
29 |
{ |
|
30 |
if (!setlocale (LC_CTYPE, "")) { |
|
31 |
perror ("SETLOCALE"); |
|
115 | 32 |
exit (EXIT_FAILURE); |
0 | 33 |
} |
34 |
return EXIT_SUCCESS; |
|
35 |
} |
|
36 |
||
45
7197576fedcf
pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
27
diff
changeset
|
37 |
wchar_t func (WINDOW * win) |
0 | 38 |
{ |
115 | 39 |
wchar_t guess = '\0'; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
40 |
|
114
6f6850407ccf
buffer overflow in utf8 and ncurses demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
41 |
char dest[6]; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
42 |
|
0 | 43 |
int maxx; |
44 |
int maxy; |
|
45 |
||
46 |
while (guess != ESCAPE) { |
|
47 |
while ((get_wch (&guess)) != OK) |
|
48 |
/* |
|
49 |
* waiting for multibyte character |
|
50 |
*/ |
|
51 |
; |
|
52 |
||
53 |
maxx = getmaxx (win); |
|
54 |
maxy = getmaxy (win); |
|
55 |
||
114
6f6850407ccf
buffer overflow in utf8 and ncurses demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
56 |
memset (&dest, 0, sizeof (dest)); |
0 | 57 |
if (wctomb (dest, guess) == -1) { |
58 |
perror ("WCTOMB"); |
|
59 |
return -1; |
|
60 |
} |
|
61 |
||
62 |
mvprintw (maxy / 2, maxx / 2 - 12, |
|
63 |
"UTF-8: (%2X:%2X:%2X:%2X)\t\t", (u_char) dest[0], |
|
64 |
(u_char) dest[1], (u_char) dest[2], (u_char) dest[3]); |
|
65 |
||
66 |
mvprintw (maxy - 1, 0, "KEY: %2lc\t", guess); |
|
67 |
mvprintw (maxy - 1, maxx - 14, "UNICODE: %4X", guess); |
|
68 |
mvprintw (maxy - 1, maxx / 2 - 10, "Press ESC to quit"); |
|
69 |
} |
|
70 |
return guess; |
|
71 |
} |
|
72 |
||
73 |
int main () |
|
74 |
{ |
|
75 |
wchar_t x; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
76 |
|
0 | 77 |
WINDOW *win; |
78 |
||
79 |
check_locale (); |
|
80 |
win = initscr (); |
|
45
7197576fedcf
pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
27
diff
changeset
|
81 |
keypad (win, 1); |
0 | 82 |
noecho (); |
45
7197576fedcf
pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
27
diff
changeset
|
83 |
|
7197576fedcf
pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
27
diff
changeset
|
84 |
x = func (win); |
7197576fedcf
pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
27
diff
changeset
|
85 |
|
0 | 86 |
endwin (); |
87 |
printf ("\n"); |
|
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
88 |
|
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
89 |
return EXIT_SUCCESS; |
0 | 90 |
} |