1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/* WRITE OPTION ROUTINES -- Code to set options from orville.conf file or
* command line.
*/
#include "write.h"
/* SET_OPTS: Given a pointer to a string of one character option flags,
* terminated by a null or by white space, this sets the global variables
* corresponding to those flags. It returns 1 if any of the characters were
* illegal, 0 otherwise.
*/
int set_opts(char *str)
{
for(;;str++)
switch(*str)
{
case 'S':
no_switch= TRUE;
break;
case 'c':
char_by_char= TRUE;
break;
case 'l':
char_by_char= FALSE;
break;
case 'n':
case 'y':
case 's':
tmp_mesg= *str;
break;
case 'f':
f_pipes= FALSE;
break;
case 't':
telegram= TRUE;
break;
case 'r':
ask_root= TRUE;
break;
case 'p':
postpone= TRUE;
break;
case 'v':
fprintf(stderr,"Orville Write version %s\n",version);
break;
case '\0':
case '\n':
case '\t':
case ' ':
return(0);
default:
return(1);
}
}
/* SET_DFLT: Called for each "options" line in the configuration file. It
* is passed the rest of the line after the "options" keyword and any spaces.
* It checks if the filename matchs our program name, and if so, sets the
* options on that line.
*/
void set_dflt(char *p)
{
int len;
/* Does file line start with progname or a star? */
if (*p == '*')
p++;
else
{
len= strlen(progname);
if (!strncmp(progname,p,len))
p= p+len;
else
return;
}
/* Is that name followed by a space character? */
if (*p != ' ' && *p != '\t')
return;
/* Scan the rest of the line for dashes, and set anything after it */
for (p++ ;*p != '\n' && *p != '\0'; p++)
if (*p == '-' && set_opts(p+1))
fprintf(stderr,"%s: Unknown flag on options line in "
ORVILLE_CONF"\n", progname);
}
/* DEFAULT_OPTS: This reads through the orville.conf file loading configuration
* settings and looking for "options" lines that match progname. (Either
* progname itself or a "*"). If a match is found, the rest of the line is
* scanned for strings starting with a "-" that we can interpret as option
* flags. Those flags are set. Then we continue scanning for more matches.
*/
void default_opts(char *argname)
{
/* Strip leading components off invocation name of program */
progname= leafname(argname);
/* Use common reading routine */
readconfig(set_dflt);
}
/* USER_OPTS: This parses the command line arguments. It sets options, and
* also gets the name or tty of the person to write, if given. If there are
* problems in the argument syntax, then it prints a usage message. Note
* that with the telegram option, all arguments after the user name (possibly
* including the ttyname) are stuck into linebuf. telmsg is pointed at the
* part after the first word which might be the target tty rather than part
* of the message.
*/
void user_opts(int argc, char **argv)
{
int i;
char *c;
bool gotname=FALSE;
bool gottty=FALSE;
int wastelegram= telegram;
char *telegram_end= linebuf;
telmsg= NULL;
for (i=1; i<argc; i++)
{
c= argv[i];
if (!(gottty && telegram) && *c == '-')
{
if (*(++c) == '\000')
{
if (gotname)
{
gottty= TRUE;
telmsg= linebuf;
}
else
gotname= TRUE;
}
else if (set_opts(c))
goto usage;
}
else if (!gotname)
{
strncpy(hisname, c, UT_NAMESIZE);
gotname= TRUE;
}
else if (!gottty || telegram)
{
if (!gottty)
{
if (telegram && strlen(c) > UT_LINESIZE)
telmsg= linebuf;
else
strncpy(histty, c, UT_LINESIZE);
gottty= TRUE;
}
if (telegram)
{
if (telegram_end != linebuf)
{
*(telegram_end++)= ' ';
if (telmsg == NULL)
telmsg= telegram_end;
}
while (*c != '\0' & telegram_end < linebuf+LBSIZE-1)
{
*(telegram_end++)= *c;
/* Erase message words so they don't show on "ps" */
/* what needs to be done to do this varies on unixes */
/* but this should work most places */
*(c++)= ' ';
}
}
}
else
goto usage;
}
*(telegram_end++)= '\n';
*telegram_end= '\0';
return;
usage:
if (wastelegram)
printf("usage: %s [-ynsr] user [ttyname] [message...]\n",
progname);
else
printf("usage: %s [-clynsptr] user [ttyname]\n",progname);
done(1);
}
|