Mercurial > paster
comparison util.c @ 51:07b6887d3557
pasterd: split into individual pages
While here add a similar mini.css theme as in imgup.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 21 Dec 2020 18:22:44 +0100 |
parents | f455893bf0b0 |
children | fba88439ec0a |
comparison
equal
deleted
inserted
replaced
50:520f57836ff3 | 51:07b6887d3557 |
---|---|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 */ | 17 */ |
18 | 18 |
19 #include <assert.h> | 19 #include <assert.h> |
20 #include <ctype.h> | |
20 #include <errno.h> | 21 #include <errno.h> |
22 #include <limits.h> | |
21 #include <stdarg.h> | 23 #include <stdarg.h> |
22 #include <stdio.h> | 24 #include <stdio.h> |
23 #include <stdlib.h> | 25 #include <stdlib.h> |
24 #include <stdnoreturn.h> | 26 #include <stdnoreturn.h> |
25 #include <string.h> | 27 #include <string.h> |
26 #include <time.h> | 28 #include <time.h> |
27 | 29 |
30 #include "config.h" | |
28 #include "util.h" | 31 #include "util.h" |
32 #include "paste.h" | |
33 | |
34 const char *languages[] = { | |
35 "nohighlight", | |
36 "1c", | |
37 "abnf", | |
38 "accesslog", | |
39 "actionscript", | |
40 "ada", | |
41 "apache", | |
42 "applescript", | |
43 "arduino", | |
44 "armasm", | |
45 "asciidoc", | |
46 "aspectj", | |
47 "autohotkey", | |
48 "autoit", | |
49 "avrasm", | |
50 "awk", | |
51 "axapta", | |
52 "bash", | |
53 "basic", | |
54 "bnf", | |
55 "brainfuck", | |
56 "cal", | |
57 "capnproto", | |
58 "ceylon", | |
59 "clean", | |
60 "clojure", | |
61 "clojure-repl", | |
62 "cmake", | |
63 "coffeescript", | |
64 "coq", | |
65 "cos", | |
66 "cpp", | |
67 "crmsh", | |
68 "crystal", | |
69 "cs", | |
70 "csp", | |
71 "css", | |
72 "dart", | |
73 "delphi", | |
74 "diff", | |
75 "django", | |
76 "d", | |
77 "dns", | |
78 "dockerfile", | |
79 "dos", | |
80 "dsconfig", | |
81 "dts", | |
82 "dust", | |
83 "ebnf", | |
84 "elixir", | |
85 "elm", | |
86 "erb", | |
87 "erlang", | |
88 "erlang-repl", | |
89 "excel", | |
90 "fix", | |
91 "flix", | |
92 "fortran", | |
93 "fsharp", | |
94 "gams", | |
95 "gauss", | |
96 "gcode", | |
97 "gherkin", | |
98 "glsl", | |
99 "go", | |
100 "golo", | |
101 "gradle", | |
102 "groovy", | |
103 "haml", | |
104 "handlebars", | |
105 "haskell", | |
106 "haxe", | |
107 "hsp", | |
108 "htmlbars", | |
109 "http", | |
110 "hy", | |
111 "inform7", | |
112 "ini", | |
113 "irpf90", | |
114 "java", | |
115 "javascript", | |
116 "jboss-cli", | |
117 "json", | |
118 "julia", | |
119 "julia-repl", | |
120 "kotlin", | |
121 "lasso", | |
122 "ldif", | |
123 "leaf", | |
124 "less", | |
125 "lisp", | |
126 "livecodeserver", | |
127 "livescript", | |
128 "llvm", | |
129 "lsl", | |
130 "lua", | |
131 "makefile", | |
132 "markdown", | |
133 "mathematica", | |
134 "matlab", | |
135 "maxima", | |
136 "mel", | |
137 "mercury", | |
138 "mipsasm", | |
139 "mizar", | |
140 "mojolicious", | |
141 "monkey", | |
142 "moonscript", | |
143 "n1ql", | |
144 "nginx", | |
145 "nimrod", | |
146 "nix", | |
147 "nsis", | |
148 "objectivec", | |
149 "ocaml", | |
150 "openscad", | |
151 "oxygene", | |
152 "parser3", | |
153 "perl", | |
154 "pf", | |
155 "php", | |
156 "pony", | |
157 "powershell", | |
158 "processing", | |
159 "profile", | |
160 "prolog", | |
161 "protobuf", | |
162 "puppet", | |
163 "purebasic", | |
164 "python", | |
165 "q", | |
166 "qml", | |
167 "rib", | |
168 "r", | |
169 "roboconf", | |
170 "routeros", | |
171 "rsl", | |
172 "ruby", | |
173 "ruleslanguage", | |
174 "rust", | |
175 "scala", | |
176 "scheme", | |
177 "scilab", | |
178 "scss", | |
179 "shell", | |
180 "smali", | |
181 "smalltalk", | |
182 "sml", | |
183 "sqf", | |
184 "sql", | |
185 "stan", | |
186 "stata", | |
187 "step21", | |
188 "stylus", | |
189 "subunit", | |
190 "swift", | |
191 "taggerscript", | |
192 "tap", | |
193 "tcl", | |
194 "tex", | |
195 "thrift", | |
196 "tp", | |
197 "twig", | |
198 "typescript", | |
199 "vala", | |
200 "vbnet", | |
201 "vbscript-html", | |
202 "vbscript", | |
203 "verilog", | |
204 "vhdl", | |
205 "vim", | |
206 "x86asm", | |
207 "xl", | |
208 "xml", | |
209 "xquery", | |
210 "yaml", | |
211 "zephir" | |
212 }; | |
213 | |
214 const size_t languagesz = NELEM(languages); | |
29 | 215 |
30 noreturn void | 216 noreturn void |
31 die(const char *fmt, ...) | 217 die(const char *fmt, ...) |
32 { | 218 { |
33 va_list ap; | 219 va_list ap; |
73 | 259 |
74 strftime(buf, sizeof (buf), fmt, tm); | 260 strftime(buf, sizeof (buf), fmt, tm); |
75 | 261 |
76 return buf; | 262 return buf; |
77 } | 263 } |
264 | |
265 const char * | |
266 path(const char *filename) | |
267 { | |
268 assert(filename); | |
269 | |
270 /* Build path to the template file. */ | |
271 static char path[PATH_MAX]; | |
272 | |
273 snprintf(path, sizeof (path), "%s/%s", config.themedir, filename); | |
274 | |
275 return path; | |
276 } | |
277 | |
278 void | |
279 replace(char **dst, const char *s) | |
280 { | |
281 assert(dst); | |
282 assert(s); | |
283 | |
284 /* Trim leading spaces. */ | |
285 while (*s && isspace(*s)) | |
286 s++; | |
287 | |
288 if (*s) { | |
289 free(*dst); | |
290 *dst = estrdup(s); | |
291 } | |
292 } | |
293 | |
294 const char * | |
295 ttl(time_t timestamp, long long int duration) | |
296 { | |
297 const time_t now = time(NULL); | |
298 const long long int left = duration - difftime(now, timestamp); | |
299 | |
300 if (left < PASTE_DURATION_HOUR) | |
301 return bprintf("%lld minute(s)", left / 60); | |
302 if (left < PASTE_DURATION_DAY) | |
303 return bprintf("%lld hour(s)", left / 3600); | |
304 | |
305 /* Other in days. */ | |
306 return bprintf("%lld day(s)", left / 86400); | |
307 } |