home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / bbs / programs / amiga / makeindex.lha / makeindex-2.12 / src / mkind.c < prev    next >
C/C++ Source or Header  |  1994-04-02  |  18KB  |  758 lines

  1. /*
  2.  *
  3.  *  This file is part of
  4.  *    MakeIndex - A formatter and format independent index processor
  5.  *
  6.  *  Copyright (C) 1989 by Chen & Harrison International Systems, Inc.
  7.  *  Copyright (C) 1988 by Olivetti Research Center
  8.  *  Copyright (C) 1987 by Regents of the University of California
  9.  *
  10.  *  Author:
  11.  *    Pehong Chen
  12.  *    Chen & Harrison International Systems, Inc.
  13.  *    Palo Alto, California
  14.  *    USA
  15.  *    (phc@renoir.berkeley.edu or chen@orc.olivetti.com)
  16.  *
  17.  *  Contributors:
  18.  *    Please refer to the CONTRIB file that comes with this release
  19.  *    for a list of people who have contributed to this and/or previous
  20.  *    release(s) of MakeIndex.
  21.  *
  22.  *  All rights reserved by the copyright holders.  See the copyright
  23.  *  notice distributed with this software for a complete description of
  24.  *  the conditions under which it is made available.
  25.  *
  26.  */
  27.  
  28. #define MKIND_C    1
  29. #include    "mkind.h"
  30. #undef MKIND_C
  31.  
  32. int     letter_ordering = FALSE;
  33. int     compress_blanks = FALSE;
  34. int     merge_page = TRUE;
  35. int     init_page = FALSE;
  36. int     even_odd = -1;
  37. int     verbose = TRUE;
  38. int     german_sort = FALSE;
  39. int     fn_no = -1;               /* total number of files */
  40. int     idx_dot = TRUE;               /* flag which shows dot in ilg being
  41.                     * active */
  42. int     idx_tt = 0;               /* total entry count (all files) */
  43. int     idx_et = 0;               /* erroneous entry count (all files) */
  44. int     idx_gt = 0;               /* good entry count (all files) */
  45.  
  46. FIELD_PTR *idx_key;
  47. FILE   *log_fp;
  48. FILE   *sty_fp;
  49. FILE   *idx_fp;
  50. FILE   *ind_fp;
  51. FILE   *ilg_fp;
  52.  
  53. #if OS_BS2000
  54. FILE   *widx_fp;
  55. char    widx_fn[STRING_MAX + 5];
  56. #endif                       /* OS_BS2000 */
  57.  
  58. char   *pgm_fn;
  59. #if OS_AMIGA
  60. char    sty_fn[ARRAY_MAX];
  61. #else
  62. char    sty_fn[LINE_MAX];
  63. #endif
  64. char   *idx_fn;
  65. char    ind[STRING_MAX];
  66. char   *ind_fn;
  67. char    ilg[STRING_MAX];
  68. char   *ilg_fn;
  69. char    pageno[NUMBER_MAX];
  70.  
  71. static char log_fn[STRING_MAX];
  72. static char base[STRING_MAX];
  73. static int need_version = TRUE;
  74.  
  75. static    void    check_all ARGS((char *fn,int ind_given,int ilg_given,
  76.             int log_given));
  77. static    void    check_idx ARGS((char *fn,int open_fn));
  78. static    void    find_pageno ARGS((void));
  79. static    void    open_sty ARGS((char *fn));
  80. static    void    prepare_idx ARGS((void));
  81. static    void    process_idx ARGS((char * *fn,int use_stdin,int sty_given,
  82.             int ind_given,int ilg_given,int log_given));
  83.  
  84. #if    OS_PCDOS
  85. #if    IBM_PC_TURBO
  86. unsigned    _stklen = 0xf000;    /* Turbo C ignores size set in
  87.                     .exe file by LINK or EXEMOD,
  88.                     sigh... */
  89. #endif                    /* IBM_PC_TURBO */
  90. #endif                    /* OS_PCDOS */
  91.  
  92. #if OS_ATARI
  93. long    _stksize = 20000L;           /* make the stack larger than 2KB */
  94. /* size must  be given */
  95. #endif                       /* OS_ATARI */
  96.  
  97. #ifdef DEBUG
  98. long totmem = 0L;            /* for debugging memory usage */
  99. #endif /* DEBUG */
  100.  
  101. int
  102. #if STDC
  103. main(int argc, char *argv[])
  104. #else
  105. main(argc, argv)
  106. int     argc;
  107. char   *argv[];
  108. #endif
  109. {
  110. #if OS_AMIGA
  111.     char   **fns; /* better to alloc using malloc (safe stack), so MakeIndex
  112.                      works with just 4000 stack size */
  113. #else
  114.     char   *fns[ARRAY_MAX];
  115. #endif /* OS_AMIGA */
  116.     char   *ap;
  117.     int     use_stdin = FALSE;
  118.     int     sty_given = FALSE;
  119.     int     ind_given = FALSE;
  120.     int     ilg_given = FALSE;
  121.     int     log_given = FALSE;
  122.  
  123.     /* determine program name */
  124. #if (OS_ATARI | OS_VAXVMS | OS_BS2000 | OS_MVSXA | OS_VMCMS)
  125.     pgm_fn = "MakeIndex";           /* Use symbolic name on some systems */
  126. #else
  127.     pgm_fn = strrchr(*argv, DIR_DELIM);
  128.     if (pgm_fn == NULL)
  129.     pgm_fn = *argv;
  130.     else
  131.     pgm_fn++;
  132.  
  133. #if OS_PCDOS
  134.     {
  135.     register char *ext = pgm_fn + strlen(pgm_fn)-4;
  136.     if ( *ext == '.' )
  137.         *ext = NUL;            /* cut off ".EXE" */
  138.     }
  139.     (void)strlwr(pgm_fn);        /* lower program name    */
  140. #endif                    /* OS_PCDOS */
  141.  
  142. #endif                       /* OS_VAXVMS | OS_BS2000 | OS_MVSXA */
  143.  
  144. #if OS_AMIGA
  145.     if ((fns = malloc(ARRAY_MAX*sizeof(char *))) == NULL)
  146.         FATAL("Not enough memory...abort.\n","");
  147. #endif /* OS_AMIGA */
  148.  
  149.     /* process command line options */
  150.     while (--argc > 0) {
  151.     if (**++argv == SW_PREFIX) {
  152.         if (*(*argv + 1) == NUL)
  153.         break;
  154.         for (ap = ++*argv; *ap != NUL; ap++)
  155. #if  (OS_BS2000 | OS_MVSXA | OS_VAXVMS)
  156.         switch (tolower(*ap)) {
  157. #else
  158.         switch (*ap) {
  159. #endif /* (OS_BS2000 | OS_MVSXA | OS_VAXVMS) */
  160.  
  161.             /* use standard input */
  162.         case 'i':
  163.             use_stdin = TRUE;
  164.             break;
  165.  
  166.             /* enable letter ordering */
  167.         case 'l':
  168.             letter_ordering = TRUE;
  169.             break;
  170.  
  171.             /* disable range merge */
  172.         case 'r':
  173.             merge_page = FALSE;
  174.             break;
  175.  
  176.             /* supress progress message -- quiet mode */
  177.         case 'q':
  178.             verbose = FALSE;
  179.             break;
  180.  
  181.             /* compress blanks */
  182.         case 'c':
  183.             compress_blanks = TRUE;
  184.             break;
  185.  
  186.             /* style file */
  187.         case 's':
  188.             argc--;
  189.             if (argc <= 0)
  190.             FATAL("Expected -s <stylefile>\n","");
  191.             open_sty(*++argv);
  192.             sty_given = TRUE;
  193.             break;
  194.  
  195.             /* output index file name */
  196.         case 'o':
  197.             argc--;
  198.             if (argc <= 0)
  199.             FATAL("Expected -o <ind>\n","");
  200.             ind_fn = *++argv;
  201.             ind_given = TRUE;
  202.             break;
  203.  
  204.             /* transcript file name */
  205.         case 't':
  206.             argc--;
  207.             if (argc <= 0)
  208.             FATAL("Expected -t <logfile>\n","");
  209.             ilg_fn = *++argv;
  210.             ilg_given = TRUE;
  211.             break;
  212.  
  213.             /* initial page */
  214.         case 'p':
  215.             argc--;
  216.             if (argc <= 0)
  217.             FATAL("Expected -p <num>\n","");
  218.             strcpy(pageno, *++argv);
  219.             init_page = TRUE;
  220.             if (STREQ(pageno, EVEN)) {
  221.             log_given = TRUE;
  222.             even_odd = 2;
  223.             } else if (STREQ(pageno, ODD)) {
  224.             log_given = TRUE;
  225.             even_odd = 1;
  226.             } else if (STREQ(pageno, ANY)) {
  227.             log_given = TRUE;
  228.             even_odd = 0;
  229.             }
  230.             break;
  231.  
  232.             /* enable german sort */
  233.         case 'g':
  234.             german_sort = TRUE;
  235.             break;
  236.  
  237.             /* bad option */
  238.         default:
  239.             FATAL("Unknown option -%c.\n", *ap);
  240.             break;
  241.         }
  242.     } else {
  243.         if (fn_no < ARRAY_MAX) {
  244.         check_idx(*argv, FALSE);
  245.         fns[++fn_no] = *argv;
  246.         } else {
  247.         FATAL("Too many input files (max %d).\n", ARRAY_MAX);
  248.         }
  249.     }
  250.     }
  251.  
  252. /* changes for 2.12 (May 20, 1993) by Julian Reschke (jr@ms.maus.de):
  253.    if only one input file and no explicit style file was given */
  254. #if OS_BS2000
  255. /* not supported */
  256. #else
  257.     if (fn_no == 0 && !sty_given)
  258.     {
  259.         char tmp[STRING_MAX + 5];
  260.         
  261.         /* base set by last call to check_idx */
  262.         sprintf (tmp, "%s%s", base, INDEX_STY);
  263.         if (0 == access(tmp, R_OK)) {
  264.             open_sty (tmp);
  265.             sty_given = TRUE;
  266.         }
  267.     }
  268. #endif
  269.  
  270.     process_idx(fns, use_stdin, sty_given, ind_given, ilg_given, log_given);
  271.     idx_gt = idx_tt - idx_et;
  272.     ALL_DONE;
  273.     if (idx_gt > 0) {
  274.     prepare_idx();
  275.     sort_idx();
  276.     gen_ind();
  277.     MESSAGE("Output written in %s.\n", ind_fn);
  278.     } else
  279.     MESSAGE("Nothing written in %s.\n", ind_fn);
  280.  
  281.     MESSAGE("Transcript written in %s.\n", ilg_fn);
  282.     CLOSE(ind_fp);
  283.     CLOSE(ilg_fp);
  284. #if OS_AMIGA
  285.     free(fns); /* freeing fns vector */
  286. #endif
  287.     EXIT(0);
  288.  
  289.     return (0);                   /* never executed--avoids complaints */
  290.     /* about no return value */
  291. }
  292.  
  293.  
  294. static void
  295. prepare_idx(VOID_ARG)
  296. {
  297.     NODE_PTR ptr = head;
  298.     int     i = 0;
  299.  
  300. #ifdef DEBUG
  301.     totmem += idx_gt * sizeof(FIELD_PTR);
  302.     (void)fprintf(stderr,"prepare_idx(): calloc(%d,%d)\ttotmem = %ld\n",
  303.     idx_gt,sizeof(FIELD_PTR),totmem);
  304. #endif /* DEBUG */
  305.  
  306.     if (head == (NODE_PTR)NULL)
  307.     FATAL("No valid index entries collected.\n", "");
  308.  
  309.     if ((idx_key = (FIELD_PTR *) calloc(idx_gt, sizeof(FIELD_PTR))) == NULL) {
  310.     FATAL("Not enough core...abort.\n", "");
  311.     }
  312.     for (i = 0; i < idx_gt; i++) {
  313.     idx_key[i] = &(ptr->data);
  314.     ptr = ptr->next;
  315.     }
  316. }
  317.  
  318.  
  319. static void
  320. #if STDC
  321. process_idx(char *fn[], int use_stdin, int sty_given, int ind_given,
  322.         int ilg_given, int log_given)
  323. #else
  324. process_idx(fn, use_stdin, sty_given, ind_given, ilg_given, log_given)
  325. char   *fn[];
  326. int     use_stdin;
  327. int     sty_given;
  328. int     ind_given;
  329. int     ilg_given;
  330. int     log_given;
  331. #endif
  332. {
  333.     int     i;
  334.  
  335.     if (fn_no == -1)
  336.     /* use stdin if no input files specified */
  337.     use_stdin = TRUE;
  338.     else {
  339.     check_all(fn[0], ind_given, ilg_given, log_given);
  340.     PUT_VERSION;
  341.     if (sty_given)
  342.         scan_sty();
  343.     if (german_sort && (idx_quote == '"'))
  344. FATAL("Option -g invalid, quote character must be different from '%c'.\n",
  345. '"');
  346.     scan_idx();
  347.     ind_given = TRUE;
  348.     ilg_given = T