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