Adding upstream version 3.31.
[syslinux-debian/hramrach.git] / com32 / lib / sys / xserial_write.c
blobf119083a88ea3376f593b6e75bc506f49148a056
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2004-2006 H. Peter Anvin - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
12 * conditions:
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
29 * xserial_write.c
31 * Raw writing to the serial port; no \n -> \r\n translation, but
32 * convert \1# sequences.
35 #include <errno.h>
36 #include <string.h>
37 #include <com32.h>
38 #include <minmax.h>
39 #include <colortbl.h>
40 #include "file.h"
42 static void emit(char ch)
44 static com32sys_t ireg; /* Zeroed with the BSS */
46 ireg.eax.w[0] = 0x0400 | (unsigned char)ch;
48 __intcall(0x21, &ireg, NULL);
51 ssize_t __xserial_write(struct file_info *fp, const void *buf, size_t count)
53 const char *bufp = buf;
54 size_t n = 0;
55 static enum { st_0, st_1, st_2, st_3 } state = st_0;
56 static int ncolor = 0;
57 int num;
58 const char *p;
60 (void)fp;
62 while ( count-- ) {
63 unsigned char ch = *bufp++;
65 switch (state) {
66 case st_0:
67 if (ch == '\1') {
68 state = st_1;
69 } else {
70 emit(ch);
72 break;
74 case st_1:
75 if (ch == '#') {
76 state = st_2;
77 ncolor = 0;
78 } else {
79 state = st_0;
81 break;
83 case st_2:
84 num = ch-'0';
85 if (num < 10) {
86 ncolor = num*10;
87 state = st_3;
88 } else {
89 state = st_0;
91 break;
93 case st_3:
94 num = ch-'0';
95 if (num < 10) {
96 ncolor += num;
98 if (ncolor < console_color_table_size) {
99 emit('\033');
100 emit('[');
101 emit('0');
102 emit(';');
103 for (p = console_color_table[ncolor].ansi; *p; p++)
104 emit(*p);
105 emit('m');
108 state = st_0;
109 break;
112 n++;
115 return n;