console.c (3053B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: console.c 8 * Purpose: User TTY I/O 9 */ 10 11 #include <sys/types.h> 12 #include <stdio.h> 13 #include <stdarg.h> 14 #include <unistd.h> 15 16 #include <sal/core/sync.h> 17 #include <sal/core/thread.h> 18 #include <sal/appl/io.h> 19 20 static sal_mutex_t console_mutex; 21 22 /* 23 * sal_console_init 24 */ 25 26 int 27 sal_console_init(void) 28 { 29 console_mutex = sal_mutex_create("console mutex"); 30 31 return 0; 32 } 33 34 /* 35 * Function: 36 * sal_console_info 37 * Purpose: 38 * Get console information 39 * Parameters: 40 * info - console info 41 * Returns: 42 * 0 on success 43 * -1 if stdin is not a tty 44 */ 45 46 int 47 sal_console_info_get(sal_console_info_t *info) 48 { 49 #if defined(TIOCGWINSZ) 50 struct winsize W; 51 #endif /* defined(TIOCGWINSZ) */ 52 53 if (!isatty(0)) { 54 return -1; 55 } 56 if (info == NULL) { 57 return 0; 58 } 59 memset(info, 0, sizeof(*info)); 60 #if defined(TIOCGWINSZ) 61 if (ioctl(0, TIOCGWINSZ, &W) >= 0) { 62 info->cols = (int)W.ws_col; 63 info->rows = (int)W.ws_row; 64 } 65 #endif /* defined(TIOCGWINSZ) */ 66 return 0; 67 } 68 69 /* 70 * Function: 71 * sal_console_write 72 * Purpose: 73 * Write characters to console 74 * Parameters: 75 * buf - buffer to write 76 * count - number of characters in buffer 77 */ 78 79 int 80 sal_console_write(const void *buf, int count) 81 { 82 return write(0, buf, count); 83 } 84 85 /* 86 * Function: 87 * sal_console_read 88 * Purpose: 89 * Read characters from console 90 * Parameters: 91 * buf - buffer to fill 92 * count - number of characters to read 93 */ 94 95 int 96 sal_console_read(void *buf, int count) 97 { 98 return read(0, buf, count); 99 } 100 101 /* 102 * Function: 103 * sal_console_gets 104 * Purpose: 105 * Read line from console 106 * Parameters: 107 * buf - input buffer 108 * size - size of input buffer 109 */ 110 111 char * 112 sal_console_gets(char *buf, int size) 113 { 114 char *p = fgets(buf, size, stdin); 115 if (p == NULL) { 116 clearerr(stdin); 117 } 118 return p; 119 } 120 121 #ifndef SDK_CONFIG_SAL_VPRINTF 122 /* 123 * sal_vprintf 124 * 125 * This is the base routine upon which all standard printing is built. 126 */ 127 128 int sal_vprintf(const char *fmt, va_list varg) 129 { 130 int retv; 131 #ifndef SAL_THREAD_NAME_PRINT_DISABLE 132 char thread_name[SAL_THREAD_NAME_MAX_LEN]; 133 sal_thread_t thread; 134 135 thread = sal_thread_self(); 136 thread_name[0] = 0; 137 138 if (thread != sal_thread_main_get()) { 139 sal_thread_name(thread, thread_name, sizeof (thread_name)); 140 } 141 #endif /* !SAL_THREAD_NAME_PRINT_DISABLE */ 142 143 sal_mutex_take(console_mutex, sal_mutex_FOREVER); 144 145 #ifndef SAL_THREAD_NAME_PRINT_DISABLE 146 if (thread_name[0] != 0) { 147 (void) printf("[%s]", thread_name); 148 } 149 #endif /* !SAL_THREAD_NAME_PRINT_DISABLE */ 150 151 retv = vprintf(fmt, varg); 152 153 fflush(stdout); 154 155 sal_mutex_give(console_mutex); 156 157 return retv; 158 } 159 #endif /* !SDK_CONFIG_SAL_VPRINTF */ 160 161 int sal_printf(const char *fmt, ...) 162 { 163 int retv; 164 va_list varg; 165 va_start(varg, fmt); 166 retv = sal_vprintf(fmt, varg); 167 va_end(varg); 168 return retv; 169 }