comparison enc-text/main.c @ 2:a16b1b9728f6

enc-text: sms-encode-text program written
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 05 Aug 2023 02:07:22 +0000
parents
children e56bb9f09ff1
comparison
equal deleted inserted replaced
1:13518c86b73c 2:a16b1b9728f6
1 /*
2 * This is the main module for sms-encode-text 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 "defs.h"
12
13 int utf8_input, ucs2_mode;
14 int concat_enable, concat_refno_set;
15 char msgtext[MAX_MSG_CHARS*2+2];
16 u_char concat_refno;
17
18 process_cmdline(argc, argv)
19 char **argv;
20 {
21 int c;
22 extern int optind;
23 extern char *optarg;
24
25 while ((c = getopt(argc, argv, "cC:uU")) != EOF) {
26 switch (c) {
27 case 'c':
28 concat_enable = 1;
29 continue;
30 case 'C':
31 concat_enable = 1;
32 concat_refno = strtoul(optarg, 0, 0);
33 concat_refno_set = 1;
34 continue;
35 case 'u':
36 utf8_input = 1;
37 continue;
38 case 'U':
39 ucs2_mode = 1;
40 continue;
41 default:
42 /* error msg already printed */
43 exit(1);
44 }
45 }
46 if (argc > optind + 1) {
47 fprintf(stderr, "usage: %s [options] [message]\n", argv[0]);
48 exit(1);
49 }
50 if (argc < optind + 1)
51 return(0);
52 if (strlen(argv[optind]) > MAX_MSG_CHARS*2) {
53 fprintf(stderr, "error: message argument is too long\n");
54 exit(1);
55 }
56 strcpy(msgtext, argv[optind]);
57 return(1);
58 }
59
60 read_msgtext_from_stdin()
61 {
62 unsigned pos, remain;
63 int cc;
64
65 pos = 0;
66 remain = sizeof(msgtext);
67 for (;;) {
68 if (!remain) {
69 fprintf(stderr,
70 "error: message on stdin is too long\n");
71 exit(1);
72 }
73 cc = read(0, msgtext + pos, remain);
74 if (cc < 0) {
75 fprintf(stderr, "error reading message from stdin\n");
76 exit(1);
77 }
78 if (cc == 0)
79 break;
80 pos += cc;
81 remain -= cc;
82 }
83 msgtext[pos] = '\0';
84 }
85
86 trim_trailing_newlines()
87 {
88 char *cp;
89
90 cp = index(msgtext, '\0');
91 while (cp > msgtext && cp[-1] == '\n')
92 cp--;
93 *cp = '\0';
94 }
95
96 main(argc, argv)
97 char **argv;
98 {
99 if (!process_cmdline(argc, argv))
100 read_msgtext_from_stdin();
101 trim_trailing_newlines();
102 if (ucs2_mode)
103 ucs2_mode_main();
104 else
105 gsm7_mode_main();
106 }