Skip to content

Commit

Permalink
Use system wrapper callSystemDebug() in programs
Browse files Browse the repository at this point in the history
* also have callSystemDebug() return an int (success or failure)
  • Loading branch information
DanBloomberg committed Jan 24, 2024
1 parent 9606115 commit 0d44776
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 46 deletions.
2 changes: 1 addition & 1 deletion prog/alltests_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ SARRAY *sa;
#else /* windows interprets '/' as a commandline flag */
snprintf(command, sizeof(command) - 2, "%s %s", tests[i], argv[1]);
#endif /* ! _WIN32 */
ret = system(command);
ret = callSystemDebug(command);
if (ret) {
snprintf(buf, sizeof(buf), "Failed to complete %s\n", tests[i]);
if (dotest) {
Expand Down
2 changes: 1 addition & 1 deletion prog/cleanpdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ SARRAY *sa;
lept_free(tail);
lept_free(basename);
lept_stderr("%s\n", buf);
ret = system(buf); /* pdfimages or pdftoppm */
callSystemDebug(buf); /* pdfimages or pdftoppm */
}
sarrayDestroy(&sa);

Expand Down
2 changes: 1 addition & 1 deletion prog/compresspdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ SARRAY *sa;
lept_free(tail);
lept_free(basename);
lept_stderr("%s\n", buf);
ret = system(buf);
callSystemDebug(buf);
}
sarrayDestroy(&sa);

Expand Down
2 changes: 1 addition & 1 deletion prog/croppdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ SARRAY *sa;
lept_free(tail);
lept_free(basename);
lept_stderr("%s\n", buf);
ret = system(buf);
callSystemDebug(buf);
}
sarrayDestroy(&sa);

