home *** CD-ROM | disk | FTP | other *** search
/ Phoenix Heaven Sunny 2 / APPARE2.BIN / oh_towns / dic / src / main.c < prev    next >
Text File  |  1995-06-20  |  12KB  |  526 lines

  1. /*************************************************************
  2.  
  3.     CD辞書検索プログラム
  4.  
  5. **************************************************************/
  6. #include    "defs.h"
  7. #include    "getopt.h"
  8.  
  9. #define    VERSION    "%s version 0.23 1993.12.15 Nanno-NET(Ken)\n"
  10.  
  11.     char    *program_name = "dic";
  12.  
  13.     int    all_index_disp = FALSE;
  14.     int    src_index_disp = FALSE;
  15.  
  16.     int    lines_max = DEF_LINE_MAX;
  17.     int    cols_max = DEF_COLS_MAX;
  18.  
  19.     int    towns_flg = TRUE;    /* Towns CD_mode_set call */
  20.     char    *dict_file_name = NULL;
  21.     FILE    *dev_fp = NULL;
  22.     char    *config_file_name = NULL;
  23.  
  24. #ifdef    STDFILE
  25.     char    *dev_unit_name = CD_ROM;
  26. #endif
  27.  
  28. #ifdef    MSCDEX
  29.     int    dev_unit_no = 0;    /* MSCDEX CD Device No. */
  30.     char    *dev_unit_name = NULL;
  31. #endif
  32.  
  33. #ifdef    FMRBIOS
  34.     int    dev_unit_no = 0xC0;    /* Towns CD Device No. */
  35.     char    *dev_unit_name = "C0";
  36. #endif
  37.  
  38.     char    *select_dic_str = NULL;
  39.     int    now_dic_mode = DIC_DEVICE_MODE;
  40.     int    now_dev_id = (-1);
  41.     MENU    *dic_menu = NULL;
  42.     MENU    *now_dic_menu = NULL;
  43.     int    dic_count = 0;
  44.  
  45.     FILE    *ext_out_fp = NULL;
  46.     char    ext_out_file[128];
  47.  
  48.     int    pause_flg = FALSE;
  49.     int    prompt_flg = TRUE;
  50.     int    zenkou_flag = FALSE;
  51.     int    cutof_flag = FALSE;
  52.     int    gaiji_flag = FALSE;
  53.     int    color_flag = FALSE;
  54.     int    bitmap_flag = FALSE;
  55.     int    extdisp_flag = FALSE;
  56.     int    extinput_flag = FALSE;
  57.     int    copr_disp_flg = FALSE;
  58.     int    menu_disp_flg = FALSE;
  59.     int    catalog_disp_flag = FALSE;
  60.     int    tex_out_flag = FALSE;
  61.  
  62. static    struct option opt_tab[] = {
  63.     { 'A',    0,    "catalog" },
  64.     { 'B',  0,    "bitmap" },
  65.     { 'C',    0,    "copr" },
  66.     { 'D',    1,    "dictionary" },
  67.     { 'L',  1,    "log" },
  68.     { 'M',  1,    "map" },
  69.     { 'N',    0,    "non" },
  70.     { 'P',  0,    "noprompt" },
  71.     { 'S',  0,    "noseparate" },
  72.     { 'V',  0,    "version" },
  73. #ifdef    MAKDIC
  74.     { 'W',    1,    "makdic" },
  75. #endif
  76.     { 'X',  0,    "pager" },
  77.     { 'a',    0,    "all_index" },
  78.     { 'c',    1,    "cols" },
  79.     { 'd',    1,    "device" },
  80.     { 'f',    1,    "file" },
  81.     { 'g',  0,    "gaiji" },
  82. #ifdef    HISTORY
  83.     { 'h',  0,    "history" },
  84. #endif
  85.     { 'l',    1,    "line" },
  86.     { 'm',    0,    "menu" },
  87.     { 'n',    0,    "no_towns" },
  88.     { 'p',    0,    "pause" },
  89.     { 'q',    1,    "config" },
  90.     { 's',    0,    "serach_index" },
  91.     { 'x',  0,    "extcolor" },
  92.     { 't',    0,    "tex" },
  93.     { 0,    0,    NULL } };
  94.  
  95. char    *progname(char *prog)
  96. {
  97.     char    *p;
  98.  
  99.     if ( (p = strrchr(prog, '\\')) != NULL )
  100.     prog = p + 1;
  101.     if ( (p = strrchr(prog, '/')) != NULL )
  102.     prog = p + 1;
  103.     for ( p = prog ; *p != '\0' ; p++ )
  104.     *p = tolower(*p);
  105.     if ( (p = strrchr(prog, '.')) != NULL && strcmp(p, ".exe") == 0 )
  106.     *p = '\0';
  107.     return prog;
  108. }
  109. void    usage(char *prog)
  110. {
  111.     int     n;
  112.     static char *help[] = {
  113. "Usage : %s [-adfghmnpqsxABCLMPSVX] [-D dic] <KeyWord...>\n",
  114. "\t[-a] [--all_index]  [-c] [--cols]    [-d] [--device] [-f] [--file]\n",
  115. "\t[-g] [--gaiji]      [-h] [--history] [-l] [--line]   [-m] [--menu]\n",
  116. "\t[-n] [--no_towns]   [-p] [--pause]   [-s] [--serach_index]\n",
  117. "\t[-x] [--extcolor]   [-B] [--bitmap]  [-C] [--copr] [-A] [--catalog]\n",
  118. "\t[-D] [--dictionary] [-L] [--log]     [-M] [--map]  [-P] [--noprompt]\n",
  119. "\t[-S] [--noseparate] [-X] [--pager]   [-V] [--version]\n",
  120. NULL };
  121.  
  122.     for ( n = 0 ; help[n] != NULL ; n++ )
  123.     fprintf(stderr, help[n], prog);
  124. }
  125. int    main_device_open(int mode, char *file)
  126. {
  127.     int     n;
  128.     char    tmp[8];
  129.  
  130.     if ( Cache_flush(name_to_id(file)) )
  131.     return FALSE;
  132.  
  133.     if ( dev_fp != NULL ) {
  134.     fclose(dev_fp);
  135.     dev_fp = NULL;
  136.     }
  137.  
  138.     now_dic_mode = mode;
  139.     file_start_block = 0L;
  140.     file_end_block = 0xFFFFFFFFL;
  141.     comp_dic_mode = FALSE;
  142.  
  143.     if ( now_dic_mode == DIC_FILE_MODE ) {
  144. #ifndef    UNIX
  145.     if ( (dev_fp = fopen(file, "rb")) == NULL )
  146. #else
  147.     if ( (dev_fp = fopen(file, "r")) == NULL )
  148. #endif
  149.         return ERR;
  150.     if ( fread(tmp, 1, 4, dev_fp) == 4 && strncmp(tmp, "EDIC", 4) == 0 )
  151.         comp_dic_mode = TRUE;
  152.     return FALSE;
  153.     }
  154.  
  155. #ifdef    STDFILE
  156.     if ( (dev_fp = fopen(file, "r")) == NULL )
  157.     return ERR;
  158.     if ( fread(tmp, 1, 4, dev_fp) == 4 && strncmp(tmp, "EDIC", 4) == 0 )
  159.     comp_dic_mode = TRUE;
  160. #else
  161.     dev_unit_no = htoi(file);
  162. #endif
  163.  
  164.     for ( n = 0 ; n < 3 ; n++ ) {
  165.     if ( !CD_file_init() )
  166.         break;
  167.     }
  168.  
  169.     if ( n >= 3 )
  170.     return ERR;
  171.  
  172.     return FALSE;
  173. }
  174. int    main_dic_open(MENU *mp)
  175. {
  176.     if ( main_device_open(mp->mode, mp->file) ) {
  177.     fprintf(stderr,"Can't open Device %s\n", mp->file);
  178.     return ERR;
  179.     }
  180.  
  181.     if ( mp->mode == DIC_DEVICE_MODE && CD_file_open(mp->no) ) {
  182.     fprintf(stderr,"Can't open Dictionary %s\n", mp->ttl);
  183.     return ERR;
  184.     }
  185.  
  186.     if ( dic_open() ) {
  187.     fprintf(stderr,"Dictionary Init Error %s\n", mp->ttl);
  188.     return ERR;
  189.     }
  190.  
  191.     if ( gaiji_flag || bitmap_flag )
  192.     Gaiji_init(mp->gaz, mp->gah);
  193.     else
  194.     Gaiji_close();
  195.  
  196.     return FALSE;
  197. }
  198. int    main_menu_set(int mode, char *file,
  199.         char *ttl, int no, int gah, int gaz)
  200. {
  201.     MENU    *tp, *mp;
  202.  
  203.     if ( (mp = (MENU *)malloc(sizeof(MENU))) == NULL )
  204.     return ERR;
  205.  
  206.     mp->next = NULL;
  207.     mp->mode = mode;
  208.     mp->file = strdup(file);
  209.     mp->ttl  = strdup(ttl == NULL ? file:ttl);
  210.     mp->no   = no;
  211.     mp->gah  = gah;
  212.     mp->gaz  = gaz;
  213.  
  214.     if ( dic_menu == NULL )
  215.     dic_menu = mp;
  216.     else {
  217.     tp = dic_menu;
  218.     while ( tp->next != NULL )
  219.         tp = tp->next;
  220.     tp->next = mp;
  221.     }
  222.     dic_count++;
  223.  
  224.     return FALSE;
  225. }
  226. int    main_catalog_set(char *file, int no)
  227. {
  228.     CATALOG    *tp, *cp;
  229.  
  230.     if ( main_device_open(DIC_DEVICE_MODE, file) )
  231.     return ERR;
  232.  
  233.     if ( (tp = catalog_load("CATALOG1")) == NULL &&
  234.      (tp = catalog_load("CATALOG2")) == NULL )
  235.     return ERR;
  236.  
  237.     if ( no == ERR ) {
  238.     for ( cp = tp ; cp != NULL ; cp = cp->next ) {
  239.         if ( main_menu_set(DIC_DEVICE_MODE, file,
  240.             cp->ttl, cp->no, cp->gah, cp->gaz) )
  241.             return ERR;
  242.     }
  243.     } else {
  244.     cp = tp;
  245.     while ( --no > 0 && cp != NULL )
  246.         cp = cp->next;
  247.     if ( cp == NULL ||
  248.          main_menu_set(DIC_DEVICE_MODE, file,
  249.             cp->ttl, cp->no, cp->gah, cp->gaz) )
  250.         return ERR;
  251.     }
  252.  
  253.     catalog_free(tp);
  254.     return FALSE;
  255. }
  256. int    main_dic_list()
  257. {
  258.     int     n = 0;
  259.     MENU    *mp;
  260.  
  261.     mp = dic_menu;
  262.     while ( mp != NULL ) {
  263.     printf("%2d:%s\n", ++n, mp->ttl);
  264.     mp = mp->next;
  265.     }
  266.     return n;
  267. }
  268.  
  269. #define    FUKUSU_MAX    10
  270.  
  271. static    int    fukusu_dic = 1;
  272. static    int    fukusu_tab[FUKUSU_MAX];
  273.  
  274. int    main_dic_select(int no, char *str)
  275. {
  276.     int     n, i;
  277.     MENU    *mp;
  278.     char    tmp[COLS_MAX + 2];
  279.     char    *av[FUKUSU_MAX];
  280.  
  281.     if ( no <= 0 && dic_count > 1 ) {
  282.     for ( ; ; ) {
  283.         if ( str == NULL ) {
  284.         main_dic_list();
  285.             printf("Dic No. ? ");
  286.             fflush(stdout);
  287.             if ( gets(tmp) == NULL )
  288.                 return ERR;
  289.         } else {
  290.         strcpy(tmp, str);
  291.         str = NULL;
  292.         }
  293.  
  294.         n = strcut(FUKUSU_MAX, av, tmp);
  295.         if ( n == 1 ) {
  296.             if ( (no = atoi(av[0])) > 0 && no <= dic_count ) {
  297.             fukusu_dic = 1;
  298.             break;
  299.         }
  300.         fprintf(stderr, "dic: dic select 1-%d\n", dic_count);
  301.  
  302.         } else if ( n > 1 ) {
  303.         for ( i = 0 ; i < n ; i++ ) {
  304.             if ( (no = atoi(av[i])) <= 0 || no > dic_count ) {
  305.             fprintf(stderr, "dic: dic select 1-%d\n", dic_count);
  306.             break;
  307.             }
  308.             fukusu_tab[i] = no;
  309.         }
  310.         if ( i == n ) {
  311.             fukusu_dic = i;
  312.             no = fukusu_tab[0];
  313.             break;
  314.         }
  315.         }
  316.     }
  317.     }
  318.  
  319.     mp = dic_menu;
  320.     for ( n = 1 ; n < no && mp != NULL ; n++ )
  321.     mp = mp->next;
  322.  
  323.     if ( mp == NULL || main_dic_open(mp) )
  324.     return ERR;
  325.  
  326.     now_dic_menu = mp;
  327.     return FALSE;
  328. }
  329. void    main_dic_kensaku(char *str)
  330. {
  331.     int     n;
  332.  
  333.     if ( fukusu_dic > 1 ) {
  334.     for ( n = 0 ; n < fukusu_dic ; n++ ) {
  335.         if ( !main_dic_select(fukusu_tab[n], NULL) ) {
  336.         printf("%2d:%s\n", fukusu_tab[n], now_dic_menu->ttl);
  337.          dic_print(dic_kensaku(str));
  338.         }
  339.     }
  340.     if ( main_dic_select(fukusu_tab[0], NULL) )
  341.         exit(1);
  342.     } else
  343.         dic_print(dic_kensaku(str));
  344. }
  345. int    main(int ac, char *av[])
  346. {
  347.     int     n;
  348.     int     mx = 1;
  349.     int     non_flag = FALSE;
  350.     char    *p;
  351.     char    tmp[COLS_MAX + 2];
  352. #ifdef    MAKDIC
  353.     char    *comp_dic = NULL;
  354. #endif
  355.  
  356. #ifdef    MSDOS
  357.     static char iobuffer[BUFSIZ];
  358.  
  359.     setbuf(stdout, iobuffer);
  360. #endif
  361.  
  362. #ifdef    HAVE_LICO
  363.     cols_lines_get(&cols_max, &lines_max);
  364. #endif
  365.  
  366.     insert_opt(&ac, &av, 1, getenv("DICOPT"));
  367.     program_name = progname(av[0]);
  368.  
  369. #ifdef    MSCDEX
  370.     if ( CD_mscdex_init() ) {
  371.     fprintf(stderr, "MSCDEX get Drive Error\n");
  372.     exit(1);
  373.     }
  374. #endif
  375.  
  376.     for ( ; ; ) {
  377.     while ( (n = getopt_long(ac, av, opt_tab)) != EOF ) {
  378.         switch(n) {
  379.         case 'a': all_index_disp = TRUE; break;
  380.         case 's': src_index_disp = TRUE; break;
  381.         case 'p': pause_flg = TRUE; break;
  382.         case 'd': dev_unit_name = optarg; break;
  383.         case 'D': select_dic_str = optarg; break;
  384.         case 'l': lines_max = atoi(optarg); break;
  385.         case 'c': cols_max = atoi(optarg); break;
  386.         case 'n': towns_flg = FALSE; break;
  387.         case 'C': copr_disp_flg = TRUE; break;
  388.         case 'm': menu_disp_flg = TRUE; break;
  389.         case 'f': dict_file_name = optarg; break;
  390.         case 'L':
  391.         if ( (ext_out_fp = fopen(optarg, "a")) == NULL &&
  392.              (ext_out_fp = fopen(optarg, "w")) == NULL ) {
  393.             fprintf(stderr, "%s: Can't open %s\n",
  394.             program_name, optarg);
  395.             exit(1);
  396.         }
  397.         strcpy(ext_out_file, optarg);
  398.         break;
  399.         case 'P': prompt_flg = FALSE; break;
  400.         case 'S': cutof_flag = TRUE; break;
  401.         case 'g': gaiji_flag = TRUE; break;
  402.         case 'x': color_flag = TRUE; break;
  403.         case 'B': bitmap_flag = TRUE; break;
  404.         case 'M': Gaiji_config(optarg); break;
  405.         case 'X': extdisp_flag = TRUE; break;
  406.         case 'h': extinput_flag = TRUE; break;
  407.         case 'N': non_flag = TRUE; break;
  408.         case 'A': catalog_disp_flag = TRUE; break;
  409.         case 'V': fprintf(stderr, VERSION, program_name); exit(0);
  410.         case 'q': config_file_name = optarg; break;
  411.         case 't': tex_out_flag = TRUE; break;
  412. #ifdef    MAKDIC
  413.         case 'W': comp_dic = optarg; break;
  414. #endif
  415.         default: usage(program_name); exit(1);
  416.         }
  417.     }
  418.     if ( optind >= ac )
  419.         break;
  420.     av[mx++] = av[optind++];
  421.     }
  422.  
  423.     Cache_init();
  424.     Gaiji_tab_init();
  425.  
  426.     if ( !isatty(0) || !isatty(1) ) {
  427.     prompt_flg = FALSE;
  428.     pause_flg = FALSE;
  429.     extdisp_flag = FALSE;
  430.     extinput_flag = FALSE;
  431.     }
  432.  
  433.     if ( config_file_name != NULL ) {
  434.     if ( device_config(config_file_name) )
  435.         exit(1);
  436.  
  437.     } else if ( dict_file_name != NULL ) {
  438.     if ( main_menu_set(DIC_FILE_MODE, dict_file_name, NULL, 0, 0, 0) )
  439.         exit(1);
  440.  
  441.     } else if ( main_catalog_set(dev_unit_name, ERR) ) {
  442.     fprintf(stderr, "dic: catalog load error\n");
  443.     exit(1);
  444.     }
  445.  
  446.     if ( catalog_disp_flag ) {
  447.     main_dic_list();
  448.     if ( mx == 1 && copr_disp_flg == FALSE &&
  449.             menu_disp_flg == FALSE &&
  450.             bitmap_flag   == FALSE &&
  451.             all_index_disp == FALSE )
  452.         exit(0);
  453.     }
  454.  
  455.     if ( main_dic_select(ERR, select_dic_str) ) {
  456.     fprintf(stderr, "dic: dic select error\n");
  457.     exit(1);
  458.     }
  459.  
  460. #ifdef    MAKDIC
  461.     if ( comp_dic != NULL ) {
  462.     if ( compress(TRUE, comp_dic) )
  463.         fprintf(stderr, "dic: makdic error %s\n", comp_dic);
  464.     exit(1);
  465.     }
  466. #endif
  467.  
  468.     if ( bitmap_flag ) {
  469.     Gaiji_zenkaku_bitmap();
  470.     Gaiji_hankaku_bitmap();
  471.     }
  472.  
  473.     if ( all_index_disp ) {
  474.     for ( n = 0 ; n < index_max ; n++ )
  475.         printf("\t%s\n", idx_name(&(index_buf[n])));
  476.     multi_dic_print(multi_top);
  477.     }
  478.  
  479.     if ( copr_disp_flg && IDX_COPR != NULL ) {
  480.     dic_display(IDX_COPR->start_block, 0);
  481.     if ( mx == 1 && menu_disp_flg == FALSE )
  482.         exit(0);
  483.     }
  484.  
  485.     if ( menu_disp_flg && IDX_MENU != NULL ) {
  486.     dic_display(IDX_MENU->start_block, 0);
  487.     if ( mx == 1 )
  488.         exit(0);
  489.     }
  490.  
  491.     if ( mx > 1 ) {
  492.     while ( --mx > 0 )
  493.         main_dic_kensaku(*(++av));
  494.  
  495.     } else if ( non_flag == FALSE ) {
  496.     for ( ; ; ) {
  497.         if ( prompt_flg )
  498.         printf(">");
  499.  
  500.         fflush(stdout);
  501.  
  502. #ifdef    HISTORY
  503.         if ( extinput_flag )
  504.         input(tmp, cols_max - 2);
  505.         else if ( gets(tmp) == NULL )
  506.         break;
  507. #else
  508.         if ( gets(tmp) == NULL )
  509.         break;
  510. #endif
  511.  
  512.         if ( (p = strchr(tmp, '\n')) != NULL )
  513.             *p = '\0';
  514.  
  515.         if ( tmp[0] == '/' ) {
  516.         if ( command(tmp) )
  517.             break;
  518.  
  519.         } else if ( tmp[0] != '\0' )
  520.         main_dic_kensaku(tmp);
  521.     }
  522.     }
  523.  
  524.     exit(0);
  525. }
  526.