~ubuntu-branches/ubuntu/raring/less/raring-proposed

« back to all changes in this revision

Viewing changes to lessecho.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Schoepf
  • Date: 2002-04-04 16:43:52 UTC
  • Revision ID: james.westby@ubuntu.com-20020404164352-qldq048yoc7x5sd5
Tags: upstream-374
ImportĀ upstreamĀ versionĀ 374

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 1984-2000  Mark Nudelman
 
3
 *
 
4
 * You may distribute under the terms of either the GNU General Public
 
5
 * License or the Less License, as specified in the README file.
 
6
 *
 
7
 * For more information about less, or for information on how to 
 
8
 * contact the author, see the README file.
 
9
 */
 
10
 
 
11
 
 
12
/*
 
13
 * lessecho [-ox] [-cx] [-pn] [-dn] [-a] file ...
 
14
 * Simply echos its filename arguments on standard output.
 
15
 * But any argument containing spaces is enclosed in quotes.
 
16
 *
 
17
 * -ox  Specifies "x" to be the open quote character.
 
18
 * -cx  Specifies "x" to be the close quote character.
 
19
 * -pn  Specifies "n" to be the open quote character, as an integer.
 
20
 * -dn  Specifies "n" to be the close quote character, as an integer.
 
21
 * -mx  Specifies "x" to be a metachar.
 
22
 * -nn  Specifies "n" to be a metachar, as an integer.
 
23
 * -ex  Specifies "x" to be the escape char for metachars.
 
24
 * -fn  Specifies "x" to be the escape char for metachars, as an integer.
 
25
 * -a   Specifies that all arguments are to be quoted.
 
26
 *      The default is that only arguments containing spaces are quoted.
 
27
 */
 
28
 
 
29
#include "less.h"
 
30
 
 
31
static char *version = "$Revision: 1.7 $";
 
32
 
 
33
static int quote_all = 0;
 
34
static char openquote = '"';
 
35
static char closequote = '"';
 
36
static char *meta_escape = "\\";
 
37
static char metachars[64] = "";
 
38
static int num_metachars = 0;
 
39
 
 
40
        static void
 
41
pr_usage()
 
42
{
 
43
        fprintf(stderr,
 
44
                "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-mx] [-nn] [-a] file ...\n");
 
45
}
 
46
 
 
47
        static void
 
48
pr_version()
 
49
{
 
50
        char *p;
 
51
        char buf[10];
 
52
        char *pbuf = buf;
 
53
 
 
54
        for (p = version;  *p != ' ';  p++)
 
55
                if (*p == '\0')
 
56
                        return;
 
57
        for (p++;  *p != '$' && *p != ' ' && *p != '\0';  p++)
 
58
                *pbuf++ = *p;
 
59
        *pbuf = '\0';
 
60
        printf("%s\n", buf);
 
61
}
 
62
 
 
63
        static void
 
64
pr_error(s)
 
65
        char *s;
 
66
{
 
67
        fprintf(stderr, "%s\n", s);
 
68
        exit(1);
 
69
}
 
70
 
 
71
        static long
 
72
lstrtol(s, radix, pend)
 
73
        char *s;
 
74
        int radix;
 
75
        char **pend;
 
76
{
 
77
        int v;
 
78
        int neg = 0;
 
79
        long n = 0;
 
80
 
 
81
        /* Skip leading white space. */
 
82
        while (*s == ' ' || *s == '\t')
 
83
                s++;
 
84
 
 
85
        /* Check for a leading + or -. */
 
86
        if (*s == '-')
 
87
        {
 
88
                neg = 1;
 
89
                s++;
 
90
        } else if (*s == '+')
 
91
        {
 
92
                s++;
 
93
        }
 
94
 
 
95
        /* Determine radix if caller does not specify. */
 
96
        if (radix == 0)
 
97
        {
 
98
                radix = 10;
 
99
                if (*s == '0')
 
100
                {
 
101
                        switch (*++s)
 
102
                        {
 
103
                        case 'x':
 
104
                                radix = 16;
 
105
                                s++;
 
106
                                break;
 
107
                        default:
 
108
                                radix = 8;
 
109
                                break;
 
110
                        }
 
111
                }
 
112
        }
 
113
 
 
114
        /* Parse the digits of the number. */
 
115
        for (;;)
 
116
        {
 
117
                if (*s >= '0' && *s <= '9')
 
118
                        v = *s - '0';
 
119
                else if (*s >= 'a' && *s <= 'f')
 
120
                        v = *s - 'a' + 10;
 
121
                else if (*s >= 'A' && *s <= 'F')
 
122
                        v = *s - 'A' + 10;
 
123
                else
 
124
                        break;
 
125
                if (v >= radix)
 
126
                        break;
 
127
                n = n * radix + v;
 
128
                s++;
 
129
        }
 
130
 
 
131
        if (pend != NULL)
 
132
        {
 
133
                /* Skip trailing white space. */
 
134
                while (*s == ' ' || *s == '\t')
 
135
                        s++;
 
136
                *pend = s;
 
137
        }
 
138
        if (neg)
 
139
                return (-n);
 
140
        return (n);
 
141
}
 
