Update of /cvsroot/ganc/ganc/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3310/src Modified Files: Makefile.am callbacks.c display.c display_text.c main.c parser_stuff.c syntax.y variables.c Added Files: commands.c display_gtk.c Log Message: -Added support for commands in input line (only 'exit' so far) -Better defined gtk and simple text environments --- NEW FILE: commands.c --- #ifdef HAVE_CONFIG_H #include <config.h> #endif #include "include/defines.h" #include "include/functions.h" #define COMMAND_EXIT 0 #define COMMAND_LS 1 void RunCommand (int com) { switch (com) { case COMMAND_EXIT: TerminateDisplay (); break; case COMMAND_LS: break; default: break; } } --- NEW FILE: display_gtk.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 <gtk/gtk.h> #include <gconf/gconf-client.h> #include <pango/pango.h> #include "include/defines.h" #include "include/functions.h" #include "support.h" /*********************************************************************** * * External Variables * ***********************************************************************/ extern GtkTextBuffer *display_text_buffer; extern GtkWidget *display; /*********************************************************************** * * Global Variables * ***********************************************************************/ GtkTextMark *start_mark; GtkTextMark *end_mark; GtkTextMark *insert_mark; GtkTextMark *selection_mark; GtkTextMark *cursor_mark; /*********************************************************************** * * Local Constants * ***********************************************************************/ #define MAX_NUMBER_LINES 100 /*********************************************************************** * * Local Functions * ***********************************************************************/ // // Inserts text in display at cursor's position // checks insert point is not in forbidden zone // void InsertText (char *text) { GtkTextIter start_iter, sel_iter, insert_iter; gtk_text_buffer_get_selection_bounds (display_text_buffer, &sel_iter, &insert_iter); gtk_text_buffer_get_iter_at_mark (display_text_buffer, &start_iter, start_mark); if (gtk_text_iter_compare (&sel_iter, &start_iter)<0 || gtk_text_iter_compare (&insert_iter, &start_iter)<0) { gtk_text_buffer_get_iter_at_mark (display_text_buffer, &insert_iter, end_mark); gtk_text_buffer_place_cursor (display_text_buffer, &insert_iter); } gtk_text_buffer_insert_at_cursor (display_text_buffer, text, -1); } // // deletes the input line // void ClearInputLine (void) { GtkTextIter start_iter, end_iter; gtk_text_buffer_get_iter_at_mark (display_text_buffer, &start_iter, start_mark); gtk_text_buffer_get_iter_at_mark (display_text_buffer, &end_iter, end_mark); gtk_text_buffer_delete (display_text_buffer, &start_iter, &end_iter); gtk_text_buffer_place_cursor (display_text_buffer, &start_iter); } // // sets geometry hints for display (minimum size, size increment) // void SetDisplayGeometryHints (GtkWindow *window) { GdkGeometry hints; PangoLayout *playout; PangoContext *pcontext; int width, height; // int ww, wh; // pcontext belongs to widget, so we don't free it pcontext= gtk_widget_get_pango_context (GTK_WIDGET (display)); playout= pango_layout_new (pcontext); pango_layout_set_text(playout, "X", 1); pango_layout_get_pixel_size(playout, &width, &height); g_object_unref (G_OBJECT (playout)); // free created playout // geometry hints hints.base_width= 0; hints.base_height= 0; hints.width_inc= width; // x increment size hints.height_inc= height; // y increment size hints.min_width= 20*hints.width_inc; // min x size hints.min_height= 4*hints.height_inc; // min y size gtk_window_set_geometry_hints(window, GTK_WIDGET(display), &hints, GDK_HINT_RESIZE_INC|GDK_HINT_MIN_SIZE| GDK_HINT_BASE_SIZE);//|GDK_HINT_USER_SIZE); // copied from gnome-terminal source (terminal-window.c) GtkRequisition toplevel_request; GtkRequisition widget_request; int w, h, xpad, ypad; int grid_width, grid_height; gtk_widget_set_size_request (GTK_WIDGET (display), 2000, 2000); gtk_widget_size_request (GTK_WIDGET (window), &toplevel_request); gtk_widget_size_request (GTK_WIDGET (display), &widget_request); w= toplevel_request.width - widget_request.width; h= toplevel_request.height - widget_request.height; grid_width= 30; grid_height= 10; xpad= 0; ypad= 0; w+= xpad + width * grid_width; h+= ypad + height * grid_height; //gtk_window_resize (window, w, h); gtk_window_set_default_size (window, w, h); } // // get monospace font (terminal font) from gnome preferences (gconf) // set font for display // void SetDisplayDefaultFont (void) { PangoFontDescription *monospace_font_desc; GConfClient *conf_client; conf_client= gconf_client_get_default (); monospace_font_desc= pango_font_description_from_string (gconf_client_get_string (conf_client, "/desktop/gnome/interface/monospace_font_name", NULL)); gtk_widget_modify_font (display, monospace_font_desc); pango_font_description_free (monospace_font_desc); g_object_unref (G_OBJECT (conf_client)); // free conf_client } // // // void TravelEntryHistory (int direction) { char *line, *line2=NULL; line= GetInputLine (); if (direction==HISTORY_PREVIOUS) { line2= PreviousHistory (line); } else if (direction==HISTORY_NEXT) { line2= NextHistory (line); } free (line); if (line2!=NULL) { SetInputLine (line2); } } // // // void LimitNumberEntries (void) { int lines; GtkTextIter iter, start_iter; GtkTextTag *result_tag; lines= gtk_text_buffer_get_line_count (display_text_buffer); if (lines>MAX_NUMBER_LINES) { gtk_text_buffer_get_iter_at_offset (display_text_buffer, &start_iter, 0); gtk_text_buffer_get_iter_at_offset (display_text_buffer, &iter, 0); result_tag= gtk_text_tag_table_lookup (gtk_text_buffer_get_tag_table (display_text_buffer), "result"); gtk_text_iter_forward_to_tag_toggle (&iter, result_tag); // start result gtk_text_iter_forward_to_tag_toggle (&iter, result_tag); // end result gtk_text_buffer_delete (display_text_buffer, &start_iter, &iter); } } // // Returns text in input line // char *GetInputLine (void) { char *line; GtkTextIter start_iter, end_iter; gtk_text_buffer_get_iter_at_mark (display_text_buffer, &start_iter, start_mark); gtk_text_buffer_get_iter_at_mark (display_text_buffer, &end_iter, end_mark); line= gtk_text_buffer_get_text (display_text_buffer, &start_iter, &end_iter, FALSE); return line; } // // Sets input line to text in 'line' // void SetInputLine (char *line) { GtkTextIter start_iter, end_iter; gtk_text_buffer_get_iter_at_mark (display_text_buffer, &start_iter, start_mark); gtk_text_buffer_get_iter_at_mark (display_text_buffer, &end_iter, end_mark); gtk_text_buffer_delete (display_text_buffer, &start_iter, &end_iter); gtk_text_buffer_insert (display_text_buffer, &start_iter, line, -1); gtk_text_buffer_place_cursor (display_text_buffer, &start_iter); } // // Appends 'text' at the end of everything in display, using format // properties in 'prop' // void AppendDisplayText (char *text, int prop) { GtkTextIter point_iter; gtk_text_buffer_get_iter_at_mark (display_text_buffer, &point_iter, end_mark); if (prop==DISPLAY_TAG_PLAIN) { gtk_text_buffer_insert (display_text_buffer, &point_iter, text, -1); } else if (prop==DISPLAY_TAG_RESULT) { gtk_text_buffer_insert (display_text_buffer, &point_iter, "\n", -1); gtk_text_buffer_insert_with_tags_by_name (display_text_buffer, &point_iter, text, -1, "result", NULL); } else if (prop==DISPLAY_TAG_ERROR) { gtk_text_buffer_insert (display_text_buffer, &point_iter, "\n", -1); gtk_text_buffer_insert_with_tags_by_name (display_text_buffer, &point_iter, text, -1, "error", NULL); } } // // update all the needed stuff after output is finished and a new input line // should be ready to be entered // void UpdateDisplayAfterOutput (void) { GtkTextIter point_iter; PrintPrompt (); // get pos at end of buffer gtk_text_buffer_get_iter_at_mark (display_text_buffer, &point_iter, end_mark); // set 'start' and 'end' marks and position cursor gtk_text_buffer_move_mark (display_text_buffer, start_mark, &point_iter); gtk_text_buffer_move_mark (display_text_buffer, end_mark, &point_iter); gtk_text_buffer_place_cursor (display_text_buffer, &point_iter); // scroll display to show cursor (if necessary) gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (display), insert_mark, 0., FALSE, 0., 0.); } // // Handles a terminate request (typed 'exit' in input) // void TerminateDisplay (void) { gtk_main_quit (); } /* 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.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Makefile.am 27 Feb 2004 00:34:07 -0000 1.5 --- Makefile.am 4 Mar 2004 00:41:54 -0000 1.6 *************** *** 10,14 **** ## 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 --- 10,14 ---- ## GUI extra sources and headers if BUILD_GUI ! GUISRC = support.c interface.c callbacks.c display_gtk.c GUIHDR = callbacks.h interface.h support.h else *************** *** 19,23 **** ganc_SOURCES = main.c evaluate.c syntax.y parser_stuff.c history.c \ ! variables.c $(GUISRC) ganc_LDADD = @LIBS@ @PACKAGE_LIBS@ --- 19,23 ---- ganc_SOURCES = main.c evaluate.c syntax.y parser_stuff.c history.c \ ! variables.c display.c commands.c $(GUISRC) ganc_LDADD = @LIBS@ @PACKAGE_LIBS@ Index: callbacks.c =================================================================== RCS file: /cvsroot/ganc/ganc/src/callbacks.c,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** callbacks.c 4 Feb 2004 00:32:10 -0000 1.12 --- callbacks.c 4 Mar 2004 00:41:54 -0000 1.13 *************** *** 250,257 **** if (event->keyval==GDK_Up || event->keyval==GDK_Down || event->keyval==GDK_Page_Up || event->keyval==GDK_Page_Down) { ! TravelEntryHistory (event->keyval); gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key-press-event"); return TRUE; } // HOME pressed if (event->keyval==GDK_Home) { --- 250,262 ---- if (event->keyval==GDK_Up || event->keyval==GDK_Down || event->keyval==GDK_Page_Up || event->keyval==GDK_Page_Down) { ! if (event->keyval==GDK_Up || event->keyval==GDK_Page_Up) { ! TravelEntryHistory (HISTORY_PREVIOUS); ! } else if (event->keyval==GDK_Down || event->keyval==GDK_Page_Down) { ! TravelEntryHistory (HISTORY_NEXT); ! } gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key-press-event"); return TRUE; } + // HOME pressed if (event->keyval==GDK_Home) { Index: display.c =================================================================== RCS file: /cvsroot/ganc/ganc/src/display.c,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** display.c 27 Feb 2004 03:04:52 -0000 1.4 --- display.c 4 Mar 2004 00:41:54 -0000 1.5 *************** *** 8,50 **** #include <math.h> #include <string.h> - #include <gtk/gtk.h> - #include <gdk/gdkkeysyms.h> - #include <gconf/gconf-client.h> - #include <pango/pango.h> #include "include/defines.h" #include "include/functions.h" - #include "support.h" - /*********************************************************************** - * - * External Variables - * - ***********************************************************************/ - extern GtkTextBuffer *display_text_buffer; - extern GtkWidget *display; - - - /*********************************************************************** - * - * Global Variables - * - ***********************************************************************/ - GtkTextMark *start_mark; - GtkTextMark *end_mark; - GtkTextMark *insert_mark; - GtkTextMark *selection_mark; - GtkTextMark *cursor_mark; - - - - /*********************************************************************** - * - * Local Constants - * - ***********************************************************************/ - #define MAX_NUMBER_LINES 100 - /*********************************************************************** --- 8,16 ---- *************** *** 82,86 **** * ***********************************************************************/ - static void LimitNumberEntries (void); static char *PreparePrintedResult (void); static int *ExtractSublines (char *line, int *num_sublines); --- 48,51 ---- *************** *** 93,183 **** // prints prompt in display // ! void PrintPrompt (GtkTextIter *point_iter) ! { ! gtk_text_buffer_insert (display_text_buffer, point_iter, prompt, -1); ! } ! ! ! // ! // ! // ! void TravelEntryHistory (guint keyval) ! { ! char *line, *line2=NULL; ! GtkTextIter start_iter, end_iter; ! ! gtk_text_buffer_get_iter_at_mark (display_text_buffer, &start_iter, start_mark); ! gtk_text_buffer_get_iter_at_mark (display_text_buffer, &end_iter, end_mark); ! line= gtk_text_buffer_get_text (display_text_buffer, &start_iter, &end_iter, FALSE); ! if (keyval==GDK_Up) { ! line2= PreviousHistory (line); ! } else if (keyval==GDK_Down) { ! line2= NextHistory (line); ! } else if (keyval==GDK_Page_Up) { ! line2= PreviousHistory (line); ! } else if (keyval==GDK_Page_Down) { ! line2= NextHistory (line); ! } ! free (line); ! if (line2!=NULL) { ! gtk_text_buffer_delete (display_text_buffer, &start_iter, &end_iter); ! gtk_text_buffer_insert (display_text_buffer, &start_iter, line2, -1); ! gtk_text_buffer_place_cursor (display_text_buffer, &start_iter); ! } ! } ! ! ! // ! // Inserts text in display at cursor's position ! // checks insert point is not in forbidden zone ! // ! void InsertText (char *text) ! { ! GtkTextIter start_iter, sel_iter, insert_iter; ! ! gtk_text_buffer_get_selection_bounds (display_text_buffer, &sel_iter, &insert_iter); ! gtk_text_buffer_get_iter_at_mark (display_text_buffer, &start_iter, start_mark); ! if (gtk_text_iter_compare (&sel_iter, &start_iter)<0 || ! gtk_text_iter_compare (&insert_iter, &start_iter)<0) { ! gtk_text_buffer_get_iter_at_mark (display_text_buffer, &insert_iter, end_mark); ! gtk_text_buffer_place_cursor (display_text_buffer, &insert_iter); ! } ! gtk_text_buffer_insert_at_cursor (display_text_buffer, text, -1); ! } ! ! ! // ! // deletes the input line ! // ! void ClearInputLine (void) ! { ! GtkTextIter start_iter, end_iter; ! ! gtk_text_buffer_get_iter_at_mark (display_text_buffer, &start_iter, start_mark); ! gtk_text_buffer_get_iter_at_mark (display_text_buffer, &end_iter, end_mark); ! gtk_text_buffer_delete (display_text_buffer, &start_iter, &end_iter); ! gtk_text_buffer_place_cursor (display_text_buffer, &start_iter); ! } ! ! ! // ! // ! // ! static void LimitNumberEntries (void) { ! int lines; ! GtkTextIter iter, start_iter; ! GtkTextTag *result_tag; ! ! lines= gtk_text_buffer_get_line_count (display_text_buffer); ! ! if (lines>MAX_NUMBER_LINES) { ! gtk_text_buffer_get_iter_at_offset (display_text_buffer, &start_iter, 0); ! gtk_text_buffer_get_iter_at_offset (display_text_buffer, &iter, 0); ! result_tag= gtk_text_tag_table_lookup (gtk_text_buffer_get_tag_table (display_text_buffer), "result"); ! gtk_text_iter_forward_to_tag_toggle (&iter, result_tag); // start result ! gtk_text_iter_forward_to_tag_toggle (&iter, result_tag); // end result ! gtk_text_buffer_delete (display_text_buffer, &start_iter, &iter); ! } } --- 58,64 ---- // prints prompt in display // ! void PrintPrompt (void) { ! AppendDisplayText (prompt, DISPLAY_TAG_PLAIN); } *************** *** 294,299 **** void ExpressionReady (void) { ! GtkTextIter point_iter, start_iter; ! gchar *line, *extra; int error; int num_sublines, start, i; --- 175,179 ---- void ExpressionReady (void) { ! char *line, *extra; int error; int num_sublines, start, i; *************** *** 302,308 **** int perror, size; ! gtk_text_buffer_get_iter_at_mark (display_text_buffer, &start_iter, start_mark); ! gtk_text_buffer_get_iter_at_mark (display_text_buffer, &point_iter, end_mark); ! line= gtk_text_buffer_get_text (display_text_buffer, &start_iter, &point_iter, FALSE); // check line is not empty --- 182,186 ---- int perror, size; ! line= GetInputLine (); // check line is not empty *************** *** 311,316 **** // add current line to history AddHistory (line); - - gtk_text_buffer_insert (display_text_buffer, &point_iter, "\n", -1); // extract sub-lines from input --- 189,192 ---- *************** *** 346,360 **** free (extra); } ! gtk_text_buffer_insert_with_tags_by_name ! (display_text_buffer, &point_iter, output_text, -1, "result", "error", NULL); } else { // No error, print result output_text= PreparePrintedResult (); ! gtk_text_buffer_insert_with_tags_by_name ! (display_text_buffer, &point_iter, output_text, -1, "result", NULL); } - // print output_text (it should already end in '\n') using tag 'result' - - // free memory of output text free (output_text); --- 222,231 ---- free (extra); } ! AppendDisplayText (output_text, DISPLAY_TAG_ERROR); } else { // No error, print result output_text= PreparePrintedResult (); ! AppendDisplayText (output_text, DISPLAY_TAG_RESULT); } // free memory of output text free (output_text); *************** *** 365,380 **** // free memory free (sublines); ! g_free (line); ! PrintPrompt (&point_iter); ! ! // set 'start' and 'end' marks and position cursor ! gtk_text_buffer_move_mark (display_text_buffer, start_mark, &point_iter); ! gtk_text_buffer_move_mark (display_text_buffer, end_mark, &point_iter); ! gtk_text_buffer_place_cursor (display_text_buffer, &point_iter); ! ! // scroll display to show cursor (if necessary) ! gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (display), insert_mark, ! 0., FALSE, 0., 0.); // make sure number of entries in display doesn't exceed a maximum --- 236,243 ---- // free memory free (sublines); ! free (line); ! // get ready for next input line ! UpdateDisplayAfterOutput (); // make sure number of entries in display doesn't exceed a maximum *************** *** 383,458 **** - // - // sets geometry hints for display (minimum size, size increment) - // - void SetDisplayGeometryHints (GtkWindow *window) - { - GdkGeometry hints; - PangoLayout *playout; - PangoContext *pcontext; - int width, height; - int ww, wh; - - // pcontext belongs to widget, so we don't free it - pcontext= gtk_widget_get_pango_context (GTK_WIDGET (display)); - playout= pango_layout_new (pcontext); - pango_layout_set_text(playout, "X", 1); - pango_layout_get_pixel_size(playout, &width, &height); - g_object_unref (G_OBJECT (playout)); // free created playout - - // geometry hints - hints.base_width= 0; - hints.base_height= 0; - hints.width_inc= width; // x increment size - hints.height_inc= height; // y increment size - hints.min_width= 20*hints.width_inc; // min x size - hints.min_height= 4*hints.height_inc; // min y size - gtk_window_set_geometry_hints(window, GTK_WIDGET(display), &hints, - GDK_HINT_RESIZE_INC|GDK_HINT_MIN_SIZE| - GDK_HINT_BASE_SIZE);//|GDK_HINT_USER_SIZE); - - // copied from gnome-terminal source (terminal-window.c) - GtkRequisition toplevel_request; - GtkRequisition widget_request; - int w, h, xpad, ypad; - int grid_width, grid_height; - - gtk_widget_set_size_request (GTK_WIDGET (display), 2000, 2000); - gtk_widget_size_request (window, &toplevel_request); - gtk_widget_size_request (GTK_WIDGET (display), &widget_request); - - w= toplevel_request.width - widget_request.width; - h= toplevel_request.height - widget_request.height; - - grid_width= 30; - grid_height= 10; - - xpad= 0; - ypad= 0; - - w+= xpad + width * grid_width; - h+= ypad + height * grid_height; - - //gtk_window_resize (window, w, h); - gtk_window_set_default_size (window, w, h); - } - - - // - // get monospace font (terminal font) from gnome preferences (gconf) - // set font for display - // - void SetDisplayDefaultFont (void) - { - PangoFontDescription *monospace_font_desc; - GConfClient *conf_client; - - conf_client= gconf_client_get_default (); - monospace_font_desc= pango_font_description_from_string (gconf_client_get_string (conf_client, "/desktop/gnome/interface/monospace_font_name", NULL)); - gtk_widget_modify_font (display, monospace_font_desc); - pango_font_description_free (monospace_font_desc); - g_object_unref (G_OBJECT (conf_client)); // free conf_client - } - /* --- 246,249 ---- Index: display_text.c =================================================================== RCS file: /cvsroot/ganc/ganc/src/display_text.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** display_text.c 27 Feb 2004 03:04:52 -0000 1.3 --- display_text.c 4 Mar 2004 00:41:54 -0000 1.4 *************** *** 15,237 **** /*********************************************************************** * ! * 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, FORMAT_STRING, ResultValue ()); ! i= 0; ! while (output_text[i]==' ') ++i; ! strcpy (output_text, output_text + i); ! } 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); ! } } --- 15,113 ---- /*********************************************************************** * ! * Local Constants * ***********************************************************************/ ! #define MAX_LINE_SIZE 200 /*********************************************************************** * ! * Local Variables * ***********************************************************************/ ! static int terminate=0; // ! // main function for text display // ! void StartTextDisplay (void) { ! while (!terminate) { ! PrintPrompt (); ! ExpressionReady (); } ! } // // ! // ! void TravelEntryHistory (int direction) { ! /* nothing can be done for text displays */ ! } ! // ! // ! // ! void LimitNumberEntries (void) ! { ! /* nothing can be done for text displays */ } // ! // Returns text in input line // ! char *GetInputLine (void) { ! char *line; ! line= (char *) malloc (MAX_LINE_SIZE + 1); ! fgets (line, MAX_LINE_SIZE, stdin); ! // eliminate end of line '\n' ! line[strlen (line) - 1]= '\0'; ! ! return line; } + // + // Sets input line to text in 'line' + // + void SetInputLine (char *line) + { + /* nothing can be done in text displays */ + } ! // ! // Appends 'text' at the end of everything in display, using format ! // properties in 'prop' ! // ! void AppendDisplayText (char *text, int prop) ! { ! if (!terminate) printf ("%s", text); ! } ! // ! // update all the needed stuff after output is finished and a new input line ! // should be ready to be entered ! // ! void UpdateDisplayAfterOutput (void) ! { ! /* nothing is required for text displays */ ! } ! // ! // Handles a terminate request (typed 'exit' in input) ! // ! void TerminateDisplay (void) ! { ! terminate= 1; } Index: main.c =================================================================== RCS file: /cvsroot/ganc/ganc/src/main.c,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** main.c 27 Feb 2004 00:34:07 -0000 1.11 --- main.c 4 Mar 2004 00:41:54 -0000 1.12 *************** *** 38,41 **** --- 38,43 ---- extern GtkTextMark *cursor_mark; + static void SetUpInterface (void); + #endif /* BUILD_GUI */ *************** *** 46,50 **** ***********************************************************************/ static void InitializeVars (void); - static void SetUpInterface (void); static void CleanUp (void); --- 48,51 ---- *************** *** 120,123 **** --- 121,126 ---- // create tags gtk_text_buffer_create_tag (display_text_buffer, "error", + "justification", GTK_JUSTIFY_RIGHT, + "right-margin", 10, "foreground", "red", NULL); *************** *** 127,133 **** NULL); ! // create initial mark for start of line gtk_text_buffer_get_iter_at_offset (display_text_buffer, &start_iter, 0); ! PrintPrompt (&start_iter); start_mark= gtk_text_buffer_create_mark (display_text_buffer, "start-line", &start_iter, TRUE); --- 130,136 ---- NULL); ! // create marks gtk_text_buffer_get_iter_at_offset (display_text_buffer, &start_iter, 0); ! start_mark= gtk_text_buffer_create_mark (display_text_buffer, "start-line", &start_iter, TRUE); *************** *** 139,142 **** --- 142,147 ---- selection_mark= gtk_text_buffer_get_mark (display_text_buffer, "selection_bound"); + // print prompt and get display ready for input line + UpdateDisplayAfterOutput (); // signal connections *************** *** 176,180 **** #else /* not BUILD_GUI */ ! TextDisplay (); #endif /* not BUILD_GUI */ --- 181,185 ---- #else /* not BUILD_GUI */ ! StartTextDisplay (); #endif /* not BUILD_GUI */ Index: parser_stuff.c =================================================================== RCS file: /cvsroot/ganc/ganc/src/parser_stuff.c,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** parser_stuff.c 27 Feb 2004 02:44:28 -0000 1.17 --- parser_stuff.c 4 Mar 2004 00:41:54 -0000 1.18 *************** *** 79,82 **** --- 79,87 ---- "" }; + static char *command_names[]={ // Names of defined commands + "exit", //0 + "ls", + "" + }; static int previous_token_closing; // set when last token was a 'syntactically closing' token static int initial_pos; *************** *** 143,146 **** --- 148,167 ---- // + // Checks 'entry' to see if it's a known command + // Returns: ID of command if found, -1 if command not found + // + static int CheckCommand (char *entry) + { + int i=0; + int found=0; + + while (*command_names[i]!='\x0' && !found) + found= !strcmp (entry, command_names[i++]); + if (found) --i; else i= -1; + return i; + } + + + // // Lexical function called by parser // returns the token *************** *** 183,187 **** aux= input[epos]; input[epos]= '\0'; ! if ((id= CheckFunction (input + pos))!=-1) { // it's a function yylval.id= id; return_value= FUNCTION; --- 204,211 ---- aux= input[epos]; input[epos]= '\0'; ! if ((id= CheckCommand (input + pos))!=-1) { // command ! yylval.id= id; ! return_value= COMMAND; ! } else if ((id= CheckFunction (input + pos))!=-1) { // it's a function yylval.id= id; return_value= FUNCTION; Index: syntax.y =================================================================== RCS file: /cvsroot/ganc/ganc/src/syntax.y,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** syntax.y 4 Feb 2004 02:54:32 -0000 1.8 --- syntax.y 4 Mar 2004 00:41:54 -0000 1.9 *************** *** 14,17 **** --- 14,18 ---- %token <name> ENTRY %token <id> FUNCTION + %token <id> COMMAND %token <name> VARIABLE %token <id> LEX_ERROR *************** *** 41,44 **** --- 42,46 ---- | exp ':' ':' format_string { OutputResult ($1); if (CheckForErrors ()) YYABORT; } + | COMMAND { RunCommand ($1); } ; Index: variables.c =================================================================== RCS file: /cvsroot/ganc/ganc/src/variables.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** variables.c 27 Feb 2004 02:44:28 -0000 1.3 --- variables.c 4 Mar 2004 00:41:54 -0000 1.4 *************** *** 339,342 **** --- 339,351 ---- + // + // Prints variable list to display + // + void PrintVarList (int filter) + { + + } + + /* Local Variables: |