]>
git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
1 /* $Id: man_macro.c,v 1.12 2009/04/02 06:51:44 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
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
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.
26 #define FL_NLINE (1 << 0)
27 #define FL_TLINE (1 << 1)
29 static int man_args(struct man
*, int,
30 int *, char *, char **);
32 static int man_flags
[MAN_MAX
] = {
59 man_macro(struct man
*man
, int tok
, int line
,
60 int ppos
, int *pos
, char *buf
)
66 if ( ! man_elem_alloc(man
, line
, ppos
, tok
))
69 man
->next
= MAN_NEXT_CHILD
;
73 w
= man_args(man
, line
, pos
, buf
, &p
);
80 if ( ! man_word_alloc(man
, line
, la
, p
))
82 man
->next
= MAN_NEXT_SIBLING
;
85 if (n
== man
->last
&& (FL_NLINE
& man_flags
[tok
])) {
86 if (MAN_NLINE
& man
->flags
)
87 return(man_verr(man
, line
, ppos
,
88 "next-line scope already open"));
89 man
->flags
|= MAN_NLINE
;
93 if (FL_TLINE
& man_flags
[tok
]) {
94 if (MAN_NLINE
& man
->flags
)
95 return(man_verr(man
, line
, ppos
,
96 "next-line scope already open"));
97 man
->flags
|= MAN_NLINE
;
102 * Note that when TH is pruned, we'll be back at the root, so
103 * make sure that we don't clobber as its sibling.
106 for ( ; man
->last
; man
->last
= man
->last
->parent
) {
109 if (man
->last
->type
== MAN_ROOT
)
111 if ( ! man_valid_post(man
))
113 if ( ! man_action_post(man
))
120 * Same here regarding whether we're back at the root.
123 if (man
->last
->type
!= MAN_ROOT
&& ! man_valid_post(man
))
125 if (man
->last
->type
!= MAN_ROOT
&& ! man_action_post(man
))
127 if (man
->last
->type
!= MAN_ROOT
)
128 man
->next
= MAN_NEXT_SIBLING
;
135 man_macroend(struct man
*m
)
138 for ( ; m
->last
&& m
->last
!= m
->first
;
139 m
->last
= m
->last
->parent
) {
140 if ( ! man_valid_post(m
))
142 if ( ! man_action_post(m
))
145 assert(m
->last
== m
->first
);
147 if ( ! man_valid_post(m
))
149 if ( ! man_action_post(m
))
158 man_args(struct man
*m
, int line
,
159 int *pos
, char *buf
, char **v
)
165 /* First parse non-quoted strings. */
167 if ('\"' != buf
[*pos
]) {
171 if (' ' == buf
[*pos
])
172 if ('\\' != buf
[*pos
- 1])
185 while (buf
[*pos
] && ' ' == buf
[*pos
])
191 if ( ! man_vwarn(m
, line
, *pos
, "trailing spaces"))
198 * If we're a quoted string (and quoted strings are allowed),
199 * then parse ahead to the next quote. If none's found, it's an
200 * error. After, parse to the next word.
205 while (buf
[*pos
] && '\"' != buf
[*pos
])
208 if (0 == buf
[*pos
]) {
209 if ( ! man_vwarn(m
, line
, *pos
, "unterminated quote"))
218 while (buf
[*pos
] && ' ' == buf
[*pos
])
224 if ( ! man_vwarn(m
, line
, *pos
, "trailing spaces"))