-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpilcd.c
132 lines (124 loc) · 2.79 KB
/
pilcd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* $Log: pilcd.c,v $
* Revision 1.6 2023-06-23 17:47:36+05:30 Cprogrammer
* interchanged rownum, scroll arguments
*
* Revision 1.5 2023-06-22 23:40:20+05:30 Cprogrammer
* refactored code
*
* Revision 1.4 2014-09-02 22:12:32+05:30 Cprogrammer
* added scroll argument
*
* Revision 1.2 2014-09-02 00:18:48+05:30 Cprogrammer
* fixed minor issues with usage
*
* Revision 1.1 2014-09-01 20:03:44+05:30 Cprogrammer
* Initial revision
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include "substdio.h"
#include "stralloc.h"
#include "lcdPrint.h"
#include "subprintf.h"
#include "scan.h"
#include "str.h"
static char ssoutbuf[512], sserrbuf[512];
static substdio ssout = SUBSTDIO_FDBUF(write, 1, ssoutbuf, sizeof ssoutbuf);
static substdio sserr = SUBSTDIO_FDBUF(write, 2, sserrbuf, sizeof(sserrbuf));
stralloc message = {0};
static void
flush()
{
if (substdio_flush(&sserr))
_exit (111);
else
if (substdio_flush(&ssout))
_exit (111);
}
void
usage()
{
if (subprintf(&sserr, "USAGE: pilcd [-s] [-c clear_type] -r rownum arg1 [arg2 ...]\n") == -1)
_exit (111);
else
if (subprintf(&sserr, " or\n") == -1)
_exit (111);
else
if (subprintf(&sserr, "USAGE: pilcd -c clear_type\n") == -1)
_exit (111);
flush();
return;
}
int
get_options(int argc, char **argv, int *clear, int *scroll, int *rpos, char ***msgptr)
{
int c;
*clear = *scroll = 0;
*rpos = -1;
*msgptr = (char **) 0;
while ((c = getopt(argc, argv, "c:sr:")) != -1) {
switch (c)
{
case 'c':
scan_int(optarg, clear);
break;
case 's':
*scroll = 1;
break;
case 'r':
scan_int(optarg, rpos);
break;
default:
usage();
return (1);
}
}
if (optind < argc)
*msgptr = argv + optind++;
if (*clear == 4 || *clear == 5 || *clear == 6) {
return (lcdPrint(&sserr, 0, 0, *clear, 0));
}
if (*rpos == -1) {
if (subprintf(&sserr, "row on which message to be put not specified\n") == -1)
_exit (111);
usage();
return (1);
} else
if (!(*rpos == 0 || *rpos == 1 || *rpos == 2 || *rpos == 3)) {
if (subprintf(&sserr, "LCD row postion must be 0, 1, 2 or 3\n") == -1)
_exit (111);
usage();
return (1);
} else
if (!*msgptr) {
if (subprintf(&sserr, "message not specified\n") == -1)
_exit (111);
usage();
return (1);
}
return (0);
}
int
main(int argc, char **argv)
{
int scroll = 0, rpos, clear;
char **ptr, **msg;
if (get_options(argc, argv, &clear, &scroll, &rpos, &ptr))
return (1);
for (msg = ptr;msg && *msg; msg++) {
stralloc_catb(&message, *msg, str_len(*msg));
stralloc_append(&message, " ");
}
message.len--;
stralloc_0(&message);
return (lcdPrint(&sserr, rpos, scroll, clear, message.len ? message.s : 0));
}