=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/mandoc/tbl_term.c,v retrieving revision 1.1 retrieving revision 1.2 diff -c -r1.1 -r1.2 *** src/usr.bin/mandoc/tbl_term.c 2010/10/15 19:20:03 1.1 --- src/usr.bin/mandoc/tbl_term.c 2010/10/15 21:33:47 1.2 *************** *** 1,6 **** ! /* $Id: tbl_term.c,v 1.1 2010/10/15 19:20:03 schwarze Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above --- 1,7 ---- ! /* $Id: tbl_term.c,v 1.2 2010/10/15 21:33:47 schwarze Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons + * Copyright (c) 2010 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above *************** *** 21,26 **** --- 22,29 ---- #include #include + #include "out.h" + #include "term.h" #include "tbl_extern.h" /* FIXME: `n' modifier doesn't always do the right thing. */ *************** *** 30,48 **** static void calc_data_literal(struct tbl_data *); static void calc_data_number(struct tbl_data *); static void calc_data_spanner(struct tbl_data *); ! static inline void write_char(char, int); ! static void write_data(const struct tbl_data *, int); ! static void write_data_literal(const struct tbl_data *, int); ! static void write_data_number(const struct tbl_data *, int); ! static void write_data_spanner(const struct tbl_data *, int); ! static void write_hframe(const struct tbl *); ! static void write_hrule(const struct tbl_span *); ! static void write_spanner(const struct tbl_head *); ! static void write_vframe(const struct tbl *); int ! tbl_write_term(const struct tbl *tbl) { const struct tbl_span *span; const struct tbl_data *data; --- 33,55 ---- static void calc_data_literal(struct tbl_data *); static void calc_data_number(struct tbl_data *); static void calc_data_spanner(struct tbl_data *); ! static inline void write_char(struct termp *, char, int); ! static void write_data(struct termp *, ! const struct tbl_data *, int); ! static void write_data_literal(struct termp *, ! const struct tbl_data *, int); ! static void write_data_number(struct termp *, ! const struct tbl_data *, int); ! static void write_data_spanner(struct termp *, ! const struct tbl_data *, int); ! static void write_hframe(struct termp *, const struct tbl *); ! static void write_hrule(struct termp *, const struct tbl_span *); ! static void write_spanner(struct termp *, const struct tbl_head *); ! static void write_vframe(struct termp *, const struct tbl *); int ! tbl_write_term(struct termp *p, const struct tbl *tbl) { const struct tbl_span *span; const struct tbl_data *data; *************** *** 53,61 **** * were set when tbl_calc_term was called. */ /* First, write out our head horizontal frame. */ ! write_hframe(tbl); /* * Iterate through each span, and inside, through the global --- 60,71 ---- * were set when tbl_calc_term was called. */ + term_newln(p); + p->flags |= TERMP_NOSPACE | TERMP_NONOSPACE; + /* First, write out our head horizontal frame. */ ! write_hframe(p, tbl); /* * Iterate through each span, and inside, through the global *************** *** 65,78 **** */ TAILQ_FOREACH(span, &tbl->span, entries) { ! write_vframe(tbl); /* Accomodate for the horizontal rule. */ if (TBL_DATA_DHORIZ & span->flags || TBL_DATA_HORIZ & span->flags) { ! write_hrule(span); ! write_vframe(tbl); ! printf("\n"); continue; } --- 75,88 ---- */ TAILQ_FOREACH(span, &tbl->span, entries) { ! write_vframe(p, tbl); /* Accomodate for the horizontal rule. */ if (TBL_DATA_DHORIZ & span->flags || TBL_DATA_HORIZ & span->flags) { ! write_hrule(p, span); ! write_vframe(p, tbl); ! term_flushln(p); continue; } *************** *** 82,91 **** case (TBL_HEAD_VERT): /* FALLTHROUGH */ case (TBL_HEAD_DVERT): ! write_spanner(head); break; case (TBL_HEAD_DATA): ! write_data(data, head->width); if (data) data = TAILQ_NEXT(data, entries); break; --- 92,101 ---- case (TBL_HEAD_VERT): /* FALLTHROUGH */ case (TBL_HEAD_DVERT): ! write_spanner(p, head); break; case (TBL_HEAD_DATA): ! write_data(p, data, head->width); if (data) data = TAILQ_NEXT(data, entries); break; *************** *** 94,107 **** /* NOTREACHED */ } } ! write_vframe(tbl); ! printf("\n"); } /* Last, write out our tail horizontal frame. */ ! write_hframe(tbl); return(1); } --- 104,119 ---- /* NOTREACHED */ } } ! write_vframe(p, tbl); ! term_flushln(p); } /* Last, write out our tail horizontal frame. */ ! write_hframe(p, tbl); + p->flags &= ~TERMP_NONOSPACE; + return(1); } *************** *** 147,153 **** static void ! write_hrule(const struct tbl_span *span) { const struct tbl_head *head; char c; --- 159,165 ---- static void ! write_hrule(struct termp *p, const struct tbl_span *span) { const struct tbl_head *head; char c; *************** *** 167,179 **** TAILQ_FOREACH(head, &span->tbl->head, entries) { switch (head->pos) { case (TBL_HEAD_DATA): ! write_char(c, head->width); break; case (TBL_HEAD_DVERT): ! write_char('+', head->width); /* FALLTHROUGH */ case (TBL_HEAD_VERT): ! write_char('+', head->width); break; default: abort(); --- 179,191 ---- TAILQ_FOREACH(head, &span->tbl->head, entries) { switch (head->pos) { case (TBL_HEAD_DATA): ! write_char(p, c, head->width); break; case (TBL_HEAD_DVERT): ! write_char(p, '+', head->width); /* FALLTHROUGH */ case (TBL_HEAD_VERT): ! write_char(p, '+', head->width); break; default: abort(); *************** *** 184,190 **** static void ! write_hframe(const struct tbl *tbl) { const struct tbl_head *head; --- 196,202 ---- static void ! write_hframe(struct termp *p, const struct tbl *tbl) { const struct tbl_head *head; *************** *** 200,234 **** */ if (TBL_OPT_DBOX & tbl->opts) { ! printf("+"); TAILQ_FOREACH(head, &tbl->head, entries) ! write_char('-', head->width); ! printf("+\n"); } ! printf("+"); TAILQ_FOREACH(head, &tbl->head, entries) { switch (head->pos) { case (TBL_HEAD_DATA): ! write_char('-', head->width); break; default: ! write_char('+', head->width); break; } } ! printf("+\n"); } static void ! write_vframe(const struct tbl *tbl) { /* Always just a single vertical line. */ if ( ! (TBL_OPT_BOX & tbl->opts || TBL_OPT_DBOX & tbl->opts)) return; ! printf("|"); } --- 212,248 ---- */ if (TBL_OPT_DBOX & tbl->opts) { ! term_word(p, "+"); TAILQ_FOREACH(head, &tbl->head, entries) ! write_char(p, '-', head->width); ! term_word(p, "+"); ! term_flushln(p); } ! term_word(p, "+"); TAILQ_FOREACH(head, &tbl->head, entries) { switch (head->pos) { case (TBL_HEAD_DATA): ! write_char(p, '-', head->width); break; default: ! write_char(p, '+', head->width); break; } } ! term_word(p, "+"); ! term_flushln(p); } static void ! write_vframe(struct termp *p, const struct tbl *tbl) { /* Always just a single vertical line. */ if ( ! (TBL_OPT_BOX & tbl->opts || TBL_OPT_DBOX & tbl->opts)) return; ! term_word(p, "|"); } *************** *** 348,354 **** static void ! write_data_spanner(const struct tbl_data *data, int width) { /* --- 362,368 ---- static void ! write_data_spanner(struct termp *p, const struct tbl_data *data, int width) { /* *************** *** 356,373 **** * layout) or as data. */ if (TBL_DATA_HORIZ & data->flags) ! write_char('-', width); else if (TBL_DATA_DHORIZ & data->flags) ! write_char('=', width); else if (TBL_CELL_HORIZ == data->cell->pos) ! write_char('-', width); else if (TBL_CELL_DHORIZ == data->cell->pos) ! write_char('=', width); } static void ! write_data_number(const struct tbl_data *data, int width) { char *dp, pnt; int d, padl, sz; --- 370,387 ---- * layout) or as data. */ if (TBL_DATA_HORIZ & data->flags) ! write_char(p, '-', width); else if (TBL_DATA_DHORIZ & data->flags) ! write_char(p, '=', width); else if (TBL_CELL_HORIZ == data->cell->pos) ! write_char(p, '-', width); else if (TBL_CELL_DHORIZ == data->cell->pos) ! write_char(p, '=', width); } static void ! write_data_number(struct termp *p, const struct tbl_data *data, int width) { char *dp, pnt; int d, padl, sz; *************** *** 393,406 **** padl = data->cell->head->decimal - d + 1; assert(width - sz - padl); ! write_char(' ', padl); ! (void)printf("%s", data->string); ! write_char(' ', width - sz - padl); } static void ! write_data_literal(const struct tbl_data *data, int width) { int padl, padr; --- 407,420 ---- padl = data->cell->head->decimal - d + 1; assert(width - sz - padl); ! write_char(p, ' ', padl); ! term_word(p, data->string); ! write_char(p, ' ', width - sz - padl); } static void ! write_data_literal(struct termp *p, const struct tbl_data *data, int width) { int padl, padr; *************** *** 409,449 **** switch (data->cell->pos) { case (TBL_CELL_LONG): padl = 1; ! padr = width - (int)strlen(data->string) - 1; break; case (TBL_CELL_CENTRE): ! padl = width - (int)strlen(data->string); if (padl % 2) padr++; padl /= 2; padr += padl; break; case (TBL_CELL_RIGHT): ! padl = width - (int)strlen(data->string); break; default: ! padr = width - (int)strlen(data->string); break; } ! write_char(' ', padl); ! (void)printf("%s", data->string); ! write_char(' ', padr); } static void ! write_data(const struct tbl_data *data, int width) { if (NULL == data) { ! write_char(' ', width); return; } if (TBL_DATA_HORIZ & data->flags || TBL_DATA_DHORIZ & data->flags) { ! write_data_spanner(data, width); return; } --- 423,463 ---- switch (data->cell->pos) { case (TBL_CELL_LONG): padl = 1; ! padr = width - (int)term_strlen(p, data->string) - 1; break; case (TBL_CELL_CENTRE): ! padl = width - (int)term_strlen(p, data->string); if (padl % 2) padr++; padl /= 2; padr += padl; break; case (TBL_CELL_RIGHT): ! padl = width - (int)term_strlen(p, data->string); break; default: ! padr = width - (int)term_strlen(p, data->string); break; } ! write_char(p, ' ', padl); ! term_word(p, data->string); ! write_char(p, ' ', padr); } static void ! write_data(struct termp *p, const struct tbl_data *data, int width) { if (NULL == data) { ! write_char(p, ' ', width); return; } if (TBL_DATA_HORIZ & data->flags || TBL_DATA_DHORIZ & data->flags) { ! write_data_spanner(p, data, width); return; } *************** *** 451,457 **** case (TBL_CELL_HORIZ): /* FALLTHROUGH */ case (TBL_CELL_DHORIZ): ! write_data_spanner(data, width); break; case (TBL_CELL_LONG): /* FALLTHROUGH */ --- 465,471 ---- case (TBL_CELL_HORIZ): /* FALLTHROUGH */ case (TBL_CELL_DHORIZ): ! write_data_spanner(p, data, width); break; case (TBL_CELL_LONG): /* FALLTHROUGH */ *************** *** 460,469 **** case (TBL_CELL_LEFT): /* FALLTHROUGH */ case (TBL_CELL_RIGHT): ! write_data_literal(data, width); break; case (TBL_CELL_NUMBER): ! write_data_number(data, width); break; default: abort(); --- 474,483 ---- case (TBL_CELL_LEFT): /* FALLTHROUGH */ case (TBL_CELL_RIGHT): ! write_data_literal(p, data, width); break; case (TBL_CELL_NUMBER): ! write_data_number(p, data, width); break; default: abort(); *************** *** 473,504 **** static void ! write_spanner(const struct tbl_head *head) { ! char *p; ! p = NULL; switch (head->pos) { case (TBL_HEAD_VERT): ! p = "|"; break; case (TBL_HEAD_DVERT): ! p = "||"; break; default: break; } assert(p); ! printf("%s", p); } static inline void ! write_char(char c, int len) { int i; for (i = 0; i < len; i++) ! printf("%c", c); } --- 487,520 ---- static void ! write_spanner(struct termp *p, const struct tbl_head *head) { ! char *w; ! w = NULL; switch (head->pos) { case (TBL_HEAD_VERT): ! w = "|"; break; case (TBL_HEAD_DVERT): ! w = "||"; break; default: break; } assert(p); ! term_word(p, w); } static inline void ! write_char(struct termp *p, char c, int len) { int i; + static char w[2]; + w[0] = c; for (i = 0; i < len; i++) ! term_word(p, w); }