142
 
 
143
 
 
144
#if !HAVE_STRCHR
 
145
        char *
 
146
strchr(s, c)
 
147
        char *s;
 
148
        int c;
 
149
{
 
150
        for ( ;  *s != '\0';  s++)
 
151
                if (*s == c)
 
152
                        return (s);
 
153
        if (c == '\0')
 
154
                return (s);
 
155
        return (NULL);
 
156
}
 
157
#endif
 
158
 
 
159
        int
 
160
main(argc, argv)
 
161
        int argc;
 
162
        char *argv[];
 
163
{
 
164
        char *arg;
 
165
        char *s;
 
166
        int no_more_options;
 
167
 
 
168
        no_more_options = 0;
 
169
        while (--argc > 0)
 
170
        {
 
171
                arg = *++argv;
 
172
                if (*arg != '-' || no_more_options)
 
173
                        break;
 
174
                switch (*++arg)
 
175
                {
 
176
                case 'a':
 
177
                        quote_all = 1;
 
178
                        break;
 
179
                case 'c':
 
180
                        closequote = *++arg;
 
181
                        break;
 
182
                case 'd':
 
183
                        closequote = lstrtol(++arg, 0, &s);
 
184
                        if (s == arg)
 
185
                                pr_error("Missing number after -d");
 
186
                        break;
 
187
                case 'e':
 
188
                        if (strcmp(++arg, "-") == 0)
 
189
                                meta_escape = "";
 
190
                        else
 
191
                                meta_escape = arg;
 
192
                        break;
 
193
                case 'o':
 
194
                        openquote = *++arg;
 
195
                        break;
 
196
                case 'p':
 
197
                        openquote = lstrtol(++arg, 0, &s);
 
198
                        if (s == arg)
 
199
                                pr_error("Missing number after -p");
 
200
                        break;
 
201
                case 'm':
 
202
                        metachars[num_metachars++] = *++arg;
 
203
                        metachars[num_metachars] = '\0';
 
204
                        break;
 
205
                case 'n':
 
206
                        metachars[num_metachars++] = lstrtol(++arg, 0, &s);
 
207
                        if (s == arg)
 
208
                                pr_error("Missing number after -n");
 
209
                        metachars[num_metachars] = '\0';
 
210
                        break;
 
211
                case '?':
 
212
                        pr_usage();
 
213
                        return (0);
 
214
                case '-':
 
215
                        if (*++arg == '\0')
 
216
                        {
 
217
                                no_more_options = 1;
 
218
                                break;
 
219
                        }
 
220
                        if (strcmp(arg, "version") == 0)
 
221
                        {
 
222
                                pr_version();
 
223
                                return (0);
 
224
                        }
 
225
                        if (strcmp(arg, "help") == 0)
 
226
                        {
 
227
                                pr_usage();
 
228
                                return (0);
 
229
                        }
 
230
                        pr_error("Invalid option after --");
 
231
                default:
 
232
                        pr_error("Invalid option letter");
 
233
                }
 
234
        }
 
235
 
 
236
        while (argc-- > 0)
 
237
        {
 
238
                int has_meta = 0;
 
239
                arg = *argv++;
 
240
                for (s = arg;  *s != '\0';  s++)
 
241
                {
 
242
                        if (strchr(metachars, *s) != NULL)
 
243
                        {
 
244
                                has_meta = 1;
 
245
                                break;
 
246
                        }
 
247
                }
 
248
                if (quote_all || (has_meta && strlen(meta_escape) == 0))
 
249
                        printf("%c%s%c", openquote, arg, closequote);
 
250
                else 
 
251
                {
 
252
                        for (s = arg;  *s != '\0';  s++)
 
253
                        {
 
254
                                if (strchr(metachars, *s) != NULL)
 
255
                                        printf("%s", meta_escape);
 
256
                                printf("%c", *s);
 
257
                        }
 
258
                }
 
259
                if (argc > 0)
 
260
                        printf(" ");
 
261
                else
 
262
                        printf("\n");
 
263
        }
 
264
        return (0);
 
265
}