/* Key routines extracted from: * scwmrepl.c,v 1.19 2000/01/22 21:13:16 gjb Exp $ * Copyright (C) 1997-2000, Maciej Stachowiak and Greg J. Badros * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this software; see the file COPYING.GPL. If not, write to * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include #include #include #include #include #include #ifdef HAVE_READLINE #include #ifdef HAVE_HISTORY #include #endif /* HAVE_HISTORY */ #endif /* HAVE_READLINE */ #ifdef HAVE_READLINE char *scwm_complete(char *text, int state) { static char *result=NULL; static char *last=NULL; static char *completions[1024]; extern Display *display; extern Window w; if (!state) { char *szSymbol; char *query, *output, *error; unsigned n; if (!last || strcmp(last,text)!=0) { if (last) { g_free(last); } last=strdup(text); if (result) { g_free(result); output = NULL; } query = malloc(strlen(text)+30); sprintf(query,"(apropos-internal \"^%s\")",text); result = xgexec_exec_full(display, w, query, &output, &error); if (error) { if (strlen(error) > 0) fprintf(stderr,"Got error querying apropos-internal for completion: %s",error); g_free(error); error = NULL; } if (output) { g_free(output); output = NULL; } free(query); query = NULL; } /* result is something like "(documentation documentation-debug doc-files)" */ szSymbol = strtok(result+1," \t)"); n=0; while (n<1023 && szSymbol) { completions[n++] = strdup(szSymbol); szSymbol = strtok(NULL," \t)"); } completions[n]=NULL; } return completions[state]; } void init_readline() { rl_completion_entry_function=(Function *)scwm_complete; } #endif int appending_fgets(char **sofar) { #ifdef HAVE_READLINE char *buffer; unsigned pos,len; buffer=readline("gwave> "); if (buffer==NULL) { return 0; } len=strlen(buffer); #if HAVE_HISTORY if (len>0) { add_history(buffer); } #endif pos=strlen(*sofar); *sofar=g_realloc(*sofar,pos+len+2); strncpy(*sofar+pos,buffer,len); (*sofar)[pos+len]='\n'; (*sofar)[pos+len+1]=0; #else char buffer [512]; fputs("gwave> ", stdout); do { fgets(buffer, 512, stdin); if (strlen(buffer)==0) { return 0; } *sofar=g_realloc(*sofar,strlen(*sofar)+strlen(buffer)+1); strcat(*sofar,buffer); } while (buffer[strlen(buffer)-1]!='\n'); #endif return 1; } int check_balance(char *expr) { /* If you think _this_ is hairy, try doing it for C statements. */ int i; int end; int non_whitespace_p=0; int paren_count=0; int prev_separator=1; int quote_wait=0; end=strlen(expr); i=0; while (i