faq 19.1

U

Uno

I've been working through the material in faq 19.1:
http://www.c-faq.com/osdep/cbreak.html
.. Only one main program is shown there, and this one is definitely not
behaving well:

$ pwd
/home/dan/source
$ gcc -Wall -Wextra -lcurses p3.c -o out
$ ./out
$ cat p3.c



#include <stdio.h>
#include <curses.h>

int tty_break()
{
initscr();
cbreak();
return 0;
}

int tty_getchar()
{
return getch();
}

int tty_fix()
{
endwin();
return 0;
}


int main(void)
{
int i;
if(tty_break() != 0)
return 1;
for(i = 0; i < 10; i++)
printf(" = %d\n", tty_getchar());
tty_fix();
return 0;
}




// gcc -Wall -Wextra -lcurses p3.c -o out
$

So far I have output that looks like this and then disappears:
http://i50.tinypic.com/28bxm43.png

I have a variety of questions about this library written in C. I don't
get what tty_fix is supposed to be doing, partially because I don't know
what endwin is doing.

How and where do I grep for endwin()?

Thanks for your comment, and cheers,
 
B

Ben Bacarisse

Uno said:
I've been working through the material in faq 19.1:
http://www.c-faq.com/osdep/cbreak.html
.

The "osdep" is a big clue. Have you asked in a Unix group?

I have a variety of questions about this library written in C.

Is that supposed to suggest that curses programming is topical here?
That would open up a very wide notion of topicality.
I
don't get what tty_fix is supposed to be doing, partially because I
don't know what endwin is doing.

How and where do I grep for endwin()?

Why would you grep for it? Did you read the manual?

I know "read the manual" is one of those things that people like me say
but, really, I can't think of a better way to find out what endwin is
doing. Reading the manual may raise even more questions, but those will
be better questions and they can be asked in a more suitable group. If
you can't find the manual, then that too is a good and specific question
for another group.
 
U

Uno

Ben said:
The "osdep" is a big clue. Have you asked in a Unix group?



Is that supposed to suggest that curses programming is topical here?
That would open up a very wide notion of topicality.


Why would you grep for it? Did you read the manual?

I know "read the manual" is one of those things that people like me say
but, really, I can't think of a better way to find out what endwin is
doing. Reading the manual may raise even more questions, but those will
be better questions and they can be asked in a more suitable group. If
you can't find the manual, then that too is a good and specific question
for another group.

You snipped out all the C and wondered where the C was. Why is main
junk in this standard C program:

int tty_break()
{
initscr();
cbreak();
return 0;
}

int tty_getchar()
{
return getch();
}

int tty_fix()
{
endwin();
return 0;
}


int main(void)
{
int i;
if(tty_break() != 0)
return 1;
for(i = 0; i < 10; i++)
printf(" = %d\n", tty_getchar());
tty_fix();
return 0;
}
 
J

Jens Thoms Toerring

Uno said:
I've been working through the material in faq 19.1:
http://www.c-faq.com/osdep/cbreak.html
. Only one main program is shown there, and this one is definitely not
behaving well:
[snipped]

I have a variety of questions about this library written in C. I don't
get what tty_fix is supposed to be doing, partially because I don't know
what endwin is doing.

Just because it's written in C doesn't mean that comp.lang.c is a
good place to ask about it. You will have a lot more luck in e.g.
comp.unix.programmer since this library is rather UNIX specific.
How and where do I grep for endwin()?

You don't grep for it but you should read the documentation
for the curses (or nowadays ncurses) library. That's were
everything should be explained. Since you obviously are
running some kind of UNIX and seem to have the library al-
ready installed you probably best start with

man curses

or, if you have a more modern version of UNIX and thus the
ncurses instead of the old curses library,

man ncurses

(if that doesn't show anything you need to also install the
documentation part). In that man pages you will find that
there are a lot of man pages for the functions of the library.
E.g.

man endwin

should give you the documentation for the ncurses endwin()
function.

And then there's also a how-to to be found at e.g.

http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

which might be easier to start with than from the "official"
documentation.
Regards, Jens
 
I

Ian Collins

You snipped out all the C and wondered where the C was. Why is main junk
in this standard C program:

Because you don't include allthe required non-standard headers!
int tty_break()
{
initscr();
cbreak();
return 0;
}

int tty_getchar()
{
return getch();
}

int tty_fix()
{
endwin();
return 0;
}


int main(void)
{
int i;
if(tty_break() != 0)
return 1;
for(i = 0; i < 10; i++)
printf(" = %d\n", tty_getchar());
tty_fix();
return 0;
}

man curses and friends.
 
U

Uno

Jens said:
Just because it's written in C doesn't mean that comp.lang.c is a
good place to ask about it. You will have a lot more luck in e.g.
comp.unix.programmer since this library is rather UNIX specific.

