1 /* Citadel/UX support routines */
11 #include <sys/types.h>
12 #include <sys/ioctl.h>
25 void sttybbs(int cmd);
26 void newprompt(char *prompt, char *str, int len);
27 void val_user(char *user);
28 int intprompt(char *prompt, int ival, int imin, int imax);
29 void formout(char *name);
30 void logoff(int code);
31 void set_keepalives(int s);
32 void strprompt(char *prompt, char *str, int len);
33 void newprompt(char *prompt, char *str, int len);
34 void color(int colornum);
36 #define IFAIDE if(axlevel>=6)
37 #define IFNAIDE if (axlevel<6)
39 extern unsigned userflags;
40 extern char *axdefs[7];
41 extern char sigcaught;
42 extern struct CtdlServInfo serv_info;
43 extern char rc_floor_mode;
45 int struncmp(char *lstr, char *rstr, int len)
50 lc=tolower(lstr[pos]);
51 rc=tolower(rstr[pos]);
52 if ((lc==0)&&(rc==0)) return(0);
53 if (lc<rc) return(-1);
62 * check for the presence of a character within a string (returns count)
64 int haschar(char *st, int ch)
68 for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
74 * num_parms() - discover number of parameters...
76 int num_parms(char *source)
81 for (a=0; a<strlen(source); ++a)
82 if (source[a]=='|') ++count;
87 * extract() - extract a parameter from a series of "|" separated...
89 void extract(char *dest, char *source, int parmnum)
95 n = num_parms(source);
102 if ( (parmnum == 0) && (n == 1) ) {
107 while (count++ < parmnum) do {
109 } while( (strlen(buf)>0) && (buf[0]!='|') );
110 if (buf[0]=='|') strcpy(buf,&buf[1]);
111 for (count = 0; count<strlen(buf); ++count)
112 if (buf[count] == '|') buf[count] = 0;
117 * extract_int() - extract an int parm w/o supplying a buffer
119 int extract_int(char *source, int parmnum)
123 extract(buf,source,parmnum);
128 * extract_long() - extract a long parm w/o supplying a buffer
130 long extract_long(char *source, int parmnum)
134 extract(buf,source,parmnum);
138 void back(int spaces) /* Destructive backspace */
141 for (a=1; a<=spaces; ++a) {
142 putc(8,stdout); putc(32,stdout); putc(8,stdout);
146 int yesno(void) { /* Returns 1 for yes, 0 for no */
149 a=inkey(); a=tolower(a);
150 if (a=='y') { printf("Yes\n"); return(1); }
151 if (a=='n') { printf("No\n"); return(0); }
155 int yesno_d(int d) /* Returns 1 for yes, 0 for no, arg is default value */
159 a=inkey(); a=tolower(a);
160 if (a==13) a=(d ? 'y' : 'n');
161 if (a=='y') { printf("Yes\n"); return(1); }
162 if (a=='n') { printf("No\n"); return(0); }
167 void hit_any_key(void) { /* hit any key to continue */
170 printf("%s\r",serv_info.serv_moreprompt);
173 for (a=0; a<strlen(serv_info.serv_moreprompt); ++a)
177 if (b==NEXT_KEY) sigcaught = SIGINT;
178 if (b==STOP_KEY) sigcaught = SIGQUIT;
182 * change a user's access level
189 newprompt("User name: ",who,25);
190 sprintf(buf,"QUSR %s",who);
194 printf("%s\n",&buf[4]);
202 int set_attr(int sval, char *prompt, unsigned int sbit)
209 printf("%45s [", prompt);
211 printf("%3s", ((temp&sbit) ? "Yes":"No"));
215 a=yesno_d(temp&sbit);
218 if (!a) temp=(temp^sbit);
223 * modes are: 0 - .EC command, 1 - .EC for new user,
224 * 2 - toggle Xpert mode 3 - toggle floor mode
226 void enter_config(int mode)
228 int width,height,flags;
235 printf("%s\n",&buf[4]);
239 width = extract_int(&buf[4],0);
240 height = extract_int(&buf[4],1);
241 flags = extract_int(&buf[4],2);
243 if ((mode==0)||(mode==1)) {
245 width = intprompt("Enter your screen width",width,20,255);
246 height = intprompt("Enter your screen height",height,3,255);
248 flags = set_attr(flags,
249 "Are you an experienced Citadel user",US_EXPERT);
250 if ( ((flags&US_EXPERT)==0) && (mode==1))
252 flags = set_attr(flags,
253 "Print last old message on New message request",US_LASTOLD);
254 if ((flags&US_EXPERT)==0) formout("unlisted");
255 flags = set_attr(flags,"Be unlisted in userlog",US_UNLISTED);
256 flags = set_attr(flags,"Suppress message prompts",US_NOPROMPT);
257 if ((flags & US_NOPROMPT)==0)
258 flags = set_attr(flags,"Use 'disappearing' prompts",US_DISAPPEAR);
259 flags = set_attr(flags,
260 "Pause after each screenful of text",US_PAGINATOR);
261 if (rc_floor_mode == RC_DEFAULT) {
262 flags = set_attr(flags,
263 "View rooms by floor",US_FLOORS);
268 if (flags & US_EXPERT) {
269 flags = (flags ^ US_EXPERT);
270 printf("Expert mode now OFF\n");
273 flags = (flags | US_EXPERT);
274 printf("Expert mode now ON\n");
279 if (flags & US_FLOORS) {
280 flags = (flags ^ US_FLOORS);
281 printf("Floor mode now OFF\n");
284 flags = (flags | US_FLOORS);
285 printf("Floor mode now ON\n");
289 sprintf(buf,"SETU %d|%d|%d",width,height,flags);
292 if (buf[0]!='2') printf("%s\n",&buf[4]);
297 * getstring() - get a line of text from a file
298 * ignores lines beginning with "#"
300 int getstring(FILE *fp, char *string)
315 } while(string[0]=='#');
316 return(strlen(string));
319 int pattern(char *search, char *patn) /* Searches for patn in search string */
324 for (a=0; a<strlen(search); ++a)
325 { b=struncmp(&search[a],patn,strlen(patn));
331 void interr(int errnum) /* display internal error as defined in errmsgs */
333 printf("*** INTERNAL ERROR %d\n",errnum);
334 printf("(Press any key to continue)\n");
342 * Check to see if we need to pause at the end of a screen.
343 * If we do, we have to disable server keepalives during the pause because
344 * we are probably in the middle of a server operation and the NOOP command
345 * would confuse everything.
347 int checkpagin(int lp, int pagin, int height)
349 if (pagin!=1) return(0);
350 if (lp>=(height-1)) {
351 set_keepalives(KA_NO);
353 set_keepalives(KA_YES);
360 void strproc(char *string)
364 if (strlen(string)==0) return;
366 /* Convert non-printable characters to blanks */
367 for (a=0; a<strlen(string); ++a) {
368 if (string[a]<32) string[a]=32;
369 if (string[a]>126) string[a]=32;
372 /* Remove leading and trailing blanks */
373 while(string[0]<33) strcpy(string,&string[1]);
374 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
376 /* Remove double blanks */
377 for (a=0; a<strlen(string); ++a) {
378 if ((string[a]==32)&&(string[a+1]==32)) {
379 strcpy(&string[a],&string[a+1]);
384 /* remove characters which would interfere with the network */
385 for (a=0; a<strlen(string); ++a) {
386 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
387 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
388 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
389 if (string[a]==',') strcpy(&string[a],&string[a+1]);
390 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
391 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
397 #ifndef HAVE_STRERROR
399 * replacement strerror() for systems that don't have it
401 char *strerror(int e)
405 sprintf(buf,"errno = %d",e);
411 void progress(long int curr, long int cmax)
413 static long dots_printed;
417 printf(".......................................");
418 printf(".......................................\r");
422 else if (curr==cmax) {
423 printf("\r%79s\n","");
426 a=(curr * 100) / cmax;
428 while (dots_printed < a) {
438 * NOT the same locate_host() in locate_host.c. This one just does a
439 * 'who am i' to try to discover where the user is...
441 void locate_host(char *hbuf)
447 who = (FILE *)popen("who am i","r");
449 strcpy(hbuf,serv_info.serv_fqdn);
456 for (a=0; a<strlen(buf); ++a) {
457 if ((buf[a]=='(')||(buf[a]==')')) ++b;
460 strcpy(hbuf,serv_info.serv_fqdn);
464 for (a=0; a<strlen(buf); ++a) {
466 strcpy(buf,&buf[a+1]);
469 for (a=0; a<strlen(buf); ++a) {
470 if (buf[a]==')') buf[a] = 0;
473 if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
474 else strncpy(hbuf,buf,24);
478 * miscellaneous server commands (testing, etc.)
480 void misc_server_cmd(char *cmd) {
487 while (serv_gets(buf), strcmp(buf,"000")) {
494 newprompt("> ",buf,255);
496 } while(strcmp(buf,"000"));
503 * compute the checksum of a file
505 int file_checksum(char *filename)
511 fp = fopen(filename,"r");
512 if (fp == NULL) return(0);
514 /* yes, this algorithm may allow cksum to overflow, but that's ok
515 * as long as it overflows consistently, which it will.
517 while (ch=getc(fp), ch>=0) {
518 cksum = (cksum + ch);
526 * nuke a directory and its contents
528 int nukedir(char *dirname)
534 dp = opendir(dirname);
539 while (d = readdir(dp), d != NULL) {
540 sprintf(filename, "%s/%s", dirname, d->d_name);
545 return(rmdir(dirname));