1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <slang.h>
5
issue_instructions(void)6 static void issue_instructions (void)
7 {
8 (void) fputs ("Enter any text at the prompts.\n", stdout);
9 (void) fputs ("Entering \"quit\" will exit the example\n", stdout);
10 (void) fputs ("An EOF (^D) will also exit the example\n", stdout);
11 (void) fputs ("\n", stdout);
12 }
13
example_1(void)14 static int example_1 (void)
15 {
16 SLrline_Type *rl;
17 unsigned int width = 80;
18
19 issue_instructions ();
20
21 SLang_init_tty (-1, 0, 1);
22 rl = SLrline_open (width, SL_RLINE_BLINK_MATCH);
23
24 while (1)
25 {
26 char *line;
27 unsigned int len;
28
29 line = SLrline_read_line (rl, "prompt>", &len);
30 if (line == NULL)
31 break;
32 if (0 == strcmp (line, "quit"))
33 {
34 SLfree (line);
35 break;
36 }
37 (void) fprintf (stdout, "\nRead %d bytes: %s\n", strlen(line), line);
38 SLfree (line);
39 }
40 SLrline_close (rl);
41 SLang_reset_tty ();
42 return 0;
43 }
44
example_2(void)45 static int example_2 (void)
46 {
47 SLrline_Type *rl;
48 unsigned int width = 80;
49
50 issue_instructions ();
51
52 SLang_init_tty (-1, 0, 1);
53 SLang_set_abort_signal (NULL);
54
55 rl = SLrline_open (width, SL_RLINE_BLINK_MATCH);
56 if (rl == NULL)
57 return -1;
58
59 while (1)
60 {
61 char *line;
62 unsigned int len;
63
64 line = SLrline_read_line (rl, "prompt>", &len);
65 if (line == NULL)
66 {
67 int err = SLang_get_error ();
68 if (err == SL_UserBreak_Error)
69 {
70 (void) fprintf (stderr, "*Interrupted*\n");
71 SLang_set_error (0);
72 SLKeyBoard_Quit = 0;
73 continue;
74 }
75 if (err == 0)
76 break; /* EOF */
77 fprintf (stderr, "Error Occurred: %s\n", SLerr_strerror (err));
78 break;
79 }
80 if (0 == strcmp (line, "quit"))
81 {
82 SLfree (line);
83 break;
84 }
85 (void) fprintf (stdout, "\nRead %d bytes: %s\n", strlen(line), line);
86 SLfree (line);
87 }
88 SLrline_close (rl);
89 SLang_reset_tty ();
90 return 0;
91 }
92
example_3(void)93 static int example_3 (void)
94 {
95 SLrline_Type *rl;
96 unsigned int width = 80;
97
98 if ((-1 == SLang_init_all ())
99 || (-1 == SLang_init_array_extra ())
100 || (-1 == SLang_init_import ()))
101 return -1;
102
103 (void) SLpath_set_load_path ("../slsh/lib");
104
105 if (-1 == SLrline_init ("demo/rline", NULL, NULL))
106 return -1;
107
108 issue_instructions ();
109
110 SLang_init_tty (-1, 0, 1);
111 SLang_set_abort_signal (NULL);
112
113 rl = SLrline_open2 ("rline", width, SL_RLINE_BLINK_MATCH);
114 if (rl == NULL)
115 return -1;
116
117 while (1)
118 {
119 char *line;
120 unsigned int len;
121
122 line = SLrline_read_line (rl, "prompt>", &len);
123 if (line == NULL)
124 {
125 int err = SLang_get_error ();
126 if (err == SL_UserBreak_Error)
127 {
128 (void) fprintf (stderr, "*Interrupted*\n");
129 SLang_set_error (0);
130 SLKeyBoard_Quit = 0;
131 continue;
132 }
133 if (err == 0)
134 break; /* EOF */
135 fprintf (stderr, "Error Occurred: %s\n", SLerr_strerror (err));
136 break;
137 }
138 if (0 == strcmp (line, "quit"))
139 {
140 SLfree (line);
141 break;
142 }
143 (void) fprintf (stdout, "\nRead %d bytes: %s\n", strlen(line), line);
144 if (-1 == SLrline_save_line (rl))
145 break;
146
147 SLfree (line);
148 }
149 SLrline_close (rl);
150 SLang_reset_tty ();
151 return 0;
152 }
153
main(int argc,char ** argv)154 int main (int argc, char **argv)
155 {
156 int n = 1;
157
158 if (argc == 2)
159 n = atoi (argv[1]);
160
161 switch (n)
162 {
163 case 1:
164 (void) example_1 ();
165 break;
166 case 2:
167 (void) example_2 ();
168 break;
169 case 3:
170 (void) example_3 ();
171 break;
172
173 default:
174 (void) fprintf (stderr, "Invalid/Unsupported example number: %d\n", n);
175 return 1;
176 }
177 (void) fprintf (stdout, "\nExample %d done\n", n);
178 return 0;
179 }
180