Benches macOS

15

Help with writing a c program

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hello,

I want to write a c program, but could use some help. I want to write a program that will add a NUL character to each line? I want to have a NUL character put on the end of text either written or read to the file? In terminal the keystroke to write a NUL character is cntrl shift @. With the help of others I have written a shell script that when executed uses the echo command, there was a variable in it and then 0 was added to the end of the variable. And the script worked, when I would run the script my cursor would drop down a line, I could then enter text and when I hit return a NUL character would be automatically added to the text I had written. I confirmed this because my serial device a speech synthesizer spoke the text I had written. I could after I hit return type another line of text or press cntrl d to exit the shell script. I have been trying to figure out a way to have a complete unix session spoken by my serial port device. I got ckermit a terminal emulator, and using certain ckermit commands was able to transmit a text file (which based on how I saved the file must have ended with a NUL because my synthesizer spoke some files I transmitted, and other files it didn't speak). I also used the ckermit lineout command to send text directly to my device, and that worked. I then tried ckermits log session command to send a whole unix session to my synthesizer. And so far I haven't been able to get audio output? So I want to write a c program, that will add a NUL character to the end of a line sent to my synthesizer. I don't know if I need a NUL at the end of text on each line, or just at the very end of text? i.e. if I do ls, would I need to put a NUL at the end of each line, or just at the very end of the ls listing? btw ls is just an example if possible I want my program when I am running it to work with a complete unix session i.e. pwd would produce my synthesizer speaking my current working directory. I really don't know where to begin, I think I would use printf but I don't know where to start? Any help would be appreciated.

15 replies

Sign in or join Beachball Help to reply.

  1. 0

    jpimbert

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    It's better to use fwrite instead of printf. Because printf handles C character Strings, and 0 is the marker for the end of a C char-string; printf will not proceed the additional null character.

  2. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I don't have time to write the C code now, but Perl is easy:


    #!/usr/bin/perl -w
    use strict;
    use Getopt::Long;
    my $output;
    GetOptions('o|output=s' => $output);
    open OUT, ">$output"
    if $output;
    while(my $line = )
    {
    my @results = `$line`;
    foreach my $result (@results)
    {
    print $result;
    if($output)
    {
    print OUT $result;

    print OUT '

  3. 0

    BobHarris

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    First, you should take a breath when writing your request. Huge paragraphs with no breaks are difficult to find useful information in.

    If C-Kermit is more or less working for you, why not get the sources and modify the session logging code to append your NUL?

    <ftp://kermit.columbia.edu/kermit/archives/&gt;

    OK, that is not necessary trivial, but it is one way to do what you want.

    Another approach might be to filter the C-Kermit session log on its way to your serial port. I DO NOT know if this will work, but it is an idea.


    mkfifo /path/to/pipeto_nuladder
    /path/to/nuladderscript </path/to/pipeto_nuladder >/dev/serial_device & # drop into background
    kermit
    ... tell kermit to log the session to /path/to/pipeto_nuladder ...

    Again, I do not know if this would work, but it might be easy enough to try.

    The
    mkfifo is creating a pipe that you will have C-Kermit direct its session log towards, and on the other side of the pipe you will have a script that appends a NUL to each line and write that line to the serial port.

    The
    nuladderscript would look something like:


    #!/usr/bin/env awk
    {printf("%s %c",$0,0)}

    which you would make executable with "chmod +x nul
    adderscript".

  4. 0

    jamesapp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    etresoft a couple of questions. I have the developer tools installed do I need to download anything to run your

    perl script? How do I save the perl script, let's say I call it talk and put it in a folder called doubletalk in my

    home folder, how do I save talk as an executable script? When I run it do I need cat running aswell I thought to

    Invoke it I would at a shell prompt, change to the directory where I saved the script and then I would type

    $ ./talk -o /dev/cu.usbserial-FTKVMAFF

    and then what would happen, I could conduct a unix session and everything would be sent to /dev/cu.usbserial-

    FTKVMAFF that is the designation of my serial device? How do I exit the program and stop appending NULs?

  5. 0

    jamesapp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    BobHarris

    a guy who had been helping me with ckermit gave me the following command:


    log session "|cat /dev/cu.usbserial-FTKVMAFF

    couldn't I try to replace cat with nul_adder? And how do I invoke nul_adder the scripts I have

    run I have typed like,

    $ sh scriptname

  6. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    jamesapp wrote:

    etresoft a couple of questions. I have the developer tools installed do I need to download anything to run your perl script?

    Nope.

    How do I save the perl script, let's say I call it talk and put it in a folder called doubletalk in my

    home folder

    Use your favorite text editor such as Text Wranger, Xcode, or vi.

    how do I save talk as an executable script?

    If you save it as a text file named "talk", then do "chmod u+x talk".

    When I run it do I need cat running as well

    No. You don't need to use cat. However, if you had text in some external file that you wanted to send to your speech synthesizer, you could use cat to pipe the contents of a file to the script as standard input. "cat /path/to/file/to/speak.txt | talk -o /dev/serial/port"

    I thought to Invoke it I would at a shell prompt, change to the directory where I saved the script and then I would type

    $ ./talk -o /dev/cu.usbserial-FTKVMAFF

    and then what would happen, I could conduct a unix session and everything would be sent to /dev/cu.usbserial-

    FTKVMAFF that is the designation of my serial device?

    Yes. That is correct. Or, you could copy "talk" to any directory in your path, such as "/usr/local/bin" and then you can run it from anywhere.

    How do I exit the program and stop appending NULs?

    The script reads from standard input. If you are piping from a file it will automatically close standard input and terminate the script when it has read all the file. If you are running it from standard input, you will have to type "Control - D" to close the input stream. Since the script is Perl and a bit more intelligent that "cat", you could also add a line to quit it with a command. For example, say you wanted to quit the program just by typing "stop". Do something like:


    while(my $line = )
    {
    chomp $line;
    exit
    if $line eq 'stop';
    ...
    }

  7. 0

    BobHarris

    Imported from the Apple Support Forums via the Wayback Machine 🔗


    log session "|cat /dev/cu.usbserial-FTKVMAFF

    couldn't I try to replace cat with nul_adder? And how do I invoke nul_adder the scripts I have

    Yes you could.
    cat is just an executable file. A script is an executable file. One runs compiled code, the other contains commands for a shell to execute.

    Put the following bit of code in a text file (for example
    nul_adder ).


    #!/usr/bin/env bash
    awk '{printf("%s %c",$0,0)}' >$1

    Make the file executable


    chmod +x /path/to/nul_adder

    Now specify your C-Kernit command as:


    log session "|/path/to/nul_adder /dev/cu.usbserial-FTKVMAFF

    That should run your
    nul_adder script, which will append a NUL to the end of every line and redirect the output to
    /dev/cu.usbserial-FTKVMAFF

    I hope this helps.

  8. 0

    jamesapp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    The script reads from standard input. If you are piping from a file it will automatically close standard input and terminate the script when it has read all the file. If you are running it from standard input, you will have to type "Control - D" to close the input stream. Since the script is Perl and a bit more intelligent that "cat", you could also add a line to quit it with a command. For example, say you wanted to quit the program just by typing "stop". Do something like:

    while(my $line = )

    {

    chomp $line;

    exit

    if $line eq 'stop';

    ...

    }

    I put the above code in the perl program after the 3 }

    and before the close OUT line

    just want to make sure this is right, the ... do I need to ad code?

  9. 0

    jamesapp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    BobHarris

    How do I exit null_adder? Would cntrl-d work?

  10. 0

    jamesapp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    etresoft

    got 2 syntax errors

    one at line 15, near "= ) "

    also a syntax error at line 28, near "}"

  11. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Sorry, even with the

    formatting the forums screwed it up.
    The line to read from standard input should be:

    while(my $line = <>)

    {code}

    Hopefully that will fix line 28 as well.

  12. 0

    jamesapp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    etresoft

    I ran the script, and I did an ls command, did not get immediate audio output

    I pressed cntrl-d to exit, I then at the next shell prompt

    entered a line of text with cat and ended the line with a NUL

    character. And my synthesizer said something like "back slash greater then etc."

    and then my synthesizer said the line that I had written with cat?

    I think that the text from the talk program was sent to my synthesizers buffer?

    I don't know if my output from the ls command had a NUL character appended to it?

    The line that says:


    my @results = '$line'

    should $line have single quotes around it? My iPhone where I am writing this

    doesen't have the character I actually used which on my MacBook pro is the key

    above tab it is on the same key as ~, should that line have single quotes?

    Don't know what I am doing wrong?

  13. 0

    BobHarris

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    How do I exit null_adder? Would cntrl-d work?

    When you stop your C-Kermit Session Logging, that will close the pipe between C-Kermit and
    nul_adder which will provide End-of-File to the
    awk script, which is its natural *I'm Done* indicator.

  14. 0

    jamesapp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    awk '{printf("%sn%c", $0, 00)}' >/dev/cu.usbserial-FTKVMAFF

    got this from another thread I had started, just wondering if this could work?

  15. 0

    jamesapp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    etresoft

    the guy from ckermit had told me that when I run ckermits log session

    command when I start my unix session to enter the following command

    export TERM=tty

    this command prevents escape sequences from being transmitted

    we thought that my synthesizer uses certain characters for different things

    i.e. Cntrl-A is used to program the synthesizer, change the voice pitch etc.

    the synthesizer doesen't speak what is directly after the cntrl-A character

    so I thought maybe certain characters are causing my synthesizer not to speak?

    BobHarris

    the actual ckermit command I gave you was:


    log session "| cat > /dev/cu.usbserial-FTKVMAFF

    I had left out the redirect character earlier

    the file from the other post is almost like yours except

    they gave to zeros and then they redirected to the serial port.

    Just wondered what you thought.