Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/builtin/asa.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1992-2012 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* David Korn <[email protected]> *
19
* *
20
***********************************************************************/
21
#pragma prototyped
22
/*
23
* asa.c
24
* Written by David Korn
25
* AT&T Labs
26
* Tue Mar 25 11:03:23 EST 2003
27
*/
28
29
static const char usage[] =
30
"[-?\n@(#)$Id: asa (AT&T Research) 2003-03-25 $\n]"
31
"[-author?David Korn <[email protected]>]"
32
"[-license?http://www.research.att.com/sw/tools/reuse]"
33
"[+NAME?asa - interpret carriage-control characters]"
34
"[+DESCRIPTION?\basa\b writes its input files to standard output mapping "
35
"carriage control characters to line-printer control sequences.]"
36
"[+?The first character of each line or record is removed from the input and "
37
"is processed as follows before the rest of the line is output:]{"
38
"[+0?A new-line is output.]"
39
"[+1?Advance to the next page.]"
40
"[++?Overwrite the previous line.]"
41
"[+\aspace\a?Just output the remainder of the line.]"
42
"}"
43
"[+?Any other character as the first character is treated as if "
44
"it were a space.]"
45
"[+?If no \afile\a operands are given or if the \afile\a is \b-\b, \basa\b "
46
"reads from standard input. The start of the file is defined as the "
47
"current offset.]"
48
"[r]#[reclen?If \areclen\a is greater than zero, the file is assumed to "
49
"consist of fixed length records of length \areclen\a.]"
50
"\n"
51
"\n[file ... ]\n"
52
"\n"
53
"[+EXIT STATUS?]{"
54
"[+0?Success.]"
55
"[+>0?An error occurred.]"
56
"}"
57
"[+SEE ALSO?\blpr\b(1)]"
58
;
59
60
#include <cmd.h>
61
62
static int asa(register Sfio_t *in, Sfio_t *out, int reclen)
63
{
64
register char *cp;
65
register int n, c = 0;
66
while(1)
67
{
68
if(reclen>0)
69
cp = sfreserve(in,n=reclen, -1);
70
else
71
cp = sfgetr(in,'\n',0);
72
if(!cp)
73
break;
74
if(reclen<=0)
75
n = sfvalue(in)-2;
76
else
77
while(--n>0 && cp[n]==' ');
78
switch(*cp)
79
{
80
case '\n':
81
break;
82
case '0':
83
sfputc(out,'\n');
84
break;
85
case '1':
86
if(c)
87
{
88
sfputc(out,c);
89
c = '\f';
90
}
91
break;
92
case '+':
93
c = '\r';
94
break;
95
}
96
if(c)
97
sfputc(out,c);
98
if(n>0)
99
sfwrite(out,cp+1,n);
100
c = '\n';
101
}
102
if(c)
103
sfputc(out,c);
104
return(0);
105
}
106
107
int
108
b_asa(int argc, char** argv, Shbltin_t* context)
109
{
110
register char *cp;
111
register Sfio_t *fp;
112
register int n, reclen=0;
113
114
cmdinit(argc, argv, (void*)0, (const char*)0, 0);
115
while (n = optget(argv, usage)) switch (n)
116
{
117
case 'r':
118
reclen = opt_info.num;
119
break;
120
case ':':
121
error(2, "%s", opt_info.arg);
122
break;
123
case '?':
124
error(ERROR_usage(2), "%s", opt_info.arg);
125
break;
126
}
127
argv += opt_info.index;
128
if(error_info.errors)
129
error(ERROR_usage(2),"%s",optusage((char*)0));
130
if (cp = *argv)
131
argv++;
132
do
133
{
134
if (!cp || streq(cp,"-"))
135
fp = sfstdin;
136
else if (!(fp = sfopen(NiL, cp, "r")))
137
{
138
error(ERROR_system(0), "%s: cannot open", cp);
139
error_info.errors = 1;
140
continue;
141
}
142
n = asa(fp, sfstdout,reclen);
143
if (fp != sfstdin)
144
sfclose(fp);
145
} while (cp = *argv++);
146
return(error_info.errors);
147
}
148
149