Annotation of mandoc/noop.c, Revision 1.1
1.1 ! kristaps 1: /* $Id: mlg.c,v 1.28 2008/12/10 13:41:58 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 <err.h>
! 21: #include <stdlib.h>
! 22:
! 23: #include "private.h"
! 24:
! 25:
! 26: struct md_noop {
! 27: const struct md_args *args;
! 28: const struct md_rbuf *rbuf;
! 29: struct rofftree *tree;
! 30: };
! 31:
! 32:
! 33: static int noop_roffmsg(void *arg,
! 34: enum roffmsg, const char *,
! 35: const char *, const char *);
! 36: static int noop_roffhead(void *, const struct tm *,
! 37: const char *, const char *,
! 38: enum roffmsec, enum roffvol);
! 39: static int noop_rofftail(void *, const struct tm *,
! 40: const char *, const char *,
! 41: enum roffmsec, enum roffvol);
! 42: static int noop_roffin(void *, int,
! 43: int *, const char **);
! 44: static int noop_roffdata(void *, int,
! 45: const char *, const char *);
! 46: static int noop_roffout(void *, int);
! 47: static int noop_roffblkin(void *, int, int *,
! 48: const char **);
! 49: static int noop_roffblkout(void *, int);
! 50: static int noop_roffspecial(void *, int,
! 51: const char *, const int *,
! 52: const char **, const char **);
! 53: static int noop_roffblkheadin(void *, int,
! 54: int *, const char **);
! 55: static int noop_roffblkheadout(void *, int);
! 56: static int noop_roffblkbodyin(void *, int,
! 57: int *, const char **);
! 58: static int noop_roffblkbodyout(void *, int);
! 59:
! 60: #ifdef __linux__
! 61: extern size_t strlcat(char *, const char *, size_t);
! 62: extern size_t strlcpy(char *, const char *, size_t);
! 63: #endif
! 64:
! 65:
! 66: int
! 67: md_exit_noop(void *data, int flush)
! 68: {
! 69: struct md_noop *noop;
! 70: int c;
! 71:
! 72: noop = (struct md_noop *)data;
! 73: c = roff_free(noop->tree, flush);
! 74: free(noop);
! 75: return(c);
! 76: }
! 77:
! 78:
! 79: int
! 80: md_line_noop(void *data, char *buf)
! 81: {
! 82: struct md_noop *noop;
! 83:
! 84: noop = (struct md_noop *)data;
! 85: return(roff_engine(noop->tree, buf));
! 86: }
! 87:
! 88:
! 89: void *
! 90: md_init_noop(const struct md_args *args,
! 91: struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
! 92: {
! 93: struct roffcb cb;
! 94: struct md_noop *noop;
! 95:
! 96: if (NULL == (noop = calloc(1, sizeof(struct md_noop))))
! 97: err(1, "calloc");
! 98:
! 99: noop->args = args;
! 100: noop->rbuf = rbuf;
! 101:
! 102: cb.roffhead = noop_roffhead;
! 103: cb.rofftail = noop_rofftail;
! 104: cb.roffin = noop_roffin;
! 105: cb.roffout = noop_roffout;
! 106: cb.roffblkin = noop_roffblkin;
! 107: cb.roffblkheadin = noop_roffblkheadin;
! 108: cb.roffblkheadout = noop_roffblkheadout;
! 109: cb.roffblkbodyin = noop_roffblkbodyin;
! 110: cb.roffblkbodyout = noop_roffblkbodyout;
! 111: cb.roffblkout = noop_roffblkout;
! 112: cb.roffspecial = noop_roffspecial;
! 113: cb.roffmsg = noop_roffmsg;
! 114: cb.roffdata = noop_roffdata;
! 115:
! 116: if (NULL == (noop->tree = roff_alloc(&cb, noop))) {
! 117: free(noop);
! 118: return(NULL);
! 119: }
! 120: return(noop);
! 121: }
! 122:
! 123:
! 124: /* ARGSUSED */
! 125: static int
! 126: noop_roffhead(void *arg, const struct tm *tm, const char *os,
! 127: const char *title, enum roffmsec sec, enum roffvol vol)
! 128: {
! 129:
! 130: return(1);
! 131: }
! 132:
! 133:
! 134: /* ARGSUSED */
! 135: static int
! 136: noop_rofftail(void *arg, const struct tm *tm, const char *os,
! 137: const char *title, enum roffmsec sec, enum roffvol vol)
! 138: {
! 139:
! 140: return(1);
! 141: }
! 142:
! 143:
! 144: /* ARGSUSED */
! 145: static int
! 146: noop_roffspecial(void *arg, int tok, const char *start,
! 147: const int *argc, const char **argv, const char **more)
! 148: {
! 149:
! 150: return(1);
! 151: }
! 152:
! 153:
! 154: /* ARGSUSED */
! 155: static int
! 156: noop_roffblkin(void *arg, int tok,
! 157: int *argc, const char **argv)
! 158: {
! 159:
! 160: return(1);
! 161: }
! 162:
! 163:
! 164: /* ARGSUSED */
! 165: static int
! 166: noop_roffblkout(void *arg, int tok)
! 167: {
! 168:
! 169: return(1);
! 170: }
! 171:
! 172:
! 173: /* ARGSUSED */
! 174: static int
! 175: noop_roffblkbodyin(void *arg, int tok,
! 176: int *argc, const char **argv)
! 177: {
! 178:
! 179: return(1);
! 180: }
! 181:
! 182:
! 183: /* ARGSUSED */
! 184: static int
! 185: noop_roffblkbodyout(void *arg, int tok)
! 186: {
! 187:
! 188: return(1);
! 189: }
! 190:
! 191:
! 192: /* ARGSUSED */
! 193: static int
! 194: noop_roffblkheadin(void *arg, int tok,
! 195: int *argc, const char **argv)
! 196: {
! 197:
! 198: return(1);
! 199: }
! 200:
! 201:
! 202: /* ARGSUSED */
! 203: static int
! 204: noop_roffblkheadout(void *arg, int tok)
! 205: {
! 206:
! 207: return(1);
! 208: }
! 209:
! 210:
! 211: /* ARGSUSED */
! 212: static int
! 213: noop_roffin(void *arg, int tok, int *argc, const char **argv)
! 214: {
! 215:
! 216: return(1);
! 217: }
! 218:
! 219:
! 220: /* ARGSUSED */
! 221: static int
! 222: noop_roffout(void *arg, int tok)
! 223: {
! 224:
! 225: return(1);
! 226: }
! 227:
! 228:
! 229: /* ARGSUSED */
! 230: static int
! 231: noop_roffdata(void *arg, int tok,
! 232: const char *start, const char *buf)
! 233: {
! 234:
! 235: return(1);
! 236: }
! 237:
! 238:
! 239: static int
! 240: noop_roffmsg(void *arg, enum roffmsg lvl,
! 241: const char *buf, const char *pos, const char *msg)
! 242: {
! 243: struct md_noop *p;
! 244: char *level;
! 245: char b[256];
! 246: int i;
! 247:
! 248: p = (struct md_noop *)arg;
! 249: assert(p);
! 250:
! 251: switch (lvl) {
! 252: case (ROFF_WARN):
! 253: level = "warning";
! 254: if ( ! (MD_WARN_ALL & p->args->warnings))
! 255: return(1);
! 256: break;
! 257: case (ROFF_ERROR):
! 258: level = "error";
! 259: break;
! 260: default:
! 261: abort();
! 262: /* NOTREACHED */
! 263: }
! 264:
! 265: if (pos) {
! 266: assert(pos >= buf);
! 267: if (0 < p->args->verbosity) {
! 268: (void)snprintf(b, sizeof(b),
! 269: "%s:%zu: %s: %s\n",
! 270: p->rbuf->name, p->rbuf->line,
! 271: level, msg);
! 272: (void)strlcat(b, "Error at: ", sizeof(b));
! 273: (void)strlcat(b, p->rbuf->linebuf, sizeof(b));
! 274:
! 275: (void)strlcat(b, "\n ", sizeof(b));
! 276: for (i = 0; i < pos - buf; i++)
! 277: (void)strlcat(b, " ", sizeof(b));
! 278: (void)strlcat(b, "^", sizeof(b));
! 279:
! 280: } else
! 281: (void)snprintf(b, sizeof(b),
! 282: "%s:%zu: %s: %s (col %zu)",
! 283: p->rbuf->name, p->rbuf->line,
! 284: level, msg, pos - buf);
! 285: } else
! 286: (void)snprintf(b, sizeof(b), "%s: %s: %s",
! 287: p->rbuf->name, level, msg);
! 288:
! 289: (void)fprintf(stderr, "%s\n", b);
! 290: return(lvl == ROFF_WARN ? 1 : 0);
! 291: }
! 292:
CVSweb