Annotation of src/usr.bin/mg/macro.c, Revision 1.14
1.14 ! lum 1: /* $OpenBSD: macro.c,v 1.13 2008/06/10 02:39:22 kjell Exp $ */
1.9 kjell 2:
3: /* This file is in the public domain. */
1.4 niklas 4:
1.3 millert 5: /*
6: * Keyboard macros.
7: */
1.1 deraadt 8:
9: #include "def.h"
10: #include "key.h"
11: #include "macro.h"
1.7 vincent 12:
1.13 kjell 13: int inmacro = FALSE; /* Macro playback in progess */
14: int macrodef = FALSE; /* Macro recording in progress */
1.7 vincent 15: int macrocount = 0;
16:
1.10 deraadt 17: struct line *maclhead = NULL;
18: struct line *maclcur;
1.7 vincent 19:
20: union macrodef macro[MAXMACRO];
1.1 deraadt 21:
1.2 millert 22: /* ARGSUSED */
1.3 millert 23: int
1.6 vincent 24: definemacro(int f, int n)
1.1 deraadt 25: {
1.10 deraadt 26: struct line *lp1, *lp2;
1.1 deraadt 27:
28: macrocount = 0;
1.3 millert 29:
1.2 millert 30: if (macrodef) {
31: ewprintf("already defining macro");
1.8 db 32: return (macrodef = FALSE);
1.1 deraadt 33: }
1.3 millert 34:
1.1 deraadt 35: /* free lines allocated for string arguments */
1.2 millert 36: if (maclhead != NULL) {
37: for (lp1 = maclhead->l_fp; lp1 != maclhead; lp1 = lp2) {
38: lp2 = lp1->l_fp;
1.11 kjell 39: free(lp1);
1.2 millert 40: }
1.11 kjell 41: free(lp1);
1.1 deraadt 42: }
1.3 millert 43:
1.2 millert 44: if ((maclhead = lp1 = lalloc(0)) == NULL)
1.8 db 45: return (FALSE);
1.3 millert 46:
1.1 deraadt 47: ewprintf("Defining Keyboard Macro...");
48: maclcur = lp1->l_fp = lp1->l_bp = lp1;
1.8 db 49: return (macrodef = TRUE);
1.1 deraadt 50: }
51:
1.2 millert 52: /* ARGSUSED */
1.3 millert 53: int
1.6 vincent 54: finishmacro(int f, int n)
1.1 deraadt 55: {
1.12 kjell 56: if (macrodef == TRUE) {
57: macrodef = FALSE;
58: ewprintf("End Keyboard Macro Definition");
59: return (TRUE);
60: }
61: return (FALSE);
1.1 deraadt 62: }
63:
1.2 millert 64: /* ARGSUSED */
1.3 millert 65: int
1.6 vincent 66: executemacro(int f, int n)
1.1 deraadt 67: {
1.3 millert 68: int i, j, flag, num;
69: PF funct;
1.2 millert 70:
1.5 mickey 71: if (macrodef ||
1.13 kjell 72: (macrocount >= MAXMACRO && macro[MAXMACRO - 1].m_funct
73: != finishmacro)) {
74: ewprintf("Macro too long. Aborting.");
1.8 db 75: return (FALSE);
1.13 kjell 76: }
1.3 millert 77:
1.2 millert 78: if (macrocount == 0)
1.8 db 79: return (TRUE);
1.3 millert 80:
1.2 millert 81: inmacro = TRUE;
1.3 millert 82:
1.2 millert 83: for (i = n; i > 0; i--) {
84: maclcur = maclhead->l_fp;
85: flag = 0;
86: num = 1;
87: for (j = 0; j < macrocount - 1; j++) {
88: funct = macro[j].m_funct;
89: if (funct == universal_argument) {
90: flag = FFARG;
91: num = macro[++j].m_count;
92: continue;
93: }
1.3 millert 94: if ((*funct)(flag, num) != TRUE) {
1.2 millert 95: inmacro = FALSE;
1.8 db 96: return (FALSE);
1.2 millert 97: }
98: lastflag = thisflag;
99: thisflag = 0;
100: flag = 0;
101: num = 1;
102: }
1.1 deraadt 103: }
1.2 millert 104: inmacro = FALSE;
1.8 db 105: return (TRUE);
1.1 deraadt 106: }