Expand Down
2 changes: 1 addition & 1 deletion prog/htmlviewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ SARRAY *safiles, *sathumbs, *saviews, *sahtml, *salink;
/* Make the output directory if it doesn't already exist */
#ifndef _WIN32
snprintf(buf, sizeof(buf), "mkdir -p %s", dirout);
ret = system(buf);
ret = callSystemDebug(buf);
#else
ret = CreateDirectory(dirout, NULL) ? 0 : 1;
#endif /* !_WIN32 */
Expand Down
35 changes: 17 additions & 18 deletions prog/livre_makefigs.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
int main(int argc,
char **argv)
{
char buf[256];
l_int32 ignore;
char buf[256];

if (argc != 1)
return ERROR_INT(" Syntax: livre_makefigs", __func__, 1);
Expand All @@ -52,53 +51,53 @@ l_int32 ignore;
lept_mkdir("lept/livre");

/* Generate Figure 1 (page segmentation) */
ignore = system("livre_seedgen");
callSystemDebug("livre_seedgen");
snprintf(buf, sizeof(buf),
"cp /tmp/lept/livre/seedgen.png /tmp/lept/livre/dia_fig1.png");
ignore = system(buf);
callSystemDebug(buf);

/* Generate Figures 2-5 (page segmentation) */
snprintf(buf, sizeof(buf), "livre_pageseg pageseg2.tif");
ignore = system(buf);
callSystemDebug(buf);
snprintf(buf, sizeof(buf),
"cp /tmp/lept/livre/segout.1.png /tmp/lept/livre/dia_fig2.png");
ignore = system(buf);
callSystemDebug(buf);
snprintf(buf, sizeof(buf),
"cp /tmp/lept/livre/segout.2.png /tmp/lept/livre/dia_fig3.png");
ignore = system(buf);
callSystemDebug(buf);
snprintf(buf, sizeof(buf),
"cp /tmp/lept/livre/segout.3.png /tmp/lept/livre/dia_fig4.png");
ignore = system(buf);
callSystemDebug(buf);
snprintf(buf, sizeof(buf),
"cp /tmp/lept/livre/segout.4.png /tmp/lept/livre/dia_fig5.png");
ignore = system(buf);
callSystemDebug(buf);

/* Generate Figure 6 (hmt sels for text orientation) */
ignore = system("livre_orient");
callSystemDebug("livre_orient");
snprintf(buf, sizeof(buf),
"cp /tmp/lept/livre/orient.png /tmp/lept/livre/dia_fig6.png");
ignore = system(buf);
callSystemDebug(buf);

/* Generate Figure 7 (hmt sel for fancy "Tribune") */
ignore = system("livre_hmt 1 8");
callSystemDebug("livre_hmt 1 8");
snprintf(buf, sizeof(buf),
"cp /tmp/lept/livre/hmt.png /tmp/lept/livre/dia_fig7.png");
ignore = system(buf);
callSystemDebug(buf);

/* Generate Figure 8 (hmt sel for fancy "T") */
ignore = system("livre_hmt 2 4");
callSystemDebug("livre_hmt 2 4");
snprintf(buf, sizeof(buf),
"cp /tmp/lept/livre/hmt.png /tmp/lept/livre/dia_fig8.png");
ignore = system(buf);
callSystemDebug(buf);

/* Generate Figure 9 (tophat background cleaning) */
ignore = system("livre_tophat");
callSystemDebug("livre_tophat");
snprintf(buf, sizeof(buf),
"cp /tmp/lept/livre/tophat.jpg /tmp/lept/livre/dia_fig9.jpg");
ignore = system(buf);
callSystemDebug(buf);

/* Run livre_adapt to generate an expanded version of Figure 9 */
ignore = system("livre_adapt");
callSystemDebug("livre_adapt");


return 0;
Expand Down
2 changes: 1 addition & 1 deletion prog/pdfio2_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ L_REGPARAMS *rp;
tempfile2 = genPathname("/tmp/lept/pdf2", "cat_pdftk.pdf");
snprintf(buffer, sizeof(buffer), "pdftk %s output %s",
tempfile1, tempfile2);
ret = system(buffer); /* pdftk */
ret = callSystemDebug(buffer); /* pdftk */
lept_free(tempfile1);
lept_free(tempfile2);
}
Expand Down
4 changes: 2 additions & 2 deletions prog/printimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ SARRAY *sa;
}
if (!extra) {
snprintf(buffer, sizeof(buffer), "lpr %s -P%s &", fname, printer);
ret = system(buffer);
ret = callSystemDebug(buffer);
} else {
snprintf(buffer, sizeof(buffer), "lpr %s -P%s %s &",
fname, printer, extra);
ret = system(buffer);
ret = callSystemDebug(buffer);
}
lept_free(extra);
}
Expand Down
4 changes: 2 additions & 2 deletions prog/printsplitimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int main(int argc,
{
char *filein, *fname, *printer;
char buf[512];
l_int32 nx, ny, i, w, h, ws, hs, n, ignore, index;
l_int32 nx, ny, i, w, h, ws, hs, n, index;
l_float32 scale;
FILE *fp;
PIX *pixs, *pixt, *pixr;
Expand Down Expand Up @@ -137,7 +137,7 @@ SARRAY *sa;
for (i = 0; i < n; i++) {
fname = sarrayGetString(sa, i, L_NOCOPY);
snprintf(buf, sizeof(buf), "lpr -P%s %s &", printer, fname);
ignore = system(buf);
callSystemDebug(buf);
}
}

Expand Down
7 changes: 3 additions & 4 deletions prog/printtiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@
int main(int argc,
char **argv)
{
l_int32 ret;
char *filein, *tempfile, *printer;
char buf[512];
char *filein, *tempfile, *printer;
char buf[512];

if (argc != 2 && argc != 3)
return ERROR_INT(" Syntax: printtiff filein [printer]", __func__, 1);
Expand All @@ -88,7 +87,7 @@ char buf[512];

if (argc == 3) {
snprintf(buf, sizeof(buf), "lpr -P%s %s &", printer, tempfile);
ret = system(buf);
callSystemDebug(buf);
}

lept_free(tempfile);
Expand Down
2 changes: 1 addition & 1 deletion prog/psioseg_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ L_REGPARAMS *rp;
psname = genPathname("/tmp/lept/regout", "psioseg.5.ps");
pdfname = genPathname("/tmp/lept/regout", "psioseg.5.pdf");
snprintf(buf, sizeof(buf), "ps2pdf %s %s", psname, pdfname);
ret = system(buf); /* ps2pdf */
ret = callSystemDebug(buf); /* ps2pdf */
lept_free(psname);
lept_free(pdfname);
if (!ret)
Expand Down
4 changes: 2 additions & 2 deletions prog/splitpdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main(int argc,
{
char *filein, *rootname;
char outname[512], buffer[1024];
l_int32 i, n, npages, nfiles, ret, val, start, end;
l_int32 i, n, npages, nfiles, val, start, end;
NUMA *naeach;

if (argc != 4)
Expand Down Expand Up @@ -97,7 +97,7 @@ NUMA *naeach;
snprintf(buffer, sizeof(buffer),
"mutool clean -g -g %s %s %d-%d",
filein, outname, start, end);
ret = system(buffer);
callSystemDebug(buffer);
start = end + 1;
}

Expand Down
5 changes: 2 additions & 3 deletions prog/tiffpdftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ int main(int argc,
char **argv)
{
char buf[256];
l_int32 ret;
l_float32 fract;
PIX *pix1, *pix2, *pix3, *pix4;
PIXA *pixa1, *pixa2, *pixa3;
Expand Down Expand Up @@ -76,7 +75,7 @@ PIXA *pixa1, *pixa2, *pixa3;
lept_mkdir("lept/tmp");
snprintf(buf, sizeof(buf),
"pdftoppm -r 300 /tmp/lept/tiffpdf/set1.pdf /tmp/lept/tmp/sevens");
ret = system(buf);
callSystemDebug(buf);

/* Re-wrap them */
pix1 = pixRead("/tmp/lept/tmp/sevens-1.ppm");
Expand All @@ -100,7 +99,7 @@ PIXA *pixa1, *pixa2, *pixa3;
lept_mkdir("lept/tmp");
snprintf(buf, sizeof(buf),
"pdftoppm -r 300 /tmp/lept/tiffpdf/set2.pdf /tmp/lept/tmp/sevens");
ret = system(buf);
callSystemDebug(buf);

/* And wrap them up again */
pix1 = pixRead("/tmp/lept/tmp/sevens-1.ppm");
Expand Down
2 changes: 1 addition & 1 deletion prog/xtractprotos.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ SARRAY *sa;
continue;
snprintf(buf, L_BUFSIZE, "cpp -ansi -DNO_PROTOS %s %s",
filein, tempfile);
ret = system(buf); /* cpp */
ret = callSystemDebug(buf); /* cpp */
if (ret) {
lept_stderr("cpp failure for %s; continuing\n", filein);
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/allheaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -2720,7 +2720,7 @@ LEPT_DLL extern l_int32 lept_rm ( const char *subdir, const char *tail );
LEPT_DLL extern l_int32 lept_rmfile ( const char *filepath );
LEPT_DLL extern l_int32 lept_mv ( const char *srcfile, const char *newdir, const char *newtail, char **pnewpath );
LEPT_DLL extern l_int32 lept_cp ( const char *srcfile, const char *newdir, const char *newtail, char **pnewpath );
LEPT_DLL extern void callSystemDebug ( const char *cmd );
LEPT_DLL extern l_int32 callSystemDebug ( const char *cmd );
LEPT_DLL extern l_ok splitPathAtDirectory ( const char *pathname, char **pdir, char **ptail );
LEPT_DLL extern l_ok splitPathAtExtension ( const char *pathname, char **pbasename, char **pextension );
LEPT_DLL extern char * pathJoin ( const char *dir, const char *fname );
Expand Down
12 changes: 7 additions & 5 deletions src/utils2.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
* l_int32 lept_cp()
*
* Special debug/test function for calling 'system'
* void callSystemDebug()
* l_int32 callSystemDebug()
*
* General file name operations
* l_int32 splitPathAtDirectory()
Expand Down Expand Up @@ -2656,7 +2656,7 @@ l_int32 ret;
* \brief callSystemDebug()
*
* \param[in] cmd command to be exec'd
* \return void
* \return 0 on success
*
* <pre>
* Notes:
Expand All @@ -2667,18 +2667,18 @@ l_int32 ret;
* generate an error message.
* </pre>
*/
void
l_int32
callSystemDebug(const char *cmd)
{
l_int32 ret;

if (!cmd) {
L_ERROR("cmd not defined\n", __func__);
return;
return 1;
}
if (LeptDebugOK == FALSE) {
L_INFO("'system' calls are disabled\n", __func__);
return;
return 1;
}

#if defined(__APPLE__) /* iOS 11 does not support system() */
Expand All @@ -2694,6 +2694,8 @@ l_int32 ret;
ret = system(cmd);

#endif /* __APPLE__ */

return ret;
}


Expand Down
8 changes: 7 additions & 1 deletion version-notes.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ <h2 align=center> <IMG SRC="moller52.jpg" border=1 ALIGN_MIDDLE> </h2>
<pre>

1.85 not released
* Source files changed: gplot.c, partify.c, pdfio2.c, psio2.c
Use wrapper callSystemDebug() instead of system() in programs.
* Source files changed: gplot.c, partify.c, pdfio2.c, psio2.c,
utils2.c
* Prog files changed: libre_makefigs.c, cleanpdf.c croppdf.c,
compresspdf.c, alltests_reg.c, htmlviewer.c, pdfio2_reg.c,
printimage.c, printsplitimage.c, printtiff.c, psioseg_reg.c,
splitpdf.c, tiffpdftest.c, xtractprotos.c

1.84.1 Jan 3, 2024
* Remove support for openjpeg versions < 2.1.
Expand Down

0 comments on commit 0d44776

Please sign in to comment.