home *** CD-ROM | disk | FTP | other *** search
- /* MACRO.C */
- static char sccsid[] = "@(#)macro.c 1.19 93/06/07 Copyright (c)1993 thalerd";
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <sys/types.h>
- #include "config.h"
- #include "struct.h"
- #include "globals.h"
- #include "macro.h"
- #include "lib.h" /* for ultrix where strdup() is defined */
- #include "xalloc.h"
-
- static unsigned short num_macros=0;
- static macro_t macro[MAX_MACROS];
-
- char *
- itostr(i)
- short i;
- {
- static char buff[MAX_LINE_LENGTH];
-
- sprintf(buff,"%d",i);
- return buff;
- }
-
- /******************************************************************************/
- /* EXPAND A MACRO */
- /* UNK Could put hashing in later to speed things up if needed */
- /******************************************************************************/
- char * /* RETURNS: string macro expands to */
- expand(mac,mask) /* ARGUMENTS: */
- char *mac; /* Macro name to expand */
- unsigned short mask; /* Type of macro (see macro.h) */
- {
- unsigned short i;
-
- for (i=0; i<num_macros && (!(mask & macro[i].mask)
- || !match(mac,macro[i].name)); i++);
- if (i==num_macros) {
- /* A few default values */
- if (flags & O_DEBUG)
- printf("expand: '%s' %hu\n",mac, mask);
- if (mask & DM_VAR) {
- if (match(mac,"editor")) {
- if (getenv("EDITOR")) return getenv("EDITOR");
- return EDITOR;
- }
- if (match(mac,"visual")) {
- if (getenv("VISUAL")) return getenv("VISUAL");
- return expand("editor",DM_VAR);
- }
- /* This should not be there, since "define pager" should turn
- * off paging.
- if (match(mac,"pager")) return PAGER;
- */
- if (match(mac,"prompt")) return PROMPT;
- if (match(mac,"noconfp")) return NOCONFP;
- if (match(mac,"rfpprompt")) return RFPPROMPT;
- if (match(mac,"obvprompt")) return OBVPROMPT;
- if (match(mac,"joqprompt")) return JOQPROMPT;
- if (match(mac,"edbprompt")) return EDBPROMPT;
- if (match(mac,"text")) return TEXT;
- if (match(mac,"escape")) return ESCAPE;
- if (match(mac,"cmddel")) return CMDDEL;
- if (match(mac,"bufdel")) return BUFDEL;
- if (match(mac,"gecos")) return GECOS;
- if (match(mac,"rsep")) return RSEP;
- if (match(mac,"nsep")) return NSEP;
- if (match(mac,"ishort")) return ISHORT;
- if (match(mac,"isep")) return ISEP;
- if (match(mac,"fsep")) return FSEP;
- if (match(mac,"txtsep")) return TXTSEP;
- if (match(mac,"zsep")) return ZSEP;
- if (match(mac,"printmsg")) return PRINTMSG;
- if (match(mac,"mailmsg")) return MAILMSG;
- if (match(mac,"linmsg")) return LINMSG;
- if (match(mac,"loutmsg")) return LOUTMSG;
- if (match(mac,"indxmsg")) return INDXMSG;
- if (match(mac,"joinmsg")) return JOINMSG;
- if (match(mac,"partmsg")) return PARTMSG;
- if (match(mac,"checkmsg")) return CHECKMSG;
- if (match(mac,"confmsg")) return CONFMSG;
- if (match(mac,"bullmsg")) return BULLMSG;
- if (match(mac,"wellmsg")) return WELLMSG;
- if (match(mac,"listmsg")) return LISTMSG;
- if (match(mac,"replysep")) return REPLYSEP;
- #ifdef NEWS
- if (match(mac,"newssep")) return NEWSSEP;
- #endif
- if (match(mac,"shell")) {
- if (getenv("SHELL")) return getenv("SHELL");
- return SHELL;
- }
-
- /* Global status variables */
- if (match(mac,"lastitem")) return itostr(st_glob.i_last);
- if (match(mac,"firstitem")) return itostr(st_glob.i_first);
- if (match(mac,"curitem")) return itostr(st_glob.i_current);
- if (match(mac,"newresp")) return itostr(st_glob.i_newresp);
- if (match(mac,"brandnew")) return itostr(st_glob.i_brandnew);
- if (match(mac,"unseen")) return itostr(st_glob.i_unseen);
- if (match(mac,"numitems")) return itostr(st_glob.i_numitems);
- if (match(mac,"lowresp")) return itostr(st_glob.r_first);
- if (match(mac,"highresp")) return itostr(st_glob.r_last);
- if (match(mac,"curresp")) return itostr(st_glob.r_current);
- if (match(mac,"lastresp")) return itostr(st_glob.r_max);
- if (match(mac,"seenresp")) return itostr(st_glob.r_lastseen);
- if (match(mac,"curline")) return itostr(st_glob.l_current);
- if (match(mac,"fullname")) return st_glob.fullname;
- }
- return 0;
- }
- return macro[i].value;
- }
-
- /******************************************************************************/
- /* PROCESS MACRO DEFINES AND UNDEFINES */
- /******************************************************************************/
- int /* RETURNS: (nothing) */
- define(argc,argv) /* ARGUMENTS: */
- int argc; /* Number of arguments */
- char **argv; /* Argument list */
- {
- unsigned short i;
- FILE *fp;
-
- switch(argc) {
- case 1: /* Display current macros */
- open_pipe();
- if (status & S_REDIRECT) fp=st_glob.outp;
- else fp=stdout;
- fprintf(fp,"What Is Short For\n\n");
- for (i=0; i<num_macros && !(status & S_INT); i++)
- fprintf(fp,"%-10s %3hd %s\n",macro[i].name, macro[i].mask,
- macro[i].value);
- return 1;
-
- case 2: /* Remove name from macro table */
- undef_name(argv[1]);
- return 1;
-
- case 3: def_macro(argv[1],DM_VAR,argv[2]);
- return 1;
-
- case 4: def_macro(argv[1],atoi(argv[2]),argv[3]);
- return 1;
- default: printf("Bad parameters near \"%s\"\n",argv[4]);
- }
- return 2;
- }
-
- /******************************************************************************/
- /* DEFINE A MACRO EXPANSION */
- /******************************************************************************/
- void /* RETURNS: (nothing) */
- def_macro(name,mask,val) /* ARGUMENTS: */
- char *name; /* Alias/variable name */
- int mask; /* Type of macro (see macro.h) */
- char *val; /* String to expand it to */
- {
- short i;
- char buff[MAX_LINE_LENGTH];
- char *value;
-
- if (!mask) {
- printf("UNK Bad mask value.\n");
- return;
- }
- value=noquote(val,0);
-
- #ifndef NOPUTENV
- if (mask & DM_ENVAR) {
- sprintf(buff,"%s=%s",name,value);
- putenv(buff);
- }
- #endif
-
- /* Add name to macro table */
- if (mask & ~DM_ENVAR) {
- if (mode>=M_SUPERSANE) mask |= mode;
- for (i=0; i<num_macros && (!(mask & macro[i].mask)
- || !match(name,macro[i].name)); i++);
- if (i==num_macros) {
- macro[num_macros].name = xstrdup(name);
- macro[num_macros].mask = mask;
- macro[num_macros++].value = xstrdup(value);
- } else { /* already defined */
- if ((macro[i].mask & (M_SANE|M_SUPERSANE))
- || !(mask & (M_SANE|M_SUPERSANE))) {
- macro[i].mask = mask;
- xfree(macro[i].value);
- macro[i].value = xstrdup(value);
- }
- }
- }
- }
-
- /******************************************************************************/
- /* REMOVE A CLASS OF MACRO EXPANSIONS */
- /******************************************************************************/
- void /* RETURNS: (nothing) */
- undefine(mask) /* ARGUMENTS: */
- unsigned short mask; /* Bitmask of types to remove */
- {
- unsigned short i;
-
- if (flags & O_DEBUG) printf("undefine: mask=%hd\n",mask);
- for (i=0; i<num_macros; i++) {
- if (macro[i].mask & mask) {
- if (flags & O_DEBUG)
- printf("undefine: %s %hd (%d/%d)\n",macro[i].name,macro[i].mask,i,num_macros);
- undef_macro(i);
- i--;
- }
- }
- }
-
- void
- undef_name(name)
- char *name;
- {
- short i;
-
- /* Remove name from macro table */
- for (i=0; i<num_macros && !match(name,macro[i].name); i++);
- if (i==num_macros)
- printf("Can't find definition for %s\n",name);
- else
- undef_macro(i);
- }
-
- /******************************************************************************/
- /* REMOVE A SINGLE MACRO EXPANSION */
- /******************************************************************************/
- void /* RETURNS: (nothing) */
- undef_macro(i) /* ARGUMENTS: */
- unsigned short i; /* Index of macro to undefine */
- {
- xfree(macro[i].name);
- xfree(macro[i].value);
- macro[i].name = macro[num_macros-1].name;
- macro[i].mask = macro[num_macros-1].mask;
- macro[i].value = macro[num_macros-1].value;
- /* memcpy(macro[i],macro[num_macros-1],sizeof(macro_t)); */
- num_macros--;
- }
-