12 Load Env File

      Retrieve Info
      Utilities for Retrieving Info
      Open Directory
      kpathsea vs kpse-find-file
      Root Directory
      Env File from Command Line
      Env File from Location of t4ht.c

Retrieve Info

<..load env file..>
 {                                FILE* file;
                                  U_CHAR  env_loc[512];
    env_loc[0] = ’\0’;
    <.find tex4ht.env.>
    if( file ){
       <.get info from env file.>
       (IGNORED) fclose(file);
 }  }
 -_-_-

<..get info from env file..>
 eoln_ch = (int) ’x’;
 while( eoln_ch != EOF ) {                Q_CHAR ch;
   ch = (char) (eoln_ch = getc(file));
   if( eoln_ch != (int) ’\n’ ){
      status = scan_until_end_str("", 1, TRUE, file);
      if( status ){
        switch( ch ){
          case ’A’:{ <.script for chmod.>  break;}
          case ’C’:{ <.script for copy.>  break;}
          case ’E’:{ <.script for empty gifs.>  break;}
          case ’F’:{ <.script for dvigif of glyps.>   break;}
          case ’G’:{ <.script for dvigif.>   break;}
          case ’M’:{ <.script for move.>  break;}
          case ’S’:{ <.env permissions for system calls.>  break;}
          case ’X’:{ <.script for file.>  break;}
          case ’.’:{ <.script for file ext.>  break;}
          case ’<’:{ <.env selector.>  break;}
          default:{ }
 } }  }  }
 -_-_-

<..script for dvigif of glyps..>
 if( debug ){
       (IGNORED) printf(".......’F’ script\n"); }
 dvigif_glyp_script = add_script(dvigif_glyp_script);
 -_-_-

<..script for move..>
 if( debug ){
       (IGNORED) printf(".......’M’ script\n"); }
 move_script = add_script(move_script);
 -_-_-

<..script for copy..>
 if( debug ){
       (IGNORED) printf(".......’C’ script\n"); }
 copy_script = add_script(copy_script);
 -_-_-

<..script for file..>
 if( debug ){
       (IGNORED) printf(".......’X’ script\n"); }
 <.file-script.> = add_script(<.file-script.>);
 -_-_-

<..script for file ext..>
 if( debug ){
       (IGNORED) printf(".......’.’ script\n"); }
 <.ext-script.> = add_script(<.ext-script.>);
 -_-_-

<..script for chmod..>
 if( debug ){
       (IGNORED) printf(".......’A’ script\n"); }
 chmod_script = add_script(chmod_script);
 -_-_-

<..script for empty gifs..>
 if( debug ){
       (IGNORED) printf(".......’E’ script\n"); }
 empty_fig_script = add_script(empty_fig_script);
 -_-_-

Utilities for Retrieving Info

<..env selector..>
 if( *(match[1]) != ’/’ ){
                          U_CHAR *p;
                          BOOL env_skip;
   for( p = match[1];
       (*p != ’>’) && (*p != ’\n’) && (*p != EOF ) ;
       p++ ){}
   if( *p == ’>’ ){ *p = ’\0’; }
   <.env-skip = skip segment match[1].>
   if( env_skip ){
      <.skip env segment.>
   } else {
      if( debug ){
         (IGNORED) printf(".......   <%s>\n", match[1]);
      }
   }
 } else if( debug ){
    (IGNORED) printf(".......   <%s\n", match[1]);
 }
 -_-_-

<..skip env segment..>
                          U_CHAR cur_block[90];
 if( debug ){
    (IGNORED) printf(".......   <%s>  skipping ...\n", match[1]);
 }
 (IGNORED) strcpy((char *)  cur_block, (char *) match[1]);
 status = FALSE;
 while( !status && (eoln_ch != EOF) ){
   status = scan_str("</", TRUE, file);
   status = scan_until_str(">", 1, status, file);
   status = scan_until_end_str("", 2, status, file);
   if( status ){
       *(match[1] + strlen((char *) match[1]) - 1) = ’\0’;
       status = eq_str(match[1], cur_block);
       if( debug ){
          (IGNORED) printf(".......   </%s>\n", match[1]);
 } }   }
 -_-_-

<..header functions..>+
 static struct script_struct * add_script( ARG_I(struct script_struct *) );
 -_-_-

<..functions..>+
 
 static struct script_struct * add_script(script)
                            struct script_struct * script
 ;{
    struct script_struct* temp, * q;
    temp = (struct script_struct *)
                       m_alloc(struct script_struct, (int) 1);
    temp->command = match[1];
    if( debug ){
       (IGNORED) printf(".......   %s\n", temp->command); }
    temp->next = NULL_SCRIPT;
    match[1] = (Q_CHAR *) malloc(70);
    max_match[1] = 70;
    if( script ){
       q = script;
       while( q->next ){ q = q->next; }
       q->next = temp;
    } else {
       script = temp;
    }
    return script;
 }
 -_-_-

