home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / programs / amiga / pastex / src / flib / flib.c next >
C/C++ Source or Header  |  1992-11-04  |  47KB  |  1,874 lines

  1. /*
  2.  *    Fontlibrary program.
  3.  *
  4.  *        used for our TeX-system to save disk space and for
  5.  *        speed up the loading of fonts for "ShowDVI" and "DVIprint".
  6.  *
  7.  *    This program is freely redistributable. You may modify and use this
  8.  *    program to your heart's content, so long as you send modifications
  9.  *    to Georg Hessmann. It can be included in any distribution, commercial
  10.  *    or otherwise, so long as the banner string defined in the constant
  11.  *    HEADER is not modified (except for the version number and the machine
  12.  *    type) and this banner is printed on program invocation, or can be
  13.  *    printed on program invocation with the -? option.
  14.  *
  15.  *    For further information read the README file.
  16.  *
  17.  *      31.01.90  (hes)      (C) Copyright 1990/91 Georg Hessmann.
  18.  *
  19.  *      12.02.90  (hes) v1.01    new flib-format, rename command
  20.  *      14.02.90  (rbs)        ported to Atari ST
  21.  *      25.04.90  (jrb)        fixed up for Atari ST gcc
  22.  *    05.06.90  (hes) v1.02    little bug fixes, add flib links (hes)
  23.  *    08.01.91  (hes) v1.03    add relative flib links
  24.  *      03.11.92  (hes) v1.10   add wildcard support (only with Dice/Amiga)
  25.  *                add two new flags 's' and 'e'
  26.  *                's' -- strip path, don't store the path of a font
  27.  *                'e' -- strip extension, don't store the extension
  28.  */
  29.  
  30.  
  31. /* Is the compiler a ANSI-C compiler */
  32. #ifdef AMIGA
  33. #  ifdef LATTICE
  34. #    define ANSI
  35. #  endif
  36. #  ifdef AZTEC_C
  37. #    define ANSI
  38. #  endif /* AZTEC_C */
  39. #  ifdef _DCC
  40. #    define ANSI
  41. #  endif /* _DCC */
  42. #else
  43. #  ifdef ATARI
  44. #    ifdef __GNUC__
  45. #      define ANSI
  46. #    endif /* __GNUC__ */
  47. #  endif /* ATARI */
  48. #endif /* AMIGA */
  49.  
  50. /* standard includes */
  51. #include <stdio.h>
  52. #ifdef ANSI
  53. #  include <stdlib.h>
  54. #  include <string.h>
  55. #  ifdef AMIGA
  56. #    ifndef _DCC
  57. #      include <dos.h>
  58. #    endif /* !_DCC */
  59. #  endif /* AMIGA */
  60. #  ifdef ATARI
  61. #    ifndef __GNUC__
  62. #        include <tos.h>
  63. #        include <ext.h>
  64. #    else
  65. #        include <unixlib.h>
  66. #        include <fcntl.h>
  67. #        define S_IWRITE    W_OK
  68. #        define S_IREAD     R_OK
  69. #    endif /* __GNUC__ */
  70. #  endif /* ATARI */
  71. #endif /* ANSI */
  72.  
  73.  
  74.  
  75. /* open mode for files */
  76. #ifdef ATARI
  77. #  define OPEN_FOR_READING "rb"
  78. #  define OPEN_FOR_WRITING "wb"
  79. #  define OPEN_FOR_APPEND  "rb+"
  80. #  ifndef __GNUC__
  81.      extern int access(char *, int);
  82. #  endif /* __GNUC__ */
  83. #else
  84. #  define OPEN_FOR_READING "r"
  85. #  define OPEN_FOR_WRITING "w"
  86. #  define OPEN_FOR_APPEND  "r+"
  87. #endif
  88.  
  89. /* prototyping on/off */
  90. #ifdef ANSI
  91. #  define Args(x)    x
  92. #else
  93. #  define Args(x)    ()
  94. #endif /* ANSI */
  95.  
  96.  
  97. #ifdef AZTEC_C
  98.    void *malloc        Args((unsigned));
  99.    void free        Args((char *));
  100.    char *strchr();
  101.    char *strrchr();
  102. #endif /* AZTEC_C */
  103.  
  104.  
  105. #ifdef AMIGA
  106. #  ifdef LATTICE
  107. #    define perror(x)    poserr(x)
  108. #  endif /* LATTICE */
  109. #  ifdef AZTEC_C
  110. #    define remove(x)    unlink(x)
  111.      /* warning: AztecC seems to have problems with the returncode from fseek */
  112. #  endif /* AZTEC_C */
  113. #else AMIGA
  114. #  ifndef ANSI
  115. #    define remove(x)    unlink(x)
  116. #  endif /* ANSI */
  117. #endif /* AMIGA */
  118.  
  119. #ifdef ATARI
  120. #  include <errno.h>
  121. #else
  122. extern int errno;
  123. #endif
  124.  
  125. /*    Aufbau einer Fontlibrary :
  126.  *    +---------------------------------------+
  127.  *    |   magic number        4 Bytes    |
  128.  *    |   number of direntries    4 Bytes    |
  129.  *    |   [flib_dirent]*            |
  130.  *    |   [modules]*                |
  131.  *    +---------------------------------------+
  132.  */
  133.  
  134.  
  135.  
  136. /* important constants */
  137. #define OLD_LIBMAGIC    0xA797F033L        /* magic number */
  138. #define LIBMAGIC    (((long)'F'<<24) | ((long)'L'<<16) | ((long)'I'<<8) | (long)'B')
  139. #define LNKMAGIC    (((long)'F'<<24) | ((long)'L'<<16) | ((long)'N'<<8) | (long)'K')
  140. #define FILENAMELEN    14
  141. #define NEWFILENAMELEN    22
  142. #define TMPFLIB        "tmpflib.tmp"
  143. #define BUFSIZE        20480
  144. #define DOSNAMESIZE    100
  145. #define MAXLINKLEVELS    20
  146.  
  147. #ifdef AMIGA
  148. #  define MACHINE    "AMIGA"
  149. #else
  150. #  ifdef ATARI
  151. #    define MACHINE    "ATARI"
  152. #  else
  153. #    define MACHINE    "UNIX"
  154. #    include <sys/file.h>
  155. #    define S_IWRITE    W_OK
  156. #    define S_IREAD    R_OK
  157. #  endif /* ATARI */
  158. #endif /* AMIGA */
  159.  
  160. #define VERSION        "1.10"
  161. #ifdef ATARI
  162. #  define HEADER        "FontLib Manager - Version %s for %s \275 1990/91/92 Georg Hessmann/Robert Stabl\n"
  163. #else
  164. #  define HEADER        "FontLib Manager - Version %s for %s (c) 1990/91/92 Georg Hessmann\n"
  165. #endif
  166.  
  167. #ifdef AMIGA
  168. static char version[] = "\0$VER: flib 1.10 (11/03/92)";
  169. #endif
  170.  
  171.  
  172. #define    OLD_VERSION    0
  173. #define NEW_VERSION    1
  174.  
  175.  
  176. static char buffer[BUFSIZE];    /* used to copy modules */
  177.  
  178.  
  179. #ifndef FALSE
  180. #  define FALSE        0
  181. #endif /* FALSE */
  182. #ifndef TRUE
  183. #  define TRUE        (!FALSE)
  184. #endif /* TRUE */
  185.  
  186. #define offset(ptr,member)    ((long)(&((ptr)->member))-(long)(ptr))
  187.  
  188.  
  189. /************ structure definitions ***************/
  190. /* fontlib directory structures */
  191. struct flib_dirent { char mname[FILENAMELEN];        /* old version */
  192.                      long size;        /* size of pk-module in bytes */
  193.                      long where;    /* position in flib-file */
  194.                    };
  195.  
  196. struct new_flib_dirent { char  mname[NEWFILENAMELEN];    /* new version */
  197.              unsigned short checksum;
  198.              long  size;        /* size of pk-module in bytes */
  199.              long  where;        /* position in flib-file */
  200.              char * real_path;    /* only for flib internal use!! */
  201.                          /* real_path won't saved to the flib */
  202.                        };
  203.  
  204. union direntry { struct flib_dirent    old;
  205.          struct new_flib_dirent    new;
  206.            };
  207.  
  208.  
  209. /* internal representation of the flib-directory */
  210. struct dirlist { struct new_flib_dirent dirent;
  211.                  struct dirlist *next;
  212.            };
  213.  
  214. struct dir { long        total;
  215.          long        alloc;
  216.          int        version;    /* old or new flib */
  217.          int        is_link;
  218.              struct dirlist *dirlist;
  219.              char        real_name[DOSNAMESIZE];
  220.        };
  221.  
  222.  
  223. /*********   P R O T O T Y P E S   ********/
  224.  
  225.     /* main function                        */
  226. void main                Args((int argc, char *argv[]));
  227.  
  228.  
  229. /********* local functions **********/
  230.     /* print the help                        */
  231. static void  usage            Args((char *pname));
  232.     /* parse the first argument                    */
  233. static int   decode_args        Args((int argc, char **argv,
  234.                           int *table,
  235.                           int *extract,
  236.                           int *create,
  237.                           int *delete,
  238.                           int *compress,
  239.                           int *verbose,
  240.                           int *rename,
  241.                           int *test,
  242.                           int *link,
  243.                           int *term_input));
  244.  
  245.  
  246.     /* calculates the length of a module name */
  247. static int   module_len(char * name);
  248.  
  249.     /* delete flib with the name "name" and rename the tmpflib to "name" */
  250.     /* nr is the number of modules in the flib => nr == 0 --> no flib    */
  251. static int   insert_name_into_dir    Args((char *name,
  252.                           struct dirlist **dirl,
  253.                           int version));
  254.     /* is module from "test" in "inp1" or "inp2" ? */
  255. static struct dirlist *exist_entry    Args((struct dirlist *inp1,
  256.                           struct dirlist *inp2,
  257.                           struct dirlist *test));
  258.     /* create a dirlist structure entry whith the module-name "name"    */
  259. static int   read_new_modules        Args((int term_input,
  260.                           int argc,
  261.                           char **argv,
  262.                           struct dir *directory,
  263.                           struct dirlist **start_dirl,
  264.                           int verbose));
  265.     /* read a list of modules from argv or from stdin        */
  266. static FILE *open_flib            Args((char *name,
  267.                           char *mode,
  268.                           struct dir *directory,
  269.                           short levels));
  270.     /* open a flib and test the magic-number            */
  271. static int   read_dir            Args((FILE *f,
  272.                           struct dir *directory));
  273.     /* read the directory from the flib (file-pointer must be on the first dir)       */
  274.     /* only this directory entries a correct where "where != 0" the others are unused */
  275. static void  print_dir            Args((struct dir *directory,
  276.                           char *lib_name,
  277.                           int verbose));
  278.     /* print the contents of the directory                */
  279. static int   ex_module            Args((struct dir *directory,
  280.                           char *mname,
  281.                           FILE *flib, int verbose));
  282.     /* search a module in the flib and copy it to a new file    */
  283. static int   create_new_flib        Args((struct dir *directory,
  284.                           char *name,
  285.                           int verbose));
  286.     /* create a complete (new) flib, copy only files into the flib    */
  287. static int   copy_module        Args((char *name,
  288.                           char *real_path,
  289.                           FILE *flib,
  290.                           long *size,
  291.                           long *where,
  292.                           unsigned short *check));
  293.     /* create a new flib and prepare the directory part        */
  294. static FILE *c