File indexing completed on 2025-05-11 08:24:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 #ifdef HAVE_CONFIG_H
0064 #include "config.h"
0065 #endif
0066
0067 #include <stdio.h>
0068 #include <termios.h>
0069 #include <unistd.h>
0070 #include <ctype.h>
0071 #include <errno.h>
0072 #include <string.h>
0073 #include <rtems/shell.h>
0074
0075 static int rtems_shell_discard( int c, FILE *stream)
0076 {
0077 return c;
0078 }
0079
0080 static bool rtems_shell_get_text(
0081 FILE *in,
0082 FILE *out,
0083 char *line,
0084 size_t size
0085 )
0086 {
0087 int fd_in = fileno( in);
0088 int (*put)( int, FILE *) =
0089 out != NULL && isatty( fd_in)
0090 ? fputc
0091 : rtems_shell_discard;
0092 size_t i = 0;
0093
0094 if (size < 1) {
0095 return false;
0096 }
0097
0098 tcdrain( fd_in);
0099 if (out != NULL){
0100 tcdrain( fileno(out) );
0101 }
0102
0103 while (true) {
0104 int c = fgetc(in);
0105
0106 switch (c) {
0107 case EOF:
0108 clearerr( in );
0109 return false;
0110 case '\n':
0111 case '\r':
0112 put('\n', out);
0113 line [i] = '\0';
0114 return true;
0115 case 127:
0116 case '\b':
0117 if (i > 0) {
0118 put('\b', out);
0119 put(' ', out);
0120 put('\b', out);
0121 --i;
0122 } else {
0123 put('\a', out);
0124 }
0125 break;
0126 default:
0127 if (!iscntrl( c)) {
0128 if (i < size - 1) {
0129 line [i] = (char) c;
0130 ++i;
0131 put( c, out);
0132 } else {
0133 put('\a', out);
0134 }
0135 } else {
0136 put('\a', out);
0137 }
0138 break;
0139 }
0140 }
0141 return true;
0142 }
0143
0144 bool rtems_shell_login_prompt(
0145 FILE *in,
0146 FILE *out,
0147 const char *device,
0148 rtems_shell_login_check_t check
0149 )
0150 {
0151 int fd_in = fileno(in);
0152 struct termios termios_previous;
0153 bool restore_termios = false;
0154 int i = 0;
0155 bool result = false;
0156
0157 if (tcgetattr( fd_in, &termios_previous) == 0) {
0158 struct termios termios_new = termios_previous;
0159
0160
0161
0162
0163
0164 termios_new.c_lflag &= (unsigned char) ~ECHO;
0165 termios_new.c_cc [VTIME] = 0;
0166 termios_new.c_cc [VMIN] = 1;
0167
0168 restore_termios = tcsetattr( fd_in, TCSANOW, &termios_new) == 0;
0169 }
0170
0171 for (i = 0; i < 3; ++i) {
0172 char user [32];
0173 char passphrase [128];
0174
0175 fprintf( out, "%s login: ", device );
0176 fflush( out );
0177 result = rtems_shell_get_text( in, out, user, sizeof(user) );
0178 if ( !result )
0179 break;
0180 if (0 == strlen(user))
0181 continue;
0182
0183 fflush( in);
0184 fprintf( out, "Password: ");
0185 fflush( out);
0186 result = rtems_shell_get_text( in, NULL, passphrase, sizeof(passphrase) );
0187 if ( !result )
0188 break;
0189 fputc( '\n', out);
0190
0191 result = check( user, passphrase );
0192 if (result)
0193 break;
0194
0195 fprintf( out, "Login incorrect\n\n");
0196 sleep( 2);
0197 }
0198
0199 if (restore_termios) {
0200
0201 tcsetattr( fd_in, TCSANOW, &termios_previous);
0202 }
0203
0204 return result;
0205 }