<..defines..>+
 struct script_struct{
   Q_CHAR *command;
   struct script_struct *next;
 };
 -_-_-

<..vars..>+
 static struct script_struct
     * dvigif_script = NULL_SCRIPT,
     * dvigif_glyp_script = NULL_SCRIPT,
     * move_script = NULL_SCRIPT,
     * empty_fig_script = NULL_SCRIPT,
     * copy_script = NULL_SCRIPT,
     * <.file-script.> = NULL_SCRIPT,
     * <.ext-script.> = NULL_SCRIPT,
     * chmod_script = NULL_SCRIPT;
 -_-_-

<..defines..>+
 #define NULL_SCRIPT (struct script_struct *) 0
 -_-_-

%   dvips -mode ibmvga -D 110 -f foo.idv -pp 92  > tmp.ps 
%   convert -crop 0x0 -density 110x110 -transparent ’#FFFFFF’ tmp.ps foo.gif 
Gdvips -mode ibmvga -D 110 -f %1 -pp %2  > tmp.ps 
Gconvert -crop 0x0 -density 110x110 -transparent ’#FFFFFF’ tmp.ps %3 
% t4ht -d%2 
Mmv %1 %2%3 
Ccp %1 %2%3 
% t4ht -d%2 -m%1 
Achmod %1 %2%3 
H/n/gold/5/gurari/tex4ht.dir/ 

Open Directory

<..env file at work dir..>
 if( !file ) {
    if( debug ){
       (IGNORED) printf("tex4ht.env?\n");
    }
    file = f_open("tex4ht.env", READ_TEXT_FLAGS);
    (IGNORED) strcpy((char *) &env_loc[0], "tex4ht.env");
    if( debug && file ){
       (IGNORED) printf(".......Open: ./tex4ht.env\n"); }
 }
 -_-_-

<..find tex4ht.env..>
 {         Q_CHAR  str[512],
                                                       *TEX4HTENV;
    <.env file from command switch.>
    <.env file at work dir.>
    <.env file .tex4ht.>
    <.TEX4HTENV env.>
    <.env file at root dir.>
    <.ENVFILE env.>
    if( !file ) { <.env file from prog loc.> }
    <.kpathsea env file.>
    if( !file ) warn_i_str( 5, <.warn 1.>);
 }
 -_-_-

<..TEX4HTENV env..>
 if( !file ){
   TEX4HTENV = getenv("TEX4HTENV");
   if( TEX4HTENV ){
      if( debug ){
          (IGNORED) printf("%s?\n", TEX4HTENV);
      }
      file = f_home_open(TEX4HTENV,READ_TEXT_FLAGS);
   }
 }
 -_-_-

<..env file .tex4ht..>
 #ifndef DOS_WIN32
    if( !file ) {
        if( debug ){
           (IGNORED) printf(".tex4ht?\n");
        }
        file = f_open(".tex4ht", READ_TEXT_FLAGS);
        (IGNORED) strcpy((char *) &env_loc[0], ".tex4ht");
        if( debug && file ){
          (IGNORED) printf(".......Open: ./.tex4ht\n"); }
    }
 #endif
 -_-_-

<..ENVFILE env..>
 #ifdef ENVFILE
    if( !file ) {
       if( debug ){
           (IGNORED) printf("%s?\n", ENVFILE);
       }
       file = f_home_open( ENVFILE,READ_TEXT_FLAGS);
        (IGNORED) strcpy((char *) &env_loc[0], (char *) ENVFILE);
        if( debug && file ){
          (IGNORED) printf(".......Open: %s\n", ENVFILE); }
    }
 #endif
 -_-_-

<..h-defines..>
 #ifndef ENVFILE
 
 #endif
 -_-_-

<..warn 1..>
 #ifdef  DOS_WIN32
    "tex4ht.env"
 #endif
 #ifndef  DOS_WIN32
    "tex4ht.env | .tex4ht"
 #endif
 -_-_-

<..kpathsea env file..>
 
 #ifdef KPATHSEA
 if( !file )  {               U_CHAR * envfile;
                              char *arch, *p, str[256];
    <.arch = tail of SELFAUTOLOC.>
    envfile = (char *) 0;
    <.envfile = kpse-find-file architecture/tex4ht.env.>
    if ( !envfile ){ <.envfile = kpse-find-file tex4ht.env.> }
    if ( !envfile ){ <.envfile = kpathsea tex4ht.env.> }
    if ( envfile ){
       file = kpse_open_file (envfile, kpse_program_text_format);
       (IGNORED) printf("(%s)\n",  envfile);
    }
    if( debug && file ){
       (IGNORED) printf(".......Open kpathsea %s\n", envfile);
    }
 }
 if( debug ){
                        U_CHAR *p, *q;
   <.trace T4HTINPUTS.>
 }
 #endif
 -_-_-

