TPCCLIB
Loading...
Searching...
No Matches
proginfo.c File Reference

Functions for printing usage and build information from executables. More...

#include "libtpcmisc.h"

Go to the source code of this file.

Functions

int tpcProcessStdOptions (const char *s, int *print_usage, int *print_version, int *verbose_level)
 
void tpcProgramName (const char *program, int version, int copyright, char *prname, int n)
 
void tpcPrintUsage (const char *program, char *text[], FILE *fp)
 
int tpcHtmlUsage (const char *program, char *text[], const char *path)
 
void tpcPrintBuild (const char *program, FILE *fp)
 

Detailed Description

Functions for printing usage and build information from executables.

Author
Vesa Oikonen

Definition in file proginfo.c.

Function Documentation

◆ tpcHtmlUsage()

int tpcHtmlUsage ( const char * program,
char * text[],
const char * path )

Write program usage given as argument, plus program name, tpcclib version, and default copyright text, into HTML file.

  • Any string @P, separated by space characters, is replaced by program name in the output. It can only be used once per line.
  • When line contains text 'stdoptions', in place of that the description of standard command-line options '-h, -v, -q, -s etc' are displayed.
    Returns
    Returns 0 when successful.
    Author
    Vesa Oikonen
    See also
    tpcPrintUsage
Parameters
programProgram name, may contain extension and path.
textProgram usage text.
pathPath name where to create file programname.html; path may contain trailing '/' or '\'.

Definition at line 213 of file proginfo.c.

