home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / yapp / part02 / macro.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-29  |  8.9 KB  |  249 lines

  1. /* MACRO.C */
  2. static    char sccsid[] = "@(#)macro.c 1.19 93/06/07 Copyright (c)1993 thalerd";
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <sys/types.h>
  8. #include "config.h"
  9. #include "struct.h"
  10. #include "globals.h"
  11. #include "macro.h"
  12. #include "lib.h"     /* for ultrix where strdup() is defined */
  13. #include "xalloc.h"
  14.  
  15. static unsigned short num_macros=0;
  16. static macro_t macro[MAX_MACROS];
  17.  
  18. char *
  19. itostr(i)
  20. short i;
  21. {
  22. static char buff[MAX_LINE_LENGTH];
  23.  
  24.    sprintf(buff,"%d",i);
  25.    return buff;
  26. }
  27.  
  28. /******************************************************************************/
  29. /* EXPAND A MACRO                                                             */
  30. /* UNK Could put hashing in later to speed things up if needed                */
  31. /******************************************************************************/
  32. char *               /* RETURNS: string macro expands to */
  33. expand(mac,mask)     /* ARGUMENTS:                       */
  34. char          *mac;  /*    Macro name to expand          */
  35. unsigned short mask; /*    Type of macro (see macro.h)   */
  36. {
  37.    unsigned short i;
  38.  
  39.    for (i=0; i<num_macros && (!(mask & macro[i].mask)
  40.     || !match(mac,macro[i].name)); i++);
  41.    if (i==num_macros) {
  42.       /* A few default values */
  43.       if (flags & O_DEBUG)
  44.          printf("expand: '%s' %hu\n",mac, mask);
  45.       if (mask & DM_VAR) {
  46.          if (match(mac,"editor"))     {
  47.             if (getenv("EDITOR")) return getenv("EDITOR");
  48.             return EDITOR;
  49.          }
  50.          if (match(mac,"visual"))     { 
  51.             if (getenv("VISUAL")) return getenv("VISUAL");
  52.             return expand("editor",DM_VAR);
  53.          }
  54. /* This should not be there, since "define pager" should turn
  55.  * off paging.
  56.          if (match(mac,"pager"))     return PAGER;
  57.  */
  58.          if (match(mac,"prompt"))    return PROMPT;
  59.          if (match(mac,"noconfp"))   return NOCONFP;
  60.          if (match(mac,"rfpprompt")) return RFPPROMPT;
  61.          if (match(mac,"obvprompt")) return OBVPROMPT;
  62.          if (match(mac,"joqprompt")) return JOQPROMPT;
  63.          if (match(mac,"edbprompt")) return EDBPROMPT;
  64.          if (match(mac,"text"))      return TEXT;
  65.          if (match(mac,"escape"))    return ESCAPE;
  66.          if (match(mac,"cmddel"))    return CMDDEL;
  67.          if (match(mac,"bufdel"))    return BUFDEL;
  68.          if (match(mac,"gecos"))     return GECOS;
  69.          if (match(mac,"rsep"))      return RSEP;
  70.          if (match(mac,"nsep"))      return NSEP;
  71.          if (match(mac,"ishort"))    return ISHORT;
  72.          if (match(mac,"isep"))      return ISEP;
  73.          if (match(mac,"fsep"))      return FSEP;
  74.          if (match(mac,"txtsep"))    return TXTSEP;
  75.          if (match(mac,"zsep"))      return ZSEP;
  76.          if (match(mac,"printmsg"))  return PRINTMSG;
  77.          if (match(mac,"mailmsg"))   return MAILMSG;
  78.          if (match(mac,"linmsg"))    return LINMSG;
  79.          if (match(mac,"loutmsg"))   return LOUTMSG;
  80.          if (match(mac,"indxmsg"))   return INDXMSG;
  81.          if (match(mac,"joinmsg"))   return JOINMSG;
  82.          if (match(mac,"partmsg"))   return PARTMSG;
  83.          if (match(mac,"checkmsg"))  return CHECKMSG;
  84.          if (match(mac,"confmsg"))   return CONFMSG;
  85.          if (match(mac,"bullmsg"))   return BULLMSG;
  86.          if (match(mac,"wellmsg"))   return WELLMSG;
  87.          if (match(mac,"listmsg"))   return LISTMSG;
  88.          if (match(mac,"replysep"))  return REPLYSEP;
  89. #ifdef NEWS
  90.          if (match(mac,"newssep"))   return NEWSSEP;
  91. #endif
  92.          if (match(mac,"shell"))     {
  93.             if (getenv("SHELL")) return getenv("SHELL");
  94.             return SHELL;
  95.          }
  96.  
  97.          /* Global status variables */
  98.          if (match(mac,"lastitem"))  return itostr(st_glob.i_last);
  99.          if (match(mac,"firstitem")) return itostr(st_glob.i_first);
  100.          if (match(mac,"curitem"))   return itostr(st_glob.i_current);
  101.          if (match(mac,"newresp"))   return itostr(st_glob.i_newresp);
  102.          if (match(mac,"brandnew"))  return itostr(st_glob.i_brandnew);
  103.          if (match(mac,"unseen"))    return itostr(st_glob.i_unseen);
  104.          if (match(mac,"numitems"))  return itostr(st_glob.i_numitems);
  105.          if (match(mac,"lowresp"))   return itostr(st_glob.r_first);
  106.          if (match(mac,"highresp"))  return itostr(st_glob.r_last);
  107.          if (match(mac,"curresp"))   return itostr(st_glob.r_current);
  108.          if (match(mac,"lastresp"))  return itostr(st_glob.r_max);
  109.          if (match(mac,"seenresp"))  return itostr(st_glob.r_lastseen);
  110.          if (match(mac,"curline"))   return itostr(st_glob.l_current);
  111.          if (match(mac,"fullname"))  return st_glob.fullname;
  112.       }
  113.       return 0;
  114.    }
  115.    return macro[i].value;
  116. }
  117.  
  118. /******************************************************************************/
  119. /* PROCESS MACRO DEFINES AND UNDEFINES                                        */
  120. /******************************************************************************/
  121. int               /* RETURNS: (nothing)     */
  122. define(argc,argv) /* ARGUMENTS:             */
  123. int    argc;      /*    Number of arguments */
  124. char **argv;      /*    Argument list       */
  125. {
  126.    unsigned short i;
  127.    FILE *fp;
  128.  
  129.    switch(argc) {
  130.    case 1: /* Display current macros */
  131.            open_pipe();
  132.            if (status & S_REDIRECT) fp=st_glob.outp;
  133.            else                     fp=stdout;
  134.            fprintf(fp,"What       Is Short For\n\n");
  135.            for (i=0; i<num_macros && !(status & S_INT); i++) 
  136.               fprintf(fp,"%-10s %3hd %s\n",macro[i].name, macro[i].mask, 
  137.                  macro[i].value);
  138.            return 1;
  139.  
  140.    case 2: /* Remove name from macro table */
  141.            undef_name(argv[1]);
  142.            return 1;
  143.  
  144.    case 3: def_macro(argv[1],DM_VAR,argv[2]);
  145.            return 1;
  146.  
  147.    case 4: def_macro(argv[1],atoi(argv[2]),argv[3]);
  148.            return 1;
  149.    default: printf("Bad parameters near \"%s\"\n",argv[4]);
  150.    }
  151.    return 2;
  152. }
  153.  
  154. /******************************************************************************/
  155. /* DEFINE A MACRO EXPANSION                                                   */
  156. /******************************************************************************/
  157. void                       /* RETURNS: (nothing)             */
  158. def_macro(name,mask,val)   /* ARGUMENTS:                     */
  159. char *name;                /*    Alias/variable name         */
  160. int   mask;                /*    Type of macro (see macro.h) */
  161. char *val;                 /*    String to expand it to      */
  162. {
  163.    short i;
  164.    char buff[MAX_LINE_LENGTH];
  165.    char *value;
  166.  
  167.    if (!mask) {
  168.       printf("UNK Bad mask value.\n");
  169.       return;
  170.    }
  171.    value=noquote(val,0);
  172.  
  173. #ifndef NOPUTENV
  174.    if (mask & DM_ENVAR) {
  175.       sprintf(buff,"%s=%s",name,value);
  176.       putenv(buff);
  177.    }
  178. #endif
  179.  
  180.    /* Add name to macro table */
  181.    if (mask & ~DM_ENVAR) {
  182.       if (mode>=M_SUPERSANE) mask |= mode;
  183.       for (i=0; i<num_macros && (!(mask & macro[i].mask)
  184.        || !match(name,macro[i].name)); i++);
  185.       if (i==num_macros) {
  186.          macro[num_macros].name  = xstrdup(name);
  187.          macro[num_macros].mask  = mask;
  188.          macro[num_macros++].value = xstrdup(value);
  189.       } else { /* already defined */
  190.          if ((macro[i].mask & (M_SANE|M_SUPERSANE))
  191.           || !(mask & (M_SANE|M_SUPERSANE))) {
  192.             macro[i].mask  = mask;
  193.             xfree(macro[i].value);
  194.             macro[i].value = xstrdup(value);
  195.          }
  196.       }
  197.    }
  198. }
  199.  
  200. /******************************************************************************/
  201. /* REMOVE A CLASS OF MACRO EXPANSIONS                                         */
  202. /******************************************************************************/
  203. void                 /* RETURNS: (nothing)            */
  204. undefine(mask)       /* ARGUMENTS:                    */
  205. unsigned short mask; /*    Bitmask of types to remove */
  206. {
  207.    unsigned short i;
  208.  
  209.    if (flags & O_DEBUG) printf("undefine: mask=%hd\n",mask);
  210.    for (i=0; i<num_macros; i++) {
  211.       if (macro[i].mask & mask) {
  212.          if (flags & O_DEBUG)
  213.             printf("undefine: %s %hd (%d/%d)\n",macro[i].name,macro[i].mask,i,num_macros);
  214.          undef_macro(i);
  215.          i--;
  216.       }
  217.    }
  218. }
  219.  
  220. void
  221. undef_name(name) 
  222. char *name;
  223. {
  224.    short i;
  225.  
  226.    /* Remove name from macro table */
  227.    for (i=0; i<num_macros && !match(name,macro[i].name); i++);
  228.    if (i==num_macros)
  229.       printf("Can't find definition for %s\n",name);
  230.    else
  231.       undef_macro(i);
  232. }
  233.  
  234. /******************************************************************************/
  235. /* REMOVE A SINGLE MACRO EXPANSION                                            */
  236. /******************************************************************************/
  237. void              /* RETURNS: (nothing)           */
  238. undef_macro(i)    /* ARGUMENTS:                   */
  239. unsigned short i; /*   Index of macro to undefine */
  240. {
  241.    xfree(macro[i].name);
  242.    xfree(macro[i].value);
  243.    macro[i].name = macro[num_macros-1].name;
  244.    macro[i].mask = macro[num_macros-1].mask;
  245.    macro[i].value = macro[num_macros-1].value;
  246. /*   memcpy(macro[i],macro[num_macros-1],sizeof(macro_t)); */
  247.    num_macros--;
  248. }
  249.