FreeCalypso > hg > sms-coding-utils
comparison gen-pdu/input.c @ 9:003660a57f99
new program sms-gen-tpdu
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sat, 05 Aug 2023 07:43:45 +0000 |
| parents | |
| children | d9d722033ff1 |
comparison
equal
deleted
inserted
replaced
| 8:265b8103530c | 9:003660a57f99 |
|---|---|
| 1 /* | |
| 2 * This module implements input handling for sms-gen-tpdu. | |
| 3 */ | |
| 4 | |
| 5 #include <ctype.h> | |
| 6 #include <stdio.h> | |
| 7 #include <stdlib.h> | |
| 8 #include <string.h> | |
| 9 #include <strings.h> | |
| 10 #include "error.h" | |
| 11 | |
| 12 extern char input_line[]; | |
| 13 extern int input_lineno; | |
| 14 | |
| 15 extern void set_sc_addr(); | |
| 16 extern void set_user_addr(); | |
| 17 extern void set_mr_byte(); | |
| 18 extern void set_pid_byte(); | |
| 19 extern void set_dcs(); | |
| 20 extern void set_scts(); | |
| 21 extern void cmd_msg_plain(); | |
| 22 extern void cmd_msg_udh(); | |
| 23 | |
| 24 static struct cmdtab { | |
| 25 char *cmd; | |
| 26 int minargs; | |
| 27 int maxargs; | |
| 28 void (*func)(); | |
| 29 } cmdtab[] = { | |
| 30 {"sc-addr", 1, 1, set_sc_addr}, | |
| 31 {"user-addr", 1, 1, set_user_addr}, | |
| 32 {"mr", 1, 1, set_mr_byte}, | |
| 33 {"pid", 1, 1, set_pid_byte}, | |
| 34 {"dcs", 2, 2, set_dcs}, | |
| 35 {"sc-ts", 1, 1, set_scts}, | |
| 36 {"msg", 1, 1, cmd_msg_plain}, | |
| 37 {"msg-udh", 1, 1, cmd_msg_udh}, | |
| 38 /* table search terminator */ | |
| 39 {0, 0, 0, 0} | |
| 40 }; | |
| 41 | |
| 42 void | |
| 43 process_input_line() | |
| 44 { | |
| 45 char *argv[10]; | |
| 46 char *cp, **ap; | |
| 47 struct cmdtab *tp; | |
| 48 | |
| 49 cp = index(input_line, '\n'); | |
| 50 if (!cp) { | |
| 51 fprintf(stderr, ERR_PREFIX "missing newline\n", input_lineno); | |
| 52 exit(1); | |
| 53 } | |
| 54 for (cp = input_line; isspace(*cp); cp++) | |
| 55 ; | |
| 56 if (*cp == '\0' || *cp == '#') | |
| 57 return; | |
| 58 argv[0] = cp; | |
| 59 while (*cp && !isspace(*cp)) | |
| 60 cp++; | |
| 61 if (*cp) | |
| 62 *cp++ = '\0'; | |
| 63 for (tp = cmdtab; tp->cmd; tp++) | |
| 64 if (!strcmp(tp->cmd, argv[0])) | |
| 65 break; | |
| 66 if (!tp->func) { | |
| 67 fprintf(stderr, ERR_PREFIX "invalid command \"%s\"\n", | |
| 68 input_lineno, argv[0]); | |
| 69 exit(1); | |
| 70 } | |
| 71 for (ap = argv + 1; ; ) { | |
| 72 while (isspace(*cp)) | |
| 73 cp++; | |
| 74 if (!*cp || *cp == '#') | |
| 75 break; | |
| 76 if (ap - argv - 1 >= tp->maxargs) { | |
| 77 fprintf(stderr, ERR_PREFIX "too many arguments\n", | |
| 78 input_lineno); | |
| 79 exit(1); | |
| 80 } | |
| 81 if (*cp == '"') { | |
| 82 *ap++ = ++cp; | |
| 83 for (;;) { | |
| 84 if (!*cp) { | |
| 85 unterm_qstring: fprintf(stderr, ERR_PREFIX | |
| 86 "unterminated quoted string\n", | |
| 87 input_lineno); | |
| 88 exit(1); | |
| 89 } | |
| 90 if (*cp == '"') | |
| 91 break; | |
| 92 if (*cp++ == '\\') { | |
| 93 if (!*cp) | |
| 94 goto unterm_qstring; | |
| 95 cp++; | |
| 96 } | |
| 97 } | |
| 98 *cp++ = '\0'; | |
| 99 } else { | |
| 100 *ap++ = cp; | |
| 101 while (*cp && !isspace(*cp)) | |
| 102 cp++; | |
| 103 if (*cp) | |
| 104 *cp++ = '\0'; | |
| 105 } | |
| 106 } | |
| 107 if (ap - argv - 1 < tp->minargs) { | |
| 108 fprintf(stderr, ERR_PREFIX "too few arguments\n", input_lineno); | |
| 109 exit(1); | |
| 110 } | |
| 111 *ap = 0; | |
| 112 tp->func(ap - argv, argv); | |
| 113 } |
