home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / mush6.0 / part02 / aliases.c next >
Encoding:
C/C++ Source or Header  |  1988-04-12  |  4.5 KB  |  177 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.     char *tmpargv[2];
  100.     (void) argv_to_string(buf, argv);
  101.     if ((p = any(buf, " \t=")) && *p != '=')
  102.         *p = '=';
  103.     /* if we're setting an alias, enforce the insertion of commas
  104.      * between each well-formed address.
  105.      */
  106.     if (list == &aliases)
  107.         fix_up_addr(p+1);
  108.     tmpargv[0] = buf;
  109.     tmpargv[1] = NULL;
  110.     (void) add_option(list, tmpargv);
  111.     }
  112.     return -1;
  113. }
  114.  
  115. /* takes string 's' which can be a name or list of names separated by
  116.  * commas and checks to see if each is aliased to something else.
  117.  * return address of the static buf.
  118.  */
  119. char *
  120. alias_to_address(s)
  121. register char *s;
  122. {
  123.     static char buf[BUFSIZ];
  124.     register char *p, *p2, *tmp;
  125.     char newbuf[BUFSIZ], c;
  126.     static int recursive;
  127.  
  128.     if (!aliases)
  129.     return strcpy(buf, s);
  130.     if (!s || !*s) {
  131.     print("No recipeints!?!\n");
  132.     return NULL;
  133.     }
  134.     if (!recursive) {
  135.     bzero(buf, BUFSIZ);
  136.     p2 = buf;  /* if we're starting all this, p2 starts at &buf[0] */
  137.     } else
  138.     p2 = buf+strlen(buf);   /* else, pick up where we left off */
  139.  
  140.     if (++recursive == 30) {
  141.     print("alias references too many addresses!\n");
  142.     recursive = 0;
  143.     return NULL;
  144.     }
  145.     do  {
  146.     if (!(p = get_name_n_addr(s, NULL, NULL)))
  147.         break;
  148.     c = *p, *p = 0;
  149.  
  150.     /* if this is an alias, recurse this routine to expand it out */
  151.     if ((tmp = do_set(aliases, s)) && *tmp) {
  152.         if (!alias_to_address(strcpy(newbuf, tmp))) {
  153.         *p = c;
  154.         return NULL;
  155.         } else
  156.         p2 = buf+strlen(buf);
  157.     /* Now, make sure the buffer doesn't overflow */
  158.     } else if (strlen(s) + (p2-buf) + 2 > BUFSIZ) {  /* add " "  + NULL */
  159.         print("address length too long.\n");
  160.         recursive = 0;
  161.         *p = c;
  162.         return NULL;
  163.     } else {
  164.         /* append the new alias (or unchanged address) onto the buffer */
  165.         p2 += Strcpy(p2, s);
  166.         *p2++ = ',', *p2++ = ' ';
  167.     }
  168.     for (*p = c; *p == ',' || isspace(*p); p++)
  169.         ;
  170.     } while (*(s = p));
  171.     if (recursive)
  172.     recursive--;
  173.     if (!recursive)
  174.     *(p2-2) = 0;  /* get rid of last ", " if end of recursion */
  175.     return buf;
  176. }
  177.