home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / mush5.7 / part02 / aliases.c next >
Encoding:
C/C++ Source or Header  |  1987-09-16  |  4.4 KB  |  171 lines

  1. /* (c) copyright @(#)aliases.c    2.4    10/15/86 (Dan Heller) */
  2.  
  3. #include "mush.h"
  4.  
  5. /*
  6.  * do_alias handles aliases, header settings, functions, and fkeys.
  7.  * since they're all handled in the same manner, the same routine is
  8.  * used. argv[0] determines which to use.
  9.  * alias is given here as an example
  10.  *
  11.  * alias           identify all aliases
  12.  * alias name      identify alias
  13.  * alias name arg1 arg2 arg3... -> name="arg1 arg2 arg3"; call set_option
  14.  * unalias arg1 [arg2 arg3 ... ]        unalias args
  15.  *
  16.  * same is true for dealing with your own headers.
  17.  * (also the expand command)
  18.  * always return -1 since it nas no effect on messages
  19.  */
  20. do_alias(argc, argv)
  21. register char **argv;
  22. {
  23.     register char *cmd = *argv, *p;
  24.     struct options **list;
  25.     char firstchar = *cmd, buf[BUFSIZ];
  26.  
  27.     if (argc == 0)
  28.     return -1;
  29.     if (firstchar == 'u')
  30.     firstchar = cmd[2];
  31.     if (*++argv && !strcmp(*argv, "-?")) { /* doesn't apply for fkeys */
  32.     register char *help_str;
  33.     if (firstchar == 'a' || firstchar == 'e')
  34.         help_str = "alias";
  35.     else if (firstchar == 'c')
  36.         help_str = "func_help";
  37.     else if (firstchar == 'f')
  38.         help_str = "fkey_help";
  39.     else
  40.         help_str = "own_hdrs";
  41.     return help(0, help_str, cmd_help);
  42.     }
  43.  
  44.     if (firstchar == 'a')
  45.     list = &aliases;
  46.     else if (firstchar == 'c')
  47.     list = &functions;
  48.     else if (firstchar == 'f')
  49.     list = &fkeys;
  50.     else
  51.     list = &own_hdrs;
  52.  
  53.     if (*cmd == 'u') {
  54.     if (!*argv)
  55.         print("%s what?\n", cmd);
  56.     /* unset a list separated by spaces or ',' */
  57.     else while (*argv) {
  58.         if (!strcmp(*argv, "*")) /* unset everything */
  59.         while (*list)
  60.             (void) un_set(list, (*list)->option);
  61.         else if (!un_set(list, *argv))
  62.         print("\"%s\" isn't set\n", *argv);
  63.         argv++;
  64.     }
  65.     return -1;
  66.     }
  67.  
  68.     if (!*argv && *cmd != 'e') {
  69.     /* just type out all the aliases or own_hdrs */
  70.     (void) do_set(*list, NULL);
  71.     return -1;
  72.     }
  73.  
  74.     if (*cmd == 'e') {   /* command was "expand" (aliases only) */
  75.     if (!*argv)
  76.         print("expand which alias?\n");
  77.     else
  78.         do  {
  79.         print("%s: ", *argv);
  80.         if (p = alias_to_address(*argv))
  81.             print("%s\n", p);
  82.         } while (*++argv);
  83.     return -1;
  84.     }
  85.  
  86.     /* at this point, *argv now points to a variable name ...
  87.      * check for hdr -- if so, *argv better end with a ':' (check *p)
  88.      */
  89.     if (list == &own_hdrs && !(p = index(*argv, ':'))) {
  90.     print("header labels must end with a ':' (%s)\n", *argv);
  91.     return -1;
  92.     }
  93.     if (!argv[1] && !index(*argv, '='))
  94.     if (p = do_set(*list, *argv))
  95.         print("%s\n", p);
  96.     else
  97.         print("%s is not set\n", *argv);
  98.     else {
  99.     (void) argv_to_string(buf, argv);
  100.     if ((p = any(buf, " \t=")) && *p != '=')
  101.         *p = '=';
  102.     argv[0] = buf;
  103.     argv[1] = NULL;
  104.     (void) add_option(list, argv);
  105.     }
  106.     return -1;
  107. }
  108.  
  109. /* takes string 's' which can be a name or list of names separated by
  110.  * spaces or commas and checks to see if each is aliased to something else.
  111.  * return address of the static buf.
  112.  */
  113. char *
  114. alias_to_address(s)
  115. register char *s;
  116. {
  117.     static char buf[BUFSIZ];
  118.     register char *p = s, *p2, *tmp;
  119.     char newbuf[BUFSIZ];
  120.     static int recursive;
  121.  
  122.     if (!aliases)
  123.     return s;
  124.     if (!s || !*s) {
  125.     print("No recipeints!?!\n");
  126.     return NULL;
  127.     }
  128.     skipspaces(0);
  129.     if (!recursive) {
  130.     bzero(buf, BUFSIZ);
  131.     p2 = buf;  /* if we're starting all this, p2 starts at &buf[0] */
  132.     } else
  133.     p2 = buf+strlen(buf);   /* else, pick up where we left off */
  134.  
  135.     if (++recursive == 30) {
  136.     print("alias references too many addresses!\n");
  137.     recursive = 0;
  138.     return NULL;
  139.     }
  140.     /* find a comma, space, or newline -- if none exists, still go thru once */
  141.     for (s = p; (p = any(s, ", \n")) || *s; s = p) {
  142.     if (p)
  143.         *p++ = 0;  /* null terminate, comma/space/newline was found */
  144.     else
  145.         for(p = s; *p; p++);   /* last in list; go to end of string */
  146.  
  147.     /* if this is an alias, recurse this routine to expand it out */
  148.     if ((tmp = do_set(aliases, s)) && *tmp) {
  149.         if (!alias_to_address(strcpy(newbuf, tmp)))
  150.         return NULL;
  151.         else
  152.         p2 = buf+strlen(buf);
  153.     /* Now, make sure the buffer doesn't overflow */
  154.     } else if (strlen(s) + (p2-buf) + 2 > BUFSIZ) {  /* add " "  + NULL */
  155.         print("address length too long.\n");
  156.         recursive = 0;
  157.         return NULL;
  158.     } else {
  159.         /* append the new alias (or unchanged address) onto the buffer */
  160.         p2 += Strcpy(p2, s);
  161.         p2 += Strcpy(p2, ", "); /* two Strcpy's is faster than sprintf */
  162.     }
  163.     skipspaces(0);
  164.     }
  165.     if (recursive)
  166.     recursive--;
  167.     if (!recursive)
  168.     *(p2-2) = 0;  /* get rid of last ", " if end of recursion */
  169.     return buf;
  170. }
  171.