chm2pdf fails with shell errors

43 views
Skip to first unread message

Prasad Joshi

unread,
Jul 11, 2013, 9:21:31 AM7/11/13
to chm...@googlegroups.com
Hello All,

I have a chm file which has special characters in file name i.e. open and close bracket. These characters must be escaped when used with shell command. Otherwise, shell reports errors.

For example:
prasad@pjoshi:~/sources/chm2pdf-read-only$ chm2pdf --book "(1)_1.chm"
sh: 1: Syntax error: word unexpected (expecting ")")
sh: 1: Syntax error: word unexpected (expecting ")")
sh: 1: Syntax error: "(" unexpected
Something wrong happened when launching htmldoc.
exit value:  512
Check if output exists or if it is good.
Done.

chm2pdf runs shell utilities and commands like rm, htmldoc using os.system(). I suppose, os.system() start a shell and pass the command to it. Therefore, if file has special characters the command fails.

Thanks and Regards,
Prasad

Prasad Joshi

unread,
Jul 11, 2013, 9:24:07 AM7/11/13
to chm...@googlegroups.com
Following patch should fix the problem

Index: chm2pdf
===================================================================
--- chm2pdf    (revision 28)
+++ chm2pdf    (working copy)
@@ -110,7 +110,8 @@
     takes the list of files inside the chm archive, with the correct urls of each one.
     '''
 
-    os.system('enum_chmLib '+filename+' > '+CHM2PDF_WORK_DIR+'/urlslist.txt')
+    cmd = "enum_chmLib '%s' > '%s/urlslist.txt'" % (filename, CHM2PDF_WORK_DIR)
+    os.system(cmd)
     flist=open(CHM2PDF_WORK_DIR+'/urlslist.txt','rU')
     urls_list=[]
     for line in flist.readlines()[3:]:
@@ -119,7 +120,7 @@
         urls_list.append(spline[5])
     flist.close()
     # os.remove(CHM2PDF_WORK_DIR+'/urlslist.txt')
-   
+
     return urls_list
 
 
@@ -386,7 +387,7 @@
 
         if os.path.exists(page_filename) and (options['titlefile'] == '' or not options['titlefile'] in url):
             htmlout_filename=CHM2PDF_WORK_DIR+'/temp'+'%(#)04d' %{"#":c}+'.html'
-            htmlout_filename_list+=' '+ htmlout_filename
+            htmlout_filename_list += " '%s'" % (htmlout_filename)
             htmlout_filenames.append(htmlout_filename)
    
             if options['dontextract'] == '':
@@ -589,10 +590,12 @@
             elif key=='version': htmldoc_opts += ' ' + value
             elif key=='webpage': htmldoc_opts += ' ' + value
           
+    cmd = "htmldoc %s %s -f '%s' > /dev/null" % (htmldoc_opts, htmlout_filename_list, outputfilename)
     if options['verbose']=='--verbose' and options['verbositylevel']=='high':
-        print 'htmldoc' + htmldoc_opts + ' ' + htmlout_filename_list + " -f "+ outputfilename + " > /dev/null"
-    exit_value=os.system ('htmldoc' + htmldoc_opts + ' ' + htmlout_filename_list + " -f "+ outputfilename + " > /dev/null")
+        print cmd
 
+    exit_value=os.system(cmd)
+
     if exit_value != 0:
         print 'Something wrong happened when launching htmldoc.'
         print 'exit value: ',exit_value
@@ -1072,7 +1075,7 @@
     else:
         usage(sys.argv[0])
         return
-
+
     CHM2PDF_WORK_DIR = CHM2PDF_TEMP_WORK_DIR + os.sep + basename
     CHM2PDF_ORIG_DIR = CHM2PDF_TEMP_ORIG_DIR + os.sep + basename
 
@@ -1088,8 +1091,8 @@
     if options['dontextract'] == '':
         if options['verbose']=='--verbose' and options['verbositylevel']=='high':
             print 'Removing any previous temporary files...'
-        os.system('rm -r '+CHM2PDF_ORIG_DIR+'/*')
-        os.system('rm -r '+CHM2PDF_WORK_DIR+'/*')
+        cmd = "rm -r '%s'/* '%s'/*" % (CHM2PDF_ORIG_DIR, CHM2PDF_WORK_DIR)
+        os.system(cmd)
    
     cfile = chm.CHMFile()
     cfile.LoadCHM(filename)
@@ -1100,9 +1103,11 @@
             print 'Will use the files in ' + CHM2PDF_ORIG_DIR + ' and ' + CHM2PDF_WORK_DIR + '.'
     else:
         if options['verbose'] == '--verbose' and options['verbositylevel'] == 'high':
-            os.system('extract_chmLib ' + filename + ' ' + CHM2PDF_ORIG_DIR)
+            cmd = "extract_chmLib '%s' '%s'" % (filename, CHM2PDF_ORIG_DIR)
+            os.system(cmd)
         else:
-            os.system('extract_chmLib ' + filename + ' ' + CHM2PDF_ORIG_DIR + '&> /dev/null')
+            cmd = "extract_chmLib '%s' '%s' &>/dev/null" % (filename, CHM2PDF_ORIG_DIR)
+            os.system(cmd)
    
     convert_to_pdf(cfile, filename, outputfilename, options)
 
Attaching the patch file.


Thanks and Regards,
Prasad
escape_file_names.patch
Reply all
Reply to author
Forward
0 new messages