mandoc 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +7 -0
  3. data/COPYING +674 -0
  4. data/README.md +117 -0
  5. data/ext/mandoc/extconf.rb +59 -0
  6. data/ext/mandoc/rb_mandoc.c +548 -0
  7. data/ext/mandoc/rb_mandoc.h +22 -0
  8. data/lib/mandoc/version.rb +19 -0
  9. data/lib/mandoc.rb +26 -0
  10. data/mandoc-1.14.6/LICENSE +55 -0
  11. data/mandoc-1.14.6/arch.c +54 -0
  12. data/mandoc-1.14.6/att.c +49 -0
  13. data/mandoc-1.14.6/catman.c +260 -0
  14. data/mandoc-1.14.6/cgi.c +1279 -0
  15. data/mandoc-1.14.6/chars.c +507 -0
  16. data/mandoc-1.14.6/compat_err.c +103 -0
  17. data/mandoc-1.14.6/compat_fts.c +696 -0
  18. data/mandoc-1.14.6/compat_fts.h +106 -0
  19. data/mandoc-1.14.6/compat_getline.c +59 -0
  20. data/mandoc-1.14.6/compat_getsubopt.c +87 -0
  21. data/mandoc-1.14.6/compat_isblank.c +23 -0
  22. data/mandoc-1.14.6/compat_mkdtemp.c +50 -0
  23. data/mandoc-1.14.6/compat_mkstemps.c +63 -0
  24. data/mandoc-1.14.6/compat_ohash.c +330 -0
  25. data/mandoc-1.14.6/compat_ohash.h +72 -0
  26. data/mandoc-1.14.6/compat_progname.c +31 -0
  27. data/mandoc-1.14.6/compat_reallocarray.c +40 -0
  28. data/mandoc-1.14.6/compat_recallocarray.c +99 -0
  29. data/mandoc-1.14.6/compat_strcasestr.c +64 -0
  30. data/mandoc-1.14.6/compat_stringlist.c +135 -0
  31. data/mandoc-1.14.6/compat_stringlist.h +48 -0
  32. data/mandoc-1.14.6/compat_strlcat.c +57 -0
  33. data/mandoc-1.14.6/compat_strlcpy.c +52 -0
  34. data/mandoc-1.14.6/compat_strndup.c +42 -0
  35. data/mandoc-1.14.6/compat_strsep.c +70 -0
  36. data/mandoc-1.14.6/compat_strtonum.c +67 -0
  37. data/mandoc-1.14.6/compat_vasprintf.c +47 -0
  38. data/mandoc-1.14.6/config.h +52 -0
  39. data/mandoc-1.14.6/dba.c +508 -0
  40. data/mandoc-1.14.6/dba.h +50 -0
  41. data/mandoc-1.14.6/dba_array.c +190 -0
  42. data/mandoc-1.14.6/dba_array.h +47 -0
  43. data/mandoc-1.14.6/dba_read.c +74 -0
  44. data/mandoc-1.14.6/dba_write.c +127 -0
  45. data/mandoc-1.14.6/dba_write.h +30 -0
  46. data/mandoc-1.14.6/dbm.c +480 -0
  47. data/mandoc-1.14.6/dbm.h +68 -0
  48. data/mandoc-1.14.6/dbm_map.c +194 -0
  49. data/mandoc-1.14.6/dbm_map.h +29 -0
  50. data/mandoc-1.14.6/demandoc.c +260 -0
  51. data/mandoc-1.14.6/eqn.c +1132 -0
  52. data/mandoc-1.14.6/eqn.h +72 -0
  53. data/mandoc-1.14.6/eqn_html.c +246 -0
  54. data/mandoc-1.14.6/eqn_parse.h +48 -0
  55. data/mandoc-1.14.6/eqn_term.c +174 -0
  56. data/mandoc-1.14.6/html.c +1102 -0
  57. data/mandoc-1.14.6/html.h +142 -0
  58. data/mandoc-1.14.6/lib.c +35 -0
  59. data/mandoc-1.14.6/libman.h +42 -0
  60. data/mandoc-1.14.6/libmandoc.h +85 -0
  61. data/mandoc-1.14.6/libmdoc.h +87 -0
  62. data/mandoc-1.14.6/main.c +1375 -0
  63. data/mandoc-1.14.6/main.h +53 -0
  64. data/mandoc-1.14.6/man.c +345 -0
  65. data/mandoc-1.14.6/man.h +21 -0
  66. data/mandoc-1.14.6/man_html.c +640 -0
  67. data/mandoc-1.14.6/man_macro.c +470 -0
  68. data/mandoc-1.14.6/man_term.c +1143 -0
  69. data/mandoc-1.14.6/man_validate.c +660 -0
  70. data/mandoc-1.14.6/manconf.h +58 -0
  71. data/mandoc-1.14.6/mandoc.c +669 -0
  72. data/mandoc-1.14.6/mandoc.h +329 -0
  73. data/mandoc-1.14.6/mandoc_aux.c +118 -0
  74. data/mandoc-1.14.6/mandoc_aux.h +27 -0
  75. data/mandoc-1.14.6/mandoc_msg.c +375 -0
  76. data/mandoc-1.14.6/mandoc_ohash.c +65 -0
  77. data/mandoc-1.14.6/mandoc_ohash.h +23 -0
  78. data/mandoc-1.14.6/mandoc_parse.h +44 -0
  79. data/mandoc-1.14.6/mandoc_xr.c +123 -0
  80. data/mandoc-1.14.6/mandoc_xr.h +31 -0
  81. data/mandoc-1.14.6/mandocd.c +282 -0
  82. data/mandoc-1.14.6/mandocdb.c +2448 -0
  83. data/mandoc-1.14.6/manpath.c +363 -0
  84. data/mandoc-1.14.6/mansearch.c +851 -0
  85. data/mandoc-1.14.6/mansearch.h +118 -0
  86. data/mandoc-1.14.6/mdoc.c +433 -0
  87. data/mandoc-1.14.6/mdoc.h +158 -0
  88. data/mandoc-1.14.6/mdoc_argv.c +682 -0
  89. data/mandoc-1.14.6/mdoc_html.c +1762 -0
  90. data/mandoc-1.14.6/mdoc_macro.c +1600 -0
  91. data/mandoc-1.14.6/mdoc_man.c +1850 -0
  92. data/mandoc-1.14.6/mdoc_markdown.c +1610 -0
  93. data/mandoc-1.14.6/mdoc_state.c +256 -0
  94. data/mandoc-1.14.6/mdoc_term.c +1964 -0
  95. data/mandoc-1.14.6/mdoc_validate.c +3062 -0
  96. data/mandoc-1.14.6/msec.c +37 -0
  97. data/mandoc-1.14.6/out.c +544 -0
  98. data/mandoc-1.14.6/out.h +70 -0
  99. data/mandoc-1.14.6/preconv.c +179 -0
  100. data/mandoc-1.14.6/read.c +732 -0
  101. data/mandoc-1.14.6/roff.c +4390 -0
  102. data/mandoc-1.14.6/roff.h +561 -0
  103. data/mandoc-1.14.6/roff_html.c +119 -0
  104. data/mandoc-1.14.6/roff_int.h +94 -0
  105. data/mandoc-1.14.6/roff_term.c +266 -0
  106. data/mandoc-1.14.6/roff_validate.c +151 -0
  107. data/mandoc-1.14.6/soelim.c +182 -0
  108. data/mandoc-1.14.6/st.c +82 -0
  109. data/mandoc-1.14.6/tag.c +327 -0
  110. data/mandoc-1.14.6/tag.h +35 -0
  111. data/mandoc-1.14.6/tbl.c +183 -0
  112. data/mandoc-1.14.6/tbl.h +121 -0
  113. data/mandoc-1.14.6/tbl_data.c +323 -0
  114. data/mandoc-1.14.6/tbl_html.c +293 -0
  115. data/mandoc-1.14.6/tbl_int.h +47 -0
  116. data/mandoc-1.14.6/tbl_layout.c +376 -0
  117. data/mandoc-1.14.6/tbl_opts.c +173 -0
  118. data/mandoc-1.14.6/tbl_parse.h +30 -0
  119. data/mandoc-1.14.6/tbl_term.c +948 -0
  120. data/mandoc-1.14.6/term.c +1113 -0
  121. data/mandoc-1.14.6/term.h +158 -0
  122. data/mandoc-1.14.6/term_ascii.c +424 -0
  123. data/mandoc-1.14.6/term_ps.c +1362 -0
  124. data/mandoc-1.14.6/term_tab.c +130 -0
  125. data/mandoc-1.14.6/term_tag.c +227 -0
  126. data/mandoc-1.14.6/term_tag.h +34 -0
  127. data/mandoc-1.14.6/tree.c +536 -0
  128. metadata +170 -0
