comparison uptools/atcmd/smsend_basic.c @ 362:89fe66cb60f6

fcup-smsend program put together, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 05 Mar 2018 01:47:18 +0000
parents
children ac311a48630e
comparison
equal deleted inserted replaced
361:ae2556e256a4 362:89fe66cb60f6
1 /*
2 * This is the main module for the basic fcup-smsend utility.
3 */
4
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <strings.h>
10 #include <unistd.h>
11 #include "../../rvinterf/include/exitcodes.h"
12
13 int sms_write_mode, text_mode, utf8_input;
14 u_char dest_addr[12];
15 char msgtext[322];
16 u_char msgtext_gsm7[160];
17 unsigned msgtext_gsmlen;
18
19 process_cmdline(argc, argv)
20 char **argv;
21 {
22 int c;
23 extern int optind;
24
25 while ((c = getopt(argc, argv, "B:np:RtuwWX:")) != EOF) {
26 if (atinterf_cmdline_opt(c))
27 continue;
28 switch (c) {
29 case 't':
30 text_mode = 1;
31 continue;
32 case 'u':
33 utf8_input = 1;
34 continue;
35 case 'w':
36 sms_write_mode = 1;
37 continue;
38 case 'W':
39 sms_write_mode = 2;
40 continue;
41 default:
42 /* error msg already printed */
43 exit(ERROR_USAGE);
44 }
45 }
46 if (argc > optind + 2) {
47 fprintf(stderr, "usage: %s [options] dest-addr [message]\n",
48 argv[0]);
49 exit(ERROR_USAGE);
50 }
51 if (!argv[optind] || !argv[optind][0]) {
52 if (sms_write_mode == 2) {
53 dest_addr[0] = 0;
54 dest_addr[1] = 0x80;
55 } else {
56 fprintf(stderr,
57 "error: destination address must be specified\n");
58 exit(ERROR_USAGE);
59 }
60 } else if (parse_and_encode_dest_addr(argv[optind], dest_addr) < 0) {
61 fprintf(stderr,
62 "error: destination address argument is invalid\n");
63 exit(ERROR_USAGE);
64 }
65 if (!argv[optind+1])
66 return(0);
67 if (strlen(argv[optind+1]) > 320) {
68 fprintf(stderr, "error: message argument is too long\n");
69 exit(ERROR_USAGE);
70 }
71 strcpy(msgtext, argv[optind+1]);
72 return(1);
73 }
74
75 read_msgtext_from_stdin()
76 {
77 char *nl;
78
79 if (!fgets(msgtext, sizeof msgtext, stdin)) {
80 fprintf(stderr, "error reading message from stdin\n");
81 exit(ERROR_USAGE);
82 }
83 nl = index(msgtext, '\n');
84 if (!nl) {
85 fprintf(stderr,
86 "error: message on stdin is too long or unterminated\n");
87 exit(ERROR_USAGE);
88 }
89 *nl = '\0';
90 }
91
92 main(argc, argv)
93 char **argv;
94 {
95 int rc;
96
97 if (!process_cmdline(argc, argv))
98 read_msgtext_from_stdin();
99 if (utf8_input && utf8_to_latin1(msgtext) < 0) {
100 fprintf(stderr, "error: invalid UTF-8 message\n");
101 exit(ERROR_USAGE);
102 }
103 if (text_mode) {
104 if (strlen(msgtext) > 160) {
105 toolong: fprintf(stderr, "error: message exceeds 160 chars\n");
106 exit(ERROR_USAGE);
107 }
108 } else {
109 rc = latin1_to_gsm7(msgtext, msgtext_gsm7, 160,
110 &msgtext_gsmlen);
111 if (rc == -1) {
112 fprintf(stderr,
113 "error: message not valid for GSM7 charset\n");
114 exit(ERROR_USAGE);
115 }
116 if (rc == -2)
117 goto toolong;
118 }
119 /* get to work */
120 atinterf_init();
121 if (text_mode) {
122 prep_for_text_mode();
123 send_in_text_mode(dest_addr, msgtext);
124 } else {
125 prep_for_pdu_mode();
126 send_in_pdu_mode(dest_addr, msgtext_gsm7, msgtext_gsmlen, 0, 0);
127 }
128 if (sms_write_mode == 1)
129 sendafterwr_process();
130 exit(0);
131 }