"Fossies" - the Fresh Open Source Software Archive

Member "ytree-2.00/history.c" (16 Nov 2019, 10962 Bytes) of package /linux/misc/ytree-2.00.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "history.c" see the Fossies "Dox" file reference documentation.

    1 /***************************************************************************
    2  *
    3  * $Header: /usr/local/cvsroot/utils/ytree/history.c,v 1.6 2005/09/07 18:50:33 werner Exp $
    4  *
    5  * Functionkey F2 History
    6  *
    7  ***************************************************************************/
    8 
    9 
   10 #include "ytree.h"
   11 
   12 
   13 
   14 #define MAX_HST_FILE_LINES 50
   15 #define MAX(a,b) (((a) > (b)) ? (a):(b))
   16 
   17 typedef struct _history
   18 {
   19   char             *hst;
   20   struct _history  *next;
   21   struct _history  *prev;
   22 } History;
   23 
   24 
   25 static int total_hist     = 0;
   26 static int cursor_pos     = 0;
   27 static int disp_begin_pos = 0;
   28 static History *Hist = NULL ;
   29 
   30 
   31 
   32 void ReadHistory(char *Filename)
   33 {
   34   FILE *HstFile;
   35   char buffer[BUFSIZ];
   36 
   37   if ( (HstFile = fopen( Filename, "r" ) ) != NULL) {
   38     while( fgets( buffer, sizeof( buffer ), HstFile ) ) 
   39     {
   40         if (strlen(buffer) > 0)
   41         { 
   42           buffer[strlen(buffer) -1] = '\0';
   43           InsHistory(buffer);
   44         }
   45     }
   46     fclose( HstFile );
   47   }
   48   return;
   49 }
   50 
   51 
   52 
   53 void SaveHistory(char *Filename)
   54 {
   55   FILE *HstFile;
   56   int j;
   57   History *hst, *last_hst;
   58   
   59   hst = last_hst = NULL;
   60 
   61   if((hst = Hist)) {
   62     if( (HstFile = fopen( Filename, "w" ) ) != NULL) {
   63       for(j = 0 ; hst && j < MAX_HST_FILE_LINES; j++ ) {
   64         last_hst = hst;
   65         hst = hst->next;
   66       }
   67 
   68       /* write in reverse order */
   69       for(hst=last_hst; hst; hst=hst->prev) {
   70          fputs(hst->hst, HstFile);
   71          fputs("\n", HstFile);
   72       }
   73       fclose( HstFile );
   74     }
   75   }
   76   return;
   77 }
   78 
   79 
   80 void InsHistory( char *NewHst)
   81 {
   82    History *TMP, *TMP2 = NULL;
   83    int flag = 0;
   84 
   85    if (strlen(NewHst)== 0)
   86      return;
   87 
   88    TMP2 = Hist;
   89    for ( TMP = Hist; TMP != NULL; TMP = TMP -> next)
   90    {
   91        if (strcmp(TMP -> hst, NewHst) == 0)
   92        {
   93          if (TMP2 != TMP) 
   94          {
   95             TMP2 -> next = TMP -> next;
   96             TMP -> next = Hist;
   97             Hist = TMP;
   98          }
   99          flag = 1;
  100          break;
  101        };
  102        TMP2 = TMP;
  103    }
  104    
  105    if (flag == 0)
  106    {
  107       if ((TMP=(History *) malloc(sizeof(struct _history))) != NULL)
  108       {
  109          TMP -> next = Hist;
  110      TMP -> prev = NULL;
  111      if(( TMP -> hst = Strdup(NewHst)) == NULL) 
  112      {
  113         free(TMP);
  114         return;
  115      }
  116 
  117          if (Hist != NULL) 
  118          Hist -> prev = TMP;
  119          Hist = TMP;
  120          total_hist++;
  121       }
  122    }
  123    return;
  124 }
  125 
  126 
  127 
  128 void PrintHstEntry(int entry_no, int y, int color, 
  129                    int start_x, int *hide_left, int *hide_right)
  130 {
  131   int     n;
  132   History *pp;
  133   char    buffer[BUFSIZ];
  134   char    *line_ptr;
  135   int     window_width;
  136   int     window_height;
  137   int     ef_window_width;
  138 
  139   
  140   GetMaxYX( history_window, &window_height, &window_width );
  141   ef_window_width = window_width - 2; /* Effektive Window-Width */
  142 
  143 #ifdef NO_HIGHLIGHT
  144   ef_window_width = window_width - 3; /* Effektive Window-Width */
  145 #else
  146   ef_window_width = window_width - 2; /* Effektive Window-Width */
  147 #endif
  148 
  149   *hide_left = *hide_right = 0;
  150 
  151   for(n=0, pp=Hist; pp && (n < entry_no); pp = pp->next)
  152   {
  153     n++; 
  154   }
  155 
  156   if(pp)
  157   {
  158     (void) strncpy( buffer, pp->hst, BUFSIZ - 3);
  159     buffer[BUFSIZ - 3] = '\0';
  160     n = strlen( buffer );
  161     wmove(history_window,y,1);
  162 
  163     if(n <= ef_window_width) {
  164 
  165       /* will completely fit into window */
  166       /*---------------------------------*/
  167 
  168       line_ptr = buffer;
  169     } else {
  170       /* does not completely fit into window;
  171        * ==> use start_x
  172        */
  173 
  174       if(n > (start_x + ef_window_width))
  175         line_ptr = &buffer[start_x];
  176       else
  177         line_ptr = &buffer[n - ef_window_width];
  178 
  179       *hide_left = start_x;
  180       *hide_right = n - start_x - ef_window_width;
  181 
  182       line_ptr[ef_window_width] ='\0';
  183     }
  184 
  185 #ifdef NO_HIGHLIGHT
  186     strcat(line_ptr, (color == HIHST_COLOR) ? " <" : "  ");
  187     WAddStr( history_window, line_ptr );
  188 #else
  189 #ifdef COLOR_SUPPORT 
  190     WbkgdSet(history_window, COLOR_PAIR(color)|A_BOLD);
  191 #else
  192     if(color == HIHST_COLOR)
  193       wattrset( history_window, A_REVERSE );
  194 #endif /* COLOR_SUPPORT */
  195     WAddStr( history_window, line_ptr );
  196 #ifdef COLOR_SUPPORT
  197     WbkgdSet(history_window, COLOR_PAIR(WINHST_COLOR)| A_BOLD);
  198 #else
  199     if(color == HIHST_COLOR)
  200       wattrset( history_window, 0 );
  201 #endif /* COLOR_SUPPORT */
  202 #endif /* NO_HIGHLIGHT */
  203   }
  204   return;
  205 }
  206 
  207 
  208 
  209 
  210 int DisplayHistory()
  211 {
  212   int i, hilight_no, p_y;
  213   int hide_left, hide_right;
  214 
  215   hilight_no = disp_begin_pos + cursor_pos;
  216   p_y = -1;
  217   werase( history_window );
  218   for(i=0; i < HISTORY_WINDOW_HEIGHT; i++)
  219   {
  220     if (disp_begin_pos + i >= total_hist ) break;
  221     if (disp_begin_pos + i != hilight_no )
  222         PrintHstEntry(disp_begin_pos + i, i, HST_COLOR, 
  223                   0, &hide_left, &hide_right);
  224     else
  225       p_y = i;
  226   }
  227   if(p_y >= 0) {
  228     PrintHstEntry(disp_begin_pos + p_y, p_y, HIHST_COLOR,
  229               0, &hide_left, &hide_right);
  230   }
  231   return 0;
  232 }
  233 
  234 
  235 
  236 char *GetHistory()
  237 {
  238   int     ch, tmp;
  239   int     start_x;
  240   char    *RetVal = NULL;
  241   History *TMP;
  242   int     hide_left, hide_right;
  243 
  244 
  245   disp_begin_pos = 0;
  246   cursor_pos     = 0;
  247   start_x        = 0;
  248   /* leaveok(stdscr, TRUE); */
  249   (void) DisplayHistory();
  250 
  251   do
  252   {
  253     RefreshWindow( history_window );
  254     doupdate();
  255     ch = Getch();
  256 
  257     if(ch != -1 && ch != KEY_RIGHT && ch != KEY_LEFT) {
  258       if(start_x) {
  259         start_x = 0;
  260     PrintHstEntry( disp_begin_pos + cursor_pos,
  261                cursor_pos, HIHST_COLOR, 
  262                start_x, &hide_left, &hide_right);
  263       }
  264     }
  265 
  266     switch( ch )
  267     {
  268       case -1:       RetVal = NULL;
  269                      break;
  270 
  271       case ' ':      break;  /* Quick-Key */
  272 
  273       case KEY_RIGHT: start_x++;
  274               PrintHstEntry( disp_begin_pos + cursor_pos,
  275                          cursor_pos, HIHST_COLOR,
  276                              start_x, &hide_left, &hide_right);
  277               if(hide_right < 0) 
  278                 start_x--;
  279               break;
  280       
  281       case KEY_LEFT:  if(start_x > 0)
  282                     start_x--;
  283               PrintHstEntry( disp_begin_pos + cursor_pos,
  284                          cursor_pos, HIHST_COLOR,
  285                              start_x, &hide_left, &hide_right);
  286               break;
  287       
  288       case '\t':
  289       case KEY_DOWN: if (disp_begin_pos + cursor_pos+1 >= total_hist)
  290                  {
  291                beep();
  292              }
  293              else
  294              { if( cursor_pos + 1 < HISTORY_WINDOW_HEIGHT )
  295                {
  296              PrintHstEntry( disp_begin_pos + cursor_pos,
  297                     cursor_pos, HST_COLOR,
  298                                 start_x, &hide_left, &hide_right);
  299              cursor_pos++;
  300              PrintHstEntry( disp_begin_pos + cursor_pos,
  301                     cursor_pos, HIHST_COLOR,
  302                                 start_x, &hide_left, &hide_right);
  303                        }
  304                else
  305                {
  306              PrintHstEntry( disp_begin_pos + cursor_pos, 
  307                     cursor_pos, HST_COLOR,
  308                                 start_x, &hide_left, &hide_right);
  309              scroll( history_window );
  310              disp_begin_pos++;
  311              PrintHstEntry( disp_begin_pos + cursor_pos, 
  312                     cursor_pos, HIHST_COLOR,
  313                                 start_x, &hide_left, &hide_right);
  314                        }
  315              }
  316                      break;
  317       case KEY_BTAB:
  318       case KEY_UP  : if( disp_begin_pos + cursor_pos - 1 < 0 )
  319              {   beep(); }
  320              else
  321              {
  322                if( cursor_pos - 1 >= 0 )
  323                {
  324              PrintHstEntry( disp_begin_pos + cursor_pos, 
  325                     cursor_pos, HST_COLOR,
  326                                 start_x, &hide_left, &hide_right);
  327              cursor_pos--;
  328              PrintHstEntry( disp_begin_pos + cursor_pos,
  329                     cursor_pos, HIHST_COLOR,
  330                                 start_x, &hide_left, &hide_right);
  331                        }
  332                else
  333                {
  334              PrintHstEntry( disp_begin_pos + cursor_pos, 
  335                     cursor_pos, HST_COLOR,
  336                                 start_x, &hide_left, &hide_right);
  337              wmove( history_window, 0, 0 );
  338              winsertln( history_window );
  339              disp_begin_pos--;
  340              PrintHstEntry( disp_begin_pos + cursor_pos, 
  341                     cursor_pos, HIHST_COLOR,
  342                                 start_x, &hide_left, &hide_right);
  343                        }
  344              }
  345                      break;
  346       case KEY_NPAGE:
  347                  if( disp_begin_pos + cursor_pos >= total_hist - 1 )
  348              {  beep();  }
  349              else
  350              {
  351                if( cursor_pos < HISTORY_WINDOW_HEIGHT - 1 )
  352                {
  353              PrintHstEntry( disp_begin_pos + cursor_pos, 
  354                     cursor_pos, HST_COLOR,
  355                                 start_x, &hide_left, &hide_right);
  356                  if( disp_begin_pos + HISTORY_WINDOW_HEIGHT > total_hist  - 1 )
  357                cursor_pos = total_hist - disp_begin_pos - 1;
  358              else
  359                cursor_pos = HISTORY_WINDOW_HEIGHT - 1;
  360              PrintHstEntry( disp_begin_pos + cursor_pos, 
  361                     cursor_pos, HIHST_COLOR,
  362                                 start_x, &hide_left, &hide_right);
  363                }
  364                else
  365                {
  366              if( disp_begin_pos + cursor_pos + HISTORY_WINDOW_HEIGHT < total_hist )
  367              {
  368                disp_begin_pos += HISTORY_WINDOW_HEIGHT;
  369                cursor_pos = HISTORY_WINDOW_HEIGHT - 1;
  370              }
  371              else
  372              {
  373                disp_begin_pos = total_hist - HISTORY_WINDOW_HEIGHT;
  374                if( disp_begin_pos < 0 ) disp_begin_pos = 0;
  375                cursor_pos = total_hist - disp_begin_pos - 1;
  376              }
  377                          DisplayHistory();
  378                } 
  379              }
  380                      break;
  381       case KEY_PPAGE:
  382              if( disp_begin_pos + cursor_pos <= 0 )
  383              {  beep();  }
  384              else
  385              {
  386                if( cursor_pos > 0 )
  387                {
  388              PrintHstEntry( disp_begin_pos + cursor_pos, 
  389                     cursor_pos, HST_COLOR,
  390                                 start_x, &hide_left, &hide_right);
  391              cursor_pos = 0;
  392              PrintHstEntry( disp_begin_pos + cursor_pos, 
  393                     cursor_pos, HIHST_COLOR,
  394                                 start_x, &hide_left, &hide_right);
  395                }
  396                else
  397                {
  398              if( (disp_begin_pos -= HISTORY_WINDOW_HEIGHT) < 0 )
  399              {
  400                disp_begin_pos = 0;
  401              }
  402                          cursor_pos = 0;
  403                          DisplayHistory();
  404                } 
  405              }
  406                      break;
  407       case KEY_HOME: if( disp_begin_pos == 0 && cursor_pos == 0 )
  408              {   beep();    }
  409              else
  410              {
  411                disp_begin_pos = 0;
  412                cursor_pos     = 0;
  413                        DisplayHistory();
  414              }
  415                      break;
  416       case KEY_END : 
  417                      disp_begin_pos = MAX(0, total_hist - HISTORY_WINDOW_HEIGHT);
  418              cursor_pos     = total_hist - disp_begin_pos - 1;
  419                      DisplayHistory();
  420                      break;
  421       case LF :
  422       case CR : 
  423                      TMP = Hist;
  424                      for(tmp = 0; (tmp != disp_begin_pos + cursor_pos); tmp++)
  425                      {
  426                         TMP = TMP -> next;
  427                         if (TMP == NULL)
  428                           break;
  429                      }
  430                      if (TMP != NULL)
  431                         RetVal = TMP -> hst;
  432                      else
  433                         RetVal = NULL;
  434              break;
  435 
  436       case ESC:      RetVal = NULL;
  437                      break;
  438 
  439       default :      beep();
  440              break;
  441     } /* switch */
  442   } while(ch != CR && ch != ESC && ch != -1);
  443   /* leaveok(stdscr, FALSE); */
  444   touchwin(stdscr);
  445   return RetVal;
  446 }
  447 
  448