Debian Codegraph @

terps/alan3/act.c

raw symbols SCIP
1 /*----------------------------------------------------------------------*\
2
3 act.c
4
5 Action routines
6
7 \*----------------------------------------------------------------------*/
8
9 #include "act.h"
10
11 /* Import */
12
13 #include "AltInfo.h"
14 #include "output.h"
15 #include "msg.h"
16 #include "exe.h"
17 #include "lists.h"
18
19
20 /*----------------------------------------------------------------------*/
21 static void executeCommand(int verb, Parameter parameters[])
22 {
23 static AltInfo *altInfos = NULL; /* Need to survive lots of different exits...*/
24 int altIndex;
25
26 /* Did we leave anything behind last time... */
27 if (altInfos != NULL)
28 free(altInfos);
29
30 altInfos = findAllAlternatives(verb, parameters);
31
32 if (anyCheckFailed(altInfos, EXECUTE_CHECK_BODY_ON_FAIL))
33 return;
34
35 /* Check for anything to execute... */
36 if (!anythingToExecute(altInfos))
37 error(M_CANT0);
38
39 /* Now perform actions! First try any BEFORE or ONLY from inside out */
40 for (altIndex = lastAltInfoIndex(altInfos); altIndex >= 0; altIndex--) {
41 if (altInfos[altIndex].alt != 0) // TODO Can this ever be NULL? Why?
42 if (altInfos[altIndex].alt->qual == (Aword)Q_BEFORE
43 || altInfos[altIndex].alt->qual == (Aword)Q_ONLY) {
44 if (!executedOk(&altInfos[altIndex]))
45 abortPlayerCommand();
46 if (altInfos[altIndex].alt->qual == (Aword)Q_ONLY)
47 return;
48 }
49 }
50
51 /* Then execute any not declared as AFTER, i.e. the default, from outside in */
52 for (altIndex = 0; !altInfos[altIndex].end; altIndex++) {
53 if (altInfos[altIndex].alt != 0)
54 if (altInfos[altIndex].alt->qual != (Aword)Q_AFTER)
55 if (!executedOk(&altInfos[altIndex]))
56 abortPlayerCommand();
57 }
58
59 /* Finally, the ones declared as AFTER, from inside out */
60 for (altIndex = lastAltInfoIndex(altInfos); altIndex >= 0; altIndex--) {
61 if (altInfos[altIndex].alt != 0)
62 if (!executedOk(&altInfos[altIndex]))
63 abortPlayerCommand();
64 }
65 }
66
67
68 /*======================================================================
69
70 action()
71
72 Execute the command. Handles acting on multiple items
73 such as ALL, THEM or lists of objects.
74
75 */
76 void action(int verb, Parameter parameters[], Parameter multipleMatches[])
77 {
78 int i, multiplePosition;
79 char marker[20];
80
81 multiplePosition = findMultiplePosition(parameters);
82 if (multiplePosition != -1) {
83 jmp_buf savedReturnLabel;
84 memcpy(savedReturnLabel, returnLabel, sizeof(returnLabel));
85 sprintf(marker, "($%d)", multiplePosition+1); /* Prepare a printout with $1/2/3 */
86 for (i = 0; !isEndOfArray(&multipleMatches[i]); i++) {
87 copyParameter(&parameters[multiplePosition], &multipleMatches[i]);
88 setGlobalParameters(parameters); /* Need to do this here since the marker use them */
89 output(marker);
90 // TODO: if execution for one parameter aborts we should return here, not to top level
91 if (setjmp(returnLabel) == NO_JUMP_RETURN)
92 executeCommand(verb, parameters);
93 if (multipleMatches[i+1].instance != EOF)
94 para();
95 }
96 memcpy(returnLabel, savedReturnLabel, sizeof(returnLabel));
97 parameters[multiplePosition].instance = 0;
98 } else {
99 setGlobalParameters(parameters);
100 executeCommand(verb, parameters);
101 }
102
103 }