Adding upstream version 3.50~pre5.
[syslinux-debian/hramrach.git] / com32 / lib / sys / xserial_write.c
blobebdc953e9870c52d5c52154848150233813b8985
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.b[1] = 0x04;
47 ireg.edx.b[0] = ch;
49 __intcall(0x21, &ireg, NULL);
52 ssize_t __xserial_write(struct file_info *fp, const void *buf, size_t count)
54 const char *bufp = buf;
55 size_t n = 0;
56 static enum { st_0, st_1, st_2, st_3 } state = st_0;
57 static int ncolor = 0;
58 int num;
59 const char *p;
61 (void)fp;
63 while ( count-- ) {
64 unsigned char ch = *bufp++;
66 switch (state) {
67 case st_0:
68 if (ch == '\1') {
69 state = st_1;
70 } else {
71 emit(ch);
73 break;
75 case st_1:
76 if (ch == '#') {
77 state = st_2;
78 ncolor = 0;
79 } else {
80 state = st_0;
82 break;
84 case st_2:
85 num = ch-'0';
86 if (num < 10) {
87 ncolor = num*10;
88 state = st_3;
89 } else {
90 state = st_0;
92 break;
94 case st_3:
95 num = ch-'0';
96 if (num < 10) {
97 ncolor += num;
99 if (ncolor < console_color_table_size) {
100 emit('\033');
101 emit('[');
102 emit('0');
103 emit(';');
104 for (p = console_color_table[ncolor].ansi; *p; p++)
105 emit(*p);
106 emit('m');
109 state = st_0;
110 break;
113 n++;
116 return n;