lessecho.c (60786) | lessecho.c (128345) |
---|---|
1/* | 1/* |
2 * Copyright (C) 1984-2000 Mark Nudelman | 2 * Copyright (C) 1984-2002 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. | 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. |
|
21 * -a Specifies that all arguments are to be quoted. 22 * The default is that only arguments containing spaces are quoted. 23 */ 24 25#include "less.h" 26 | 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 |
27static char *version = "$Revision: 1.6 $"; | 31static char *version = "$Revision: 1.9 $"; |
28 29static int quote_all = 0; 30static char openquote = '"'; 31static char closequote = '"'; | 32 33static int quote_all = 0; 34static char openquote = '"'; 35static char closequote = '"'; |
36static char *meta_escape = "\\"; 37static char meta_escape_buf[2]; 38static char metachars[64] = ""; 39static int num_metachars = 0; |
|
32 33 static void 34pr_usage() 35{ 36 fprintf(stderr, | 40 41 static void 42pr_usage() 43{ 44 fprintf(stderr, |
37 "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-a] file ...\n"); | 45 "usage: lessecho [-ox] [-cx] [-pn] [-dn] [-mx] [-nn] [-ex] [-fn] [-a] file ...\n"); |
38} 39 40 static void 41pr_version() 42{ 43 char *p; 44 char buf[10]; 45 char *pbuf = buf; --- 118 unchanged lines hidden (view full) --- 164 arg = *++argv; 165 if (*arg != '-' || no_more_options) 166 break; 167 switch (*++arg) 168 { 169 case 'a': 170 quote_all = 1; 171 break; | 46} 47 48 static void 49pr_version() 50{ 51 char *p; 52 char buf[10]; 53 char *pbuf = buf; --- 118 unchanged lines hidden (view full) --- 172 arg = *++argv; 173 if (*arg != '-' || no_more_options) 174 break; 175 switch (*++arg) 176 { 177 case 'a': 178 quote_all = 1; 179 break; |
172 case 'o': 173 openquote = *++arg; 174 break; | |
175 case 'c': 176 closequote = *++arg; 177 break; | 180 case 'c': 181 closequote = *++arg; 182 break; |
183 case 'd': 184 closequote = lstrtol(++arg, 0, &s); 185 if (s == arg) 186 pr_error("Missing number after -d"); 187 break; 188 case 'e': 189 if (strcmp(++arg, "-") == 0) 190 meta_escape = ""; 191 else 192 meta_escape = arg; 193 break; 194 case 'f': 195 meta_escape_buf[0] = lstrtol(++arg, 0, &s); 196 meta_escape = meta_escape_buf; 197 if (s == arg) 198 pr_error("Missing number after -f"); 199 break; 200 case 'o': 201 openquote = *++arg; 202 break; |
|
178 case 'p': 179 openquote = lstrtol(++arg, 0, &s); 180 if (s == arg) | 203 case 'p': 204 openquote = lstrtol(++arg, 0, &s); 205 if (s == arg) |
181 pr_error("Missing number after -O"); | 206 pr_error("Missing number after -p"); |
182 break; | 207 break; |
183 case 'd': 184 closequote = lstrtol(++arg, 0, &s); | 208 case 'm': 209 metachars[num_metachars++] = *++arg; 210 metachars[num_metachars] = '\0'; 211 break; 212 case 'n': 213 metachars[num_metachars++] = lstrtol(++arg, 0, &s); |
185 if (s == arg) | 214 if (s == arg) |
186 pr_error("Missing number after -C"); | 215 pr_error("Missing number after -n"); 216 metachars[num_metachars] = '\0'; |
187 break; 188 case '?': 189 pr_usage(); 190 return (0); 191 case '-': 192 if (*++arg == '\0') 193 { 194 no_more_options = 1; --- 12 unchanged lines hidden (view full) --- 207 pr_error("Invalid option after --"); 208 default: 209 pr_error("Invalid option letter"); 210 } 211 } 212 213 while (argc-- > 0) 214 { | 217 break; 218 case '?': 219 pr_usage(); 220 return (0); 221 case '-': 222 if (*++arg == '\0') 223 { 224 no_more_options = 1; --- 12 unchanged lines hidden (view full) --- 237 pr_error("Invalid option after --"); 238 default: 239 pr_error("Invalid option letter"); 240 } 241 } 242 243 while (argc-- > 0) 244 { |
245 int has_meta = 0; |
|
215 arg = *argv++; | 246 arg = *argv++; |
216 if (quote_all || strchr(arg, ' ') != NULL) | 247 for (s = arg; *s != '\0'; s++) 248 { 249 if (strchr(metachars, *s) != NULL) 250 { 251 has_meta = 1; 252 break; 253 } 254 } 255 if (quote_all || (has_meta && strlen(meta_escape) == 0)) |
217 printf("%c%s%c", openquote, arg, closequote); | 256 printf("%c%s%c", openquote, arg, closequote); |
218 else 219 printf("%s", arg); | 257 else 258 { 259 for (s = arg; *s != '\0'; s++) 260 { 261 if (strchr(metachars, *s) != NULL) 262 printf("%s", meta_escape); 263 printf("%c", *s); 264 } 265 } |
220 if (argc > 0) 221 printf(" "); 222 else 223 printf("\n"); 224 } 225 return (0); 226} | 266 if (argc > 0) 267 printf(" "); 268 else 269 printf("\n"); 270 } 271 return (0); 272} |