Counting chars after fprintf on a terminal

  • Thread starter Antonio Maschio
  • Start date
A

Antonio Maschio

Hi,

I've got the following problem:

I wrote a program A that uses fprintf to stdout to output some strings
or numbers as result of some commands.

Now I want to improve this program A, and I want to write a debugger B
that prints information on a new line, for each command of A, and this
output is interspersed with the program A output, of course.

I'd want to know, for any debugger line, if the program A (not the
debugger) is in the middle of the terminal, or has just printed a "\n".

On Mac OS X, using ftell(stdout) in two different moments (at the end of
line n and at beginning of line n+1), subtracting the long values I get,
I obtain a zero if program A didn't print anything, or a positive value
if it printed something; in the first case I simply print the debugging
information, in the second case I print a "\n" before the debugging
information to go to a new line

The problem is that this work on the Mac OS X terminal (which is
emulated), but not a real 80-columns terminal under Linux (in this case,
the long value returned is always -1).

Any idea? I'd like something portable and ANSI C, to work on any
environment.

Thanks in advance to anyone answering.

-- Antonio

Tried on:
Mac OS X Tiger 4.11 (gcc 4.X)
Linux RedHat 9A (gcc 3.X)
Linux Fedora Core 4 (gcc 3.2)
(old linux system, uhu?)
 
R

Rafael

Hello Antonio, List
Now I want to improve this program A, and I want to write a debugger B
that prints information on a new line, for each command of A, and this
output is interspersed with the program A output, of course.

How exactly is program a is talking with program b (or b with a)?
Any idea? I'd like something portable and ANSI C, to work on any
environment.

Can you post some code? It's really hard to guess based only in conjectures.


Rafael
 
A

Antonio Maschio

Rafael ha scritto:
Hello Antonio, List


How exactly is program a is talking with program b (or b with a)?


Can you post some code? It's really hard to guess based only in
conjectures.

Well, here's an example:

*********** Program A **********
/* program A scans all characters from a char stream called input */
while (c=*input) {
switch(c) {
case 'A': ... /* it may fprintf something */
case 'B': ... /* it may fprintf ...*/
case 'C': ... /* ...*/
}
}
********** Program A - end *******

This is the original I wrote time ago.

Now I want to improve it, adding some debug information (basically, it
should print all the things that program A **will** do. So:

********* program A + B **********

/* program A scans all characters from a char stream called input */
long dpos=0, dposex=0;
while (c=*input) {
/* program B intercepts the stream */
if (isDebug) {
int u;
dpos=ftell(stdout);
/* if printing position is not at the start of line
* print CR */
if ((dpos-dposex)>0) putchar('\n');
fprintf(stdout, " [ ");
for (u = 1; u <= TOS; u++)
fprintf(stdout, "%d ", Stack);
fprintf(stdout, "] -- ");
fprintf(stdout, "%c", c);
/* ....
does something else, and in the end... */
dposex=ftell(stdout);
}
/* the rest is unchanged */
switch(c) {
case 'A': ... /* it may fprintf something */
case 'B': ... /* it may fprintf ...*/
case 'C': ... /* ...*/
}
}

********** program A + B end *********

The code I wrote (getting dposex and dpos and testing their difference)
works great on Mac OS X (I don't know if it's so because the terminal is
emulated); on Linux and on a real terminal (ALT+F1~F6) the code does not
work, because ftell(stdout) always returns -1; so the putchar('\n')
never happens;

I'd want a way that works on real and emulated terminals, on
UNIX/Linux/MacOSX and possibly command.com (Win32). And of course ANSI.

Any idea?

Note: I called it program B, while in effect it's a snippet of code
inside Program A, but it is so peculiar that I think of it as a
separated program.

Thanks.

-- Antonio
 
S

Spiros Bousbouras

/* program A scans all characters from a char stream called input */

The code I wrote (getting dposex and dpos and testing their difference)
works great on Mac OS X (I don't know if it's so because the terminal is
emulated); on Linux and on a real terminal (ALT+F1~F6) the code does not
work, because ftell(stdout) always returns -1; so the putchar('\n')
never happens;

I'd want a way that works on real and emulated terminals, on
UNIX/Linux/MacOSX and possibly command.com (Win32). And of course ANSI.

Any idea?

Note: I called it program B, while in effect it's a snippet of code
inside Program A, but it is so peculiar that I think of it as a
separated program.

If A and B are the same programme then surely the simplest thing to do
is to store in a variable or something whether A has just output a
newline. Otherwise the only thing I can think of is that B should
always
output a newline before writing the debugging information. You will
get
an empty line sometimes but nothing terrible.

Regarding Mac OS X I think you have encountered implementation
specific
behaviour. On Linux you can try calling perror() after ftell() returns
-1 to see what happened but you will probably get a message saying
that
the stream is not seekable.
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top