@@ -0,0 +1,190 @@
1
+ /* $Id: dba_array.c,v 1.2 2020/06/22 19:20:40 schwarze Exp $ */
2
+ /*
3
+ * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
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
+ * Allocation-based arrays for the mandoc database, for read-write access.
18
+ * The interface is defined in "dba_array.h".
19
+ */
20
+ #include "config.h"
21
+
22
+ #include <assert.h>
23
+ #include <stdint.h>
24
+ #include <stdlib.h>
25
+ #include <string.h>
26
+
27
+ #include "mandoc_aux.h"
28
+ #include "dba_write.h"
29
+ #include "dba_array.h"
30
+
31
+ struct dba_array {
32
+ void **ep; /* Array of entries. */
33
+ int32_t *em; /* Array of map positions. */
34
+ int flags;
35
+ int32_t ea; /* Entries allocated. */
36
+ int32_t eu; /* Entries used (including deleted). */
37
+ int32_t ed; /* Entries deleted. */
38
+ int32_t ec; /* Currently active entry. */
39
+ int32_t pos; /* Map position of this array. */
40
+ };
41
+
42
+
43
+ struct dba_array *
44
+ dba_array_new(int32_t ea, int flags)
45
+ {
46
+ struct dba_array *array;
47
+
48
+ assert(ea > 0);
49
+ array = mandoc_malloc(sizeof(*array));
50
+ array->ep = mandoc_reallocarray(NULL, ea, sizeof(*array->ep));
51
+ array->em = mandoc_reallocarray(NULL, ea, sizeof(*array->em));
52
+ array->ea = ea;
53
+ array->eu = 0;
54
+ array->ed = 0;
55
+ array->ec = 0;
56
+ array->flags = flags;
57
+ array->pos = 0;
58
+ return array;
59
+ }
60
+
61
+ void
62
+ dba_array_free(struct dba_array *array)
63
+ {
64
+ int32_t ie;
65
+
66
+ if (array == NULL)
67
+ return;
68
+ if (array->flags & DBA_STR)
69
+ for (ie = 0; ie < array->eu; ie++)
70
+ free(array->ep[ie]);
71
+ free(array->ep);
72
+ free(array->em);
73
+ free(array);
74
+ }
75
+
76
+ void
77
+ dba_array_set(struct dba_array *array, int32_t ie, void *entry)
78
+ {
79
+ assert(ie >= 0);
80
+ assert(ie < array->ea);
81
+ assert(ie <= array->eu);
82
+ if (ie == array->eu)
83
+ array->eu++;
84
+ if (array->flags & DBA_STR)
85
+ entry = mandoc_strdup(entry);
86
+ array->ep[ie] = entry;
87
+ array->em[ie] = 0;
88
+ }
89
+
90
+ void
91
+ dba_array_add(struct dba_array *array, void *entry)
92
+ {
93
+ if (array->eu == array->ea) {
94
+ assert(array->flags & DBA_GROW);
95
+ array->ep = mandoc_reallocarray(array->ep,
96
+ 2, sizeof(*array->ep) * array->ea);
97
+ array->em = mandoc_reallocarray(array->em,
98
+ 2, sizeof(*array->em) * array->ea);
99
+ array->ea *= 2;
100
+ }
101
+ dba_array_set(array, array->eu, entry);
102
+ }
103
+
104
+ void *
105
+ dba_array_get(struct dba_array *array, int32_t ie)
106
+ {
107
+ if (ie < 0 || ie >= array->eu || array->em[ie] == -1)
108
+ return NULL;
109
+ return array->ep[ie];
110
+ }
111
+
112
+ void
113
+ dba_array_start(struct dba_array *array)
114
+ {
115
+ array->ec = array->eu;
116
+ }
117
+
118
+ void *
119
+ dba_array_next(struct dba_array *array)
120
+ {
121
+ if (array->ec < array->eu)
122
+ array->ec++;
123
+ else
124
+ array->ec = 0;
125
+ while (array->ec < array->eu && array->em[array->ec] == -1)
126
+ array->ec++;
127
+ return array->ec < array->eu ? array->ep[array->ec] : NULL;
128
+ }
129
+
130
+ void
131
+ dba_array_del(struct dba_array *array)
132
+ {
133
+ if (array->ec < array->eu && array->em[array->ec] != -1) {
134
+ array->em[array->ec] = -1;
135
+ array->ed++;
136
+ }
137
+ }
138
+
139
+ void
140
+ dba_array_undel(struct dba_array *array)
141
+ {
142
+ memset(array->em, 0, sizeof(*array->em) * array->eu);
143
+ }
144
+
145
+ void
146
+ dba_array_setpos(struct dba_array *array, int32_t ie, int32_t pos)
147
+ {
148
+ array->em[ie] = pos;
149
+ }
150
+
151
+ int32_t
152
+ dba_array_getpos(struct dba_array *array)
153
+ {
154
+ return array->pos;
155
+ }
156
+
157
+ void
158
+ dba_array_sort(struct dba_array *array, dba_compare_func func)
159
+ {
160
+ assert(array->ed == 0);
161
+ qsort(array->ep, array->eu, sizeof(*array->ep), func);
162
+ }
163
+
164
+ int32_t
165
+ dba_array_writelen(struct dba_array *array, int32_t nmemb)
166
+ {
167
+ dba_int_write(array->eu - array->ed);
168
+ return dba_skip(nmemb, array->eu - array->ed);
169
+ }
170
+
171
+ void
172
+ dba_array_writepos(struct dba_array *array)
173
+ {
174
+ int32_t ie;
175
+
176
+ array->pos = dba_tell();
177
+ for (ie = 0; ie < array->eu; ie++)
178
+ if (array->em[ie] != -1)
179
+ dba_int_write(array->em[ie]);
180
+ }
181
+
182
+ void
183
+ dba_array_writelst(struct dba_array *array)
184
+ {
185
+ const char *str;
186
+
187
+ dba_array_FOREACH(array, str)
188
+ dba_str_write(str);
189
+ dba_char_write('\0');
190
+ }
@@ -0,0 +1,47 @@
1
+ /* $Id: dba_array.h,v 1.1 2016/07/19 21:31:55 schwarze Exp $ */
2
+ /*
3
+ * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
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
+ * Public interface for allocation-based arrays
18
+ * for the mandoc database, for read-write access.
19
+ * To be used by dba*.c and by makewhatis(8).
20
+ */
21
+
22
+ struct dba_array;
23
+
24
+ #define DBA_STR 0x01 /* Map contains strings, not pointers. */
25
+ #define DBA_GROW 0x02 /* Allow the array to grow. */
26
+
27
+ #define dba_array_FOREACH(a, e) \
28
+ dba_array_start(a); \
29
+ while (((e) = dba_array_next(a)) != NULL)
30
+
31
+ typedef int dba_compare_func(const void *, const void *);
32
+
33
+ struct dba_array *dba_array_new(int32_t, int);
34
+ void dba_array_free(struct dba_array *);
35
+ void dba_array_set(struct dba_array *, int32_t, void *);
36
+ void dba_array_add(struct dba_array *, void *);
37
+ void *dba_array_get(struct dba_array *, int32_t);
38
+ void dba_array_start(struct dba_array *);
39
+ void *dba_array_next(struct dba_array *);
40
+ void dba_array_del(struct dba_array *);
41
+ void dba_array_undel(struct dba_array *);
42
+ void dba_array_setpos(struct dba_array *, int32_t, int32_t);
43
+ int32_t dba_array_getpos(struct dba_array *);
44
+ void dba_array_sort(struct dba_array *, dba_compare_func);
45
+ int32_t dba_array_writelen(struct dba_array *, int32_t);
46
+ void dba_array_writepos(struct dba_array *);
47
+ void dba_array_writelst(struct dba_array *);
@@ -0,0 +1,74 @@
1
+ /* $Id: dba_read.c,v 1.5 2020/06/22 19:20:40 schwarze Exp $ */
2
+ /*
3
+ * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
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
+ * Function to read the mandoc database from disk into RAM,
18
+ * such that data can be added or removed.
19
+ * The interface is defined in "dba.h".
20
+ * This file is seperate from dba.c because this also uses "dbm.h".
21
+ */
22
+ #include "config.h"
23
+
24
+ #include <regex.h>
25
+ #include <stdint.h>
26
+ #include <stdlib.h>
27
+ #include <stdio.h>
28
+ #include <string.h>
29
+
30
+ #include "mandoc_aux.h"
31
+ #include "mansearch.h"
32
+ #include "dba_array.h"
33
+ #include "dba.h"
34
+ #include "dbm.h"
35
+
36
+
37
+ struct dba *
38
+ dba_read(const char *fname)
39
+ {
40
+ struct dba *dba;
41
+ struct dba_array *page;
42
+ struct dbm_page *pdata;
43
+ struct dbm_macro *mdata;
44
+ const char *cp;
45
+ int32_t im, ip, iv, npages;
46
+
47
+ if (dbm_open(fname) == -1)
48
+ return NULL;
49
+ npages = dbm_page_count();
50
+ dba = dba_new(npages < 128 ? 128 : npages);
51
+ for (ip = 0; ip < npages; ip++) {
52
+ pdata = dbm_page_get(ip);
53
+ page = dba_page_new(dba->pages, pdata->arch,
54
+ pdata->desc, pdata->file + 1, *pdata->file);
55
+ for (cp = pdata->name; *cp != '\0'; cp = strchr(cp, '\0') + 1)
56
+ dba_page_add(page, DBP_NAME, cp);
57
+ for (cp = pdata->sect; *cp != '\0'; cp = strchr(cp, '\0') + 1)
58
+ dba_page_add(page, DBP_SECT, cp);
59
+ if ((cp = pdata->arch) != NULL)
60
+ while (*(cp = strchr(cp, '\0') + 1) != '\0')
61
+ dba_page_add(page, DBP_ARCH, cp);
62
+ cp = pdata->file;
63
+ while (*(cp = strchr(cp, '\0') + 1) != '\0')
64
+ dba_page_add(page, DBP_FILE, cp);
65
+ }
66
+ for (im = 0; im < MACRO_MAX; im++) {
67
+ for (iv = 0; iv < dbm_macro_count(im); iv++) {
68
+ mdata = dbm_macro_get(im, iv);
69
+ dba_macro_new(dba, im, mdata->value, mdata->pp);
70
+ }
71
+ }
72
+ dbm_close();
73
+ return dba;
74
+ }
@@ -0,0 +1,127 @@
1
+ /* $Id: dba_write.c,v 1.3 2016/08/05 23:15:08 schwarze Exp $ */
2
+ /*
3
+ * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
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
+ * Low-level functions for serializing allocation-based data to disk.
18
+ * The interface is defined in "dba_write.h".
19
+ */
20
+ #include "config.h"
21
+
22
+ #include <assert.h>
23
+ #if HAVE_ENDIAN
24
+ #include <endian.h>
25
+ #elif HAVE_SYS_ENDIAN
26
+ #include <sys/endian.h>
27
+ #elif HAVE_NTOHL
28
+ #include <arpa/inet.h>
29
+ #endif
30
+ #if HAVE_ERR
31
+ #include <err.h>
32
+ #endif
33
+ #include <errno.h>
34
+ #include <fcntl.h>
35
+ #include <stdint.h>
36
+ #include <stdio.h>
37
+
38
+ #include "dba_write.h"
39
+
40
+ static FILE *ofp;
41
+
42
+
43
+ int
44
+ dba_open(const char *fname)
45
+ {
46
+ ofp = fopen(fname, "w");
47
+ return ofp == NULL ? -1 : 0;
48
+ }
49
+
50
+ int
51
+ dba_close(void)
52
+ {
53
+ return fclose(ofp) == EOF ? -1 : 0;
54
+ }
55
+
56
+ int32_t
57
+ dba_tell(void)
58
+ {
59
+ long pos;
60
+
61
+ if ((pos = ftell(ofp)) == -1)
62
+ err(1, "ftell");
63
+ if (pos >= INT32_MAX) {
64
+ errno = EOVERFLOW;
65
+ err(1, "ftell = %ld", pos);
66
+ }
67
+ return pos;
68
+ }
69
+
70
+ void
71
+ dba_seek(int32_t pos)
72
+ {
73
+ if (fseek(ofp, pos, SEEK_SET) == -1)
74
+ err(1, "fseek(%d)", pos);
75
+ }
76
+
77
+ int32_t
78
+ dba_align(void)
79
+ {
80
+ int32_t pos;
81
+
82
+ pos = dba_tell();
83
+ while (pos & 3) {
84
+ dba_char_write('\0');
85
+ pos++;
86
+ }
87
+ return pos;
88
+ }
89
+
90
+ int32_t
91
+ dba_skip(int32_t nmemb, int32_t sz)
92
+ {
93
+ const int32_t out[5] = {0, 0, 0, 0, 0};
94
+ int32_t i, pos;
95
+
96
+ assert(sz >= 0);
97
+ assert(nmemb > 0);
98
+ assert(nmemb <= 5);
99
+ pos = dba_tell();
100
+ for (i = 0; i < sz; i++)
101
+ if (nmemb - fwrite(&out, sizeof(out[0]), nmemb, ofp))
102
+ err(1, "fwrite");
103
+ return pos;
104
+ }
105
+
106
+ void
107
+ dba_char_write(int c)
108
+ {
109
+ if (putc(c, ofp) == EOF)
110
+ err(1, "fputc");
111
+ }
112
+
113
+ void
114
+ dba_str_write(const char *str)
115
+ {
116
+ if (fputs(str, ofp) == EOF)
117
+ err(1, "fputs");
118
+ dba_char_write('\0');
119
+ }
120
+
121
+ void
122
+ dba_int_write(int32_t i)
123
+ {
124
+ i = htobe32(i);
125
+ if (fwrite(&i, sizeof(i), 1, ofp) != 1)
126
+ err(1, "fwrite");
127
+ }
@@ -0,0 +1,30 @@
1
+ /* $Id: dba_write.h,v 1.1 2016/07/19 21:31:55 schwarze Exp $ */
2
+ /*
3
+ * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
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
+ * Internal interface to low-level functions
18
+ * for serializing allocation-based data to disk.
19
+ * For use by dba_array.c and dba.c only.
20
+ */
21
+
22
+ int dba_open(const char *);
23
+ int dba_close(void);
24
+ int32_t dba_tell(void);
25
+ void dba_seek(int32_t);
26
+ int32_t dba_align(void);
27
+ int32_t dba_skip(int32_t, int32_t);
28
+ void dba_char_write(int);
29
+ void dba_str_write(const char *);
30
+ void dba_int_write(int32_t);