Annotation of OpenXM_contrib2/asir2000/fep/fep_alias.c, Revision 1.1
1.1 ! noro 1: /* Copyright (c) 1987, 1988 by Software Research Associates, Inc. */
! 2:
! 3: #ifndef lint
! 4: static char rcsid[]=
! 5: "$Header: fep_alias.c,v 4.2 88/11/13 15:50:52 utashiro Exp $ (SRA)";
! 6: #endif /* lint */
! 7:
! 8: #include <stdio.h>
! 9: #include <ctype.h>
! 10: #include "fep_defs.h"
! 11: #include "fep_glob.h"
! 12:
! 13: typedef struct _alias {
! 14: char *al_name;
! 15: char *al_value;
! 16: struct _alias *al_next;
! 17: } ALIAS;
! 18:
! 19: ALIAS alias_top = {"top", "top", (ALIAS *)0};
! 20: ALIAS *alias_list = &alias_top;
! 21: CHAR aliased_line[MAXCMDLEN+1];
! 22:
! 23: /*
! 24: * Functions
! 25: */
! 26: int set_alias (/* char *name, char *value */);
! 27: char *look_alias (/* char *name */);
! 28: ALIAS *getap (/* char *name */);
! 29:
! 30: extern char *allocAndCopyThere();
! 31: extern char *prompt;
! 32:
! 33: /*
! 34: * Check alias list, and if found alias change command by its value
! 35: */
! 36: CHAR *
! 37: check_alias (comline)
! 38: char *comline;
! 39: {
! 40: char *argv[MAXARGS];
! 41: int argc;
! 42: char *av;
! 43: int substitute = 0;
! 44:
! 45: while (isspace (*comline))
! 46: ++comline;
! 47: if (*comline == '\0')
! 48: return ((CHAR *)0);
! 49:
! 50: argc = mkargv (comline, argv, MAXARGS);
! 51:
! 52: if (av = look_alias (argv[0])) {
! 53: int len = strlen (argv[0]);
! 54: char *cp;
! 55: CHAR *ap;
! 56:
! 57: *aliased_line = '\0';
! 58: ap = aliased_line;
! 59: cp = av;
! 60: for (; *cp; ++cp) {
! 61:
! 62: if (*cp != '!')
! 63: *ap++ = *cp;
! 64: else {
! 65: switch (*(cp + 1)) {
! 66: case '^':
! 67: if (argc > 1) {
! 68: strcpy (ap, argv[1]);
! 69: ap += strlen (argv[1]);
! 70: }
! 71: substitute++;
! 72: break;
! 73:
! 74: case '$':
! 75: if (argc > 1) {
! 76: strcpy (ap, argv[argc-1]);
! 77: ap += strlen (argv[argc - 1]);
! 78: }
! 79: substitute++;
! 80: break;
! 81:
! 82: case '*':
! 83: {
! 84: int j;
! 85:
! 86: for (j = 1; j < argc; j++) {
! 87: strcpy (ap, " ");
! 88: strcat (ap, argv[j]);
! 89: ap += strlen (argv[j]) + 1;
! 90: }
! 91: substitute++;
! 92: break;
! 93: }
! 94:
! 95: default:
! 96: *ap = *cp;
! 97: *++ap = *(cp + 1);
! 98: break;
! 99: }
! 100: cp++;
! 101: }
! 102: }
! 103: *ap = '\0';
! 104:
! 105: /*
! 106: * If there is no ! notation, copy all arguments in command line.
! 107: */
! 108: if (substitute == 0)
! 109: strcat (aliased_line, comline + len);
! 110:
! 111: return (aliased_line);
! 112: }
! 113: else
! 114: return ((CHAR *)0);
! 115: }
! 116:
! 117: /*
! 118: * Set alias
! 119: */
! 120: set_alias (name, value)
! 121: char *name, *value;
! 122: {
! 123: ALIAS *vp;
! 124:
! 125: vp = getap (name, 1, 0);
! 126:
! 127: if (vp->al_value)
! 128: free (vp->al_value);
! 129:
! 130: vp->al_value = allocAndCopyThere (value);
! 131: }
! 132:
! 133: /*
! 134: * Unset alias
! 135: */
! 136: unset_alias (name)
! 137: char *name;
! 138: {
! 139: ALIAS *vp, *prev;
! 140:
! 141: vp = getap (name, 0, &prev);
! 142:
! 143: if (!vp)
! 144: return;
! 145:
! 146: prev->al_next = vp->al_next;
! 147: free (vp->al_name);
! 148: free (vp->al_value);
! 149: free (vp);
! 150: return;
! 151: }
! 152:
! 153: /*
! 154: * Look up alias
! 155: */
! 156: char *
! 157: look_alias (name)
! 158: char *name;
! 159: {
! 160:
! 161: ALIAS *vp;
! 162:
! 163: vp = getap (name, 0, 0);
! 164:
! 165: if (vp && vp->al_value)
! 166: return (vp->al_value);
! 167: else
! 168: return ((char *)0);
! 169: }
! 170:
! 171: /*
! 172: * Show alias list
! 173: */
! 174: show_aliaslist (a)
! 175: char *a;
! 176: {
! 177: register ALIAS *vp;
! 178:
! 179: for (vp = alias_list->al_next; vp; vp = vp->al_next) {
! 180: if (a && strcmp (a, vp->al_name))
! 181: continue;
! 182: printf ("%-16s %s\n", vp->al_name, vp->al_value);
! 183: }
! 184: }
! 185:
! 186:
! 187: /*
! 188: * Get pointer to ALIAS.
! 189: * If there is no memoly associated to the alias and alloc argument is 1,
! 190: * allocate the area and initialize name field.
! 191: */
! 192: ALIAS *
! 193: getap (name, alloc, lastvp)
! 194: char *name;
! 195: int alloc;
! 196: ALIAS **lastvp;
! 197: {
! 198: ALIAS *vp, *last = (ALIAS *)0;
! 199:
! 200: for (vp = alias_list->al_next, last = alias_list;
! 201: vp; last = vp, vp = vp->al_next) {
! 202: int r;
! 203:
! 204: r = strcmp (name, vp->al_name);
! 205: if (r == 0) {
! 206: if (lastvp)
! 207: *lastvp = last;
! 208: return (vp);
! 209: }
! 210: else if (r < 0)
! 211: break;
! 212: }
! 213:
! 214: if (alloc == 0)
! 215: return (0);
! 216:
! 217: vp = (ALIAS *) calloc (sizeof (ALIAS), 1);
! 218: vp->al_value = (char *)0;
! 219: vp->al_next = (ALIAS *) 0;
! 220: vp->al_name = allocAndCopyThere (name);
! 221:
! 222: vp->al_next = last->al_next;
! 223: last->al_next = vp;
! 224: return (vp);
! 225: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>