Mail Archives: djgpp/2002/12/13/12:15:23
On 13 Dec 2002, Martin Stromberg wrote:
>
> Arthur J. O'Dwyer (ajo AT andrew DOT cmu DOT edu) wrote:
> : > If you are writing a tr-clone, your best bet might be to disable globbing
> : > (i think this is not so bad since globbing is meant to work with file
> : > names).
>
> : Yeah, that's what I've done as a stopgap measure. But it would be
> : nice if globbing worked correctly, so one could invoke a program as
>
> : % tr -d '*\' *.txt
>
> : and have it work as expected. I don't know why I'd want to delete all
>
> You can, with some extra work. 1. Disable globbing as described so
> far. 2. When you find an argument that isn't an option (i. e. a file
> name) call glob() on it.
Could you expand on this answer, please? I still think the default
globbing is badly designed, but if it's not going to get "fixed", I'd
like to know more about the user-defined globbing functions so I can
work around it when necessary.
If I wanted to write my own globbing code, could I put it in
__crt0_glob_function() something like this?
#include <crt0.h>
char **__crt0_glob_function(char *arg)
{
char **argv = malloc(100 * sizeof *argv); /* the returned array */
int cur_arg = 0;
char temp[200], *tptr; /* the current argument */
int inquotes = 0; /* flag: inside "" */
int inchotes = 0; /* flag: inside '' */
int i;
tptr = temp;
for (i=0; arg[i]; i++) {
if (arg[i] == '\"' && !inchotes) {
inquotes = !inquotes;
if (!inquotes && (tptr > temp)) {
*tptr++ = '\0';
argv[cur_arg] = malloc(tptr-temp);
strcpy(argv[cur_arg], temp);
tptr = temp;
++ cur_arg;
}
}
else if (arg[i] == '\'' && !inquotes) {
inchotes = !inchotes;
if (!inchotes && (tptr > temp)) {
*tptr++ = '\0';
argv[cur_arg] = malloc(tptr-temp);
strcpy(argv[cur_arg], temp);
tptr = temp;
++ cur_arg;
}
}
else if (arg[i] == '\\') {
++ i;
if (arg[i]) {
*tptr++ = arg[i];
}
}
else {
if (!inquotes && !inchotes && isspace(arg[i])) {
*tptr++ = '\0';
argv[cur_arg] = malloc(tptr-temp);
strcpy(argv[cur_arg], temp);
tptr = temp;
++ cur_arg;
}
else *tptr++ = arg[i];
}
}
if (inquotes) {
fprintf(stderr, "Mismatched \".\n");
exit(1);
}
else if (inchotes) {
fprintf(stderr, "Mismatched '.\n");
exit(1);
}
argv[cur_arg] = NULL;
return argv;
}
Or is parameter 'arg' only a single argument, not the whole line?
And if so, how can I do the parsing I need to do?
Thanks,
-Arthur
- Raw text -