/* built-in editor for eggdrop */ /* WILL BE REWRITTEN ANY DAY NOW */ /* don't count on it. */ /* please PLEASE use an external editor now -- this one sucks */ #include #include #include #include "eggdrop.h" #ifndef EXT_EDITOR extern int dcc_total; extern struct dcc_t dcc[]; /* editor being used by someone? */ int editing=0; /* current line number */ int curlin=1; /* lines in current file */ int lines=0; /* currently editing file */ char filename[121]; /* structure for holding editing data */ typedef struct linestr { char *s; struct linestr *next; struct linestr *prev; } line; /* the current file */ line *edit=NULL; /* memory expected to be used by this module */ int expmem_edit() { int tot; line *l=edit; tot=0; while (l!=NULL) { tot+=sizeof(line)+strlen(l->s)+1; l=l->next; } return tot; } /* free up the buffer memory */ void free_edit() { line *l,*x; l=edit; while (l!=NULL) { if (l->s != NULL) nfree(l->s); x=l; l=l->next; nfree(x); } edit=NULL; } /* load file into buffer (assuming buffer is empty!) */ void load_edit(fn) char *fn; { FILE *f; line *l; char s[81]; strcpy(filename,fn); lines=0; f=fopen(fn,"r"); if (f==NULL) { edit=NULL; return; } edit=(line *)nmalloc(sizeof(line)); edit->prev=NULL; edit->next=NULL; edit->s=NULL; l=edit; while (!feof(f)) { fgets(s,81,f); if (!feof(f)) { if (s[strlen(s)-1]=='\n') s[strlen(s)-1]=0; l->s=(char *)nmalloc(strlen(s)+1); strcpy(l->s,s); l->next=(line *)nmalloc(sizeof(line)); l->next->prev=l; l=l->next; l->next=NULL; l->s=NULL; lines++; } } fclose(f); l->prev->next=NULL; nfree(l); } /* save edit buffer to file */ void save_edit(fn) char *fn; { FILE *f; line *l; char s[121]; if (edit==NULL) { unlink(fn); return; } f=fopen(fn,"w"); if (f==NULL) return; l=edit; while (l!=NULL) { if (l->s != NULL) fprintf(f,"%s\n",l->s); else fprintf(f,"\n"); l=l->next; } fclose(f); } /* show edit buffer to dcc user */ void show_edit(z) int z; { int i; line *l=edit; if (curlin==0) tprintf(z,"*[--]\n"); i=0; while (l!=NULL) { i++; if (l->s!=NULL) tprintf(z,"%c[%2d] %s\n",(i==curlin)?'*':' ',i,l->s); else tprintf(z,"[%2d]->blank\n",i); l=l->next; } } /* get line by line # */ line *get_line(n) int n; { line *l=edit; while (n>1) { if (l!=NULL) l=l->next; n--; } return l; } void leave_edit(idx,z) int idx,z; { int i; free_edit(); tprintf(z,"Returning to the party line...\n"); dcc[idx].type=DCC_CHAT; for (i=0; i\n"); return; } if (editing) { tprintf(z,"Sorry, editor is currently in use.\n"); return; } editing=1; tprintf(z,"Opening file '%s' for editing...\n",fn); load_edit(fn); curlin=lines; /* show_edit(z); */ if (lines) { tprintf(z,"Read %d lines.\n",lines); l=get_line(curlin); tprintf(z,"*[%2d] %s\n",curlin,l->s); } else tprintf(z,"New file!\n"); dcc[idx].type=DCC_EDIT; for (i=0; i0) { j=atoi(&buf[1]); l=get_line(j); if (l==NULL) tprintf(z,"--- Invalid line number.\n"); else { curlin=j; tprintf(z,"*[%2d] %s\n",curlin,l->s); } buf[1]='.'; } code=buf[1]; if ((code>='A') && (code<='Z')) code=code-'A'+'a'; split(NULL,buf); if (code=='h') { tprintf(z,"--- Internal editor commands:\n"); tprintf(z,".h help (this)\n"); tprintf(z,".l list contents of editor\n"); tprintf(z,".+ move one line forward\n"); tprintf(z,".- move one line backward\n"); tprintf(z,".<#> move to line number <#>\n"); tprintf(z,".d delete current line\n"); tprintf(z,".r xx/yy replace 'xx' with 'yy' on the current line\n"); tprintf(z,".q quit (without saving)\n"); tprintf(z,".s save and quit\n"); tprintf(z,"Text typed in is added AFTER the current line.\n"); tprintf(z,"--- End of help\n"); } else if (code=='q') leave_edit(idx,z); else if (code=='s') { if (edit==NULL) tprintf(z,"--- Erasing file '%s'...\n",filename); else tprintf(z,"--- Saving file '%s'...\n",filename); save_edit(filename); leave_edit(idx,z); } else if (code=='l') show_edit(z); else if (code=='+') { if (curlin==0) { if (edit==NULL) tprintf(z,"--- No lines in buffer!\n"); else { curlin++; tprintf(z,"*[ 1] %s\n",edit->s); } } else { l=get_line(curlin); if (l->next==NULL) tprintf(z,"--- Already at last line.\n"); else { curlin++; l=l->next; tprintf(z,"*[%2d] %s\n",curlin,l->s); } } } else if (code=='-') { if (curlin==0) tprintf(z,"--- Already at first line.\n"); else { l=get_line(curlin); if (l->prev==NULL) { curlin=0; tprintf(z,"*[--] \026(head of file)\026\n"); } else { curlin--; l=l->prev; tprintf(z,"*[%2d] %s\n",curlin,l->s); } } } else if (code=='d') { if (edit==NULL) tprintf(z,"--- No lines to delete!\n"); else { if (curlin==0) tprintf(z,"--- Not on a line.\n"); else { l=get_line(curlin); if (l->prev != NULL) l->prev->next=l->next; if (l->next != NULL) l->next->prev=l->prev; if (l->next == NULL) curlin--; if (l==edit) edit=l->next; if (l->s != NULL) nfree(l->s); nfree(l); lines--; if (curlin) { l=get_line(curlin); tprintf(z,"*[%2d] %s\n",curlin,l->s); } else tprintf(z,"--- Editor buffer is now empty.\n"); } } } else if (code=='r') { char new[512],old[512],*p; if (edit==NULL) tprintf(z,"No text in the editor...\n"); strcpy(new,buf); /* fix quoted stuff */ for (i=0; is,old); if (p==NULL) tprintf(z,"String not found.\n"); else { strcat(new,(char *)(p+strlen(old))); *p=0; strcpy(old,l->s); nfree(l->s); l->s=(char *)nmalloc(strlen(old)+strlen(new)+1); strcpy(l->s,old); strcat(l->s,new); tprintf(z,"*[%2d] %s\n",curlin,l->s); } } } else if (code=='.'); /* ignore */ else tprintf(z,"--- ? (Try .h)\n"); } else { /* insert into current marker */ if (curlin==0) { ll=edit; edit=(line *)nmalloc(sizeof(line)); edit->next=ll; edit->prev=NULL; l=edit; } else { l=get_line(curlin); ll=l->next; l->next=(line *)nmalloc(sizeof(line)); l->next->prev=l; l->next->next=ll; l=l->next; } l->s=(char *)nmalloc(strlen(buf)+1); strcpy(l->s,buf); curlin++; tprintf(z,"*[%2d] %s\n",curlin,l->s); } } #endif /* ifndef EXT_EDITOR */