DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 9th March 2020
attilio attilio is offline
Port Guard
 
Join Date: Jun 2015
Posts: 43
Default graphics with c piping to postscript

Hello.
I'm interested to have graphic output using c code, but without a graphic library for c (es cairo).
The idea is to use only printf to obtain, in stdout, commands in postscript language.
Examples of postscript commands are
Code:
0 0 moveto
100 100 rlineto
stroke
Then I would like to pipe these commands to ghostscript.
I'm not able to set the right pipe sintax in the shell command.

This is the code of the c program.

Code:
#include<stdio.h>
int main(){
        char c;
        int i;
        for (i=0; i<10; i++){
                printf("%d %d %s\n",0,5*i,"moveto");
                printf("%d %d %s\n",100,0,"rlineto");
                printf("%s\n","stroke");
                scanf("%c",&c);
        }
}
The output, in postscript code, traces 10 parallel lines.
The scanf line is useful to obtain pauses between one line and the next.

Supposing the code is compiled in a.out,
the following two commands work:
Code:
$ ./a.out >output-file
$ gs output-file
and you can see ten lines. Then gs waits for other commands in its prompt. If you type "quit" gs exits.

But I'm not able to pipe and to see a line after each "enter".
The "enter"s are needed (due to scanf) and wanted to obtain a graphic that proceeds step by step.
I tried
./a.out | gs -
but didn't work.
I tried also with a fifo, but it didn't work.

I'm not sure to have been able to explain the problem.
Is there anybody iterested that can suggest me the correct piping command in order to see a line drawed after each "enter"?
Reply With Quote
  #2   (View Single Post)  
Old 9th March 2020
bsdun bsdun is offline
Real Name: Steve
Fdisk Soldier
 
Join Date: Feb 2020
Posts: 48
Default

When you pipe a.out into gs, a.out sends all it's output to gs and stops running. I don't think you can pipe one line at a time.

When I use C, I use it in a combination with Lua language, because Lua is written as a C library and work perfectly together with C. Lua is designed to work with large amount of text and data and can be embedded in C and C++ programs. With Lua you would have os.execute() to call gs after each line. Free Lua book from Lua's author:
https://www.lua.org/pil/contents.html
Reply With Quote
  #3   (View Single Post)  
Old 9th March 2020
attilio attilio is offline
Port Guard
 
Join Date: Jun 2015
Posts: 43
Default

Thank you bsdun.
You are right, a.out writes all his output and, then, all the output is piped.
I tested it with something like this
Code:
$ ./a.out |cat > /dev/pts/2
where the output of cat in sent to a terminal window different from that from which I run a.out.
Also with a fifo the behaviour is the same:

On terminal window A
Code:
$ mkfifo  xxx
$ ./a.out > xxx
and on a different terminal window B
Code:
$ cat xxx
produces output in B all together at the end, when a.out stops.

Does this depend on a bufferization of the output?
If yes, is it possible to eliminate bufferization?

Thank you also for the interesting reference to LUA. I don't know it at all.
I will read something about.
Reply With Quote
  #4   (View Single Post)  
Old 9th March 2020
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 1,027
Default

Another thought is that gs might wait until its standard input closes before processing anything received from it. If that's so, and I don't know if it is or not, then un-buffering probably wouldn't help.
Reply With Quote
  #5   (View Single Post)  
Old 9th March 2020
attilio attilio is offline
Port Guard
 
Join Date: Jun 2015
Posts: 43
Default

Maybe idOp.

Anyway, when one uses gs in interactive mode
Code:
$ gs
gs offers a prompt and the graphic output is in real time.
So gs would seem to have different behaviours according to wher the input comes from.

Effectively, I have tried
Code:
gs -
and, until I type ctrl-D to comunicate the end of file, nothing appears on the window dedicated to the graphics.

Does this mean that gs waits for the command list to finish before executing them,
or can stdin be bufferized too? Boh (italian expression to say "I don't know").

Thank you idOp
Reply With Quote
  #6   (View Single Post)  
Old 9th March 2020
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 1,027
Default

Quote:
Originally Posted by attilio View Post
Effectively, I have tried
Code:
gs -
and, until I type ctrl-D to comunicate the end of file, nothing appears on the window dedicated to the graphics.
That's an interesting idea. I tried it, and a blank graphics window comes up. Also, gs offers no prompt on the terminal since it's not in interactive mode. I assume it is reading from the terminal like it would from a file. I can type in some commands, such as from your program, nothing shows in the graphics widow. When I hit Ctrl-D the graphics window disappears, so it's not possible to see if it "drew the commands". But it does suggest it may be waiting for the whole file (in this case std input) before doing anything.

Quote:
Does this mean that gs waits for the command list to finish before executing them,
or can stdin be bufferized too?
To me it suggests the first part. As to the second part, I am not an expert in that, I'd guess that if it were buffered it might still get a line at a time when one's ready, but am really not sure.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PDF/Postscript -> print filter -> Print Queue Filter shep Programming 6 24th December 2018 03:33 PM
building graphics/imlib2? spiderpig OpenBSD Packages and Ports 1 26th January 2015 04:09 AM
2D Graphics acceleration in X virtuvoos OpenBSD General 10 18th October 2013 02:40 PM
Intel graphics tolstoi NetBSD General 5 20th February 2013 08:10 AM
nwn failed to initialize graphics maxrussell FreeBSD General 8 22nd November 2008 08:52 PM


All times are GMT. The time now is 07:13 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick