ftp.nice.ch/pub/next/text/tex/apps/TeXview-kp-hm01.s.tar.gz#/texview-hm/search.c

This is search.c in view mode; [Download] [Up]

#include "structures.h" /* The copyright notice in that file is included too! */
#include <stdio.h>             /* for FILE and fopen */
#include <sgtty.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <mach/cthreads.h> 
#include <kpathsea/c-ctype.h>
#include <kpathsea/c-fopen.h>
#include <kpathsea/c-pathch.h>
#include <kpathsea/proginit.h>
#include <kpathsea/progname.h>
#include <kpathsea/tex-file.h>
#include <kpathsea/tex-glyph.h>
#include <dpsclient/dpsNeXT.h>
/*
#include <kpathsea/debug.h>
*/

void kpse_init() {
	FILE *tmp;
	char tmpbuf[1000];
	int	i=0;
	int c;

	objc_setMultithreaded(TRUE);

        mgetenv("PATH"); /* to get enviroment up before kpse*/
	tmp = popen("which tex","r");
	while( (c = fgetc(tmp)) != EOF ) tmpbuf[i++] = c;
	pclose(tmp);

/*	if(tmpbuf[0] == '/')
		sprintf(& tmpbuf[i-1], "view");
*/		
        tmpbuf[i-1]=0;  
	kpse_set_progname (tmpbuf);
/*
	    if (debug & DBG_OPEN) KPSE_DEBUG_SET (KPSE_DEBUG_FOPEN);
	    if (debug & DBG_STAT) KPSE_DEBUG_SET (KPSE_DEBUG_STAT);
	    if (debug & DBG_HASH) KPSE_DEBUG_SET (KPSE_DEBUG_HASH);
	    if (debug & DBG_PATHS) KPSE_DEBUG_SET (KPSE_DEBUG_PATHS);
	    if (debug & DBG_EXPAND) KPSE_DEBUG_SET (KPSE_DEBUG_EXPAND);
	    if (debug & DBG_SEARCH) KPSE_DEBUG_SET (KPSE_DEBUG_SEARCH);

 KPSE_DEBUG_SET (KPSE_DEBUG_FOPEN);
 KPSE_DEBUG_SET (KPSE_DEBUG_STAT);
 KPSE_DEBUG_SET (KPSE_DEBUG_HASH);
 KPSE_DEBUG_SET (KPSE_DEBUG_PATHS);
 KPSE_DEBUG_SET (KPSE_DEBUG_EXPAND);
 KPSE_DEBUG_SET (KPSE_DEBUG_SEARCH);
*/


   kpse_init_prog ("tex", 100, "nextscrn", true, "cmr10");
   start_up();
};

static int my_fildes[2];
int my_log_file;
cthread_t my_kpse_thread;

static void exit_thread() {
	putchar(4);
//	fflush(stdout);
//	fflush(stderr);
};

static void start_up() {
    FILE *stream1, *stream2, *stream3; 

	my_log_file=dup(2); 
	if(my_log_file < 3) {
		perror("search.c"); 
		printf("Trying to open my_log_file(%d)\n",my_log_file); 
		exit(5);
	};
	stream2 = fdopen(my_log_file,"w");
	fclose(stdin); 
	fclose(stdout); 
	fclose(stderr);
    
	if (pipe( my_fildes )==0) {
    
	    if(my_fildes[0] != 0 || my_fildes[1] != 1) exit(1);
	    dup2(my_fildes[1],2);
	    stream1 = fdopen(0,"r");
	    stream2 = fdopen(1,"w");
	    stream3 = fdopen(2,"w");
	    if(stream1 != stdin || stream2 != stdout || stream3 != stderr) exit(2);
	    setbuf(stdin ,NULL); 	
	    setbuf(stdout,NULL); 	
	    setbuf(stderr,NULL); 	
	}
	else { perror("! error grabbing pipe") ; exit(3);
	}
}

void run_in_thread (any_t proc, volatile any_t args ) { 
	    my_kpse_thread = cthread_fork( (any_t) proc,(any_t) args);
		ModalConsoleOutput();
	    cthread_join(my_kpse_thread);
};

/******************************************/
/*
 *   The search routine takes a directory list, separated by PATHSEP, and
 *   tries to open a file.  Null directory components indicate current
 *   directory. if the file SUBDIR exists and the file is a font file,
 *   it checks for the file in a subdirectory named the same as the font name.
 *   Returns the open file descriptor if ok, else NULL.
 */
/*
 *
 *   We hope MAXPATHLEN is enough -- only rudimentary checking is done!
 */
extern void error() ;
#ifdef DEBUG
extern integer debug_flag;
#endif  /* DEBUG */

struct threaded_kpse_find_file_args { char filename[1000] ; kpse_file_format_type format; char found_name[1000];};

void threaded_kpse_find_file(struct threaded_kpse_find_file_args *args) {
	char *tmp;
 tmp = kpse_find_file (args->filename, args->format, args->format != kpse_vf_format);
 if(tmp) { strcpy((args->found_name),tmp); free(tmp);}
 else *(args->found_name) = 0;
 exit_thread();
};

FILE *search(format, filename, mode)
    kpse_file_format_type format;
    char *filename, *mode ;
{
    FILE   *ret;
    static struct threaded_kpse_find_file_args args;

  /* Most file looked for through here must exist -- the exception is
     VF's. Bitmap fonts go through pksearch. */

  strcpy(args.filename,filename);
  args.format   = format;
  args.found_name[0]=0;

  run_in_thread ( &threaded_kpse_find_file, &args);

  if (args.found_name[0])
    ret = fopen ( args.found_name, mode);
  else
    ret = NULL;

  return ret;
}               /* end search */



struct threaded_kpse_find_pk_args 
	{ char font_name[1000] ; unsigned dpi; kpse_glyph_file_type font_file; char found_name[1000];};

void threaded_kpse_find_pk(struct threaded_kpse_find_pk_args *args) {
	char *tmp;
 tmp = kpse_find_pk (args->font_name, args->dpi, &(args->font_file) );
 if(tmp)
	{ strcpy((args->found_name),tmp); free(tmp); }
 else *(args->found_name) = 0;
 exit_thread();
};


FILE *pksearch( font_name, dpi, name_ret, dpi_ret)
        char *font_name;
	char *name_ret ;
	halfword dpi;
	halfword *dpi_ret ;
{
  FILE *ret;
  static struct threaded_kpse_find_pk_args args;

  strcpy(args.font_name,font_name);
  args.dpi = dpi;
  args.found_name[0] = 0;
  
  run_in_thread ( &threaded_kpse_find_pk, &args);

  if (args.found_name[0])
    {
      ret = fopen (args.found_name, "r");
      if (!ret)  return ret;
      strcpy (name_ret, args.font_file.name);
      *dpi_ret = (halfword) args.font_file.dpi;
    }
  else
    ret = NULL;

  return(ret);
}               /* end search */


struct threaded_kpse_expand_args 
	{ char *file_name_in ; char *file_name_out; };

void threaded_kpse_expand(struct threaded_kpse_expand_args *args) {
    args->file_name_out = kpse_expand(args->file_name_in);
    exit_thread();
};


char *file_name_expand( char *original)
{
  static struct threaded_kpse_expand_args args;

  args.file_name_in = original;
  
  run_in_thread ( &threaded_kpse_expand, &args);

  return(args.file_name_out);	/* should be freed by reciever */
}               

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.