There is a part of this problem that directly bears on c.l.c., namely
that one of the faqs needs to be fixed up to be useful. Main here

int main(void)
{
int i;
if(tty_break() != 0)
return 1;
for(i = 0; i < 10; i++)
printf(" = %d\n", tty_getchar());
tty_fix();
return 0;
}

is freaking retarded, and judging by my machine's reaction, invoking
undefined behavior.
You don't grep for it but you should read the documentation
for the curses (or nowadays ncurses) library. That's were
everything should be explained. Since you obviously are
running some kind of UNIX and seem to have the library al-
ready installed you probably best start with

man curses

or, if you have a more modern version of UNIX and thus the
ncurses instead of the old curses library,

man ncurses

(if that doesn't show anything you need to also install the
documentation part). In that man pages you will find that
there are a lot of man pages for the functions of the library.
E.g.

man endwin

should give you the documentation for the ncurses endwin()
function.

And then there's also a how-to to be found at e.g.

http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

which might be easier to start with than from the "official"
documentation.

Alright, thx, jens, I forgot I could man, and thx again for the link.
That will give plenty me of reading. I'll follow up on the unix aspects
of this in c.u.p.

How exciting is soccer right now? I think the Argentines and the
Germans are the teams to beat. And Ghana beat you? Wahnsinn.
 
U

Uno

Ian said:
Because you don't include allthe required non-standard headers!

You don't have to if you can include a definition, tailored to a purpose.

#include <stdio.h>
int endwin(void)
{
printf("execution was here\n");
}
man curses and friends.

$ man curses
No manual entry for curses
$ man friends
No manual entry for friends
$

Did you mean friends like the ones who invited me over for soccer, a
coors, barbq chicken and good times?
 
I

Ian Collins

There is a part of this problem that directly bears on c.l.c., namely
that one of the faqs needs to be fixed up to be useful. Main here

int main(void)
{
int i;
if(tty_break() != 0)
return 1;
for(i = 0; i < 10; i++)
printf(" = %d\n", tty_getchar());
tty_fix();
return 0;
}

is freaking retarded, and judging by my machine's reaction, invoking
undefined behavior.

The examples in the FAQ (with the exception of the non-standard main)
work as advertised (at least on a system derived from System V Unix).

Define "retarded".

I see you have a thread on c.u.p as well, do follow up over there.
 
T

Tim Streater

Because you don't include allthe required non-standard headers!

You don't have to if you can include a definition, tailored to a purpose.

#include <stdio.h>
int endwin(void)
{
printf("execution was here\n");
}
man curses and friends.

$ man curses
No manual entry for curses
$ man friends
No manual entry for friends
$[/QUOTE]

Well this is just typical unix. Even if there is a manual entry, it'll
be for a version other than the one installed on your machine.
 
I

Ian Collins

You don't have to if you can include a definition, tailored to a purpose.

#include <stdio.h>
int endwin(void)
{
printf("execution was here\n");
}

Which you didn't.
$ man curses
No manual entry for curses

Install the required man pages. If your system is missing the man
pages, it may well be missing the correct libraries as well.
$ man friends
No manual entry for friends
$

Smart arse.
 
I

Ian Collins

You don't have to if you can include a definition, tailored to a purpose.

#include <stdio.h>
int endwin(void)
{
printf("execution was here\n");
}

$ man curses
No manual entry for curses
$ man friends
No manual entry for friends
$

Well this is just typical unix. Even if there is a manual entry, it'll
be for a version other than the one installed on your machine.[/QUOTE]

Nonsense. Any half decent system packages the man pages with the
corresponding headers and libraries. If you have a counter example,
bring it up on c.u.p.
 
U

Uno

Ian said:
The examples in the FAQ (with the exception of the non-standard main)
work as advertised (at least on a system derived from System V Unix).

The advertising says much more than this and minimally needs a re-write.
Define "retarded".

Invoking undefined behavior.
I see you have a thread on c.u.p as well, do follow up over there.

Part of this problem is best cast as standard C. The rest of it, I'll
discuss there.

Does anyone have any better notion of what to do with a main control in
such a program than using a retarded 'for' loop?
 
I

Ian Collins

The advertising says much more than this and minimally needs a re-write.

Invoking undefined behavior.

Given sensible implementations of the called functions, there isn't any
undefined behaviour.
Part of this problem is best cast as standard C. The rest of it, I'll
discuss there.

Does anyone have any better notion of what to do with a main control in
such a program than using a retarded 'for' loop?

Please explain the the undefined behaviour in "for(i = 0; i < 10; i++)".
 
J

Jens Thoms Toerring

There is a part of this problem that directly bears on c.l.c., namely
that one of the faqs needs to be fixed up to be useful. Main here
int main(void)
{
int i;
if(tty_break() != 0)
return 1;
for(i = 0; i < 10; i++)
printf(" = %d\n", tty_getchar());
tty_fix();
return 0;
}
is freaking retarded, and judging by my machine's reaction, invoking
undefined behavior.

