writing to pipe

C

C. Gordon Liddy

Morris Dovey said:
c gordon liddy wrote:

[re-ordered for thematic reasons]
You're welcome - it was satisfying to see you come up to speed
so quickly.
It's March Madness. I'm faster than the wind and above the rim.

I came up with a result for the -v switch. The testfile is given here:

#include <stdio.h>
int main(void)
{
int c;
printf ("the quick brown fox\n");
for (c = 0; c <= 25; c ++) {
putchar(c);
}
printf ("jumps over\n");
for (c = 100; c <= 200; c ++) {
putchar(c);
}
printf ("the lazy dog\n");
return 0;
}
// gcc -o mkchars mkchars3.c
// mkchars >text52.txt

// catv8.c
#include <stdio.h>

int main(int argc, char **argv)
{

FILE *fp;
void filecopy(FILE *, FILE *);

if (argc < 2) printf("die");
else
while (--argc > 0)
if ((fp = fopen(*++argv, "rb")) == NULL)
{
printf("catv can't open %s\n", *argv);
return 1;
}
else
{
filecopy(fp, stdout);
fclose(fp);
}

return 0;
}

/*filecopy */
void filecopy(FILE *ifp, FILE *ofp)
{
int c;
int ch;
int result;
int ch1 = '^';
int ch2;
int ch3 = 'M';

while((ch=getc(ifp)) != EOF)
{
if (iscntrl(ch))
{

result = putchar(ch1);
if (result == EOF)
{
/* Failed, terminate the loop */
break;
}

if (ch == '\177')
{
/* ch is DEL, we want "^?" */
ch2 = '?';
}
else if(ch == 26)
{
/* we don't want ctrl-z coming out of here */
ch2 = '#';
}

else
{
/*
* ch is another control character.
* Transform 1 to 'A', 2 to 'B', etc. using
* our intimate knowledge of ASCII encoding.
*/
ch2 = ch | 0100;
}

/* Print as above */
result = putchar(ch2);
if (result == EOF)
{
break;
}


// outer brace of if (iscntrl(ch))
}


else if (!isascii(ch))
{
if (putchar('M') == EOF || putchar('-') == EOF)
break;
ch = toascii(ch);
putchar(ch);
}



else putchar(ch);


// outer brace of while control
}
// outer brace of function
}
// gcc -o catv catv8.c >text22.txt 2>text23.txt
// catv text42.txt >text43.txt

Can you see how this compares to running
cat -v text52.txt
?

Sounds good. I wrote a version in which I hijacked the -t option
for defining tab stops (I used -t3 to replace tabs with spaces
for the code you saw) and I suspect that you'll find more handy
features to add. :)
Can you elaborate? For what I looked at -t option, it didn't seem so. But
then ....
 
M

Morris Dovey

C. Gordon Liddy said:
I tried to write a program that would detab. This is close but doesn't
behave:

<snip>

As you process non-tab chars, keep track of the column - and when
you find a tab, calculate the next tab stop and output only
enough spaces to get there. Repeat as needed.
I'm out of here. Maybe next time you can tell me about solar.

I've probably got enough of that on my web site to gag a hippo.
The most interesting stuff going on in my shop isn't for sale
(yet) - I'm working on solar-powered Stirling-cycle engines whose
only moving parts are air and water. If you're interested in
weird technology, follow the link below.
 
C

C. Gordon Liddy

Morris Dovey said:
The standard -t option is supposed to be equivalent to the option
pair -vT (or -v -T), where -v causes control characters to be
displayed as in the version you're working on, plus -T which
causes tabs to be displayed as ^I.

I wanted cat to replace tabs with spaces so that, among other
things, I could format C source for posting here. I started out
by writing a utility program that processed a single file. Then I
modified the utility to process a list of files. Later I added a
feature to read from stdin if the utility was invoked without any
files specified on the command line. The final step was to
recognize "-" as a command line argument to trigger reading from
stdin when the command line included file names. The only option
designation that seemed appropriate to me was -t followed by a
numerical column width.

I've thought about adding the -n (number lines) and -s (squeeze
multiple blank lines to a single blank line), but haven't ever
needed 'em. I've also thought about adding -d and -u options to
specify DOS <CR><LF> or unix <LF> line endings, but already have
a separate filter for that and have been too lazy to incorporate
it into this program. Maybe someday when/if I have more time...
I tried to write a program that would detab. This is close but doesn't
behave:
#include <stdio.h>

int main(int argc, char **argv)
{

FILE *fp;
void filecopy(FILE *, FILE *);

if (argc < 2) printf("die\n");
else
while (--argc > 0)
if ((fp = fopen(*++argv, "rb")) == NULL)
{
printf("cat can't open %s\n", *argv);
return 1;
}
else
{
filecopy(fp, stdout);
fclose(fp);
}

return 0;
}

/*filecopy */
void filecopy(FILE *ifp, FILE *ofp)
{

int ch;


while((ch=getc(ifp)) != EOF)
{



if (ch = '\t')
{
putchar(' ');
putchar(' ');
putchar(' ');
putchar(' ');

}

else putchar(ch);


}

}
// gcc -o cat_t cat_t2.c >text22.txt 2>text23.txt
// cat_t text52.txt >text43.txt


I'm out of here. Maybe next time you can tell me about solar.
 
C

C. Gordon Liddy

#include <stdio.h>

int main(int argc, char **argv)
{

FILE *fp;
void filecopy(FILE *, FILE *);

if (argc < 2) printf("die\n");
else
while (--argc > 0)
if ((fp = fopen(*++argv, "rb")) == NULL)
{
printf("cat can't open %s\n", *argv);
return 1;
}
else
{
filecopy(fp, stdout);
fclose(fp);
}

return 0;
}

/*filecopy */
void filecopy(FILE *ifp, FILE *ofp)
{

int ch;

while((ch=getc(ifp)) != EOF)
{

if (ch == '\t')
{
putchar(' ');
putchar(' ');
putchar(' ');
putchar(' ');

}
else putchar(ch);
}
}
// gcc -o cat_t cat_t2.c >text22.txt 2>text23.txt
// cat_t text52.txt >text43.txt



== as opposed to =
fuer Dich als Abschiedsgeschenk.
 
D

David Thompson

#if offtopic == Unixoid
I wanted cat to replace tabs with spaces so that, among other
things, I could format C source for posting here. I started out
by writing a utility program that processed a single file. Then I
modified the utility to process a list of files [and stdin] <snip>

Did/do you know that the traditional and standard Unix utility
'expand' does tab expansion? (And 'unexpand' the reverse?)
In addition 'pr' whose main job is to format pages, columns, and
headings for printing, also does tab expansion and unexpansion,
and I think with some effort you can get it to do that alone?

I'm not saying you always have to follow that (offtopic) standard, but
here I see no benefit from diverging.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

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,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top