1 /* Citadel/UX support routines */
10 #include <sys/types.h>
11 #include <sys/ioctl.h>
24 void sttybbs(int cmd);
25 void newprompt(char *prompt, char *str, int len);
26 void val_user(char *user);
27 int intprompt(char *prompt, int ival, int imin, int imax);
28 void formout(char *name);
29 void logoff(int code);
30 void set_keepalives(int s);
31 void strprompt(char *prompt, char *str, int len);
32 void newprompt(char *prompt, char *str, int len);
33 void color(int colornum);
35 #define IFAIDE if(axlevel>=6)
36 #define IFNAIDE if (axlevel<6)
38 extern unsigned userflags;
39 extern char *axdefs[7];
40 extern char sigcaught;
41 extern struct CtdlServInfo serv_info;
42 extern char rc_floor_mode;
44 int struncmp(char *lstr, char *rstr, int len)
49 lc=tolower(lstr[pos]);
50 rc=tolower(rstr[pos]);
51 if ((lc==0)&&(rc==0)) return(0);
52 if (lc<rc) return(-1);
61 * check for the presence of a character within a string (returns count)
63 int haschar(char *st, int ch)
67 for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
73 * num_parms() - discover number of parameters...
75 int num_parms(char *source)
80 for (a=0; a<strlen(source); ++a)
81 if (source[a]=='|') ++count;
86 * extract() - extract a parameter from a series of "|" separated...
88 void extract(char *dest, char *source, int parmnum)
94 n = num_parms(source);
101 if ( (parmnum == 0) && (n == 1) ) {
106 while (count++ < parmnum) do {
108 } while( (strlen(buf)>0) && (buf[0]!='|') );
109 if (buf[0]=='|') strcpy(buf,&buf[1]);
110 for (count = 0; count<strlen(buf); ++count)
111 if (buf[count] == '|') buf[count] = 0;
116 * extract_int() - extract an int parm w/o supplying a buffer
118 int extract_int(char *source, int parmnum)
122 extract(buf,source,parmnum);
127 * extract_long() - extract a long parm w/o supplying a buffer
129 long extract_long(char *source, int parmnum)
133 extract(buf,source,parmnum);
137 void back(int spaces) /* Destructive backspace */
140 for (a=1; a<=spaces; ++a) {
141 putc(8,stdout); putc(32,stdout); putc(8,stdout);
145 int yesno(void) { /* Returns 1 for yes, 0 for no */
148 a=inkey(); a=tolower(a);
149 if (a=='y') { printf("Yes\n"); return(1); }
150 if (a=='n') { printf("No\n"); return(0); }
154 int yesno_d(int d) /* Returns 1 for yes, 0 for no, arg is default value */
158 a=inkey(); a=tolower(a);
159 if (a==13) a=(d ? 'y' : 'n');
160 if (a=='y') { printf("Yes\n"); return(1); }
161 if (a=='n') { printf("No\n"); return(0); }
166 void hit_any_key(void) { /* hit any key to continue */
169 printf("%s\r",serv_info.serv_moreprompt);
172 for (a=0; a<strlen(serv_info.serv_moreprompt); ++a)
176 if (b==NEXT_KEY) sigcaught = SIGINT;
177 if (b==STOP_KEY) sigcaught = SIGQUIT;
181 * change a user's access level
188 newprompt("User name: ",who,25);
189 sprintf(buf,"QUSR %s",who);
193 printf("%s\n",&buf[4]);
201 int set_attr(int sval, char *prompt, unsigned int sbit)
208 printf("%45s [", prompt);
210 printf("%3s", ((temp&sbit) ? "Yes":"No"));
214 a=yesno_d(temp&sbit);
217 if (!a) temp=(temp^sbit);
222 * modes are: 0 - .EC command, 1 - .EC for new user,
223 * 2 - toggle Xpert mode 3 - toggle floor mode
225 void enter_config(int mode)
227 int width,height,flags;
234 printf("%s\n",&buf[4]);
238 width = extract_int(&buf[4],0);
239 height = extract_int(&buf[4],1);
240 flags = extract_int(&buf[4],2);
242 if ((mode==0)||(mode==1)) {
244 width = intprompt("Enter your screen width",width,20,255);
245 height = intprompt("Enter your screen height",height,3,255);
247 flags = set_attr(flags,
248 "Are you an experienced Citadel user",US_EXPERT);
249 if ( ((flags&US_EXPERT)==0) && (mode==1))
251 flags = set_attr(flags,
252 "Print last old message on New message request",US_LASTOLD);
253 if ((flags&US_EXPERT)==0) formout("unlisted");
254 flags = set_attr(flags,"Be unlisted in userlog",US_UNLISTED);
255 flags = set_attr(flags,"Suppress message prompts",US_NOPROMPT);
256 if ((flags & US_NOPROMPT)==0)
257 flags = set_attr(flags,"Use 'disappearing' prompts",US_DISAPPEAR);
258 flags = set_attr(flags,
259 "Pause after each screenful of text",US_PAGINATOR);
260 if (rc_floor_mode == RC_DEFAULT) {
261 flags = set_attr(flags,
262 "View rooms by floor",US_FLOORS);
267 if (flags & US_EXPERT) {
268 flags = (flags ^ US_EXPERT);
269 printf("Expert mode now OFF\n");
272 flags = (flags | US_EXPERT);
273 printf("Expert mode now ON\n");
278 if (flags & US_FLOORS) {
279 flags = (flags ^ US_FLOORS);
280 printf("Floor mode now OFF\n");
283 flags = (flags | US_FLOORS);
284 printf("Floor mode now ON\n");
288 sprintf(buf,"SETU %d|%d|%d",width,height,flags);
291 if (buf[0]!='2') printf("%s\n",&buf[4]);
296 * getstring() - get a line of text from a file
297 * ignores lines beginning with "#"
299 int getstring(FILE *fp, char *string)
314 } while(string[0]=='#');
315 return(strlen(string));
318 int pattern(char *search, char *patn) /* Searches for patn in search string */
323 for (a=0; a<strlen(search); ++a)
324 { b=struncmp(&search[a],patn,strlen(patn));
330 void interr(int errnum) /* display internal error as defined in errmsgs */
332 printf("*** INTERNAL ERROR %d\n",errnum);
333 printf("(Press any key to continue)\n");
341 * Check to see if we need to pause at the end of a screen.
342 * If we do, we have to disable server keepalives during the pause because
343 * we are probably in the middle of a server operation and the NOOP command
344 * would confuse everything.
346 int checkpagin(int lp, int pagin, int height)
348 if (pagin!=1) return(0);
349 if (lp>=(height-1)) {
350 set_keepalives(KA_NO);
352 set_keepalives(KA_YES);
359 void strproc(char *string)
363 if (strlen(string)==0) return;
365 /* Convert non-printable characters to blanks */
366 for (a=0; a<strlen(string); ++a) {
367 if (string[a]<32) string[a]=32;
368 if (string[a]>126) string[a]=32;
371 /* Remove leading and trailing blanks */
372 while(string[0]<33) strcpy(string,&string[1]);
373 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
375 /* Remove double blanks */
376 for (a=0; a<strlen(string); ++a) {
377 if ((string[a]==32)&&(string[a+1]==32)) {
378 strcpy(&string[a],&string[a+1]);
383 /* remove characters which would interfere with the network */
384 for (a=0; a<strlen(string); ++a) {
385 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
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]);
396 #ifndef HAVE_STRERROR
398 * replacement strerror() for systems that don't have it
400 char *strerror(int e)
404 sprintf(buf,"errno = %d",e);
410 void progress(long int curr, long int cmax)
412 static long dots_printed;
416 printf(".......................................");
417 printf(".......................................\r");
421 else if (curr==cmax) {
422 printf("\r%79s\n","");
425 a=(curr * 100) / cmax;
427 while (dots_printed < a) {
437 * NOT the same locate_host() in locate_host.c. This one just does a
438 * 'who am i' to try to discover where the user is...
440 void locate_host(char *hbuf)
446 who = (FILE *)popen("who am i","r");
448 strcpy(hbuf,serv_info.serv_fqdn);
455 for (a=0; a<strlen(buf); ++a) {
456 if ((buf[a]=='(')||(buf[a]==')')) ++b;
459 strcpy(hbuf,serv_info.serv_fqdn);
463 for (a=0; a<strlen(buf); ++a) {
465 strcpy(buf,&buf[a+1]);
468 for (a=0; a<strlen(buf); ++a) {
469 if (buf[a]==')') buf[a] = 0;
472 if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
473 else strncpy(hbuf,buf,24);
477 * miscellaneous server commands (testing, etc.)
479 void misc_server_cmd(char *cmd) {
486 while (serv_gets(buf), strcmp(buf,"000")) {
493 newprompt("> ",buf,255);
495 } while(strcmp(buf,"000"));
502 * compute the checksum of a file
504 int file_checksum(char *filename)
510 fp = fopen(filename,"r");
511 if (fp == NULL) return(0);
513 /* yes, this algorithm may allow cksum to overflow, but that's ok
514 * as long as it overflows consistently, which it will.
516 while (ch=getc(fp), ch>=0) {
517 cksum = (cksum + ch);
525 * nuke a directory and its contents
527 int nukedir(char *dirname)
533 dp = opendir(dirname);
538 while (d = readdir(dp), d != NULL) {
539 sprintf(filename, "%s/%s", dirname, d->d_name);
544 return(rmdir(dirname));