The only thing I can see that's questionable from a C point of
view is returning 1 instead of EXIT_FAILURE (but which is per-
fectly legal under UNIX). So I don't know what you find so "re-
tarded" about it and where else it invoke undefined behaviour.
The FAQ points out that the example code you're trying to use
is for "classic UNIX" and curses which probably doesn't cover
Linux and ncurses you seem to be using. But then nobody is
keeping you from contacting Steve Summit ([email protected]) if
you have suggestions on how to improve that section;-) Of
course, one could also argue that the topic shouldn't be
covered at all in the FAQ except as far as the first few
paragraphs go, i.e. pointing out that it can't be done in
a system-independent fashion and that one should ask in a
group about the specific system one is using...
How exciting is soccer right now? I think the Argentines and the
Germans are the teams to beat. And Ghana beat you? Wahnsinn.

There aren't too many things I find more boring than soccer so
I just hope it's over soon and people around here stop talking
about nothing else...;-)
Regards, Jens
 
K

Keith Thompson

Uno said:
You don't have to if you can include a definition, tailored to a purpose.

Yes, you can obtain declarations for library functions that you call
either by writing them yourself or by including the appropriate header.

Is there any reason *not* to include the appropriate header?
 
K

Kenny McCormack

Yes, you can obtain declarations for library functions that you call
either by writing them yourself or by including the appropriate header.

Is there any reason *not* to include the appropriate header?

Yes. Just to piss you off, Kiki.

--
No, I haven't, that's why I'm asking questions. If you won't help me,
why don't you just go find your lost manhood elsewhere.

CLC in a nutshell.
 
U

Uno

Keith said:
Yes, you can obtain declarations for library functions that you call
either by writing them yourself or by including the appropriate header.

Is there any reason *not* to include the appropriate header?

*Yes.* There's gonna be a whole lot of stuff I don't understand in
there. So, I'd like to re-write these functions in a *much* simpler
form. No, these functions I'm fixing to write won't have all the
functionality of what you would get by the header inclusion, but I do so
only to create scaffolding, and, yes, stay topical.


int tty_getchar()
{
return getch();
}

This part is standard C, and it looks right syntactically.

Does main on faq 19.1 invoke undefined behavior?
 
I

Ian Collins

*Yes.* There's gonna be a whole lot of stuff I don't understand in
there. So, I'd like to re-write these functions in a *much* simpler
form. No, these functions I'm fixing to write won't have all the
functionality of what you would get by the header inclusion, but I do so
only to create scaffolding, and, yes, stay topical.


int tty_getchar()
{
return getch();
}

This part is standard C, and it looks right syntactically.

No, it isn't. getch() isn't a standard C function.
Does main on faq 19.1 invoke undefined behavior?

No (if you ignore the implicit int).
 
U

Uno

Jens said:
The only thing I can see that's questionable from a C point of
view is returning 1 instead of EXIT_FAILURE (but which is per-
fectly legal under UNIX). So I don't know what you find so "re-
tarded" about it and where else it invoke undefined behaviour.
The FAQ points out that the example code you're trying to use
is for "classic UNIX" and curses which probably doesn't cover
Linux and ncurses you seem to be using. But then nobody is
keeping you from contacting Steve Summit ([email protected]) if
you have suggestions on how to improve that section;-) Of
course, one could also argue that the topic shouldn't be
covered at all in the FAQ except as far as the first few
paragraphs go, i.e. pointing out that it can't be done in
a system-independent fashion and that one should ask in a
group about the specific system one is using...

Quote from page:

As an example, here is a tiny test program which prints the decimal
values of the next ten characters as they are typed, without waiting for
RETURN. It is written in terms of three functions, as described, and is
followed by implementations of the three functions for curses, classic
Unix, System V Unix, and MS-DOS. (The on-line archives associated with
this list contain a more complete set of functions.)

// end quote

This program does not perform this way on my machine.
There aren't too many things I find more boring than soccer so
I just hope it's over soon and people around here stop talking
about nothing else...;-)

Yeah, there are more-important things, but it was a bucolic sports-lover
day today.

I've decided to call the afghanistan conflict "the halliburton war"
henceforth.
 
I

Ian Collins

Quote from page:

As an example, here is a tiny test program which prints the decimal
values of the next ten characters as they are typed, without waiting for
RETURN. It is written in terms of three functions, as described, and is
followed by implementations of the three functions for curses, classic
Unix, System V Unix, and MS-DOS. (The on-line archives associated with
this list contain a more complete set of functions.)

// end quote

This program does not perform this way on my machine.

So? It's from the platform specific section of the document. It does
what it says on the tin on mine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top