1 
2 #include "ytree.h"
3 #include "tilde.h"
4 #include "xmalloc.h"
5 
6 
7 /***************************************************************************
8  * InputStr                                                                *
9  * Liest eine Zeichenkette an Position (y,x) mit der max. Laenge length    *
10  * Vorschlagswert fuer die Eingabe ist s selbst                            *
11  * Zurueckgegeben wird das Zeichen, mit dem die Eingabe beendet wurde      *
12  ***************************************************************************/
13 
14 
StrLeft(const char * str,size_t count)15 char *StrLeft(const char *str, size_t count)
16 {
17 #ifdef WITH_UTF8
18   mbstate_t state;
19 #endif
20   char *rez, *tmp;
21   size_t len, i;
22 
23 #ifdef WITH_UTF8
24   memset(&state, 0, sizeof(state));
25 #endif
26   if (count == 0) return(Strdup(""));
27   len = StrVisualLength(str);
28   if (count >= len) return(Strdup(str));
29 
30   len = 0;
31 
32   tmp = Strdup(str);
33   for (i = 0; i < count; i++) {
34 #ifdef WITH_UTF8
35     len += mbrlen(p, 4, &state);
36 #else
37     len++;
38 #endif
39   }
40   free(tmp);
41 
42   rez = Strndup(str, len);
43   rez[len] = '\0';
44   return(rez);
45 }
46 
StrRight(const char * str,size_t count)47 char *StrRight(const char *str, size_t count)
48 {
49 #ifdef WITH_UTF8
50   mbstate_t state;
51 #endif
52   char *rez, *p, *tmp;
53   size_t byte_len, char_len, tmp_len, i;
54 
55 #ifdef WITH_UTF8
56   memset(&state, 0, sizeof(state));
57 #endif
58 
59   if (count == 0) return(Strdup(""));
60 
61   byte_len = strlen(str);
62   char_len = StrVisualLength(str);
63 
64   if (count > char_len) count = char_len;
65 
66   tmp = Strdup(str);
67   p = tmp;
68   i = 0;
69   rez = NULL;
70   while ( (p - tmp) < byte_len ) {
71     if (i == (char_len - count) ) {
72       rez = Strdup(p);
73     }
74 #ifdef WITH_UTF8
75     tmp_len = mbrlen(p, 4, &state);
76 #else
77     tmp_len = 1;
78 #endif
79     p += tmp_len;
80     i++;
81   }
82 
83   free(tmp);
84   return(rez);
85 }
86 
StrVisualLength(const char * str)87 int StrVisualLength(const char *str)
88 {
89 #ifdef WITH_UTF8
90   mbstate_t state;
91   int len = 0;
92 
93   memset(&state, '\0', sizeof(state));
94   len = mbsrtowcs(NULL, &str, strlen(str), &state);
95   if(len < 0) {
96     /* Invalid multibyte sequence */
97     len = strlen(str);
98   }
99 #else
100   return(strlen(str));
101 #endif
102 }
103 
104 
InputString(char * s,int y,int x,int cursor_pos,int length,char * term)105 int InputString(char *s, int y, int x, int cursor_pos, int length, char *term)
106                                /* Ein- und Ausgabestring              */
107                                /* Position auf Bildschirm             */
108                                /* max. Laenge                         */
109                                /* Menge von Terminierungszeichen      */
110 {
111   int p;                       /* Aktuelle Position                   */
112   int c1;                      /* Gelesenes Zeichen                   */
113   int i;                       /* Laufvariable                        */
114   char *pp;
115   BOOL len_flag = FALSE;
116   char path[PATH_LENGTH + 1];
117   char buf[2], sbuf[20], *ls, *rs;
118   static BOOL insert_flag = TRUE;
119 
120   buf[1] = '\0';
121   strcpy(sbuf, "");
122 
123   /* Feld gefuellt ausgeben */
124   /*------------------------*/
125   print_time = FALSE;
126   curs_set(1);
127   MvAddStr( y, x, s );
128   leaveok(stdscr, FALSE);
129 
130   for(i=strlen(s); i < length; i++)
131     addch( '_' );
132 
133   p = cursor_pos;
134 
135   MvAddStr( y, x, s );
136 
137   nodelay( stdscr, TRUE );
138   do {
139     c1 = wgetch(stdscr);
140 
141     if ( c1 != ERR ) {
142 
143       if( c1 >= ' ' && c1 < 0xff && c1 != 127 ) {
144         if ( len_flag == TRUE) beep();
145         else {
146           buf[0] = (char)c1;
147 	  strcat(sbuf, buf);
148 	}
149       } else {
150         /* Control symbols */
151         switch( c1 )
152         {
153         case 'C' & 0x1f     : c1 = 27;
154 			      break;
155 
156         case KEY_LEFT       : if( p > 0 )
157                                 p--;
158                               else
159                                 beep();
160                               break;
161         case KEY_RIGHT      : if( p < StrVisualLength(s) )
162                                 p++;
163                               else
164                                 beep();
165                               break;
166 	case KEY_UP         :
167 	                      nodelay(stdscr, FALSE);
168 			      pp = GetHistory();
169 			      nodelay(stdscr, TRUE);
170                              if (pp == NULL) break;
171                              if(*pp)
172                              {
173 			       ls = StrLeft(pp, length);
174 			       strcpy(s, ls);
175 			       free(ls);
176 			       p = StrVisualLength(s);
177                                MvAddStr( y, x, s );
178                                for(i=p; i < length; i++)
179                                  addch( '_' );
180                                RefreshWindow( stdscr );
181                                doupdate();
182                              }
183                              break;
184 
185         case KEY_HOME       : p = 0;
186                               break;
187         case KEY_END        : p = StrVisualLength( s );
188                               break;
189         case KEY_DC         : if( p < StrVisualLength(s) )
190                               {
191 			        ls = StrLeft(s, p);
192 				rs = StrRight(s, StrVisualLength(s) - p - 1);
193 				strcpy(s, ls);
194 				strcat(s, rs);
195 				free(ls);
196 				free(rs);
197 				MvAddStr( y, x, s );
198                                 addch( '_' );
199                               }
200                               break;
201 	case 0x08           :
202         case 0x7F           :
203         case KEY_BACKSPACE  : if( p > 0 )
204                               {
205 			        ls = StrLeft(s, p - 1);
206 				rs = StrRight(s, StrVisualLength(s) - p);
207 				strcpy(s, ls);
208 				strcat(s, rs);
209 				free(ls);
210 				free(rs);
211 				MvAddStr( y, x, s );
212                                 addch( '_' );
213                                 p--;
214                               }
215                               else
216                                 beep();
217                               break;
218         case KEY_DL         : for(i=0; i < StrVisualLength(s) - p; i++) addch( '_' );
219 			      ls = StrLeft(s, p);
220 			      strcpy(s, ls);
221 			      free(ls);
222                               break;
223 	case KEY_EIC        :
224         case KEY_IC         : insert_flag ^= TRUE;
225                               break;
226 	case '\t'           : if(( pp = GetMatches(s)) == NULL) {
227 			       break;
228 			     }
229                              if(*pp)
230                              {
231 			       ls = StrLeft(pp, length);
232 			       strcpy(s, ls);
233 			       free(ls);
234 			       p = StrVisualLength(s);
235 			       free(pp);
236                                MvAddStr( y, x, s );
237                                for(i=p; i < length; i++) addch( '_' );
238                                RefreshWindow( stdscr );
239                                doupdate();
240                              }
241                              break;
242 #ifdef KEY_F
243         case KEY_F(2)       :
244 #endif
245         case 'F' & 0x1f     : if(KeyF2Get( statistic.tree,
246                                            statistic.disp_begin_pos,
247                                            statistic.cursor_pos, path))
248                               {
249 			        /* beep(); */
250 				break;
251 			      }
252                               if(*path)
253                               {
254 			        ls = StrLeft(path, length);
255 				strcpy(s, ls);
256 				free(ls);
257 			        p = StrVisualLength(s);
258                                 MvAddStr( y, x, s );
259                                 for(i=p; i < length; i++) addch( '_' );
260                                 RefreshWindow( stdscr );
261                                 doupdate();
262                               }
263                               break;
264 
265         default             : if( c1 == LF ) c1 = CR;
266                               break;
267         } /* switch */
268       } /* else control symbols */
269     } else {
270 
271       if (strlen(sbuf) > 0) {
272        if ( insert_flag ) {
273   	    /* append symbol */
274 	    if ( p >= StrVisualLength(s)) strcat(s, sbuf);
275 	    else {
276 	      /* insert symbol at cursor position */
277 	      if ( p > 0 ) ls = StrLeft(s, p);
278 	      else ls = Strdup("");
279 	      rs = StrRight(s, StrVisualLength(s) - p);
280 	      strcpy(s, ls);
281 	      strcat(s, sbuf);
282 	      strcat(s, rs);
283 	      free(ls);
284 	      free(rs);
285 	    }
286           } else {
287             /* owerwrite symbol at cursor position */
288 	    if ( p > StrVisualLength(s) - 1) strcat(s, sbuf);
289 	    else {
290   	      if (p > 0) ls = StrLeft(s, p);
291 	      else ls = Strdup("");
292 	      rs = StrRight(s, StrVisualLength(s) - p - 1);
293 	      strcpy(s, ls);
294 	      strcat(s, sbuf);
295 	      strcat(s, rs);
296 	      free(ls);
297 	      free(rs);
298 	    }
299 	  }
300 
301 	strcpy(sbuf, "");
302 	p++;
303       }
304 
305       if (StrVisualLength(s) >= length) len_flag = TRUE;
306       else len_flag = FALSE;
307 
308       MvAddStr( y, x, s );
309       wmove(stdscr, y, x + p);
310     }
311   } while( c1 !=  27 && c1 != CR );
312 
313   nodelay( stdscr, FALSE );
314 
315   p = strlen( s );
316   move( y, x + p );
317 
318   for(i=0; i < length - p; i++ )
319    addch( ' ' );
320 
321   move( y, x );
322   leaveok( stdscr, TRUE);
323   curs_set(0);
324   print_time = TRUE;
325   InsHistory( s );
326 #ifdef READLINE_SUPPORT
327   pp = tilde_expand(s);
328 #else
329   pp = Strdup(s);
330 #endif
331 
332   strncpy( s, pp, length - 1);
333   s[length]='\0';
334   xfree(pp);
335   return( c1 );
336 }
337 
338 
339 
340 
341 
InputChoise(char * msg,char * term)342 int InputChoise(char *msg, char *term)
343 {
344   int  c;
345 
346   ClearHelp();
347 
348   curs_set(1);
349   leaveok(stdscr, FALSE);
350   mvprintw( LINES - 2, 1, msg );
351   RefreshWindow( stdscr );
352   doupdate();
353   do
354   {
355     c = Getch();
356     if(c >= 0)
357       if( islower( c ) ) c = toupper( c );
358   } while( c != -1 && !strchr( term, c ) );
359 
360   if(c >= 0)
361     echochar( c );
362 
363   move( LINES - 2, 1 ); clrtoeol();
364   leaveok(stdscr, TRUE);
365   curs_set(0);
366 
367   return( c );
368 }
369 
370 
371 
372 
373 
GetTapeDeviceName(void)374 int GetTapeDeviceName( void )
375 {
376   int  result;
377   char path[PATH_LENGTH * 2 +1];
378 
379   result = -1;
380 
381   ClearHelp();
382 
383   (void) strcpy( path, statistic.tape_name );
384 
385   MvAddStr( LINES - 2, 1, "Tape-Device:" );
386   if( InputString( path, LINES - 2, 14, 0, COLS - 15, "\r\033" ) == CR )
387   {
388     result = 0;
389     (void) strcpy( statistic.tape_name, path );
390   }
391 
392   move( LINES - 2, 1 ); clrtoeol();
393 
394   return( result );
395 }
396 
397 
HitReturnToContinue(void)398 void HitReturnToContinue(void)
399 {
400 #ifndef XCURSES
401   curs_set(1);
402   vidattr( A_REVERSE );
403   putp( "[Hit return to continue]" );
404   vidattr( 0 );
405   (void) fflush( stdout );
406   (void) Getch();
407 #endif /* XCURSES */
408   curs_set(0);
409   doupdate();
410 }
411 
412 
413 
KeyPressed()414 BOOL KeyPressed()
415 {
416   BOOL pressed = FALSE;
417 
418 #if !defined( linux ) || !defined( TERMCAP )
419   nodelay( stdscr, TRUE );
420   if( wgetch( stdscr ) != ERR ) pressed = TRUE;
421   nodelay( stdscr, FALSE );
422 #endif /* linux/TERMCAP */
423 
424   return( pressed );
425 }
426 
427 
EscapeKeyPressed()428 BOOL EscapeKeyPressed()
429 {
430   BOOL pressed = FALSE;
431   int  c;
432 
433 #if !defined( linux ) || !defined( TERMCAP )
434   nodelay( stdscr, TRUE );
435   if( ( c = wgetch( stdscr ) ) != ERR ) pressed = TRUE;
436   nodelay( stdscr, FALSE );
437 #endif /* linux/TERMCAP */
438 
439   return( ( pressed && c == ESC ) ? TRUE : FALSE );
440 }
441 
442 
443 
444 
445 #ifdef VI_KEYS
446 
ViKey(int ch)447 int ViKey( int ch )
448 {
449   switch( ch )
450   {
451     case VI_KEY_UP:    ch = KEY_UP;    break;
452     case VI_KEY_DOWN:  ch = KEY_DOWN;  break;
453     case VI_KEY_RIGHT: ch = KEY_RIGHT; break;
454     case VI_KEY_LEFT:  ch = KEY_LEFT;  break;
455     case VI_KEY_PPAGE: ch = KEY_PPAGE; break;
456     case VI_KEY_NPAGE: ch = KEY_NPAGE; break;
457   }
458   return(ch);
459 }
460 
461 #endif /* VI_KEYS */
462 
463 
464 #ifdef _IBMR2
465 #undef wgetch
466 
AixWgetch(WINDOW * w)467 int AixWgetch( WINDOW *w )
468 {
469   int c;
470 
471   if( ( c = wgetch( w ) ) == KEY_ENTER ) c = LF;
472 
473   return( c );
474 }
475 
476 #endif
477 
478 
Getch()479 int Getch()
480 {
481   int c;
482 
483   c = getch();
484 
485 #ifdef KEY_RESIZE
486   if(c == KEY_RESIZE) {
487     resize_request = TRUE;
488     c = -1;
489   }
490 #endif
491 
492   return(c);
493 }
494 
495