<..arch = tail of SELFAUTOLOC..>
 p = arch = (char *) kpse_var_value( "SELFAUTOLOC" );
 while( *p != ’\0’ ){
    if( (*p ==   ’/’) || (*p == ’\\’) ){
       arch = p;
    }
    p++;
 }
 -_-_-

<..envfile = kpse-find-file architecture/tex4ht.env..>
 if( arch ){
   (IGNORED) sprintf(str,"%s%ctex4ht.env", arch+1, *arch);
   if( debug ){
     (IGNORED) printf(
       "kpse_open_file (\"%s\", kpse_program_text_format)?\n", str );
   }
   envfile= kpse_find_file (str, kpse_program_text_format, 0);
 }
 -_-_-

<..envfile = kpse-find-file tex4ht.env..>
 if( debug ){
   (IGNORED) printf(
     "kpse_open_file (\"tex4ht.env\", kpse_program_text_format)?\n");
 }
 envfile= kpse_find_file ("tex4ht.env", kpse_program_text_format, 0);
 -_-_-

<..trace T4HTINPUTS..>
 p = (U_CHAR *) kpse_var_value( "T4HTINPUTS" );
 if( p ){
    (IGNORED) printf("T4HTINPUTS = %s\n", p);
 }
 q = getenv("T4HTINPUTS");
 if( q ){  (IGNORED) printf(
    "Environmet var T4HTINPUTS:  %s\n", q);
 }
 if( !p && !q ){
    (IGNORED) printf( "Missing T4HTINPUTS for kpathsea\n" );
 }
 -_-_-

kpathsea vs kpse-find-file

The kpathsea utility might succeed where kpse-find-file fails. Maybe due to: “Kpathsea’s subdirectory searching has an irremediable deficiency: If a directory D being searched for subdirectories contains plain files and symbolic links to other directories, but no true subdirectories, D will be considered a leaf directory, i.e., the symbolic links will not be followed.” [kpathsea.info]

<..envfile = kpathsea tex4ht.env..>
 if( system("kpsewhich --progname=tex4ht tex4ht.env > tex4ht.tmp") == 0 ){
    <.fileaddr = read tex4ht.tmp.>
    envfile= kpse_find_file (fileaddr, kpse_program_text_format, 0);
 }
 -_-_-

Don’t know how to avoid the use of intermediate file: popen(...) and fork() are not stadard utilities. Also the redirection > might need to be changed, e.g., to >&.

<..fileaddr = read tex4ht.tmp..>
 char fileaddr [256];
 int loc = 0;
 FILE* file =  f_open("tex4ht.tmp", READ_TEXT_FLAGS);
 if( file ){
   while( (fileaddr[loc] = getc(file)) >=0  ){
     if( fileaddr[loc] == ’\n’ ){ fileaddr[loc] = ’\0’; break; }
     loc++;
   }
   (IGNORED) fclose(file);
 }
 -_-_-

Root Directory

<..env file at root dir..>
 if( !file ){
   if( HOME_DIR ){ (IGNORED) sprintf(str,<."s/tex4ht.env".>, HOME_DIR);
      if( debug ){
          (IGNORED) printf("%s?\n", str);
      }
      file = f_open(str,READ_TEXT_FLAGS);
      (IGNORED) strcpy((char *) &env_loc[0], (char *) str);
      if( debug && file ){
         (IGNORED) printf(".......Open: %s\n", str); }
   }
 }
 #ifndef DOS_WIN32
   if( !file ){
      if( HOME_DIR ){
           (IGNORED) sprintf(str,"%s/.tex4ht", HOME_DIR);
           if( debug ){
              (IGNORED) printf("%s?\n", str);
           }
           file = f_open(str,READ_TEXT_FLAGS);
           (IGNORED) strcpy((char *) &env_loc[0], (char *) str);
           if( debug && file ){
              (IGNORED) printf(".......Open: %s\n", str); }
       }
   }
 #endif
 #if defined(DOS_WIN32) || defined(__MSDOS__)
    if( !file ){
       if( debug ){
           (IGNORED) printf("C:/tex4ht.env?\n");
       }
       file = f_open("C:/tex4ht.env",READ_TEXT_FLAGS);
        (IGNORED) strcpy((char *) &env_loc[0], "C:/tex4ht.env");
       if( debug && file ){
         (IGNORED) printf(".......Open: C:/tex4ht.env\n"); }
    }
 #endif
 -_-_-

