This is Unofficial EPICS BASE Doxygen Site
osdReadline.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Saskatchewan
3 * Copyright (c) 2014 UChicago Argonne LLC, as Operator of Argonne
4 * National Laboratory.
5 * EPICS BASE is distributed subject to a Software License Agreement found
6 * in file LICENSE that is included with this distribution.
7 \*************************************************************************/
8 /* Author: Eric Norum Date: 12DEC2001 */
9 
10 /*
11  * This file is included by epicsReadline.c which has already included the
12  * headers stdio.h, stdlib.h, errno.h, envDefs.h and epicsReadline.h
13  */
14 
15 #include <string.h>
16 #include <ledLib.h>
17 
18 /* FIXME: Remove line-lenth limitation */
19 #define LEDLIB_LINESIZE 1000
20 
21 #ifndef _WRS_VXWORKS_MAJOR
22 typedef int LED_ID;
23 #endif
24 
25 struct osdContext {
26  LED_ID ledId;
27  char line[LEDLIB_LINESIZE];
28 };
29 
30 /*
31  * Create a command-line context
32  */
33 static void
34 osdReadlineBegin(struct readlineContext *context)
35 {
36  struct osdContext *osd = malloc(sizeof *osd);
37 
38  if (osd != NULL) {
39  osd->ledId = (LED_ID) ERROR;
40  if (context->in == NULL) {
41  long i = 50;
42 
44  if (i < 1)
45  i = 1;
46 
47  osd->ledId = ledOpen(fileno(stdin), fileno(stdout), i);
48  if (osd->ledId == (LED_ID) ERROR) {
49  context->in = stdin;
50  printf("Warning -- Unabled to allocate space for command-line history.\n");
51  printf("Warning -- Command-line editting disabled.\n");
52  }
53  }
54  context->osd = osd;
55  }
56 }
57 
58 /*
59  * Read a line of input
60  */
61 static char *
62 osdReadline (const char *prompt, struct readlineContext *context)
63 {
64  struct osdContext *osd = context->osd;
65  int i;
66 
67  if (prompt) {
68  fputs(prompt, stdout);
69  fflush(stdout);
70  }
71  if (osd->ledId != (LED_ID) ERROR) {
72  i = ledRead(osd->ledId, osd->line, LEDLIB_LINESIZE-1);
73  if (i < 0)
74  return NULL;
75  }
76  else {
77  if (fgets(osd->line, LEDLIB_LINESIZE, context->in) == NULL)
78  return NULL;
79  i = strlen(osd->line);
80  }
81  if ((i >= 1) && (osd->line[i-1] == '\n'))
82  osd->line[i-1] = '\0';
83  else
84  osd->line[i] = '\0';
85  return osd->line;
86 }
87 
88 /*
89  * Destroy a command-line context
90  */
91 static void
92 osdReadlineEnd (struct readlineContext *context)
93 {
94  LED_ID ledId = context->osd->ledId;
95 
96  if (ledId != (LED_ID) ERROR)
97  ledClose(ledId);
98  free(context->osd);
99 }
100 
int i
Definition: scan.c:967
#define printf
Definition: epicsStdio.h:41
#define NULL
Definition: catime.c:38
LIBCOM_API const ENV_PARAM IOCSH_HISTSIZE
char * line
Definition: reader.c:25
#define LEDLIB_LINESIZE
Definition: osdReadline.c:19
#define stdout
Definition: epicsStdio.h:30
#define stdin
Definition: epicsStdio.h:28
LIBCOM_API long epicsStdCall envGetLongConfigParam(const ENV_PARAM *pParam, long *pLong)
Get value of a long configuration parameter.
Definition: envSubr.c:303
struct osdContext * osd
Definition: epicsReadline.c:38
int LED_ID
Definition: osdReadline.c:22