]> git.cameronkatri.com Git - mandoc.git/blob - action.c
81d38ba179c37a836a2fa3b3b7c23b659bd1362a
[mandoc.git] / action.c
1 /* $Id: action.c,v 1.3 2009/01/07 15:53:00 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <assert.h>
20 #include <stdlib.h>
21
22 #include "private.h"
23
24 typedef int (*a_act)(struct mdoc *, int, int);
25
26
27 struct actions {
28 a_act action;
29 };
30
31
32 static int action_sh(struct mdoc *, int, int);
33
34
35 const struct actions mdoc_actions[MDOC_MAX] = {
36 { NULL }, /* \" */
37 { NULL }, /* Dd */
38 { NULL }, /* Dt */
39 { NULL }, /* Os */
40 { action_sh }, /* Sh */
41 { NULL }, /* Ss */
42 { NULL }, /* Pp */
43 { NULL }, /* D1 */
44 { NULL }, /* Dl */
45 { NULL }, /* Bd */
46 { NULL }, /* Ed */
47 { NULL }, /* Bl */
48 { NULL }, /* El */
49 { NULL }, /* It */
50 { NULL }, /* Ad */
51 { NULL }, /* An */
52 { NULL }, /* Ar */
53 { NULL }, /* Cd */
54 { NULL }, /* Cm */
55 { NULL }, /* Dv */
56 { NULL }, /* Er */
57 { NULL }, /* Ev */
58 { NULL }, /* Ex */
59 { NULL }, /* Fa */
60 { NULL }, /* Fd */
61 { NULL }, /* Fl */
62 { NULL }, /* Fn */
63 { NULL }, /* Ft */
64 { NULL }, /* Ic */
65 { NULL }, /* In */
66 { NULL }, /* Li */
67 { NULL }, /* Nd */
68 { NULL }, /* Nm */
69 { NULL }, /* Op */
70 { NULL }, /* Ot */
71 { NULL }, /* Pa */
72 { NULL }, /* Rv */
73 { NULL }, /* St */
74 { NULL }, /* Va */
75 { NULL }, /* Vt */
76 { NULL }, /* Xr */
77 { NULL }, /* %A */
78 { NULL }, /* %B */
79 { NULL }, /* %D */
80 { NULL }, /* %I */
81 { NULL }, /* %J */
82 { NULL }, /* %N */
83 { NULL }, /* %O */
84 { NULL }, /* %P */
85 { NULL }, /* %R */
86 { NULL }, /* %T */
87 { NULL }, /* %V */
88 { NULL }, /* Ac */
89 { NULL }, /* Ao */
90 { NULL }, /* Aq */
91 { NULL }, /* At */
92 { NULL }, /* Bc */
93 { NULL }, /* Bf */
94 { NULL }, /* Bo */
95 { NULL }, /* Bq */
96 { NULL }, /* Bsx */
97 { NULL }, /* Bx */
98 { NULL }, /* Db */
99 { NULL }, /* Dc */
100 { NULL }, /* Do */
101 { NULL }, /* Dq */
102 { NULL }, /* Ec */
103 { NULL }, /* Ef */
104 { NULL }, /* Em */
105 { NULL }, /* Eo */
106 { NULL }, /* Fx */
107 { NULL }, /* Ms */
108 { NULL }, /* No */
109 { NULL }, /* Ns */
110 { NULL }, /* Nx */
111 { NULL }, /* Ox */
112 { NULL }, /* Pc */
113 { NULL }, /* Pf */
114 { NULL }, /* Po */
115 { NULL }, /* Pq */
116 { NULL }, /* Qc */
117 { NULL }, /* Ql */
118 { NULL }, /* Qo */
119 { NULL }, /* Qq */
120 { NULL }, /* Re */
121 { NULL }, /* Rs */
122 { NULL }, /* Sc */
123 { NULL }, /* So */
124 { NULL }, /* Sq */
125 { NULL }, /* Sm */
126 { NULL }, /* Sx */
127 { NULL }, /* Sy */
128 { NULL }, /* Tn */
129 { NULL }, /* Ux */
130 { NULL }, /* Xc */
131 { NULL }, /* Xo */
132 { NULL }, /* Fo */
133 { NULL }, /* Fc */
134 { NULL }, /* Oo */
135 { NULL }, /* Oc */
136 { NULL }, /* Bk */
137 { NULL }, /* Ek */
138 { NULL }, /* Bt */
139 { NULL }, /* Hf */
140 { NULL }, /* Fr */
141 { NULL }, /* Ud */
142 };
143
144
145 static int
146 action_sh(struct mdoc *mdoc, int tok, int pos)
147 {
148 enum mdoc_sec sec;
149 int i;
150 struct mdoc_node *n;
151 char *args[MDOC_LINEARG_MAX];
152
153 if (MDOC_HEAD != mdoc->last->type)
154 return(1);
155
156 n = mdoc->last->child;
157 assert(n);
158
159 for (i = 0; n && i < MDOC_LINEARG_MAX; n = n->next, i++) {
160 assert(MDOC_TEXT == n->type);
161 assert(NULL == n->child);
162 assert(n->data.text.string);
163 args[i] = n->data.text.string;
164 }
165
166 sec = mdoc_atosec((size_t)i, (const char **)args);
167 if (SEC_CUSTOM != sec)
168 mdoc->sec_lastn = sec;
169 mdoc->sec_last = sec;
170
171 return(1);
172 }
173
174
175 int
176 mdoc_action(struct mdoc *mdoc, int pos)
177 {
178 int t;
179
180 switch (mdoc->last->type) {
181 case (MDOC_BODY):
182 t = mdoc->last->data.body.tok;
183 break;
184 case (MDOC_ELEM):
185 t = mdoc->last->data.elem.tok;
186 break;
187 case (MDOC_BLOCK):
188 t = mdoc->last->data.block.tok;
189 break;
190 case (MDOC_HEAD):
191 t = mdoc->last->data.head.tok;
192 break;
193 default:
194 return(1);
195 }
196
197 if (NULL == mdoc_actions[t].action)
198 return(1);
199 /* TODO: MDOC_Nm... ? */
200 return((*mdoc_actions[t].action)(mdoc, t, pos));
201 }
202