<.."s/tex4ht.env"..>
 #if defined(DOS_WIN32) || defined(__DJGPP__)
   is_forward_slash(HOME_DIR)?  "%s/tex4ht.env" :  "%s\\tex4ht.env"
 #else
   "%s/tex4ht.env"
 #endif
 -_-_-

Env File from Command Line

<..env file from command switch..>
 if( tex4ht_env_file ){
    if( debug ){
       (IGNORED) printf("%s?\n", tex4ht_env_file);
    }
    file = f_home_open( tex4ht_env_file, READ_TEXT_FLAGS );
 } else {
    file = NULL;
 }
 if( tex4ht_env_file ){
     (IGNORED) strcpy((char *) &env_loc[0], (char *) tex4ht_env_file);
 }
 if( debug && file ){
       (IGNORED) printf(".......Open: %s\n", tex4ht_env_file); }
 -_-_-

<..env file from prog loc..>
 if( dos_env_file ){
    if( debug ){
        (IGNORED) printf("%s?\n", dos_env_file);
    }
    file =  f_open( dos_env_file, READ_TEXT_FLAGS ) ;
    (IGNORED) strcpy((char *) &env_loc[0], (char *) dos_env_file);
    if( debug && file ){
       (IGNORED) printf(".......Open: %s\n", dos_env_file); }
 }
 -_-_-

Search along the path: command line file work directory home directory system file.

<..get .env directory from com ln..>
 tex4ht_env_file = q;
 -_-_-

The function ‘int access(const char *pathname, int mode)’, and the mode ‘F_OK’ that tests for the existence of file, are defined in the following directory. The function returns 0 if ok and -1 on error .

Where access comes from in dos?

<..h-include before KPATHSEA..>
 #ifndef __DJGPP__
 <.h-include near KPATHSEA.>
 #endif
 -_-_-

The following is because ‘<unistd.h> defines F_OK in DJGPP’.

<..h-include after KPATHSEA..>
 #ifdef __DJGPP__
 <.h-include near KPATHSEA.>
 #endif
 -_-_-

<..h-include near KPATHSEANO..>
 #ifndef F_OK
 #ifdef DOS_WIN32
 #define  F_OK 0                does file exist 
 #endif
 #ifndef DOS_WIN32
 #define HAVE_UNISTD_H
 #endif
 #endif
 #ifdef HAVE_IO_H
 #include <io.h>
 #endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 -_-_-

<..h-include near KPATHSEA..>
 #ifndef F_OK
 #ifdef DOS_WIN32
 #define  F_OK 0                does file exist 
 #endif
 #ifndef KPATHSEA
 #ifndef DOS_WIN32
 #define HAVE_UNISTD_H
 #endif
 #endif
 #endif
 #ifdef HAVE_IO_H
 #include <io.h>
 #endif
 #ifndef KPATHSEA
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 #endif
 -_-_-

Env File from Location of t4ht.c

<..main’s vars..>+
 Q_CHAR * tex4ht_env_file = (Q_CHAR *) 0;
 Q_CHAR * dos_env_file =
 #if defined(__MSDOS__)
   <.get dos env file.>;
 #endif
 #if !defined(__MSDOS__)
   (Q_CHAR *) 0;
 #endif
 
 -_-_-

Under DOS, argv[0] usually includes the full path to the program - even if it wasn’t typed in. This can be used to find the program’s own directory, and hence the configuration file. I’ve implemented this already as follows:

<..get dos env file..>
 get_env_dir(argv[0])
 -_-_-

<..functions..>+
 #if defined(__MSDOS__)
 <.get tex4ht.env in dos.>
 #endif
 -_-_-

<..get tex4ht.env in dos..>
 
 static char *get_env_dir(progname)
       Q_CHAR *progname
 
 ;{    int  i;
       Q_CHAR *p;
   if(! progname || ! *progname)  return NULL;   Safety 
   i = (int) strlen((char *) progname);
   while( (progname[--i] != (int) dir_path_slash(progname) )
         && i > 0) ;                               Search for slash
   if(i == 0)  return NULL;                        Not found? Give up
   p = (Q_CHAR *) malloc(i+12);
   if(p == NULL)  return NULL;     Space for new extention after dot
   strncpy(p, progname, i+1);                         Copy up to slash
   (IGNORED) strcpy((char *) &p[i+1],
                    "tex4ht.env");                    Append new extention
   return p;
 }
 -_-_-

The 12 above is for accomodating tex4ht.env.

Then, in main(), I changed:

‘’char* tex4ht˙env˙file = (Q˙CHAR *) 0;’

to:

char* tex4ht_env_file = get_env_dir(argv[0]);

This works like a charm here, and allows me to use tex4ht from a different directory without typing a single commandline option.