home *** CD-ROM | disk | FTP | other *** search
- /* EDBUF.C */
- static char sccsid[] = "@(#)edbuf.c 1.16 93/06/09 Copyright (c)1993 thalerd";
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include "config.h"
- #include "struct.h"
- #include "globals.h"
- #include "macro.h"
- #include "driver.h"
- #include "lib.h"
- #include "item.h"
- #include "edbuf.h"
- #include "help.h"
- #include "files.h"
- static FILE *file;
- static char post;
- static char oldmode;
- static int resp;
-
- /* Regarding mode: NOW USES oldmode
- * Set mode back to RFP for respond command
- * Enter command will set mode to OK itself
- */
-
- /******************************************************************************/
- /* MAIN TEXT ENTRY LOOP */
- /******************************************************************************/
- char /* RETURNS: 1 on success, 0 on failure */
- cfbufr_open(flg) /* ARGUMENTS: */
- short flg; /* File open type (r,w, etc) */
- {
- char buff[MAX_LINE_LENGTH];
-
- sprintf(buff,"%s/cf.buffer",work);
- if ((file=mopen(buff,flg))==NULL) return 0;
- printf("Type \".\" to exit or \":help\".\n");
- return 1;
- }
-
- /******************************************************************************/
- char /* RETURNS: 1 on post, 0 on abort */
- text_loop(new,res) /* ARGUMENTS: (none) */
- int new;
- int res;
- {
- char ok=1,inbuff[MAX_LINE_LENGTH],
- fromname[MAX_LINE_LENGTH],toname[MAX_LINE_LENGTH];
- register int cpid,wpid;
- int statusp;
- struct stat st;
-
- resp = res;
- if ((status & S_MOTIF) || (flags & O_EDALWAYS))
- return edit(work,"cf.buffer",0);
-
- post=0;
-
- /* Fork & setuid down when creating cf.buffer */
- if (cpid=fork()) { /* parent */
- if (cpid<0) return -1; /* error: couldn't fork */
- while ((wpid = wait(&statusp)) != cpid && wpid != -1);
- post = !statusp;
- } else { /* child */
- if (setuid(getuid())) error("setuid","");
- setgid(getgid());
-
- if (new) {
- sprintf(fromname,"%s/cf.buffer",work);
- if (!stat( fromname ,&st)) { /* cf.buffer exists */
- sprintf(toname,"%s/cbf.%d",work,getpid());
- if (rename(fromname,toname))
- error("renaming cf.buffer to ",toname);
- }
- }
-
- printf("%s your %s%s\n",(new) ? "Enter" : "(Continue",
- (resp)? "response" : "text",
- (new) ? ":" : " entry)" );
- if (!cfbufr_open((new)? O_WPLUS : O_APLUS)) return 0; /* "w+" */
-
- oldmode = mode;
- mode = M_TEXT;
- while (mode==M_TEXT && ok) {
- /* For optimization purposes, we do not allow seps in TEXT mode
- * prompt. This could be changed back if confsep would dump
- * out most strings quickly without accessing the disk.
- */
- wputs(TEXT);
- /* print_prompt(mode); */
-
- ok = (ngets(inbuff,st_glob.inp)!=NULL);
- if (ok && (status & S_INT)) {
- status &= ~S_INT; /* Clear interrupt */
- ok = !get_yes((resp)? "Abort response? " : "Abort item? " );
- if (!ok) post= -1;
- }
- /* printf("ok=%d inbuff=%s\n",ok,inbuff); */
- if (!ok) {
- printf("\n");
- mode = oldmode; /* Ctrl-D same as "." */
- post++; /* post on ^D or . don't post on ^C */
- } else if (inbuff[0]==':') {
- if (inbuff[1]) ok = command(inbuff+1,0);
- else ok = command("e",0);
- } else if ((flags & O_DOT) && !strcmp(inbuff,".")) {
- mode = oldmode; /* done */
- post = 1;
- } else { /* Add to file buffer */
- if (fprintf(file,"%s\n",inbuff)<0) ok=0;
- else fflush(file);
- }
- }
- mclose(file);
- exit(!post);
- } /* ENDFORK */
-
- return post;
- }
-
- /* Commands available while in text entry mode */
- static dispatch_t text_cmd[]={
- "q_uit", text_abort,
- "c_ommand", text_done,
- "ok", text_done,
- "p_rint", text_print,
- "e_dit", text_edit,
- "v_isual", text_edit,
- "h_elp", help,
- "?", help,
- "cl_ear", text_clear,
- "em_pty", text_clear,
- "r_ead", text_read,
- "w_rite", text_write,
- 0, 0
- };
-
- /******************************************************************************/
- /* DISPATCH CONTROL TO APPROPRIATE TEXT COMMAND FUNCTION */
- /******************************************************************************/
- char /* RETURNS: 1 on abort, 0 else */
- text_cmd_dispatch(argc,argv) /* ARGUMENTS: */
- int argc; /* Number of arguments */
- char **argv; /* Argument list */
- {
- int i;
-
- for (i=0; text_cmd[i].name; i++)
- if (match(argv[0],text_cmd[i].name))
- return text_cmd[i].func(argc,argv);
-
- /* Command dispatch */
- if (match(argv[0],"d_one") /* same as . on a new line */
- || match(argv[0],"st_op") /* ? */
- || match(argv[0],"ex_it"))/* ?*/{
- mode = oldmode; post = 1; /* mark as done */
- } else {
- printf("Don't understand that!\n\n");
- text_abort(argc,argv);
- }
- return 1;
- }
-
- /******************************************************************************/
- /* READ TEXT FROM A FILE INTO THE BUFFER */
- /******************************************************************************/
- int /* RETURNS: (nothing) */
- text_read(argc,argv) /* ARGUMENTS: */
- int argc; /* Number of arguments */
- char **argv; /* Argument list */
- {
- FILE *fp;
- char buff[MAX_LINE_LENGTH];
-
- /* PicoSpan puts spaces into the filename, we don't */
- if (argc!=2) {
- printf("UNK Syntax: r filename\n");
- return 1;
- }
-
- /* This is done inside the secure portion
- * writing to the cf.buffer file, so it's already secure
- */
- printf("Reading %s\n",argv[1]);
- if ((fp=mopen(argv[1],O_R))==NULL) {
- return 1;
- }
- while (ngets(buff,fp))
- fprintf(file,"%s\n",buff);
- mclose(fp);
- return 1;
- }
-
- /******************************************************************************/
- /* WRITE TEXT IN BUFFER OUT TO A FILE */
- /******************************************************************************/
- int /* RETURNS: (nothing) */
- text_write(argc,argv) /* ARGUMENTS: */
- int argc; /* Number of arguments */
- char **argv; /* Argument list */
- {
- FILE *fp;
- char buff[MAX_LINE_LENGTH];
-
- /* PicoSpan puts spaces into the filename, we don't */
- if (argc!=2) {
- printf("UNK Syntax: w filename\n");
- return 1;
- }
-
- /* This is done inside the secure portion
- * writing to the cf.buffer file, so is already secure
- */
- printf("Writing %s\n",argv[1]);
- if ((fp=mopen(argv[1],O_W))==NULL) { /* use normal umask */
- return 1;
- }
- fseek(file,0L,0);
- while (ngets(buff,file))
- fprintf(fp,"%s\n",buff);
- mclose(fp);
- return 1;
- }
-
- /******************************************************************************/
- /* DUMP TEXT IN BUFFER AND START OVER */
- /******************************************************************************/
- int /* RETURNS: (nothing) */
- text_clear(argc,argv) /* ARGUMENTS: */
- int argc; /* Number of arguments */
- char **argv; /* Argument list */
- {
- mclose(file);
- printf("Enter your %s:\n",(oldmode==M_OK)? "text" : "response" );
- if (!cfbufr_open(O_WPLUS)) /* "w+" */
- mode = oldmode; /* abort */
- else
- mode = M_TEXT;
- return 1;
- }
-
- /******************************************************************************/
- /* REPRINT CURRENT CONTENTS OF BUFFER */
- /******************************************************************************/
- int /* RETURNS: (nothing) */
- text_print(argc,argv) /* ARGUMENTS: */
- int argc; /* Number of arguments */
- char **argv; /* Argument list */
- {
- char buff[MAX_LINE_LENGTH];
- fseek(file,0L,0);
- while (ngets(buff,file) && !(status & S_INT))
- printf(" %s\n",buff);
- return 1;
- }
-
- /******************************************************************************/
- /* INVOKE UNIX EDITOR ON THE BUFFER */
- /******************************************************************************/
- int /* RETURNS: (nothing) */
- text_edit(argc,argv) /* ARGUMENTS: */
- int argc; /* Number of arguments */
- char **argv; /* Argument list */
- {
- char buff[MAX_LINE_LENGTH];
-
- mclose(file);
- edit(work,"cf.buffer",(argv[0][0]=='v')); /* 'v_isual' check */
- printf("(Continue your %s entry)\n",(resp)? "response" : "text" );
- cfbufr_open(O_APLUS); /* a+ */
- return 1;
- }
-
- /******************************************************************************/
- /* ABORT TEXT ENTRY MODE */
- /******************************************************************************/
- int /* RETURNS: (nothing) */
- text_abort(argc,argv) /* ARGUMENTS: */
- int argc; /* Number of arguments */
- char **argv; /* Argument list */
- {
- if (get_yes("Ok to abandon text? "))
- mode = oldmode;
- return 1;
- }
-
- /******************************************************************************/
- /* END TEXT ENTRY MODE AND POST IT */
- /******************************************************************************/
- int /* RETURNS: (nothing) */
- text_done(argc,argv) /* ARGUMENTS: */
- int argc; /* Number of arguments */
- char **argv; /* Argument list */
- {
- /* Main EDB cmd loop */
- mode = M_EDB;
- while (mode==M_EDB && get_command(NULL));
- return 1;
- }
-
- /******************************************************************************/
- /* FIGURE OUT WHAT TO DO WHEN ESCAPING OUT OF TEXT MODE */
- /******************************************************************************/
- char
- edb_cmd_dispatch(argc,argv) /* ARGUMENTS: */
- int argc; /* Number of arguments */
- char **argv; /* Argument list */
- {
- /* Command dispatch */
- if (match(argv[0],"n_on")
- || match(argv[0],"nop_e")) {
- /* printf("Response aborted! Returning to current item.\n");*/
- mode = oldmode;
- } else if (match(argv[0],"y_es")
- || match(argv[0],"ok")) { post = 1; mode = oldmode; }
- else if (match(argv[0],"ed_it")) text_edit(argc,argv);
- else if (match(argv[0],"ag_ain")
- || match(argv[0],"c_ontinue")) {
- mode = M_TEXT;
- printf("(Continue your text entry)\nType \".\" to exit or \":help\".\n");
- } else if (match(argv[0],"pr_int")) text_print(argc,argv);
- else if (match(argv[0],"em_pty")
- || match(argv[0],"cl_ear")) text_clear(argc,argv);
- else return misc_cmd_dispatch(argc,argv);
- return 1;
- }
-