65f04177b57a57809480c0d2b1e3c3983b91dc62
3 * $Id: serial.c,v 1.1 2002/01/25 19:34:45 mdw Exp $
5 * Common serial functionality
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Jog: Programming for a jogging machine.
14 * Jog is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Jog is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Jog; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 2002/01/25 19:34:45 mdw
37 /*----- Header files ------------------------------------------------------*/
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @getbaud@ --- *
49 * Arguments: @const char *p@ = pointer to string
50 * @const char **pp@ = where to store final pointer
52 * Returns: Baud rate, or @-1@ on failure.
54 * Use: Parses a baud rate, either as a raw number or as an
55 * expression like `9k6'.
58 static long getbaud(const char *p
, const char **pp
)
64 /* --- Main loop --- */
67 int ch
= (unsigned char)*p
;
95 x
= x
* s
+ (ch
- '0') * f
;
108 /* --- @serial_parse@ --- *
110 * Arguments: @const char *p@ = configuration string to parse
111 * @serial_config *sc@ = pointer to serial config structure
113 * Returns: Zero if OK, or @-1@ on error.
115 * Use: Parses a serial config string into something more
116 * reasonable. The config structure should have been
117 * initialized to something sensible (e.g., @SERIAL_INIT@)
121 int serial_parse(const char *p
, serial_config
*sc
)
127 /* --- Pick out an initial baud rate --- */
129 while (isspace((unsigned char)*p
))
131 if (*p
!= 0 && *p
!= ':') {
132 if ((x
= getbaud(p
, &p
)) < 0)
136 while (isspace((unsigned char)*p
))
141 /* --- Pick out a word length --- */
143 while (isspace((unsigned char)*p
))
145 if ((x
= strtol(p
, &qq
, 10)) == 0)
149 while (isspace((unsigned char)*p
))
154 /* --- Pick out a parity designation --- */
156 while (isspace((unsigned char)*p
))
158 for (q
= p
; isalpha((unsigned char)*q
); q
++)
162 if (strncmp(p
, "none", q
- p
) == 0)
163 sc
->parity
= PARITY_NONE
;
164 else if (strncmp(p
, "odd", q
- p
) == 0)
165 sc
->parity
= PARITY_ODD
;
166 else if (strncmp(p
, "even", q
- p
) == 0)
167 sc
->parity
= PARITY_EVEN
;
171 while (isspace((unsigned char)*p
))
176 /* --- Pick out a number of stop bits --- */
178 while (isspace((unsigned char)*p
))
180 if ((x
= strtol(p
, &qq
, 10)) == 0)
184 while (isspace((unsigned char)*p
))
197 /*----- Test rig ----------------------------------------------------------*/
203 int main(int argc
, char *argv
[])
206 static const char *ptab
[] = { "none", "odd", "even" };
208 for (i
= 1; i
< argc
; i
++) {
209 serial_config sc
= SERIAL_INIT
;
211 if (serial_parse(argv
[i
], &sc
))
212 printf("invalid serial configuration `%s'\n", argv
[i
]);
214 printf("%lu %u-%s-%u\n", sc
.baud
, sc
.wordlen
,
215 ptab
[sc
.parity
], sc
.stopbits
);
223 /*----- That's all, folks -------------------------------------------------*/