|
From: <ho...@us...> - 2004-02-27 00:42:25
|
Update of /cvsroot/ganc/ganc/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8237/src Modified Files: Makefile.am display.c main.c Added Files: display_text.c Log Message: Added possibility to compile without GUI (very basic) --- NEW FILE: display_text.c --- #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <math.h> #include <string.h> #include "include/defines.h" #include "include/functions.h" /*********************************************************************** * * Local Variables * ***********************************************************************/ static const char prompt[]="> "; static char *parse_errors[]={ // Texts of parse errors "unknown variable", // 1 "syntax error", "parse error", "isolated decimal dot", "", // 5 "base out of range", "base conversion error", "invalid argument to LN", "invalid argument to LOG", "invalid argument to factorial", // 10 "invalid argument to SQRT", "invalid argument to ASIN", "invalid argument to ACOS", "division by zero", "overflow", // 15 "NaN", "assignment of constant", "illegal identifier", "illegal constant" }; /*********************************************************************** * * Local Functions * ***********************************************************************/ static char *PreparePrintedResult (void); static int *ExtractSublines (char *line, int *num_sublines); static int LineIsEmpty (char *line); // // prepare text that will be outputed as output // static char *PreparePrintedResult (void) { unsigned long result; int digits, i, d, aux; unsigned int base; char *output_text; int printbase, extra_size=0; base= GetResultOutputFormat (OUTPUT_FORMAT_BASE); printbase= GetResultOutputFormat (OUTPUT_FORMAT_PRINTBASE); if (printbase) extra_size= 3; if (base==10) { // base 10 -> EASY output_text= (char *) malloc (50 + extra_size); sprintf (output_text, "%-15.10Lg\n", ResultValue ()); } else { // other base result= ResultValue (); // calculate number of digits number 'result' takes in base 'base' digits= (int) (log (result)/log (base) + 1); // malloc: 2 for '\n\0' and 2 for safety margin output_text= (char *) malloc (digits + 2 + 2 + extra_size); i= 0; while (result >= base) { d= result % base; if (d<10) output_text[i++]= d + '0'; else output_text[i++]= (d - 10) + 'a'; result/= base; } if (result<10) output_text[i]= result + '0'; else output_text[i]= (result - 10) + 'a'; output_text[i + 1]= '\n'; output_text[i + 2]= '\0'; d= 0; while (d<i) { aux= output_text[i]; output_text[i]= output_text[d]; output_text[d]= aux; --i; d++; } } if (printbase) { i= strlen (output_text) - 1; if (base<10) sprintf (output_text + i, "_%1d\n", base); else sprintf (output_text + i, "_%2d\n", base); } return output_text; } // // Extracts position of sublines (indicated by ';' character) // Returns array with positions of ';' characters, i.e. ends of sublines // and number of sublines found (minimum 1) in 'num_sublines' // array contains at least one element, i.e. end of line // Returned array needs to be freed later // static int *ExtractSublines (char *line, int *num_sublines) { int *sublines; int count, i, j, size; // store size of line size= strlen (line); // find sub-lines embeded in input // run through the input to find ';' characters, store positions in 'sublines' for (i= 0, count= 0; i<size; ++i) if (line[i]==';') ++count; ++count; // at least 1: case of no sub-lines sublines= malloc (count*sizeof (int)); // If some sub-lines are found, store their positions if (count>1) { // there were some ';' for (i= 0, j= 0; i<size; ++i) if (line[i]==';') { sublines[j]= i; ++j; } } // consider end of input as the end of another sub-line // it makes the case with no sub-lines work sublines[count - 1]= size; *num_sublines= count; return sublines; } // // returns 1 if line is empty (0 or more spaces); returns 0 otherwise // static int LineIsEmpty (char *line) { int i; i= strlen (line) - 1; while (i>=0 && line[i]==' ') --i; if (i>=0) return 0; return 1; } #define MAX_LINE_SIZE 200 void TextDisplay (void) { char line[MAX_LINE_SIZE + 1], *extra; int error; int num_sublines, start, i; int *sublines = NULL; char *output_text; int perror, size; while (1) { // read line printf ("%s", prompt); fgets (line, MAX_LINE_SIZE, stdin); line[strlen (line) - 1]= '\0'; // 'exit' entered? if (strcmp (line, "exit")==0) break; // check line is not empty if (LineIsEmpty (line)) continue; // extract sub-lines from input sublines= ExtractSublines (line, &num_sublines); start= 0; // stores starting position of sub-line for (i= 0; i<num_sublines; ++i) { line[sublines[i]]= '\0'; // ignore empty sublines if (LineIsEmpty (line + start)) { start= sublines[i] + 1; continue; } error= ParseTypedExpression (line + start); if (error) { // see if there's any extra text to add to error output perror= ParseError (); extra= ParseErrorExtra (); if (extra==NULL) { size= 20; size+= strlen (parse_errors[perror - 1]); output_text= (char *) malloc (size); //sprintf (output_text, "error: %s\n", parse_errors[perror - 1]); sprintf (output_text, "%s\n", parse_errors[perror - 1]); } else { size= 30; size+= strlen (parse_errors[perror - 1]); size+= strlen (extra); output_text= (char *) malloc (size); //sprintf (output_text, "error: %s: %s\n", parse_errors[perror - 1], extra); sprintf (output_text, "%s: %s\n", parse_errors[perror - 1], extra); free (extra); } } else { // No error, print result output_text= PreparePrintedResult (); } // print output_text (it should already end in '\n') using tag 'result' printf ("%s", output_text); // free memory of output text free (output_text); // set start of next subline start= sublines[i] + 1; } // free memory free (sublines); } } /* Local Variables: c-file-style: "gnu" c-file-offsets: ((case-label . +)) End: */ Index: Makefile.am =================================================================== RCS file: /cvsroot/ganc/ganc/src/Makefile.am,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Makefile.am 20 Jan 2004 01:33:40 -0000 1.4 --- Makefile.am 27 Feb 2004 00:34:07 -0000 1.5 *************** *** 10,23 **** ## GUI extra sources and headers if BUILD_GUI ! GUISRC = support.c interface.c callbacks.c GUIHDR = callbacks.h interface.h support.h else ! GUISRC = GUIHDR = endif ! ganc_SOURCES = main.c display.c evaluate.c syntax.y parser_stuff.c \ ! history.c variables.c $(GUISRC) ganc_LDADD = @LIBS@ @PACKAGE_LIBS@ --- 10,23 ---- ## GUI extra sources and headers if BUILD_GUI ! GUISRC = support.c interface.c callbacks.c display.c GUIHDR = callbacks.h interface.h support.h else ! GUISRC = display_text.c GUIHDR = endif ! ganc_SOURCES = main.c evaluate.c syntax.y parser_stuff.c history.c \ ! variables.c $(GUISRC) ganc_LDADD = @LIBS@ @PACKAGE_LIBS@ Index: display.c =================================================================== RCS file: /cvsroot/ganc/ganc/src/display.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** display.c 3 Feb 2004 21:28:01 -0000 1.2 --- display.c 27 Feb 2004 00:34:07 -0000 1.3 *************** *** 42,46 **** /*********************************************************************** * ! * Local Variables * ***********************************************************************/ --- 42,46 ---- /*********************************************************************** * ! * Local Constants * ***********************************************************************/ Index: main.c =================================================================== RCS file: /cvsroot/ganc/ganc/src/main.c,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** main.c 3 Feb 2004 21:28:01 -0000 1.10 --- main.c 27 Feb 2004 00:34:07 -0000 1.11 *************** *** 13,17 **** #include "support.h" #include "interface.h" ! #endif #include "include/defines.h" --- 13,17 ---- #include "support.h" #include "interface.h" ! #endif /* BUILD_GUI */ #include "include/defines.h" *************** *** 31,35 **** GtkTextBuffer *display_text_buffer; GtkWidget *display; - #endif extern GtkTextMark *start_mark; --- 31,34 ---- *************** *** 39,42 **** --- 38,43 ---- extern GtkTextMark *cursor_mark; + #endif /* BUILD_GUI */ + /*********************************************************************** * *************** *** 91,99 **** GtkWidget *window; GtkTextIter start_iter; ! // create interface window= create_window1 (); - // extract widgets //scrollbar1= lookup_widget (window, "vscrollbar1"); --- 92,99 ---- GtkWidget *window; GtkTextIter start_iter; ! // create interface window= create_window1 (); // extract widgets //scrollbar1= lookup_widget (window, "vscrollbar1"); *************** *** 155,159 **** } ! #endif --- 155,159 ---- } ! #endif /* BUILD_GUI */ *************** *** 163,181 **** int main (int argc, char *argv[]) { - - #ifdef BUILD_GUI - // Start gnome - gnome_init (PACKAGE, VERSION, argc, argv); - #endif - // Initialize everything InitializeVars (); ! // set up gtk interface SetUpInterface (); ! // start gtk loop ! gtk_main (); // clean up everything before leaving CleanUp (); --- 163,184 ---- int main (int argc, char *argv[]) { // Initialize everything InitializeVars (); ! ! #ifdef BUILD_GUI ! ! // Start gnome and gtk stuff ! gnome_init (PACKAGE, VERSION, argc, argv); SetUpInterface (); + gtk_main (); // start gtk loop + + #else /* not BUILD_GUI */ ! TextDisplay (); ! ! #endif /* not BUILD_GUI */ + // clean up everything before leaving CleanUp (); |