install approved copyright notice
[unix-history] / usr / src / usr.bin / rdist / expand.c
index 73110ab..3a01a5d 100644 (file)
@@ -1,9 +1,27 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)expand.c    4.10 (Berkeley) 84/02/09";
-#endif
+static char sccsid[] = "@(#)expand.c   5.4 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "defs.h"
 
 
 #include "defs.h"
 
+#define        GAVSIZ  NCARGS / 6
 #define LC '{'
 #define RC '}'
 
 #define LC '{'
 #define RC '}'
 
@@ -24,6 +42,10 @@ char *entp;
 char   **sortbase;
 
 char   *index();
 char   **sortbase;
 
 char   *index();
+int    argcmp();
+
+#define sort() qsort((char *)sortbase, &eargv[eargc] - sortbase, \
+                     sizeof(*sortbase), argcmp), sortbase = &eargv[eargc]
 
 /*
  * Take a list of names and expand any macros, etc.
 
 /*
  * Take a list of names and expand any macros, etc.
@@ -31,6 +53,8 @@ char  *index();
  * wh = E_SHELL if expanding shell characters.
  * wh = E_TILDE if expanding `~'.
  * or any of these or'ed together.
  * wh = E_SHELL if expanding shell characters.
  * wh = E_TILDE if expanding `~'.
  * or any of these or'ed together.
+ *
+ * Major portions of this were snarfed from csh/sh.glob.c.
  */
 struct namelist *
 expand(list, wh)
  */
 struct namelist *
 expand(list, wh)
@@ -47,8 +71,14 @@ expand(list, wh)
                prnames(list);
        }
 
                prnames(list);
        }
 
-       if (wh == 0)
+       if (wh == 0) {
+               register char *cp;
+
+               for (nl = list; nl != NULL; nl = nl->n_next)
+                       for (cp = nl->n_name; *cp; cp++)
+                               *cp = *cp & TRIM;
                return(list);
                return(list);
+       }
 
        which = wh;
        path = tpathp = pathp = pathbuf;
 
        which = wh;
        path = tpathp = pathp = pathbuf;
