Annotation of mandoc/out.c, Revision 1.6
1.6 ! kristaps 1: /* $Id: out.c,v 1.5 2009/10/18 19:02:10 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2009 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 above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <sys/types.h>
18:
1.6 ! kristaps 19: #include <assert.h>
1.1 kristaps 20: #include <ctype.h>
1.3 kristaps 21: #include <stdio.h>
1.1 kristaps 22: #include <stdlib.h>
1.6 ! kristaps 23: #include <string.h>
1.1 kristaps 24:
25: #include "out.h"
26:
1.3 kristaps 27:
28: /*
29: * Convert a `scaling unit' to a consistent form, or fail. Scaling
1.5 kristaps 30: * units are documented in groff.7, mdoc.7, man.7.
1.3 kristaps 31: */
1.1 kristaps 32: int
1.5 kristaps 33: a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
1.1 kristaps 34: {
1.4 kristaps 35: char buf[BUFSIZ], hasd;
1.1 kristaps 36: int i;
1.3 kristaps 37: enum roffscale unit;
1.1 kristaps 38:
1.5 kristaps 39: if ('\0' == *src)
40: return(0);
41:
1.4 kristaps 42: i = hasd = 0;
43:
44: switch (*src) {
45: case ('+'):
46: src++;
47: break;
48: case ('-'):
49: buf[i++] = *src++;
50: break;
51: default:
52: break;
53: }
54:
1.5 kristaps 55: if ('\0' == *src)
56: return(0);
57:
1.4 kristaps 58: while (i < BUFSIZ) {
59: if ( ! isdigit((u_char)*src)) {
60: if ('.' != *src)
61: break;
62: else if (hasd)
63: break;
64: else
65: hasd = 1;
66: }
67: buf[i++] = *src++;
68: }
1.1 kristaps 69:
1.3 kristaps 70: if (BUFSIZ == i || (*src && *(src + 1)))
1.1 kristaps 71: return(0);
72:
1.4 kristaps 73: buf[i] = '\0';
1.1 kristaps 74:
1.3 kristaps 75: switch (*src) {
76: case ('c'):
77: unit = SCALE_CM;
78: break;
79: case ('i'):
80: unit = SCALE_IN;
81: break;
82: case ('P'):
83: unit = SCALE_PC;
84: break;
85: case ('p'):
86: unit = SCALE_PT;
87: break;
88: case ('f'):
89: unit = SCALE_FS;
90: break;
91: case ('v'):
92: unit = SCALE_VS;
93: break;
94: case ('m'):
95: unit = SCALE_EM;
96: break;
97: case ('\0'):
1.5 kristaps 98: if (SCALE_MAX == def)
99: return(0);
100: unit = SCALE_BU;
101: break;
1.3 kristaps 102: case ('u'):
103: unit = SCALE_BU;
104: break;
105: case ('M'):
106: unit = SCALE_MM;
107: break;
108: case ('n'):
109: unit = SCALE_EN;
110: break;
111: default:
1.1 kristaps 112: return(0);
1.3 kristaps 113: }
1.1 kristaps 114:
1.4 kristaps 115: if ((dst->scale = atof(buf)) < 0)
1.3 kristaps 116: dst->scale = 0;
117: dst->unit = unit;
1.4 kristaps 118: dst->pt = hasd;
119:
1.3 kristaps 120: return(1);
1.1 kristaps 121: }
1.6 ! kristaps 122:
! 123:
! 124: /*
! 125: * Correctly writes the time in nroff form, which differs from standard
! 126: * form in that a space isn't printed in lieu of the extra %e field for
! 127: * single-digit dates.
! 128: */
! 129: void
! 130: time2a(time_t t, char *dst, size_t sz)
! 131: {
! 132: struct tm tm;
! 133: char buf[5];
! 134: char *p;
! 135: size_t nsz;
! 136:
! 137: assert(sz > 1);
! 138: localtime_r(&t, &tm);
! 139:
! 140: p = dst;
! 141: nsz = 0;
! 142:
! 143: dst[0] = '\0';
! 144:
! 145: if (0 == (nsz = strftime(p, sz, "%B ", &tm)))
! 146: return;
! 147:
! 148: p += (int)nsz;
! 149: sz -= nsz;
! 150:
! 151: if (0 == strftime(buf, sizeof(buf), "%e, ", &tm))
! 152: return;
! 153:
! 154: nsz = strlcat(p, buf + (' ' == buf[0] ? 1 : 0), sz);
! 155:
! 156: if (nsz >= sz)
! 157: return;
! 158:
! 159: p += (int)nsz;
! 160: sz -= nsz;
! 161:
! 162: (void)strftime(p, sz, "%Y", &tm);
! 163: }
! 164:
CVSweb