TPCCLIB
Loading...
Searching...
No Matches
pdoc2htm.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <unistd.h>
13#include <string.h>
14#include <math.h>
15/*****************************************************************************/
16#include "tpcextensions.h"
17/*****************************************************************************/
18
19/*****************************************************************************/
20static char *info[] = {
21 "Convert user information text file, printed by command-line programs and",
22 "scripts, to HTML format.",
23 " ",
24 "Usage: @P [Options] txtfile [htmlfile]",
25 " ",
26 "Options:",
27 " -links",
28 " Sets HTML-links for 'See also:' list.",
29 " -GPL=<yes|no>",
30 " Link to GNU GPL is added (default) or not added.",
31 " -stdoptions", // List standard options like --help, -v, etc
32 " ",
33 "Example:",
34 " program --help > temp.txt",
35 " pdoc2htm temp.txt program.html",
36 " ",
37 "See also: addvers",
38 0};
39/*****************************************************************************/
40
41/*****************************************************************************/
42/* Turn on the globbing of the command line, since it is disabled by default in
43 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
44 In Unix&Linux wildcard command line processing is enabled by default. */
45/*
46#undef _CRT_glob
47#define _CRT_glob -1
48*/
49int _dowildcard = -1;
50/*****************************************************************************/
51
52/*****************************************************************************/
56int main(int argc, char **argv)
57{
58 int ai, help=0, version=0, verbose=1;
59 char *cptr, txtfile[FILENAME_MAX], htmfile[FILENAME_MAX];
60 int set_links=0; // 0=no, 1=yes
61 int set_gpl=1; // 0=no, 1=yes
62
63
64 /*
65 * Get arguments
66 */
67 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
68 txtfile[0]=htmfile[0]=(char)0;
69 /* Options */
70 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
71 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
72 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
73 if(strncasecmp(cptr, "GPL=", 4)==0) {
74 cptr+=4;
75 if(strncasecmp(cptr, "NO", 1)==0) {
76 set_gpl=0; continue;
77 } else if(strncasecmp(cptr, "YES", 1)==0) {
78 set_gpl=1; continue;
79 }
80 } else if(strncasecmp(cptr, "LINKS", 1)==0) {
81 set_links=1; continue;
82 }
83 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
84 return(1);
85 } else break; // tac name argument may start with '-'
86
87 TPCSTATUS status; statusInit(&status);
88 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
89 status.verbose=verbose-1;
90
91 /* Print help or version? */
92 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
93 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
94 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
95
96 /* Process other arguments, starting from the first non-option */
97 if(ai<argc) {strlcpy(txtfile, argv[ai++], FILENAME_MAX);}
98 if(ai<argc) {strlcpy(htmfile, argv[ai++], FILENAME_MAX);}
99 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
100
101 /* Is something missing or wrong? */
102 if(!txtfile[0]) {
103 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
104 return(1);
105 }
106 /* Construct the output file name, if not specified by user */
107 if(!htmfile[0]) {
108 strcpy(htmfile, txtfile);
109 if(!filenameRmExtension(htmfile)) {
110 fprintf(stderr, "Error: cannot construct output file name.\n");
111 return(1);
112 }
113 strlcat(htmfile, ".html", FILENAME_MAX);
114 }
115
116 /* In verbose mode print arguments and options */
117 if(verbose>1) {
118 printf("txtfile := %s\n", txtfile);
119 printf("htmlfile := %s\n", htmfile);
120 printf("set_links := %d\n", set_links);
121 printf("set_gpl := %d\n", set_gpl);
122 }
123
124
125 /* Open inputfile */
126 FILE *fp;
127 if(verbose>1) printf("opening %s for reading\n", txtfile);
128 if((fp=fopen(txtfile, "r")) == NULL) {
129 printf("Error: cannot open %s.\n", txtfile);
130 return(2);
131 }
132 /* Get its size */
133 size_t txtsize=asciiFileSize(fp, NULL);
134 if(txtsize<3 || txtsize>99999) {
135 printf("Error: invalid contents in %s.\n", txtfile);
136 fclose(fp); return(3);
137 }
138 /* Read file into buffer */
139 char *buf=asciiFileRead(fp, NULL, txtsize+1);
140 fclose(fp);
141 if(buf==NULL) {
142 printf("Error: cannot read %s.\n", txtfile);
143 return(4);
144 }
145
146 /* Open output file */
147 if(verbose>1) printf("opening %s for writing\n", htmfile);
148 if((fp=fopen(htmfile, "w")) == NULL) {
149 printf("Error: cannot open %s.\n", htmfile);
150 free(buf); return(11);
151 }
152
153 /* Write HTML header */
154 int len=fprintf(fp, "<!DOCTYPE html>\n");
155 if(len<10) {free(buf); fclose(fp); return(12);}
156 fprintf(fp, "<html>\n");
157 fprintf(fp, "<head>\n");
158 fprintf(fp, " <meta charset=\"UTF-8\">\n");
159 fprintf(fp, " <title>%s</title>\n", "User information");
160 fprintf(fp, " <style type=\"text/css\">\n");
161 fprintf(fp, " * {font-family: monospace;}\n");
162 fprintf(fp, " footer {\n");
163 fprintf(fp, " border:1px solid gray;\n");
164 fprintf(fp, " font-size: smaller;\n");
165 fprintf(fp, " }\n");
166 fprintf(fp, " img {border-width: 0px;}\n");
167 fprintf(fp, " </style>\n");
168 fprintf(fp, "</head>\n\n");
169
170 /* Write HTML body */
171 fprintf(fp, "<body>\n");
172
173 /* Print usage */
174 fprintf(fp, "<pre>\n");
175
176 int i=1;
177 char docline[256];
178 len=strTokenNCpy(buf, "\n\r", i, docline, 256);
179 while(len>0) {
180 /* Check if line contains (c) or (C) */
181 cptr=strcasestr(docline, "(c)");
182 if(cptr) {
183 *cptr=(char)0;
184 /* Check if there is also 'Turku PET Centre' */
185 char *cptr2=strcasestr(cptr+3, "Turku PET Centre");
186 if(cptr2) {
187 *cptr2=(char)0;
188 fprintf(fp, "%s&copy;%s", docline, cptr+3);
189 // Phrasing content is allowed inside <pre>
190 fprintf(fp, "<a href=\"https://www.turkupetcentre.fi\">Turku PET Centre</a>\n");
191 } else {
192 fprintf(fp, "%s&copy;%s\n", docline, cptr+3);
193 }
194 len=strTokenNCpy(buf, "\n\r", ++i, docline, 256);
195 continue;
196 }
197
198 /* Set links to "see also" docs;
199 phrasing content is allowed inside <pre>
200 */
201 if(set_links!=0 && strcasestr(docline, "See also: ")!=NULL) {
202 /* copy until ':' */
203 char *cptr2=docline;
204 while(*cptr2!='\0') {
205 fputc(*cptr2, fp);
206 if(*cptr2==':') {cptr2++; break;}
207 cptr2++;
208 }
209 /* the rest of line with token */
210 int j=1, len2;
211 char swname[32];
212 len2=strTokenNCpy(cptr2, " ,;\t\n\r", j, swname, 32);
213 while(len2>0) {
214 if(j>1) fputc(',', fp);
215 fprintf(fp, " <a href=\"./%s.html\">%s</a>", swname, swname);
216 len2=strTokenNCpy(cptr2, " ,;\t\n\r", ++j, swname, 32);
217 }
218 fputs("\n", fp);
219 len=strTokenNCpy(buf, "\n\r", ++i, docline, 256);
220 continue;
221 }
222
223 /* Write the line, but replace <, >, and & characters with html codes */
224 cptr=docline;
225 while(*cptr!='\0') {
226 if(*cptr=='<') fputs("&lt;", fp);
227 else if(*cptr=='>') fputs("&gt;", fp);
228 else if(*cptr=='&') fputs("&amp;", fp);
229 else fputc(*cptr, fp);
230 cptr++;
231 }
232 fputs("\n", fp);
233 len=strTokenNCpy(buf, "\n\r", ++i, docline, 256);
234 }
235 fprintf(fp, "</pre>\n");
236
237 /* Write footer */
238 fprintf(fp, "\n<footer>\n");
239
240 /* Licence info */
241 if(set_gpl) {
242 fprintf(fp, "<div>\n");
243 /* Icon with link to GNU-GPL */
244 fprintf(fp, "<a href=\"https://www.gnu.org/licenses/gpl-3.0-standalone.html\">\n");
245 fprintf(fp, "<img alt=\"GNU GPL\" ");
246 fprintf(fp, "style=\"width:88px; height:31px; float:left; margin: 5px 20px 5px 5px;\"");
247 fprintf(fp, "\n src=\"https://www.turkupetcentre.net/software/gplv3-88x31.png\"></a>\n");
248 fprintf(fp, "</div>\n");
249 }
250
251 fprintf(fp, "</footer>\n");
252
253 /* Close HTML */
254 fprintf(fp, "</body>\n");
255 fprintf(fp, "</html>\n");
256 /* ... and file */
257 fclose(fp);
258
259 free(buf);
260 return(0);
261}
262/*****************************************************************************/
263
264/*****************************************************************************/
int filenameRmExtension(char *s)
Definition filename.c:71
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
char * asciiFileRead(FILE *fp, char *data, size_t maxlen)
size_t asciiFileSize(FILE *fp, int *nonprintable)
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
int strTokenNCpy(const char *s1, const char *s2, int i, char *s3, int count)
Definition stringext.c:53
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
char * strcasestr(const char *haystack, const char *needle)
Definition stringext.c:155
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition stringext.c:592
int verbose
Verbose level, used by statusPrint() etc.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.