home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
tass.lzh
/
misc.c
< prev
next >
Wrap
Text File
|
1993-01-24
|
11KB
|
535 lines
#include <stdio.h>
#include <signal.h>
#include <pwd.h>
#ifndef OSK
#include <sys/types.h>
#include <sys/stat.h>
#else /* OSK */
#include <types.h>
#include <stat.h>
#endif /* OSK */
#include "tass.h"
extern char *rindex ();
char active_file[LEN];
#ifdef OSK
char spooldir[LEN];
#endif /* OSK */
char homedir[LEN];
char userid[LEN];
char delgroups[LEN];
char newsrc[LEN];
char newnewsrc[LEN];
char indexdir[LEN];
char my_org[LEN]; /* organization */
#ifdef INCSTR
char *incstr;
#endif /* INCSTR */
/*
* Which base note (an index into base[]) does a respnum
* (an index into arts[]) corresponsd to?
*
* In other words, base[] points to an entry in arts[] which is
* the head of a thread, linked with arts[].thread. For any q: arts[q],
* find i such that base[i]->arts[n]->arts[o]->...->arts[q]
*/
which_base(n)
int n;
{
int i, j;
for (i = 0; i < top_base; i++)
for (j = base[i]; j >= 0; j = arts[j].thread)
if (j == n)
return i;
fprintf(stderr, "can't find base article\n");
return 0;
}
/*
* Find how deep in a thread a response is. Start counting at zero
*/
which_resp(n)
int n;
{
int i, j;
int num = 0;
i = which_base(n);
for (j = base[i]; j != -1; j = arts[j].thread)
if (j == n)
break;
else
num++;
return num;
}
/*
* Given an index into base[], find the number of responses for
* that basenote
*/
nresp(n)
int n;
{
int i;
int oldi = -3;
int sum = 0;
assert(n < top_base);
for (i = base[n]; i != -1; i = arts[i].thread) {
assert(i != -2);
assert(i != oldi);
oldi = i;
sum++;
}
return sum - 1;
}
asfail(file, line, cond)
char *file;
int line;
char *cond;
{
fprintf(stderr, "tass: assertion failure: %s (%d): %s\n",
file, line, cond);
exit(1);
}
/*
* Make regular expressions pleasant for the masses: glob them
*/
glob_name(group, grp)
char *group;
char *grp;
{
char *p, *q;
/*
* Prefix the .'s in the group name so they won't be interpreted
* as regular expression commands. Change 'all' into '*'
*/
p = group;
q = grp;
if (strncmp(p, "all", 3) == 0 && (p[3] == '.' || p[3] == '\0')) {
*q++ = '.';
*q++ = '*';
p = &p[3];
}
while (*p != '\0') {
if (*p == '.') {
*q++ = '\\';
*q++ = '.';
p++;
if (strncmp(p, "all", 3) == 0 &&
(p[3] == '.' || p[3] == '\0')) {
*q++ = '.';
*q++ = '*';
p = &p[3];
}
} else if (*p == '*') {
*q++ = '.';
*q++ = '*';
p++;
} else
*q++ = *p++;
}
*q = '\0';
}
/*
* init_selfinfo
* Deterimines users home directory, userid, and a path
* for an rc file in the home directory
*/
init_selfinfo()
{
struct passwd *myentry;
extern struct passwd *getpwuid();
struct stat sb;
char nam[LEN];
char *p;
extern char *getenv();
FILE *fp;
#ifndef OSK
myentry = getpwuid(getuid());
#else /* OSK */
setpwent ();
#ifdef USE_UID
myentry = getpwuid(real_uid () & 0xffff);
#else /* USE_UID */
myentry = getpwuid(getuid () & 0xffff);
#endif /* USE_UID */
endpwent ();
#endif /* OSK */
strcpy(userid, myentry->pw_name);
strcpy(homedir, myentry->pw_dir);
sprintf(newsrc, "%s/.newsrc", homedir);
sprintf(newnewsrc, "%s/.newnewsrc", homedir);
sprintf(delgroups, "%s/.delgroups", homedir);
sprintf(indexdir, "%s/.tindex", homedir);
#ifndef OSK
sprintf(active_file, "%s/active", LIBDIR);
#else /* OSK */
if (!info_str ("MNEWS.LIB", active_file, LEN - 20))
strcpy (active_file, LIBDIR);
strcat (active_file, "/Active");
if (!info_str ("MNEWS.DIR", spooldir, LEN))
strcpy (spooldir, SPOOLDIR);
#endif /* OSK */
#ifdef INCSTR
if (!(incstr = getenv ("INCSTR")))
incstr = "> ";
#endif /* INCSTR */
if (stat(active_file, &sb) >= 0)
goto got_active;
#ifndef OSK
/*
* I hate forgetting to define LIBDIR correctly. Guess a
* couple of likely places if it's not where LIBDIR says it is.
*/
strcpy(active_file, "/usr/lib/news/active");
if (stat(active_file, &sb) >= 0)
goto got_active;
strcpy(active_file, "/usr/local/lib/news/active");
if (stat(active_file, &sb) >= 0)
goto got_active;
strcpy(active_file, "/usr/public/lib/news/active");
if (stat(active_file, &sb) >= 0)
goto got_active;
/*
* Oh well. Revert to what LIBDIR says it is to produce a
* useful error message when read_active() fails later.
*/
sprintf(active_file, "%s/active", LIBDIR);
#endif /* OSK */
got_active:
*my_org = '\0';
p = getenv("ORGANIZATION");
if (p != NULL) {
strcpy(my_org, p);
goto got_org;
}
#ifndef OSK
sprintf(nam, "%s/organization", LIBDIR);
fp = fopen(nam, "r");
if (fp == NULL) {
sprintf(nam, "/usr/lib/news/organization");
fp = fopen(nam, "r");
}
if (fp == NULL) {
sprintf(nam, "/usr/local/lib/news/organization");
fp = fopen(nam, "r");
}
if (fp == NULL) {
sprintf(nam, "/usr/public/lib/news/organization");
fp = fopen(nam, "r");
}
if (fp == NULL) {
sprintf(nam, "/etc/organization");
fp = fopen(nam, "r");
}
if (fp != NULL) {
if (fgets(my_org, LEN, fp) != NULL) {
for (p = my_org; *p && *p != '\n'; p++) ;
*p = '\0';
}
fclose(fp);
}
#endif /* OSK */
got_org:;
}
char *
my_malloc(size)
unsigned size;
{
char *p;
extern char *malloc();
p = malloc(size);
if (p == NULL) {
fprintf(stderr, "tass: out of memory\n");
exit(1);
}
return p;
}
char *
my_realloc(p, size)
char *p;
unsigned size;
{
extern char *malloc();
extern char *realloc();
if (p == NULL)
p = malloc(size);
else
p = realloc(p, size);
if (p == NULL) {
fprintf(stderr, "tass: out of memory\n");
exit(1);
}
return p;
}
char *
str_save(s)
char *s;
{
char *p;
assert(s != NULL);
p = my_malloc(strlen(s) + 1);
strcpy(p, s);
return(p);
}
copy_fp(a, b, prefix)
FILE *a;
FILE *b;
char *prefix;
{
char buf[8192];
while (fgets(buf, 8192, a) != NULL) {
#ifdef MNEWS
conv_charset (buf, strlen (buf));
#endif /* MNEWS */
fprintf(b, "%s%s", prefix, buf);
}
}
char *
get_val(env, def)
char *env; /* Environment variable we're looking for */
char *def; /* Default value if no environ value found */
{
extern char *getenv();
char *ptr;
if ((ptr = getenv(env)) != NULL)
return(ptr);
else
return(def);
}
invoke_editor(nam, line)
char *nam;
int line;
{
char buf[200];
static int first = TRUE;
static char editor[200];
static int type = -1;
char *ptr;
if (first) {
#ifndef OSK
strcpy(editor, get_val("EDITOR", "/usr/bin/vi"));
#else /* OSK */
strcpy(editor, get_val("EDITOR", "me"));
#endif /* OSK */
if (ptr = rindex (editor, '/'))
++ptr;
else
ptr = editor;
if (!strncmp (ptr, "vi", 2))
type = 0;
else if ((!strncmp (ptr, "me", 2)) || (!strncmp (ptr, "emacs")))
type = 1;
first = FALSE;
}
++line;
switch (type) {
case -1:
default:
sprintf(buf, "%s %s", editor, nam);
break;
case 0:
sprintf (buf, "%s +%d %s", editor, line, nam);
break;
case 1:
sprintf (buf, "%s -g=%d %s", editor, line, nam);
break;
}
printf("\r%s", buf);
CleartoEOLN ();
return invoke_cmd(buf);
}
invoke_cmd(nam)
char *nam;
{
int ret;
#ifdef SIGTSTP
void (*susp)();
#endif
Raw(FALSE);
#ifdef USE_UID
setuid(real_uid);
setgid(real_gid);
#endif /* USE_UID */
#ifndef OSK
#ifdef SIGTSTP
susp = signal(SIGTSTP, SIG_DFL);
#endif
ret = system(nam);
#ifdef SIGTSTP
signal(SIGTSTP, susp);
#endif
#else /* OSK */
ret = system (nam);
#endif /* OSK */
#ifdef USE_UID
setuid(tass_uid);
setgid(tass_gid);
#endif /* USE_UID */
Raw(TRUE);
return ret == 0;
}
shell_escape() {
char shell[LEN];
char *p;
#ifdef SIGTSTP
void (*susp)();
#endif
if (!parse_string("!", shell))
strcpy(shell, get_val("SHELL", "/bin/sh"));
for (p = shell; *p && (*p == ' ' || *p == '\t'); p++) ;
if (!*p)
strcpy(shell, get_val("SHELL", "/bin/sh"));
Raw(FALSE);
#ifdef USE_UID
setuid(real_uid);
setgid(real_gid);
#endif /* USE_UID */
fputs("\r\012", stdout);
#ifndef OSK
#ifdef SIGTSTP
susp = signal(SIGTSTP, SIG_DFL);
#endif
system(p);
#ifdef SIGTSTP
signal(SIGTSTP, susp);
#endif
#else /* OSK */
system (p);
#endif /* OSK */
#ifdef USE_UID
setuid(tass_uid);
setgid(tass_gid);
#endif /* USE_UID */
Raw(TRUE);
continue_prompt();
mail_setup();
}
/*
* Find the next unread response in this group
*/
next_unread(n)
int n;
{
while (n >= 0) {
if (arts[n].unread == 1)
return n;
n = next_response(n);
}
return -1;
}
/*
* Find the previous unread response in this thread
*/
prev_unread(n)
int n;
{
while (n >= 0) {
if (arts[n].unread == 1)
return n;
n = prev_response(n);
}
return -1;
}