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

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

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "tpcextensions.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 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)
 
void tpcProgramName (const char *program, int version, int copyright, char *prname, int n)
 
int tpcYesNo (const char *s)
 

Detailed Description

Functions for printing usage and build information from executables.

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, tpcProgramName, tpcPrintBuild
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 169 of file proginfo.c.

176 {
177 unsigned int len, i, j;
178 char *bprogram, *fname, *cptr, *line;
179 FILE *fp;
180
181 if(program==NULL || text==NULL || strnlen(program, 1)<1) return 1;
182
183 /* Clean program name */
184 bprogram=strdup(program);
185 filenameRmPath(bprogram); filenameRmExtension(bprogram);
186
187 /* Make filename */
188 fname=calloc(strlen(path)+1+strlen(bprogram)+5, sizeof(char));
189 if(fname==NULL) {free(bprogram); return 1;}
190 strcpy(fname, path); len=strlen(fname);
191 if(len>0 && (fname[len-1]=='/' || fname[len-1]=='\\')) fname[len-1]=(char)0;
192 len=strlen(fname); if(len>0) strcat(fname, "/");
193 strcat(fname, bprogram); strcat(fname, ".html");
194
195 //printf("fname := '%s'\n", fname);
196
197 /* Open file for write */
198 fp=stdout;
199
200 /* Write HTML header */
201 len=fprintf(fp, "<!DOCTYPE html>\n");
202 if(len<10) {free(bprogram); free(fname); return 2;}
203 fprintf(fp, "<html lang=\"en-GB\">\n");
204 fprintf(fp, "<head>\n");
205 fprintf(fp, " <meta charset=\"utf-8\">\n");
206 fprintf(fp, " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
207 fprintf(fp, " <title>%s</title>\n", bprogram);
208 fprintf(fp, " <style type=\"text/css\">\n");
209 fprintf(fp, " body {\n");
210 fprintf(fp, " margin-left: 2em;\n");
211 fprintf(fp, " font-family: monospace;\n");
212 fprintf(fp, " font-size: 1em;\n");
213 fprintf(fp, " }\n");
214 fprintf(fp, " h1 {\n");
215 fprintf(fp, " font-size: 1.3em;\n");
216 fprintf(fp, " margin-top: 1em;\n");
217 fprintf(fp, " margin-bottom: 1em;\n");
218 fprintf(fp, " }\n");
219 fprintf(fp, " footer {\n");
220 fprintf(fp, " border:1px solid gray;\n");
221 fprintf(fp, " font-size: 0.8em;\n");
222 fprintf(fp, " }\n");
223 fprintf(fp, " footer p {margin-left: 1em;}\n");
224 fprintf(fp, " </style>\n");
225 fprintf(fp, "</head>\n\n");
226
227 /* Write HTML body */
228 fprintf(fp, "<body>\n");
229
230 /* Write program name, version and copyright as title; */
231 /* replace (c) with html code when necessary */
232 fprintf(fp, "<h1>%s - tpcclib %d.%d.%d ", bprogram, tpcclib_VERSION_MAJOR,
233 tpcclib_VERSION_MINOR, tpcclib_VERSION_PATCH);
234 line=tpcclib_COPYRIGHT; len=strlen(line);
235 for(j=0; j<len; j++) {
236 if(strncasecmp(line+j, "(C)", 3)==0) {fputs("&copy;", fp); j+=2; continue;}
237 fputc(line[j], fp);
238 }
239 fputs("</h1>\n\n", fp);
240
241 /* Print usage */
242 fprintf(fp, "<pre>\n");
243 i=0; while(text[i]!=0) {
244 line=text[i];
245 /* If line contains string 'stdoptions' then print instead of it the
246 description of standard command-line options */
247 if(strstr(line, "stdoptions")) {
248 int j=0;
249 while(tpcstdoptions[j]!=0) fprintf(fp, "%s\n", tpcstdoptions[j++]);
250 i++; continue;
251 }
252
253 /* Process "See also" line: add links to other programs */
254 if(strstr(line, "See also: ")!=NULL) {
255 /* copy until the first ':' */
256 j=0; while(line[j]!='\0') {
257 fputc(line[j], fp);
258 j++; if(line[j-1]==':') break;
259 }
260 /* the rest of line with token */
261 char *tline; unsigned int n=0;
262 tline=strdup(line+j); cptr=strtok(tline, ", :;\t\n\r");
263 while(cptr!=NULL) {
264 if(n>0) fputc(',', fp);
265 fprintf(fp, " <a href=\"./%s.html\">%s</a>", cptr, cptr);
266 cptr=strtok(NULL, ", :;\t\n\r");
267 n++;
268 }
269 fputs("\n", fp);
270 free(tline);
271 i++; continue;
272 }
273
274
275 /* Print the line one character at the time */
276 len=strlen(line); j=0;
277 while(j<len) {
278
279 /* If WWW Address follows, then add a link */
280 if(strncasecmp(line+j, "https://", 7)==0) {
281 unsigned int li;
282 cptr=line+j; len=strcspn(cptr, " ),;");
283 fputs("<a href=\"", fp);
284 for(li=0; li<len; li++) fputc(line[j+li], fp);
285 fputs("\">", fp);
286 for(li=0; li<len; li++) fputc(line[j+li], fp);
287 fputs("</a>", fp);
288 j+=len; continue;
289 }
290
291 /* If necessary, replace '@P' with program name */
292 if(strncmp(line+j, " @P ", 4)==0) {
293 fprintf(fp, " %s ", bprogram);
294 j+=4; continue;
295 }
296 /* Replace (c) or (C) with html code */
297 if(strncasecmp(line+j, "(C)", 3)==0) {
298 fputs("&copy;", fp);
299 j+=3; continue;
300 }
301
302 /* Replace <, >, and & characters with html codes */
303 if(line[j]=='<') {fputs("&lt;", fp); j++; continue;}
304 if(line[j]=='>') {fputs("&gt;", fp); j++; continue;}
305 if(line[j]=='&') {fputs("&amp;", fp); j++; continue;}
306
307 /* Just write the char normally */
308 fputc(line[j], fp); j++;
309 }
310 fprintf(fp, "\n");
311 i++; continue;
312
313 }
314 fprintf(fp, "</pre>\n");
315
316 /* Write footer */
317 fprintf(fp, "\n<footer>\n");
318 fprintf(fp, "<p>");
319 i=0; while(tpclicense4html[i]!=0) fprintf(fp, "%s<br>\n", tpclicense4html[i++]);
320 fprintf(fp, "</p>\n");
321 fprintf(fp, "</footer>\n");
322
323 /* Close HTML */
324 fprintf(fp, "</body>\n");
325 fprintf(fp, "</html>\n");
326
327
328 free(bprogram); free(fname);
329 return 0;
330}
void filenameRmPath(char *s)
Definition filename.c:20
int filenameRmExtension(char *s)
Definition filename.c:71
char * strdup(const char *s)
Definition stringext.c:185
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566

◆ tpcPrintBuild()

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

Print tpctools build information.

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

Definition at line 339 of file proginfo.c.

344 {
345 fprintf(fp, "\n");
346 if(program!=NULL) {
347 /* Print program name */
348 char *s; s=strdup(program);
350 fprintf(fp, " Program: %s\n", s);
351 free(s);
352 }
353 /* Build time */
354 fprintf(fp, " Build: %s %s\n",__DATE__,__TIME__);
355 /* tpcclib (and program) version */
356 fprintf(fp, " tpcclib version: %d.%d.%d\n", tpcclib_VERSION_MAJOR,
357 tpcclib_VERSION_MINOR, tpcclib_VERSION_PATCH);
358 /* Compiler information */
359 fprintf(fp, " Build platform: %s\n", tpcclib_BUILD_HOST_SYSTEM_NAME);
360 fprintf(fp, " Build processor: %s\n", tpcclib_BUILD_HOST_SYSTEM_PROCESSOR);
361#if defined(__STDC_VERSION__)
362 fprintf(fp, " Version of C: %ld\n", __STDC_VERSION__);
363#endif
364#if defined(__GNUC__) && defined(__VERSION__)
365 fprintf(fp, " GNU C version: %s\n", __VERSION__);
366#endif
367#if defined(__clang__) && defined(__clang_version__)
368 fprintf(fp, " Clang/LLVM version: %s\n", __clang_version__);
369#endif
370#ifdef _OPENMP
371 fprintf(fp, " OpenMP version: %d\n", _OPENMP);
372#endif
373 /* Platform information */
374#if defined(__x86_64__) || defined(__LP64__) || defined(__ppc64__) || \
375 defined(__LLP64__) || defined(__ILP64__)
376 fprintf(fp, " Architecture: 64-bit\n");
377#else
378 fprintf(fp, " Architecture: 32-bit\n");
379#endif
380 /* Check if IEC 60559 floating-point standard is officially supported;
381 that is 'always' supported, but PET data i/o functions rely on it.
382 GCC does not define __STDC_IEC_559__ or implement the associated standard pragmas. */
383#ifdef __STDC_IEC_559__
384 fprintf(fp, " IEC 60559 floating-point standard is fully supported.\n");
385#endif
386 /* Large File Support extension (LFS) */
387#if defined(_FILE_OFFSET_BITS)
388 fprintf(fp, " _FILE_OFFSET_BITS: %d\n", _FILE_OFFSET_BITS);
389#else
390 fprintf(fp, " _FILE_OFFSET_BITS not defined\n");
391#endif
392#if defined(_LARGEFILE_SOURCE)
393 fprintf(fp, " _LARGEFILE_SOURCE defined\n");
394#endif
395#if defined(_LARGEFILE64_SOURCE)
396 fprintf(fp, " _LARGEFILE64_SOURCE defined\n");
397#endif
398}

◆ 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.
    See also
    tpcHtmlUsage, tpcProgramName, tpcPrintBuild
    Author
    Vesa Oikonen
Parameters
programProgram name.
textProgram usage text.
fpFile pointer where to print; usually stdout.

Definition at line 114 of file proginfo.c.

121 {
122 int i;
123 char *cptr, *bprogram;
124
125 /* Print program name, version, and copyright */
126 if(strlen(program)>0) bprogram=strdup(program);
127 else bprogram=strdup("unknown");
128 filenameRmPath(bprogram); filenameRmExtension(bprogram);
129 fprintf(fp, "\n %s - tpcclib %d.%d.%d %s\n \n", bprogram,
130 tpcclib_VERSION_MAJOR, tpcclib_VERSION_MINOR, tpcclib_VERSION_PATCH,
131 tpcclib_COPYRIGHT);
132 /* Print usage */
133 i=0; while(text[i]!=0) {
134 /* If line contains string 'stdoptions' then print instead of it the
135 description of standard command-line options */
136 if(strstr(text[i], "stdoptions")) {
137 int j=0;
138 while(tpcstdoptions[j]!=0) fprintf(fp, " %s\n", tpcstdoptions[j++]);
139 i++; continue;
140 }
141 /* If line does not contain program name, then just print it as it is */
142 cptr=strstr(text[i], " @P ");
143 if(cptr==NULL) {fprintf(fp, " %s\n", text[i++]); continue;}
144 /* Replace '@P' with program name */
145 char *s; s=strdup(text[i]);
146 s[strlen(text[i])-strlen(cptr)]=(char)0;
147 fprintf(fp, " %s %s %s\n", s, bprogram, cptr+4);
148 free(s); i++;
149 }
150 fprintf(fp, " \n");
151 /* Print licence info */
152 i=0; while(tpclicense[i]!=0) fprintf(fp, " %s\n", tpclicense[i++]);
153 fprintf(fp, "\n");
154 free(bprogram);
155}

◆ 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
tpcYesNo, statusSet
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 47 of file proginfo.c.

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

◆ tpcProgramName()

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

Process program name and optionally version into given string, based on user-given string, usually argv[0].

See also
tpcPrintBuild, tpcPrintUsage
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.
nBuffer length of prname, max n-1 characters are written into it, plus nul char.

Definition at line 406 of file proginfo.c.

417 {
418 char *tmp;
419
420 /* Check the input */
421 if(prname==NULL || n<1) return;
422 prname[0]=(char)0;
423
424 /* Remove path and extension */
425 if(strnlen(program, 1024)>0) tmp=strndup(program, 1024);
426 else tmp=strdup("unknown");
428 /* Copy it if possible */
429 n-=strnlen(tmp, 1024); if(n>0) strlcpy(prname, tmp, n);
430 else {free(tmp); return;}
431 free(tmp);
432
433 /* Add version, if required */
434 if(version!=0) {
435 /* Create string with version number */
436 char v[256];
437 sprintf(v, " %d.%d.%d", tpcclib_VERSION_MAJOR, tpcclib_VERSION_MINOR, tpcclib_VERSION_PATCH);
438 /* Copy it if possible */
439 strlcat(prname, v, n);
440 }
441
442 /* Add copyright, if required */
443 if(copyright!=0) {
444 /* Copy it if possible */
445 strlcat(prname, " ", n);
446 strlcat(prname, tpcclib_COPYRIGHT, n);
447 }
448
449 return;
450}
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition stringext.c:592
char * strndup(const char *s, size_t n)
Definition stringext.c:205

◆ tpcYesNo()

int tpcYesNo ( const char * s)

Check whether argument string is 'y(es)' or 'n(o)', or 'on' or 'off'.

Returns
Returns 0 when argument means no, 0 when it means no, and -1 if neither.
Author
Vesa Oikonen
See also
atofVerified, atofCheck, atoiCheck
Parameters
sString to be tested.

Definition at line 459 of file proginfo.c.

462 {
463 if(s==NULL || strnlen(s, 3)<1) return(-1);
464 if(strncasecmp(s, "YES", 1)==0) return(1);
465 if(strncasecmp(s, "NO", 1)==0) return(0);
466 if(strcasecmp(s, "ON")==0) return(1);
467 if(strcasecmp(s, "OFF")==0) return(0);
468 return(-1);
469}