1 /*******************************************************************/
2 /*  slibtool: a skinny libtool implementation, written in C        */
3 /*  Copyright (C) 2016--2018  Z. Gilboa                            */
4 /*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
5 /*******************************************************************/
6 
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <slibtool/slibtool.h>
10 
11 #include "slibtool_driver_impl.h"
12 #include "slibtool_dprintf_impl.h"
13 #include "slibtool_errinfo_impl.h"
14 
15 static const char aclr_null[]    = "";
16 static const char aclr_reset[]   = "\x1b[0m";
17 static const char aclr_bold[]    = "\x1b[1m";
18 
19 static const char aclr_green[]   = "\x1b[32m";
20 static const char aclr_blue[]    = "\x1b[34m";
21 static const char aclr_magenta[] = "\x1b[35m";
22 
slbt_output_exec_annotated(const struct slbt_driver_ctx * dctx,const struct slbt_exec_ctx * ectx,const char * step)23 static int slbt_output_exec_annotated(
24 	const struct slbt_driver_ctx *	dctx,
25 	const struct slbt_exec_ctx *	ectx,
26 	const char *			step)
27 {
28 	int          fdout;
29 	char **      parg;
30 	const char * aclr_set;
31 	const char * aclr_color;
32 	const char * aclr_unset;
33 
34 	fdout = slbt_driver_fdout(dctx);
35 
36 	if (slbt_dprintf(
37 			fdout,"%s%s%s: %s%s%s%s:%s",
38 			aclr_bold,aclr_magenta,
39 			dctx->program,aclr_reset,
40 			aclr_bold,aclr_green,step,aclr_reset) < 0)
41 		return SLBT_SYSTEM_ERROR(dctx,0);
42 
43 	for (parg=ectx->argv; *parg; parg++) {
44 		if ((parg == ectx->lout[0]) || (parg == ectx->mout[0])) {
45 			aclr_set      = aclr_bold;
46 			aclr_color    = aclr_blue;
47 			aclr_unset    = aclr_null;
48 		} else {
49 			aclr_set      = aclr_null;
50 			aclr_color    = aclr_null;
51 			aclr_unset    = aclr_reset;
52 		}
53 
54 		if (slbt_dprintf(
55 				fdout," %s%s%s%s",
56 				aclr_set,aclr_color,
57 				*parg,
58 				aclr_unset) < 0)
59 			return SLBT_SYSTEM_ERROR(dctx,0);
60 
61 	}
62 
63 	if (slbt_dprintf(fdout,"\n") < 0)
64 		return SLBT_SYSTEM_ERROR(dctx,0);
65 
66 	return 0;
67 }
68 
slbt_output_exec_plain(const struct slbt_driver_ctx * dctx,const struct slbt_exec_ctx * ectx,const char * step)69 static int slbt_output_exec_plain(
70 	const struct slbt_driver_ctx *	dctx,
71 	const struct slbt_exec_ctx *	ectx,
72 	const char *		step)
73 {
74 	int	fdout;
75 	char ** parg;
76 
77 	fdout = slbt_driver_fdout(dctx);
78 
79 	if (slbt_dprintf(fdout,"%s: %s:",dctx->program,step) < 0)
80 		return SLBT_SYSTEM_ERROR(dctx,0);
81 
82 	for (parg=ectx->argv; *parg; parg++)
83 		if (slbt_dprintf(fdout," %s",*parg) < 0)
84 			return SLBT_SYSTEM_ERROR(dctx,0);
85 
86 	if (slbt_dprintf(fdout,"\n") < 0)
87 		return SLBT_SYSTEM_ERROR(dctx,0);
88 
89 	return 0;
90 }
91 
slbt_output_exec(const struct slbt_driver_ctx * dctx,const struct slbt_exec_ctx * ectx,const char * step)92 int slbt_output_exec(
93 	const struct slbt_driver_ctx *	dctx,
94 	const struct slbt_exec_ctx *	ectx,
95 	const char *			step)
96 {
97 	int fdout = slbt_driver_fdout(dctx);
98 
99 	if (dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_NEVER)
100 		return slbt_output_exec_plain(dctx,ectx,step);
101 
102 	else if (dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_ALWAYS)
103 		return slbt_output_exec_annotated(dctx,ectx,step);
104 
105 	else if (isatty(fdout))
106 		return slbt_output_exec_annotated(dctx,ectx,step);
107 
108 	else
109 		return slbt_output_exec_plain(dctx,ectx,step);
110 }
111 
slbt_output_compile(const struct slbt_driver_ctx * dctx,const struct slbt_exec_ctx * ectx)112 int slbt_output_compile(
113 	const struct slbt_driver_ctx *	dctx,
114 	const struct slbt_exec_ctx *	ectx)
115 {
116 	return slbt_output_exec(dctx,ectx,"compile");
117 }
118 
slbt_output_execute(const struct slbt_driver_ctx * dctx,const struct slbt_exec_ctx * ectx)119 int slbt_output_execute(
120 	const struct slbt_driver_ctx *	dctx,
121 	const struct slbt_exec_ctx *	ectx)
122 {
123 	return slbt_output_exec(dctx,ectx,"execute");
124 }
125 
slbt_output_install(const struct slbt_driver_ctx * dctx,const struct slbt_exec_ctx * ectx)126 int slbt_output_install(
127 	const struct slbt_driver_ctx *	dctx,
128 	const struct slbt_exec_ctx *	ectx)
129 {
130 	return slbt_output_exec(dctx,ectx,"install");
131 }
132 
slbt_output_link(const struct slbt_driver_ctx * dctx,const struct slbt_exec_ctx * ectx)133 int slbt_output_link(
134 	const struct slbt_driver_ctx *	dctx,
135 	const struct slbt_exec_ctx *	ectx)
136 {
137 	return slbt_output_exec(dctx,ectx,"link");
138 }
139 
slbt_output_uninstall(const struct slbt_driver_ctx * dctx,const struct slbt_exec_ctx * ectx)140 int slbt_output_uninstall(
141 	const struct slbt_driver_ctx *	dctx,
142 	const struct slbt_exec_ctx *	ectx)
143 {
144 	return slbt_output_exec(dctx,ectx,"uninstall");
145 }
146