1: /*
2: * $Id: cmd_write.c,v 1.6 2025/04/09 19:52:02 snw Exp $
3: * Implementation of the WRITE command
4: *
5: *
6: * Author: Serena Willis <snw@coherent-logic.com>
7: * Copyright (C) 1998 MUG Deutschland
8: * Copyright (C) 2023 Coherent Logic Development LLC
9: *
10: *
11: * This file is part of FreeM.
12: *
13: * FreeM is free software: you can redistribute it and/or modify
14: * it under the terms of the GNU Affero Public License as published by
15: * the Free Software Foundation, either version 3 of the License, or
16: * (at your option) any later version.
17: *
18: * FreeM is distributed in the hope that it will be useful,
19: * but WITHOUT ANY WARRANTY; without even the implied warranty of
20: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21: * GNU Affero Public License for more details.
22: *
23: * You should have received a copy of the GNU Affero Public License
24: * along with FreeM. If not, see <https://www.gnu.org/licenses/>.
25: *
26: * $Log: cmd_write.c,v $
27: * Revision 1.6 2025/04/09 19:52:02 snw
28: * Eliminate as many warnings as possible while building with -Wall
29: *
30: * Revision 1.5 2025/03/24 04:05:36 snw
31: * Replace crlf with frm_crlf to avoid symbol conflict with readline on OS/2
32: *
33: * Revision 1.4 2025/03/22 18:43:54 snw
34: * Make STRLEN 255 chars and add BIGSTR macro for larger buffers
35: *
36: * Revision 1.3 2025/03/09 19:14:24 snw
37: * First phase of REUSE compliance and header reformat
38: *
39: *
40: * SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
41: * SPDX-License-Identifier: AGPL-3.0-or-later
42: **/
43:
44: #include <string.h>
45: #include <stdlib.h>
46: #include "mpsdef.h"
47: #include "mcommand.h"
48: #if !defined(MSDOS)
49: # include "io_socket.h"
50: #endif
51:
52: MRESULT cmd_write(MACTION *ra, int *i)
53: {
54: char vn[255];
55: char *wr_terminator = (char *) malloc (STRLEN * sizeof (char));
56: char *sw_buf = (char *) malloc (BIGSTR * sizeof (char));
57:
58: NULLPTRCHK(wr_terminator,"cmd_write");
59: NULLPTRCHK(sw_buf,"cmd_write");
60:
61: if (io > FIRSTSCK) msck_get_terminator (io, wr_terminator);
62:
63: if (io != HOME && devopen[io] == 'r' && io < FIRSTSCK) {
64: free (wr_terminator);
65: free (sw_buf);
66: return NOWRITE;
67: }
68:
69: if ((*i = (*codptr)) == SP || *i == EOL) {
70: free (wr_terminator);
71: free (sw_buf);
72: return ARGLIST;
73: }
74:
75: writeproc:
76:
77: switch (*i)
78: {
79:
80: case '!':
81:
82: if (io < FIRSTSCK) {
83:
84: if (frm_crlf[io]) {
85: write_m ("\012\201");
86: }
87: else {
88: write_m ("\015\012\201");
89: }
90:
91: }
92: else {
93: /* WRITE ! for sockets */
94: strcat (sw_buf, wr_terminator);
95: }
96:
97: break;
98:
99: case '#':
100:
101: if (io < FIRSTSCK) {
102: write_m ("\015\014\201");
103: }
104: else {
105: return SYNTERR;
106: }
107:
108: break;
109:
110: case '?':
111:
112: codptr++;
113:
114: expr (STRING);
115:
116: if (merr ()) return merr ();
117:
118: if (io < FIRSTSCK) {
119: write_t ((short) intexpr (argptr));
120: }
121: else {
122: return SYNTERR;
123: }
124:
125: break;
126:
127: case '/':
128: codptr++;
129:
130: expr (NAME);
131: if (merr ()) return merr ();
132:
133: if (io < FIRSTSCK) {
134: write_f (varnam);
135: }
136: else {
137: return SYNTERR;
138: }
139:
140: codptr++;
141:
142: break;
143:
144: case '*':
145: codptr++;
146:
147: expr (STRING);
148: if (merr ()) return merr ();
149:
150: argptr[0] = (char) UNSIGN (intexpr (argptr));
151: argptr[1] = EOL;
152:
153: /* special treatment for EOL as long as we don't have 8 bit */
154: if (argptr[0] == EOL) {
155:
156: mcmnd = '*';
157:
158: if (io < FIRSTSCK) {
159: m_output (argptr);
160: }
161: else {
162: /* EOL handling for socket devices */
163: }
164: mcmnd = WRITE;
165:
166: }
167: else {
168:
169: if (io < FIRSTSCK) {
170: write_m (argptr);
171: }
172: else {
173: /* non-EOL char-code handling for sockets */
174: }
175:
176: }
177:
178: break;
179:
180: default:
181: expr (STRING);
182: if (merr ()) return merr ();
183:
184: if (io < FIRSTSCK) {
185: write_m (argptr);
186: }
187: else {
188: /* regular output for socket devices */
189: stcpy (vn, argptr);
190: stcnv_m2c (vn);
191: strcat (sw_buf, vn);
192: }
193: }
194:
195: if ((*i == '!') || (*i == '#')) {
196: if ((*i = *++codptr) == '!' || *i == '#' || *i == '?') goto writeproc;
197: }
198:
199: if (io >= FIRSTSCK) {
200: msck_write (io, sw_buf, strlen (sw_buf));
201: }
202:
203: free (sw_buf);
204: free (wr_terminator);
205:
206: *ra = RA_CONTINUE;
207: return OK;
208: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>