Paul Eggert <eggert <at> CS.UCLA.EDU> writes: > > Eric Blake <ebb9 <at> byu.net> writes: > > > Since AS_BASENAME is undocumented, and since it is most useful in variable > > assignments, perhaps it is time to redefine it as follows (untested) > > > > # AS_BASENAME(var, name, [ext]) > > # ----------------------------- > > # Compute the basename of NAME, with trailing EXT removed, and assign > > # the result to the shell variable VAR. > > I like this idea. It's been on my mental to-do list for quite some > time, and it's an obvious improvement. We can't simply use "${1##*/}", > though, as that mishandles the basenames of strings like "a/b/" and "///".
Good points on doing the implementation correctly. And I guess we should seriously think about the impacts of implementing suffix stripping support (as was pointed out, doing it in sed or expr is not necessarily fun). But it should still be possible without a fork, and now that we are ready to assume shell functions, the hairy logic will only be output once rather than at every use of the current _AS_BASENAME_EXPR. Hmm. While AS_BASENAME is undocumented, AS_DIRNAME is documented, and users will cry foul if we break backwards compatibility (and I have the nagging suspicion that AS_BASENAME is probably used in the wild, in spite of being undocumented, although I haven't yet found anything). We could do: AS_DIRNAME(name, [var]) where we use the old semantics if var is empty, and new assignment semantics if var is provided. But how does that translate to basename? AS_BASENAME(name, [ext], [var]) is awkward to use (the second argument will usually be empty). This is not entirely a showstopper (after all, m4_for usually has an empty argument), but it still seems weird that basename has an empty argument when dirname does not. Plus, seeing name before var reads differently than straight shell code, where you expect var=<code to process $name>. Maybe we invent a new set of names, and mark AS_DIRNAME obsolete in favor of the new name, along these lines? # AS_BASENAME(name) # ----------------- # Output shell code that can safely be enclosed in `` (but not "``"), # which will compute the basename of NAME. m4_defun([AS_BASENAME], [AS_ASSIGN_BASENAME([as_var], [$1]) AS_ECHO(["$as_var"])]) # AS_ASSIGN_BASENAME(var, name, [ext]) # --------------------------------- # Compute the basename of NAME, strip any suffix EXT, and place the result # in shell variable VAR. m4_defun([AS_ASSIGN_BASENAME], [as_func_basename "$2" "$3"]) # AS_DIRNAME(name) # similar to AS_BASENAME # AS_ASSIGN_DIRNAME(var, name) # similar to AS_ASSIGN_BASENAME -- Eric Blake