This is misc.c in view mode; [Download] [Up]
/* miscellaneous */
#include <curses.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/signal.h>
#include "global.h"
#include "prototypes.h"
#define TIMESTRLEN 30
#define TERMCAPMAXLL 50
#define MAXLINELEN 1024
/* local prototypes */
static void donothing(void);
static void hangup(void);
static void listemus(void);
void erasescreen()
{
clear();
refresh();
}
void donothing()
{
return;
}
void hangup()
{
log("got hangup-signal, terminating...");
endwin();
exit(EXIT_SUCCESS);
}
void initsig()
{
if(signal(SIGHUP, (void (*)())&hangup)==BADSIG)
log("couldn't catch SIGHUP.");
if(signal(SIGINT, (void (*)())&donothing)==BADSIG)
log("couldn't catch SIGINT.");
}
void log(char *fmt, ...)
{
va_list args;
FILE *logh;
time_t t;
char timestr[TIMESTRLEN];
va_start(args,fmt);
if((logh=fopen(LOGFILE,"a"))==0L)
{
printf("\n\nlogfile inaccessible. Couldn't log message:\n");
vprintf(fmt,args);
printf("\n\n");
}
else
{
t=time(0L);
strcpy(timestr, ctime(&t));
timestr[24]='\0';
fprintf(logh,"%s ",timestr);
vfprintf(logh,fmt,args);
fprintf(logh,"\n");
fclose(logh);
if(chmod(LOGFILE,664)!=0)
perror("chmod");
/* chown guest LOGFILE would be nice here, but we
are not setuid root. */
}
va_end(args);
}
bool testemu(char *emulation)
{
FILE *tch;
char tcbuf[TERMCAPMAXLL], *emuocc, *colocc;
bool emufound=FALSE;
if(*emulation=='\0')
{
printf("\nYou _must_ specify an emulation !\n");
return FALSE;
}
if((tch=fopen(TERMCAP,"r"))==0L)
{
log("couldn't open termcap. ( filename '%s' )",TERMCAP);
strcpy(emulation,"vt100");
printf("\nsomething is wrong with my termcap-file.\n"
"Trying to set $TERM to vt100 !\n");
return TRUE;
}
while(((fgets(tcbuf,sizeof(tcbuf),tch))!=0L)&&(!emufound))
if((emuocc=strstr(tcbuf,emulation)) &&
(*tcbuf!='#') &&
((colocc=strchr(tcbuf,':'))==0 ?
TRUE : (emuocc<colocc)))
emufound=TRUE;
fclose(tch);
if(!emufound)
log("Emulation not found: %s.",emulation);
return emufound;
}
#define TERMCAPFS '|'
#define PRINTFIELD 2
void listemus()
{
char shcmd[SHCMDLEN];
printf("\n\n");
sprintf(shcmd,"%s 'BEGIN { FS=\"%c\"; i=0 }"
"(NF>2) {"
"if(index($%i,\":\")!=0) {"
"$%i=substr($%i,1,index($%i,\":\")-1)"
"};"
"i+=(length($%i)+1);"
"if(i>68) { printf(\"\\n\"); i=0 };"
"printf(\"%%s \",$%i)"
"}' < /etc/termcap",
AWKBIN, TERMCAPFS, PRINTFIELD, PRINTFIELD, PRINTFIELD,
PRINTFIELD, PRINTFIELD, PRINTFIELD);
if(system(shcmd))
log("error running awk. ( %s )", AWKBIN);
printf("\n\n");
}
void setemu(bool curseson)
{
char emulation[MAXEMULEN],shcmd[SHCMDLEN];
do
{
do
{
printf("\nplease enter emulation ( such as vt100 )\n"
"or 'l' for a list ( VERY LONG ! ) >");
if(curseson)
refresh();
gets(emulation);
if(strcasecmp(emulation,"l")==0)
listemus();
}while(strcasecmp(emulation,"l")==0);
}while(!testemu(emulation));
if(curseson)
refresh();
setenv("TERM",emulation,TRUE);
unsetenv("TERMCAP");
#ifdef STTY
if(system(STTY))
perror("system");
#endif
sprintf(shcmd,"%s -r >/dev/null",TSETBIN);
if(system(shcmd))
perror("system");
}
/* switch terminal to raw mode then read a single character and reset
terminal to old state. */
char readchar()
{
char c;
struct sgttyb bufold, bufnew;
gtty(0,&bufold);
bufnew=bufold;
bufnew.sg_flags|=RAW;
stty(0,&bufnew);
read(0,&c,1);
stty(0,&bufold);
return(c);
}
int showfile(char *filebody)
{
char c, textfile[FILENAME_MAX], line[MAXLINELEN];
FILE *th;
int linecnt=0;
erasescreen();
sprintf(textfile,"%s%s",TEXTPATH,filebody);
if((th=fopen(textfile,"r"))==0L)
{
log("couldn't access file '%s'", textfile);
return FALSE;
}
do {
while( !feof(th) && (linecnt<LINES-1))
if(fgets(line,sizeof(line),th)!=0)
{
printf("%s",line);
linecnt+=((strlen(line)/COLS)+1);
}
/* FIXME: invert bar */
printf(" >> CR to scroll one screen,"
" q to quit << ");
refresh();
while(((c=getchar())!='q') && (c!=10))
;
linecnt=0;
printf("\r");
refresh();
} while((!feof(th)) && (c!='q'));
fclose(th);
return TRUE;
}
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.