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