On 1/31/07, Thanh Han The
Hi,
I am working on a wrapper in C so that application can call pdftex via a library call. Can you please have a look at the proposed API and comment on it if you find a potential problem? I attached two files:
test0.c: a minimal test file test1.c: the same file with comments
Thanks, Thanh
the attachments seem to be discarded by the listserv, so I resend the files as inline text: test0.c: ========================== #include "runptex.h" int main() { pdftex_data_struct tmp; if (init_pdftex_data(&tmp, "/home/thanh/runptex/good-file.tex", "/home/thanh/tmp/runptex", "-fmt=pdflatex" ) != 0) pds_print_error_and_exit(&tmp); if (run_pdftex(&tmp) != 0) pds_print_error_and_exit(&tmp); printf("running pdftex succeeded, the output is %s in directory %s\n", tmp.pdf_file, tmp.working_dir); destroy_pdftex_data(&tmp); return 0; } ========================== test1.c: ========================== #include "runptex.h" int main() { pdftex_data_struct tmp; /* pdftex_data_struct is defined in runptex.h as follows: typedef struct { char *tex_file; char *pdf_file; char *log_file; char *working_dir; char *pdftex_opts; int return_code; char *return_msg; } pdftex_data_struct; */ if (init_pdftex_data(&tmp, "/home/thanh/runptex/good-file.tex", "/home/thanh/tmp/runptex", "-fmt=pdflatex") != 0) /* Prototype: int init_pdftex_data(pdftex_data_struct *pds, const char *tex_file, const char *working_dir, const char *pdftex_opts); Description: initialize pds as follows: - sets all fields of pds to NULL/0 - checks that tex_file and working_dir are: - not null - not too long (< MAX_FILENAME_LENGTH) - contains only allowed characters (defined by is_path_char()) - absolute (full) path - checks that tex filename (the last component of tex_file): - contains only allowed characters (defined by is_filename_char()) - has extension '.tex' - checks that pdftex_opts contains only allowed characters (defined by is_option_char()) - checks that tex_file can be read - checks that working_dir is a directory - store relevant strings (paths, pdf/log filenames, etc.) in pds Return: 0 if ok, otherwise pds->return_code (>0 in case of error) */ pds_print_error_and_exit(&tmp); /* Prototype: void pds_print_error_and_exit(pdftex_data_struct *pds); Description: a helper function for testing purpose; just print the error message stored in pds and exit the program */ if (run_pdftex(&tmp) != 0) /* prototype: int run_pdftex(pdftex_data_struct *pds); description: execute pdftex as follows: - construct the argument to call pdftex from pds->tex_file and pds->pdftex_opts - change working dir to pds->working_dir - try to create an empty pdf file and an empty log file to verify file permissions - copy pds->tex_file to the working directory - run system() to excecute the pdftex command constructed above - checks whether the log file has been created - checks whether the log file contains any error, ie line(s) beginning with '!' - checks whether the pdf file has been created - check for the pdf header mark and eof mark */ pds_print_error_and_exit(&tmp); printf("running pdftex succeeded, the output is %s in directory %s\n", tmp.pdf_file, tmp.working_dir); destroy_pdftex_data(&tmp); /* Prototype: void destroy_pdftex_data(pdftex_data_struct *pds); Description: free the strings stored in pds */ return 0; } ==========================