220 {
221 unsigned int len, i, j;
222 char *bprogram, *fname, *cptr, *line;
223 FILE *fp;
224
225 if(program==NULL || text==NULL || strnlen(program, 1)<1) return 1;
226
227 /* Clean program name */
228 bprogram=strdup(program);
229 filenameRmPath(bprogram); filenameRmExtension(bprogram);
230
231 /* Make file name */
232 fname=calloc(strlen(path)+1+strlen(bprogram)+5, sizeof(char));
233 if(fname==NULL) {free(bprogram); return 1;}
234 strcpy(fname, path); len=strlen(fname);
235 if(len>0 && (fname[len-1]=='/' || fname[len-1]=='\\')) fname[len-1]=(char)0;
236 len=strlen(fname); if(len>0) strcat(fname, "/");
237 strcat(fname, bprogram); strcat(fname, ".html");
238
239 //printf("fname := '%s'\n", fname);
240
241 /* Open file for write */
242 fp=stdout;
243
244 /* Write HTML header */
245 len=fprintf(fp, "<!DOCTYPE html>\n");
246 if(len<10) {free(bprogram); free(fname); return 2;}
247 fprintf(fp, "<html lang=\"en-GB\">\n");
248 fprintf(fp, "<head>\n");
249 fprintf(fp, " <meta charset=\"utf-8\">\n");
250 fprintf(fp, " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
251 fprintf(fp, " <title>%s</title>\n", bprogram);
252 fprintf(fp, " <style type=\"text/css\">\n");
253 fprintf(fp, " body {\n");
254 fprintf(fp, " margin-left: 2em;\n");
255 fprintf(fp, " font-family: monospace;\n");
256 fprintf(fp, " font-size: 1em;\n");
257 fprintf(fp, " }\n");
258 fprintf(fp, " h1 {\n");
259 fprintf(fp, " font-size: 1.3em;\n");
260 fprintf(fp, " margin-top: 1em;\n");
261 fprintf(fp, " margin-bottom: 1em;\n");
262 fprintf(fp, " }\n");
263 fprintf(fp, " footer {\n");
264 fprintf(fp, " border:1px solid gray;\n");
265 fprintf(fp, " font-size: 0.8em;\n");
266 fprintf(fp, " }\n");
267 fprintf(fp, " footer p {margin-left: 1em;}\n");
268 fprintf(fp, " </style>\n");
269 fprintf(fp, "</head>\n\n");
270
271 /* Write HTML body */
272 fprintf(fp, "<body>\n");
273
274 /* Write program name, version and copyright as title; */
275 /* replace (c) with html code when necessary */
276 fprintf(fp, "<h1>%s - tpcclib %d.%d.%d ", bprogram, tpcclib_VERSION_MAJOR,
277 tpcclib_VERSION_MINOR, tpcclib_VERSION_PATCH);
278 line=tpcclib_COPYRIGHT; len=strlen(line);
279 for(j=0; j<len; j++) {
280 if(strncasecmp(line+j, "(C)", 3)==0) {fputs("&copy;", fp); j+=2; continue;}
281 fputc(line[j], fp);
282 }
283 fputs("</h1>\n\n", fp);
284
285 /* Print usage */
286 fprintf(fp, "<pre>\n");
287 i=0; while(text[i]!=0) {
288 line=text[i];
289 /* If line contains string 'stdoptions' then print instead of it the
290 description of standard command-line options */
291 if(strstr(line, "stdoptions")) {
292 int j=0;
293 while(tpcstdoptions[j]!=0) fprintf(fp, "%s\n", tpcstdoptions[j++]);
294 i++; continue;
295 }
296
297 /* Process "See also" line: add links to other programs */
298 if(strstr(line, "See also: ")!=NULL) {
299 /* copy until the first ':' */
300 j=0; while(line[j]!='\0') {
301 fputc(line[j], fp);
302 j++; if(line[j-1]==':') break;
303 }
304 /* the rest of line with token */
305 char *tline; unsigned int n=0;
306 tline=strdup(line+j); cptr=strtok(tline, ", :;\t\n\r");
307 while(cptr!=NULL) {
308 if(n>0) fputc(',', fp);
309 fprintf(fp, " <a href=\"./%s.html\">%s</a>", cptr, cptr);
310 cptr=strtok(NULL, ", :;\t\n\r");
311 n++;
312 }
313 fputs("\n", fp);
314 free(tline);
315 i++; continue;
316 }
317
318
319 /* Print the line one character at the time */
320 len=strlen(line); j=0;
321 while(j<len) {
322
323 /* If WWW Address follows, then add a link */
324 if(strncasecmp(line+j, "https://", 7)==0) {
325 unsigned int li;
326 cptr=line+j; len=strcspn(cptr, " ),;");
327 fputs("<a href=\"", fp);
328 for(li=0; li<len; li++) fputc(line[j+li], fp);
329 fputs("\">", fp);
330 for(li=0; li<len; li++) fputc(line[j+li], fp);
331 fputs("</a>", fp);
332 j+=len; continue;
333 }
334
335 /* If necessary, replace '@P' with program name */
336 if(strncmp(line+j, " @P ", 4)==0) {
337 fprintf(fp, " %s ", bprogram);
338 j+=4; continue;
339 }
340 /* Replace (c) or (C) with html code */
341 if(strncasecmp(line+j, "(C)", 3)==0) {
342 fputs("&copy;", fp);
343 j+=3; continue;
344 }
345
346 /* Replace <, >, and & characters with html codes */
347 if(line[j]=='<') {fputs("&lt;", fp); j++; continue;}
348 if(line[j]=='>') {fputs("&gt;", fp); j++; continue;}
349 if(line[j]=='&') {fputs("&amp;", fp); j++; continue;}
350
351 /* Just write the char normally */
352 fputc(line[j], fp); j++;
353 }
354 fprintf(fp, "\n");
355 i++; continue;
356
357 }
358 fprintf(fp, "</pre>\n");
359
360 /* Write footer */
361 fprintf(fp, "\n<footer>\n");
362 fprintf(fp, "<p>");
363 i=0; while(tpclicense4html[i]!=0) fprintf(fp, "%s<br>\n", tpclicense4html[i++]);
364 fprintf(fp, "</p>\n");
365 fprintf(fp, "</footer>\n");
366
367 /* Close HTML */
368 fprintf(fp, "</body>\n");
369 fprintf(fp, "</html>\n");
370
371
372 free(bprogram); free(fname);
373 return 0;
374}
void filenameRmPath(char *s)
Definition filename.c:13
int filenameRmExtension(char *s)
Definition filename.c:34
size_t strnlen(const char *s, size_t n)
Definition strext.c:181

◆ tpcPrintBuild()

void tpcPrintBuild ( const char * program,
FILE * fp )

Print tpctools build information.

Author
Vesa Oikonen
See also
tpcProgramName, tpcPrintUsage
Parameters
programProgram name; enter NULL, if not to be printed.
fpFile pointer where to print; usually stdout.

Definition at line 383 of file proginfo.c.

388 {
389 fprintf(fp, "\n");
390 if(program!=NULL) {
391 /* Print program name */
392 char *s; s=strdup(program);
394 fprintf(fp, " Program: %s\n", s);
395 free(s);
396 }
397 /* Build time */
398 fprintf(fp, " Build: %s %s\n",__DATE__,__TIME__);
399 /* tpcclib (and program) version */
400 fprintf(fp, " tpcclib version: %d.%d.%d\n", tpcclib_VERSION_MAJOR,
401 tpcclib_VERSION_MINOR, tpcclib_VERSION_PATCH);
402 /* Compiler information */
403 fprintf(fp, " Build platform: %s\n", tpcclib_BUILD_HOST_SYSTEM_NAME);
404 fprintf(fp, " Build processor: %s\n", tpcclib_BUILD_HOST_SYSTEM_PROCESSOR);
405#if defined(__STDC_VERSION__)
406 fprintf(fp, " Version of C: %ld\n", __STDC_VERSION__);
407#endif
408#if defined(__GNUC__) && defined(__VERSION__)
409 fprintf(fp, " GNU C version: %s\n", __VERSION__);
410#endif
411#if defined(__clang__) && defined(__clang_version__)
412 fprintf(fp, " Clang/LLVM version: %s\n", __clang_version__);
413#endif
414#ifdef _OPENMP
415 fprintf(fp, " OpenMP version: %d\n", _OPENMP);
416#endif
417 /* Platform information */
418#if defined(__x86_64__) || defined(__LP64__) || defined(__ppc64__) || \
419 defined(__LLP64__) || defined(__ILP64__)
420 fprintf(fp, " Architecture: 64-bit\n");
421#else
422 fprintf(fp, " Architecture: 32-bit\n");
423#endif
424 /* Check if IEC 60559 floating-point standard is officially supported;
425 that is 'always' supported, but PET data i/o functions rely on it.
426 GCC does not define __STDC_IEC_559__ or implement the associated standard pragmas. */
427#ifdef __STDC_IEC_559__
428 fprintf(fp, " IEC 60559 floating-point standard is fully supported.\n");
429#endif
430 /* Large File Support extension (LFS) */
431#if defined(_FILE_OFFSET_BITS)
432 fprintf(fp, " _FILE_OFFSET_BITS: %d\n", _FILE_OFFSET_BITS);
433#else
434 fprintf(fp, " _FILE_OFFSET_BITS not defined\n");
435#endif
436#if defined(_LARGEFILE_SOURCE)
437 fprintf(fp, " _LARGEFILE_SOURCE defined\n");
438#endif
439#if defined(_LARGEFILE64_SOURCE)
440 fprintf(fp, " _LARGEFILE64_SOURCE defined\n");
441#endif
442}

◆ tpcPrintUsage()

void tpcPrintUsage ( const char * program,
char * text[],
FILE * fp )

Print program usage given as argument, plus program name, tpcclib version, and default copyright text.

  • Any string @P, separated by space characters, is replaced by program name in the output. It can only be used once per line.
  • When line contains text 'stdoptions', in place of that the description of standard command-line options '-h, -v, -q, -s etc' are displayed.
    Author
    Vesa Oikonen
    See also
    tpcHtmlUsage, tpcPrintBuild, tpcProcessStdOptions
Parameters
programProgram name
textProgram usage text
fpFile pointer where to print; usually stdout

Definition at line 158 of file proginfo.c.

165 {
166 int i;
167 char *cptr, *bprogram;
168
169 /* Print program name, version, and copyright */
170 if(strlen(program)>0) bprogram=strdup(program);
171 else bprogram=strdup("unknown");
172 filenameRmPath(bprogram); filenameRmExtension(bprogram);
173 fprintf(fp, "\n %s - tpcclib %d.%d.%d %s\n \n", bprogram,
174 tpcclib_VERSION_MAJOR, tpcclib_VERSION_MINOR, tpcclib_VERSION_PATCH,
175 tpcclib_COPYRIGHT);
176 /* Print usage */
177 i=0; while(text[i]!=0) {
178 /* If line contains string 'stdoptions' then print instead of it the
179 description of standard command-line options */
180 if(strstr(text[i], "stdoptions")) {
181 int j=0;
182 while(tpcstdoptions[j]!=0) fprintf(fp, " %s\n", tpcstdoptions[j++]);
183 i++; continue;
184 }
185 /* If line does not contain program name, then just print it as it is */
186 cptr=strstr(text[i], " @P ");
187 if(cptr==NULL) {fprintf(fp, " %s\n", text[i++]); continue;}
188 /* Replace '@P' with program name */
189 char *s; s=strdup(text[i]);
190 s[strlen(text[i])-strlen(cptr)]=(char)0;
191 fprintf(fp, " %s %s %s\n", s, bprogram, cptr+4);
192 free(s); i++;
193 }
194 fprintf(fp, " \n");
195 /* Print licence info */
196 i=0; while(tpclicense[i]!=0) fprintf(fp, " %s\n", tpclicense[i++]);
197 fprintf(fp, "\n");
198 free(bprogram);
199}

◆ tpcProcessStdOptions()

int tpcProcessStdOptions ( const char * s,
int * print_usage,
int * print_version,
int * verbose_level )

Check if given command-line argument string is one of the standard command-line options of this project.

Returns
0 if string was identified as standard option, otherwise 1.
Author
Vesa Oikonen
See also
tpcProgramName, tpcPrintUsage
Parameters
sPointer to command-line option string.
print_usageIf option string is either -h or –help, then this variable is set to 1.
print_versionIf option string is either -v, -V, –version, or –build, then this variable is set to 1.
verbose_levelThe level of debugging messages and listings:
  • If option string is -d, –debug or –verbose, then +1 is added to this variable.
  • If option string is -d[n], –debug[=n], or –verbose[=n], then +n is added to this variable.
  • If options string is -q or –quiet, then this variable is set to 0.
  • If options string is -s or –silent, then this variable is set to -1.

Definition at line 40 of file proginfo.c.

54 {
55 int n;
56
57 char *cptr;
58 /* Check that string is option, starting with '-' or '--' */
59 if(s==NULL || strlen(s)<2 || s[0]!='-') return 1;
60 /* Set pointer to the character after the first '-' */
61 cptr=(char*)s+1;
62 /* If also the next character is '-', then try the long forms of options */
63 if(*cptr=='-') {
64 cptr++; if(strlen(cptr)<1) return 1;
65 if(strcasecmp(cptr, "help")==0) {*print_usage=1; return 0;}
66 if(strcasecmp(cptr, "helphtml")==0) {*print_usage=2; return 0;}
67 if(strcasecmp(cptr, "version")==0) {*print_version=1; return 0;}
68 if(strcasecmp(cptr, "build")==0) {*print_version=1; return 0;}
69 if(strcasecmp(cptr, "debug")==0) {*verbose_level+=1; return 0;}
70 if(strcasecmp(cptr, "verbose")==0) {*verbose_level+=1; return 0;}
71 if(strncasecmp(cptr, "debug=", 6)==0) {
72 if(!isdigit(cptr[6])) return 1;
73 n=atoi(cptr+6); *verbose_level+=n; return 0;
74 }
75 if(strncasecmp(cptr, "verbose=", 8)==0) {
76 if(!isdigit(cptr[8])) return 1;
77 n=atoi(cptr+8); *verbose_level+=n; return 0;
78 }
79 if(strcasecmp(cptr, "quiet")==0) {*verbose_level=0; return 0;}
80 if(strcasecmp(cptr, "silent")==0) {*verbose_level=-1; return 0;}
81 return 1;
82 }
83 /* So it is the short form, if anything */
84 if(strcmp(cptr, "h")==0) {*print_usage=1; return 0;}
85 if(strcasecmp(cptr, "v")==0) {*print_version=1; return 0;}
86 if(strcmp(cptr, "d")==0) {*verbose_level+=1; return 0;}
87 if(strncmp(cptr, "d", 1)==0 && strlen(cptr)>1) {
88 if(!isdigit(cptr[1])) return 1;
89 n=atoi(cptr+1); *verbose_level+=n; return 0;
90 }
91 if(strcmp(cptr, "q")==0) {*verbose_level=0; return 0;}
92 if(strcmp(cptr, "s")==0) {*verbose_level=-1; return 0;}
93 return 1;
94}

◆ tpcProgramName()

void tpcProgramName ( const char * program,
int version,
int copyright,
char * prname,
int n )

Process program name and optionally version into given string from argv[0].

See also
tpcPrintBuild, tpcPrintUsage, tpcProcessStdOptions
Parameters
programSet to argv[0]
versionAdd version (1) or do not add (0)
copyrightAdd copyright (1) or do not add (0)
prnamePointer to string where program name is written
nLength of prname string, including trailing zero

Definition at line 101 of file proginfo.c.

112 {
113 char *tmp;
114
115 /* Check the input */
116 if(prname==NULL || n<1) return;
117 prname[0]=(char)0;
118
119 /* Remove path and extension */
120 if(strlen(program)>0) tmp=strdup(program); else tmp=strdup("unknown");
122 /* Copy it if possible */
123 n-=strlen(tmp); if(n>0) strcpy(prname, tmp); else {free(tmp); return;}
124 free(tmp);
125
126 /* Add version, if required */
127 if(version!=0) {
128 /* Create string with version number */
129 char v[256];
130 sprintf(v, "%d.%d.%d", tpcclib_VERSION_MAJOR, tpcclib_VERSION_MINOR, tpcclib_VERSION_PATCH);
131 /* Copy it if possible */
132 n--; // space
133 n-=strlen(v); if(n>0) {strcat(prname, " "); strcat(prname, v);}
134 }
135
136 /* Add copyright, if required */
137 if(copyright!=0) {
138 /* Copy it if possible */
139 n--; // space
140 n-=strlen(tpcclib_COPYRIGHT);
141 if(n>0) {strcat(prname, " "); strcat(prname, tpcclib_COPYRIGHT);}
142 }
143
144 return;
145}