delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/12/13/12:15:23

From: "Arthur J. O'Dwyer" <ajo AT andrew DOT cmu DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Bug in command-line globbing
Date: Fri, 13 Dec 2002 11:58:54 -0500 (EST)
Organization: Carnegie Mellon, Pittsburgh, PA
Lines: 101
Message-ID: <Pine.GSO.4.44L-027.0212131136220.4357-100000@unix4.andrew.cmu.edu>
NNTP-Posting-Host: smtp5.andrew.cmu.edu
Mime-Version: 1.0
X-Trace: bb3.andrew.cmu.edu 1039798734 15590 128.2.10.85 (13 Dec 2002 16:58:54 GMT)
X-Complaints-To: advisor AT andrew DOT cmu DOT edu
NNTP-Posting-Date: 13 Dec 2002 16:58:54 GMT
In-Reply-To: <atc54i$m3m$2@antares.lu.erisoft.se>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019