@@ -101,18 +131,18 @@ expstr(s)
        if ((which & E_VARS) && (cp = index(s, '$')) != NULL) {
                *cp++ = '\0';
                if (*cp == '\0') {
        if ((which & E_VARS) && (cp = index(s, '$')) != NULL) {
                *cp++ = '\0';
                if (*cp == '\0') {
-                       error("no variable name after '$'\n");
+                       yyerror("no variable name after '$'");
                        return;
                }
                if (*cp == LC) {
                        cp++;
                        if ((tail = index(cp, RC)) == NULL) {
                        return;
                }
                if (*cp == LC) {
                        cp++;
                        if ((tail = index(cp, RC)) == NULL) {
-                               error("unmatched %c\n", *cp);
+                               yyerror("unmatched '{'");
                                return;
                        }
                        *tail++ = savec = '\0';
                        if (*cp == '\0') {
                                return;
                        }
                        *tail++ = savec = '\0';
                        if (*cp == '\0') {
-                               error("no variable name after '$'\n");
+                               yyerror("no variable name after '$'");
                                return;
                        }
                } else {
                                return;
                        }
                } else {
@@ -153,7 +183,8 @@ expstr(s)
                        *cp1 = '\0';
                        if (pw == NULL || strcmp(pw->pw_name, buf+1) != 0) {
                                if ((pw = getpwnam(buf+1)) == NULL) {
                        *cp1 = '\0';
                        if (pw == NULL || strcmp(pw->pw_name, buf+1) != 0) {
                                if ((pw = getpwnam(buf+1)) == NULL) {
-                                       error("unknown user %s\n", buf+1);
+                                       strcat(buf, ": unknown user name");
+                                       yyerror(buf+1);
                                        return;
                                }
                        }
                                        return;
                                }
                        }
@@ -179,27 +210,17 @@ expstr(s)
        oeargc = eargc;
        expany = 0;
        expsh(s);
        oeargc = eargc;
        expany = 0;
        expsh(s);
-       if (eargc != oeargc)
-               sort();
+       if (eargc == oeargc)
+               Cat(s, "");             /* "nonomatch" is set */
+       sort();
 }
 
 }
 
-/*
- * Bubble sort any new entries
- */
-sort()
+static
+argcmp(a1, a2)
+       char **a1, **a2;
 {
 {
-       register char **p1, **p2, *c;
-       char **ap = &eargv[eargc];
-
-       p1 = sortbase;
-       while (p1 < ap-1) {
-               p2 = p1;
-               while (++p2 < ap)
-                       if (strcmp(*p1, *p2) > 0)
-                               c = *p1, *p1 = *p2, *p2 = c;
-               p1++;
-       }
-       sortbase = ap;
+
+       return (strcmp(*a1, *a2));
 }
 
 /*
 }
 
 /*
@@ -278,7 +299,9 @@ matchdir(pattern)
 patherr1:
        closedir(dirp);
 patherr2:
 patherr1:
        closedir(dirp);
 patherr2:
-       error("%s: %s\n", path, sys_errlist[errno]);
+       strcat(path, ": ");
+       strcat(path, sys_errlist[errno]);
+       yyerror(path);
 }
 
 execbrc(p, s)
 }
 
 execbrc(p, s)
@@ -308,12 +331,14 @@ execbrc(p, s)
                        for (pe++; *pe && *pe != ']'; pe++)
                                continue;
                        if (!*pe)
                        for (pe++; *pe && *pe != ']'; pe++)
                                continue;
                        if (!*pe)
-                               error("Missing ]\n");
+                               yyerror("Missing ']'");
                        continue;
                }
 pend:
                        continue;
                }
 pend:
-       if (brclev || !*pe)
-               fatal("Missing }\n");
+       if (brclev || !*pe) {
+               yyerror("Missing '}'");
+               return (0);
+       }
        for (pl = pm = p; pm <= pe; pm++)
                switch (*pm & (QUOTE|TRIM)) {
 
        for (pl = pm = p; pm <= pe; pm++)
                switch (*pm & (QUOTE|TRIM)) {
 
@@ -352,7 +377,7 @@ doit:
                        for (pm++; *pm && *pm != ']'; pm++)
                                continue;
                        if (!*pm)
                        for (pm++; *pm && *pm != ']'; pm++)
                                continue;
                        if (!*pm)
-                               error("Missing ]\n");
+                               yyerror("Missing ']'");
                        continue;
                }
        return (0);
                        continue;
                }
        return (0);
@@ -408,8 +433,10 @@ amatch(s, p)
                                        if (scc == (lc = cc))
                                                ok++;
                        }
                                        if (scc == (lc = cc))
                                                ok++;
                        }
-                       if (cc == 0)
-                               fatal("Missing ]\n");
+                       if (cc == 0) {
+                               yyerror("Missing ']'");
+                               return (0);
+                       }
                        continue;
 
                case '*':
                        continue;
 
                case '*':
@@ -428,7 +455,7 @@ amatch(s, p)
                        return (scc == '\0');
 
                default:
                        return (scc == '\0');
 
                default:
-                       if (c != scc)
+                       if ((c & TRIM) != scc)
                                return (0);
                        continue;
 
                                return (0);
                        continue;
 
@@ -488,8 +515,10 @@ smatch(s, p)
                                        if (scc == (lc = cc))
                                                ok++;
                        }
                                        if (scc == (lc = cc))
                                                ok++;
                        }
-                       if (cc == 0)
-                               fatal("Missing ]\n");
+                       if (cc == 0) {
+                               yyerror("Missing ']'");
+                               return (0);
+                       }
                        continue;
 
                case '*':
                        continue;
 
                case '*':
@@ -525,7 +554,7 @@ Cat(s1, s2)
 
        nleft -= len;
        if (nleft <= 0 || ++eargc >= GAVSIZ)
 
        nleft -= len;
        if (nleft <= 0 || ++eargc >= GAVSIZ)
-               error("Arguments too long\n");
+               yyerror("Arguments too long");
        eargv[eargc] = 0;
        eargv[eargc - 1] = s = malloc(len);
        if (s == NULL)
        eargv[eargc] = 0;
        eargv[eargc - 1] = s = malloc(len);
        if (s == NULL)
@@ -542,9 +571,11 @@ addpath(c)
 {
 
        if (pathp >= lastpathp)
 {
 
        if (pathp >= lastpathp)
-               fatal("Pathname too long\n");
-       *pathp++ = c;
-       *pathp = '\0';
+               yyerror("Pathname too long");
+       else {
+               *pathp++ = c & TRIM;
+               *pathp = '\0';
+       }
 }
 
 /*
 }
 
 /*
@@ -580,7 +611,7 @@ exptilde(buf, file)
                        s3 = NULL;
                if (pw == NULL || strcmp(pw->pw_name, file) != 0) {
                        if ((pw = getpwnam(file)) == NULL) {
                        s3 = NULL;
                if (pw == NULL || strcmp(pw->pw_name, file) != 0) {
                        if ((pw = getpwnam(file)) == NULL) {
-                               error("unknown user %s\n", file);
+                               error("%s: unknown user name\n", file);
                                if (s3 != NULL)
                                        *s3 = '/';
                                return(NULL);
                                if (s3 != NULL)
                                        *s3 = '/';
                                return(NULL);